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函数,这是造成我开始超时的原因

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e2+, mod = 1e9+, inf = 2e9; int dp[N][],a[N],n,q;
void st() {
for(int j = ; (<<j) <= n; ++j) {
for(int i = ; (i + (<<j) - ) <= n; ++i) {
dp[i][j] = min(dp[i][j-],dp[i + (<<(j-))][j-]);
}
}
}
int query(int l,int r) {
int len = r - l + ;
int k = ;
while (( << (k + )) <= len) k++;
return min(dp[l][k],dp[r - (<<k) + ][k]);
}
int _binary_search(int l,int r,int res) {
int s = r+;
while(l <= r) {
int md = (l + r) >> ;
if(query(l,md) <= res) r = md - ,s = md;
else l = md + ;
}
return s;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]),dp[i][]=a[i];
st();
scanf("%d",&q);
for(int i = ; i <= q; ++i) {
int x,y,L,R;
scanf("%d%d",&x,&y);
int res = a[x];
L = x+, R = y;
while(L <= R && res) {
L = _binary_search(L,R,res);
if(L<=R) {
res%=a[L];L++;
}
}
printf("%d\n",res);
}
}
return ;
}

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. 百度和google的区别

    一.百度 二.google

  2. 禁用visual studio实时调试器

    最近每次开机时都会出来一个visual Studio实时调试器,报“发生了未处理的异常(‘System ComponentModel.Win32Exception’,发生位置是 BSSocketSms ...

  3. Django~queries

    API queries create, retrieve, update and delete

  4. 解压zip文件中文文件名乱码问题

    主要原因是,在windows下压缩文件时,是以系统的默认编码(gbk,gb18030)来压缩,zip文件并没有声明编码的格式,因此,linux下解压缩时,也会使用系统默认的格式(utf-8)解压缩,编 ...

  5. jquery 中的一写常用方法

    $('form').submit(); // 表单提交 window.parent.location.reload(); // 子窗口刷新父页面 window.location.reload(); / ...

  6. 【leetcode】 Permutation Sequence (middle)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. 【leetcode】Single Number (Medium) ☆

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  8. sqlserver 动态行转列

    DECLARE @SQL VARCHAR(8000)SET @SQL = 'select overcode 'SELECT @SQL = @SQL + ' , max(case header when ...

  9. cocos2d-x 第三篇 基本概念介绍

    场景(scene): 也有人叫做屏幕或舞台,是一个独立的程序流,一个程序可以有很多场景但当前运行的场景就只有一个.比如游戏中可以有介绍场景,菜单场景,第一关场景,过场1场景,第二关场景,胜利场景等.一 ...

  10. 22中编程语言的HelloWorld

    C:printf("HelloWorld"); C++ : cout<<"HelloWorld"; QBasic : Print "Hel ...