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!
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 9D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
Sample Output
10
2
4
Hint
An illustration of the sample input:
OOOOOOOD 3 OOXOOOO
D 6 OOXOOXO
D 5 OOXOXXO
R OOXOOXO
R OOXOOOO
#include<stdio.h>
#include<string.h>
#define M 50005
int lb[M],stack[M],a[M];
bool b[M];
int n,m;
int sum(int k)
{
int ans=0;
while(k>0)
{
ans+=a[k];
k-=lb[k];
}
return ans;
}
void insert(int k,int p)
{
while(k<=n)
{
a[k]+=p;
k+=lb[k];
}
}
int search(int t)
{
int l=1,r=n+2;
int mid,tt;
while(l<r)
{
if(sum(l)==t)
return l;
mid=(l+r)/2;
tt=sum(mid);
if(t>tt)
l=mid+1;
else
r=mid;
}
return l;
}
int main()
{
int i,j,t,top;
char s[20];
for(i=0;i<M;i++)
{
lb[i]=i&(-i);
}
while(~scanf("%d%d",&n,&m))
{
memset(b,0,sizeof(b));
memset(a,0,sizeof(a));
top=0;
insert(n+2,1);
insert(1,1);
while(m--)
{
scanf("%s",s);
if(s[0]=='D')
{
scanf("%d",&t);
t++;
stack[top++]=t;
b[t]=true;
insert(t,1);
}
else if(s[0]=='R')
{
t=stack[--top];
b[t]=false;
insert(t,-1);
}
else
{
scanf("%d",&t);
t++;
if(b[t])
{
printf("0\n");
}
else
{
t=sum(t);
int r=search(t+1);
int l=search(t);
printf("%d\n",r-l-1);
}
}
}
}
return 0;
}
本文深入探讨了游戏开发领域的核心技术,包括游戏引擎、动画、3D空间视频等,旨在为开发者提供全面的游戏开发知识和实践经验。
1184

被折叠的 条评论
为什么被折叠?



