题意:对空集S进行运算,问你S最后的区间表示

解题思路:这题首先难下手的地方就是他的开区间与闭区间,这里我们要对数字乘以2(偶数代表端点,奇数代表区间),然后两个操作,覆盖和取反(自行思考),然后是5种情况,最后用hash记录覆盖区域输出,这题要注意的地方较多,在向下跟新的时候要有三种状态标记,和一种取反标记要注意,错了40次终于ac了,这题成功的把我poj的正确率拉低到了10% 以下。。

解题代码:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #define maxn 140000
  5 struct node
  6 {
  7     int l ,r , m, c ,f;
  8 } tree[maxn * 8];
  9 int L(int c)
 10 {
 11     return 2*c;
 12 }
 13 int R(int c)
 14 {
 15     return 2*c+1;
 16 }
 17 int hs[maxn*8];
 18 void build(int c, int p , int v )
 19 {
 20     tree[c].l = p;
 21     tree[c].r = v;
 22     tree[c].m = (p+v)/2;
 23     tree[c].c = 0 ;
 24     tree[c].f = 0 ;
 25     if(p == v)
 26         return;
 27     build(L(c),p,tree[c].m);
 28     build(R(c),tree[c].m+1,v);
 29 }
 30 void Fxor(int c)
 31 {
 32     if(tree[c].c != -1) tree[c].c ^= 1;
 33     else tree[c].f ^= 1;
 34 }
 35 void Pushdown(int c)
 36 {
 37     if(tree[c].c!= -1)
 38     {
 39         tree[L(c)].c = tree[c].c;
 40         tree[R(c)].c = tree[c].c;
 41         tree[R(c)].f = tree[L(c)].f = 0 ;
 42         tree[c].c = -1;
 43     }
 44     if(tree[c].f)
 45     {
 46         Fxor(L(c));
 47         Fxor(R(c));
 48         tree[c].f = 0 ;
 49     }
 50 
 51 
 52 }
 53 void update(int c , int p , int v ,int value )
 54 {
 55     if( p <= tree[c].l && v >= tree[c].r)
 56     {
 57         tree[c].c = value;
 58         tree[c].f = 0;
 59         return ;
 60     }
 61     Pushdown(c);
 62     if(v <= tree[c].m) update(L(c),p,v,value);
 63     else if(p > tree[c].m) update(R(c),p,v,value);
 64     else
 65     {
 66         update(L(c),p,tree[c].m,value);
 67         update(R(c),tree[c].m + 1 ,v,value);
 68     }
 69 }
 70 void fan(int c ,int p , int v)
 71 {
 72     if(p <= tree[c].l && v >= tree[c].r)
 73     {
 74         Fxor(c);
 75         return ;
 76     }
 77     Pushdown(c);
 78     if(v <= tree[c].m) fan(L(c),p,v);
 79     else if(p > tree[c].m) fan(R(c),p,v);
 80     else
 81     {
 82         fan(L(c),p,tree[c].m);
 83         fan(R(c),tree[c].m + 1 ,v);
 84     }
 85 }
 86 
 87 void geths(int c,int p ,int v )
 88 {
 89 
 90     if(p <= tree[c].l && v >= tree[c].r)
 91     {
 92         if(tree[c].c == 1)
 93         {
 94             for(int i = tree[c].l ; i <= tree[c].r ; i ++)
 95             {
 96                 hs[i] = 1;
 97             }
 98             return;
 99         }
100 
101     }
102     if(tree[c].l == tree[c].r)
103         return;
104     Pushdown(c);
105     if(v <= tree[c].m)  geths(L(c),p,v);
106     else if(p > tree[c].m) geths(R(c),p,v);
107     else
108     {
109         geths(L(c),p,tree[c].m);
110         geths(R(c),tree[c].m+1,v);
111     }
112 }
113 int main()
114 {
115     build(1,0,maxn*2);
116     memset(hs,0,sizeof(hs));
117 
118     char op,l,r;
119     int a,b;
120     while(scanf("%c %c%d,%d%c",&op,&l,&a,&b,&r) != EOF)
121     {
122         getchar();
123         a *= 2;
124         b *= 2;
125         if(l == '(')
126             a++;
127         if(r == ')')
128             b--;
129 
130        //printf("%d %d\n",a,b);
131         if(a > b)
132         {
133             if(op == 'C' || op == 'I')
134                 tree[1].c  = tree[1].f =  0 ;
135             continue;
136         }
137         if(op == 'U')
138         {
139             update(1,a,b,1);
140         }
141         else if(op == 'I')
142         {
143             if(a != 0 )
144             update(1,0,a -1,0);
145             update(1,b +1 ,maxn,0);
146         }
147         else if(op == 'D')
148         {
149             update(1,a,b,0);
150         }
151         else if(op == 'C')
152         {
153             if(a != 0 )
154             update(1,0,a-1,0);
155             update(1,b+1,maxn,0);
156             fan(1,a,b);
157         }
158         else
159         {  // printf("*****\n");
160             fan(1,a,b);
161         }
162     
163     }
164      memset(hs,0,sizeof(hs));
165         geths(1,0,maxn);
166         int ok = 0 ;
167         for(int i = 0 ; i <= maxn;)
168         {
169             if(hs[i] == 1)
170             {
171                 ok ++;
172                 int be = i ;
173                 while(hs[i] == 1 )
174                     i++;
175                 int en = i-1;
176                 if(be % 2 == 1)
177                 printf(ok == 1?"(%d,":" (%d,",be/2);
178                 else
179                 printf(ok == 1?"[%d,":" [%d,",be/2);
180                 if(en % 2 == 0 )
181                     printf("%d]",en/2);
182                 else printf("%d)",en/2+ 1);
183             }
184             else
185             {
186                 i++;
187             }
188 
189         }
190         if(ok == 0 )
191             printf("empty set");
192         puts("");
193 
194     return 0 ;
195 }
View Code

 

posted on 2013-08-12 13:22  dark_dream  阅读(218)  评论(0编辑  收藏  举报