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
题意
给你一到n连续的数字,三种操作,(1)D 去掉数字x (2) Q 查询与x所在连续数字长度 (3) R 恢复上次去掉的数字
思路
用线段树记录当前区间左边连续长度 右边连续长度 和区间内的长度。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 50050;
struct node
{
int l,r;
int ll,rl,ml;
}tree[maxn*3];
void build(int i,int l,int r)
{
tree[i].l = l;
tree[i].r= r;
tree[i].ll = tree[i].rl = tree[i].ml = r-l+1;
if(l == r) return ;
int m = (l+r)>>1;
build(i<<1,l,m);
build((i<<1)|1,m+1,r);
}
void update(int i,int t,int val)
{
if(tree[i].l == tree[i].r)
{
if(val == 1) tree[i].ll = tree[i].rl = tree[i].ml = 1;
else tree[i].ll = tree[i].rl = tree[i].ml = 0;
return ;
}
int m = (tree[i].l + tree[i].r)>>1;
if(t <= m) update(i<<1,t,val);
else update((i<<1)|1,t,val);
tree[i].ll = tree[i<<1].ll;
tree[i].rl = tree[(i<<1)|1].rl;
tree[i].ml = max(tree[i<<1].ml,tree[(i<<1)|1].ml);
tree[i].ml = max(tree[i].ml,tree[i<<1].rl+tree[(i<<1)|1].ll);
if(tree[i<<1].ll == tree[i<<1].r - tree[i<<1].l + 1) tree[i].ll += tree[i<<1|1].ll;
if(tree[(i<<1)|1].rl == tree[(i<<1)|1].r - tree[(i<<1)|1].l + 1) tree[i].rl += tree[i<<1].rl;
}
int query(int i,int t)
{
if(tree[i].l == tree[i].r||tree[i].ml == 0||tree[i].ml == tree[i].r - tree[i].l + 1)
{
return tree[i].ml;
}
int m = (tree[i].l + tree[i].r)>>1;
if(t <= m)
{
if(t >= tree[i<<1].r - tree[i<<1].rl + 1)
{
return query(i<<1,t) + query((i<<1)|1,m+1);
}
else query(i<<1,t);
}
else
{
if(t <= tree[(i<<1)|1].l + tree[(i<<1)|1].ll-1)
return query((i<<1)|1,t) + query(i<<1,m);
else return query((i<<1)|1,t);
}
}
int que[maxn];
int top;
int main()
{
int n,m;
char str[10];
int x;
while(scanf("%d%d",&n,&m) != EOF)
{
build(1,1,n);
top = 0;
while(m--)
{
scanf("%s",&str);
if(str[0] == 'D')
{
scanf("%d",&x);
que[top++] = x;
update(1,x,0);
}
else if(str[0] == 'Q')
{
scanf("%d",&x);
printf("%d\n",query(1,x));
}
else
{
if(x > 0)
{
x = que[--top];
update(1,x,1);
}
}
}
}
return 0;
}