You are given array a with n integers and m queries. Thei-th query is given with three integers li, ri, xi.
For the i-th query find any position pi (li ≤ pi ≤ ri) so thatapi ≠ xi.
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the number of elements ina and the number of queries.
The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the arraya.
Each of the next m lines contains three integersli, 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 toxi in segment[li, ri] or the value - 1 if there is no such number.
6 4 1 2 1 1 3 5 1 4 1 2 6 2 3 4 1 3 4 2
2 6 -1 4
题意:给你一个序列,然后问在[l,r]范围内不和x相同的数字的位置
思路:用一个数组记录下一个遍历的点是哪个,也就是说将相同的数字分块,当遍历到一个点后,如果不符合直接跳到其分块的起始点前的那一位再开始。。
ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
int a[MAXN];
int p[MAXN];
int main()
{
int n,m;
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
int q=1;
for(i=1;i<=n;i++)//初始化
p[i]=i;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==a[i-1])
{
p[i]=p[i-1];
}
}
while(m--)
{
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
int k=r;
int bz=0;
while(k>=l)
{
if(a[k]!=x)
{
printf("%d\n",k);
bz=1;
break;
}
k=p[k]-1;
}
if(!bz)
printf("-1\n");
}
}
return 0;
}