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

来源:互联网 发布:php 换行符分割字符串 编辑:程序博客网 时间:2024/06/10 19:09
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 41 2 1 1 3 51 4 12 6 23 4 13 4 2
output
26-14

思路:
   一开始这题想到了线段树但是却无从下手,想了好久也不会做,之后看了看别人的思路,说直接将相同的数统计一下下标就行了,最后发现原来是有一个规律在里面,因为这题是找不同的位置所以只要将相同的位置预处理一下,有相同的直接跳过,这样之后基本就是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 1000000000typedef long long ll;typedef unsigned long long ULL;int v[T],num[T];int main(){#ifdef zsc freopen("input.txt","r",stdin); #endifint 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;}


0 0