zoukankan      html  css  js  c++  java
  • hdu1540 Tunnel Warfare

    Tunnel Warfare

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


    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个村庄,每个操作:
    D x 表示摧毁x
    Q x 询问与x相连接的有多少个
    R 恢复之前摧毁的一个城市
    这道题是线段树的区间合并。
    首先用0表示已经摧毁的村庄,1表示未摧毁或已经修复的村庄
    记录的信息有两个:
    1.l表示该区间从左数的最长1串
    2.r表示该区间从右数的最长1串
    pushup时在长度没有覆盖整个左区间或右区间时直接做

    tr[x].l=tr[ls].l;
    tr[x].r=tr[rs].r;

    如果完整覆盖了左或右区间再做特殊处理

    if(tr[ls].l==mid-l+1) tr[x].l+=tr[rs].l;
    if(tr[rs].r==r-mid) tr[x].r+=tr[ls].r;

    所以对于摧毁和恢复直接进行单点修改,将tr[x].l=tr[x].r=z(z为0或1)

    对于询问,可以分别查询a向左的最长1串和a向右最长1串-1

    特别的如果a已经被摧毁,则ans=-1

    此时要做特判。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define ls x<<1
    #define rs x<<1|1
    const int N=50005;
    struct X
    {
        int l,r;
    }tr[N<<2];
    int st[N];
    void bu(int l,int r,int x)
    {
        if(l==r) tr[x].l=tr[x].r=1;
        else
        {
            int mid=l+(r-l)/2;
            bu(l,mid,ls);
            bu(mid+1,r,rs);
            tr[x].l=tr[x].r=r-l+1;
        }
    }
    void chan(int l,int r,int x,int w,int z)
    {
        if(l==r) tr[x].l=tr[x].r=z;
        else
        {
            int mid=l+(r-l)/2;
            if(w<=mid) chan(l,mid,ls,w,z);
            else chan(mid+1,r,rs,w,z);
            tr[x].l=tr[ls].l;tr[x].r=tr[rs].r;
         if(tr[x].l==mid-l+1) tr[x].l+=tr[rs].l; if(tr[x].r==r-mid) tr[x].r+=tr[ls].r; } } int ask1(int l,int r,int x,int w) { if(w==r) return tr[x].r; else { int mid=l+(r-l)/2,re; if(w>mid) { re=ask1(mid+1,r,rs,w); if(w-mid==re) re+=tr[ls].r; } else re=ask1(l,mid,ls,w); return re; } } int ask2(int l,int r,int x,int w) { if(w==l) return tr[x].l; else { int mid=l+(r-l)/2,re; if(w<=mid) { re=ask2(l,mid,ls,w); if(mid-w+1==re) re+=tr[rs].l; } else re=ask2(mid+1,r,rs,w); return re; } } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { int s=0; memset(st,0,sizeof(st)); memset(tr,0,sizeof(tr)); bu(1,n,1); while(m--) { char c; scanf(" %c",&c); if(c=='D') { scanf("%d",&st[++s]); chan(1,n,1,st[s],0); } else if(c=='R') chan(1,n,1,st[s--],1); else { int a,ans; scanf("%d",&a); ans=ask1(1,n,1,a)+ask2(1,n,1,a)-1; if(ans>0) printf("%d ",ans); else printf("0 "); } } } return 0; }
  • 相关阅读:
    iOS开发-文件管理(一)
    浅析栈区和堆区内存分配的区别
    浅谈Block传值-匿名函数(代码块)
    cell的各种使用和赋值 总结
    类方法和对象方法的区别
    属性传值 ,代理传值,单例
    类目,延展,协议
    任意点 并查集
    Codeforces 145E. Lucky Queries 线段树
    Codeforces 103B. Cthulhu 并查集运用
  • 原文地址:https://www.cnblogs.com/bzmd/p/6224428.html
Copyright © 2011-2022 走看看