zoukankan      html  css  js  c++  java
  • 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
    ");
        }
    }
  • 相关阅读:
    final关键字
    海思NB-IOT的SDK看门狗的使用
    IAR环境下编译CC2640入门开发
    股票操作记录180613(2)
    股票操作笔记18年6月13(1)
    PyYAML学习第一篇
    片仔癀犯过的错误
    2018年5月份片仔癀最佳演员奖
    2018-05-22两只垃圾基金南方产业活力000955和鹏华全球高收益债券000290
    linux c编程:网络编程
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5187038.html
Copyright © 2011-2022 走看看