您的位置:首页 > 其它

CodeForces 622C Not Equal on a Segment

2016-04-06 23:16 507 查看

Not Equal on a Segment

Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u

Description

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 Input

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


题意:给出一个数列,m次询问,每次询问区间中与给定的数不相同的数的下标
分析:
对于一个区间来说,如果我们知道两个值,这两个值一定不同,并且在区间中,那么对与这个区间的询问来说,
指定的数要么在这两个数中,要么不在,如果在,则取另一个数,这样就解决了选取的数的问题。
一开始想的是离线处理询问,再O(n)遍历整个数列,更新这两个不同值,结果不知道哪里写挫了,WA了。
后来想了想,对于一个区间右端点来说,如果知道了这个端点对应左边不同值的下标,那么问题就就解决了么?
如此一来只需要预处理一下连续相同数字的对应下标值就行了

#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");

using namespace std;

#define INF 0x3f3f3f3f
#define maxn 200005

int pos[maxn];
int a[maxn];

int main()
{
int n,q;
while(scanf("%d%d",&n,&q)!=EOF)
{
memset(pos,0,sizeof pos);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
int p=n;
for(int i=n-1;i>=1;i--)
{
if(a[i]==a[p]) continue;
for(int j=p;j>i;j--) pos[j]=i;
p=i;
}
for(int i=0;i<q;i++)
{
int l,r,c;
scanf("%d%d%d",&l,&r,&c);
if(l==r||a[r]!=c)
{
if(a[r]!=c) printf("%d\n",r);
else printf("-1\n");
continue;
}
if(pos[r]!=0&&pos[r]>=l&&a[pos[r]]!=c)
{
printf("%d\n",pos[r]);
}
else  printf("-1\n");
}
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: