题目链接

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. Js~动态判断PC和手机浏览器

    这个只是一个小知识,也是在网上找的,挺好用! 动态判断浏览器是PC还是移动端! <script> var browser={ versions:function(){ var u = na ...

  2. [转]五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT)

    当Adobe.Microsoft.Sun等一系列巨头开始表现出对"开源"的青睐时,"开源"的时代即将到来!现今存在的开源协议很多,而经过Open Source ...

  3. HQL查询语言——转载(http://www.cnblogs.com/20gg-com/p/6045739.html)

    Hibernate查询语言(HQL)是一种面向对象的查询语言,类似于SQL,但不是对表和列操作,HQL适用于持久对象和它们的属性. HQL查询由Hibernate转换成传统的SQL查询,这在圈上的数据 ...

  4. 每天一个linux命令(37):date命令

    在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...

  5. rabbitMQ第四篇:远程调用

    前言:前面我们讲解的都是本地服务器,现在如果需要远程计算机上运行一个函数,等待结果.这就是一个不同的故事了,这种模式通常被称为远程过程调用或者RPC. 本章教程我们使用RabbitMQ搭建一个RPC系 ...

  6. Socket实现仿QQ聊天(可部署于广域网)附源码(2)-服务器搭建

    1.前言 这是本系列的第二篇文章,第一篇文章得到了很多朋友们的支持,在这里表示非常的感谢.对于这一系列文章需要补充的是这只是一篇入门级别的Socket通信文章,对于专业人员来说完全可以跳过.本文只介绍 ...

  7. sql基础语句大杂烩

    (坑Open Office,这排版...) 1.distinct列出不同值,过滤掉相同的值 例:company中有两个相同的值比如(apple和apple)时,则只取出一个值 SELECT DISTI ...

  8. poj 2594Treasure Exploration(有向图路径可相交的最小路径覆盖)

    1 #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> ...

  9. UvaOJ10369 - Arctic Network

    /* The first line of each test case contains 1 <= S <= 100, the number of satellite channels! ...

  10. c#Dictionary键值对的使用

    直接粘代码吧 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...