当前位置: 代码迷 >> 综合 >> Codeforces 622C Not Equal on a Segment (思维)
  详细解决方案

Codeforces 622C Not Equal on a Segment (思维)

热度:16   发布时间:2024-01-15 06:58:53.0

C. Not Equal on a Segment

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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.

Output

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.

Examples

input

Copy

6 4
1 2 1 1 3 5
1 4 1
2 6 2
3 4 1
3 4 2

output

Copy

2
6
-1
4

题意:给你一个数组,给你m个询问,每个询问一个区间, 一个数值,在区间里哪一个位置不是这个数值,输出任意一个就行;

分析:

此题关键在于查询,如何优化查询,其实我们发现我让我们做的无非查询很简单,我们想一想头一个数要么是,要么不是,所以就查询一次就ok,我们可以用一个预处理fa[a[i]]表示与a[i]不同的第一个位置是多少,这样就可以优化查询了。

#include<stdio.h>
#include<string>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int MAXN=200000+5;//最大元素个数
const int mod=100000007;
int n;//元素个数
int fa[MAXN];
int a[MAXN];
int main()
{int n,m;while(scanf("%d%d",&n,&m)!=-1){for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<n;i++){if(a[i]!=a[i+1]){fa[i]=i+1;}else{int pre=i;for(;a[i]==a[i+1];){i++;}//cout<<pre<<" "<<i<<endl;for(int j=pre;j<=i;j++)fa[j]=i+1;}}while(m--){int x,y,v;scanf("%d%d%d",&x,&y,&v);int flag=0;for(int i=x;i<=y;i++){if(a[i]==v){if(fa[i]>y||fa[i]==0) break;printf("%d\n",fa[i]);flag=1;break;}else{printf("%d\n",i);flag=1;break;}}if(flag==0)printf("-1\n");}//printf("%lld\n",ans);}return 0;
}

 

  相关解决方案