3226: [Sdoi2008]校门外的区间

本文介绍了一种使用线段树解决涉及集合的并、交、差等操作的问题的方法。通过特殊的区间转换技巧,实现对集合的有效管理和更新,并提供了一个完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

SSTS=STS=STS=STS=TSS=ST

题解:大力线段树
[l,r],l2,r2(l,r),l2+1,r21

1.区间覆盖1
2.补集覆盖0
3.区间覆盖0
4.操作2+区间取反
5.区间取反

需要用标记维护覆盖和取反,这两种标记在线段树中无法合并,但是在询问的时候没有区间查询,而一旦出现要标记合并的地方,就提前下传

不能合并就传下去

这里认为区间覆盖的优先级比较高,在下放标记时,有覆盖标记就清除取反标记,若有取反标记先考虑覆盖标记,次考虑取反标记

然后这题的难度主要在输出答案上面

我的收获:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

const int n=65536*2+1;
const int M=n+5;
#define ls x<<1
#define rs x<<1|1
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
#define root 1,n,1

int cov[M<<2],rev[M<<2];
bool flag;

inline int read()
{
    int x=0,f=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='(')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    if(ch==')')f=1;
    return x*2-f;
}

inline void pushdown(int x,int m)
{
    if(m==1) return;
    if(cov[x]!=-1) cov[ls]=cov[rs]=cov[x],rev[ls]=rev[rs]=0;
    if(rev[x]){
        if(cov[ls]!=-1) cov[ls]^=1;else rev[ls]^=1;
        if(cov[rs]!=-1) cov[rs]^=1;else rev[rs]^=1;
    }
    cov[x]=-1,rev[x]=0;
}

void build(int l,int r,int x)
{
    cov[x]=-1,rev[x]=0;
    if(l==r) return ;
    int m=(l+r)>>1;
    build(lson);build(rson);
}

int query(int k,int l,int r,int x) 
{
    if(l==r){
        if(cov[x]!=-1) return cov[x];
        return rev[x];
    }
    pushdown(x,r-l+1);
    int m=(l+r)>>1;
    if(k<=m) return query(k,lson);
    return query(k,rson);
}

void modify(int L,int R,int v,int l,int r,int x)
{
    if(L<=l&&r<=R){
        if(v==-1){
            if(cov[x]!=-1) cov[x]^=1;
            else rev[x]^=1;
        }
        else cov[x]=v,rev[x]=0;
        return ;
    }
    pushdown(x,r-l+1);
    int m=(l+r)>>1;
    if(L<=m) modify(L,R,v,lson);
    if(R>m) modify(L,R,v,rson);
}

inline void print(){
    int start=-1,last=-1,flag=0;
    for(int i=1;i<=n;i++)
        if(query(i,root))
        {
            if(start==-1)start=i;
            last=i;
        }
        else 
        {
            if(start!=-1)
            {
                if(flag)printf(" ");
                else flag=1;
                if(start&1)printf("(");
                else printf("[");
                printf("%d",start/2-1);
                printf(",");
                printf("%d",(last+1)/2-1);
                if(last&1)printf(")");
                else printf("]");
            }
            last=start=-1;
        }
    if(!flag)puts("empty set");
}

void work(){
    print();
}

void init()
{
    build(root);
    char opt[5];int x,y;
    while(scanf("%s",opt)!=EOF){
        x=read(),y=read();x+=2;y+=2;
        switch(opt[0])
        {
            case 'U':modify(x,y,1,root);break;
            case 'I':modify(1,x-1,0,root),modify(y+1,n,0,root);break;
            case 'D':modify(x,y,0,root);break;
            case 'C':modify(1,x-1,0,root),modify(y+1,n,0,root),modify(x,y,-1,root);break;
            case 'S':modify(x,y,-1,root);
        }
    }
}

int main()
{
    init();
    work();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值