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
     
     
    题意:D k表示破坏村庄k,R表示恢复上次破坏的村庄,Q k表示查询包括k在内连续村庄的长度。
    题解:分别维护左端点序列maxo[]和右端点序列mino[].
    破坏/修复一个村子k的时候更新对应段的最大/最小值。破坏的时候把对应点的mino[]和maxo[]变成k。修复的时候把mino[]变成n+1,maxo[]变成0.
    这样查询k时[1,k]中maxo[]的最大值代表k的左端点。[k,n]中的mino[]最小值代表k的右端点。
    就相当于对坏掉的村庄做处理。查询村庄的时候分别找到左边和右边离它最近的。
    比如查询k就找[1,k]中被破坏掉的最右边的点和[k,n]中被破坏掉的最左边的点。
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 50010;
    #define lson l , m , num << 1
    #define rson m + 1 , r , num << 1|1
    int n , m , k , maxo[maxn << 2] , mino[maxn << 2];char s[2];
    void up(int num){maxo[num] = max(maxo[num << 1] , maxo[num << 1|1]); mino[num] = min(mino[num << 1] ,mino[num << 1|1]);}
    void built(int l , int r , int num){
        if(l == r) {maxo[num] = 0 ; mino[num] = n + 1;return;}
        int m = (l + r)>>1;
        built(lson);built(rson);
        up(num);
        return;
    }
    void des(int k , int l , int r , int num){
        if(l == r){maxo[num] = k; mino[num] = k;return;}
        int m = (l + r)>>1; if(k <= m) des(k,lson); if(k > m)des(k,rson);up(num);
    }
    void red(int k , int l , int r , int num){
        if(l == r){maxo[num] = 0; mino[num] = n + 1;return;}
        int m = (l + r)>>1; if(k <= m) red(k,lson); if(k > m)red(k,rson);up(num);
    }
    int getl(int L , int R , int l , int r , int num){
        if(L <= l && r <= R){return maxo[num];}
        int ans = 0;int m = (l + r) >> 1; if(L <= m) ans = max(ans , getl(L , R , lson));
        if(R > m) ans = max(ans , getl(L , R , rson));
        return ans;
    }
    int getr(int L , int R , int l , int r , int num){
        if(L <= l && r <= R){return mino[num];}
        int ans = n + 1;int m = (l + r) >> 1; if(L <= m) ans = min(ans , getr(L , R , lson));
        if(R > m) ans = min(ans , getr(L , R , rson));
        return ans;
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m)){
            built(1 , n , 1);
            stack<int>q;
            while(m--){
                scanf("%s",s);
                if(s[0] == 'D'){
                     scanf("%d",&k); des(k , 1 , n , 1);
                     q.push(k);
                }
                if(s[0] == 'R'){
                    if(q.empty()){
                        continue;
                    }
                    k = q.top(); q.pop();
                    red(k , 1 , n , 1);
                }
                if(s[0] == 'Q'){
                    scanf("%d",&k);
                    int ll = getl(1 , k , 1 , n , 1);
                    int rr = getr(k , n , 1 , n , 1);
                    int ans = rr - ll - 1;
                    if(ans < 0) ans = 0;
                    printf("%d
    ",ans);
                }
            }
        }
    }
    View Code
  • 相关阅读:
    团队项目-smart原则
    团队项目-作业管理系统
    团队模式和团队的开发模式是什么,它们有什么关系?
    软件工作量的估计有哪些方法?
    腾讯qq的发展史
    软件过程与项目管理(第二次作业)
    有学生提到,在大学选课的时候,可以写一个“刷课机”的程序,利用学校选课系统的弱点或漏洞,帮助某些人选到某些课程。或者帮助用户刷购票网站,先买到火车票。这些软件合法么?符合道德规范么?是在“软件工程”的研究范围么?
    mysql中explain的type的解释
    php 中类型转换 numfamat、round函数tips
    python client端收不到server端构造的结构体数据
  • 原文地址:https://www.cnblogs.com/rtyfcvb/p/6536507.html
Copyright © 2011-2022 走看看