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题意:有很多节点,初值均为一,有三种操作,炸,修,查询输入节点最大连续区间解法:运用树状数组,单点更新,被炸就赋值为零,修复了就赋值为一,需注意的是,每次修复的节点是最后炸的节点,所以可以用栈把炸的节点存起来,修复时再依次退栈同时注意,需要开三个变量,即左区间连续最大值,右区间连续最大值,区间最大值,这么说很难理解,详情看代码<span style="font-size:18px;">#include<stdio.h> #include<algorithm> #include<string.h> #include<stack> using namespace std; #define MAXN 50010 stack<int>p; struct node { int l,r,lsum,rsum,msum;//开三个变量,记录一个区间左边连续最大值,右边连续最大值,区间最大值 }tr[MAXN*4]; void build(int id,int l,int r)//建树没什么说的,赋值为一就可以了 { tr[id].l=l; tr[id].r=r; if(l==r) tr[id].lsum=tr[id].rsum=tr[id].msum=1; else { int mid=(l+r)/2; build(id*2,l,mid); build(id*2+1,mid+1,r); tr[id].lsum=tr[id].rsum=tr[id].msum=tr[id*2].msum+tr[id*2+1].msum; } } void update(int id,int pos,int val) { if(tr[id].l==tr[id].r) { if(val==1)//判断是修复还是摧毁 tr[id].lsum=tr[id].rsum=tr[id].msum=1; else tr[id].lsum=tr[id].rsum=tr[id].msum=0; } else { int mid=(tr[id].l+tr[id].r)/2; if(pos<=mid) update(id*2,pos,val); else update(id*2+1,pos,val); tr[id].lsum=tr[id*2].lsum;//左连续最大为左儿子左连续最大 tr[id].rsum=tr[id*2+1].rsum;//同理 if(tr[id*2].lsum==tr[id*2].r-tr[id*2].l+1)//这里很重要,即若左儿子整个区间都是好的,则父亲节点还需要加上右儿子的左区间最大连续和 tr[id].lsum+=tr[id*2+1].lsum; if(tr[id*2+1].rsum==tr[id*2+1].r-tr[id*2+1].l+1)//同理 tr[id].rsum+=tr[id*2].rsum; tr[id].msum=max(tr[id*2].msum,max(tr[id*2+1].msum,tr[id*2].rsum+tr[id*2+1].lsum));//本区间最大值即为左儿子最大值,右儿子最大,左儿子右连续和加上右儿子左区间三个中的最大值 } } int query(int id,int pos1) { if(tr[id].l==tr[id].r||tr[id].msum==0||tr[id].msum==tr[id].r-tr[id].l+1) return tr[id].msum;//找到叶子节点,或者本区间完全被摧毁,或本区间都未被摧毁就直接返回最大和 else { int mid=(tr[id].l+tr[id].r)/2; if(pos1<=mid) { if(pos1>=tr[id*2].r-tr[id*2].rsum+1)//此处若pos1在左儿子的右区间,则还要加上右儿子的左区间 return query(id*2,pos1)+query(id*2+1,mid+1); else return query(id*2,pos1); } else { if(pos1<=tr[id*2+1].l+tr[id*2+1].lsum-1)//同理 return query(id*2+1,pos1)+query(id*2,mid); else return query(id*2+1,pos1); } } } int main() { int m,n,i,x; char ch[2]; while(scanf("%d%d",&n,&m)!=EOF)//注意多组输入,坑死爹了 { build(1,1,n); while(m--) { scanf("%s",ch); if(ch[0]=='D') { scanf("%d",&x); update(1,x,0); p.push(x);//每次把要炸的节点存下来,这里也可以用一个数组来代替栈 } else if(ch[0]=='Q') { scanf("%d",&x); printf("%d\n",query(1,x)); } else { if(!p.empty())//要先判断栈是否为空,否则有问题 { x=p.top(); p.pop(); update(1,x,1); } } } } return 0; } </span>