版权声明:抱最大的希望,为最大的努力,做最坏的打算。 https://blog.csdn.net/qq_37748451/article/details/86623046
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.
Examples
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
题意:有n个数,然后m个询问,每次询问给你l,r,x,让你找到一个位置k,使得a[k]!=x,且l<=k<=r
思路:并查集,我们将a[i]=a[i-1]的合并在一起
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1e6+5;
int a[maxn];
int fa[maxn];
int Find(int x)
{
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void un(int x,int y)
{
int p=Find(x);
int q=Find(y);
if(p!=q)
fa[p]=fa[q];
}
int main()
{
int n,m,i,flag=0;
int l,r,x,p;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
fa[i]=i;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==a[i-1])
un(i,i-1);
}
while(m--)
{
flag=0;
scanf("%d%d%d",&l,&r,&x);
p=r;
while(p>=l)
{
if(a[p]!=x)
{
printf("%d\n",p);
flag=1;
break;
}
p=Find(p)-1;
}
if(flag==0)
printf("-1\n");
}
return 0;
}