BZOJ3295/Luogu3157 [CQOI2011]动态逆序对 (CDQ or 树套树 )

博客提到另一种方法是使用BIT(二进制索引树)来处理值的线段树,并给出了转载来源。
/*
Dear friend, wanna learn CDQ?
As a surprice, this code is totally wrong.
You may ask, then why you put it on your blog, are you fucking crazy with the fans, isn't it clear you do not have one.
Well, I just found a anime picture in other's blog.
I love it, so I wanna put it on my blog, but, if I waste time for a picture, I'll feel that I'm a shame.jpg!.
So I cheated to myself that I just use this picture to decorate my code.
Good reason, isn't it.

*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ (a))
#define nR(a,b,c) for(register int a = (b); a >= (c); -- (a))
#define ll long long
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n---------------------\n\n");

#else

#define D_e_Line ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x *= f;
        return *this;
    }
}io;

using namespace std;

const int N = 100007;

int n,m;
int tim;

struct Element{
    int a,b,c;
    long long ans;
    bool operator< (const Element &com)const{
        if(a != com.a) return a < com.a;
        if(b != com.b) return b > com.b;
        return c < com.c;
    }
}a[N],tmp[N];

long long t[N];
inline void Updata(int x, int w){
    for(; x <= tim; x += x&-x) t[x] += w;
}
inline long long Query(int x){
    long long s = 0;
    for(; x; x -= x&-x) s += t[x];
    return s;
}

inline void CDQ(int l,int r){
    if(l == r) return;
    int mid = (l + r) >> 1;
    CDQ(l, mid), CDQ(mid + 1, r); 
    int i = l, j = mid + 1, k = l;
    while(i <= mid && j <= r){
        if(a[i].b >= a[j].b){
            Updata(a[i].c, 1);
            tmp[k++] = a[i++];
        }
        else{
            a[j].ans += Query(a[j].c);
            tmp[k++] = a[j++];
        }
    }
    while(j <= r) a[j].ans += Query(a[j].c), tmp[k++] = a[j++];
    R(j, l, i - 1) Updata(a[j].c, -1);
    while(i <= mid) tmp[k++] = a[i++];
    R(i,l,r) a[i] = tmp[i];
}

long long tot[N];

int main(){
    io >> n >> m;
    
    tim = 1;
    
    R(i,1,n){
        io >> a[i].b;
        a[i].a = i;
        a[i].c = tim;
    }
    
    R(i,1,m){
        int x;
        io >> x;
        a[x].c = ++tim;
    }
    
    sort(a + 1, a + n + 1);
    
    CDQ(1, n);
    
    R(i,1,n){
        tot[a[i].c] += a[i].ans;
        //cout<<"$$"<<a[i].ans<<endl;
    }
    
    R(i,2,tim){
        printf("%lld\n", tot[i]);
    }
    
    return 0;
}

1570282-20190713212135556-1991028467.png

Another way is to use BIT to work with segment tree of value.

#define lson t[rt].l, l, mid
#define rson t[rt].r, mid + 1, r
struct SegmentTree{
    int l,r;
    int siz;
}t[N*100];
int treeIndex;
inline void Pushup(int &rt){//pointer, pointer, once again! pointer!!!!@!$#$!@%$!@%!@^!$#&*$(&
    t[rt].siz = t[t[rt].l].siz + t[t[rt].r].siz;
}
inline void SegModify(int &rt,int l,int r,int x,int val){//be careful, rt is a pointer for rt[]
    if(!rt) rt = ++treeIndex;
    if(l == r){
        t[rt].siz += val;
        return;
    }
    int mid = (l + r) >> 1;
    if(x <= mid)
        SegModify(lson, x, val);
    else
        SegModify(rson, x, val);
    Pushup(rt);
}
inline int SegQuery(int rt,int l,int r,int L,int R){
    if(!rt || L > R) return 0;
    if(L <= l && r <= R) return t[rt].siz;
    int mid = (l + r) >> 1, sum = 0;
    if(L <= mid) sum += SegQuery(lson, L, R);
    if(R > mid) sum += SegQuery(rson, L, R);
    return sum;
}

int rt[N];
inline void Updata(int x,int w,int val){
    for(; x <= n; x += x&-x) SegModify(rt[x], 1, n, w, val);
}
inline int Query(int x,int w){
    int s = 0;
    for(; x; x -= x&-x) s += SegQuery(rt[x], 1, n, 1, w);
    return s;
}

int pos[N];
int a[N];

int main(){
//  freopen("IN.txt","r",stdin);
//  freopen("OUT.txt","w",stdout);
    int m;
    io >> n >> m;
    R(i,1,n){
        io >> a[i];
        Updata(i, a[i], 1);
        pos[a[i]] = i;
    }
    
    long long ans = 0;
    
    R(i,1,n){
        ans += Query(i - 1, n) - Query(i - 1, a[i]);//QAQ
    }
    
    while(m--){
        int x;
        io >> x;
        printf("%lld\n", ans);
        ans -= Query(pos[x] - 1, n) - Query(pos[x] - 1, x) + Query(n, x - 1) - Query(pos[x], x - 1);//QTAQ
        Updata(pos[x], x, -1);
    }
    
    return 0;
}

转载于:https://www.cnblogs.com/bingoyes/p/11182054.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值