bzoj 3585: mex
2016-04-27 08:37
387 查看
3585: mex
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 619 Solved: 327
[Submit][Status][Discuss]
Description
有一个长度为n的数组{a1,a2,...,an}。m次询问,每次询问一个区间内最小没有出现过的自然数。Input
第一行n,m。第二行为n个数。
从第三行开始,每行一个询问l,r。
Output
一行一个数,表示每个询问的答案。Sample Input
5 52 1 0 2 1
3 3
2 3
2 4
1 2
3 5
Sample Output
12
3
0
3
HINT
数据规模和约定对于100%的数据:
1<=n,m<=200000
0<=ai<=109
1<=l<=r<=n
Source
By 佚名提供[Submit][Status][Discuss]
题解:bzoj 3339的升级版。需要离散化。。。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<map> #define N 200003 #define inf 1000000000 using namespace std; int n,m; int tr[N*4],b ,sg ,next ,point ,ans ,p ,c ; int pd ; map<int,int> mark; struct data { int l,r,num; };data a ; void build(int now,int l,int r) { tr[now]=inf; if (l==r) { tr[now]=sg[l]; pd[l]=now; return; } int mid=(l+r)/2; build(now<<1,l,mid); build(now<<1|1,mid+1,r); } void pushdown(int x) { tr[x<<1]=min(tr[x],tr[x<<1]); tr[x<<1|1]=min(tr[x],tr[x<<1|1]); } void change(int now,int l,int r,int ll,int rr,int x) { if (tr[now]!=inf&&l!=r) pushdown(now); if (l>=ll&&r<=rr) { tr[now]=min(tr[now],x); return; } int mid=(l+r)/2; if (ll<=mid) change(now<<1,l,mid,ll,rr,x); if (rr>mid) change(now<<1|1,mid+1,r,ll,rr,x); } int cmp(data a,data b) { return a.l<b.l; } int ask(int now,int l,int r,int x) { if (tr[now]!=inf&&l!=r) pushdown(now); if (l==r) return tr[now]; int mid=(l+r)/2; if (x<=mid) return ask(now<<1,l,mid,x); else return ask(now<<1|1,mid+1,r,x); } int cmp1(int x,int y) { return c[x]<c[y]; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%d",&c[i]),p[i]=i; sort(p+1,p+n+1,cmp1); int k=0; int cnt=0; for (int i=1;i<=n;i++) if (c[p[i]]==c[p[i-1]]) b[p[i]]=cnt; else cnt++, b[p[i]]=cnt; for (int i=1;i<=n;i++) { mark[c[i]]=1; if (c[i]==k) while (mark[k]) k++; sg[i]=k; } for (int i=1;i<=n;i++) point[b[i]]=n+1; for (int i=n;i>=1;i--) { next[i]=point[b[i]]; point[b[i]]=i; } for (int i=1;i<=m;i++) scanf("%d%d",&a[i].l,&a[i].r),a[i].num=i; sort(a+1,a+m+1,cmp); build(1,1,n); int l=1; for (int i=1;i<=m;i++) { while (l<a[i].l) { int r; r=next[l]-1; change(1,1,n,l,r,c[l]); l++; } ans[a[i].num]=ask(1,1,n,a[i].r); } for (int i=1;i<=m;i++) printf("%d\n",ans[i]); }
相关文章推荐
- openwrt之luci安装
- android sdk content loader 0%
- AMH面板命令操作大全
- week9:个人博客作业
- 利用套接字进行数据传输
- 缺少动态连接库.so--cannot open shared object file: No such file or directory
- 缺少动态连接库.so--cannot open shared object file: No such file or directory
- 根据字符串计算UILabel尺寸
- 学会用各种姿势备份MySQL数据库
- Could not create the view: An unexpected exception was thrown.
- ajax,Unsupported media type-415(不 支持的媒体类型)
- [BZOJ3585]mex(离线+离散化+线段树)
- ZOJ 3469 food delivery 有趣的区间dp
- java反射 .
- JVM:如何分析线程堆栈
- 自定义tag标签的方法
- oracle表空间的查看创建删除
- Ubuntu wifi
- GeoHash算法
- [Poi2011]Tree Rotations 解题报告