Title:
Ideas:
Build a line segment tree according to the value, and maintain the last position where the value appears for each position, so that it can be persisted later, when querying [l, r] [l, r][l,r ] , we only need to be in[1, r] [1,r][1,r ] to find the last occurrence position<l <l<The position of l is sufficient, that is, therr of thechairman treer tree find value<l <l<The minimum value of l' s position is just two points directly above it.
Note becausemex mex is requiredm e x , reduction summary0, a [i] + 1 0, a [i] +10,a[i]+1 are inserted.
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n,m;
int a[N],tot;
int root[N];
vector<int>v;
struct Node
{
int l,r;
int mi;
}tr[N*40];
void insert(int p,int &q,int l,int r,int x,int pos)
{
q=++tot; tr[q]=tr[p];
if(l==r) {
tr[q].mi=pos; return; }
int mid=l+r>>1;
if(x<=mid) insert(tr[p].l,tr[q].l,l,mid,x,pos);
else insert(tr[p].r,tr[q].r,mid+1,r,x,pos);
tr[q].mi=min(tr[tr[q].l].mi,tr[tr[q].r].mi);
}
int query(int u,int l,int r,int x)
{
if(!u||l==r) return v[l-1];
int mid=l+r>>1;
if(x>tr[tr[u].l].mi) return query(tr[u].l,l,mid,x);
else return query(tr[u].r,mid+1,r,x);
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
scanf("%d%d",&n,&m); v.pb(0);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),v.pb(a[i]),v.pb(a[i]+1);
sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end());
for(int i=1;i<=n;i++)
{
a[i]=lower_bound(v.begin(),v.end(),a[i])-v.begin()+1;
insert(root[i-1],root[i],1,v.size(),a[i],i);
}
while(m--)
{
int l,r; scanf("%d%d",&l,&r);
printf("%d\n",query(root[r],1,v.size(),l));
}
、
return 0;
}
/*
*/