• POJ 3225 线段树+lazy标记


    lazy写崩了…….

    查了好久

    /*
    U—> [l,r]–>1
    I—> [1,l-1] [r+1,+无穷] –>0
    D—> [l,r]–>0
    C—> [1,l-1] [r+1,+无穷]–>0 xor[l,r]
    S—> [l,r]–>xor
    */

    //By SiriusRen
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define maxn 140000
    struct Tree{int lazy;bool cover;Tree(){lazy=-1;}}tree[maxn*16];//=0 不选  =1 选   =2 xor
    int xx,yy,vis[maxn*2];
    char op,Left,Right,f;
    void push_down(int pos){
        int lson=pos<<1,rson=pos<<1|1;
        if(tree[pos].lazy==2){
            if(tree[lson].lazy==-1)tree[lson].lazy=2,tree[lson].cover=!tree[lson].cover;
            else if(tree[lson].lazy==2)tree[lson].lazy=-1,tree[lson].cover=!tree[lson].cover;
            else tree[lson].lazy=!tree[lson].lazy,tree[lson].cover=!tree[lson].cover;
            if(tree[rson].lazy==-1)tree[rson].lazy=2,tree[rson].cover=!tree[rson].cover;
            else if(tree[rson].lazy==2)tree[rson].lazy=-1,tree[rson].cover=!tree[rson].cover;
            else tree[rson].lazy=!tree[rson].lazy,tree[rson].cover=!tree[rson].cover;
        }
        else tree[lson].cover=tree[rson].cover=tree[pos].lazy,tree[lson].lazy=tree[rson].lazy=tree[pos].lazy;
        tree[pos].lazy=-1;
    }
    void update(int l,int r,int pos,int L,int R,int id){
        if(l>=L&&r<=R){
            if(id!=2)tree[pos].cover=id,tree[pos].lazy=id;
            else{
                if(tree[pos].lazy==-1)tree[pos].lazy=2;
                else if(tree[pos].lazy==2)tree[pos].lazy=-1;
                else tree[pos].lazy=!tree[pos].lazy;
                tree[pos].cover=!tree[pos].cover;
            }
            return;
        }
        if(~tree[pos].lazy)push_down(pos);
        int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
        if(mid>=R)update(l,mid,lson,L,R,id);
        else if(mid<L)update(mid+1,r,rson,L,R,id);
        else update(l,mid,lson,L,R,id),update(mid+1,r,rson,L,R,id);
    }
    void query(int l,int r,int pos,int x){
        if(~tree[pos].lazy)push_down(pos);
        if(l==r){vis[l]=tree[pos].cover;return;}
        int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
        if(mid>=x)query(l,mid,lson,x);
        else query(mid+1,r,rson,x);
    }
    int main(){
        while(scanf("%c %c%d,%d%c",&op,&Left,&xx,&yy,&Right)!=EOF){
            xx<<=1,yy<<=1;
            if(Left=='(')xx++;if(Right==')')yy--;
            if(xx>yy)xx=yy=maxn-1;
            if(op=='U')update(0,maxn,1,xx,yy,1);
            else if(op=='I'){
                if(xx)update(0,maxn,1,0,xx-1,0);
                update(0,maxn,1,yy+1,maxn,0);
            }
            else if(op=='D')update(0,maxn,1,xx,yy,0);
            else if(op=='C'){
                if(xx)update(0,maxn,1,0,xx-1,0);
                update(0,maxn,1,yy+1,maxn,0);
                update(0,maxn,1,xx,yy,2);
            }
            else if(op=='S')update(0,maxn,1,xx,yy,2);
            getchar();
        }
        for(int i=0;i<maxn;i++)query(0,maxn,1,i);
        xx=-1;
        for(int i=0;i<135000;i++){
            if(vis[i]&&~xx)yy=i;
            else if(!vis[i]){
                if(~xx){
                    if(f)printf(" ");
                    if(!f)f=1;
                    if(xx&1)putchar('(');
                    else putchar('[');
                    printf("%d,%d",xx>>1,(yy+1)>>1);
                    if(yy&1)putchar(')');
                    else putchar(']');
                }
                xx=-1;
            }
            else if(vis[i]&&xx==-1)xx=yy=i;
        }
        if(!f)printf("empty set");
    }

    这里写图片描述

  • 相关阅读:
    检查宿舍卫生
    代码优化中 主窗口 问题 解决
    授权 存储
    升级Xcode7&iOS9后,出现NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -980X)
    控制器里面添加控制器
    版本新特性
    ios图片适配问题
    启动图片调整
    luogu_1896【题解】状压DP
    luogu_2602【题解】数位DP 数字计数
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6532283.html
Copyright © 2020-2023  润新知