黑龙江省赛 A Sequence Game(离散化+莫队算法+ST表 RMQ)

2024-02-10 16:48

本文主要是介绍黑龙江省赛 A Sequence Game(离散化+莫队算法+ST表 RMQ),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题 D: A Sequence Game

时间限制: 1 Sec  内存限制: 128 MB
提交: 148  解决: 42
[提交] [状态] [讨论版] [命题人:admin]

题目描述

One day, WNJXYK found a very hard problem on an Online Judge. This problem is so hard that he had been thinking about the solutions for a couple of days. And then he had a surprise that he misunderstood that problem and easily figured out a solution using segment tree. Now he still wonders that solution for the misread problem.
There is a sequence with N positive integers A1,A2,…,An and M queries. Each query will give you an interval [L,R] and require an answer with YES / NO indicates that whether the numbers in this interval are continuous in its integer range. 
Let us assume that the maximal number in an interval is mx and the minimal   number is mi. The numbers in this interval are continuous in its integer range means that each number from mi to mx appears at least once in this interval.

 

输入

The input starts with one line contains exactly one positive integer T which is the number of test cases. And then there are T cases follow.
The first line contains two positive integers n,m which has been explained above.
The second line contains n positive integers A1,A2,…,An.
Then there will be m lines followed. Each line contains to positive numbers Li,Ri indicating that the i th query’s interval is [Li,Ri].

输出

For each test case, output m line.
Each of following m lines contains a single string “YES”/ “NO” which is the answer you have got.

样例输入

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

样例输出

YES
YES
NO
NO
YES
YES

提示

T=5
1≤n≤100000
1≤Ai≤10^9
1≤m≤100000
The input file is very large, so you are recommend to use scanf() and printf() for IO.

[提交][状态]

题意:给你一个长度为n的序列,有m次询问,每次问询一个区间中是否区间中最大的数到最小的数都出现了至少一次

题解:明显可以用莫队算法对于区间查询进行排序然后分块处理,每次查询区间的最大值和最小值然后莫队处理区间中不同的数字的个数,若最大值减去最小值+1<=区间中不同数字的个数,则输出YES否则输出NO,区间RMQ可以用ST表进行优化,复杂度O(n*sqrt(n)),一开始我用map标记区间中出现的数字的个数总是运行错误,将数据离散化再用数组标记就可以了;

#include<bits/stdc++.h>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define SI(i) scanf("%lld",&i)
#define PI(i) printf("%lld\n",i)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int MAX=2e5+5;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
int dir[9][2]={0,1,0,-1,1,0,-1,0, -1,-1,-1,1,1,-1,1,1};
template<class T>bool gmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>bool gmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>void gmod(T &a,T b){a=((a+b)%mod+mod)%mod;}
typedef pair<ll,ll> PII;ll n,cnt,st[MAX][25][2],powll[50],lg[MAX],ans[MAX],vis[MAX],a[MAX],b[MAX];int del(ll x)
{x=st[x][0][0];if(vis[x]==1) cnt--;vis[x]--;
}int add(ll x)
{x=st[x][0][0];if(vis[x]==0) cnt++;vis[x]++;
}struct node
{ll l,r,id;
}p[MAX];int block;
int cmp(node a,node b)
{if(!block) block++;if( (a.l/block) == (b.l/block) )return a.r<b.r;return a.l<b.l;
}int main()
{powll[0]=1;for(int i=1;i<=25;i++)powll[i]=(powll[i-1]<<1);lg[0]=-1;int T;scanf("%d",&T);while(T--){memset(vis,0,sizeof(vis));ll m;scanf("%lld%lld",&n,&m);block=(int)(sqrt(n*1.0)+0.5);for(int i=1;i<=n;i++)scanf("%lld",&a[i]),lg[i]=lg[i>>1]+1,b[i]=a[i];sort(b+1,b+1+n);for(int i=1;i<=n;i++){st[i][0][0]=lower_bound(b+1,b+1+n,a[i])-b-1;st[i][0][1]=st[i][0][0];}for(int j=1;(1<<j)<=n;j++)for(int i=1;i<=n;i++){st[i][j][0]=max(st[i][j-1][0],st[i+(1<<j-1)][j-1][0]);st[i][j][1]=min(st[i][j-1][1],st[i+(1<<j-1)][j-1][1]);}for(int i=1;i<=m;i++){scanf("%lld%lld",&p[i].l,&p[i].r);p[i].id=i;}sort(p+1,p+1+m,cmp);ll L=1,R=0;cnt=0;for(int i=1;i<=m;i++){ll l=p[i].l;ll r=p[i].r;while(L<l) del(L++);while(L>l) add(--L);while(R>r) del(R--);while(R<r) add(++R);ll d=lg[r-l+1];r=r-powll[d]+1;ll a=min(st[l][d][1],st[r][d][1]);ll b=max(st[l][d][0],st[r][d][0]);if(cnt>=b-a+1)ans[p[i].id]=1;elseans[p[i].id]=0;}for(int i=1;i<=m;i++)if(ans[i])printf("YES\n");elseprintf("NO\n");}return 0;
}

 

这篇关于黑龙江省赛 A Sequence Game(离散化+莫队算法+ST表 RMQ)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/697592

相关文章

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

PostgreSQL 序列(Sequence) 与 Oracle 序列对比差异分析

《PostgreSQL序列(Sequence)与Oracle序列对比差异分析》PostgreSQL和Oracle都提供了序列(Sequence)功能,但在实现细节和使用方式上存在一些重要差异,... 目录PostgreSQL 序列(Sequence) 与 oracle 序列对比一 基本语法对比1.1 创建序

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1