zoukankan      html  css  js  c++  java
  • HDU1540 区间合并

    Tunnel Warfare

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 8115    Accepted Submission(s): 3142


    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
     
    Source
     题意:
    n个村庄连在一起,最初都完好,D a表示a村庄被破坏,Q a表示询问与a村庄直接或间接相连的有几个,R表示修复了最后破坏的一个村庄。
    代码:
    //线段树维护pre最大前缀,suf最大后缀,最后求某一点的答案就是其
    //前缀加上后缀。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<stack>
    using namespace std;
    const int maxn=50004;
    int suf[maxn*4],pre[maxn*4];
    void pushup(int i,int l,int r)
    {
        int m=(l+r)>>1;
        pre[i]=pre[i<<1];
        if(pre[i]==m-l+1) pre[i]+=pre[i<<1|1];
        suf[i]=suf[i<<1|1];
        if(suf[i]==r-m) suf[i]+=suf[i<<1];
    }
    void build(int i,int l,int r)
    {
        if(l==r){
            suf[i]=pre[i]=1;
            return ;
        }
        int m=(l+r)>>1;
        build(i<<1,l,m);
        build(i<<1|1,m+1,r);
        pushup(i,l,r);
    }
    void update(int id,int c,int i,int l,int r)
    {
        if(l==r){
            suf[i]=pre[i]=c;
            return ;
        }
        int m=(l+r)>>1;
        if(id<=m) update(id,c,i<<1,l,m);
        else update(id,c,i<<1|1,m+1,r);
        pushup(i,l,r);
    }
    int query_pre(int ql,int qr,int i,int l,int r)
    {
        if(ql<=l&&qr>=r) return pre[i];
        int m=(l+r)>>1,res;
        if(qr<=m) return query_pre(ql,qr,i<<1,l,m);
        if(ql>m) return query_pre(ql,qr,i<<1|1,m+1,r);
        res=query_pre(ql,qr,i<<1,l,m);
        if(res==m-max(l,ql)+1) res+=query_pre(ql,qr,i<<1|1,m+1,r);
        //注意记住写上max函数
        return res;
    }
    int query_suf(int ql,int qr,int i,int l,int r)
    {
        if(ql<=l&&qr>=r) return suf[i];
        int m=(l+r)>>1,res;
        if(qr<=m) return query_suf(ql,qr,i<<1,l,m);
        if(ql>m) return query_suf(ql,qr,i<<1|1,m+1,r);
        res=query_suf(ql,qr,i<<1|1,m+1,r);
        if(res==min(r,qr)-m) res+=query_suf(ql,qr,i<<1,l,m);
        return res;
    }
    int main()
    {
        int n,m,x;
        char ch[3];
        while(scanf("%d%d",&n,&m)==2){
            build(1,1,n);
            stack<int>s;
            while(m--){
                scanf("%s",ch);
                if(ch[0]=='D'){
                    scanf("%d",&x);
                    update(x,0,1,1,n);
                    s.push(x);
                }
                else if(ch[0]=='R'){
                    if(!s.empty()){
                        int x=s.top();s.pop();
                        update(x,1,1,1,n);
                    }
                }
                else if(ch[0]=='Q'){
                    scanf("%d",&x);
                    int ans=query_suf(1,x,1,1,n);
                    if(ans==0) printf("%d
    ",ans);
                    else{
                        ans+=query_pre(x,n,1,1,n)-1;
                        printf("%d
    ",ans);
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    LCA+线段树/树状数组 POJ2763 Housewife Wind
    图论 洛谷P2052 道路修建
    动态规划 洛谷P2365 任务安排
    GCD问题 洛谷P1372 又是毕业季I & P1414 又是毕业季II
    动态规划 洛谷P1140 相似基因
    动态规划 洛谷P1868 饥饿的奶牛
    动态规划 P1280 尼克的任务
    倍增LCA BZOJ1776 cowpol奶牛政坛
    P1416 攻击火星
    搜索 洛谷 P1434滑雪
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6533936.html
Copyright © 2011-2022 走看看