Tunnel Warfare [HDU--1540]

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

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
题意 : 一条线上的点
D x是破坏这个点,Q x是表示查询以x所在的最长的连续的点的个数,R是恢复上一次破坏的点。
线段树的维护区间合并经典用法
设置一个 ll 记录区间左端点开始的最大连续个数, rr 记录区间右端点开始的最大的连续个数,
ml表示该区间最大的连续点的个数,我觉得吧这个道题不会的话可以记住这个题的思想,因为很经典

AC code:(睡觉前的阳历都不过得代码,睡觉起来后AC 懵)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

#define lson rt<<1
#define rson rt<<1|1

using namespace std;

const int maxn = 5e4+5;
const int inf = 0x7f7f7f7f;

struct segtree {
    int l,r;
    int ll,rr,ml; // ll一段区间从左边开始的最长连续区间 rr一段区间从右边开始的最长连续区间  ml一段区间中最长的连续区间
}ss[maxn<<2];

void build(int l,int r,int rt) {
    ss[rt].l = l,ss[rt].r = r;
    ss[rt].ll = ss[rt].ml = ss[rt].rr = r-l+1;
    if(l == r) return;
    int mid = (l+r)>>1;
    build(l,mid,lson); build(mid+1,r,rson);
}

int query(int pos,int rt) {
    if ( ss[rt].l == ss[rt].r || ss[rt].ml == 0 || ss[rt].ml == ss[rt].r - ss[rt].l + 1) {
        return ss[rt].ml;
    } 
    int mid = (ss[rt].l+ss[rt].r)>>1;
    if ( pos <= mid ) { 
        if ( pos >= ss[lson].r - ss[lson].rr + 1 ) 
            return query(pos,lson) + query(mid+1,rson);
        else return query(pos,lson);
    } else {
        if ( pos <= ss[rson].l + ss[rson].ll - 1 ) 
            return query(mid,lson) + query(pos,rson); 
        else return query(pos,rson);
    }
}

void update(int pos,int rt,int val) {
    if ( ss[rt].l == ss[rt].r ) {
        if(val) ss[rt].ll = ss[rt].rr= ss[rt].ml = 1;
        else ss[rt].ll = ss[rt].rr= ss[rt].ml = 0;
        return;
    } 
    int mid = (ss[rt].l+ss[rt].r) >>1;
    if ( pos <= mid ) update(pos,lson,val);
    else update(pos,rson,val);
    ss[rt].ll = ss[lson].ll;
    ss[rt].rr = ss[rson].rr;
    ss[rt].ml = max(ss[lson].ml,ss[rson].ml);
    ss[rt].ml = max(ss[rt].ml,ss[lson].rr+ss[rson].ll);
    if ( ss[lson].ll == ss[lson].r - ss[lson].l + 1 ) ss[rt].ll += ss[rson].ll; 
    if ( ss[rson].rr == ss[rson].r - ss[rson].l + 1 ) ss[rt].rr += ss[lson].rr; 
}

int sta[maxn];
int pos;

int main(){
    int n,m;
    while (~scanf("%d %d",&n,&m)){
        build(1,n,1); pos = 0;
        char str[10]; int s;
        while( m -- ) {
            scanf("%s",str);
            if ( str[0] == 'D' ) {
                scanf ("%d",&s); sta[pos++] = s;
                update(s,1,0);
            } else if ( str[0] == 'Q' ) {
                scanf ("%d",&s);
                printf("%d\n",query(s,1));
            } else if ( str[0] == 'R' ) {
                if ( pos > 0 ) {
                    s = sta[--pos] ;
                    update(s,1,1);
                }
            } 
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Acer12138/article/details/81702989