Function

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2427    Accepted Submission(s): 858

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

题目链接:HDU 5875

题意就是给定区间[L,R],求A[L]%A[L+1]%A[L+2]%……%A[R]的值,这里有一个技巧,可以发现在b>a时,a%b是无意义的,即结果还是a,因此把取模过程改一下,每一次从当前位置idx向右二分找到第一个值小于A[idx]的数,然后对它取模,然后这样迭代地继续寻找,直到在idx~r中找不到这样的位置即可。对了这题其实最大的意义是让我知道了log类函数常数比较大,一开始交用这个函数超时,不管是log2还是log都这样,因此用位运算模拟来求这个指数k。

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100007;
int arr[N], Min[N][18]; int Scan()
{
int res = 0, ch, flag = 0; if ((ch = getchar()) == '-') //判断正负
flag = 1; else if (ch >= '0' && ch <= '9') //得到完整的数
res = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9' )
res = (res << 3) + (res << 1) + ch - '0'; return flag ? -res : res;
}
void RMQ_init(int l, int r)
{
int i, j;
for (i = l; i <= r; ++i)
Min[i][0] = arr[i];
for (j = 1; l + (1 << j) - 1 <= r; ++j)
for (i = l; i + (1 << j) - 1 <= r; ++i)
Min[i][j] = min(Min[i][j - 1], Min[i + (1 << (j - 1))][j - 1]);
}
inline int ST(int l, int r)
{
int len = r - l + 1;
int k = 0;
while (1 << (k + 1) <= len)
++k;
return min(Min[l][k], Min[r - (1 << k) + 1][k]);
}
int getfirstpos(int key, int l, int r)
{
int L = l, R = r;
int ans = -1;
while (L <= R)
{
int mid = (L + R) >> 1;
if (ST(l, mid) <= key)
{
ans = mid;
R = mid - 1;
}
else
L = mid + 1;
}
return ans;
}
int main(void)
{
int tcase, n, m, l, r, i;
scanf("%d", &tcase);
while (tcase--)
{
scanf("%d", &n);
getchar();
for (i = 1; i <= n; ++i)
arr[i] = Scan();
RMQ_init(1, n);
scanf("%d", &m);
while (m--)
{
scanf("%d%d", &l, &r);
if (l == r)
printf("%d\n", arr[l]);
else
{
int idx = l;
int res = arr[l];
int pos;
while (idx + 1 <= r && ~(pos = getfirstpos(res, idx + 1, r)) && res)
{
res %= arr[pos];
idx = pos;
}
printf("%d\n", res);
}
}
}
return 0;
}

HDU 5875 Function(RMQ-ST+二分)的更多相关文章

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

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

  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 + 二分

    Function Problem Description   The shorter, the simpler. With this problem, you should be convinced ...

  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. 【BZOJ1833】[ZJOI2010] count 数字计数(数位DP)

    点此看题面 大致题意: 求在给定的两个正整数\(a\)和\(b\)中的所有整数中,\(0\sim9\)各出现了多少次. 数位\(DP\) 很显然,这是一道数位\(DP\)题. 我们可以用前缀和的思想, ...

  2. 2018.5.17 oracle函数查询

    --*********函数*********** --1.显示当前日期 select sysdate from dual; --2.显示当前日期,格式为****年月日,别名为hday select t ...

  3. python序列化(数据本地存放持久性存储)和反序列化

    http://blog.csdn.net/uestcyao/article/details/7874817 #读取图片并存储为矩阵 from scipy.misc import imread im = ...

  4. 基于纹理内存的CUDA热传导模拟

    原文链接 项目中有三个,第一个是全局内存,其余两个分别是基于1d和2d纹理内存.项目打包下载. 纹理内存是只读内存,与常量内存相同的是,纹理内存也缓存在芯片中,因此某些情况下,它能减少对内存的请求并提 ...

  5. 博学谷-数据分析pandas

    import pandas as pd df=pd.read_csv() df=pd.read_sql()

  6. es6中的模版字符串

    目录 字符串拼接 includes() startsWith() endsWith() padStart() es6中的模版字符串替代了原有的字符串拼接功能. 字符串拼接 es5方式 传统的字符串拼接 ...

  7. Everything Be True-freecodecamp算法题目

    Everything Be True 1.要求 完善every函数,如果集合(collection)中的所有对象都存在对应的属性(pre),并且属性(pre)对应的值为真.函数返回ture.反之,返回 ...

  8. 关于union的一些问题

    创建一个测试数据表 CREATE TABLE `temp_table` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT ...

  9. Ubuntu12.04下YouCompleteMe安装教程(部分)

    1.通过源码编译安装VIM 开发中使用的是Ubuntu 12.04 LTS,通过sudo apt-get install vim安装的版本较低,不支持YCM,所以,用源码编译并安装最新的Vim. 卸载 ...

  10. Python基础:条件与循环

    条件语句 除了 boolean 类型的数据,条件判断最好是显性的 if i != 0: ... 而不是只写出变量名: if i: ... For循环与While循环 通常来说,如果你只是遍历一个已知的 ...