poj 3225 Help with Intervals

题目大意:初始时S为空集,经过一系列操作(区间的交,并,补,差等),求最后一条命令执行之后的集合S

思路:集合T表示为(a,b).

(1)'U':将(a,b)置1。

(2)'I':将(0,a-1)和(b+1,maxn)之置0。

(3)'D':将(a,b)置0;

(4)'C':将(0,a-1)和(b+1,maxn)置0,(a,b)进行异或操作。

(5)‘S’:将(a,b)进行抑或操作。

由于进行‘I’和‘C’操作时,没有判断边界,贡献了无数个re。-_-#

做了这题,发现自己太过粗心,必须客服。。。。。

View Code
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cctype>
  4 #include <algorithm>
  5 using namespace std;
  6 #define lson l,m,rt<<1
  7 #define rson m+1,r,rt<<1|1
  8 #define maxn 131072
  9 struct node
 10 {
 11     int op;
 12 }setree[maxn<<2];
 13 bool hash[maxn];
 14 void build(int l,int r,int rt)
 15 {
 16     if(l==r){
 17         setree[rt].op=0;
 18         return;
 19     }
 20     setree[rt].op=-1;
 21     int m=(l+r)>>1;
 22     build(lson);
 23     build(rson);
 24 } 
 25 void pushdown(int rt)
 26 {
 27     if(setree[rt].op!=-1){
 28         if(setree[rt].op==1||setree[rt].op==0)
 29             setree[rt<<1].op=setree[rt<<1|1].op=setree[rt].op;
 30         else{
 31             if(setree[rt<<1].op==-1)
 32                 setree[rt<<1].op=2;
 33             else if(setree[rt<<1].op==1||setree[rt<<1].op==0)
 34                 setree[rt<<1].op=1-setree[rt<<1].op;
 35             else
 36                 setree[rt<<1].op=-1;
 37             if(setree[rt<<1|1].op==-1)
 38                 setree[rt<<1|1].op=2;
 39             else  if(setree[rt<<1|1].op==1||setree[rt<<1|1].op==0)
 40                 setree[rt<<1|1].op=1-setree[rt<<1|1].op;
 41             else
 42                 setree[rt<<1|1].op=-1;
 43         }
 44         setree[rt].op=-1; 
 45     }
 46 }
 47 void update(int l,int r,int rt,int L,int R,int c)
 48 {
 49     if(L<=l&&r<=R){
 50         setree[rt].op=c;
 51         return;
 52     }
 53     pushdown(rt);
 54     int m=(l+r)>>1;
 55     if(L<=m)
 56     update(lson,L,R,c);
 57     if(R>m)
 58     update(rson,L,R,c);
 59 }
 60 void xorupdate(int l,int r,int  rt,int L,int R)
 61 {
 62     if(L<=l&&r<=R){
 63         if(setree[rt].op==-1)
 64         setree[rt].op=2;
 65         else if(setree[rt].op==1||setree[rt].op==0)
 66         setree[rt].op=1-setree[rt].op;
 67         else
 68         setree[rt].op=-1;
 69         return;
 70     }
 71     pushdown(rt);
 72     int m=(l+r)>>1;
 73     if(L<=m)
 74     xorupdate(lson,L,R);
 75     if(R>m)
 76     xorupdate(rson,L,R);
 77 }
 78 void query(int l,int r,int rt)
 79 {
 80     if(setree[rt].op==1){
 81         for(int i=l;i<=r;i++)
 82         hash[i]=true;
 83         return;
 84     }
 85     else if(setree[rt].op==0)
 86     return;
 87     if(l==r)
 88     return;
 89     pushdown(rt);
 90     int m=(l+r)>>1;
 91     query(lson);
 92     query(rson);
 93 }
 94 int main()
 95 {
 96     int a,b;
 97     char opp,lc,rc;
 98     build(0,maxn,1);
 99     memset(hash,false,sizeof(hash));
100     while(~scanf("%c %c%d,%d%c\n",&opp,&lc,&a,&b,&rc)){
101         //getchar();
102         a=a*2;
103         b=b*2;
104         if(lc=='(')
105         a++;
106         if(rc==')')
107         b--;
108         switch(opp){
109             case 'U':update(0,maxn,1,a,b,1);break;
110             case 'I':
111             if(a-1>=0)
112             update(0,maxn,1,0,a-1,0);
113             if(b+1<=maxn)
114             update(0,maxn,1,b+1,maxn,0);
115             break;
116             case 'D':update(0,maxn,1,a,b,0);break;
117             case 'C':
118             if(a-1>=0)
119             update(0,maxn,1,0,a-1,0);
120             if(b+1<=maxn)
121             update(0,maxn,1,b+1,maxn,0);xorupdate(0,maxn,1,a,b);break;
122             case 'S':xorupdate(0,maxn,1,a,b);break;
123         }
124     }
125     query(0,maxn,1);
126     bool flag=false;
127     int s=-1,e;
128     for(int i=0;i<=maxn;i++){
129         if(hash[i]){
130            if(s==-1)
131            s=i;
132            e=i;
133         }
134         else if(s!=-1){
135             if(s/2*2==s)
136               lc='[';
137               else
138               lc='(';
139               if(e/2*2==e)
140               rc=']';
141               else{
142               e++;
143               rc=')';
144               }
145               if(flag)
146               putchar(' ');
147               printf("%c%d,%d%c",lc,s/2,e/2,rc);
148               flag=true;
149               s=-1;
150         }
151     }
152     if(!flag)
153     printf("empty set\n");
154 }
原文地址:https://www.cnblogs.com/kim888168/p/2764164.html