线段树 求调
查看原帖
线段树 求调
637788
kimi0705楼主2023/8/11 11:28
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, q, x, y, k, cnt;
int a[N];
struct node {
    int l, r, Gcd;
} tree[N << 2];
void pushup(int o) { tree[o].Gcd = __gcd(tree[o * 2].Gcd, tree[o * 2 + 1].Gcd); }
void build(int o, int l, int r) {
    if (l == r) {
        tree[o].Gcd = a[l];
        return;
    }
    int m = (l + r) >> 1;
    build(o * 2, l, m);
    build(o * 2 + 1, m + 1, r);
    pushup(o);
}
void query(int o, int x, int y, int k) {
    if (cnt > 1) return;
    if (tree[o].l == tree[o].r) {
        cnt++;
        return;
    }
    int m = (tree[o].l + tree[o].r) >> 1;
    if (x <= m && tree[o << 1].Gcd % k) query(o * 2, x, y, k);
    if (y > m && tree[1 + (o << 1)].Gcd % k) query((o << 1) + 1, x, y, k);
}
void update(int o, int x, int k) {
    if (tree[o].l == tree[o].r) {
        tree[o].Gcd = k;
        return;
    }
    int m = (tree[o].l + tree[o].r) >> 1;
    if (x <= m)
        update(o * 2, x, k);
    else
        update(o * 2 + 1, x, k);
    pushup(o);
}
signed main() {
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    build(1, 1, n);
    cin >> q;
    for (int i = 1; i <= q; i++) {
        int op;
        cin >> op;
        if (op == 1) {
            cin >> x >> y >> k;
            cnt = 0;
            query(1, x, y, k);
            if (cnt > 1)
                cout << "NO" << endl;
            else
                cout << "YES" << endl;
        } else {
            cin >> x >> k;
            update(1, x, k);
        }
    }
    return 0;
}

2023/8/11 11:28
加载中...