Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10884 Accepted Submission(s): 4263
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
Source
Recommend
题意:
有N个村庄在一条直线上,相邻之间是联通的。有q次操作,D为破坏该村庄,Q为查询该村庄最多联通多少个村庄。R为恢复上一个被破坏的村庄。
这里有两种方法,我总结一下。
思路1:
记录每一个节点的可以到达的最左端和最右端(不包括该点),查询的时候只需要算abs(ansmaxn-ansminn)+1。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn=50005;
struct node
{
int maxn,minn;
}tr[maxn<<2];
int ansmaxn,ansminn;
int n,m,x;
char str[5];
stack<int> sta;
void pushup(int i)
{
tr[i].maxn=max(tr[i*2].maxn,tr[2*i+1].maxn);
tr[i].minn=min(tr[i*2].minn,tr[2*i+1].minn);
}
void build(int i,int l,int r)
{
if(l==r)
{
tr[i].maxn=0;
tr[i].minn=n+1;
return;
}
int mid=(l+r)/2;
build(2*i,l,mid);
build(2*i+1,mid+1,r);
pushup(i);
return;
}
void updatemaxn(int i,int l,int r,int pos,int c)
{
if(l==r&&l==pos)
{
tr[i].maxn=c;
return;
}
int mid=(l+r)/2;
if(pos<=mid) updatemaxn(2*i,l,mid,pos,c);
else updatemaxn(2*i+1,mid+1,r,pos,c);
pushup(i);
return;
}
void updateminn(int i,int l,int r,int pos,int c)
{
if(l==r&&l==pos)
{
tr[i].minn=c;
return;
}
int mid=(l+r)/2;
if(pos<=mid) updateminn(2*i,l,mid,pos,c);
else updateminn(2*i+1,mid+1,r,pos,c);
pushup(i);
return;
}
int querymaxn(int i,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
ansmaxn=max(ansmaxn,tr[i].maxn);
return ansmaxn;
}
int mid=(l+r)/2;
if(x<=mid) ansmaxn=max(ansmaxn,querymaxn(2*i,l,mid,x,y));
if(y>mid) ansmaxn=max(ansmaxn,querymaxn(2*i+1,mid+1,r,x,y));
return ansmaxn;
}
int queryminn(int i,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
ansminn=min(ansminn,tr[i].minn);
return ansminn;
}
int mid=(l+r)/2;
if(x<=mid) ansminn=min(ansminn,queryminn(2*i,l,mid,x,y));
if(y>mid) ansminn=min(ansminn,queryminn(2*i+1,mid+1,r,x,y));
return ansminn;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
while(!sta.empty()) sta.pop();
build(1,1,n);
for(int i=1;i<=m;i++)
{
scanf("%s",str);
if(str[0]=='D')
{
scanf("%d",&x);
sta.push(x);
updatemaxn(1,1,n,x,x);
updateminn(1,1,n,x,x);
}
else if(str[0]=='Q')
{
scanf("%d",&x);
ansmaxn=0;
ansminn=n+1;
querymaxn(1,1,n,1,x);
queryminn(1,1,n,x,n);
printf("maxn=%d minn=%d\n",ansmaxn,ansminn);
if(abs(ansmaxn-ansminn)==0)
printf("0\n");
else
printf("%d\n",abs(ansmaxn-ansminn)-1);
}
else
{
x=sta.top();
sta.pop();
updatemaxn(1,1,n,x,0);
updateminn(1,1,n,x,n+1);
}
}
}
return 0;
}
思路2:
线段树的区间合并。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
const int maxn=50005;
struct node
{
int l,r;
int ls,rs,ms;
}tr[maxn<<2];
stack<int> sta;
void build(int i,int l,int r)
{
tr[i].l=l,tr[i].r=r;
tr[i].ls=tr[i].rs=tr[i].ms=r-l+1;
if(l!=r)
{
int mid=(l+r)/2;
build(2*i,l,mid);
build(2*i+1,mid+1,r);
}
}
void insert(int i,int pos,int c)
{
if(tr[i].l==tr[i].r&&tr[i].l==pos)
{
tr[i].ms=tr[i].ls=tr[i].rs=c;
return;
}
int mid=(tr[i].l+tr[i].r)/2;
if(pos<=mid) insert(2*i,pos,c);
else insert(2*i+1,pos,c);
tr[i].ls=tr[2*i].ls;
tr[i].rs=tr[2*i+1].rs;
tr[i].ms=max(tr[2*i].ms,max(tr[2*i+1].ms,tr[2*i].rs+tr[2*i+1].ls));
if(tr[2*i].ls==tr[2*i].r-tr[2*i].l+1)
tr[i].ls+=tr[2*i+1].ls;
if(tr[2*i+1].rs==tr[2*i+1].r-tr[2*i+1].l+1)
tr[i].rs+=tr[2*i].rs;
}
int query(int i,int pos)
{
if(tr[i].ms==0||tr[i].ms==tr[i].r-tr[i].l+1||tr[i].l==tr[i].r)
return tr[i].ms;
int mid=(tr[i].l+tr[i].r)/2;
if(pos<=mid)
{
if(pos>=tr[2*i].r-tr[2*i].rs+1)
return query(2*i,pos)+query(2*i+1,mid+1);
else
return query(2*i,pos);
}
else
{
if(pos<=tr[2*i+1].l+tr[2*i+1].ls-1)
return query(2*i+1,pos)+query(2*i,mid);
else
return query(2*i+1,pos);
}
}
int main()
{
int n,m,x;
char op[5];
while(~scanf("%d%d",&n,&m))
{
while(!sta.empty()) sta.pop();
build(1,1,n);
for(int i=1;i<=m;i++)
{
scanf("%s",op);
if(op[0]=='D')
{
scanf("%d",&x);
sta.push(x);
insert(1,x,0);
}
else if(op[0]=='Q')
{
scanf("%d",&x);
printf("%d\n",query(1,x));
}
else
{
x=sta.top();
sta.pop();
insert(1,x,1);
}
}
}
return 0;
}