Tunnel Warfare HDU - 1540

Posted 2aptx4869

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tunnel Warfare HDU - 1540相关的知识,希望对你有一定的参考价值。

学习set,其他容器每次都要去sort(),除了堆(但其无法lower_bound)

题面

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.
OutputOutput the answer to each of the Army commanders’ request in order on a separate line.

 

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, m, x, c;
    while (scanf("%d%d", &n, &m) != EOF)
    {
        stack<int> sk;
        set<int> st;
        st.insert(0), st.insert(n + 1);
        while (m--)
        {
            getchar();
            scanf("%c", &c);
            if (c == R) st.erase(sk.top()), sk.pop();
            else
            {
                scanf("%d", &x);
                if (c == D) st.insert(x), sk.push(x);
                else if (st.find(x) != st.end()) printf("0
");
                else
                {
                    auto l = st.lower_bound(x), r = st.lower_bound(x);
                    printf("%d
", *r - *--l - 1);
                }
            }
        }
    }
    return 0;
}

以上是关于Tunnel Warfare HDU - 1540的主要内容,如果未能解决你的问题,请参考以下文章

HDU1540 Tunnel Warfare —— 线段树 区间合并

hdu1540 Tunnel Warfare

hdu1540 Tunnel Warfare 线段树/树状数组

hdu--1540 Tunnel Warfare(线段树+区间合并)

线段树区间合并HDU1540-Tunnel Warfare

HDU 1540 Tunnel Warfare(线段树,单点更新,区间查询)