题目 :http://poj.org/problem?id=3225
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define M 65540<<1
#define inf 1LL<<60
#define ll long long
struct Node
{
int l, r;
int inv, flag;
}nde[M<<2];
int head, tail;
void build( int l, int r, int p )
{
nde[p].l = l; nde[p].r = r;
nde[p].inv = nde[p].flag = 0;
if( l == r ) return;
int m = (l+r)>>1;
build( l, m, p<<1 );
build( m+1, r, p<<1|1 );
}
void Xor( int p )
{
if( nde[p].flag != -1 )
nde[p].flag = !nde[p].flag;
else
nde[p].inv = !nde[p].inv;
}
void pushDown( int p )
{
if( nde[p].flag != -1 ){
nde[p<<1].flag = nde[p<<1|1].flag = nde[p].flag;
nde[p<<1].inv = nde[p<<1|1].inv = 0;
nde[p].flag = -1;
}
if( nde[p].inv ){
Xor( p<<1 );
Xor( p<<1|1 );
nde[p].inv = 0;
}
}
void update( char op, int l, int r, int p )
{
if( l <= nde[p].l && r >= nde[p].r ){
if( op == 'U' ) nde[p].flag = 1, nde[p].inv = 0;
if( op == 'D' ) nde[p].flag = nde[p].inv = 0;
if( op == 'C' || op == 'S' ) Xor( p );
return;
}
if( l > nde[p].r || r < nde[p].l ){
if( op == 'I' || op == 'C' ) nde[p].flag = nde[p].inv = 0;
return;
}
pushDown( p );
update( op, l, r, p<<1 );
update( op, l, r, p<<1|1 );
}
void query( int p )
{
if( nde[p].flag != -1 ){
if( nde[p].flag == 1 ){
if( tail + 1 != nde[p].l ){
if( head++ ) printf( " " );
if( nde[p].l & 1 ) printf( "(%d,", nde[p].l/2 );
else printf( "[%d,", nde[p].l/2 );
}
tail = nde[p].r;
}
else{
if( head&1 ){
if( tail & 1 ) printf( "%d)", tail/2 + 1 );
else printf( "%d]", tail / 2 );
head++;
}
}
return;
}
if( nde[p].l == nde[p].r ) return;
pushDown( p );
query( p<<1 );
query( p<<1|1 );
}
int main()
{
//freopen( "a.in", "r", stdin );
char op, a, b = ',', c;
int x, y;
build( 0, M, 1 );
while( scanf( " %c %c%d,%d%c", &op, &a, &x, &y, &c ) != EOF ){
x <<= 1, y <<= 1;
if( a == '(' ) x++;
if( c == ')' ) y--;
if( x > y ){
if( op == 'I' || op == 'C' ) nde[1].flag = nde[1].inv = 0;
}
else{
update( op, x, y, 1 );
}
}
tail = -10; head = 0;
query( 1 );
if( head == 0 ) puts( "empty set" );
}