• hdu 1540 Tunnel Warfare 线段数区间合并


    Tunnel Warfare

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    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
    思路:线段数区间合并;
    #include<bits/stdc++.h>
    using namespace std;
    #define ll __int64
    #define esp 0.00000000001
    const int N=1e5+10,M=1e6+10,inf=1e9+10,mod=1000000007;
    int max(int x,int y,int z)
    {
        return max(x,max(y,z));
    }
    struct is
    {
        int l,r,len,lans,rans,sans;//这个区间的左端连续最大,右端,区间连续最大
    }tree[N*4];
    void buildtree(int l,int r,int pos)
    {
        tree[pos].l=l;
        tree[pos].r=r;
        tree[pos].len=tree[pos].lans=tree[pos].rans=tree[pos].sans=(r-l+1);
        if(l==r)
        return;
        int mid=(l+r)>>1;
        buildtree(l,mid,pos<<1);
        buildtree(mid+1,r,pos<<1|1);
    }
    void update(int point,int change,int pos)
    {
        if(tree[pos].l==point&&tree[pos].r==tree[pos].l)
        {
            tree[pos].len=tree[pos].lans=tree[pos].rans=tree[pos].sans=change;
            return;
        }
        int mid=(tree[pos].l+tree[pos].r)>>1;
        if(point<=mid)
        update(point,change,pos<<1);
        else
        update(point,change,pos<<1|1);
        tree[pos].lans=tree[pos<<1].lans;
        tree[pos].rans=tree[pos<<1|1].rans;
        tree[pos].sans=max(tree[pos<<1].sans,tree[pos<<1|1].sans,tree[pos<<1].rans+tree[pos<<1|1].lans);
        if(tree[pos<<1].lans==tree[pos<<1].r-tree[pos<<1].l+1)
        tree[pos].lans+=tree[pos<<1|1].lans;
        if(tree[pos<<1|1].rans==tree[pos<<1|1].r-tree[pos<<1|1].l+1)
        tree[pos].rans+=tree[pos<<1].rans;
    }
    int query(int x,int pos)
    {
        if(tree[pos].l==x&&tree[pos].r==x||tree[pos].sans==0||tree[pos].sans==tree[pos].r-tree[pos].l+1)
        {
            return tree[pos].sans;
        }
        int mid=(tree[pos].l+tree[pos].r)>>1;
        if(x<=mid)
        {
            if(x>=tree[pos<<1].r-tree[pos<<1].rans+1)
            return query(x,pos<<1)+query(mid+1,pos<<1|1);
            else
            return query(x,pos<<1);
        }
        else
        {
            if(x<=tree[pos<<1|1].l+tree[pos<<1|1].lans-1)
            return query(x,pos<<1|1)+query(mid,pos<<1);
            else
            return query(x,pos<<1|1);
        }
    }
    char a[10];
    int main()
    {
        int x,y,z,i,t;
        int T;
        while(~scanf("%d%d",&x,&y))
        {
            stack<int>s;
            buildtree(1,x,1);
            for(i=0;i<y;i++)
            {
                scanf("%s",a);
                if(a[0]=='D')
                {
                    scanf("%d",&z);
                    update(z,0,1);
                    s.push(z);
                }
                else if(a[0]=='Q')
                {
                    scanf("%d",&z);
                    printf("%d
    ",query(z,1));
                }
                else
                {
                    update(s.top(),1,1);
                    s.pop();
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    单例和工厂设计模式
    EFCore中如何移除主外键关系
    使用.Net Core实现FNV分布式hash一致性算法
    我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
    FNV哈希算法
    AspNetCore中基于Session的身份验证
    详解Microsoft.AspNetCore.CookiePolicy
    理解cookies
    详解Asp.Net Core中的Cookies
    5. 安装Dashboard UI、heapster
  • 原文地址:https://www.cnblogs.com/jhz033/p/5688433.html
Copyright © 2020-2023  润新知