HDU 5875 Function

来源:互联网 发布:雪梨网红淘宝店链接 编辑:程序博客网 时间:2024/05/16 06:55

题意:给一个数组,然后m个查询,查询[L, R]中,f = a[L] % a[L + 1] ..... % a[R],f的值

思路:因为a % b = a,当a < b时,所以只需使用线段树维护区间最小值,然后跳过被模数大于要模的数的区间

#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<string>#include<stack>#include<vector>#include<queue>#include<set>#include<algorithm>#include<iostream>typedef long long ll;const int maxn = 1e6 + 10;const double eps = 1e-7;const ll INF = 1e18;using namespace std;int tree[4 * maxn], a[maxn];int ql, qr, ind;int T, m, q, n;int ans;void update(int node, int l, int r) {    if(l == r) { tree[node] = m; return ; }    int mid = (l + r) >> 1;    if(ind <= mid) update(node * 2, l, mid);    else update(node * 2 + 1, mid + 1, r);    tree[node] = min(tree[node * 2], tree[node * 2 + 1]);}void query(int node, int l, int r) {    if(ql > qr) return ;    if(tree[node] > ans) return ;    if(l == r) { ans %= tree[node]; return ; }    int mid = (l + r) >> 1;    if(ql <= mid) query(node * 2, l, mid);    if(qr > mid) query(node * 2 + 1, mid + 1, r);}int main(){    scanf("%d", &T);    while(T--) {        scanf("%d", &n);        for(ind = 1; ind <= n; ind++) {            scanf("%d", &m);            update(1, 1, n);            a[ind] = m;        }        scanf("%d", &q);        while(q--) {            scanf("%d %d", &ql, &qr);            ans = a[ql++];            query(1, 1, n);            printf("%d\n", ans);        }    }    return 0;}


0 0
原创粉丝点击