You are given array a with n integers and m queries. The i-th query is given with three integers li, ri, xi.
For the i-th query find any position pi (li ≤ pi ≤ ri) so that api ≠ xi.
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the number of elements in a and the number of queries.
The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a.
Each of the next m lines contains three integers li, ri, xi (1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 106) — the parameters of the i-th query.
Print m lines. On the i-th line print integer pi — the position of any number not equal to xi in segment [li, ri] or the value - 1 if there is no such number.
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e6;
int a[maxn],n,q;
struct tree
{
int l,r;
long long maxx;
long long minn;
}t[4*maxn+2];
void bulid(int p,int l,int r)
{
t[p].l=l;
t[p].r=r;
if(l==r)
{
t[p].maxx=a[l];
t[p].minn=a[l];
return;
}
int mid=l+r>>1;
bulid(p*2,l,mid);
bulid(p*2+1,mid+1,r);
t[p].maxx=max(t[p*2].maxx,t[p*2+1].maxx);
t[p].minn=min(t[p*2].minn,t[p*2+1].minn);
}
//线段树二分
//[x,y]中第一个大于z的下标
long long ask(int p,int x,int y,int z)
{
if(t[p].l==x&&t[p].r==y)
{
if(t[p].maxx<=z)return -1;
else
{
if(t[p].l==t[p].r)return t[p].l;
int mid=(t[p].l+t[p].r)>>1;
if(t[p*2].maxx>z)return ask(p*2,x,mid,z);
else return ask(p*2+1,mid+1,y,z);
}
}
int mid=(t[p].l+t[p].r)>>1;
if(y<=mid)return ask(p*2,x,y,z);
else if(x>mid) return ask(p*2+1,x,y,z);
else
{
int pos=ask(p*2,x,mid,z);
if(pos==-1)return ask(p*2+1,mid+1,y,z);
else return pos;
}
}
long long ask2(int p,int x,int y,int z)
{
if(t[p].l==x&&t[p].r==y)
{
if(t[p].minn>=z)return -1;
else
{
if(t[p].l==t[p].r)return t[p].l;
int mid=(t[p].l+t[p].r)>>1;
if(t[p*2].minn<z)return ask2(p*2,x,mid,z);
else return ask2(p*2+1,mid+1,y,z);
}
}
int mid=(t[p].l+t[p].r)>>1;
if(y<=mid)return ask2(p*2,x,y,z);
else if(x>mid) return ask2(p*2+1,x,y,z);
else
{
int pos=ask2(p*2,x,mid,z);
if(pos==-1)return ask2(p*2+1,mid+1,y,z);
else return pos;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
bulid(1,1,n);
while(q--)
{
int op,x,d,l,r;
cin>>l>>r>>x;
int ans1=ask(1,l,r,x);
int ans2=ask2(1,l,r,x);
//cout<<ans1<<" "<<ans2<<"\n";
if(ans1==-1&&ans2==-1)cout<<-1<<"\n";
else if(ans1!=-1)cout<<ans1<<"\n";
else cout<<ans2<<"\n";
}
return 0;
}