黑龙江省赛 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

相关文章

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

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1

python中os.stat().st_size、os.path.getsize()获取文件大小

《python中os.stat().st_size、os.path.getsize()获取文件大小》本文介绍了使用os.stat()和os.path.getsize()函数获取文件大小,文中通过示例代... 目录一、os.stat().st_size二、os.path.getsize()三、函数封装一、os

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个