hdu--1540 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!
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
题意:在一条地道上有n个村庄,进行m次操作,D操作是摧毁村庄X,Q操作是询问村庄X能与多少村庄是连续的,R操作是重建上一个被摧毁的村庄。打印每次询问结果
思路:首先建树,a[k].lazy为1说明[ a[k].l,a[k].r ]区间内村庄全部完好,a[k].lazy为0说明[ a[k].l,a[k].r ]区间内存在村庄被摧毁。用lm,rm和m分别表示从左端点往右数最大连续村庄数,从右端点往左数最大连续村庄数和区间内的最大连续村庄数。然后进行单点更新,有pushup对父节点进行更新。最后进行询问,用到的是二分的思想,分别对左子树和右子树进行统计。
AC代码:
1 #include <iostream> 2 #include<cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 const int maxn=50000+10; 7 int num[maxn]; 8 struct note 9 { 10 int l,r,lm,rm,m,lazy; 11 }a[maxn<<2]; 12 int max(int a,int b) 13 { 14 if(a>b) 15 return a; 16 else 17 return b; 18 } 19 int build(int l,int r,int k) 20 { 21 a[k].l=l,a[k].r=r,a[k].lm=a[k].rm=a[k].m=(r-l+1); 22 a[k].lazy=1; 23 if(l==r) 24 return 0; 25 int mid=(l+r)/2; 26 build(l,mid,k*2); 27 build(mid+1,r,k*2+1); 28 return 0; 29 } 30 int pushup(int k) 31 { 32 if(a[k*2].rm<(a[k*2].r-a[k*2].l+1)) 33 a[k].lm=a[k*2].lm; 34 else 35 a[k].lm=a[k*2].rm+a[k*2+1].lm; 36 if(a[k*2+1].lm<a[k*2+1].r-a[k*2+1].l+1) 37 a[k].rm=a[k*2+1].rm; 38 else 39 a[k].rm=a[k*2+1].lm+a[k*2].rm; 40 a[k].m=max(max(a[k].lm,a[k].rm),a[k*2+1].lm+a[k*2].rm); 41 if(a[k].m==a[k].r-a[k].l+1) 42 a[k].lazy=1; 43 else 44 a[k].lazy=0; 45 return 0; 46 } 47 int ins(int d,int n,int k) 48 { 49 if(d==a[k].l&&a[k].r==a[k].l) 50 { 51 a[k].rm=a[k].lm=a[k].m=n; 52 a[k].lazy=n; 53 return 0; 54 } 55 //pushdown(k) 56 if(d<=a[k*2].r) 57 ins(d,n,k*2); 58 if(d>a[k*2].r) 59 ins(d,n,k*2+1); 60 pushup(k); 61 return 0; 62 } 63 int sea(int n,int k) 64 { 65 if(a[k].lazy==1||a[k].r==a[k].l) 66 { 67 return a[k].m; 68 } 69 if(n<=a[k*2].r) 70 { 71 if(n>=a[k*2].r-a[k*2].rm+1) 72 return sea(n,k*2)+sea(a[k*2+1].l,k*2+1); 73 else 74 return sea(n,k*2); 75 } 76 else 77 { 78 if(n<=a[k*2+1].lm+a[k*2+1].l-1) 79 return sea(n,k*2+1)+sea(a[k*2].r,k*2); 80 else 81 return sea(n,k*2+1); 82 } 83 return 0; 84 } 85 int main() 86 { 87 int n,m,k,b; 88 while(~scanf("%d%d",&n,&m)) 89 { 90 build(1,n,1); 91 k=0; 92 while(m--) 93 { 94 char s[5]; 95 scanf("%s",s); 96 if(s[0]=='D') 97 { 98 scanf("%d",&num[k]); 99 ins(num[k],0,1); 100 k++; 101 } 102 else if(s[0]=='Q') 103 { 104 scanf("%d",&b); 105 printf("%d\n",sea(b,1)); 106 } 107 else 108 { 109 k--; 110 ins(num[k],1,1); 111 } 112 } 113 } 114 115 return 0; 116 }