• Tunnel Warfare


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

    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 最后一个被毁坏的村庄重建
    
    初步思路:线段树区间合并问题
    
    #错误:暂时还没有找出来:查询写的有问题
    */
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    const int maxn=50000+5;
    int n,m;
    int res=0;//被摧毁的村庄数量
    int dis[maxn];//存放被摧毁的村庄
    char str[2];
    int x;
    struct node{//用于表示连续线段的结构体
        int c;//区间长度
        int res_l,res_r,res_m;//从左开始的最大连续片段长度
        int val_l,val_r;//左右边界的值
    };
    /****************************线段树基础模板*********************************/
    #define lson i*2, l, m
    #define rson i*2+1, m+1, r
    
    struct Segtree{
    
        node sum[maxn*4];
    
        void PushUp(int i)//向上更新
        {
            sum[i].val_l=sum[i*2].val_l;
            sum[i].val_r=sum[i*2+1].val_r;
            sum[i].res_l=sum[i*2].res_l;
            sum[i].res_r=sum[i*2+1].res_r;
            sum[i].res_m=max(sum[i*2].res_m,sum[i*2+1].res_m);
            sum[i].res_m=max(sum[i].res_m,sum[i*2].res_r+sum[i*2+1].res_l);
            if(sum[i*2].res_l==sum[i*2].c){
                sum[i].res_l+=sum[i*2+1].res_l;
            }
            if(sum[i*2+1].res_r==sum[i*2+1].c){
                sum[i].res_r+=sum[i*2].res_r;
            }
        }
    
        void build(int i,int l,int r)
        {
            sum[i].c=r-l+1;
            if(l==r)
            {    
                sum[i].val_l=sum[i].val_r=1;//刚开始的村庄都是没被破坏的
                sum[i].res_l=sum[i].res_r=sum[i].res_m=1;
                return ;
            }
            int m=(l+r)>>1;
            build(lson);
            build(rson);
            PushUp(i);
        }
        int query(int id,int i,int l,int r)
        {
            if(l==r||sum[i].res_m==0||sum[i].res_m==sum[i].c)//到了叶子节点或者该访问区间为空或者已满都不必要往下走了
                return sum[i].res_m;
            int m=(l+r)>>1;
            if(id<=m)
            {
                if(id>=m-sum[2*i].res_r+1)//因为id<=m,看左子树,左子树右边连续区间的左边界值,
                    //如果id在左子树的右区间内,则要看右子树的左区间有多长并返回
                    return query(id,lson)+query(m+1,rson);
                else
                    return query(id,lson);//如果不在左子树的右边界区间内,则只需要看左子树
            }
            else
            {
                if(id<=m+1+sum[2*i+1].res_l-1)//同理
                    return query(id,rson)+query(m,lson);
                else
                    return query(id,rson);
            }
        }
    
        void update(int id,int val,int i,int l,int r)
        {
            if(l==r)
            {
                sum[i].val_l=sum[i].val_r=val;
                sum[i].res_l=sum[i].res_r=sum[i].res_m=val;
                return ;
            }
    
            int m=(l+r)>>1;
            if(id<=m) update(id,val,lson);
            else update(id,val,rson);
            PushUp(i);
        }
    };
    Segtree segtree;
    /****************************线段树基础模板*********************************/
    void init(){
        res=0;
    }
    int main(){
        // freopen("in.txt","r",stdin);
        while(scanf("%d%d",&n,&m)!=EOF){
            init();
            segtree.build(1,1,n);
            for(int i=0;i<m;i++){
                scanf("%s",str);
                switch(str[0]){
                    case 'D':
                        scanf("%d",&x);
                        segtree.update(x,0,1,1,n);
                        dis[res++]=x;//摧毁的村庄又增加了一个
                        break;
                    case 'Q':
                        scanf("%d",&x);
                        printf("%d
    ",segtree.query(x,1,1,n));
                        break;
                    case 'R':
                        if(x>0){//如果还有摧毁的村庄的话
                            segtree.update(dis[--res],1,1,1,n);
                        }
                        break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    数位dp
    可持久化Trie
    网络流
    欧拉定理
    点、边双,圆方树
    [USACO5.3]窗体面积Window Area
    6.2三道模拟
    BZOJ2054 疯狂的馒头
    [USACO5.1]夜空繁星Starry Night
    [USACO5.1]乐曲主题Musical Themes
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6622060.html
Copyright © 2020-2023  润新知