HDU 5875 Function(ST表+二分)
【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5875
【题目大意】
给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不断对下一个数取模之后的结果。
【题解】
考虑取模的递减性质,最多取模logn次,因此如何快速找出下一个取模的位置是解决这道题的关键,首先利用ST表预处理区间最小值,之后我们每次二分取模起点到终点这个区段,每次优先查询左区间(类似于线段树上的路径查询),这样子就能logn找到取模的位置,然后直接取模就好。
【代码】
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=200010;
int d[N][30],f[N][30],a[N],lg2[N];
int T,n,m,l,r,q;
void rmq_init(int n){
for(int i=2;i<=n;i++)lg2[i]=lg2[i/2]+1;
for(int i=1;i<=n;i++)scanf("%d",&f[i][0]);
for(int j=1;(1<<j)<=n;j++){
for(int i=1;i+(1<<j)-1<=n;i++){
f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
}
}
int rmq_min(int l,int r){int k=lg2[r-l+1];return min(f[l][k],f[r-(1<<k)+1][k]);}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
rmq_init(n);
scanf("%d",&m);
while(m--){
scanf("%d%d",&l,&r);
if(l==r){
printf("%d\n",f[l][0]);
continue;
}
int flag=1,ans=f[l][0],L=l+1,R=r;
while(flag){
while(L<R){
int mid=(L+R)>>1;
if(rmq_min(L,mid)<=ans)R=mid;
else{
if(rmq_min(mid+1,R)<=ans)L=mid+1;
else{flag=0;break;}
}
}if(flag)ans=ans%f[L][0];
if(L==r)break;
L++; R=r;
}printf("%d\n",ans);
}
}return 0;
}
HDU 5875 Function(ST表+二分)的更多相关文章
- HDU 5875 Function st + 二分
Function Problem Description The shorter, the simpler. With this problem, you should be convinced ...
- HDU 5875 Function(RMQ-ST+二分)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- 「ZJOI2018」胖(ST表+二分)
「ZJOI2018」胖(ST表+二分) 不开 \(O_2\) 又没卡过去是种怎么体验... 这可能是 \(ZJOI2018\) 最简单的一题了...我都能 \(A\)... 首先我们发现这个奇怪的图每 ...
- 2019CCPC网络赛 C - K-th occurrence HDU - 6704(后缀数组+ST表+二分+主席树)
题意 求区间l,r的子串在原串中第k次出现的位置. 链接:https://vjudge.net/contest/322094#problem/C 思路 比赛的时候用后缀自动机写的,TLE到比赛结束. ...
- Interviewe HDU - 3486 (ST表+枚举 )(非二分,看下这个数据、)
YaoYao has a company and he wants to employ m people recently. Since his company is so famous, there ...
- 2016多校联合训练1 D题GCD (ST表+二分)
暑假颓废了好久啊...重新开始写博客 题目大意:给定10w个数,10w个询问.每次询问一个区间[l,r],求出gcd(a[l],a[l+1],...,a[r])以及有多少个区间[l',r']满足gcd ...
- 【BZOJ-4310】跳蚤 后缀数组 + ST表 + 二分
4310: 跳蚤 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 180 Solved: 83[Submit][Status][Discuss] De ...
- Hdu 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
- BZOJ4556 [Tjoi2016&Heoi2016]字符串 SA ST表 二分答案 主席树
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ4556.html 题目传送门 - BZOJ4556 题意 给定一个长度为 $n$ 的字符串 $s$ . ...
随机推荐
- Application Loader下载安装和上传IOS app程序
如果您安装了最新版的XCode开发环境.对于在4.2及以上版本,Developer/Applications/Utilities目录中已经有ApplicationLoader程序,无需执行以下单独安装 ...
- 转:JS在文本域鼠标指定位置插入文本-柯乐义
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ArrayList与LinkedList时间复杂度之对比
package ArrayList; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections ...
- 加载loading的ajax写法
ajaxStart()与ajaxStop():当Ajax请求开始时,会触发ajaxStart()方法的回调函数.当Ajax请求结束时,会触发ajaxStop()方法的回调函数.这些方法都是全局的方法, ...
- IP地址、子网掩码和地址分类
http://blog.csdn.net/bluishglc/article/details/47909593?utm_source=tuicool&utm_medium=referral 实 ...
- SingleNumber python实现
Single Number Given an array of integers, every element appears twice except for one. Find that sing ...
- Qt中如何禁掉所有UI操作以及注意事项(处理各个widget的eventFilter这一层,但是感觉不好,为什么不使用QApplication呢)
刚做完的一个项目,在测试时出现了一个问题:由于多线程的存在,当进行语音识别时:如果用户点击程序界面上的button或者其他接受点击事件后会发出信号的widget时,程序会crash ! 后来尝试着从多 ...
- 一个简单的游标删除SQL SERVER表
use databaseName declare @tblname char(100) declare @sql char(5000) declare table_cursor cursor for ...
- HDU 5062 Beautiful Palindrome Number(数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...
- Extjs 4 生成饼状图的例子
前台: //远程抄表设备下落图表数据 var Store1 = new Ext.data.Store({ <span style="white-space:pre"> ...