[+ Tree line number theory + Fenwick tree] seeking the interval Interval GCD greatest common divisor

Interval greatest common divisor

Link to the original question: Interval greatest common divisor

Subject to the effect

And operating segment tree almost give you a l, r let you add d, or ask you the greatest common divisor l, r's

Topic solution to a problem

Never learned elementary number theory suffer a great deal, wrote a morning, after abs sure to add std ::

According Decreases the art we know, \ (gcd (the X-, the y-) = gcd (the X-, the y-- the X-) \) then you can expand the number of cases three \ (gcd (x, y, z) = gcd ( x, yx, zy) \) this is true

Because we can construct a length \ (n-\) a new number of columns b, b is a difference in this sequence. Maintenance interval b with the greatest common divisor sequence segment tree, (and the same conclusions above)

As a result, our query is solved directly gcd (a [l], ask (1, l + 1, r)) (Think about why? Look at the above derivation formula)

Modified, then it is clear that a single point on the b modified, on a need to preserve, you can maintain a tree-like array of values

code show as below

//#define fre yes

#include <cmath>
#include <cstdio>
#include <iostream>
#define int long long

const int N = 500005;
struct Node {
    int l, r;
    long long ans;
} tree[N << 2];
int a[N], b[N], c[N];

long long cnt;

long long gcd(long long x, long long y) {
    return y ? gcd(y, x % y) : x;
}

namespace SegmentTree {
    inline void build(int rt, int l, int r) {
        tree[rt].l = l, tree[rt].r = r;
        if(l == r) {
            tree[rt].ans = b[l];
            return ;
        }
        
        int mid = (l + r) >> 1;
        build(rt * 2, l, mid);
        build(rt * 2 + 1, mid + 1, r);
        tree[rt].ans = gcd(tree[rt * 2].ans, tree[rt * 2 + 1].ans);
    }
    
    inline void change_point(int rt, int x, int k) {
        if(tree[rt].l == tree[rt].r) {
            tree[rt].ans += k;
            return ;
        }
        
        int mid = (tree[rt].l + tree[rt].r) >> 1;
        if(mid >= x) change_point(rt * 2, x, k);
        else change_point(rt * 2 + 1, x, k);
        tree[rt].ans = gcd(tree[rt * 2].ans, tree[rt * 2 + 1].ans);
    }
    
    inline void ask(int rt, int l, int r) {
        if(tree[rt].l >= l && tree[rt].r <= r) {
            cnt = gcd(cnt, tree[rt].ans);
            return ;
        }
        
        int mid = (tree[rt].l + tree[rt].r) >> 1;
        if(l <= mid) ask(rt * 2, l, r);
        if(r > mid) ask(rt * 2 + 1, l, r);
    }
}

int n;
namespace BIT {
    int lowbit(int x) {
        return x & (-x);
    }
    
    inline void add(int x, int k) {
        while(x <= n) {
            c[x] += k;
            x += lowbit(x);
        }
    }
    
    int ask(int x) {
        long long res = 0;
        while(x) {
            res += c[x];
            x -= lowbit(x);
        } return res;
    }
}

signed main() {
    static int m;
    scanf("%lld %lld", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
        b[i] = a[i] - a[i - 1];
    } SegmentTree::build(1, 1, n);
    
    char c[3];
    for (int i = 1; i <= m; i++) {
        scanf("%s", c + 1);
        if(c[1] == 'C') {
            int l, r, d;
            scanf("%lld %lld %lld", &l, &r, &d);
            SegmentTree::change_point(1, l, d);
            BIT::add(l, d);
            if(r + 1 <= n) {
                SegmentTree::change_point(1, r + 1, -d);
                BIT::add(r + 1, -d);
            }
        } else {
            cnt = 0; int l, r;
            scanf("%lld %lld", &l, &r);
            SegmentTree::ask(1, l + 1, r);
            printf("%lld\n", gcd(a[l] + BIT::ask(l), std::abs(cnt)));
        }
    } return 0;
}

Guess you like

Origin www.cnblogs.com/Nicoppa/p/11590181.html