HDU 1540 Tunnel Warfare(线段树,去最大连续区间)
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; }
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想