nbut线段树专题Q - Tunnel Warfare



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 (nm 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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. R: The village destroyed last was rebuilt.

Output

Output the answer to each of the Army commanders request in order on a separate line.


区间合并,单点更新+up操作合并区间。查询是难点,参考了kuangbin大神的做法。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=50010;

int last[maxn];
struct node
{
int l,r,l_len,r_len,m_len;
}tree[maxn<<2];

void pushup(int p)
{
int ll=(tree[p<<1].r-tree[p<<1].l+1);
int lr=(tree[p<<1|1].r-tree[p<<1|1].l+1);
tree[p].l_len=tree[p<<1].l_len;
if(tree[p<<1].l_len==ll)
tree[p].l_len+=tree[p<<1|1].l_len;
tree[p].r_len=tree[p<<1|1].r_len;
if(tree[p<<1|1].r_len==lr)
tree[p].r_len+=tree[p<<1].r_len;
tree[p].m_len=max(max(tree[p<<1].m_len,tree[p<<1|1].m_len),tree[p<<1].r_len+tree[p<<1|1].l_len);
}


void build(int p,int l,int r)
{
tree[p].l=l;
tree[p].r=r;
tree[p].l_len=tree[p].r_len=tree[p].m_len=(r-l+1);
if(l==r)
return ;
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}

void update(int p,int val,int pos)
{
if(tree[p].l==tree[p].r)
{
if(val==1)//0表示毁坏,1表示修复
tree[p].l_len=tree[p].r_len=tree[p].m_len=1;
else
tree[p].l_len=tree[p].r_len=tree[p].m_len=0;
return ;
}
int mid=(tree[p].l+tree[p].r)>>1;
if(pos<=mid)
update(p<<1,val,pos);
else
update((p<<1)|1,val,pos);
pushup(p);
}

int query(int p,int pos)
{
if(tree[p].l==tree[p].r||tree[p].m_len==0 || tree[p].m_len==tree[p].r-tree[p].l+1)//查到叶子节点,或者整段都被毁坏,都是完整的,不必再访问下去
{
return tree[p].m_len;
}
int mid=(tree[p].l+tree[p].r)>>1;
if(pos<=mid)
{


if(pos>=tree[p<<1].r-tree[p<<1].r_len+1)
return query(p<<1,pos)+query(p<<1|1,mid+1);
else
return query(p<<1,pos);//否则只有左边有
}
else
{
if(pos<=tree[p<<1|1].l+tree[p<<1|1].l_len-1)//同理在左右节点中间范围内,两个节点的都要算
return query(p<<1|1,pos)+query(p<<1,mid);
else
return query(p<<1|1,pos);//否则只查右节点右边
}

}

int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
build(1,1,n);
int cnt=0;
char op[3];
int rt;
for(int i=0;i<m;i++)
{
scanf("%s",op);
if(op[0]=='D')
{
scanf("%d",&rt);
last[cnt++]=rt;
update(1,0,rt);
}
else if(op[0]=='R')
update(1,1,last[--cnt]);
else
{
scanf("%d",&rt);
printf("%d\n",query(1,rt));
}
}
}
return 0;
}



你可能感兴趣的:(nbut线段树专题Q - Tunnel Warfare)