Hdu-5875 Function(树上倍增st算法)
来源:互联网 发布:chrome谷歌浏览器mac 编辑:程序博客网 时间:2024/06/06 03:46
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
题意:给n个数字的数列,m个询问,问F[l,r]是多少,F[l,r] = F[l,r-1] % a[r],当l = r时F[l,r] = a[l].
分析: 注意每个数最多取有效模log(a[i])次就会变成0,所以我们可以把数列中每个元素向它右边第一个小于等于它的元素连边,最后会形成一个森林,每次询问在线在森林上倍增。n*log(n)*log(a[i]).
#include<iostream>#include<string>#include<algorithm>#include<cstdlib>#include<cstdio>#include<set>#include<map>#include<vector>#include<cstring>#include<stack>#include<cmath>#include<queue>#define INF 0x3f3f3f3f#define N 100005using namespace std; typedef long long ll;int T,n,m,fa[N][35],a[N];int Find_pos(int u,int val){int now = log2(n-u+1) + 1;while(now >= 0){if(a[fa[u][now]] > val && a[fa[u][now+1]] <= val) u = fa[u][now];now--;}if(a[u] > val) u = fa[u][0];return u;}int main(){scanf("%d",&T);while(T--){memset(fa,0,sizeof(fa));a[0] = -1;scanf("%d",&n);for(int i = 1;i <= n;i++) scanf("%d",&a[i]);for(int i = n-1;i;i--){fa[i][0] = Find_pos(i+1,a[i]);for(int j = 1;(1<<j) <= n;j++) fa[i][j] = fa[fa[i][j-1]][j-1];}scanf("%d",&m);for(int i = 1;i <= m;i++){int l,r,ans;scanf("%d%d",&l,&r);int now = l+1;ans = a[l];while(now && now <= r && ans){ans %= a[now];now = Find_pos(now,ans);}printf("%d\n",ans);}}}
0 0
- Hdu-5875 Function(树上倍增st算法)
- 【树上倍增算法模板】
- hdu 5875 Function ST+二分
- HDU 5875 Function ST + 二分区间
- hdu 5875 Function (st+二分)
- codeforces 587C(树上的倍增算法)
- LCA之倍增及ST算法
- 树上倍增
- HDU 5875 Function(单调栈+在线倍增法)
- HDU 5875 Function 线段树 || (ST表 + 二分)
- HDU 5875 Function (ST表+二分 or 线段树)
- HDU 4718 The LCIS on the Tree 树上路径倍增
- 【图论,树上路径倍增算法】NOIP2013货车运输
- 树上两点最近公共祖先LCA的倍增算法 poj1986
- 浅谈算法-简单倍增及st表的应用
- cf588e & bzoj3306 树上倍增
- noip2013truck树上路径倍增
- 3306: 树 树上倍增
- Linux下libpcap的安装
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
- FTP数据入库Hive
- light oj 1134
- 单Activity多Fragment模式快速构建一个App
- Hdu-5875 Function(树上倍增st算法)
- LibGdx文档译读(一)Application接口
- 腾讯空间、新浪微博、腾讯微博登录接口的使用
- HDU1269-迷宫城堡
- Maven run as maven install发生错误
- javaScript实现全屏页面滚动效果(优化)
- HTMl5开发前介绍
- 有符号数除以非2的幂(MagicNumber为负)
- Mysql查看版本号的五种方式介绍