hdu 5875 Function 2016ACM/ICPC大连赛区网络赛1008
来源:互联网 发布:怎么进入服务器数据库 编辑:程序博客网 时间:2024/04/29 14:09
Problem Description
The shorter, the simpler. With this problem, you should be convinced of this truth.
You are given an arrayA 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 calculateF(l,r) , for each query (l,r) .
You are given an array
You job is to calculate
Input
There are multiple test cases.
The first line of input contains a integerT , indicating number of test cases, and T test cases follow.
For each test case, the first line contains an integerN(1≤N≤100000) .
The second line containsN space-separated positive integers: A1,…,AN (0≤Ai≤109) .
The third line contains an integerM denoting the number of queries.
The followingM lines each contain two integers l,r (1≤l≤r≤N) , representing a query.
The first line of input contains a integer
For each test case, the first line contains an integer
The second line contains
The third line contains an integer
The following
Output
For each query(l,r) , output F(l,r) on one line.
Sample Input
132 3 311 3
Sample Output
2
首先有一个性质,一个数A不断地做取模操作,最多经过logA次就会变为0
这样我们就可以每次查找当前位置下一个不大于它的数的位置然后取模继续查找
用线段树实现的话就是先找左子数,左子树没有再找右子树就可以了
#include<map>#include<cmath>#include<queue>#include<vector>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct tree{ int l,r; int mi,x;}tr[800001];int a[100001];inline void build(int p,int l,int r){ tr[p].l=l; tr[p].r=r; if(l!=r) { int mid=(l+r)/2; build(p*2,l,mid); build(p*2+1,mid+1,r); tr[p].mi=min(tr[p*2].mi,tr[p*2+1].mi); if(tr[p].mi==tr[p*2].mi) tr[p].x=tr[p*2].x; else tr[p].x=tr[p*2+1].x; } else { tr[p].mi=a[l]; tr[p].x=l;}}tree nw;inline tree ask(int p,int l,int r,int x){if(tr[p].mi>x)return nw; if(tr[p].l==tr[p].r) {//printf("%d %d %d\n",tr[p].l,tr[p].r,tr[p].mi); return tr[p]; } else { int mid=(tr[p].l+tr[p].r)/2; tree ans1=nw,ans2=nw; if(l<=mid&&tr[p*2].mi<=x) ans1=ask(p*2,l,r,x); if(ans1.mi!=0) return ans1; if(r>mid&&tr[p*2+1].mi<=x) return ask(p*2+1,l,r,x); return nw; }}inline int cal(int l,int r){ int d=a[l]; while(l<r) { tree xt=ask(1,l+1,r,d); if(xt.mi==0) return d; l=xt.x; d%=xt.mi; } return d;}int main(){//freopen("1008.in","r",stdin);//freopen("1008.out","w",stdout); int T; while(scanf("%d",&T)!=EOF) { while(T>0) { T--; int n; scanf("%d",&n); int i; for(i=1;i<=n;i++) scanf("%d",&a[i]); build(1,1,n); int m; scanf("%d",&m); int l,r; for(i=1;i<=m;i++) { scanf("%d%d",&l,&r); printf("%d\n",cal(l,r)); } } } return 0;}
0 0
- hdu 5875 Function 2016ACM/ICPC大连赛区网络赛1008
- hdu 5875 Function -线段树+取余 -ICPC网络赛大连赛区
- hdu 5869 Different GCD Subarray Query 2016ACM/ICPC大连赛区网络赛1002
- hdu 5869 Different GCD Subarray Query 2016ACM/ICPC大连赛区网络赛1006
- 2016 ICPC 大连网络赛 HDU 5875 Function
- 2016ACM-ICPC大连网络预选赛1008 Function
- HDU 4004 - The Frog\'s Games 第36届ACM/ICPC亚洲大连赛区网络赛
- 【2016-大连赛区网络赛-H】优化(Function,hdu 5875)
- 【icpc网络赛大连赛区】Sparse Graph
- hdu 4001 (2011ACM/ICPC大连网络赛)
- 2012 ACM/ICPC 成都赛区网络赛 HDU 4296
- 2016 大连网络赛 hdu 5876 ACM ICPC(补图求最短路)
- HDU 5875 Function 大连网络赛
- HDU 5875 Function 2016亚洲区域赛大连网络赛
- hdu5876 补图求最短路 2016ACM ICPC 大连网络赛
- hdu 5878 I Count Two Three 2016ACM/ICPC青岛赛区网络赛1001
- hdu 5879 Cure 2016ACM/ICPC青岛赛区网络赛1002
- hdu 5881 Tea 2016ACM/ICPC青岛赛区网络赛1004
- 顶部导航栏可点击可滑动,仿微信仿豆瓣
- 第3周项目2--建设“顺序表”算法库
- Cocos Creator 自定义事件
- springIoC概述
- 17 ContentProvider
- hdu 5875 Function 2016ACM/ICPC大连赛区网络赛1008
- MVC4使用微软企业库 dataset foreach遍历 转换成list<T>
- 演示TreeSet
- ASP.NET三种开发模式
- Spring中applicationContext.xml的bean里的id和name属性区别
- cocos2d-js打包APK出现的问题
- scala筑基篇-01-List操作
- scala高阶函数
- 0 1 0 * * ?