题目描述:
Balls In The Box
Time limit: 1s Memory limit: 32768 kb
Problem Description
There are N boxes in Staginner’s house, and we mark them by 1,2,…,N. There are Ni(1<=Ni<=10^4) balls in the box i initially. Now Staginner will do some operations, and he will ask you some simple questions.
The operations contain:
1. C i j : take all the balls in the box i into the box j and throw the box i away, you can sure that i is different from j.
2. A i n : add n(1<=n<=10^4) balls into the box i.
3. B i n : take n(n>=0) balls away from box i, you can sure that n is not bigger than the number of balls in the box i.
4. Q k : Staginner ask you for the k-th smallest number of balls in the box, you can sure that k is not bigger than the total number of boxes.
Input:
There are several test cases.
The first line of each case contains two integers, N(1<=N<=10^5), M(1<=M<=10^5), indicates the number of boxes and the number of operations. The next line contains N integers, the i-th integer indicates the number of balls in the box i. Then there will be M lines, each line has an operation like someone of the operations in the description.
Output:
For each test case, you need only print one integer for each operation “Q k”, indicates the k-th smallest number of balls in the box.
Sample Input:
3 7
1 2 3
A 3 1
C 2 1
Q 1
B 3 4
Q 1
C 3 1
Q 1
Sample Output:
3
0
3
题目意思很简单,给定N个盒子和M个操作,然后给出初始时每个盒子里球的个数,接下来时M个操作,操作分为四种,
A a b :把编号为a的盒子里的球的数量增加b
B a b :把编号为a的盒子里的球的数量减少b
C a b :把编号为a的盒子里的球全倒进编号为b的盒子里,然后扔掉盒子a
Q k :查询球数第k小的盒子,对于每一个Q输出一行为该盒子里的球数
思路,如果用数组直接存,A B C 三个操作都容易实现,复杂度为O(1),然而Q操作每次都会花费O(n)的时间,n达到10^5,而且有10^5个询问,给定时限只有1s,肯定超时,于是想到用平衡二叉树来动态维护这些盒子,这样A B C D的时间复杂度都为O(logn),应该不会超时。
郁结的过程:比赛当时由于SBT不会写。。AVL写的不熟,然后果断放弃了该题,比赛之后弄到了题目和数据,自己在本地慢慢的搞,起初只用盒子里的球数来作为关键码,这样对于球数相同的情况是不能够处理了(经过旋转会破坏BST的性质),然后想到用盒子的球数和盒子的编号一起做关键码(球数为第一关键码,编号为第二关键码),这样就能够保证所有的关键码都不相同,于是终于AC了。 总算是把AVL写的比较熟练了。虽然代码风格比较挫。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 100010;
//关键码结点类
struct Node
{
int wgt;
int num;
Node(int w=0,int n=0):wgt(w),num(n){}
bool operator < (const Node &n) const
{
if(wgt!=n.wgt)return wgt<n.wgt;
else return num<n.num;
}
bool operator == (const Node &n) const
{
return wgt==n.wgt&&num==n.num;
}
};
//AVL树
struct AVLTree
{
int l[MAXN],r[MAXN],size[MAXN],next[MAXN],h[MAXN],ROOT;
Node Key[MAXN];
int newnode(int w,int n)
{
int node = next[0];
Key[node].wgt = w;
Key[node].num = n;
next[0] = next[node];
h[node] = size[node] = 1;
return node;
}
void delnode(int x)
{
next[x] = next[0];
next[0] = x;
Key[x].wgt = Key[x].num = h[x] = size[x] = l[x] = r[x] = 0;
}
void init()
{
for(int i=0;i<MAXN;i++)next[i]=i+1;
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
memset(size,0,sizeof(size));
memset(h,0,sizeof(h));
ROOT = 0;
}
void left_rotate(int &p)
{
int x = r[p];
r[p] = l[x];
l[x] = p;
size[p] = size[l[p]]+size[r[p]]+1;
size[x] = size[l[x]]+size[r[x]]+1;
h[p] = max(h[l[p]],h[r[p]])+1;
h[x] = max(h[l[x]],h[r[x]])+1;
p = x;
}
void right_rotate(int &p)
{
int x = l[p];
l[p] = r[x];
r[x] = p;
size[p] = size[l[p]]+size[r[p]]+1;
size[x] = size[l[x]]+size[r[x]]+1;
h[p] = max(h[l[p]],h[r[p]])+1;
h[x] = max(h[l[x]],h[r[x]])+1;
p = x;
}
void insert(int &p,Node k)
{
if(!p)
{
p = newnode(k.wgt,k.num);
return;
}
if(k<Key[p])insert(l[p],k);
else insert(r[p],k);
h[p] = max(h[l[p]],h[r[p]])+1;
size[p] = size[l[p]]+size[r[p]]+1;
if(h[l[p]]-h[r[p]]==2)//L
{
if(h[l[l[p]]]>h[r[l[p]]])//LL
{
right_rotate(p);
}
else
{
left_rotate(l[p]);
right_rotate(p);
}
}
else if(h[l[p]]-h[r[p]]==-2)//R
{
if(h[r[r[p]]]>h[l[r[p]]])//RR
{
left_rotate(p);
}
else
{
right_rotate(r[p]);
left_rotate(p);
}
}
}
int findmin(int &p)
{
if(l[p])return findmin(l[p]);
return p;
}
void remove(int &p,Node k)
{
if(p)
{
if(k==Key[p])
{
if(!l[p])
{
int x = p;
p = r[p];
delnode(x);
}
else if(!r[p])
{
int x = p;
p = l[p];
delnode(x);
}
else
{
int x = findmin(r[p]);
Key[p] = Key[x];
remove(r[p],Key[x]);
}
}
else if(k<Key[p])remove(l[p],k);
else remove(r[p],k);
if(p)
{
h[p] = max(h[l[p]],h[r[p]])+1;
size[p] = size[l[p]]+size[r[p]]+1;
}
if(h[l[p]]-h[r[p]]==2)//L
{
if(h[l[l[p]]]>h[r[l[p]]])//LL
{
right_rotate(p);
}
else
{
left_rotate(l[p]);
right_rotate(p);
}
}
else if(h[l[p]]-h[r[p]]==-2)//R
{
if(h[r[r[p]]]>h[l[r[p]]])//RR
{
left_rotate(p);
}
else
{
right_rotate(r[p]);
left_rotate(p);
}
}
}
}
int Select(int p,int k)
{
int rank = size[l[p]]+1;
if(rank==k)return p;
else if(rank<k) return Select(r[p],k-rank);
return Select(l[p],k);
}
}avl;
int a[MAXN];
int main()
{
int n,m;
freopen("D.in","r",stdin);
freopen("DansK2.out","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF)
{
avl.init();
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)avl.insert(avl.ROOT,Node(a[i],i));
for(int i=0;i<m;i++)
{
char op[5];int x,y;
scanf("%s",op);
if(op[0]=='A')
{
scanf("%d%d",&x,&y);
avl.remove(avl.ROOT,Node(a[x],x));
a[x]+=y;
avl.insert(avl.ROOT,Node(a[x],x));
}
else if(op[0]=='B')
{
scanf("%d%d",&x,&y);
avl.remove(avl.ROOT,Node(a[x],x));
a[x]-=y;
avl.insert(avl.ROOT,Node(a[x],x));
}
else if(op[0]=='C')
{
scanf("%d%d",&x,&y);
avl.remove(avl.ROOT,Node(a[x],x));
avl.remove(avl.ROOT,Node(a[y],y));
a[y]+=a[x];
avl.insert(avl.ROOT,Node(a[y],y));
}
else
{
scanf("%d",&x);
printf("%d\n",avl.Key[avl.Select(avl.ROOT,x)].wgt);
}
}
}
return 0;
}