思路:数组链表似的,每个数开一个pos数组,指向前面第一个相同的。。并查集思想。。核心代码:if(a[i] == a[i-1]) pos[i] = pos[i-1]; else pos[i] = i; 最近做了两个这种题了,做这题的时候也想到过记录每个数前面跟他相同的。。还是没想到这种思想,看来还是没深入我心啊~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 5;
int pos[maxn], a[maxn];
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m))
{
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if(a[i] == a[i-1]) pos[i] = pos[i-1];
else pos[i] = i;
}
int l, r, x;
while(m--)
{
scanf("%d%d%d", &l, &r, &x);
if(pos[r] <= l && a[r] == x)
puts("-1");
else if(a[r] != x)
printf("%d\n", r);
else if(a[r] == x && pos[r] > l)
printf("%d\n", pos[r]-1);
}
}
return 0;
}