HDU5875 - Function
Posted rrrr-wys
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5875 - Function相关的知识,希望对你有一定的参考价值。
HDU5875 - Function
做法:st表+二分的经典题。不能使用数学函数log,否则会tle,需要预处理
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define MP make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr first
#define sc second
typedef long long ll;
const int N= 2e5 + 7;
using namespace std;
int n,m;
ll a[N], st[N][50],Log[N];
void init() {
Log[1] = 0;
for(int i = 2; i <= n; ++i) Log[i] = Log[i>>1] + 1;
for(int i = 0; i <= n; ++i) st[i][0] = a[i];
for(int j = 1; (1<<j) <= n; ++j){
for(int i = 1; i+(1<<j)-1 <= n; ++i) {
st[i][j] = min(st[i][j-1], st[i+(1<<(j-1))][j-1]);
}
}
}
inline ll ask(int u,int v) {
int k = Log[v-u+1];
return min(st[u][k], st[v-(1<<k)+1][k]);
}
int fd(int l,int r,ll x) {
int s = r + 1;
while(l <= r) {
int mid = (l+r)>>1;
if(ask(l,mid) <= x) r = mid - 1, s = mid;
else l = mid + 1;
}
return s;
}
ll solve(int l,int r) {
ll now = st[l][0];
if(l == r) return now;
l++;
while(l<=r && now){
l = fd(l,r,now);
if(l <= r) {
now %= st[l][0]; ++l;
}
}
return now;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
rep(i,1,n) scanf("%lld",&a[i]);
init();
scanf("%d",&m);
rep(i,1,m) {
int l,r;
scanf("%d%d",&l,&r);
printf("%lld
",solve(l,r));
}
}
return 0;
}
以上是关于HDU5875 - Function的主要内容,如果未能解决你的问题,请参考以下文章
Function---hdu5875(大连网选,区间连续求余)