D x
Q x 表示查询有多少个地道与x相连
R
思路:线段路,记录每个结点左边 中间,右边 连续的地道数量。
//3412K
266MS
#include <stdio.h>
#define M 50050
#define L(x) (x<<1)
#define R(x) ((x<<1)+1)
struct data
{
l,r,state;
-1表示空,1表示全被destroy
lma,mma,rma; //左中右连续的数量
}node[3*M];
int D[M];
void BuildTree(int left ,int right,int u)
{
left;
right;
right)
return ;
(left + right)>>1;
BuildTree(left,mid,L(u));
BuildTree(mid+1,right,R(u));
}
void getdown(int u,int op)
{
node[u].state = 0;
-1)
node[L(u)].state = -1;
node[L(u)].lma = node[L(u)].mma = node[L(u)].rma = node[L(u)].r -
node[L(u)].l + 1;
node[R(u)].state = -1;
node[R(u)].lma = node[R(u)].mma = node[R(u)].rma = node[R(u)].r -
node[R(u)].l + 1;
== 1)
node[L(u)].state = 1;
node[L(u)].lma = node[L(u)].mma = node[L(u)].rma = 0;
node[R(u)].state = 1;
node[R(u)].lma = node[R(u)].mma = node[R(u)].rma = 0;
}
void Union (int u)
{
(node[L(u)].state == -1)
node[u].lma = (node[L(u)].r - node[L(u)].l + 1) +
node[R(u)].lma;
node[u].lma = node[L(u)].lma;
(node[R(u)].state == -1)
node[u].rma = (node[R(u)].r - node[R(u)].l + 1) +
node[L(u)].rma;
node[u].rma = node[R(u)].rma;
= node[L(u)].rma + node[R(u)].lma; //
}
void destroy (int num,int u)
{
(node[u].state == 1)
return ;
(node[u].l == num&&node[u].r ==
num)
node[u].state = 1;
node[u].lma = node[u].rma = node[u].mma = 0;
return ;
(node[u].state == -1)
getdown(u,-1);
<= node[L(u)].r)
destroy(num,L(u));
>= node[R(u)].l)
destroy(num,R(u));
(u);
//根据左右结点,修改父结点
(node[L(u)].state == node[R(u)].state)