Not Equal on a Segment
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.
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.
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.
6 4
1 2 1 1 3 5
1 4 1
2 6 2
3 4 1
3 4 2
2
6
-1
4
题意大意为:给的序列中, 提问给一个左右范围, 给出一个数字, 给出不是这个数 的范围中的任意下标。
解决的办法为, 先判断边界的数字是否相同, 若不同, 则返回这个边界。否则, 返回预处理的数。( 打表, 进行判断。 )
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include <cstdio> using namespace std; const int all = (2e6)+5; int numa[ all ], numb[ all ]; int n, m, l, r, x; int main(void) { scanf( "%d%d", &n, &m ); for( int i=1; i <= n; ++ i ){ scanf( "%d", numa+i ); // 与前一个数进行判断, 若不同, 加前一个数的下标即可。 注意的是, 下标为 0 的值 为 0, 输入数据范围为 大于 0 if( numa[i] != numa[i-1] ){ numb[i] = i-1; } else{ // 若相同, 则存 前一个数所计算的数 numb[i] = numb[i-1]; } } while( m -- ){ scanf( "%d%d%d", &l, &r, &x ); if( x != numa[r] ){ printf( "%d\n", r ); } // 边界相同, 查表 else if( numb[r] >= l ){ printf( "%d\n", numb[r] ); } else{ puts( "-1" ); } } return 0; }