poj1442&poj2892
Treap入门题,非旋转Treap练习用。
Black Box
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8133 | Accepted: 3327 |
Description
ADD (x): put element x into Black Box;
GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that i-minimum is a number located at i-th place after Black Box elements sorting by non- descending.
Let us examine a possible sequence of 11 transactions:
Example 1
N Transaction i Black Box contents after transaction Answer (elements are arranged by non-descending) 1 ADD(3) 0 3 2 GET 1 3 3 3 ADD(1) 1 1, 3 4 GET 2 1, 3 3 5 ADD(-4) 2 -4, 1, 3 6 ADD(2) 2 -4, 1, 2, 3 7 ADD(8) 2 -4, 1, 2, 3, 8 8 ADD(-1000) 2 -1000, -4, 1, 2, 3, 8 9 GET 3 -1000, -4, 1, 2, 3, 8 1 10 GET 4 -1000, -4, 1, 2, 3, 8 2 11 ADD(2) 4 -1000, -4, 1, 2, 2, 3, 8
It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and GET transactions: 30000 of each type.
Let us describe the sequence of transactions by two integer arrays:
1. A(1), A(2), ..., A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 by their absolute value, M <= 30000. For the Example we have A=(3, 1, -4, 2, 8, -1000, 2).
2. u(1), u(2), ..., u(N): a sequence setting a number of elements which are being included into Black Box at the moment of first, second, ... and N-transaction GET. For the Example we have u=(1, 2, 6, 6).
The Black Box algorithm supposes that natural number sequence u(1), u(2), ..., u(N) is sorted in non-descending order, N <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M is valid. It follows from the fact that for the p-element of our u sequence we perform a GET transaction giving p-minimum number from our A(1), A(2), ..., A(u(p)) sequence.
Input
Output
Sample Input
7 4 3 1 -4 2 8 -1000 2 1 2 6 6
Sample Output
3 3 1 2
Source
/* Problem: 1442 User: Childe Memory: 1712K Time: 469MS Language: C++ Result: Accepted */ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #define N 2000005 using namespace std; inline int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x*f; } struct Treap{ Treap *l,*r; int fix,key,size; Treap(int key_):fix(rand()),key(key_),l(NULL),r(NULL),size(1){ } inline void update(){ size=1+(l?l->size:0)+(r?r->size:0); } }*root; typedef pair<Treap*,Treap*>Droot; inline int Size(Treap *x){ return x?x->size:0; } Droot Split(Treap *x,int k){ if(!x)return Droot(NULL,NULL); Droot y; if(Size(x->l)>=k){ y=Split(x->l,k); x->l=y.second; x->update(); y.second=x; } else{ y=Split(x->r,k-Size(x->l)-1); x->r=y.first; x->update(); y.first=x; } return y; } Treap *Merge(Treap *A,Treap *B){ if(!A)return B; if(!B)return A; if(A->fix<B->fix){ A->r=Merge(A->r,B); A->update(); return A; } else{ B->l=Merge(A,B->l); B->update(); return B; } } int Getkth(Treap *x,int v){ if(!x)return 0; return v<x->key?Getkth(x->l,v):Getkth(x->r,v)+Size(x->l)+1; } void Insert(int v){ int k=Getkth(root,v); Droot x=Split(root,k); Treap *n=new Treap(v); root=Merge(Merge(x.first,n),x.second); } void Delete(int k){ Droot x=Split(root,k-1); Droot y=Split(x.second,1); root=Merge(x.first,y.second); } int Findkth(int k){ Droot x=Split(root,k-1); Droot y=Split(x.second,1); Treap *ans=y.first; root=Merge(Merge(x.first,ans),y.second); return ans->key; } int n,m; int s[N]; int p,c,inc; int main() { c = 0; scanf("%d %d", &m, &n); for(int i = 0;i < m;i++) scanf("%d", s+i); for(int i = 0;i < n;i++) { scanf("%d", &p); while(c < p) Insert(s[c++]); printf("%d\n", Findkth(++inc)); } return 0; }
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 7178 | Accepted: 2948 |
Description
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.
Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
Input
The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.
There are three different events described in different format shown below:
- D x: The x-th village was destroyed.
- Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
- R: The village destroyed last was rebuilt.
Output
Output the answer to each of the Army commanders’ request in order on a separate line.
Sample Input
7 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4
Sample Output
1 0 2 4
Hint
An illustration of the sample input:
OOOOOOO D 3 OOXOOOO D 6 OOXOOXO D 5 OOXOXXO R OOXOOXO R OOXOOOO
Source
/* Problem: 2892 User: Childe Memory: 2488K Time: 438MS Language: C++ Result: Accepted */ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<ctime> #define N 2000005 using namespace std; inline int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x*f; } int s,t; int n,m; int d[N]; struct Treap{ Treap *l,*r; int fix,key,size; Treap(int key_):fix(rand()),key(key_),l(NULL),r(NULL),size(1){ } inline void update(){ size=1+(l?l->size:0)+(r?r->size:0); } }*root; typedef pair<Treap*,Treap*>Droot; inline int Size(Treap *x){ return x?x->size:0; } Droot Split(Treap *x,int k){ if(!x)return Droot(NULL,NULL); Droot y; if(Size(x->l)>=k){ y=Split(x->l,k); x->l=y.second; x->update(); y.second=x; } else{ y=Split(x->r,k-Size(x->l)-1); x->r=y.first; x->update(); y.first=x; } return y; } Treap *Merge(Treap *A,Treap *B){ if(!A)return B; if(!B)return A; if(A->fix<B->fix){ A->r=Merge(A->r,B); A->update(); return A; } else{ B->l=Merge(A,B->l); B->update(); return B; } } int Getkth(Treap *x,int v){ if(!x)return 0; return v<x->key?Getkth(x->l,v):Getkth(x->r,v)+Size(x->l)+1; } void Insert(int v){ int k=Getkth(root,v); Droot x=Split(root,k); Treap *n=new Treap(v); root=Merge(Merge(x.first,n),x.second); } void Delete(int k){ Droot x=Split(root,k-1); Droot y=Split(x.second,1); root=Merge(x.first,y.second); } int Findkth(int k){ Droot x=Split(root,k-1); Droot y=Split(x.second,1); Treap *ans=y.first; root=Merge(Merge(x.first,ans),y.second); return ans->key; } void find(Treap *w,int x){ if(!w)return; if(w->key>=x&&w->key<t)t=w->key; if(w->key<=x&&w->key>s)s=w->key; if(w->key<x)find(w->r,x); else find(w->l,x); } int main(){ srand(time(0)); cin>>n>>m; char ch; int a; for(int i=1,j=0;i<=m;i++){ cin>>ch; if(ch=='D'){ scanf("%d",&a); Insert(a); j++; d[j]=a; } if(ch=='R'){Delete(Getkth(root,d[j]));j--;} if(ch=='Q'){ scanf("%d",&a); s=0;t=n+1; find(root,a); if(s==a&&t==a)printf("0\n"); else printf("%d\n",t-s-1); } } return 0; }