# hdu - 1540

Posted by WildCow on February 2, 2018

# Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

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

7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

### Sample Output

1
0
2
4

1Y还是比较开心

``````
#include <bits/stdc++.h>
using namespace std;

const int N = 50050;

struct Tree
{
int l;
int r;
int ll;
int rl;
int ml;
};

Tree tree[N * 4];
int n, m;
int st[N];
int now;

void up(int x)
{
tree[x].ll = tree[x << 1].ll;
tree[x].rl = tree[x << 1 | 1].rl;
tree[x].ml = max(max(tree[x << 1].ml, tree[x << 1 | 1].ml), tree[x << 1].rl + tree[x << 1 | 1].ll);
if(tree[x << 1].ll == tree[x << 1].r - tree[x << 1].l + 1){
tree[x].ll += tree[x << 1 | 1].ll;
}
if(tree[x << 1 | 1].ll == tree[x << 1 | 1].r - tree[x << 1 | 1].l + 1){
tree[x].rl += tree[x << 1].rl;
}
}

void build(int x, int l, int r)
{
tree[x].l = l;
tree[x].r = r;
if(l == r){
tree[x].ll = tree[x].ml = tree[x].rl = 1;
}
else{
int mid;

mid = l + ((r - l) >> 1);
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
up(x);
}
}

void update(int x, int t, int v)
{
int l = tree[x].l;
int r = tree[x].r;

if(tree[x].l == tree[x].r && tree[x].l == t){
tree[x].ll = tree[x].rl = tree[x].ml = v;
}
else{
int mid;

mid = l + ((r - l) >> 1);
if(t <= mid){
update(x << 1, t, v);
}
else{
update(x << 1 | 1, t, v);
}
up(x);
}
}

int query(int x, int v)
{
int l = tree[x].l;
int r = tree[x].r;
int mid;

if(l == r || !tree[x].ml || r - l + 1 == tree[x].ml){
return tree[x].ml;
}
mid = l + ((r - l) >> 1);
if(v <= mid){
if(v >= tree[x << 1].r - tree[x << 1].rl + 1){
return query(x << 1, v) + query(x << 1 | 1, mid + 1);
}
else{
return query(x << 1, v);
}
}
else{
if(v <= tree[x << 1 | 1].l + tree[x << 1 | 1].ll - 1){
return query(x << 1 | 1, v) + query(x << 1, mid);
}
else{
return query(x << 1 | 1, v);
}
}
}

int main(int argc, char const *argv[])
{
while(~scanf("%d%d", &n, &m)){
now = 0;
build(1, 1, n);
for(int i = 0; i < m; i ++){
char ma;

scanf("%s", ma);
if(ma == 'D'){
int x;

scanf("%d", &x);
st[now] = x;
now ++;
update(1, x, 0);
}
else if(ma == 'Q'){
int x;

scanf("%d", &x);
printf("%d\n", query(1, x));
}
else{
int x;

now --;
x = st[now];
update(1, x, 1);
}
}
}

return 0;
}
``````