Educational Codeforces Round 7 C. Not Equal on a Segment 并查集

C. Not Equal on a Segment

题目连接:

http://www.codeforces.com/contest/622/problem/C

Description

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 Input

6 4
1 2 1 1 3 5
1 4 1
2 6 2
3 4 1
3 4 2

Sample Output

2
6
-1
4

Hint

题意

有n个数,然后m个询问,每次询问给你l,r,x

让你找到一个位置k,使得a[k]!=x,且l<=k<=r

题解:

并查集,我们将a[i]=a[i-1]的合并在一起,这样,我们就能一下子跳很多了

由于是找到不相等的,所以最多跳一步就能出结果,所以复杂度应该是比nlogn小的

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int a[maxn];
int fa[maxn];
int fi(int x)
{
    return x == fa[x]?x:fa[x]=fi(fa[x]);
}
int uni(int x,int y)
{
    int p = fi(x),q = fi(y);
    if(p != q)
    {
        fa[p]=fa[q];
    }
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        fa[i]=i;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]==a[i-1])
            uni(i,i-1);
    }
    for(int i=1;i<=m;i++)
    {
        int flag = 0;
        int l,r,x;
        scanf("%d%d%d",&l,&r,&x);
        int p = r;
        while(p>=l)
        {
            if(a[p]!=x)
            {
                printf("%d
",p);
                flag = 1;
                break;
            }
            p=fi(p)-1;
        }
        if(flag==0)printf("-1
");
    }
}
原文地址:https://www.cnblogs.com/qscqesze/p/5187038.html