HDU 1540 Tunnel Warfare
2018-02-24 16:45
134 查看
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10790 Accepted Submission(s): 4235
[align=left]Problem Description[/align]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!
[align=left]Input[/align]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.
[align=left]Output[/align]Output the answer to each of the Army commanders’ request in order on a separate line.
[align=left]Sample Input[/align]7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
[align=left]Sample Output[/align]1
0
2
4 #include<bits/stdc++.h>
using namespace std;
#define lson rt<<1
#define rson rt<<1|1
const int maxn=50007;
int a[maxn];
stack<int>P;
struct node
{
int left,right,mid;
int lx,rx,mx;
int dis()
{
return right-left+1;
}
} c[maxn<<2];
void Push_up(int rt)
{
c[rt].lx=c[lson].lx;
if(c[lson].lx==c[lson].dis())
c[rt].lx+=c[rson].lx;
c[rt].rx=c[rson].rx;
if(c[rson].rx==c[rson].dis())
c[rt].rx+=c[lson].rx;
c[rt].mx=max(c[lson].mx,c[rson].mx);
c[rt].mx=max(c[rt].mx,c[lson].rx+c[rson].lx);
}
void build(int l,int r,int rt)
{
c[rt].left=l;
c[rt].right=r;
c[rt].mid=(l+r)>>1;
if(l==r)
{
c[rt].lx=c[rt].rx=c[rt].mx=a[l];
return;
}
build(l,c[rt].mid,lson);
build(c[rt].mid+1,r,rson);
Push_up(rt);
}
void update(int L,int rt)
{
if(c[rt].left==c[rt].right)
{
c[rt].lx=c[rt].rx=c[rt].mx=a[L];
return;
}
if(L<=c[rt].mid) update(L,lson);
else update(L,rson);
Push_up(rt);
}
int query(int L,int rt)
{
if(c[rt].left==c[rt].right||c[rt].mx==c[rt].dis()||c[rt].mx==0)
return c[rt].mx;
//已到叶子节点或此区间已全为0或1时不必在往下查询。
if(L<=c[rt].mid)
{
if(c[rt].mid-c[lson].rx>=L)
return query(L,lson);
//若L不在左子树的右连续中,则只需查询左子树。
else
return query(L,lson)+query(c[rt].mid+1,rson);
//否则要查询左子树+右子树的左连续。
}
else
{ //同理
if(c[rt].mid+c[rson].lx<L)
return query(L,rson);
else
return query(L,rson)+query(c[rt].mid,lson);
}
}
int main()
{
int n,m,i,j;
while(~scanf("%d%d",&n,&m))
{
for(i=1; i<=n; i++)
a[i]=1;
build(1,n,1);
while(m--)
{
char t[2];
int x;
scanf("%s",&t);
if(t[0]=='D')
{
scanf("%d",&x);
a[x]=0;
P.push(x);
update(x,1);
}
else if(t[0]=='R')
{
if(P.empty())continue;
x=P.top();
P.pop();
a[x]=1;
update(x,1);
}
else
{
scanf("%d",&x);
printf("%d\n",query(x,1));
}
}
}
return 0;
}
相关文章推荐
- HDU 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- HDU - 1540 Tunnel Warfare(线段树 区间合并)
- HDU - 1540 Tunnel Warfare(线段树最大连续区间)
- HDU 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare(线段树区间合并)
- 【HDU】1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare 线段数区间合并
- HDU 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare(线段树区间合并)
- hdu 1540 Tunnel Warfare(线段树)(第三部分 区间合并)
- HDU 1540 Tunnel Warfare(set容器)
- hdu1540 && POJ2892 Tunnel Warfare
- 【HDU - 1540】Tunnel Warfare 【线段树+单点更新+区间合并】
- HDU 1540 POJ 2892 Tunnel Warfare
- HDU 1540 Tunnel Warfare (线段树,区间合并)
- HDU - 1540 Tunnel Warfare 线段树 或者 让人心酸的暴力过
- HDU-1540 Tunnel Warfare (线段树区间合并)
- HDU - 1540 Tunnel Warfare —— 区间合并