Question:
Three operations
D POS destroys the position of the POs so that it is not connected to the surrounding area.
Q: How many villages are connected to pos.
R repairs the recently destroyed villages.
Train of Thought Analysis:
The tree Array records the number of 1 in this interval.
If [s-e] has e-S + 1 1. This interval is connected.
In this way, we can use the binary method to find the maximum number of connections to a specific location.
Here is the second point.
While (L <= r)
{
If (satisfied)
{
Ans = mid;
L = Mid + 1;
}
Else r = mid-1;
}
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#define maxn 55555using namespace std;int n,m;int c[maxn];int lowbit(int x){ return (x&(-x));}int sum(int x){ int ret=0; while(x>0) { ret+=c[x]; x-=lowbit(x); } return ret;}void add(int x,int d){ while(x<=n) { c[x]+=d; x+=lowbit(x); }}int top;int stack[maxn];int main(){ while(scanf("%d%d",&n,&m)!=EOF) { memset(c,0,sizeof c); for(int i=1;i<=n;i++) { add(i,1); } char str[5]; while(m--) { scanf("%s",str); if(str[0]=='D') { int pos; scanf("%d",&pos); // if(sum(pos)-sum(pos-1)==0)continue; add(pos,-1); stack[top++]=pos; } else if(str[0]=='R') { int pos=stack[--top]; add(pos,1); } else { int cur,l,r,ans,rs,re; scanf("%d",&cur); if(sum(cur)-sum(cur-1)==0) { printf("0\n"); continue; } l=cur;r=n; while(l<=r) { int mid=(l+r)>>1; if(sum(mid)-sum(cur-1)==mid-cur+1) { ans=mid; l=mid+1; } else r=mid-1; } re=ans; l=1;r=cur; while(l<=r) { int mid=(l+r)>>1; if(sum(cur)-sum(mid-1)==cur-mid+1) { ans=mid; r=mid-1; } else l=mid+1; } rs=ans; // printf("%d %d\n",re,rs); printf("%d\n",re-rs+1); } } } return 0;}