Tunnel Warfare

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
0
2
4


#include <iostream>
#include <stack>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 50000;

struct node
{
  int l, r;
  int ll, rl, ml;
}tree[N << 2];

void Build(int l, int r, int rt)
{
   tree[rt].l = l;
   tree[rt].r = r;
   tree[rt].ll = tree[rt].rl = tree[rt].ml = r - l + 1;
   if(l == r)
     return;
   int m = (l + r) >> 1;
   Build(l, m, rt << 1);
   Build(m + 1, r, rt << 1 | 1);
}

void Update(int x, int op, int rt)
{
  if(tree[rt].l == tree[rt].r && tree[rt].l == x)
  {
    if(op == 1)
    {
      tree[rt].ll = tree[rt].rl = tree[rt].ml = 1;
    }
    else
    {
      tree[rt].ll = tree[rt].rl = tree[rt].ml = 0;
    }
    return ;
  }
  int m = (tree[rt].l + tree[rt].r) / 2;
  if(x <= m)
    Update(x, op, rt << 1);
  else 
    Update(x, op, rt << 1 | 1);
  tree[rt].ll = tree[rt << 1].ll;
  tree[rt].rl = tree[rt << 1 | 1].rl;
  tree[rt].ml = max(tree[rt << 1].ml, tree[rt << 1 | 1].ml);                      //最大值必定在两个子区间和两个子区间的组合之间选出
  tree[rt].ml = max(tree[rt].ml, tree[rt << 1].rl + tree[rt << 1 | 1].ll);
  if(tree[rt << 1].ll == tree[rt << 1].r - tree[rt << 1].l + 1)   //如果左子树全部相连,则根区间左端开始的最长连续个数顺延到有区间
    tree[rt].ll += tree[rt << 1 | 1].ll;
  if(tree[rt << 1 | 1].rl ==  tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1)
    tree[rt].rl += tree[rt << 1].rl;
}

int Query(int x, int rt)
{
  int l, r;
  l = tree[rt].l;
  r = tree[rt].r;
  if(l == r || tree[rt].ml == 0 || tree[rt].ml == r - l + 1)
  {
    return tree[rt].ml;
  }
  int m = (l + r) / 2;
  if(x <= m)
  {
    if(x >= tree[rt << 1].r - tree[rt << 1].rl + 1)
     return Query(x, rt << 1) + Query(m + 1, rt << 1 | 1);
    else 
     return Query(x, rt << 1);
  }
  else  
  {
    if(x <= tree[rt << 1 | 1].l + tree[rt << 1 | 1].ll - 1)
     return Query(x, rt << 1 | 1) + Query(m, rt << 1);
    else 
     return Query(x, rt << 1 | 1);
  }
}

int q[N];
int top;
int main()
{
  int n, m, x;
  char ch[10];
  cin >> n >> m;
while(scanf("%d %d", &n, &m) != EOF)
{
  Build(1, n, 1);
  top = 0;
  for(int i = 1; i <= m; i++)
  {
    scanf("%s", &ch);
    if(ch[0] == 'D')
    {
      scanf("%d", &x);
      Update(x, 0, 1);
      q[top++] = x;
    }
    else if(ch[0] == 'Q')
    {
      scanf("%d", &x);
      cout <<  Query(x, 1) << endl;
    }
    else if(ch[0] == 'R') 
    {    
       x = q[--top];
       Update(x, 1, 1);
    }
  }
}
  return 0;
}

猜你喜欢

转载自blog.csdn.net/aqa2037299560/article/details/80657877