Hdu 5875 Function 线段树
来源:互联网 发布:新浪微盾网络异常 编辑:程序博客网 时间:2024/06/05 11:42
Function
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3113 Accepted Submission(s): 1035
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
Source
2016 ACM/ICPC Asia Regional Dalian Online
多次查询,求a[l]%a[l+1]%...%a[r].
每次求余,若当前数比被除数大,则最多为原数一半,所以每次查询,有效的运算最多log(a[l])次。
如此,只要用线段树求区间内第一个不大于当前数的数的位置。
不断暴力查询,更新答案即可。
#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=100005,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L);int a[maxn];int num;struct Tree{int l,r,lc,rc,min;};Tree tree[maxn*4];void build(int now,int l,int r) {tree[now].l=l;tree[now].r=r;if (l!=r) {num++;tree[now].lc=num;build(num,l,(l+r)/2);num++;tree[now].rc=num;build(num,(l+r)/2+1,r);tree[now].min=min(tree[tree[now].lc].min,tree[tree[now].rc].min);} else tree[now].min=a[l];}int findmin (int now,int l,int r,int val) {//cout << now << ' ' << tree[now].l << ' ' << tree[now].r << ' ' << tree[now].max << ' ' << tree[now].tag << endl;if (tree[now].l==tree[now].r) {return tree[now].l;} else {int f;if (l<=(tree[now].l+tree[now].r)/2&&tree[tree[now].lc].min<=val) {f=findmin(tree[now].lc,l,r,val);if (f!=-1) return f;} if (r>(tree[now].l+tree[now].r)/2&&tree[tree[now].rc].min<=val) {f=findmin(tree[now].rc,l,r,val);if (f!=-1) return f;}return -1;}//cout << now << ' ' << tree[now].l << ' ' << tree[now].r << ' ' << tree[now].max << ' ' << tree[now].tag << endl;}int main() {int cas;scanf("%d",&cas);while (cas--) {int n,m,i,j,l,r;scanf("%d",&n);for (i=1;i<=n;i++) scanf("%d",&a[i]);num=1;build(1,1,n);scanf("%d",&m);for (i=1;i<=m;i++) {scanf("%d%d",&l,&r);int ans=a[l];l++;while (l<=r) {int p=findmin(1,l,r,ans);if (p==-1) break;ans%=a[p];l=p+1;}printf("%d\n",ans);}}return 0;}
阅读全文
0 0
- Hdu 5875 Function 线段树
- HDU 5875 Function(简单线段树)
- hdu 5875 Function 线段树||RMQ
- HDU 5875 Function(预处理(线段树))
- HDU 5875 Function 大连网络赛 线段树
- HDU 5875 Function (取模+分治+线段树)
- HDU 5875 Function 线段树 || (ST表 + 二分)
- HDU 5875 Function (ST表+二分 or 线段树)
- hdu 5875 Function -线段树+取余 -ICPC网络赛大连赛区
- HDU 5875 Function
- hdu 5875 Function(multiset)
- HDU 5875 Function
- HDU 5875 Function
- HDU-5875-Function
- HDU 5875 - Function
- HDU 5875 Function
- HDU 5875 Function
- hdu 5875 Function
- HIVE学习笔记:HIVE内置的三种表格式与表的划分
- 【linux】信号量
- mybatis错误:Invalid bound statement (not found)
- 51Nod-1371-填数字
- JDBC编程-优化程序(六)
- Hdu 5875 Function 线段树
- 建立J2EE开发环境
- 字符串trim()
- [ElasticStack系列]docker部署logstash
- OJ 2552: 好好学习天天向上
- hdu 1024 最大上升子序列
- java的properties文件-jdbc优化编程(五)
- IO流
- Android之获取移动网络ip