地址:http://acm.hdu.edu.cn/showproblem.php?pid=1540
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3161 Accepted Submission(s): 1214
Problem 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
Sample Output
1 0 2 4
题意:第一行输入两个数m,n,m表示村庄数目,n表示操作数目。“D i”表示摧毁i号村庄,即使i号村庄与其他村庄不相连,“Q i”是询问i号村庄与几个村子相连(包括其本身),“R”表示修复最近一个被摧毁的村庄。
#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define M 50010
struct node{
int l,r,sum; //sum用来储存有区间内多少村庄被摧毁
}tree[3*M];
int ans[M];
void ori_tree(int i,int l,int r)
{
int m=(l+r)/2;
tree[i].sum=0;
tree[i].l=l;
tree[i].r=r;
if(l!=r)
{
ori_tree(i*2,l,m);
ori_tree(i*2+1,m+1,r);
}
}
void updata(int i,int j,int num)
{
if(tree[i].l==tree[i].r)
tree[i].sum+=num;
else
{
int m=(tree[i].l+tree[i].r)/2;
if(j<=m)
updata(i*2,j,num);
else
updata(i*2+1,j,num);
tree[i].sum+=num;
}
}
int suml(int i,int l,int r) //向左搜索
{
int ans,m=(tree[i].l+tree[i].r)/2;
if(tree[i].l==l&&tree[i].r==r&&tree[i].sum==0) return r-l+1;
if(tree[i].l==tree[i].r&&l==r&&tree[i].l==l) return 1-tree[i].sum;
if(r<=m) return suml(i*2,l,r);
else if(l>m) return suml(i*2+1,l,r);
else
{
ans=suml(i*2+1,m+1,r);
if(r-m==ans) ans+=suml(i*2,l,m);
return ans;
}
}
int sumr(int i,int l,int r) //向右搜索
{
int ans,m=(tree[i].l+tree[i].r)/2;
if(tree[i].l==l&&tree[i].r==r&&tree[i].sum==0) return r-l+1;
if(tree[i].l==tree[i].r&&l==r&&tree[i].l==l) return 1-tree[i].sum;
if(r<=m) return sumr(i*2,l,r);
else if(l>m) return sumr(i*2+1,l,r);
else
{
ans=sumr(i*2,l,m);
if(m-l+1==ans) ans+=sumr(i*2+1,m+1,r);
return ans;
}
}
int main()
{
int t,i,m,n;
int hash[M];
char str;
stack<int> qi;
while(scanf("%d%d",&m,&t)>0)
{
getchar();
memset(hash,0,sizeof(hash));
ori_tree(1,1,t);
for(i=1;i<=t;i++)
{
scanf("%c",&str);
if(str=='D')
{
scanf("%d",&n);
qi.push(n);
if(!hash[n])
{
hash[n]=1;
updata(1,n,1);
}
}
else if(str=='R'&&!qi.empty())
{
n=0;
while(!hash[n])
{
n=qi.top();
qi.pop();
}
if(n!=0)
{
hash[n]=0;
updata(1,n,-1);
}
}
else if(str=='Q')
{
scanf("%d",&n);
if(!hash[n])
{
int a=suml(1,1,n);
int b=sumr(1,n,m);
printf("%d\n",a+b-1);
}
else printf("0\n");
}
getchar();
}
}
return 0;
}
本来想让sum来记录区间内完好的村庄数,这样在输出答案时的搜索可以在sum等于r-l+1时停止。但搜索函数没想好,看了下别人的代码,找到这个向左向右两方搜索求和的方法。