Problem descriptionduring the War of Resistance against Japan, tunnel warfare was carried off extensively in the vast area S of North China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the ends, every village was directly connected with the 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 is severely isolated, restoration of connection must is done immediately!
Inputthe first line of the input contains, 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 is 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 is directly or indirectly connected with includ ING itself.
R:the village destroyed was rebuilt.
Outputoutput the answer to all of the Army commanders ' request in order on a separate line.
Sample Input
7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample Output
1024
segment Tree interval merging: The reconstruction process can be seen as Cham storage. for each interval (l,r) We consider lsum: represents the maximum continuous value from the left. Rsum: Represents the maximum continuous value for the right, Maum: the maximal consecutive values of the interval.
#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& lt;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; The IF (x<=mid)//x Point is in the left interval {if (x>=mid-rsum[rs<<1]+1)//In the right continuous interval return rsum[rs<<1]+query (mi D+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;}
HDU 1540 Tunnel Warfare (segment tree single point update + interval merge)