https://blog.csdn.net/qq_40831340/article/details/91404694
此题博客进阶版本

Different GCD Subarray Query HDU - 5869

问你这个区间有多少不同GCD
GCD 最多也就log个 对于每个数据 我们分别存放 从1到它 可以有什么GCD
之后 就是HH项链了 我们把处理出GCD往里面扔存最后一个

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e5 + 5;
const int maxm = 1e6 + 5;
int n, m;
vector<pii> v[maxn];
int vis[maxm], ans[maxn];
int c[maxn];

void add(int x, int y) {
    for(; x <= n; x += x & (-x))
        c[x] += y;
}

int ask(int x) {
    int ret = 0;
    for(; x; x -= x & (-x))
        ret += c[x];
    return ret;
}

int rangeask(int l, int r) {
    return ask(r) - ask(l - 1);
}

struct node {
    int l, r, id;
    bool operator < (const node &a) const {
        return r < a.r;
    }
} q[maxn];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin >> n >> m) {
        memset(vis, 0, sizeof(vis));
        for(int i = 1, x; i <= n; i ++) {
            v[i].clear();
            c[i] = 0;
            cin >> x;
            int tmp = x;
            v[i].push_back(pii(i, x));
            for(int j = 0; j < v[i - 1].size(); j ++) {
                int gcd = __gcd(x, v[i - 1][j].second);
                int pos = v[i - 1][j].first;
                if(tmp != gcd) {
                    v[i].push_back(pii(pos, gcd));
                    tmp = gcd;
                }
            }
        }
        for(int i = 1; i <= m; i ++) {
            cin >> q[i].l >> q[i].r;
            q[i].id = i;
        }
        sort(q + 1, q + 1 + m);
        int pos = 1;
        for(int i = 1; i <= m; i ++) {
            while(pos <= q[i].r) {
                for(int j = 0; j < v[pos].size(); j ++) {
                    int gcd = v[pos][j].second;
                    int p = v[pos][j].first;
                    if(vis[gcd])
                        add(vis[gcd], -1);
                    add(p, 1);
                    vis[gcd] = p;
                }
                pos ++;
            }
            ans[q[i].id] = rangeask(q[i].l, q[i].r);
        }
        for(int i = 1; i <= m; i ++)
            cout << ans[i] << endl;
    }
    return 0;
}

No Pain No Game HDU - 4630

道理同上 这里找的最大公共GCD 只要出现2次GCD 我们线段树 离线维护最大值就好
ps md 要注意l == r 的情况 最后更新的地方绝对不再r位置 所以直接是本身 输出0 也过了
不是等于0 卧槽

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e5 + 5;
const int maxm = 1e6 + 5;
int n, m, cas;
struct node {
    int l, r, id;
    bool operator < (const node &a) const {
        return r < a.r;
    }
} q[maxn];
int a[maxn];

vector<int> v[maxn];
int vis[maxn], ans[maxn];
int tree[maxn << 2];

int push_up(int rt) {
    tree[rt] = max(tree[rt << 1], tree[rt << 1 | 1]);
}

void build(int l, int r, int rt) {
    tree[rt] = 0;
    if(l == r)
        return ;
    int mid = l + r >> 1;
    build(l, mid, rt << 1), build(mid + 1, r, rt << 1 | 1);
}

void updata(int L, int l, int r, int rt, int val) {
    if(l == r) {
        tree[rt] = max(tree[rt], val);
        return ;
    }
    int mid = l + r >> 1;
    if(L <= mid)
        updata(L, l, mid, rt << 1, val);
    else
        updata(L, mid + 1, r, rt << 1 | 1, val);
    push_up(rt);
}

int query(int L, int R, int l, int r, int rt) {
    if(L <= l && r <= R)
        return tree[rt];
    int mid = l + r >> 1;
    int ret = 0;
    if(L <= mid)
        ret = max(ret, query(L, R, l, mid, rt << 1));
    if(R > mid)
        ret = max(ret, query(L, R, mid + 1, r, rt << 1 | 1));
    return ret;
}

void init() {
    for(int i = 1; i <= 50000; i ++) {
        for(int j = i; j <= 50000; j += i)
            v[j].push_back(i);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    init();
    cin >> cas;
    for(int tt = 1; tt <= cas; tt ++) {
        cin >> n;
        build(1, n, 1);
        memset(vis, 0, sizeof vis);
        for(int i = 1; i <= n; i ++)
            cin >> a[i];
        cin >> m;
        for(int i = 1; i <= m; i ++) {
            cin >> q[i].l >> q[i].r;
            q[i].id = i;
        }
        sort(q + 1, q + 1 + m);
        int pos = 1;
        for(int i = 1; i <= m; i ++) {
            while(pos <= q[i].r) {
                for(int j = 0; j < v[a[pos]].size(); j ++) {
                    int y = v[a[pos]][j];
                    if(vis[y])
                        updata(vis[y], 1, n, 1, y);
                    vis[y] = pos;
                }
                pos ++;
            }
            if(q[i].l == q[i].r)
                ans[q[i].id] = a[q[i].l];

              ans[q[i].id] = query(q[i].l, q[i].r, 1, n, 1);
        }
        for(int i = 1; i <= m; ++i)
            cout << ans[i] << endl;
    }
    return 0;
}