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

Educational Codeforces Round 23 F. MEX Queries(线段树)

时间:2017-06-21 22:00:19      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:线段   include   open   splay   opened   round   com   ons   unique   

题目链接:Educational Codeforces Round 23 F. MEX Queries

题意:

一共有n个操作。

1.  将[l,r]区间的数标记为1。

2.  将[l,r]区间的数标记为0。

3.  将[l,r]区间取反。

对每个操作,输出标记为0的最小正整数。

题解:

hash后,用线段树xjb标记一下就行了。

技术分享
 1 #include<bits/stdc++.h>
 2 #define ls l,m,rt<<1
 3 #define rs m+1,r,rt<<1|1
 4 #define F(i,a,b) for(int i=a;i<=b;++i)
 5 using namespace std;
 6 typedef long long ll;
 7 
 8 const int N=4e5+7;
 9 int n,ed,sum[N<<2],lazy[N<<2];
10 ll hsh[N],mx;
11 struct Node
12 {
13     int type;
14     ll l,r;
15 }q[N];
16 
17 void del(int rt)
18 {
19     if(lazy[rt]==1||lazy[rt]==2)lazy[rt]=(lazy[rt]==1?2:1);
20     else if(lazy[rt]==3)lazy[rt]=0;
21     else lazy[rt]=3;
22 }
23 
24 void PD(int rt,int l,int r)
25 {
26     if(!lazy[rt])return;
27     int m=l+r>>1;
28     if(lazy[rt]==1)
29     {
30         sum[rt<<1]=m-l+1,sum[rt<<1|1]=r-m;
31         lazy[rt<<1]=lazy[rt<<1|1]=1;
32     }
33     else if(lazy[rt]==2)
34     {
35         sum[rt<<1]=sum[rt<<1|1]=0;
36         lazy[rt<<1]=lazy[rt<<1|1]=2;
37     }
38     else 
39     {
40         sum[rt<<1]=(m-l+1)-sum[rt<<1],sum[rt<<1|1]=(r-m)-sum[rt<<1|1];
41         del(rt<<1),del(rt<<1|1);
42     }
43     lazy[rt]=0;
44 }
45 
46 void PU(int rt){sum[rt]=sum[rt<<1]+sum[rt<<1|1];}
47 
48 void update(int L,int R,int v,int l=1,int r=ed,int rt=1)
49 {
50     if(L<=l&&r<=R)
51     {
52         if(v==1)sum[rt]=(r-l+1),lazy[rt]=1;
53         else if(v==2)sum[rt]=0,lazy[rt]=2;
54         else del(rt),sum[rt]=(r-l+1)-sum[rt];
55         return;
56     }
57     PD(rt,l,r);
58     int m=l+r>>1;
59     if(L<=m)update(L,R,v,ls);
60     if(R>m)update(L,R,v,rs);
61     PU(rt);
62 }
63 
64 int query(int l=1,int r=ed,int rt=1)
65 {
66     if(l==r)return l;
67     int m=l+r>>1;
68     PD(rt,l,r);
69     if(m-l+1-sum[rt<<1])return query(ls);
70     else return query(rs);
71 }
72 
73 
74 int getid(ll x){return lower_bound(hsh+1,hsh+1+ed,x)-hsh;}
75 
76 int main(){
77     scanf("%d",&n);
78     F(i,1,n)
79     {
80         scanf("%d%I64d%I64d",&q[i].type,&q[i].l,&q[i].r);
81         hsh[++ed]=q[i].l,hsh[++ed]=q[i].r;
82         hsh[++ed]=q[i].l+1,hsh[++ed]=q[i].r+1;
83     }
84     hsh[++ed]=1;
85     sort(hsh+1,hsh+1+ed),ed=unique(hsh+1,hsh+1+ed)-hsh-1;
86     F(i,1,n)
87     {
88         update(getid(q[i].l),getid(q[i].r),q[i].type);
89         printf("%I64d\n",hsh[query()]);
90     }
91     return 0;
92 }
View Code

 

Educational Codeforces Round 23 F. MEX Queries(线段树)

标签:线段   include   open   splay   opened   round   com   ons   unique   

原文地址:http://www.cnblogs.com/bin-gege/p/7061726.html

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