标签:during define except algo plain output top can desc
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 8800 | Accepted: 3641 |
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!
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:
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
An illustration of the sample input:
OOOOOOO
D 3 OOXOOOO
D 6 OOXOOXO
D 5 OOXOXXO
R OOXOOXO
R OOXOOOO
平衡树,练了下treap,寻找前驱与后继,
set,vector,数组,线段树,spaly...都支持
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 7 using namespace std; 8 9 #define lson t[k].l 10 #define rson t[k].r 11 const int N = 200100; 12 13 struct Data{ 14 int val,key,l,r; 15 }t[N]; 16 int st[N],top,tn,Root,ans; 17 18 inline int read() { 19 int x = 0,f = 1;char ch = getchar(); 20 for (; ch<‘0‘||ch>‘9‘; ch = getchar()) 21 if (ch==‘-‘) f = -1; 22 for (; ch>=‘0‘&&ch<=‘9‘; ch = getchar()) 23 x = x * 10 + ch - ‘0‘; 24 return x * f; 25 } 26 27 inline void leftturn(int &k) { 28 int a = rson; 29 rson = t[a].l; 30 t[a].l = k; 31 k = a; 32 } 33 inline void rightturn(int &k) { 34 int a = lson; 35 lson = t[a].r; 36 t[a].r = k; 37 k = a; 38 } 39 void Insert(int &k,int x) { 40 if (k==0) { 41 ++tn;k = tn; 42 t[k].val = x;t[k].key = rand(); 43 return ; 44 } 45 if (x > t[k].val) { 46 Insert(rson,x); 47 if (t[rson].key < t[k].key) leftturn(k); 48 } 49 else { 50 Insert(lson,x); 51 if (t[lson].key < t[k].key) rightturn(k); 52 } 53 } 54 void Delete(int &k,int x) { 55 if (k==0) return ; 56 if (t[k].val==x) { 57 if (t[lson].key * t[rson].key == 0) k = lson + rson; 58 else if (t[lson].key < t[rson].key) { 59 rightturn(k);Delete(k,x); 60 } 61 else { 62 leftturn(k);Delete(k,x); 63 } 64 } 65 else if (x > t[k].val) Delete(rson,x); 66 else Delete(lson,x); 67 } 68 void getpre(int k,int x) { 69 if (k==0) return ; 70 if (x==t[k].val) {ans = k;return ;} // 加这一句可以等于它 71 else if (x > t[k].val) ans = k,getpre(rson,x); 72 else getpre(lson,x); 73 } 74 void getsuc(int k,int x) { 75 if (k==0) return ; 76 if (x==t[k].val) {ans = k;return ;} 77 else if (x < t[k].val) ans = k,getsuc(lson,x); 78 else getsuc(rson,x); 79 } 80 int main() { 81 int n = read(),m = read(); 82 char s[10]; 83 Insert(Root,0);Insert(Root,n+1); 84 for (int a,b,c,i=1; i<=m; ++i) { 85 scanf("%s",s); 86 if (s[0]==‘D‘) { 87 a = read(); 88 Insert(Root,a); 89 st[++top] = a; 90 } 91 else if (s[0]==‘R‘) Delete(Root,st[top--]); 92 else { 93 a = read(); 94 ans = 0;getpre(Root,a);b = ans; 95 ans = 0;getsuc(Root,a);c = ans; 96 if (b==c) puts("0"); 97 else printf("%d\n",t[c].val-t[b].val-1); 98 } 99 } 100 return 0; 101 }
标签:during define except algo plain output top can desc
原文地址:http://www.cnblogs.com/mjtcn/p/8012877.html