zoukankan      html  css  js  c++  java
  • HDU 1540 Tunnel Warfare

    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个村庄相连,现在有些村庄会遭到破坏,而每一次修复的是最近一个被破坏的村庄,(先进后出,可以使用数组模拟栈,也可以直接用栈)现在查询与k村庄相连的有几个村庄(如果k村庄本身被破坏还未修复,则输出0
     
    #include<stdio.h>
    #include<algorithm>
    #include<stack>
    #define N 50010
    using namespace std;
    struct node
    {
        int left, right, sum, Lsum, Rsum; //sum存放与该点相连的点的总个数,Lsum存放从左端点开始向右与之相连的点的最大个数,Rsum存放从右端点开始向左与之相连的点的最大个数
    }no[4*N];
    void Bulid(int left, int right, int root)
    {
        int mid;
        no[root].left = left;
        no[root].right = right;
        no[root].sum = no[root].Lsum = no[root].Rsum = (no[root].right-no[root].left+1); //开始时所有点相连的个数都等于其区间的长度
        if (left == right) return ;
        mid = (left+right)/2;
        Bulid(left, mid, root*2);
        Bulid(mid+1, right, root*2+1);
    }
    void Pushup(int root) //向上更新
    {
        no[root].Lsum = no[root*2].Lsum;
        no[root].Rsum = no[root*2+1].Rsum;
        if (no[root*2].Lsum == no[root*2].right-no[root*2].left+1) //只有当左结点的Lsum与区间长度相等了才能加上右结点的部分,否则会有重复现象
            no[root].Lsum = no[root*2].Lsum + no[root*2+1].Lsum;
        if (no[root*2+1].Rsum == no[root*2+1].right-no[root*2+1].left+1)
            no[root].Rsum = no[root*2+1].Rsum + no[root*2].Rsum;
        no[root].sum = max(no[root].Lsum, max(no[root].Rsum, no[root*2].Rsum+no[root*2+1].Lsum));
    }
    void Update(int k, int num, int root)
    {
        int mid;
        if (no[root].left == no[root].right)
        {
            no[root].Lsum = no[root].Rsum = num;
            return ;
        }
        mid = (no[root].left+no[root].right)/2;
        if (k <= mid) Update(k, num, root*2);
        else Update(k, num, root*2+1);
        Pushup(root);
    }
    int Query(int k, int root)
    {
        int mid;
        if (no[root].sum == 0) return 0;
        if (k < no[root].left+no[root].Lsum) return no[root].Lsum;
        if (k > no[root].right-no[root].Rsum) return no[root].Rsum;
        if (k > no[root*2].right-no[root*2].Rsum && k < no[root*2+1].left+no[root*2+1].Lsum)
            return no[root*2].Rsum+no[root*2+1].Lsum;
        mid = (no[root].left+no[root].right)/2;
        if (k <= mid) return Query(k, root*2);
        else return Query(k, root*2+1);
    }
    int main ()
    {
        int n, m, k, Sum;
        char s[10];
        stack<int>Q;
        while (scanf("%d%d", &n, &m) != EOF)
        {
            Bulid(1, n, 1);
            while (m--)
            {
                scanf("%s", s);
                if (s[0] == 'D')
                {
                    scanf("%d", &k);
                    Update(k, 0, 1);
                    Q.push(k);
                }
                else if (s[0] == 'Q')
                {
                    scanf("%d", &k);
                    Sum = Query(k, 1);
                    printf("%d
    ", Sum);
                }
                else if (s[0] == 'R')
                {
                    Update(Q.top(), 1, 1);
                    Q.pop();
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    A5的文档(PDF或doc等)打印至A4纸上,使其自动缩放至适合纸张的大小,并不改变原格式
    世界各国各个国家网址网站域名后缀名国名英文缩写/简称对照表
    企业支付宝获得收款二维码的方法
    “Windows照片查看器无法显示此图片,因为计算机上的可用内存可能不足”的解决方案
    打开、查找超大文件可使用—— EmEditor
    reportqueue文件夹占用C盘大量空间的解决方案
    WPS表格:将名用“×”代替而保留姓(如:将“王老五”展示为“王××”,将“张三”展示为“张×”)
    WPS表格:通过最末几位字得出一个值(比如通过单位名判断是“法人组织”还是“非法人组织”)
    WPS表格:隐藏身份证号的几种方法(1、显示前几位;2、隐藏中间几位)
    WPS表格:批量粘贴身份证号,防止出现“科学计数法”或“末尾三个零”
  • 原文地址:https://www.cnblogs.com/syhandll/p/4699510.html
Copyright © 2011-2022 走看看