Educational Codeforces Round 7 C. Not Equal on a Segment

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. 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.

Input

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.

Output

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.

Sample test(s)
Input
6 4
1 2 1 1 3 5
1 4 1
2 6 2
3 4 1
3 4 2
Output
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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值