zoukankan      html  css  js  c++  java
  • bzoj3295[Cqoi2011]动态逆序对

    传送门

    cdq模板

    //Achen
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<vector>
    #include<cstdio>
    #include<queue>
    #include<cmath>
    const int N=100007;
    typedef long long LL;
    using namespace std;
    int n,m,a[N],b[N],pos[N],tot,ans[N],sum[N];
    LL now,res[N];
    
    template<typename T>void read(T &x) {
        char ch=getchar(); T f=1; x=0;
        while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
        if(ch=='-') f=-1,ch=getchar();
        for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    }
    
    struct node {
        int ti,x,y;
        node(){}
        node(int ti,int x,int y):ti(ti),x(x),y(y){}
        friend bool operator <(const node &A,const node &B) {
            return A.x<B.x;
        }
    }p[N],tp[N];
    
    void add(int x,int y) {
        for(int i=x;i<=n;i+=(i&(-i)))
            sum[i]+=y;
    }
    
    int qry(int x) {
        int res=0;
        for(int i=x;i;i-=(i&(-i))) 
            res+=sum[i];
        return res;
    }
    
    void cdq(int l,int r) {
        if(r<=l) return;
        int mid=((l+r)>>1),ql=l-1,qr=mid;
        for(int i=l;i<=r;i++) {
            if(p[i].ti<=mid) {
                tp[++ql]=p[i];
                add(n-p[i].y+1,1);
            }
            else {
                tp[++qr]=p[i];
                ans[p[i].ti]+=qry(n-p[i].y+1);
            }
        }
        for(int i=l;i<=ql;i++) 
            add(n-tp[i].y+1,-1);
        for(int i=l;i<=r;i++) p[i]=tp[i];
        cdq(l,mid); cdq(mid+1,r);
    }
    
    int main() {
    #ifdef orzllj
        freopen(".in","r",stdin);
        freopen(".out","w",stdout);
    #endif
        read(n); read(m);
        for(int i=1;i<=n;i++) {
            read(a[i]);
            pos[a[i]]=i;
        }
        for(int i=1;i<=m;i++) {
            read(b[i]); 
            a[pos[b[i]]]=0;
        }
        for(int i=1;i<=n;i++) 
            if(a[i]) p[++tot]=node(tot,i,a[i]);
        for(int i=m;i>=1;i--) 
            p[++tot]=node(tot,pos[b[i]],b[i]);
        sort(p+1,p+tot+1);
        cdq(1,n);
        for(int i=1;i<=tot;i++) p[i].x=n-p[i].x+1,p[i].y=n-p[i].y+1;
        sort(p+1,p+tot+1);
        cdq(1,n);
        int cc=m;
        for(int i=1;i<=n;i++) {
            now+=ans[i];
            if(i>n-m) res[cc--]=now;
        }
        for(int i=1;i<=m;i++) { 
            printf("%lld
    ",res[i]);
        }
        return 0;
    }
    /*
    5 4
    1 5 3 4 2
    5 1 4 2
    */
    View Code
  • 相关阅读:
    行业动态 | Instagram: 从Redis到Cassandra成功节省75%的成本
    技术基础 | 有关K8ssandra的那些事儿
    技术基础 | 重要指标和告警
    Cassandra与职业发展 | 阿里云栾小凡 × 蔚来汽车张旭东 × 网龙阙乃祯
    Cassandra与Kubernetes
    为何选择云原生?
    区分NoSQL数据库
    什么是NoSQL
    JMeter学习(一)JMeter的安装和目录解析
    CentOS 7 nfs客户端挂载问题
  • 原文地址:https://www.cnblogs.com/Achenchen/p/8073979.html
Copyright © 2011-2022 走看看