POJ 3225 Help with Intervals

终于A了这题了,槽点略多:

  1. 开闭区间使用奇偶数来区分开,这点非常巧妙,自己在写的时候没有这样想过。
  2. 以前一直认为有多个lazy要维护的时候,在每次操作前就down掉就能避免掉矛盾操作的情况。这题发现是不行的,因为如果两次对同样的区间进行不同的操作,那子区间还是得到了两个操作的lazy,所以只能仔细分析每个操作的优先顺序。比如这题set操作明显是盖过xor的,如果要set了,那么下面的xor也就不用搞了。而如果要xor下面的set还是要搞,所以同时出现两个lazy的情况说明set一定是先来的,那就先set再xor。坑:如果操作再多一点是什么情况……
  3. 调试的时候出现一个问题,当时把display放到主循环最后一句,发现这样可以得到正确答案,但是拿出来就会出错。当时觉得很不可思议,因为这除了跳出主循环以为并没有做任何事情。后来各种脑残调试N久才发现每次都display了就每次都query到底了,所以不存在两个lazy的情况……
/*
 Author : Speedcell
 Update : 2013-05-30
Version : soppYcell 2.2(a)
*/

#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <memory>
#include <complex>
#include <numeric>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <locale.h>

using namespace std;

#pragma pack(4)

#ifndef __CONSTANT__
#define __CONSTANT__

typedef long long LONG;

const double pi = acos(-1.0);
const int   inf = 0x7f7f7f7f;
const LONG  INF = 0x7f7f7f7f7f7f7f7fll;

const int go[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};

#endif // __CONSTANT__

#ifndef __IO__
#define __IO__

inline bool RD(int    & a) {return scanf("%d",&a)!=EOF;}
inline bool RD(char   & a) {return scanf("%c",&a)!=EOF;}
inline bool RD(char   * a) {return scanf("%s", a)!=EOF;}
inline bool RD(double & a) {return scanf("%lf",&a)!=EOF;}
inline bool RD(LONG   & a) {return scanf("%I64d",&a)!=EOF;}

template<class T1> inline bool
    IN(T1 & a) {return RD(a);}
template<class T1,class T2> inline bool
    IN(T1 & a,T2 & b) {return RD(a)&&RD(b);}
template<class T1,class T2,class T3> inline bool
    IN(T1 & a,T2 & b,T3 & c) {return RD(a)&&RD(b)&&RD(c);}
template<class T1,class T2,class T3,class T4> inline bool
    IN(T1 & a,T2 & b,T3 & c,T4 & d) {return RD(a)&&RD(b)&&RD(c)&&RD(d);}
template<class T1,class T2,class T3,class T4,class T5> inline bool
    IN(T1 & a,T2 & b,T3 & c,T4 & d,T5 & e) {return RD(a)&&RD(b)&&RD(c)&&RD(d)&&RD(e);}
template<class T1,class T2,class T3,class T4,class T5,class T6> inline bool
    IN(T1 & a,T2 & b,T3 & c,T4 & d,T5 & e,T6 & f) {return RD(a)&&RD(b)&&RD(c)&&RD(d)&&RD(e)&&RD(f);}
template<class T1,class T2,class T3,class T4,class T5,class T6,class T7> inline bool
    IN(T1 & a,T2 & b,T3 & c,T4 & d,T5 & e,T6 & f,T7 & g) {return RD(a)&&RD(b)&&RD(c)&&RD(d)&&RD(e)&&RD(f)&&RD(g);}

inline void PT(int    a) {printf("%d",a);}
inline void PT(char   a) {printf("%c",a);}
inline void PT(char * a) {printf("%s",a);}
inline void PT(double a) {printf("%f",a);}
inline void PT(LONG   a) {printf("%I64d",a);}
inline void PT(const char a[]) {printf("%s",a);}

template<class T1> inline void
    OT(T1 a) {PT(a);}
template<class T1,class T2> inline void
    OT(T1 a,T2 b) {PT(a),PT(' '),PT(b);}
template<class T1,class T2,class T3> inline void
    OT(T1 a,T2 b,T3 c) {PT(a),PT(' '),PT(b),PT(' '),PT(c);}
template<class T1,class T2,class T3,class T4> inline void
    OT(T1 a,T2 b,T3 c,T4 d) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d);}
template<class T1,class T2,class T3,class T4,class T5> inline void
    OT(T1 a,T2 b,T3 c,T4 d,T5 e) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e);}
template<class T1,class T2,class T3,class T4,class T5,class T6> inline void
    OT(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PT(f);}
template<class T1,class T2,class T3,class T4,class T5,class T6,class T7> inline void
    OT(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f,T7 g) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PT(f),PT(' '),PT(g);}

template<class T1> inline void
    OL(T1 a) {PT(a),PT('\n');}
template<class T1,class T2> inline void
    OL(T1 a,T2 b) {PT(a),PT(' '),PT(b),PT('\n');}
template<class T1,class T2,class T3> inline void
    OL(T1 a,T2 b,T3 c) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT('\n');}
template<class T1,class T2,class T3,class T4> inline void
    OL(T1 a,T2 b,T3 c,T4 d) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT('\n');}
template<class T1,class T2,class T3,class T4,class T5> inline void
    OL(T1 a,T2 b,T3 c,T4 d,T5 e) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT('\n');}
template<class T1,class T2,class T3,class T4,class T5,class T6> inline void
    OL(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PT(f),PT('\n');}
template<class T1,class T2,class T3,class T4,class T5,class T6,class T7> inline void
    OL(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f,T7 g) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PT(f),PT(' '),PT(g),PT('\n');}

#endif // __IO__

#ifndef __MACRO__
#define __MACRO__

#define ML(times) int tcase; IN(tcase); FOR(times,1,tcase)

#define FOR(i,a,b) for(int i=int(a),_##i=int(b);i<=_##i;i++)
#define DWN(i,b,a) for(int i=int(b),_##i=int(a);_##i<=i;i--)
#define ECH(i,u,pre,next) for(int i=int(pre[u]);i!=-1;i=int(next[i]))

#define MEM(a,v) memset(a,v,sizeof(a))
#define CLR(a,v) FOR(_i##a,0,sizeof(a)/sizeof(a[0])-1) a[_i##a]=v

#define LOOP(a,n)                                               \
    FOR(_i##a,0,(n)-1)                                          \
        OT(a[_i##a]),OT(_i##a!=__i##a?' ':'\n')
#define LOOP2(a,n,m)                                            \
    FOR(_i##a,0,(n)-1) FOR(_j##a,0,(m)-1)                       \
        OT(a[_i##a][_j##a]),OT(_j##a!=__j##a?' ':'\n')
#define LOOPG(G,n,pre,next)                                     \
    FOR(_i##a,0,(n)-1) ECH(_j##a,_i##a,pre,next)                \
        OL(_i##a,G[_j##a].v,G[_j##a].w)

#endif // __MACRO__

#ifndef __BIT__
#define __BIT__

template<class T> inline T lb(T i) {return i&-i;}
template<class T> inline T lc(T i) {return i<<1;}
template<class T> inline T rc(T i) {return i<<1|1;}
template<class T> inline T at(T a,int i) {return a& (T(1)<<i);}
template<class T> inline T nt(T a,int i) {return a^ (T(1)<<i);}
template<class T> inline T s1(T a,int i) {return a| (T(1)<<i);}
template<class T> inline T s0(T a,int i) {return a&~(T(1)<<i);}

#endif // __BIT__

#ifndef __DOUBLE__
#define __DOUBLE__

const double eps = 1e-8;

inline int cmp(double a,double b=0) {return fabs(a-b)<eps?0:((a-b)>eps?+1:-1);}

inline double fmax(double a,double b) {return cmp(a,b)>0?a:b;}
inline double fmin(double a,double b) {return cmp(a,b)<0?a:b;}

#endif // __DOUBLE__

const int MAXV = 140000;

class SegmentTree
{
private:
	int L,R,val[MAXV<<2],lxor[MAXV<<2],lset[MAXV<<2];

	#define ls l,(l+r)/2,lc(i)
	#define rs (l+r)/2+1,r,rc(i)

	void XOR(int l,int r,int i)
	{
		if(l<=r)
		{
			val[i]^=1;
			lxor[i]^=1;
		}
	}
	void SET(int v,int l,int r,int i)
	{
		if(l<=r)
		{
			val[i]=v;
			lset[i]=v;
			lxor[i]=0;
		}
	}
	void down(int l,int r,int i)
	{
		if(l<r)
		{
			if(lset[i]!=-1)
			{
				SET(lset[i],ls);
				SET(lset[i],rs);
				lset[i]=-1;
			}
			if(lxor[i])
			{
				XOR(ls);
				XOR(rs);
				lxor[i]=0;
			}
		}
	}
	void update_set(int x,int y,int v,int l,int r,int i)
	{
		if(x<=l&&r<=y) SET(v,l,r,i);
		else
		{
			down(l,r,i);
			if(x<=(l+r)/2) update_set(x,y,v,ls);
			if((l+r)/2<y) update_set(x,y,v,rs);
		}
	}
	void update_xor(int x,int y,int l,int r,int i)
	{
		if(x<=l&&r<=y) XOR(l,r,i);
		else
		{
			down(l,r,i);
			if(x<=(l+r)/2) update_xor(x,y,ls);
			if((l+r)/2<y) update_xor(x,y,rs);
		}
	}
	int query(int x,int l,int r,int i)
	{
		if(x==l&&r==x) return val[i];
		else
		{
			down(l,r,i);
			if(x<=(l+r)/2) return query(x,ls);
			else return query(x,rs);
		}
	}
public:
	void clear(int l=0,int r=MAXV-1)
	{
		L=l;
		R=r;
		MEM(val,0);
		MEM(lxor,0);
		MEM(lset,-1);
	}
	void update_set(int x,int y,int v)
	{
		if(x<=y) update_set(x,y,v,L,R,1);
	}
	void update_xor(int x,int y)
	{
		if(x<=y) update_xor(x,y,L,R,1);
	}
	int query(int x)
	{
		return query(x,L,R,1);
	}
}st;

int x,y;
char o,a,b;

void display(void)
{
	char a,b;
	int x,y,left=-1,right=-1;
	bool flag=false,tot=false;

	FOR(i,0,MAXV-1)
	{
		if(st.query(i))
		{
			if(!flag)
			{
				flag=true;
				left=right=i;
			}
			else right++;
		}
		else if(flag)
		{
			if(tot) printf(" ");
			if(left%2==0) a='[',left=left/2;
			else a='(',left=(left-1)/2;
			if(right%2==0) b=']',right=right/2;
			else b=')',right=(right+1)/2;
			printf("%c%d,%d%c",a,left,right,b);
			flag=false;
			tot=true;
		}
	}
	if(!tot) printf("empty set");
	printf("\n");
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("Help with Intervals.txt","r",stdin);
    #else
    #endif

    st.clear();
    while(scanf("%c %c%d,%d%c\n",&o,&a,&x,&y,&b)!=EOF)
	{
		x*=2;
		y*=2;
		if(a=='(') x++;
		if(b==')') y--;
		if(o=='U')
		{
			st.update_set(x,y,1);
		}
		else if(o=='I')
		{
			st.update_set(0,x-1,0);
			st.update_set(y+1,MAXV-1,0);
		}
		else if(o=='D')
		{
			st.update_set(x,y,0);
		}
		else if(o=='C')
		{
			st.update_set(0,x-1,0);
			st.update_set(y+1,MAXV-1,0);
			st.update_xor(x,y);
		}
		else if(o=='S')
		{
			st.update_xor(x,y);
		}
	}
	display();

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值