Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2396 Accepted Submission(s): 886
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!
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.
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
Source
Recommend
LL
題意是一條線上的點,D x是破壞這個點,Q x是表示查詢以x所在的最長的連續的點的個數,R是恢復上一次破壞的點。
線段樹結點 設置一個 ll 記錄區間左端點開始的最大連續個數, rl 記錄區間右端點開始的最大的連續個數,
ml表示該區間最大的連續點的個數。
主要是更新和查詢兩個操作。
/* HDU 1540 Tunnel Warfare 題義是對於一段線段,D x 表示破壞x點, R 表示回復最近一次被破壞的點,Q x表示 詢問以x點為中心的兩頭的最長的連續區間。 */ #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int MAXN=50050; struct Node { int l,r; int ll,rl,ml; //左邊開始連續的最大長度和右邊開始最大的連續長度 //以及這個區間最大連續長度 }segTree[MAXN*3]; void Build(int i,int l,int r) { segTree[i].l=l; segTree[i].r=r; segTree[i].ll=segTree[i].rl=segTree[i].ml=r-l+1; if(l==r)return; int mid=(l+r)>>1; Build(i<<1,l,mid); Build((i<<1)|1,mid+1,r); } void update(int i,int t,int val) { if(segTree[i].l==segTree[i].r) { if(val==1)segTree[i].ll=segTree[i].rl=segTree[i].ml=1; else segTree[i].ll=segTree[i].rl=segTree[i].ml=0; return; } int mid=(segTree[i].l+segTree[i].r)>>1; if(t<=mid)update(i<<1,t,val); else update((i<<1)|1,t,val); segTree[i].ll=segTree[i<<1].ll; segTree[i].rl=segTree[(i<<1)|1].rl; segTree[i].ml=max(segTree[i<<1].ml,segTree[(i<<1)|1].ml); segTree[i].ml=max(segTree[i].ml,segTree[i<<1].rl+segTree[(i<<1)|1].ll); if(segTree[i<<1].ll==segTree[i<<1].r-segTree[i<<1].l+1)segTree[i].ll+=segTree[(i<<1)|1].ll; if(segTree[(i<<1)|1].rl==segTree[(i<<1)|1].r-segTree[(i<<1)|1].l+1) segTree[i].rl+=segTree[i<<1].rl; } int query(int i,int t) { if(segTree[i].l==segTree[i].r||segTree[i].ml==0||segTree[i].ml==segTree[i].r-segTree[i].l+1) { return segTree[i].ml; } int mid=(segTree[i].l+segTree[i].r)>>1; if(t<=mid) { if(t>=segTree[i<<1].r-segTree[i<<1].rl+1) return query(i<<1,t)+query((i<<1)|1,mid+1); else return query(i<<1,t); } else { if(t<=segTree[(i<<1)|1].l+segTree[(i<<1)|1].ll-1) return query((i<<1)|1,t)+query(i<<1,mid); else return query((i<<1)|1,t); } } int que[MAXN]; int top; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int n,m; char str[10]; int x; while(scanf("%d%d",&n,&m)!=EOF) { Build(1,1,n); top=0; while(m--) { scanf("%s",&str); if(str[0]=='D') { scanf("%d",&x); que[top++]=x; update(1,x,0); } else if(str[0]=='Q') { scanf("%d",&x); printf("%d\n",query(1,x)); } else { if(x>0) { x=que[--top]; update(1,x,1); } } } } return 0; }
