首页 > 代码库 > bzoj 3339: Rmq Problem

bzoj 3339: Rmq Problem

3339: Rmq Problem

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 1270  Solved: 666
[Submit][Status][Discuss]

Description

技术分享

Input

技术分享

Output

技术分享

Sample Input

7 5
0 2 1 0 1 3 2
1 3
2 3
1 4
3 6
2 7

Sample Output

3
0
3
2
4

HINT

 

技术分享

 

Source

By Xhr

 
嗯,莫队
懒得敲线段树,毕竟线段树比较短
 
#include<cmath>#include<cstdio>#include<algorithm>using namespace std;const int maxn = 200006;int sz,block[maxn];struct Que{    int l,r,id;    bool operator < (const Que&a)const    {        return block[l]==block[a.l] ? r<a.r : l<a.l;    }}q[maxn];int a[maxn],n,m,tmp,have[maxn],ans[maxn];int l=1,r=0;void add(int x){    ++have[x];    if(x==tmp||!x)while(have[tmp])++tmp;}void del(int x){    --have[x];    if(x < tmp&&!have[x])tmp=x;}void md(int &l,int &r,int ql,int qr,int id){    while(r < qr)add(a[++r]);    while(r > qr)del(a[r--]);    while(l < ql)del(a[l++]);    while(l > ql)add(a[--l]);    ans[id]=tmp;}int main(){    scanf("%d%d",&n,&m);    sz=sqrt(n);    for(int i=1;i<=n;i++)        scanf("%d",a+i),block[i]=(i-1)/sz+1;    for(int i=1;i<=m;i++)        scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;    sort(q+1,q+m+1);    for(int i=1;i<=m;++i) md(l,r,q[i].l,q[i].r,q[i].id);    for(int i=1;i<=m;i++)printf("%d\n",ans[i]);    return 0;    }

 

bzoj 3339: Rmq Problem