HDU5875Function(单调队列)
You are given an array AA of NN postive integers, and MM queries in the form (l,r)(l,r). A function F(l,r) (1≤l≤r≤N)F(l,r) (1≤l≤r≤N) is defined as:
F(l,r)={AlF(l,r−1) modArl=r;l<r.F(l,r)={All=r;F(l,r−1) modArl<r.
You job is to calculate F(l,r)F(l,r), for each query (l,r)(l,r).
InputThere are multiple test cases.
The first line of input contains a integer TT, indicating number of test cases, and TT test cases follow.
For each test case, the first line contains an integer N(1≤N≤100000)N(1≤N≤100000).
The second line contains NN space-separated positive integers: A1,…,AN (0≤Ai≤109)A1,…,AN (0≤Ai≤109).
The third line contains an integer MM denoting the number of queries.
The following MM lines each contain two integers l,r (1≤l≤r≤N)l,r (1≤l≤r≤N), representing a query.OutputFor each query(l,r)(l,r), output F(l,r)F(l,r) on one line.
Sample Input
1
3
2 3 3
1
1 3
Sample Output
2
题意:
已知a[]数组,现在给出m组l,r。求a[l]%a[l+1]%a[l+2]%...a[r]的结果。
思路:
a[l]<a[l+1],那么这个膜运算是可以忽视的。单调队列,逆序预处理出每一个a[]的右边第一个小于a[]的数的位置R[],然后就是用a[l]%a[R[l]]%a[R[R[l]]]...。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn],R[maxn];
int main()
{
int n,m,T,i,j,l,r,t,ans;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(i=;i<=n;i++) scanf("%d",&a[i]);
for(i=;i<=n+;i++) R[i]=n+; a[n+]=;
for(i=n-;i>=;i--){
t=i;
while(t<n){ // 得到R数组
if(a[t+]<a[i]){ R[i]=t+; break;}
if(a[R[t+]]>=a[i]) t=R[t+];
else {
for(j=t+;j<=R[t+];j++) {//这里可以二分优化
if(a[j]<a[i]) {
R[i]=j;break;
}
} break;
}
}
}
scanf("%d",&m);
while(m--){
scanf("%d%d",&l,&r);
ans=a[l];t=l;
while(R[t]<=r){ //过滤掉无用的膜运算
t=R[t];
ans%=a[t];
}
printf("%d\n",ans);
}
} return ;
}
HDU5875Function(单调队列)的更多相关文章
- BestCoder Round #89 B题---Fxx and game(单调队列)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945 问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路: B ...
- 单调队列 && 斜率优化dp 专题
首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...
- FZU 1914 单调队列
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...
- BZOJ 1047 二维单调队列
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...
- 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...
- BZOJ1047: [HAOI2007]理想的正方形 [单调队列]
1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2857 Solved: 1560[Submit][St ...
- hdu 3401 单调队列优化DP
Trade Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 【转】单调队列优化DP
转自 : http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列是一种严格单调的队列,可以单调递增,也可以单调递减.队 ...
- hdu3530 单调队列
Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
随机推荐
- C++成员不通过对象调用的直接调用写法
C++成员不通过对象调用(.或->方式)的另类(C式)调用写法 #include <iostream> using namespace std; /* 我们知道,成员函数和普通函数最 ...
- jquery插件函数传参错误
1.jquery传参通过json,可能的错误是,参数中的结束符写成了;
- python学习(十二)模块
怎么一下子就来学了模块? 其实学了判断.循环.函数等知识就可以开始下水写程序了,不用在意其他的细节,等你用到的时候再回过头去看,此所谓囫囵吞枣学习法. 为啥学模块? 有点用的.或者有点规模的程序都是要 ...
- SkipList跳表(一)基本原理
一直听说跳表这个数据结构,说要学一下的,懒癌犯了,是该治治了 为什么选择跳表 目前经常使用的平衡数据结构有:B树.红黑树,AVL树,Splay Tree(这个树好像还没有听说过),Treep(也没有听 ...
- 资源重复 uac.res resource kept(转)
一般按照网上流传的方法制作UAC.RES放到DELPHI程序里面来就可以出现盾牌.但是某些DELPHI的项目在添加了UAC.RES后编译会报错,例如: [DCC Error] E2161 Warnin ...
- c# 枚举返回字符串操作
//内部类public static class EnumHelper { public static string GetDescription(Enum value) { if (value == ...
- 获得手机的ip
本文转载至 http://blog.csdn.net/showhilllee/article/details/8746114 iosip手机 貌似ASI里获取ip地址的链接不可以了.也曾试过 ...
- Android 异常解决方法【汇总】
(1)异常:Android中引入第三方Jar包的方法(Java.lang.NoClassDefFoundError解决办法) 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方 ...
- ch.poweredge.ntlmv2-auth
<dependency> <groupId>ch.poweredge.ntlmv2-auth</groupId> <artifactId>ntlmv2- ...
- 错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009)【转】
启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required by Tomcat v5.5 Server at localhost are alre ...