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!
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 4
Sample Output
1 0 2 4
线段树区间合并:重建过程可以看出来是湛储存。
对于每个区间(l,r)我们考虑lsum:表示左起最大连续值。
rsum:表示右起最大连续值,maum:区间的最大连续值.
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<bitset> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int maxn=50000+10; int lsum[maxn<<2],rsum[maxn<<2],msum[maxn<<2]; int s[maxn],n,m,top; void pushup(int rs,int s) { lsum[rs]=lsum[rs<<1]; rsum[rs]=rsum[rs<<1|1]; if(lsum[rs<<1]==(s-(s>>1))) lsum[rs]+=lsum[rs<<1|1]; if(rsum[rs<<1|1]==(s>>1)) rsum[rs]+=rsum[rs<<1]; msum[rs]=max(max(msum[rs<<1],msum[rs<<1|1]),rsum[rs<<1]+lsum[rs<<1|1]); } void build(int l,int r,int rs) { lsum[rs]=rsum[rs]=msum[rs]=r-l+1; if(l==r) return ; int mid=(l+r)>>1; build(l,mid,rs<<1); build(mid+1,r,rs<<1|1); pushup(rs,r-l+1); } void update(int x,int c,int l,int r,int rs) { if(l==r) { lsum[rs]=rsum[rs]=msum[rs]=c; return ; } int mid=(l+r)>>1; if(x<=mid) update(x,c,l,mid,rs<<1); if(x>mid) update(x,c,mid+1,r,rs<<1|1); pushup(rs,r-l+1); } int query(int x,int l,int r,int rs) { if(l==r||msum[rs]==0||msum[rs]==r-l+1) return msum[rs]; int mid=(l+r)>>1; if(x<=mid)//x点在左区间 { if(x>=mid-rsum[rs<<1]+1)//在靠右连续区间里 return rsum[rs<<1]+query(mid+1,mid+1,r,rs<<1|1); else return query(x,l,mid,rs<<1); } else { if(x<=mid+lsum[rs<<1|1]) return lsum[rs<<1|1]+query(mid,l,mid,rs<<1); else return query(x,mid+1,r,rs<<1|1); } } int main() { char str[3]; int x; while(~scanf("%d%d",&n,&m)) { build(1,n,1); top=0; while(m--) { scanf("%s",str); if(str[0]!='R') scanf("%d",&x); if(str[0]=='D') { update(x,0,1,n,1); s[top++]=x; } if(str[0]=='R') { x=s[--top]; update(x,1,1,n,1); } if(str[0]=='Q') printf("%d\n",query(x,1,n,1)); } } return 0; }