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,之后有m个操作
1> D x 将序号x处变为0
2> R 将最近被变为0的恢复为1
3> Q x 询问x所在区间中,连续的1的个数 。
分析 : 维护区间的连续个数问题,都可以用区间合并来解决。
#include
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)
const int MAXN = 5e4+11;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
struct Tree{
int l,r,len;
int lsum,rsum,sum;
}tree[MAXN<<2];
void Up(int o){ // 重点
tree[o].lsum=tree[o<<1].lsum;
tree[o].rsum=tree[o<<1|1].rsum;
tree[o].sum=max(tree[o<<1].sum,tree[o<<1|1].sum);
if(tree[o<<1].lsum==tree[o<<1].len)
tree[o].lsum+=tree[o<<1|1].lsum;
if(tree[o<<1|1].rsum==tree[o<<1|1].len)
tree[o].rsum+=tree[o<<1].rsum;
tree[o].sum=max(tree[o].lsum,tree[o].rsum) ;
tree[o].sum=max(tree[o].sum,tree[o<<1].rsum+tree[o<<1|1].lsum);
}
void Build(int o,int le,int ri){
tree[o].l=le,tree[o].r=ri,tree[o].len=ri-le+1;
tree[o].lsum=tree[o].rsum=tree[o].sum=1;
if(le==ri) return ;
int mid=(tree[o].l+tree[o].r)>>1;
Build(o<<1,le,mid);
Build(o<<1|1,mid+1,ri);
Up(o);
}
void Update(int o,int pos,int val){
if(tree[o].l==tree[o].r) {
tree[o].lsum=tree[o].rsum=tree[o].sum=val;
return ;
}
int mid=(tree[o].l+tree[o].r)>>1;
if(pos<=mid)
Update(o<<1,pos,val);
else
Update(o<<1|1,pos,val);
Up(o);
}
int Query(int o,int pos){
if(tree[o].l==tree[o].r) {
return tree[o].sum;
}
int mid=(tree[o].l+tree[o].r)>>1;
if(pos<=mid) { // 因为是点,所以可能和分开的右半部分有连接
int ans=0;
if(pos>=tree[o<<1].r-tree[o<<1].rsum+1)
return ans=tree[o<<1].rsum+tree[o<<1|1].lsum;
else return Query(o<<1,pos);
}else {
int ans=0;
if(tree[o<<1|1].lsum+tree[o<<1|1].l-1>=pos)
return ans=tree[o<<1|1].lsum+tree[o<<1].rsum;
else return Query(o<<1|1,pos);
}
}
int main(){
CLOSE();
// fread();
// fwrite();
int n,m;
while(~scanf("%d%d",&n,&m)){
Build(1,1,n);
stack<int>S; char op[5];
while(m--){
scanf("%s",op);
if(op[0]=='Q'){
int a;scanf("%d",&a);
printf("%d\n",Query(1,a));
}else {
int a;
if(op[0]=='D'){
scanf("%d",&a); S.push(a);
Update(1,a,0);
}
else{
if(!S.empty()){
a=S.top(); S.pop();
Update(1,a,1);
}
}
}
}
}
return 0;
}