码迷,mamicode.com
首页 > 其他好文 > 详细

FJUT ACM 2592 查询队列

时间:2018-04-15 22:55:15      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:while   查询   ane   head   amp   printf   技术分享   int   lse   

 

 

查询队列

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\n",acfind(l));
                pre[acfind(l)]=acfind(l)+1;
            }
            else
                printf("-1\n");
        }
    }
    return 0;
}

 

 

FJUT ACM 2592 查询队列

标签:while   查询   ane   head   amp   printf   技术分享   int   lse   

原文地址:https://www.cnblogs.com/qq136155330/p/8849678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!