The shorter, the simpler. With this problem, you should be convinced of this truth. 
   
  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(单调队列)的更多相关文章

  1. BestCoder Round #89 B题---Fxx and game(单调队列)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945     问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路:  B ...

  2. 单调队列 && 斜率优化dp 专题

    首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...

  3. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  4. BZOJ 1047 二维单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...

  5. 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列

    第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...

  6. BZOJ1047: [HAOI2007]理想的正方形 [单调队列]

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2857  Solved: 1560[Submit][St ...

  7. hdu 3401 单调队列优化DP

    Trade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  8. 【转】单调队列优化DP

    转自 : http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列是一种严格单调的队列,可以单调递增,也可以单调递减.队 ...

  9. hdu3530 单调队列

    Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

随机推荐

  1. 用命令行执行ROBOT FRAMEWORK

    除了在ride中执行用例,我们也可以通过命令行的形式执行用例. 1.执行一整个项目 pybot+项目路径 2.执行某个测试集 pybot+测试集的路径 3.执行某个测试集里面的某个用例 pybot - ...

  2. Django之站内搜索-Solr,Haystack

    java -version 不多说 solr 是java 开发的 java version "1.7.0_79" Java(TM) SE Runtime Environment ( ...

  3. rtems 4.11 启动流程(arm, beagle)

    请参照官方的 bsp_howto 文档,对arm来说,首先执行的文件是start.S start.S c/src/lib/libbsp/arm/shared/start/start.S 1.从 _st ...

  4. java中BigDecimal的学习

    干着java的活,但是看的都是一些偏底层的东西(或者我根本就没有看),有点荒废了java的学习. 最近一直在用到一个类是BigDecimal,但都是模棱两可地在那儿用,并没有深入研究这个类的细节,感觉 ...

  5. php解析带有命名空间的xml

    xml如果带有命名空间我们将如何解析,例如: <ns1:CreateBillResponse xmlns:ns1="http://neusoft.com" xmlns:xsd ...

  6. eclipse中三大利器

    eclipse中两大利器: 首先说下用eclipse开发工具.进行java代码,开发的时候,我们开发完成以后.需要测试.大部分我们用Junit测试工具.可是内部的代码覆盖率.和结构我们看的不是那么详细 ...

  7. ios - UISearchBar输入框背景色

    //输入框背景色 bar.searchBarStyle = UISearchBarStyleMinimal; [bar positionAdjustmentForSearchBarIcon:UISea ...

  8. python scrapy爬虫框架

    http://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/tutorial.html scrapy 提取html的标签内容 from scrapy.selec ...

  9. 【BZOJ4966】总统选举 线段树+随机化

    [BZOJ4966]总统选举 Description 黑恶势力的反攻计划被小C成功摧毁,黑恶势力只好投降.秋之国的人民解放了,举国欢庆.此时,原秋之国总统因没能守护好国土,申请辞职,并请秋之国人民的大 ...

  10. Swift 学习笔记(扩展和泛型)

    在开始介绍Swift中的扩展之前,我们先来回忆一下OC中的扩展. 在OC中如果我们想对一个类进行功能的扩充,我们会怎么做呢. 对于面向对象编程的话,首先会想到继承,但是继承有两个问题. 第一个问题:继 ...