BZOJ3295/Luogu3157 [CQOI2011]动态逆序对 (CDQ or 树套树 )
阅读原文时间:2023年07月08日阅读:1
/*
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;
}

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;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章