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. IIS6.0开启gzip压缩

    双击IIS服务器,右键点击网站,点击属性,然后点击服务,我们看到HTTP压缩,然后在压缩应用程序文件,压缩静态文件中打钩,然后点击确定,第一步就完成了   然后我们右键点击web服务扩展,点击添加一个 ...

  2. 解决ssh登录慢,等待时间长的问题

    有时候在ssh远程登录到其他主机上时发现登录时间太长,经过亲自测试,发现主要有两个问题会导致ssh登录慢: 1.使用了dns反查,这样的话当ssh某个IP时,系统会试图通过DNS反查相对应的域名,如果 ...

  3. Java读取各种文件格式内容

    所需的jar包哦也不要太记得了,大家可以搜搜,直接上代码: import java.io.BufferedInputStream; import java.io.File; import java.i ...

  4. 【前端_js】解决ajax跨域请求数据

    1.ajax发送请求必须遵循同源策略,即请求方和相应方的协议头.域名.端口全部一样.只要三者有一个不一样都视为跨域,浏览器出于安全考虑不允许跨域访问. 解决ajax跨域访问的常用方法: a.使用jso ...

  5. 三 python并发编程之多线程-理论

    一 什么是线程 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 线程顾名思义,就是一条流水线工作的过程,一条流水线必须属于一个车间,一个车间的工作过程是一个进程 车间负责把资源整合 ...

  6. OpenFaceswap 入门教程(2):软件使用篇!

    安装完OpenFaceswap之后,是不是就迫不及待的想要“见证奇迹”了呢? 都说磨刀不误砍柴工.开始之前请先做一个准备.然后大致了解一下换脸的过程 换脸基本步骤是: 把视频切成很多图片 把图片中的人 ...

  7. linux下mysql的权限设计总结

    1,进入mysql,终端中输入 mysql -u 用户名 -p   .enter键后,提示输入密码. 2,执行grant all privileges on xxxdb.* to usertest@& ...

  8. Python知识点进阶——细节问题

    int()强制转换浮点数 在int()的强制转换浮点数时候,不管是正数还是负数,只取整数部分. 注意:这里不是向上或者向下取整,也不是四舍五入. 无限递归 递归是为了将问题简化为更小规模的同类型问题, ...

  9. 闯越自动签到demo版补充说明

    demo代码:https://www.cnblogs.com/canmeng/p/11000548.html 定位出错是由于cookie 我重新登录账号过,cookies的值就变了 当时没注意cook ...

  10. Volatile小结

    1)Java 中能创建 Volatile 数组吗? 能,Java 中可以创建 volatile 类型数组,不过只是一个指向数组的引用,而不是整个数组.我的意思是,如果改变引用指向的数组,将会受到 vo ...