Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8010 Accepted Submission(s): 3094
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
LL
题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少?
解题思路:lr代表区间左端点开始的最大连续值,rl代表区间右端点开始的最大连续值,sum代表整个区间的最大连续值
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const int MAXN=200100;
int tot,sum[MAXN],lr[MAXN],rl[MAXN];
int x[MAXN];
void build(int k,int l,int r)
{
sum[k]=lr[k]=rl[k]=r-l+1;
if(l==r) return ;
int mid=(l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
}
void Insert(int k,int l,int r,int p,int v)
{
if(l==r)
{
sum[k]=lr[k]=rl[k]=v;
return ;
}
int mid=(l+r)>>1;
if(p<=mid) Insert(k<<1,l,mid,p,v);
else Insert(k<<1|1,mid+1,r,p,v);
if(lr[k<<1]==mid-l+1) lr[k]=lr[k<<1]+lr[k<<1|1];
else lr[k]=lr[k<<1];
if(rl[k<<1|1]==r-mid) rl[k]=rl[k<<1|1]+rl[k<<1];
else rl[k]=rl[k<<1|1];
sum[k]=max(max(sum[k<<1],sum[k<<1|1]),rl[k<<1]+lr[k<<1|1]);
}
int query(int k,int l,int r,int p)
{
if(sum[k]==0||sum[k]==r-l+1) return sum[k];
int mid=(l+r)>>1;
if(p<=mid)
{
if(p>=mid-rl[k<<1]+1) return query(k<<1,l,mid,p)+query(k<<1|1,mid+1,r,mid+1);
else query(k<<1,l,mid,p);
}
else
{
if(p<=mid+lr[k<<1|1]) return query(k<<1,l,mid,mid)+query(k<<1|1,mid+1,r,p);
else query(k<<1|1,mid+1,r,p);
}
}
int main()
{
int n,m,tot,a;
string s;
while(~scanf("%d %d",&n,&m))
{
tot=0;
build(1,1,n);
while(m--)
{
cin>>s;
if(s=="D")
{
scanf("%d",&a);
x[tot++]=a;
Insert(1,1,n,a,0);
}
else if(s=="R")
{
a=x[--tot];
Insert(1,1,n,a,1);
}
else
{
scanf("%d",&a);
printf("%d\n",query(1,1,n,a));
}
}
}
return 0;
}