您的位置:首页 > 其它

Educational Codeforces Round 7-C. Not Equal on a Segment(模拟)

2016-02-11 22:19 465 查看
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.

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


思路:
   一开始这题想到了线段树但是却无从下手,想了好久也不会做,之后看了看别人的思路,说直接将相同的数统计一下下标就行了,最后发现原来是有一个规律在里面,因为这题是找不同的位置所以只要将相同的位置预处理一下,有相同的直接跳过,这样之后基本就是O(1)的查询时间了,因为相同的我都已经跳过了,思维太差了,想不到这个地方可以将时间复杂度降下来。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
using namespace std;
const int T=200100;
#define inf 0x3f3f3f3fL
#define mod 1000000000
typedef long long ll;
typedef unsigned long long ULL;

int v[T],num[T];

int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif

int n,m,i,j,k;
while(~scanf("%d%d",&n,&m))
{
for(i=1;i<=n;++i){
scanf("%d",&v[i]);
if(v[i]==v[i-1]){
num[i] = num[i-1];
}
else {
num[i] = i;
}
}
int x,y,w;
while(m--)
{
bool flag = false;
scanf("%d%d%d",&x,&y,&w);
for(i=y;i>=x;--i){
if(v[i]==w){//相同直接跳到最后面
i = num[i];
}
else {//找到不相同,结束
flag = true;
break;
}
}
if(!flag)i=-1;
printf("%d\n",i);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 模拟