• CH 4302 Interval GCD


    辗转相减法的扩展 $gcd(x, y, z) = gcd(x, y - x, z - y)$ 当有n个数时也成立

    所以构造$a_{i}$的差分数组$b_{i} = a_{i} - a_{i - 1}$,用一个线段树来维护b数组的gcd,这样每次区间修改相当于两次单点修改

    考虑到询问的时候$ans = gcd(a_{l}, query(l +1, r))$所以我们再维护原数组a的值,直接差分之后用一个树状数组就好了

    注意判断边界情况。

    Code:

    #include <cstdio>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    
    const int N = 5e5 + 5;
    
    int n, qn;
    ll a[N], b[N];
    
    ll gcd(ll x, ll y) {
        return (!y) ? x : gcd(y, x % y);
    }
    
    template <typename T>
    inline void read(T &X) {
        X = 0;
        char ch = 0;
        T op = 1;
        for(; ch > '9' || ch < '0'; ch = getchar())
            if(ch == '-') op = -1;
        for(; ch >= '0' && ch <= '9'; ch = getchar())
            X = (X << 3) + (X << 1) + ch - 48;
        X *= op;
    }
    
    struct BinaryIndexTree {
        ll s[N];
        
        #define lowbit(x) ((x) & (-x))
        
        inline void add(int x, ll v) {
            for(; x <= n; x += lowbit(x))
                s[x] += v;
        } 
        
        inline ll query(int x) {
            ll res = 0;
            for(; x > 0; x -= lowbit(x))
                res += s[x];
            return res;
        }
        
    } B;
    
    struct SegT {
        ll s[N << 2];
        
        #define lc p << 1
        #define rc p << 1 | 1
        #define mid ((l + r) >> 1)
        
        inline void up(int p) {
            if(p) s[p] = gcd(s[lc], s[rc]);
        }
        
        void build(int p, int l, int r) {
            if(l == r) {
                s[p] = b[l];
                return;
            }
            
            build(lc, l, mid);
            build(rc, mid + 1, r);
            up(p);
        }
        
        void modify(int p, int l, int r, int x, ll v) {
            if(x == l && x == r) {
                s[p] += v;
                return;
            }
            
            if(x <= mid) modify(lc, l, mid, x, v);
            else modify(rc, mid + 1, r, x, v);
            up(p);
        }
        
        ll query(int p, int l, int r, int x, int y) {
            if(x > y) return 1LL;
            if(x <= l && y >= r) return s[p];
            ll res;
            if(x <= mid && y > mid) 
                res = gcd(query(lc, l, mid, x, y), query(rc, mid + 1, r, x, y)); 
            else if(y <= mid) res = query(lc, l, mid, x, y);
            else if(x > mid) res = query(rc, mid + 1, r, x, y);
            return res;
        }
    
    } A; 
    
    inline ll abs(ll x) {
        return x > 0 ? x : -x;
    }
    
    int main() {
        read(n), read(qn);
        for(int i = 1; i <= n; i++) read(a[i]);
        
        for(int i = 1; i <= n; i++) b[i] = a[i] - a[i - 1];
        for(int i = 1; i <= n; i++) B.add(i, b[i]);
        A.build(1, 1, n);
        
        char op[3];
        for(int x, y; qn--; ) {
            scanf("%s", op);
            read(x), read(y);
            if(op[0] == 'C') {
                ll v;
                read(v);
                B.add(x, v), A.modify(1, 1, n, x, v);
                if(y < n) B.add(y + 1, -v), A.modify(1, 1, n, y + 1, -v);
            } else {
                if(x < y) printf("%lld
    ", gcd(B.query(x), abs(A.query(1, 1, n, x + 1, y))));
                else printf("%lld
    ", B.query(x));
            }        
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    编译安装dropbear
    ssh的相关实验
    通过ssh协议实现用户key认证登录
    cobbler实现自动安装
    Centos 6 PXE安装
    Centos 7 PXE一键安装
    openssl命令
    实现CA证书创建及客户端申请证书
    android 动画
    BottomSheetBehavior 结合CoordinatorLayout实现底部栏
  • 原文地址:https://www.cnblogs.com/CzxingcHen/p/9480012.html
Copyright © 2020-2023  润新知