题目链接

http://acm.split.hdu.edu.cn/showproblem.php?pid=5875

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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5877 5876 5874 5873 5872 
 
题意:一个长度为n的数列,q次询问,每次输入l 和 r 表示一个区间A[l]~A[r]  求A[l]%A[l+1]A[l+2]...A[r];
 
思路:a%b=r  那么r<a 所以在区间连续取余中 如果一个数对A[i]取完余,后面的数大于A[i],则不用取余,所以只需要找A[i]后小于A[i]且最近的A[j]进行取余,依次进行下去。其实找距离最近且小于自己的数就是 单调栈,这个算法很神奇,复杂度为O(n);
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int A[];
int pos[];
int s[]; int main()
{
int T,N,M;
cin>>T;
while(T--)
{
scanf("%d",&N);
for(int i=;i<=N;i++)
scanf("%d",&A[i]);
int num=;
A[N+]=-;
s[]=N+;
for(int i=N;i>=;i--)///单调栈;
{
while(A[i]<A[s[num]]) num--;
pos[i]=s[num];
s[++num]=i;
}
scanf("%d",&M);
while(M--)
{
int l,r;
scanf("%d%d",&l,&r);
int tmp=A[l];
while(l<=r)
{
if(tmp==) break;
if(pos[l]>r) break;
tmp=tmp%A[pos[l]];
l=pos[l];
}
printf("%d\n",tmp);
}
}
return ;
}
 
 

2016 大连网赛---Function(单调栈)的更多相关文章

  1. 2016 大连网赛---Different GCD Subarray Query(GCD离散+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5869 Problem Description This is a simple probl ...

  2. 2016 大连网赛---Weak Pair(dfs+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...

  3. 团 大连网赛 1007 Friends and Enemies

    //大连网赛 1007 Friends and Enemies // 思路:思路很棒! // 转化成最大二分图 // 团:点集的子集是个完全图 // 那么朋友圈可以考虑成一个团,原题就转化成用团去覆盖 ...

  4. hdu5662 YJQQQAQ and the function (单调栈)

    Problem Description YJQQQAQ has an array A of length n. He defines a function fl,r,k where l,r,k are ...

  5. 2016大连网络赛 Function

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Probl ...

  6. HDU 5876 (大连网赛1009)(BFS + set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意:给定一个图(n个顶点m条边),求其补图最短路 思路:集合a表示当前还未寻找到的点,集合b表 ...

  7. 【CF815D】Karen and Cards 单调栈+扫描线

    [CF815D]Karen and Cards 题意:一张卡片有三个属性a,b,c,其上限分别为A,B,C,现在有n张卡片,定义一张卡片能打败另一张卡片当且仅当它的至少两项属性要严格大于另一张的对应属 ...

  8. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  9. 51nod1423 最大二"货" 单调栈

    枚举每个点作为次大值,用单调栈处理出左边 / 右边第一个比它大的数,直接回答 复杂度$O(n)$ #include <cstdio> #include <cstring> #i ...

随机推荐

  1. 浅谈iOS版本号

    作者:Travis FIR.im 一直在尽量兼容不同使用习惯的版本号形式, 但是在使用中我们发现好多开发者对怎么更好的用版本号来标示应用很陌生. 这是篇基础文章, 简单介绍 iOS 的版本号. 名词解 ...

  2. C#设计模式-工厂模式

    引入人.工厂.和斧子的问题 原始社会时,劳动社会基本没有分工,需要斧子的人(调用者)只好自己去磨一把斧子,每个人拥有自己的斧子,如果把大家的石斧改为铁斧,需要每个人都要学会磨铁斧的本领,工作效率极低. ...

  3. java 线程协作 join()

    在实际开发中我们往往会遇到这样的情况一个线程的执行需要依赖另一个线程执行后的结果.即主线程生成并起动了子线程,如果子线程里要进行大量的耗时的运算,主线程往往将于子线程之前结束,但是如果主线程处理完其他 ...

  4. 18.实现如下类之间的继承关系,并编写Music类来测试这些类。

    package zhongqiuzuoye; public class Instrument { public void play() { System.out.println("弹奏乐器& ...

  5. UML基础系列:用例图

    1. 概述 用例图(Use Case Diagram)描述“用户.需求.系统功能单元”之间的关系,是参与者所能观察和使用到的系统功能模型图. 用例图用于需求分析阶段 用例图包含6个基本元素:参与者(A ...

  6. maven项目部署打包

    方法一.把maven依赖的jar包一起打包 http://maven.apache.org/plugins/maven-assembly-plugin/usage.html pom/build中加入以 ...

  7. Ajax基础知识《一》

    对于网站开发人员,一定不会陌生的Ajax技术,本篇就让我们认识一下它,或许在日后的开发过程中我们就可以使用到.Ajax在那方面使用的比较多呢?答案:表单注册,传统的表单注册,有时需要填写大量的信息,当 ...

  8. structs2之多文件上传

    //首先是Action部分import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; i ...

  9. 2014 -> 2015

    2014年初在公司的发展不太顺利, 发现比好多小伙伴的发展速度都要慢了,钱不多,职位也不高,做的事情成长也不快. 为了职业发展考虑,年中就一直想换一个好一点的工作机会, 年中拿了好几个offer, 有 ...

  10. 献给所有从事IT行业拥有梦想的英语渣们

    本文适合读者:从小到大英语都不好,如今选择IT行业需要重拾英语追寻梦想的人,英语大神们请绕行.. 目录 一.背景介绍 二.明确学习目标 2.1 英文文法 2.2 词汇量 三.题外话 3.1 关于本文 ...