Codeforces 817F MEX Queries

【题解】

  离散化+线段树,注意处理好几种标记之间的关系。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #define LL long long
  5 #define rg register
  6 #define N 200010
  7 #define ls (u<<1)
  8 #define rs (u<<1|1)
  9 #define mid ((a[u].l+a[u].r)>>1)
 10 #define inf (1e18+1)
 11 using namespace std;
 12 int n,m,tot,type[N];
 13 LL l[N],r[N],b[N];
 14 struct tree{
 15     LL l,r,p0,p1,tag;
 16 }a[N<<3];
 17 inline LL read(){
 18     LL k=0,f=1; char c=getchar();
 19     while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
 20     while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
 21     return k*f;
 22 } 
 23 void build(int u,int l,int r){
 24     a[u].l=l; a[u].r=r; a[u].p0=l; a[u].p1=inf;
 25     if(l<r){
 26         build(ls,l,mid); build(rs,mid+1,r);
 27     }
 28 }
 29 inline void pushup(int u){
 30     a[u].p0=min(a[ls].p0,a[rs].p0);
 31     a[u].p1=min(a[ls].p1,a[rs].p1);
 32 }
 33 void pushdown(int u){
 34     if(a[u].tag==1){
 35         a[ls].p0=a[rs].p0=inf;
 36         a[ls].p1=a[ls].l; a[rs].p1=a[rs].l;
 37     }
 38     else if(a[u].tag==2){
 39         a[ls].p1=a[rs].p1=inf;
 40         a[ls].p0=a[ls].l; a[rs].p0=a[rs].l;
 41     }
 42     else{
 43         swap(a[ls].p0,a[ls].p1);
 44         swap(a[rs].p0,a[rs].p1);
 45     }
 46     if(a[u].tag==3){
 47         if(a[ls].tag==1) a[ls].tag=2;
 48         else if(a[ls].tag==2) a[ls].tag=1;
 49         else if(a[ls].tag==3) a[ls].tag=0;
 50         else a[ls].tag=3;
 51         if(a[rs].tag==1) a[rs].tag=2;
 52         else if(a[rs].tag==2) a[rs].tag=1;
 53         else if(a[rs].tag==3) a[rs].tag=0;
 54         else a[rs].tag=3;
 55     }
 56     else a[ls].tag=a[rs].tag=a[u].tag;
 57     a[u].tag=0;
 58 }
 59 void update(int u,int l,int r,int type){
 60     if(l<=a[u].l&&a[u].r<=r){
 61         if(type==1) a[u].p0=inf,a[u].p1=a[u].l;
 62         else if(type==2) a[u].p0=a[u].l,a[u].p1=inf;
 63         else swap(a[u].p0,a[u].p1);
 64         if(type==3){
 65             if(a[u].tag==1) a[u].tag=2;
 66             else if(a[u].tag==2) a[u].tag=1;
 67             else if(a[u].tag==3) a[u].tag=0;
 68             else a[u].tag=3;
 69         }
 70         else a[u].tag=type;
 71         return;
 72     }
 73     if(a[u].tag) pushdown(u);
 74     if(l<=mid) update(ls,l,r,type);
 75     if(r>mid) update(rs,l,r,type);
 76     a[u].p0=min(a[ls].p0,a[rs].p0);
 77     a[u].p1=min(a[ls].p1,a[rs].p1);
 78 }
 79 int query(int u,int pos){
 80     if(a[u].l==a[u].r) return a[u].p0;
 81     if(a[u].tag) pushdown(u);
 82     if(pos<=mid) return query(ls,pos);
 83     else return query(rs,pos);
 84 }
 85 int main(){
 86     m=read(); b[tot=1]=1;
 87     for(rg int i=1;i<=m;i++){
 88         type[i]=read(); l[i]=b[++tot]=read(); r[i]=b[++tot]=read()+1; 
 89     }
 90     sort(b+1,b+1+tot); n=unique(b+1,b+1+tot)-b-1;
 91     for(rg int i=1;i<=m;i++)
 92         l[i]=lower_bound(b+1,b+1+n,l[i])-b,
 93         r[i]=lower_bound(b+1,b+1+n,r[i])-b;
 94     build(1,1,n);
 95     for(rg int i=1;i<=m;i++){
 96         update(1,l[i],r[i]-1,type[i]);
 97         LL tmp=a[1].p0;
 98         if(tmp==inf) tmp=b[n]+1;
 99         else tmp=b[tmp];
100         printf("%lld\n",tmp);
101     }
102     return 0;
103 }

猜你喜欢

转载自www.cnblogs.com/DriverLao/p/9816641.html
今日推荐