I - Tunnel Warfare(线段树,区间合并)

本文介绍了一个基于抗日战争时期地道战的模拟系统,该系统通过算法模拟了地道连接状态的变化,包括地道的破坏与重建,以及查询指定村庄的连接情况。通过对事件的实时更新,系统能够准确地反映出地道网络的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int s[56575];
struct node
{
  int l, r;//左右区间边界
  int lb, rb, mb;//左子树最大区间,右子树最大区间,中间的最大区间
}a[556755];
void Build(int node, int l, int r)//建树
{
  //更新
  a[node].l = l;
  a[node].r = r;
  a[node].lb = a[node].rb = a[node].mb = r - l + 1;
  if(l!=r)
  {
      int m = (r + l) / 2;
      Build(node*2, l, m);
      Build(node*2+1, m+1, r);
  }
}
void UpData(int node, int t, int add)
{
   if(a[node].l==a[node].r)
   {
      if(add==1)//修复
      {
        a[node].lb = a[node].rb = a[node].mb = 1;
      }
      else//消除
      {
         a[node].lb = a[node].rb = a[node].mb = 0;
      }
      return ;
   }
   int m = (a[node].l + a[node].r) / 2;
   if(t<=m)//更新
   UpData(node*2, t, add);
   else
   UpData(node*2+1, t, add);
   a[node].lb = a[node*2].lb;
   a[node].rb = a[node*2+1].rb;
   a[node].mb = max(max(a[node*2].mb, a[node*2+1].mb), a[node*2].rb+a[node*2+1].lb);//取三者之间最大的
   if(a[node*2].lb==a[node*2].r-a[node*2].l+1)
   {
     a[node].lb += a[node*2+1].lb;
   }
   if(a[node*2+1].rb==a[node*2+1].r-a[node*2+1].l+1)
   {
     a[node].rb += a[node*2].rb;
   }
}
int Query(int node, int t)//求个数
{
if(a[node].l==a[node].r||a[node].mb==0||a[node].mb==a[node].r-a[node].l+1)
  {
    return a[node].mb;
  }
  int m = (a[node].l + a[node].r) / 2;
  if(t<=m)
  {
    if(t>=a[node*2].r-a[node*2].rb+1)
    {
       return Query(node*2, t)+Query(2*node+1, m+1);
    }
    else
    {
      return Query(node*2, t);
    }
  }
  else
  {
    if(t<=a[node*2+1].l+a[node*2+1].lb-1)
    {
      return Query(node*2+1, t)+Query(node*2, m);
    }
    else
    return Query(node*2+1, t);
  }
}
int main()
{
  int n, m;
  while(~scanf("%d %d", &n, &m))
  {
      int top = -1;
      Build(1, 1, n);
      while(m--)
      {
        int t;
        char p[12];
        scanf("%s", p);
        if(p[0]=='D')
        {
           scanf("%d", &t);
           s[++top] = t;
           UpData(1, t, 0);
        }
        else if(p[0]=='R')
        {
          if(top>=0)
          {
          UpData(1, s[top], 1);
          top--;
          }
        }
        else
        {
            scanf("%d", &t);
           printf("%d\n", Query(1, t));
        }
      }
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值