Tag: RET not str color PRE position return exists turn
Topic Links:
Http://codeforces.com/problemset/problem/622/C
Main topic:
Given a sequence of n (n not more than 200000), there is m (m not more than 200000) asking, and I ask if there is a number in an interval [Li,ri] that is not equal to a given value x. If present, the position of the number is output, otherwise output-1.
Problem Solving Ideas:
Preprocess an array p[n]. P[i] represents the position of the number from position I to the left until position 0, the first one that is not equal to a[i]. Recursion can be implemented with the complexity of O (n). Specifically, first make p[0]=-1, then recursion from left to right, if a[i-1]! = a[i], then p[i] = i-1, otherwise p[i] = p[i-1].
First check a[r] is equal to X when querying. If not equal then find a solution r; otherwise check p[r].
Code:
1#include <iostream>2#include <cstdio>3 using namespacestd;4 5 inta[1000005];6 intp[1000005];7 intN, T, L, R, X;8 voidInit ()9 {Tenp[0] = -1; One for(inti =1; I < n; i++) A { - if(A[i]! = a[i-1]) -P[i] = i-1; the Else -P[i] = p[i-1]; - } - } + intMain () - { +CIN >> N >>T; A for(inti =0; I < n; i++) at { -scanf"%d", &a[i]); - } - init (); - while(t--) - { inscanf" %d%d%d", &l, &r, &x); -l--; tor--; + if(A[r]! =x) - { theprintf"%d\n", R +1); * } $ ElsePanax Notoginseng { - if(P[r]! =-1&& P[r] >=l) the { +printf"%d\n", P[r] +1); A } the Else + { -Puts"-1"); $ } $ } - } - return 0; the}
cf622c not Equal on a Segment