Function

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1866    Accepted Submission(s): 674

Problem Description
The shorter, the simpler. With this problem, you should be convinced of this truth.
  
  You are given an array A of N postive integers, and M queries in the form (l,r). A function F(l,r) (1≤l≤r≤N) is defined as:
F(l,r)={AlF(l,r−1) modArl=r;l<r.
You job is to calculate F(l,r), for each query (l,r).
 
Input
There are multiple test cases.
  
  The first line of input contains a integer T, indicating number of test cases, and T test cases follow.
  
  For each test case, the first line contains an integer N(1≤N≤100000).
  The second line contains N space-separated positive integers: A1,…,AN (0≤Ai≤109).
  The third line contains an integer M denoting the number of queries.
  The following M lines each contain two integers l,r (1≤l≤r≤N), representing a query.
 
Output
For each query(l,r), output F(l,r) on one line.
 
Sample Input
1
3
2 3 3
1
1 3
 
Sample Output
2
 
Source
 
这个题目完全可以出个单调递减的序列卡时间啊...不知道时间复杂度怎么降下来的..因为右边比当前数大的数字是造不成影响的,所以我们用单调栈预处理出每个的右边,这样就可以跳着找了..但是我觉得数据强点不靠谱啊..
///pro do this : a[l]%a[l+1]%...%a[r]
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;
typedef long long LL;
const LL INF = 1e10;
const int N = ;
LL a[N],R[N];
int main(){
int tcase,n;
scanf("%d",&tcase);
while(tcase--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
}
memset(R,-,sizeof(R));
for(int i=n-;i>=;i--){ ///单调栈维护其右边小于 a[i] 的第一个数
int t =i+;
while(true){
if(a[i]>=a[t]){
R[i] = t;
break;
}
if(R[t]==-){
break;
}
t = R[t];
}
R[i] = t;
}
int m;
scanf("%d",&m);
while(m--){
int l,r;
scanf("%d%d",&l,&r);
LL ans = a[l];
int nxt = l;
while(R[nxt]<=r&&R[nxt]!=-){
nxt = R[nxt];
ans = ans%a[nxt];
}
printf("%lld\n",ans);
}
}
return ;
}

hdu 5875(单调栈)的更多相关文章

  1. hdu 1506 单调栈问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目的意思其实就是要找到一个尽可能大的矩形来完全覆盖这个矩形下的所有柱子,只能覆盖柱子,不能留空 ...

  2. hdu 5033 单调栈 ****

    看出来是单调栈维护斜率,但是不会写,2333,原来是和询问放在一起的 #include <iostream> #include <cstdio> #include <cs ...

  3. HDU 5033 (单调栈维护凸包) Building

    题意: 一个人在x轴上,他的左右两侧都有高楼,给出楼的横坐标Xi和高度Hi还有人的位置pos,求人所能看到的天空的最大角度. 分析: 将建筑物和人的位置从左到右排序,对于每个位置利用栈求一次人左边建筑 ...

  4. hdu 4923 单调栈

    http://acm.hdu.edu.cn/showproblem.php?pid=4923 给定一个序列a,元素由0,1组成,求一个序列b,元素在0~1之间,并且保证递增.输出最小的∑(ai−bi) ...

  5. hdu 3410 单调栈

    http://acm.hdu.edu.cn/showproblem.php?pid=3410 Passing the Message Time Limit: 2000/1000 MS (Java/Ot ...

  6. hdu 1505 单调栈升级版

    #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...

  7. hdu 1506 单调栈

    #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...

  8. HDU 5875 H - Function 用单调栈水过了

    http://acm.hdu.edu.cn/showproblem.php?pid=5875 单调栈,预处理to[i]表示第一个比a[i]小的数字,一直跳就可以. 这题是数据水而已. 这里学习下单调栈 ...

  9. HDU 5875 Function (线段树+gcd / 单调栈)

    题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对数字b取模时:如果a<b,则等于原数,否则a会变小至少一半. ...

随机推荐

  1. powershell网络钓鱼获取用户密码

    1.powershell网络钓鱼脚本: https://raw.githubusercontent.com/enigma0x3/Invoke-LoginPrompt/master/Invoke-Log ...

  2. LibreOJ #539. 「LibreOJ NOIP Round #1」旅游路线(倍增+二分)

    哎一开始看错题了啊T T...最近状态一直不对...最近很多傻逼题都不会写了T T 考虑距离较大肯定不能塞进状态...钱数<=n^2能够承受, 油量再塞就不行了...显然可以预处理出点i到j走c ...

  3. 【ST】【CF855B】 Marvolo Gaunt's Ring

    传送门 Description 给定三个数 \(p~,~q~,~r~\),以及一个数组 \(a\), 找出三个数 \(i~,~j~,~k\) ,其中 \(i~\leq~j~\leq~k\) 最大化 \ ...

  4. Python高级语法总结

    1.Python面向对象 创建类 使用class语句来创建一个新类,class之后为类的名称并以冒号结尾,如下实例: class ClassName: '类的帮助信息' #类文档字符串 class_s ...

  5. 洛谷P1195 口袋的天空

    口袋的天空 327通过 749提交 题目提供者该用户不存在 标签云端 难度普及+/提高 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 暂时没有讨论 题目背景 小杉坐在教室里,透 ...

  6. IOS性能调优系列:使用Time Profiler发现性能瓶颈

    硬广:<IOS性能调优系列>第五篇,预计会有二十多篇,持续更新,欢迎关注. 之前四篇都是关注于内存方面,分析了内存泄漏.僵尸对象.内存分配,本篇介绍Time Profiler工具的使用,开 ...

  7. 3 ways to download files with PowerShell

    Perhaps the greatest strength of PowerShell is it's foundation on the .NET framework. The .NET frame ...

  8. MyEclipse解决Launching xx on MyEclipse Tomcat has encountered a problem

    单击工具栏Run,选中Run Configurations... 将MyEclipse Server Application里面的工程右击选择Delete就好了.

  9. Node + vue 实现移动官网

    简介 使用 Node + vue 对公司的官网进行了一个简单的移动端的实现. 源码 https://github.com/wx1993/node-vue-fabaocn 效果 组件 轮播图(使用 vu ...

  10. mac activemq

    安装 brew install maven 运行 To have launchd start activemq now and restart at login: brew services star ...