对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数。给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序对数。
标签:
题解:
CDQ分治做法暂时还没想到。。。树套树做法比较直观就直接上树套树吧。
刚开始sb的写了两次查询,分别查找前面比它大的和后面比它小的。。。然后就11s卡着过了。。。货真价实的(倒)rank1。。。
1 #include<cstdio> 2 3 #include<cstdlib> 4 5 #include<cmath> 6 7 #include<cstring> 8 9 #include<algorithm> 10 11 #include<iostream> 12 13 #include<vector> 14 15 #include<map> 16 17 #include<set> 18 19 #include<queue> 20 21 #include<string> 22 23 #define inf 1000000000 24 25 #define maxn 100000+5 26 27 #define maxm 4000000+5 28 29 #define eps 1e-10 30 31 #define ll long long 32 33 #define pa pair<int,int> 34 35 #define for0(i,n) for(int i=0;i<=(n);i++) 36 37 #define for1(i,n) for(int i=1;i<=(n);i++) 38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 42 43 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 44 45 #define for5(n,m) for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) 46 47 #define mod 1000000007 48 49 using namespace std; 50 51 inline int read() 52 53 { 54 55 int x=0,f=1;char ch=getchar(); 56 57 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 58 59 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 60 61 return x*f; 62 63 } 64 int n,m,tot,a[maxn],b[maxn],c[maxn],d[maxn],sa[maxn]; 65 ll ans[maxn]; 66 int s[maxm],rt[maxn],v[maxm],rnd[maxm],l[maxm],r[maxm]; 67 inline void add(int x){for(;x<=n;x+=x&(-x))d[x]++;} 68 inline int sum(int x){int t=0;for(;x;x-=x&(-x))t+=d[x];return t;} 69 inline void pushup(int k) 70 { 71 s[k]=s[l[k]]+s[r[k]]+1; 72 } 73 inline void lturn(int &k) 74 { 75 int t=r[k];r[k]=l[t];l[t]=k;pushup(k);pushup(t);k=t; 76 } 77 inline void rturn(int &k) 78 { 79 int t=l[k];l[k]=r[t];r[t]=k;pushup(k);pushup(t);k=t; 80 } 81 inline void insert(int &k,int x) 82 { 83 if(!k) 84 { 85 k=++tot;s[k]=1;v[k]=x;rnd[k]=rand();l[k]=r[k]=0; 86 return; 87 } 88 s[k]++; 89 if(x<v[k]){insert(l[k],x);if(rnd[k]<rnd[l[k]])rturn(k);} 90 else {insert(r[k],x);if(rnd[k]<rnd[r[k]])lturn(k);} 91 } 92 inline int rank(int k,int x) 93 { 94 if(!k)return 0; 95 if(x<v[k])return rank(l[k],x); 96 else return s[l[k]]+1+rank(r[k],x); 97 } 98 inline void add(int x,int y) 99 { 100 for(;x<=n;x+=x&(-x))insert(rt[x],y); 101 } 102 inline int query1(int x,int y) 103 { 104 int t=0; 105 for(;x;x-=x&(-x))t+=s[rt[x]]-rank(rt[x],y); 106 return t; 107 } 108 inline int query2(int x,int y) 109 { 110 int t=0; 111 for(;x;x-=x&(-x))t+=rank(rt[x],y); 112 return t; 113 } 114 115 int main() 116 117 { 118 119 freopen("input.txt","r",stdin); 120 121 freopen("output.txt","w",stdout); 122 123 n=read();m=read()-1; 124 for1(i,n)a[i]=read(),sa[a[i]]=i; 125 for1(i,m)b[i]=sa[read()],c[b[i]]=1; 126 for1(i,n)if(!c[i]) 127 { 128 ans[m+1]+=(ll)(sum(n)-sum(a[i])); 129 add(a[i]); 130 add(i,a[i]); 131 } 132 for3(i,m,1) 133 { 134 ans[i]+=(ll)query1(b[i],a[b[i]]); 135 add(b[i],a[b[i]]); 136 } 137 memset(rt,0,sizeof(rt));tot=0; 138 for1(i,n>>1)swap(a[i],a[n+1-i]); 139 for1(i,n)if(!c[n+1-i])add(i,a[i]); 140 for3(i,m,1) 141 { 142 b[i]=n+1-b[i]; 143 ans[i]+=(ll)query2(b[i],a[b[i]]); 144 add(b[i],a[b[i]]); 145 } 146 for3(i,m,1)ans[i]+=ans[i+1]; 147 for1(i,m+1)printf("%lld\n",ans[i]); 148 149 return 0; 150 151 }
然后我机智的改成了1次。。。
1 #include<cstdio> 2 3 #include<cstdlib> 4 5 #include<cmath> 6 7 #include<cstring> 8 9 #include<algorithm> 10 11 #include<iostream> 12 13 #include<vector> 14 15 #include<map> 16 17 #include<set> 18 19 #include<queue> 20 21 #include<string> 22 23 #define inf 1000000000 24 25 #define maxn 100000+5 26 27 #define maxm 4000000+5 28 29 #define eps 1e-10 30 31 #define ll long long 32 33 #define pa pair<int,int> 34 35 #define for0(i,n) for(int i=0;i<=(n);i++) 36 37 #define for1(i,n) for(int i=1;i<=(n);i++) 38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 42 43 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 44 45 #define for5(n,m) for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) 46 47 #define mod 1000000007 48 49 using namespace std; 50 51 inline int read() 52 53 { 54 55 int x=0,f=1;char ch=getchar(); 56 57 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 58 59 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 60 61 return x*f; 62 63 } 64 int n,m,tot,a[maxn],b[maxn],c[maxn],d[maxn],sa[maxn]; 65 ll ans[maxn]; 66 int s[maxm],rt[maxn],v[maxm],rnd[maxm],l[maxm],r[maxm]; 67 inline void add(int x){for(;x<=n;x+=x&(-x))d[x]++;} 68 inline int sum(int x){int t=0;for(;x;x-=x&(-x))t+=d[x];return t;} 69 inline void pushup(int k) 70 { 71 s[k]=s[l[k]]+s[r[k]]+1; 72 } 73 inline void lturn(int &k) 74 { 75 int t=r[k];r[k]=l[t];l[t]=k;pushup(k);pushup(t);k=t; 76 } 77 inline void rturn(int &k) 78 { 79 int t=l[k];l[k]=r[t];r[t]=k;pushup(k);pushup(t);k=t; 80 } 81 inline void insert(int &k,int x) 82 { 83 if(!k) 84 { 85 k=++tot;s[k]=1;v[k]=x;rnd[k]=rand();l[k]=r[k]=0; 86 return; 87 } 88 s[k]++; 89 if(x<v[k]){insert(l[k],x);if(rnd[k]<rnd[l[k]])rturn(k);} 90 else {insert(r[k],x);if(rnd[k]<rnd[r[k]])lturn(k);} 91 } 92 inline int rank(int k,int x) 93 { 94 if(!k)return 0; 95 if(x<v[k])return rank(l[k],x); 96 else return s[l[k]]+1+rank(r[k],x); 97 } 98 inline void add(int x,int y) 99 { 100 for(;x<=n;x+=x&(-x))insert(rt[x],y); 101 } 102 inline int query1(int x,int y) 103 { 104 int t=0; 105 for(;x;x-=x&(-x))t+=s[rt[x]]-rank(rt[x],y); 106 return t; 107 } 108 inline int query2(int x,int y) 109 { 110 int t=0; 111 for(;x;x-=x&(-x))t+=rank(rt[x],y); 112 return t; 113 } 114 115 int main() 116 117 { 118 119 freopen("input.txt","r",stdin); 120 121 freopen("output.txt","w",stdout); 122 123 n=read();m=read()-1; 124 for1(i,n)a[i]=read(),sa[a[i]]=i; 125 for1(i,m)b[i]=sa[read()],c[b[i]]=1; 126 for1(i,n)if(!c[i]) 127 { 128 ans[m+1]+=(ll)(sum(n)-sum(a[i])); 129 add(a[i]); 130 add(i,a[i]); 131 } 132 for3(i,m,1) 133 { 134 ans[i]+=(ll)(query1(b[i],a[b[i]])+query2(n,a[b[i]])-query2(b[i],a[b[i]])); 135 add(b[i],a[b[i]]); 136 } 137 for3(i,m,1)ans[i]+=ans[i+1]; 138 for1(i,m+1)printf("%lld\n",ans[i]); 139 140 return 0; 141 142 }
N<=100000 M<=50000
标签:
原文地址:http://www.cnblogs.com/zyfzyf/p/4241413.html