• 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;
    }
  • 相关阅读:
    数据分析 --- 01. Numpy
    爬虫 --- 08. 全站爬取(CrawlSpider), 分布式, 增量式爬虫
    爬虫 --- 07. 全站爬取(手动), post请求,cookie, 传参,中间件,selenium
    数据结构 --- 02. 内存, 顺序表, 单链表
    数据结构 --- 01. 时间复杂度,timeit模块,栈,队列,双端队列
    爬虫 --- 06. scrapy框架初始,移动端数据爬取
    爬虫 --- 05. 异步协程, 浏览器自动化,
    爬虫 --- 04. 代理服务器, 验证码识别, 处理cookie,线程池
    爬虫 --- 02. 爬取图片,数据解析
    一个关于const 变量作为map键值的Bug
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6533936.html
Copyright © 2020-2023  润新知