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;
    }
  • 相关阅读:
    [转载]Jupyter Notebook 的快捷键
    【转载】mysql行列转换方法总结
    【Deep Learning Nanodegree Foundation笔记】第 10 课:Sentiment Analysis with Andrew Trask
    赵铁夫讲单词·解密单词的潜规则【2016全新精华版】
    【转载】ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
    [IJCAI-17 口碑商家客流量预测]
    【Deep Learning Nanodegree Foundation笔记】第 9 课:Model Evaluation and Validation
    【Deep Learning Nanodegree Foundation笔记】第 5 课:Logistic Regression
    不用动笔背单词之托福
    Jenkins--02命令行构建Job
  • 原文地址:https://www.cnblogs.com/syhandll/p/4699510.html
Copyright © 2011-2022 走看看