【BZOJ 2141】排队【分块+Treap】
2017-02-28 16:45
363 查看
Description
排排坐,吃果果,生果甜嗦嗦,大家笑呵呵。你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和。红星幼儿园的小朋友们排起了长长地队伍,准备吃果果。不过因为小朋友们的身高有所区别,排成的队伍高低错乱,极不美观。设第i个小朋友的身高为hi,我们定义一个序列的杂乱程度为:满足ihj的(i,j)数量。幼儿园阿姨每次会选出两个小朋友,交换他们的位置,请你帮忙计算出每次交换后,序列的杂乱程度。为方便幼儿园阿姨统计,在未进行任何交换操作时,你也应该输出该序列的杂乱程度。Input
第一行为一个正整数n,表示小朋友的数量;第二行包含n个由空格分隔的正整数h1,h2,…,hn,依次表示初始队列中小朋友的身高;第三行为一个正整数m,表示交换操作的次数;以下m行每行包含两个正整数ai和bi¬,表示交换位置ai与位置bi的小朋友。Output
输出文件共m行,第i行一个正整数表示交换操作i结束后,序列的杂乱程度。Sample Input
【样例输入】3
130 150 140
2
2 3
1 3
Sample Output
10
3
【样例说明】
未进行任何操作时,(2,3)满足条件;
操作1结束后,序列为130 140 150,不存在满足ihj的(i,j)对;
操作2结束后,序列为150 140 130,(1,2),(1,3),(2,3)共3对满足条件的(i,j)。
【数据规模和约定】
对于100%的数据,1≤m≤2*103,1≤n≤2*104,1≤hi≤109,ai≠bi,1≤ai,bi≤n。
题解
先分块,在对每一个块写一个Treap,维护每个Treap即可。交换位置时只需要扫描他们之间的数和块即可。
详细可见我通俗易懂的代码。
代码
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> using namespace std; #define N 20010 struct node{ node *ch[2]; int r,v,s,ss; node(int v):v(v){ch[0]=ch[1]=NULL;r = rand();s = 1;ss = 1;} int cmp(int x) { if(x == v) return -1; else return x > v ? 1 : 0; } void maintain() { s = ss; if(ch[1]) s += ch[1]->s; if(ch[0]) s += ch[0]->s; } }*root[150]; void rotate(node* &o,int d) { node*k = o->ch[d^1]; o->ch[d^1] = k->ch[d]; k->ch[d] = o; o->maintain();k->maintain(); o = k; } void insert(node* &o,int x) { if(o == NULL) o = new node(x); else { int d = o->cmp(x); if(d == -1) {o->ss++; o->s++;} else { insert(o->ch[d],x); if(o->ch[d]->r > o->r) rotate(o,d^1); } } o->maintain(); } void remove(node* &o,int x) { int d = o->cmp(x); if(d == -1) { if(o->ss > 1){o->ss--;o->s--;return;} node* u = o; if(o->ch[0] && o->ch[1]) { int d2 = (o->ch[0]->r > o->ch[1]->r ? 1 : 0); rotate(o,d2); remove(o->ch[d2],x); } else { if(o->ch[0] == NULL) o = o->ch[1]; else o = o->ch[0]; delete u; } } else remove(o->ch[d],x); if(o)o->maintain(); } int find(node *o,int x)//查询一个块中比x大的数有多少个 { int ans = 0; while(o != NULL) { int d = o->cmp(x); if(d == -1) return ans + (o->ch[1] ? o->ch[1]->s : 0); if(d == 0 && o->ch[1]) ans += o->ch[1]->s; if(d == 0) ans += o->ss; o = o->ch[d]; } return ans; } int findsmall(node *o,int x)//查询一个块中比x小的数有多少个 { int ans = 0; while(o != NULL) { int d = o->cmp(x); if(d == -1) return ans + (o->ch[0] ? o->ch[0]->s : 0); if(d == 1 && o->ch[0]) ans += o->ch[0]->s; if(d == 1) ans += o->ss; o = o->ch[d]; } return ans; } int n,blen,cnt,x,y,m; int l[150],r[150],belong ,h ; int main() { scanf("%d",&n); blen = sqrt(n); cnt = n / blen + (n % blen ? 1 : 0); for(int i = 1;i <= cnt;i++) { l[i] = (i-1)*blen+1; r[i] = i * blen; } r[cnt] = n; for(int i = 1;i <= n;i++) belong[i] = (i-1)/blen + 1; //belong判断i在第几个块中 int ans = 0; for(int i = 1;i <= n;i++) { scanf("%d",&h[i]); insert(root[ belong[i] ],h[i]); for(int j = 1;j <= belong[i];j++) ans += find(root[j],h[i]); } printf("%d\n",ans); scanf("%d",&m); for(int i = 1;i <= m;i++) { scanf("%d%d",&x,&y); if(x > y) swap(x,y); if(x == y || h[x] == h[y]) {printf("%d\n",ans);continue;} int a = belong[x],b = belong[y]; if(a == b) { for(int j = x+1;j < y;j++) { if(h[x] > h[j]) ans--; if(h[x] < h[j]) ans++; if(h[y] > h[j]) ans++; if(h[y] < h[j]) ans--; } if(h[x] < h[y]) ans++; if(h[x] > h[y]) ans--; printf("%d\n",ans); swap(h[x],h[y]); continue; } for(int j = x+1;j <= r[a];j++) { if(h[x] > h[j]) ans--; if(h[x] < h[j]) ans++; if(h[y] > h[j]) ans++; if(h[y] < h[j]) ans--; } for(int j = l[b];j <= y-1;j++) { if(h[x] > h[j]) ans--; if(h[x] < h[j]) ans++; if(h[y] > h[j]) ans++; if(h[y] < h[j]) ans--; } for(int j = a+1;j <= b-1;j++) { ans -= findsmall(root[j],h[x]); ans += find(root[j],h[x]); ans += findsmall(root[j],h[y]); ans -= find(root[j],h[y]); } if(h[x] < h[y]) ans++; if(h[x] > h[y]) ans--; printf("%d\n",ans); remove(root[a],h[x]);insert(root[a],h[y]); remove(root[b],h[y]);insert(root[b],h[x]); swap(h[x],h[y]);//一定要记得交换位置 } return 0; }
相关文章推荐
- BZOJ_2141_排队_树状数组+分块
- bzoj2141 排队 分块
- [BZOJ] 2141 - Atlantis - 排队 - 树状数组求逆序对 - 分块求区间比 k 小
- BZOJ - 2141 排队(树状数组套treap||分块)
- BZOJ2141 排队(分块)
- Bzoj 2141: 排队 分块,逆序对,树状数组
- [BZOJ2141]排队(分块+树状数组求逆序对)
- bzoj 2141 排队 【分块】
- [BZOJ]2141: 排队 分块+树状数组
- 【bzoj2141】排队 分块+树状数组
- [BZOJ2141]排队(分块)
- BZOJ 2141 排队 分块+树状数组
- bzoj 2141 : 排队 分块
- BZOJ 2141 排队 分块+树状数组,详细题解
- BZOJ 2141排队(树状数组套Treap)
- BZOJ 2141 排队 分块
- bzoj2141: 排队(树套树 或 分块)
- bzoj2141: 排队(分块+树状数组)
- 【BZOJ2141】排队 树状数组+分块
- 【分块】【树套树】bzoj2141 排队