「POJ2892」Tunnel Warfare
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
1 2 3 4 5 6 7 8 9 10 |
7 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4 |
Sample Output
1 2 3 4 |
1 0 2 4 |
Hint
An illustration of the sample input:
1 2 3 4 5 6 7 8 9 10 11 |
OOOOOOO D 3 OOXOOOO D 6 OOXOOXO D 5 OOXOXXO R OOXOOXO R OOXOOOO |
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
#include<iostream> #include<cstdio> #include<ctime> #include<cstdlib> using namespace std; struct data{ int l,r,num; int w,rnd; }tree[80001]; int n,m; int sz,root=0; int s,t; int d[50001]; void rturn(int &t) { int k=tree[t].l; tree[t].l=tree[k].r; tree[k].r=t; t=k; } void lturn(int &t) { int k=tree[t].r; tree[t].r=tree[k].l; tree[k].l=t; t=k; } void insert(int &w,int x) { if(w==0) { sz++; w=sz; tree[w].num=x; tree[w].w=1; tree[w].rnd=rand(); return; } if(x==tree[w].num) {tree[w].w++;return;} else if(x<tree[w].num) { insert(tree[w].l,x); if(tree[tree[w].l].rnd<tree[w].rnd) rturn(w); } else if(x>tree[w].num) { insert(tree[w].r,x); if(tree[tree[w].r].rnd<tree[w].rnd) lturn(w); } } void del(int &w,int x) { if(tree[w].num==x) { if(tree[w].w>1) {tree[w].w--;return;} if(tree[w].l*tree[w].r==0)w=tree[w].l+tree[w].r; else if(tree[tree[w].l].rnd<tree[tree[w].r].rnd) { rturn(w); del(w,x); } else if(tree[tree[w].l].rnd>=tree[tree[w].r].rnd) { lturn(w); del(w,x); } return; } else if(tree[w].num<x) del(tree[w].r,x); else del(tree[w].l,x); } void find(int &w,int x) { if(w==0)return; if(tree[w].num>=x&&tree[w].num<t)t=tree[w].num; if(tree[w].num<=x&&tree[w].num>s)s=tree[w].num; if(tree[w].num<x)find(tree[w].r,x); else find(tree[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(root,a); j++; d[j]=a; } if(ch=='R'){del(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; } |
为学习平衡树而写,tree中的w在本题中没有用(不会重复破坏)