[Codeforces]817F. MEX Queries

You are given a set of integer numbers, initially it is empty. You should perform n queries. 
There are three different types of queries: 
1 l r — Add all missing numbers from the interval [l, r] 
2 l r — Remove all present numbers from the interval [l, r] 
3 l r — Invert the interval [l, r] — add all missing and remove all present numbers from the interval [l, r] 
After each query you should output MEX of the set — the smallest positive (MEX  ≥ 1) integer number which is not presented in the set. 
Input 
The first line contains one integer number n (1 ≤ n ≤ 105). 
Next n lines contain three integer numbers t, l, r (1 ≤ t ≤ 3, 1 ≤ l ≤ r ≤ 1018) — type of the query, left and right bounds. 
Output 
Print MEX of the set after each query. 
Examples 
input 

1 3 4 
3 1 6 
2 1 3 
output 



input 

1 1 3 
3 5 6 
2 4 4 
3 1 6 
output 




Note 
Here are contents of the set after each query in the first example: 
{3, 4} — the interval [3, 4] is added 
{1, 2, 5, 6} — numbers {3, 4} from the interval [1, 6] got deleted and all the others are added 
{5, 6} — numbers {1, 2} got deleted

题意

给你一个无限长的数组,初始的时候都为0,操作1是把给定区间清零,操作2是把给定区间设为1,操作3把给定区间反转。每次操作后要输出最小位置的0。

题解

看到数据范围n<=10^5,结合题意可以考虑使用线段树维护对区间的修改操作。但是l,r<=10^18,所以首先要离散化一下。在使用线段树维护的时候,节点维护该区间数相加的总和。对于操作1和操作2,我们分别赋值为1和0,对于操作3,我们把区间反转,那么新的区间和就是区间的长度减去原来的区间和。然后每次查询最小位置的0,只需要看一下左儿子所代表的区间是否小于这个区间的长度,如果是就在左儿子,否则就在右儿子查找。

题目细节

这道题有很多坑人的点,首先,在离散化的时候必须把1也加上,因为答案可能为1;线段树在下传标记时要注意顺序;记录原来信息的数组必须得开long long,空间一定要开够。

代码

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define ll long long
#define REP(i,a,b) for(register int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(register int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,a) for(register int i=start[(a)];i;i=e[i].next)
inline int read()
{
int sum=,p=;char ch=getchar();
while(!((''<=ch && ch<='') || ch=='-'))ch=getchar();
if(ch=='-')p=-,ch=getchar();
while(''<=ch && ch<='')sum=sum*+ch-,ch=getchar();
return sum*p;
}
const int maxn=; map <ll,int> mp;
int m,cnt;
ll s[maxn*],n; struct qu {
ll l,r;
int type;
}a[maxn]; struct node {
int s,lz,id;//s记录区间和,lz为懒标记,id维护区间是否反转
}c[maxn*]; #define lc (o<<1)
#define rc (o<<1 | 1)
#define left lc,l,mid
#define right rc,mid+1,r inline void make_tree(int o,int l,int r)
{
c[o].s=;c[o].lz=-;c[o].id=;
if(l==r)return;
int mid=(l+r)>>;
make_tree(left);
make_tree(right);
} void maintain(int o,int l,int r)
{
c[o].s=c[lc].s+c[rc].s;
} void pushdown(int o,int l,int r)
{
int mid=(l+r)>>;
if(c[o].lz!=-)//下传懒标记,同时将儿子节点的反转标记清0
{
c[lc].lz=c[rc].lz=c[o].lz;
c[lc].s=(mid-l+)*c[o].lz;
c[rc].s=(r-mid)*c[o].lz;
c[lc].id=c[rc].id=;
c[o].lz=-;
}
if(c[o].id)//将儿子节点的反转标记也反转,同时维护儿子的区间和
{
c[lc].id^=;
c[rc].id^=;
c[lc].s=(mid-l+)-c[lc].s;
c[rc].s=(r-mid)-c[rc].s;
c[o].id=;
}
} inline void updates(int ql,int qr,int x,int o,int l,int r)
{
pushdown(o,l,r);
if(ql==l && r==qr)//把区间覆盖为x
{
c[o].s=(r-l+)*x;
c[o].lz=x;
c[o].id=;
return;
}
int mid=(l+r)>>;
if(ql>mid)
{
updates(ql,qr,x,right);
}
else if(qr<=mid)
{
updates(ql,qr,x,left);
}else
{
updates(ql,mid,x,left);
updates(mid+,qr,x,right);
}
maintain(o,l,r);
} inline void updatex(int ql,int qr,int o,int l,int r)
{
pushdown(o,l,r);
if(ql==l && r==qr)//把区间反转
{
c[o].s=(r-l+)-c[o].s;
c[o].id^=;
return;
}
int mid=(l+r)>>;
if(ql>mid)
{
updatex(ql,qr,right);
}
else if(qr<=mid)
{
updatex(ql,qr,left);
}else
{
updatex(ql,mid,left);
updatex(mid+,qr,right);
}
maintain(o,l,r);
} void init()
{
m=read();
REP(i,,m)
{
cin>>a[i].type>>a[i].l>>a[i].r;
a[i].r++;
s[++cnt]=a[i].l;
s[++cnt]=a[i].r;
}
s[++cnt]=;//答案中可能会有1,必须加上
sort(s+,s+cnt+);
n=unique(s+,s+cnt+)-(s+);
REP(i,,n)mp[s[i]]=i;
make_tree(,,n);
} void query(int o,int l,int r)
{
if(l==r)
{
cout<<s[l]<<endl;
return;
}
int mid=(l+r)>>;
pushdown(o,l,r);
if(c[lc].s<mid-l+)
query(left);
else query(right);
} void doing()
{
REP(i,,m)
{
int type=a[i].type,l=mp[a[i].l],r=mp[a[i].r]-;
if(type==)
{
updates(l,r,,,,n);
}
else if(type==)
{
updates(l,r,,,,n);
}else
{
updatex(l,r,,,n);
}
query(,,n);
}
} int main()
{
init();
doing();
return ;
}

[Codeforces]817F. MEX Queries 离散化+线段树维护的更多相关文章

  1. Educational Codeforces Round 23 F. MEX Queries 离散化+线段树

    F. MEX Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  2. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  3. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  4. Codeforces 817F MEX Queries

    题意:对一个维护三种操作:1.将[l..r]中的数全部加入集合中.2.将集合中[l..r]范围内的数删去.3.将集合中在[l..r]中的数删去,并将之前不在集合中的数加入集合 考虑到最近线段树总是写爆 ...

  5. mex (离散化+线段树)

    Time Limit: 3000 ms   Memory Limit: 256 MB Description 给你一个无限长的数组,初始的时候都为0,有3种操作: 操作1是把给定区间$[l,r]$设为 ...

  6. [Noi2016]区间[离散化+线段树维护+决策单调性]

    4653: [Noi2016]区间 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 621  Solved: 329[Submit][Status][D ...

  7. SPOJ - GSS1-Can you answer these queries I 线段树维护区间连续和最大值

    SPOJ - GSS1:https://vjudge.net/problem/SPOJ-GSS1 参考:http://www.cnblogs.com/shanyr/p/5710152.html?utm ...

  8. 2019牛客多校第七场E Find the median 离散化+线段树维护区间段

    Find the median 题意 刚开始集合为空,有n次操作,每次操作往集合里面插入[L[i],R[i]]的值,问每次操作后中位数是多少 分析 由于n比较大,并且数可以达到1e9,我们无法通过权值 ...

  9. Codeforces 997E - Good Subsegments(线段树维护最小值个数+历史最小值个数之和)

    Portal 题意: 给出排列 \(p_1,p_2,p_3,\dots,p_n\),定义一个区间 \([l,r]\) 是好的当且仅当 \(p_l,p_{l+1},p_{l+2},\dots,p_r\) ...

随机推荐

  1. Python自建logging模块

    本章将介绍Python内建模块:日志模块,更多内容请从参考:Python学习指南 简单使用 最开始,我们用最短的代码体验一下logging的基本功能. import logging logger = ...

  2. iOS的相对路径和绝对路径

    iOS程序有固定的文件访问限制,只能在自己的沙盒内. UIImage *img=[UIImage imageNamed:@"cellicon.png"]; 这段代码从相对路径加载了 ...

  3. 利用 HTML5 WebGL 构建的 3D 拓扑图

    现在,3D 模型已经用于各种不同的领域.在医疗行业使用它们制作器官的精确模型:电影行业将它们用于活动的人物.物体以及现实电影:视频游戏产业将它们作为计算机与视频游戏中的资源:在科学领域将它们作为化合物 ...

  4. Debugging java application with netbean

    Debugging Java Applications with NetBeans    from:https://manikandanmv.wordpress.com/2009/09/24/debu ...

  5. 2017-06-24(chgrp umask alias unalias)

    chgrp chgrp  组名  文件名 chgrp root newfile   将newfile的所属组修改为root umask umask 查看默认权限 0 022 文件的特殊权限 文件的默认 ...

  6. java8大基本数据类型

    基本类型 字节数 位数 最大值 最小值 byte 1byte 8bit 2^7 - 1 -2^7 short 2byte 16bit 2^15 - 1 -2^15 int 4byte 32bit 2^ ...

  7. 流API--流的收集

    前面的一系列博客中,我们都是从一个集合中拿到一个流,但是有时候需要执行反操作,就是从流中获得集合.实际编码中,当我们处理完流后,我们通常想查看下结果,而不是将他们聚合成一个值.我们可以调用iterat ...

  8. Java Cookie和Session

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  9. 【转】 linux下的awk程序执行

    #!/bin/awk -f awk脚本开头使用这个命令,赋予这个文本文件以执行的权限.这样做之后,你就可以在命令行中用类似于下面这样的方式调用并执行这段awk程序了. BEGIN和END的大括号必须紧 ...

  10. redis数据类型-有序集合

    有序集合类型 在集合类型的基础上有序集合类型为集合中的每个元素都关联了一个分数,这使得我们不仅可以完成插入.删除和判断元素是否存在等集合类型支持的操作,还能够获得分数最高(或最低)的前N个元素.获得指 ...