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 ...
随机推荐
- 网络通信数据处理 Xbytestring类
PS_Xbytestring a byte string for store low level data type 文件夹[TOC] PS_Xbytestring 文件夹TOC base info ...
- C# 托管
委托 委托让我们可以把函数引用保存在变量中.这就像在 C++ 中使用 typedef 保存函数指针一样. 委托使用关键字 delegate 声明.看看这个例子,你就能理解什么是委托: 例子: 代码: ...
- 【Caffe】利用log文件绘制loss和accuracy(转载)
(原文地址:http://blog.csdn.net/liuweizj12/article/details/64920428) 在训练过程中画出accuracy 和loss曲线能够更直观的观察网络训练 ...
- 数据挖掘、目标检测中的cnn和cn---卷积网络和卷积神经网络
content 概述 文字识别系统LeNet-5 简化的LeNet-5系统 卷积神经网络的实现问题 深度神经网路已经在语音识别,图像识别等领域取得前所未有的成功.本人在多年之前也曾接触过神经网络.本系 ...
- android Bluetooth-蓝牙
bluetooth 一.开启蓝牙 1.获取BluetoothAdapter BluetoothAdapter.getDefaultAdapter() 2.判断手机设备是否 有蓝牙模块 3.开启蓝牙设备 ...
- Windows App开发之经常使用控件与应用栏
控件的属性.事件与样式资源 怎样加入控件 加入控件的方式有多种,大家更喜欢以下哪一种呢? 1)使用诸如Blend for Visual Studio或Microsoft Visual Studio X ...
- python 基础 6.0 异常的常用形式
一. 异常 异常既是一个时间,该事件会在程序执行过程中发生,影响了程序的正常执行.一般情况下,在python无法正常处理程序时就会发生一个异常.异常是python对象,表示一个错误.当python ...
- android菜鸟学习笔记6----android布局(一)
Android应用的UI组件都是继承自View类,View类表示的就是一个空白的矩形区域.常用的组件如TextView.Button.EditText等都直接或间接继承自View. 此外,View还有 ...
- 题解 P1001 【A+B Problem】
#include<iostream> using namespace std; #define I int a,b; #define AK cin>>a>>b; # ...
- 使用nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器
参考: 1,使用nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器笔记(一)http://blog.csdn.net/xdwyyan/article/details/4319 ...