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

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个数的序列,以及m个查询,每次查询一个区间[l,r],求a[l]%a[l+1]%a[l+2]...%a[r]
思路:易知,比当前大的数取模没意义,实际取模的次数是log次(取模至少会减少一半),那么问题就转化为如何在所查询的区间跳着来取模,每次只对比当前小的数取模
iky:::设ans为当前的值,上一次取模的位置为p,那么我们只需要在p的右边找到一个数比ans小的第一个数,ans对那么数取模,p跳到那么位置即可。最多跳log次,每次二分一个log,复杂度就是nloglog
 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int N = 2e5 + ;
int n, m;
int a[N], mm[N], mi[N][]; void initRMQ(int n, int b[]) {
mm[] = -;
for(int i = ; i <= n; ++i) {
mm[i] = ((i & (i - )) == ) ? mm[i - ] + : mm[i - ];
mi[i][] = b[i];
}
for(int j = ; j <= mm[n]; ++j)
for(int i = ; i + ( << j) - <= n; ++i)
mi[i][j] = min(mi[i][j - ], mi[i + ( << (j-))][j - ]);
}
int rmq(int x, int y) {
if(x > y) return 0x3f3f3f3f;
int k = mm[y - x + ];
return min(mi[x][k], mi[y - ( << k) + ][k]);
}
int calc(int l, int r) {
int p = l, ans = a[l];
while(p < r) {
int L = , R = r - p + , tmp = R;
while(L < R) {
int M = (L + R) >> ;
if(rmq(p + , p + M) <= ans) R = M;
else L = M + ;
} if(L == tmp) return ans%a[r];
p = p + L; //cout << p << endl;
ans %= a[p];
}
return ans%a[r];
}
void solve() {
scanf("%d", &n);
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
int l, r;
initRMQ(n, a);
scanf("%d", &m);
for(int i = ; i <= m; ++i) {
scanf("%d%d", &l, &r);
if(l == r) printf("%d\n", a[l]);
else printf("%d\n", calc(l, r));
}
}
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int cas;
while(~scanf("%d", &cas)) {
while(cas --) {
solve();
}
}
return ;
}

2016 ACM/ICPC Asia Regional Dalian Online 1008 Function 二分+RMQ的更多相关文章

  1. 2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  2. 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  3. HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  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 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  7. hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

    Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  8. 2016 ACM/ICPC Asia Regional Dalian Online

    1009 Sparse Graph(hdu5876) 由于每条边的权值都为1,所以最短路bfs就够了,只是要求转置图的最短路,所以得用两个set来维护,一个用来存储上次扩散还没访问的点,一个用来存储这 ...

  9. 2016 ACM/ICPC Asia Regional Dalian Online(更新到五道题)

    1006 Football Games 这道题输入也很阴险!!! 这道题过题姿势最优雅的,不是if else if else if.那样很容易wa的. 如果没有平手选项, 赢得加一分的话, 可以用La ...

随机推荐

  1. 网络第二节——AFNworking

    /** 要使用常规的AFN网络访问 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; ...

  2. 前端工具HBuilder安装Sass插件

    HBuilder自带未安装的less插件,但是sass插件却没有,需要自己安装,步骤如下: 1.安装sass,参考: http://www.w3cplus.com/sassguide/install. ...

  3. jQuery中的选择器

    (1)基本#id element .class * selector1,selector2,selectorN (2)层次选择器:ancestor descendant parent > chi ...

  4. yii2图片处理扩展yii2-imagine的使用

    示例控制器: <?php /** * 图片常用处理 * * 需要 yii/yii2-imagine 的支持 * php composer.phar require --prefer-dist y ...

  5. Ubuntu14.04安装配置web/ftp/tftp/dns服务器

    目录: 1.安装ftp服务器vsftpd --基于tcp,需要帐号密码 2.安装tftp服务器tftpd-hpa,tftp-hpa --udp 3.web服务器--使用Apache2+Mysql+PH ...

  6. rabbitmq

    send端 import pika credentials = pika.PlainCredentials(') connection = pika.BlockingConnection(pika.C ...

  7. Linux下的压缩和解压缩命令——zip/unzip

    zip命令 zip是个使用广泛的压缩程序,文件经它压缩后会另外产生具有".zip"扩展名 的压缩文件. 选项: -A   调整可执行的自动解压缩文件. -b<工作目录> ...

  8. C#夯实基础之多线程二:主线程、前台线程与后台线程

    我们在<C#夯实基础之多线程一:初识多线程>一文中第二部分中指出,既然windows最终发展出了多线程模型,按理说,我们直接使用一个.NetFramework的线程类就可以直接撸代码了,但 ...

  9. erlang 虚机CPU 占用高排查

    -问题起因 近期线上一组服务中,个别节点服务器CPU使用率很低,只有其他1/4.排除业务不均,曾怀疑是系统top统计错误,从Erlang调度器的利用率调查 找到通过erlang:statistics( ...

  10. 转:IOC框架

    CSND上看了王泽滨的博客关于IOC的,觉得说的很透彻,地址为:http://blog.csdn.net/wanghao72214/article/details/3969594 1 IoC理论的背景 ...