zoukankan      html  css  js  c++  java
  • FJUT ACM 2592 查询队列

     

     

    查询队列

    TimeLimit:2500MS  MemoryLimit:32MB
    64-bit integer IO format:%lld
     
    Problem Description

    初始时,一个队列中有n个数,分别为1,2,……n

    接下来有m个操作,每次操作删去队列中在数值范围内[l,r]内最小的数

    Input

    每个测试文件只有一组数据

    第一行是两个整数n,m

    接下m行,每行有两个整数l,r

    其中n<=1e6,m<=1e6

    1<l<=r<=n

    其中20%的数据

    n <=10000

    m<=10000

    其中80%的数据

    n <= 100000

    m<=1000000

    其中100%的数据

    n <=1000000

    m<=1000000

    Output

    对于每次操作输出被删去的数,若不存在数值在[l,r]内的数则输出-1

    SampleInput
    10 10
    2 10
    3 5
    1 6
    1 6
    4 9
    4 4
    3 3
    5 5
    6 10
    4 5
    
    SampleOutput
    2
    3
    1
    4
    5
    -1
    -1
    -1
    6
    -1
    
     
    【思路】:一开始,我的思路是用并查集连接用过的值,然后l,r进行遍历,在经行判断
    然后一发

    后来,想了另一种方法,每次删掉这个值,

    就将这个值与后面的值进行连接,让这个值的最大值放置于根节点,

    每次对pre【acfind(l)】=acfind(l)+1;

    就成功的将最大值向后连接。

    这样每次查询就是只查询一个到根节点的路径

    记得进行路径压缩

    这样查询复杂度位O(log)

    一发

    #include<bits/stdc++.h>
    #define MAXN 1000005
    using namespace std;
    int pre[MAXN];
    int n,m;
    void Init()
    {
        for(int i=0; i<MAXN; i++)
        {
            pre[i]=i;
        }
    }
    int acfind(int x)
    {
        return pre[x]==x?x:pre[x]=acfind(pre[x]);
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            Init();
            for(int i=0; i<m; i++)
            {
                int l,r;
                scanf("%d %d",&l,&r);
                if(acfind(l)<=r)
                {
                    int ans=l;
                    printf("%d
    ",acfind(l));
                    pre[acfind(l)]=acfind(l)+1;
                }
                else
                    printf("-1
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    UVA 3942 Remember the Word (Trie+DP)题解
    POJ 3630 Phone List(字符串前缀重复)题解
    HDU 1247 Hat’s Words(字典树)题解
    hdu 1671 Phone List(字典树)题解
    HDU1251 统计难题 (字典树模板)题解
    BZOJ 1556 墓地秘密
    BZOJ 3624 免费道路
    BZOJ 2286 消耗战
    BZOJ 3694 最短路
    BZOJ 1589 采集糖果
  • 原文地址:https://www.cnblogs.com/qq136155330/p/8849678.html
Copyright © 2011-2022 走看看