hdu 1540

57 阅读2分钟

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

题意: 给你n个数进行m次操作,其中D表示删除这个点,Q表示查询这个点所在的区间最大值,R表示恢复上一次删除的点

思路: 区间连续和的模板

代码

#include <iostream>
#include <stack>
#include<cmath>
#include<algorithm>
#include <cstring>
using namespace std;
#define int long long
const int N = 5e4 + 10;
int n,m;
struct NOW{
    int ll,rr;
    int maxn;
    int pre,last;
}seg[N*4];
void update(int idx)
{
    seg[idx].pre = seg[idx*2].pre;
    seg[idx].last = seg[idx*2+1].last;
    seg[idx].maxn = max(seg[idx*2].maxn,seg[idx*2+1].maxn);
    seg[idx].maxn = max(seg[idx].maxn,seg[idx*2].last + seg[idx*2+1].pre);
    if(seg[idx*2].rr - seg[idx*2].ll+1 == seg[idx*2].pre)
    seg[idx].pre += seg[idx*2+1].pre;
    if(seg[idx*2+1].rr - seg[idx*2+1].ll+1 == seg[idx*2+1].last)
    seg[idx].last += seg[idx*2].last;
}

void build(int idx,int l,int r)
{
    if(l == r)
    {
        seg[idx].ll = l;
        seg[idx].rr = r;
        seg[idx].maxn = 1;
        seg[idx].pre = seg[idx].last = 1;
    }
    else
    {
        seg[idx].ll = l;
        seg[idx].rr = r;
        int mid = (l + r)/2;
        build(idx*2,l,mid);
        build(idx*2+1,mid+1,r);
        update(idx);
    }
}

void modify(int idx,int l,int r,int pos,int f)
{
    if(l == r)
    {
        seg[idx].maxn = f;
        seg[idx].pre = f;
        seg[idx].last = f;
    }
    else
    {
        int mid = (l + r)/2;
        if(pos <= mid)
            modify(idx*2,l,mid,pos,f);
        else
            modify(idx*2+1,mid + 1,r,pos,f);
        update(idx);
    }
}
int query(int idx,int l,int r,int pos)
{
    if(l == r)
    return seg[idx].maxn;
    int mid = (l + r)/2;
    if (pos <= mid)
    {
        if(pos >= seg[idx*2].rr - seg[idx*2].last+1)
        {
            return seg[idx*2].last + seg[idx*2+1].pre;
        }
        return query(idx * 2, l, mid,pos);
    }
    else
    {
        if(pos <= seg[idx*2+1].ll+seg[idx*2+1].pre-1)
        {
            return seg[idx*2].last + seg[idx*2+1].pre;
        }
        return query(idx * 2 + 1, mid + 1,r,pos);
    }
}
void solve()
{
    while(cin >> n >> m;
    {
        memset(seg,0,sizeof seg);
        build(1,1,n);
        stack<int>q;
        for(int i = 1;i <= m;i ++)
        {
            char s[10];
            int w;
            scanf("%s",s);
            if(s[0] == 'D')
            {
                cin >> w;
                modify(1,1,n,w,0);
                q.push(w);
            }
            else if(s[0] == 'R'&&q.size())
            {
                modify(1,1,n,q.top(),1);
                q.pop();
            }
            else if(s[0] == 'Q')
            {
                cin >> w;
                cout << query(1,1,n,w) << '\n';
            }
        }
     }
}
signed main()
{
    int tt = 1;
    while(tt--)
    {
        solve();
    }
}