Function

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
 

题意:

  给你一个n,n个数

  m个询问,每次询问你 l,r,, a[l] % a[l+1] % a[l+2] %……a[r] 结果是多少

题解;

  每次有效的取模会使结果减半,因此只有log次有效取模,每次往右找一个不大于结果的最靠左的数,ST表+二分

  注意RMQ查询的时候少用 log函数,这是造成我开始超时的原因

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<vector>
  7. using namespace std;
  8.  
  9. #pragma comment(linker, "/STACK:102400000,102400000")
  10. #define ls i<<1
  11. #define rs ls | 1
  12. #define mid ((ll+rr)>>1)
  13. #define pii pair<int,int>
  14. #define MP make_pair
  15.  
  16. typedef long long LL;
  17. const long long INF = 1e18;
  18. const double Pi = acos(-1.0);
  19. const int N = 1e5+, M = 1e2+, mod = 1e9+, inf = 2e9;
  20.  
  21. int dp[N][],a[N],n,q;
  22. void st() {
  23. for(int j = ; (<<j) <= n; ++j) {
  24. for(int i = ; (i + (<<j) - ) <= n; ++i) {
  25. dp[i][j] = min(dp[i][j-],dp[i + (<<(j-))][j-]);
  26. }
  27. }
  28. }
  29. int query(int l,int r) {
  30. int len = r - l + ;
  31. int k = ;
  32. while (( << (k + )) <= len) k++;
  33. return min(dp[l][k],dp[r - (<<k) + ][k]);
  34. }
  35. int _binary_search(int l,int r,int res) {
  36. int s = r+;
  37. while(l <= r) {
  38. int md = (l + r) >> ;
  39. if(query(l,md) <= res) r = md - ,s = md;
  40. else l = md + ;
  41. }
  42. return s;
  43. }
  44. int main() {
  45. int T;
  46. scanf("%d",&T);
  47. while(T--) {
  48. scanf("%d",&n);
  49. for(int i = ; i <= n; ++i) scanf("%d",&a[i]),dp[i][]=a[i];
  50. st();
  51. scanf("%d",&q);
  52. for(int i = ; i <= q; ++i) {
  53. int x,y,L,R;
  54. scanf("%d%d",&x,&y);
  55. int res = a[x];
  56. L = x+, R = y;
  57. while(L <= R && res) {
  58. L = _binary_search(L,R,res);
  59. if(L<=R) {
  60. res%=a[L];L++;
  61. }
  62. }
  63. printf("%d\n",res);
  64. }
  65. }
  66. return ;
  67. }

HDU 5875 Function st + 二分的更多相关文章

  1. HDU 5875 Function(RMQ-ST+二分)

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

  2. HDU 5875 Function 优先队列+离线

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...

  3. HDU 5875 Function(ST表+二分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...

  4. HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

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

  5. HDU 5875 Function

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

  6. HDU 5875 Function 大连网络赛 线段树

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

  7. HDU - 5875 Function(预处理)

    Function The shorter, the simpler. With this problem, you should be convinced of this truth.      Yo ...

  8. HDU 5875 Function -2016 ICPC 大连赛区网络赛

    题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...

  9. HDU 5875 Function (2016年大连网络赛 H 线段树+gcd)

    很简单的一个题的,结果后台数据有误,自己又太傻卡了3个小时... 题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对 ...

随机推荐

  1. NSIS脚本入门和进阶方法

    NSIS(Nullsoft Scriptable Install System)是一个开源的 Windows 系统下安装程序制作程序.它提供了安装.卸载.系统设置.文件解压缩等功能.对于新手来说,它有 ...

  2. Java计时器Timer和TimerTask用法

    package com.sy.game.test; import java.util.Timer; import java.util.TimerTask; public class TimeTask ...

  3. switch/ifelse 使用总结

    2015年3月30日 14:12:36 switch 中的 default  和 if/else  中最后的 else 尽可能的不要用 1. 不要default, 不要写默认处理逻辑, default ...

  4. solr单机环境配置并包含外部单机zookeeper

    首先和之前一样下载solr-5.3.1.tgz,然后执行下面命令释放文件并放置在/usr/目录下: $ .tgz $ /usr/ $ cd /usr/solr- 这个时候先不用启动solr,因为单机模 ...

  5. Dnsmasq安装与配置-搭建本地DNS服务器 更干净更快无广告DNS解析

    默认的情况下,我们平时上网用的本地DNS服务器都是使用电信或者联通的,但是这样也导致了不少的问题,首当其冲的就是上网时经常莫名地弹出广告,或者莫名的流量被消耗掉导致网速变慢.其次是部分网站域名不能正常 ...

  6. URAL 2019 Pair: normal and paranormal (贪心) -GDUT联合第七场

    比赛题目链接 题意:有n个人每人拿着一把枪想要杀死n个怪兽,大写字母代表人,小写字母代表怪兽.A只能杀死a,B只能杀死b,如题目中的图所示,枪的弹道不能交叉.人和怪兽的编号分别是1到n,问是否存在能全 ...

  7. 页面上有两个元素id相同,js中如何取值

    页面上有两个table,id都是”cont2",现要在js中取到这两个table,改变样式. js实现: var tab2=document.all.cont2(1);var  tab=do ...

  8. [Android Pro] Android 4.1 使用 Accessibility实现免Root自动批量安装功能

    reference to  :  http://www.infoq.com/cn/articles/android-accessibility-installing?utm_campaign=info ...

  9. python基础——面向对象编程

    python基础——面向对象编程 面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的 ...

  10. Android -- startActivityForResult-------&&&----setResult

    startActivityForResult与startActivity的不同之处 startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startActivit ...