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\n");
                else
                {
                    auto l = st.lower_bound(x), r = st.lower_bound(x);
                    printf("%d\n", *r - *--l - 1);
                }
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/2aptx4869/p/12123684.html

时间: 2024-10-01 05:01:06

Tunnel Warfare HDU - 1540的相关文章

Tunnel Warfare HDU 1540 区间合并+最大最小值

Tunnel Warfare HDU 1540 区间合并+最大最小值 题意 D x是破坏这个点,Q x是表示查询以x所在的最长的连续的点的个数,R是恢复上一次破坏的点. 题解思路 参考的大佬博客 这里巧妙使用了最大值最小值来进行区间的查找.上一行是大佬的详细题解,真的很妙啊. 代码实现 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> #define ls (rt&l

I - Tunnel Warfare - hdu 1540

#include <iostream>//未完成 #include <cstdio> using namespace std; const int maxn=50005; #define lson rt<<1 #define rson rt<<1|1 struct st { int l,r,len; int mid() { return (l+r)>>1; } }a[maxn<<2]; void bst(int rt,int l,in

I - Tunnel Warfare - hdu 1540(区间合并更新)

题意:在抗日战争期间,地道战在华北平原得到广泛的实施,一般而言,村庄通过一些隧道在一条线上连接,除了两端剩下的每个村庄都有两个相连. 侵略者会频繁的对这些村庄进行扫荡,并且摧他们的地道,当然八路军会把这一些已经被摧毁的村庄修复的,会优先修复最近被破坏的村庄. 分析:被这道题折磨了一上午啊,不过也学到了很多,尤其是这种涉及左右区间的. ********************************************************************* #include<std

Tunnel Warfare HDU - 1540(线段树最长连续区间)

题意: 一条线上的点,D x是破坏这个点,Q x是表示查询以x所在的最长的连续的点的个数,R是恢复上一次破坏的点. 解析: 线段树结点 设置一个  lq记录区间左端点开始的最大连续个数,  rq 记录区间右端点开始的最大的连续个数 其它和原来一样即可 看代码吧... #include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue>

I - Tunnel Warfare HDU - 1540 线段树最大连续区间

题意  :一段区间  操作1 切断点 操作2 恢复最近切断的一个点 操作3 单点查询该点所在最大连续区间 思路:  主要是push_up :  设区间x 为母区间  x<<1 ,x<<1|1分别为两个子区间 x的左端连续子段和 :当x<<1区间没有断开 也就是 x<<1 的最大连续子段ml ==tree[x<<1].r-tree[x<<1].l+1 等于区间长度时 x左端连续字段和tree[x].ll=tree[x<<1]

HDU 1540 &amp;&amp; POJ 2892 Tunnel Warfare (线段树,区间合并).

~~~~ 第一次遇到线段树合并的题,又被律爷教做人.TAT. ~~~~ 线段树的题意都很好理解吧.. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://poj.org/problem?id=2892 ~~~~ 我的代码:200ms #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #defin

hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1540 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Gene

hdu 1540 Tunnel Warfare(线段树)

题目链接:hdu 1540 Tunnel Warfare 题目大意:有连续的N个城镇,三种操作: D x:第x城镇被破坏 Q x:插叙第x城镇所在联通块有多少个城镇没有被破坏 R:修复最后一个被破坏的城镇 解题思路:线段树区间合并,每个城镇看成一个叶子节点,用一个vector记录破坏顺序.对于查询来说,每次只要判断是否在mid?R[lson(u)],mid+L[rson(u)]之间即可,否则即递归查询左右子树. #include <cstdio> #include <cstring>

hdu 1540 Tunnel Warfare【线段树】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目大意:抗日战争时期,各村庄被一条地道连接着(村庄排在一条线上),有三种操作: 第一种:某村庄被敌军摧毁: 第二种:修复上一个被摧毁的村庄: 第三种:查询与该村庄直接或间接链接的村庄有多少个(包括自己): 此题用线段树做,每个节点包含该区间从左端开始有多大连续区间ls,从右端向左有多大连续区间rs,该区间的最大连续区间mas: 代码如下: #include <iostream> #incl