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;
    }
  • 相关阅读:
    为什么这年头蓝牙功能越来越差
    猜数字-暴力枚举
    怎么使用PHPMailer实现邮件的发送??
    实现windows操作系统和VB下Linux虚拟操作系统相互传取文件方式总结
    第一篇 对Javascript中原型的深入理解
    每天进步一点点——关于SSD写入放大问题
    两步改动CentOS主机名称
    [CentOs7]搭建ftp服务器
    Another app is currently holding the yum lock
    [CentOs7]安装mysql(2)
  • 原文地址:https://www.cnblogs.com/syhandll/p/4699510.html
Copyright © 2011-2022 走看看