• 洛谷P2894 [USACO08FEB]酒店Hotel_区间更新_区间查询


    Code:

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    const int N = 200000+5;
    int n,m,k;
    struct Segment_Tree{
        int lazy,left,right,maxv;
    }Seg[N<<2];
    inline void push_down(int o,int l,int r)
    {
        if(Seg[o].lazy == -1 || l==r)return;
        int mid = (l+r)>>1;
        int lz = Seg[o].lazy, ls=o<<1, rs=(o<<1)|1;                                             
        Seg[ls].lazy = Seg[rs].lazy = lz;                           
        Seg[ls].maxv = lz==0 ? mid-l+1: 0;                                
        Seg[rs].maxv = lz==0 ?  r-mid:  0;                                 
        Seg[ls].left = Seg[ls].right = Seg[ls].maxv;                   
        Seg[rs].left = Seg[rs].right = Seg[rs].maxv;        
        Seg[o].lazy  = -1;                                                    
    }
    inline void update_seg(int o,int l,int r)
    {
        int mid = (l+r)>>1, ls=o<<1, rs=(o<<1)|1;
        Seg[o].maxv  = max(Seg[ls].maxv, Seg[rs].maxv);
        Seg[o].maxv  = max(Seg[o].maxv, Seg[ls].right+Seg[rs].left);
        Seg[o].left  = Seg[ls].maxv == mid-l+1 ? Seg[ls].maxv+Seg[rs].left : Seg[ls].left;
        Seg[o].right = Seg[rs].maxv == r-mid   ? Seg[rs].maxv+Seg[ls].right: Seg[rs].right;
    }
    void build_Tree(int l,int r,int o){
        if(l>r)return;
        if(l==r)
        {
            Seg[o].left = Seg[o].right = Seg[o].maxv = 1;
            Seg[o].lazy = -1;
            return;
        }
        int mid = (l+r)>>1, ls=o<<1, rs=(o<<1)|1;
        build_Tree(l,mid,ls);
        build_Tree(mid+1,r,rs);
        update_seg(o,l,r);
        Seg[o].lazy = -1;
    }
    void update(int L,int R,int l,int r,int val,int o){
        if(L>R)return;
        if(L>=l&&R<=r)
        {
            Seg[o].lazy = val;
            Seg[o].maxv = (val==0)?R-L+1: 0;
            Seg[o].left = Seg[o].right = Seg[o].maxv;        
            return;
        }
        push_down(o,L,R);
        int mid = (L+R)>>1,ls=o<<1, rs=(o<<1)|1;
        if(l<=mid) update(L,mid,l,r,val,ls);
        if(r>mid)  update(mid+1,R,l,r,val,rs);
        update_seg(o,L,R);
    }
    int query(int o,int l,int r){
        int mid=(l+r)>>1, ls=o<<1, rs=(o<<1)|1, ans;
        push_down(o,l,r);
        if(Seg[o].left >= k)
            ans = l;
    	else if(Seg[ls].maxv >=k )
            ans = query(ls,l,mid);
    	else if(Seg[ls].right+Seg[rs].left >= k)           
            ans = mid+1-Seg[ls].right;
    	else  
            ans = query( rs ,mid+1,r);
        update_seg(o,l,r);
        return ans;
    }
    inline int solve_1(){
        if(Seg[1].maxv < k)return 0;
        return query(1,1,n);
    }
    int main()
    {
      //  freopen("in.txt","r",stdin);
       // freopen("out.txt","w",stdout);
        scanf("%d%d",&n,&m);
        build_Tree(1,n,1);
        while(m--)
        {
            int op,x,y;
            scanf("%d%d",&op,&x);
            if(op==1)
            {
                k=x;
                int ans=solve_1();
                printf("%d
    ",ans);
                if(ans!=0)update(1,n,ans,ans+k-1,1,1);
            }
            if(op==2)
            {
                scanf("%d",&y);
                update(1,n,x,x+y-1,0,1);
            }
        }
        return 0;                                           
    }
    

      

  • 相关阅读:
    [转载]SELinux 环境下网络服务设置,配置 Apache 、Samba、NFS
    [转载]SELinux安全系统基础
    Linux NFS服务器的安装与配置
    将可执行程序设为linux服务启动
    不关闭selinux下配置php+httpd访问KingbaseES
    [转载]SELinux安全系统基础
    解决samba和SELINUX 冲突
    [转载]在线文本编辑器的基本实现原理
    [转载]使用postgresql安装wordpress
    [转载]php连接postgreSQL数据库及其操作(php5,postgreSQL9)
  • 原文地址:https://www.cnblogs.com/guangheli/p/9845146.html
Copyright © 2020-2023  润新知