HDU1540(线段树统计连续长度)

时间:2023-03-08 22:16:08

---恢复内容开始---

Tunnel Warfare


Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 (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.

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
2
4
模板题目,解释代码中
#include"cstdio"
#include"algorithm"
using namespace std;
const int MAXN=;
struct node{
int l,r;
int ll,rl,ml;
}a[MAXN*];
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].ll=a[rt].rl=a[rt].ml=r-l+;
if(l==r) return ;
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
} void merge(int rt)
{
//若左子树已满,则应与右子树左区间合并
a[rt].ll=a[rt<<].ll;
if(a[rt<<].ll==(a[rt<<].r-a[rt<<].l+))
{
a[rt].ll+=a[(rt<<)|].ll;
} //若右子树已满,则应与左子树的有区间合并
a[rt].rl=a[(rt<<)|].rl;
if(a[(rt<<)|].rl==(a[(rt<<)|].r-a[(rt<<)|].l+))
{
a[rt].rl+=a[rt<<].rl;
}
//该子树的最大连续长度为 左或右子树连续长度的最大者 或者 将左子树右区间与右子树左区间合并的长度
a[rt].ml=max(max(a[rt<<].ml,a[(rt<<)|].ml),a[rt<<].rl+a[(rt<<)|].ll);
} void update(int rt,int pos,int val)
{
if(a[rt].l==a[rt].r)
{
if(val) a[rt].ll=a[rt].rl=a[rt].ml=;
else a[rt].ll=a[rt].rl=a[rt].ml=;
return ;
} int mid=(a[rt].l+a[rt].r)>>; if(pos<=mid) update(rt<<,pos,val);
else update((rt<<)|,pos,val);
merge(rt);
} int query(int rt,int pos)
{
if(a[rt].l==a[rt].r||a[rt].ml==||a[rt].ml==a[rt].r-a[rt].l+)//到达叶子节点或者该节点已满或空,那么不必向下走了
{
return a[rt].ml;
} int mid=(a[rt].l+a[rt].r)>>; if(pos<=mid)//在左子树中
{
if(pos>=a[rt<<].r-a[rt<<].rl+)//若在左子树的右区间
{
return query(rt<<,pos)+query((rt<<)|,mid+);//则需要查询右子树
}
else
{
return query(rt<<,pos);
} }
else
{
if(pos<=a[(rt<<)|].l+a[(rt<<)|].ll-)//若在右子树的左区间
{
return query((rt<<)|,pos)+query(rt<<,mid);//则需要查询左子树
}
else
{
return query((rt<<)|,pos);
}
}
} int stack[MAXN];
int top; int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int pos;
top=;
build(,,n);
while(m--)
{
scanf("%*c");
char op;scanf("%c",&op);
if(op=='D')
{
scanf("%d",&pos);
stack[top++]=pos;
update(,pos,);
}
else if(op=='Q')
{
scanf("%d",&pos);
printf("%d\n",query(,pos));
}
else
{
pos=stack[--top];
update(,pos,);
}
}
}
}

---恢复内容结束---