hdu 5875 2016 ACM/ICPC Asia Regional Dalian Online 1008
2016-09-10 19:37
441 查看
Function
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1427 Accepted Submission(s): 126
[align=left]Problem Description[/align]
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).
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each query(l,r),
output F(l,r)
on one line.
[align=left]Sample Input[/align]
1
3
2 3 3
1
1 3
[align=left]Sample Output[/align]
2
题意:
给出m个区间 (L,R),求出从L处的数左往右取模至R的答案;
思路:
用优先队列求出一个数向右的第一个比他小的值,在求答案的时候就可以直接跳转到比他小的值。
因为在和它取模之后,再和比它大的值取模是没有意义的。
代码:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn =100005; int data[maxn]; int flag[maxn]; priority_queue<int >q; int main(){ int T; scanf("%d",&T); while(T--){ int n; int i,j; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&data[i]); q.push(1); for(i=2;i<=n;i++){ while(!q.empty()&&data[i]<data[q.top()]){ flag[q.top()]=i; q.pop(); } q.push(i); } while(!q.empty()){ flag[q.top()]=-1; q.pop(); } int m; scanf("%d",&m); for(i=1;i<=m;i++){ int l,r; scanf("%d%d",&l,&r); int fn=l; int ans=data[l]; while(fn<=r){ int wtf=flag[fn]; if(wtf==-1) break; else if(wtf>r) break; else if(wtf<=r){ ans=ans%data[wtf]; fn=wtf; } if(ans==1) break; } printf("%d\n",ans); } } return 0; }
相关文章推荐
- hdu 5875 2016 ACM/ICPC Asia Regional Dalian Online 1008
- hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)
- HDU 5877 Weak Pair(2016 ACM/ICPC Asia Regional Dalian Online)
- HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
- HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
- 补图+BFS( 邻接链表 )——HDU 5876 ( 2016 ACM/ICPC Asia Regional Dalian Online 1009 )
- HDU 5876 Sparse Graph(bfs求解补图中的单源最短路)——2016 ACM/ICPC Asia Regional Dalian Online
- 【2017 ACM/ICPC Asia Regional Qingdao Online 1008】hdu 6213 Chinese Zodiac
- HDU 5873 2016 ACM/ICPC Asia Regional Dalian Online 1006 Football Games
- 2016 ACM/ICPC Asia Regional Dalian Online 1006 football game 1007 hdu 5873 hdu 5874
- HDU 5876 Sparse Graph (补图BFS) 2016 ACM/ICPC Asia Regional Dalian Online
- Hdu 5873 2016 ACM/ICPC Asia Regional Dalian Online 1006(兰道定理)
- HDU-2017 ACM/ICPC Asia Regional Shenyang Online-1008-transaction transaction transaction
- HDU 5874 2016 ACM/ICPC Asia Regional Dalian Online 1007 Friends and Enemies
- HDU 5875 Function 2016 ACM/ICPC Asia Regional Dalian Online
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
- HDU 5876 Sparse Graph(2016 ACM/ICPC Asia Regional Dalian Online)
- HDU 5876 2016 ACM/ICPC Asia Regional Dalian Online BFS+set
- HDU 5877 2016 ACM/ICPC Asia Regional Dalian Online 1010 Weak Pair
- HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)