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. Classical Binary Search

    Find any position of a target number in a sorted array. Return -1 if target does not exist. 与题目 Firs ...

  2. Hystrix : 解决请求会被拒绝和抛出异常并且fallback不会被调用的问题

    启动脚本增加参数:-Dserver.tomcat.max-http-header-size=102400 增大Hystrix,ribbon的各项参数; hystrix: threadpool: def ...

  3. 【bzoj4940】这是我自己的发明

    Portal --> bzoj4940 Solution (原题这题面到底是..怎么回事啊深深的套路qwq) 感觉自己对根号的算法还是很..没有感觉啊== 实际上这题和bzoj5016没有任何区 ...

  4. PID控制算法的C语言实现四 增量型PID的C语言实现

    /*------------------------------------------- 2 位置型PID C实现(控制电机转速) --------------------------------- ...

  5. Jenkins基于角色的项目权限管理

    参考博客:http://www.cnblogs.com/davidwang456/p/3701972.html 一.简介 由于jenkins默认的权限管理体系不支持用户组或角色的配置,因此需要安装第三 ...

  6. 线程函数对比 win/linux

    原文

  7. REST式的web服务

    “REST”是罗伊·菲尔丁(Roy Fielding)在他的博士论文中创造的缩写.菲尔丁论文的第5章勾画出了被称为REST风格或REST式的Web服务的知道原则.他是HTTP1.1规范的主要作者和Ap ...

  8. VisualSVN 5.1.4破解

    1. 备份visualSVNbin目录 2. 打开VS命令提示工具,反编译VisualSVN.Core.L.dll 运行命令 ildasam "VisualSVN安装目录\bin\Visua ...

  9. OpenCV---Canny边缘提取

    一:Canny算法介绍 Canny 的目标是找到一个最优的边缘检测算法,最优边缘检测的含义是: 好的检测- 算法能够尽可能多地标识出图像中的实际边缘. 好的定位- 标识出的边缘要尽可能与实际图像中的实 ...

  10. 2017北京国庆刷题Day3 morning

    期望得分:100+60+0=160 实际得分:100+30+0=130 考场上用的哈希 #include<cstdio> #include<cstring> #include& ...