hdu 5726(区间gcd)

#include <bits/stdc++.h>

#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;

#define LL long long
#define pii pair<int,int>
#define MP make_pair
#define ls i << 1
#define rs ls | 1
#define md (ll + rr >> 1)
#define lson ll, md, ls
#define rson md + 1, rr, rs
#define Pi acos(-1.0)
#define mod 1000000007
#define eps 1e-10
#define inf 0x3f3f3f3f
#define N 100010
#define M 800020

struct node{
    int l, r, d;
    node(int l = 0, int r = 0, int d = 0) : l(l), r(r), d(d) {}
    bool operator < (const node &b) const {
        return r < b.r || r == b.r && l < b.l;
    }
};
int n, m, cnt, a[N];
int san[N*30], val[N], nxt[N];
LL c[N*30];
vector<node> vt[N];
int gcd(int x, int y){
    return y == 0 ? x : gcd(y, x % y);
}
int haxi(int val){
    return lower_bound(san + 1, san + 1 + cnt, val) - san;
}
int main(){
    int cas, kk = 0;
    scanf("%d", &cas);
    while(cas--){
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i){
            scanf("%d", &a[i]);
            vt[i].clear();
        }
        cnt = 0;
        nxt[1] = 1, val[1] = a[1];
        san[++cnt] = a[1];
        vt[1].push_back(node(1, 1, val[1]));
        for(int i = 2; i <= n; ++i){
            nxt[i] = i, val[i] = a[i];
            int j = i;
            while(j > 0){
                val[j] = gcd(val[j], a[i]);
                while(nxt[j] > 1 && gcd(val[nxt[j]-1], val[j]) == val[j])
                    nxt[j] = nxt[nxt[j]-1];
                san[++cnt] = val[j];
                vt[i].push_back(node(nxt[j], j, val[j]));
                j = nxt[j] - 1;
            }
        }
        sort(san + 1, san + 1 + cnt);
        cnt = unique(san + 1, san + 1 + cnt) - san - 1;
        for(int i = 1; i <= cnt; ++i) c[i] = 0;
        for(int i = 1; i <= n; ++i){
            sort(vt[i].begin(), vt[i].end());
            for(int j = 0; j < vt[i].size(); ++j){
                int v = haxi(vt[i][j].d);
                c[v] += vt[i][j].r - vt[i][j].l + 1;
            }
        }
        printf("Case #%d:\n", ++kk);
        scanf("%d", &m);
        while(m--){
            int l, r;
            scanf("%d%d", &l, &r);
            int k = lower_bound(vt[r].begin(), vt[r].end(), node(-1, l, -1)) - vt[r].begin();
            printf("%d ", vt[r][k].d);
            int v = haxi(vt[r][k].d);
            printf("%lld\n", c[v]);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值