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!
InputThe 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.
OutputOutput 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
题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少
题解:线段树的合并,这道题就是需要合并,D的话破坏了这个村庄,那怎么办呢,就相当于中间切开,那么就是
相当于把当前这个点标记为0,然后再去更新它的父亲,这样怎么样会更加好做,好理解呢。
一段区间可以成为两段区间拼凑而成。
然后,我们定义老ls,rs表示左边连续区间,右边连续区间,ms表示最大连续区间,然后开始都为
整段,然后以D来切割,每段ls与rs最大值都不能超过当前这个点表示的区间
更新时方法:
左边就位左儿子的左边,右边就位右儿子的右边,然后最大值,就位左边,右边,和左儿子右边和
右儿子左边拼起来,就可以了。
查询操作:就是如果当前点,包涵在左边的右边,则可以和右子树的左儿子合并,同理,右边也一
样,然后最大值就是判断该段是否都联通了,是的话就直接可以推出返回值了,就
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cmath> 5 #include<cstring> 6 using namespace std; 7 const int MAXN=50007; 8 char s[20]; 9 int tk[MAXN*4]={0},x,top,n,m; 10 struct fzy{ 11 int ls,rs,ms,l,r; 12 }tree[MAXN*4]; 13 void build(int l,int r,int p){ 14 tree[p].ls=tree[p].ms=tree[p].rs=r-l+1;//一开始标记为所有 15 tree[p].l=l;tree[p].r=r; 16 if (l!=r) 17 { 18 int mid=(l+r)>>1; 19 build(l,mid,p*2); 20 build(mid+1,r,p*2+1); 21 } 22 } 23 void change(int p,int t,int x) 24 { 25 if (tree[p].l==tree[p].r) 26 { 27 if (x==1) 28 { 29 tree[p].ls=tree[p].ms=tree[p].rs=1; 30 } 31 else 32 { 33 tree[p].ls=tree[p].ms=tree[p].rs=0; 34 } 35 return; 36 } 37 int mid=(tree[p].l+tree[p].r)>>1; 38 if (t<=mid) change(2*p,t,x); 39 else change(2*p+1,t,x); 40 tree[p].ls=tree[p*2].ls; 41 tree[p].rs=tree[p*2+1].rs; 42 tree[p].ms=max(max(tree[p*2].ms,tree[p*2+1].ms),tree[p*2].rs+tree[p*2+1].ls); 43 if (tree[p*2].ls==tree[p*2].r-tree[p*2].l+1) 44 tree[p].ls+=tree[p*2+1].ls; 45 if (tree[p*2+1].rs==tree[p*2+1].r-tree[p*2+1].l+1) 46 tree[p].rs+=tree[p*2].rs; 47 } 48 int query(int p,int x) 49 { 50 if (tree[p].l==tree[p].r||tree[p].ms==0||tree[p].ms==tree[p].r-tree[p].l+1) return tree[p].ms; 51 int mid=(tree[p].l+tree[p].r)>>1; 52 if (x<=mid) 53 { 54 if (x>=tree[p*2].r-tree[p*2].rs+1) return query(p*2,x)+query(p*2+1,mid+1);//表示还可以和右边连 55 else return query(p*2,x); 56 } 57 else 58 { 59 if (x<=tree[p*2+1].l+tree[p*2+1].ls-1) return query(p*2+1,x)+query(p*2,mid);//表示还可以和左边连 60 else return query(p*2+1,x); 61 } 62 } 63 int main() 64 { 65 while (~scanf("%d%d",&n,&m)) 66 { 67 top=0; 68 build(1,n,1); 69 for (int i=1;i<=m;i++) 70 { 71 scanf("%s",&s); 72 if (s[0]=='D') 73 { 74 scanf("%d",&x); 75 tk[++top]=x; 76 change(1,x,0); 77 } 78 else if (s[0]=='Q') 79 { 80 scanf("%d",&x); 81 printf("%d ",query(1,x)); 82 } 83 else 84 { 85 if (top>0) 86 { 87 x=tk[top--]; 88 change(1,x,1); 89 } 90 } 91 } 92 } 93 }
没什么了。