Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 785 Accepted Submission(s): 245
Problem 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
/************************************************************************ 此题线段树最经典的部分是更新部分,也是这题最难的部分,要考虑全面 线段树结构体定义三个变量 len,l_len, r_len 分别表示这段最长的连续区间,从左节点开始的长度,从右节点开始的长度 此题一直WA。。。做的时候考虑不全面,参考了一下大牛BLOG的代码, 发现少考虑了一些,改了才AC的~ - -| ************************************************************************/ #include <iostream> #include <vector> using namespace std; #define N 50005 #define max(a,b) (a>b?a:b) struct LineTree { int r,l; int len,l_len,r_len; }seg_tree[N*5]; char ch; void Build(int root,int a,int b) { seg_tree[root].l=a; seg_tree[root].r=b; int l=(b-a+1); seg_tree[root].len=l; seg_tree[root].l_len=l; seg_tree[root].r_len=l; if(a==b) return; int mid=(a+b)>>1; Build(root<<1,a,mid); Build((root<<1)+1,mid+1,b); } void Update(int root,int x) { if(seg_tree[root].l==seg_tree[root].r) { if(ch=='D') seg_tree[root].len=seg_tree[root].l_len=seg_tree[root].r_len=0; else seg_tree[root].len=seg_tree[root].l_len=seg_tree[root].r_len=1; return; } int mid=(seg_tree[root].l+seg_tree[root].r)>>1; if(x<=mid) Update((root<<1),x); else Update((root<<1)+1,x); seg_tree[root].len=max(seg_tree[root<<1].r_len+seg_tree[(root<<1)+1].l_len,max(seg_tree[root<<1].len,seg_tree[(root<<1)+1].len)); seg_tree[root].l_len=seg_tree[root<<1].l_len; seg_tree[root].r_len=seg_tree[(root<<1)+1].r_len; if(seg_tree[root].l_len==(mid-seg_tree[root].l+1)) seg_tree[root].l_len+=seg_tree[(root<<1)+1].l_len; if(seg_tree[root].r_len==(seg_tree[root].r-mid)) seg_tree[root].r_len+=seg_tree[root<<1].r_len; } int Search(int root,int x) { if(seg_tree[root].l==seg_tree[root].r || seg_tree[root].len==0 || seg_tree[root].len==(seg_tree[root].r-seg_tree[root].l+1)) { return seg_tree[root].len; } int mid=(seg_tree[root].l+seg_tree[root].r)>>1; if(x<=mid) { if(x > (mid-seg_tree[root<<1].r_len)) return seg_tree[root<<1].r_len+Search((root<<1)+1,mid+1); else return Search(root<<1,x); } else { if(x<=(mid+seg_tree[(root<<1)+1].l_len)) return seg_tree[(root<<1)+1].l_len+Search(root<<1,mid); else return Search((root<<1)+1,x); } } int main() { int n,m,v; vector<int> node; while (~scanf("%d%d",&n,&m)) { Build(1,1,n); getchar(); node.clear(); while (m--) { scanf("%c%*c",&ch); if(ch=='D') { scanf("%d%*c",&v); node.push_back(v); Update(1,v); } else if(ch=='R') { if(!node.empty()) { v=node.back(); node.pop_back(); Update(1,v); } } else { scanf("%d%*c",&v); printf("%d/n",Search(1,v)); } } } return 0; }