2016 ACM/ICPC Asia Regional Dalian Online 1008 Function 二分+RMQ
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2229 Accepted Submission(s): 252
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).
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.
#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的更多相关文章
- 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 ( ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 2016 ACM/ICPC Asia Regional Dalian Online
1009 Sparse Graph(hdu5876) 由于每条边的权值都为1,所以最短路bfs就够了,只是要求转置图的最短路,所以得用两个set来维护,一个用来存储上次扩散还没访问的点,一个用来存储这 ...
- 2016 ACM/ICPC Asia Regional Dalian Online(更新到五道题)
1006 Football Games 这道题输入也很阴险!!! 这道题过题姿势最优雅的,不是if else if else if.那样很容易wa的. 如果没有平手选项, 赢得加一分的话, 可以用La ...
随机推荐
- 安卓ApiDemos最简单的使用方法
http://download.csdn.net/detail/ffwmxr/7401067#comment 正确使用方法:开新工程, 名字API Demos,将下载文件里的 src,res, And ...
- C#string类型总结
字符串的特性:不可变性,每对字符串做拼接或者重新赋值之类的操作,都会在内存中产生一个新的实例. 所以说,在.Net平台下,如果你对一个字符串进行大量的拼接赋值等操作,会产生大量的垃圾. --- ...
- PYTHON map()函数详解
map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 栗子如下↓ 图片来自于网上 def f(x,a): return x+x ...
- PhotoSwipe插件的使用
1.首先引入插件 <link rel="stylesheet" href="css/photoswipe.css"> <link rel=&q ...
- linux之netstat命令
netstat用于显示各种网络相关的信息,如网络连接,路由表接口状态(interface statistics), masquerade连接,多播成员(Multicast Memberships)等等 ...
- Android WebView使用
转自:http://www.cnblogs.com/oakpip/archive/2011/04/08/2009800.html 大部分内容为网上整理其它高人的帖子,现只作整理,用于查看: 在Andr ...
- erlang 健壮性
erlang 提供了简单易用的并发编程模型,基本不需要再考虑多线程并发问题.但实际应用中并不是那么的完美,很多地方需要注意,就算标准库也有不少问题.很多在多线程编程中很多很容易解决的事情,在erlan ...
- python中单引号, 双引号,三引号的差异
1. 单引号和双引号用法都是一样的,但是如果字符串里有相同的字符时要使用\进行转义 举例:1) print 'hello'2) print "hello"1和2,结果都是hello ...
- struts2笔记(2)
<context-param> <param-name>pattern</param-name> <param-value>yyyy-MM-dd hh: ...
- python中date、datetime、string的相互转换
import datetime import time string转datetime str = '2012-11-19' date_time = datetime.datetime.strptim ...