线段树之区间更新 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!
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 4Sample Output
1 0 2 4#include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int N=5e4+5; int f[N<<2], lazy[N]; int n,m; void pushUp(int root){ int rt=root<<1; f[root]=f[rt]+f[rt+1]; } void build(int root,int left,int right){ if(left==right){ f[root]=1; return; } int mid=(left+right)>>1; int rt=root<<1; build(rt,left,mid); build(rt+1,mid+1,right); pushUp(root); } void updata(int root,int left,int right,int x,int val){ if(left==right){ f[root]=val; return; } int rt=root<<1; int mid=(left+right)>>1; if(x<=mid){ updata(rt,left,mid,x,val); } else{ updata(rt+1,mid+1,right,x,val); } pushUp(root); } int query(int root,int left,int right,int qleft,int qright){ if(qleft<=left&&right<=qright){ return f[root]; } int mid=(left+right)>>1; int ans=0; int rt=root<<1; if(qleft<=mid){ ans+=query(rt,left,mid,qleft,qright); } if(qright>mid){ ans+=query(rt+1,mid+1,right,qleft,qright); } return ans; } bool check(int left,int right){ int u=right-left+1; int t=query(1,1,n,left,right); if(u==t){ return true; } else{ return false; } } int main(){ int x,c; char ca[2]; int left,right; while(~scanf("%d%d",&n,&m)){ build(1,1,n); while(m--){ cin>>ca; if(ca[0]=='D'){ cin>>x; updata(1,1,n,x,0); lazy[++c]=x; } else if(ca[0]=='Q'){ cin>>x; left= 0; right=x; while(right-left>1){ int mid=left+((right-left)>>1); if (check(mid, x)){ right=mid; } else{ left=mid; } } int ans=x-right; left=x; right=n+1; while(right-left>1){ int mid=left+((right-left)>>1); if (check(x, mid)){ left=mid; } else{ right=mid; } } ans+=left-x; #include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int N = 5e4 + 5; int f[N<<2], lazy[N]; int n, m; void pushUp(int root){ int rt=root<<1; f[root] = f[rt] + f[rt+1]; } void build(int root, int left, int right){ if(left==right){ f[root]=1; return; } int mid=(left+right)>>1; int rt=root<<1; build(rt,left,mid); build(rt+1,mid+1,right); pushUp(root); } void update(int root, int left, int right, int x, int val){ if (left==right){ f[root]=val; return; } int mid=(left+right)>>1; int rt=root<<1; if(x<=mid){ update(rt,left,mid,x,val); } else{ update(rt+1,mid+1,right,x,val); } pushUp(root); } int query(int root,int left,int right,int qleft,int qright){ if (qleft<=left&&right<=qright){ return f[root]; } int mid=(left+right)>>1; int ans=0; int rt=root<<1; if(qleft<=mid){ ans+=query(rt,left,mid,qleft,qright); } if(qright>mid){ ans+=query(rt+1,mid+1,right,qleft,qright); } return ans; } bool check(int left , int right){ int u=right-left+1; int t=query(1,1,n,left,right); if(u==t){ return true; }else{ return false; } } int main(){ char ca[2]; int x, ww = 0; while(~scanf("%d%d",&n,&m)){ build(1,1,n); while(m--){ scanf("%s",&ca); if(ca[0]=='D'){ scanf("%d",&x); update(1,1,n,x,0); lazy[++ww]=x; } else if(ca[0]=='Q'){ scanf("%d", &x); int l=0,r=x; while(r-l>1){ int m=l+((r-l)>>1); if(check(m,x)){ r=m; } else{ l=m; } } int ans=x-r; l=x,r=n+1; while(r-l>1){ int m=l+((r-l)>>1); if(check(x,m)){ l=m; } else{ r=m; } } ans+=l-x; if(check(x, x)){ ans++; } printf("%d\n",ans); }else{ int y=lazy[ww--]; update(1,1,n,y,1); } } } return 0; } if(check(x, x)){ ans++; } cout<<ans<<endl; } else { int y = lazy[c--]; updata(1, 1, n, y, 1); } } } return 0; }
标签:right,Warfare,Tunnel,线段,mid,else,int,ans,left 来源: https://www.cnblogs.com/killjoyskr/p/16484159.html