zoukankan      html  css  js  c++  java
  • HDU1540(线段树统计连续长度)

    ---恢复内容开始---

    Tunnel Warfare


    Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
    模板题目,解释代码中
    #include"cstdio"
    #include"algorithm"
    using namespace std;
    const int MAXN=50005;
    struct node{
        int l,r;
        int ll,rl,ml;
    }a[MAXN*3];
    void build(int rt,int l,int r)
    {
        a[rt].l=l;
        a[rt].r=r;
        a[rt].ll=a[rt].rl=a[rt].ml=r-l+1;
        if(l==r)    return ;
        int mid=(l+r)>>1;
        build(rt<<1,l,mid);
        build((rt<<1)|1,mid+1,r);
    }
    
    void merge(int rt)
    {
        //若左子树已满,则应与右子树左区间合并 
        a[rt].ll=a[rt<<1].ll;
        if(a[rt<<1].ll==(a[rt<<1].r-a[rt<<1].l+1))
        {
            a[rt].ll+=a[(rt<<1)|1].ll;
        }
        
        //若右子树已满,则应与左子树的有区间合并 
        a[rt].rl=a[(rt<<1)|1].rl;
        if(a[(rt<<1)|1].rl==(a[(rt<<1)|1].r-a[(rt<<1)|1].l+1))
        {
            a[rt].rl+=a[rt<<1].rl;
        }
        //该子树的最大连续长度为 左或右子树连续长度的最大者 或者 将左子树右区间与右子树左区间合并的长度 
        a[rt].ml=max(max(a[rt<<1].ml,a[(rt<<1)|1].ml),a[rt<<1].rl+a[(rt<<1)|1].ll);
    }
    
    void update(int rt,int pos,int val)
    {
        if(a[rt].l==a[rt].r)
        {
            if(val)    a[rt].ll=a[rt].rl=a[rt].ml=1;
            else a[rt].ll=a[rt].rl=a[rt].ml=0;
            return ;
        }
        
        int mid=(a[rt].l+a[rt].r)>>1;
        
        if(pos<=mid)    update(rt<<1,pos,val);
        else update((rt<<1)|1,pos,val);
        merge(rt);
    }
    
    int query(int rt,int pos)
    {
        if(a[rt].l==a[rt].r||a[rt].ml==0||a[rt].ml==a[rt].r-a[rt].l+1)//到达叶子节点或者该节点已满或空,那么不必向下走了 
        {
            return a[rt].ml;
        }
        
        int mid=(a[rt].l+a[rt].r)>>1;
        
        if(pos<=mid)//在左子树中 
        {
            if(pos>=a[rt<<1].r-a[rt<<1].rl+1)//若在左子树的右区间 
            {
                return query(rt<<1,pos)+query((rt<<1)|1,mid+1);//则需要查询右子树 
            }
            else
            {
                return query(rt<<1,pos);
            }
            
        }
        else
        {
            if(pos<=a[(rt<<1)|1].l+a[(rt<<1)|1].ll-1)//若在右子树的左区间 
            {
                return query((rt<<1)|1,pos)+query(rt<<1,mid);//则需要查询左子树 
            }
            else
            {
                return query((rt<<1)|1,pos);
            }
        }    
    }
    
    int stack[MAXN];
    int top;
    
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            int pos;
            top=0;
            build(1,1,n);
            while(m--)
            {
                scanf("%*c");
                char op;scanf("%c",&op);
                if(op=='D')
                {
                    scanf("%d",&pos);
                    stack[top++]=pos;
                    update(1,pos,0);
                }
                else if(op=='Q')
                {
                    scanf("%d",&pos);
                    printf("%d
    ",query(1,pos));
                }
                else
                {
                    pos=stack[--top];
                    update(1,pos,1);
                }
            }    
        }
    }
     
     
     

    ---恢复内容结束---

  • 相关阅读:
    转载:《TypeScript 中文入门教程》 16、Symbols
    转载:《TypeScript 中文入门教程》 15、可迭代性
    在 docker 中(linux 系统)运行 sql server
    遇到一个在 sql server 查询中,条件语句不区分大小写的问题
    设置 MySQL 的时区
    js 控制浏览器全屏显示
    使用 bat 获取当前系统 ip 地址并生成快捷方式
    在 WinForm 中使用 Anchor 对控件进行定位
    在 WinForm 中获取嵌入的资源
    在 WinForm 窗体设计器中引用 x64 格式的程序集会发生错误
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5125995.html
Copyright © 2011-2022 走看看