码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3225 Help with Intervals

时间:2014-07-29 12:30:16      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   div   ar   

U:把区间[l,r]覆盖成1
I:把[0,l-1][r+1,∞]覆盖成0
D:把区间[l,r]覆盖成0
C:把[0,l-1][r+1,∞]覆盖成0 , 且[l,r]区间0/1互换(即异或)
S:[l,r]区间0/1互换

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include <iostream>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define debug(x) printf(#x"= %d\n",x);
#define N 140000
using namespace std;
int Val[N*4],Xor[N*4];
bool mark[N];
const int maxn =(65540<<1);
void build(int l,int r,int i)
{
    Val[i]=-1;
    Xor[i]=0;
    if(l!=r)
    {
        int mid=(l+r)>>1;
        build(l,mid,L(i));
        build(mid+1,r,R(i));
    }
}
void pushdown(int i)
{
    if(Val[i]!=-1)
    {
        Xor[L(i)]=Xor[R(i)]=0;
        Val[L(i)]=Val[R(i)]=Val[i];
        Val[i]=-1;
    }
    if(Xor[i])
    {
        Xor[L(i)]^=1;
        Xor[R(i)]^=1;
        Xor[i]=0;
    }
}
void update(int l,int r,int pl,int pr,int type,int va,int i)
{
    if(l>=pl&&r<=pr)
    {
        if(type==1){Xor[i]=0;Val[i]=va;return;}
        else {
            Xor[i]^=1;
            return;
        }
    }
    pushdown(i);
    int mid=(l+r)>>1;
    if(pl<=mid)update(l,mid,pl,pr,type,va,L(i));
    if(pr>mid)update(mid+1,r,pl,pr,type,va,R(i));
}
void query(int l,int r,int i)
{
    if(l==r)
    {
        if(Val[i]==-1)Val[i]=0;
        Val[i]^=Xor[i];
        if(Val[i])
            mark[l]=true;
        return ;
    }
    pushdown(i);
    int mid=(l+r)>>1;
    query(l,mid,L(i));
    query(mid+1,r,R(i));
}
void solve(int l,int r,int pl,int pr,char type)
{
    if(type==U)update(l,r,pl,pr,1,1,1);
    else if(type==I){
        if(pl-1>=0)
            update(l,r,0,pl-1,1,0,1);
        update(l,r,pr+1,maxn,1,0,1);
    }
    else if(type==D){
        update(l,r,pl,pr,1,0,1);
    }
    else if(type==C){
        if(pl-1>=0)
            update(l,r,0,pl-1,1,0,1);
        update(l,r,pr+1,maxn,1,0,1);
        update(l,r,pl,pr,2,1,1);
    }
    else
    {
        update(l,r,pl,pr,2,1,1);
    }

}

int main() {
    char a,b,c,d;
    int l,r;
    build(0,maxn,1);
    while(scanf(" %c %c %d %c %d %c",&a,&b,&l,&c,&r,&d)!=EOF)
    {
        l<<=1;
        r<<=1;
        if(b==()l++;
        if(d==))r--;
        if(l>r)
        {
            if(a==I||a==C)
            {
                Val[1]=Xor[1]=0;
            }
        }
        else
            solve(0,maxn,l,r,a);
    }
    memset(mark,0,sizeof(mark));
    query(0,maxn,1);
    int st,ed;
    st=ed=-1;
    int first=1;
    for(int i=0;i<=maxn;++i)
    {
        if(mark[i]){
            if(st==-1)st=i;
            ed=i;
        }
        else if(st!=-1)
        {
            if(!first)
                printf(" ");
            if(st&1)printf("(");
            else printf("[");
            printf("%d,%d",(st>>1),((ed+1)>>1));
            if(ed&1)printf(")");
            else printf("]");
            st=-1;
            first=0;
        }
    }
    if(first)printf("empty set");
    puts("");
    return 0;
}

 

POJ 3225 Help with Intervals,布布扣,bubuko.com

POJ 3225 Help with Intervals

标签:style   blog   color   os   io   for   div   ar   

原文地址:http://www.cnblogs.com/L-Ecry/p/3874746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!