https://www.lydsy.com/JudgeOnline/problem.php?id=3339
https://www.lydsy.com/JudgeOnline/problem.php?id=3585
An amazing algorithm
You should record the rightest appear in the arrow
And using the Segment Tree to record the Minimum imformation
and binary search
but not really binary search
just on the tree
compared with the l
code of AC:
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10,M=1e8+5;
struct ContinueTree{
int l,r,Min;
#define l(x) Tree[x].l
#define r(x) Tree[x].r
#define Min(x) Tree[x].Min
}Tree[30*N];
int tot=0;
int A[N],R[N];
void Insert(int &x,int l,int r,int p,int v){
Tree[++tot]=Tree[x];
x=tot;
if(l==r){
Min(x)=v;
return;
}
int mid=(l+r)/2;
if(p<=mid) Insert(l(x),l,mid,p,v);
else Insert(r(x),mid+1,r,p,v);
Min(x)=min(Min(l(x)),Min(r(x)));
}
int Query(int x,int l,int r,int v){
if(l==r) return r;
int mid=(l+r)/2;
if(Min(l(x))<v) return Query(l(x),l,mid,v);
else return Query(r(x),mid+1,r,v);
}
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;++i){
scanf("%d",&A[i]);
R[i]=R[i-1];
Insert(R[i],0,M,A[i],i);
}
while(m--){
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",Query(R[r],0,M,l));
}
}