题目:
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
线段树区间合并:定义结构体;有三个量,st[p].ll表示该区间一左边端点为起点的最长区间值,st[p].lr表示该区间一右边端点为终点的最长区间值。st[p].ml表示该区间的最长区间值,st[p].ml有三种情况 st[p].ml = max(max(st[p << 1].ml,st[p << 1|1].ml),st[p << 1].lr + st[p << 1|1].ll);//左子区间最大,右子区间最大,或者跨越的。
其他参考:https://blog.csdn.net/sunyutian1998/article/details/79618316
https://blog.csdn.net/libin56842/article/details/14105071
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int maxn = 50005;
int a[maxn];
struct node
{
int l,r;
int ll,lr,ml;
}st[maxn << 2];
void build(int p,int l,int r)
{
st[p].l = l;
st[p].r = r;
st[p].ll = st[p].lr = st[p].ml = r - l + 1;
if(l == r) return;
int m = (l + r) >> 1;
build(p << 1,l,m);
build(p << 1|1,m + 1,r);
}
void update(int p,int t,int k)
{
if(st[p].l == st[p].r)
{
if(k == 1)
st[p].ll = st[p].lr = st[p].ml = 1;
else st[p].ll = st[p].lr = st[p].ml = 0;
return ;
}
int m = (st[p].l + st[p].r) >> 1;
if(t <= m) update(p << 1,t,k);
if(t > m) update(p << 1|1,t,k);
st[p].ll = st[p << 1].ll;
st[p].lr = st[p << 1|1].lr;
st[p].ml = max(max(st[p << 1].ml,st[p << 1|1].ml),st[p << 1].lr + st[p << 1|1].ll);
if(st[p << 1].ll == st[p << 1].r - st[p << 1].l + 1) st[p].ll += st[p << 1|1].ll;
if(st[p << 1|1].lr == st[p << 1|1].r - st[p << 1|1].l + 1) st[p].lr += st[p << 1].lr;
}
int query(int p,int t)
{
if(st[p] .l == st[p].r || st[p].ml == 0 || st[p].ml == (st[p].r - st[p].l + 1))
return st[p].ml;
int m = (st[p].l + st[p].r) >> 1;
if(t <= m)
{
if(t >= st[p << 1].r - st[p << 1].lr + 1)
return query(p << 1,t) + query(p << 1|1,m + 1);
else return query(p << 1,t);
}
else {
if(t <= st[p << 1|1].l + st[p << 1|1].ll - 1)
return query(p << 1|1,t) + query(p << 1,m);
else return query(p << 1|1 ,t);
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m) == 2)
{
char ch[2];
int x;
int j = 0;
build(1,1,n);
while(m --)
{
scanf("%s",ch);//以后这种的最好开成数组,不然会跪。
getchar();
if(ch[0] == 'D')
{
scanf("%d",&x);
a[j++] = x;
update(1,x,0);
}
else if(ch[0] == 'Q')
{scanf("%d",&x);
int num = query(1,x);
printf("%d\n",num);
}
else {
if(x > 0){
x= a[ -- j];
update(1,x,1);
}
}
}
}
return 0;
}