样例输出 1 2 2 求助
查看原帖
样例输出 1 2 2 求助
817524
CR400BFAZ5254楼主2025/7/23 21:06
#include<bits/stdc++.h>
#define mid ((l+r)>>1)
#define lson rt<<1
#define rson ((rt<<1)|1)
#define MAXN 500010
using namespace std;
int a[MAXN];
int seg_sum[MAXN<<2],seg_gcd[MAXN<<2];
int n,q;
void update(int rt){
    seg_sum[rt]=seg_sum[lson]+seg_sum[rson];
    seg_gcd[rt]=__gcd(seg_gcd[lson],seg_gcd[rson]);
}
void build(int l,int r,int rt){
    if(l==r){
        seg_sum[rt]=a[l]-a[l-1];
        seg_gcd[rt]=a[l]-a[l-1];
        return ;
    }
    build(l,mid,lson);
    build(mid+1,r,rson);
    update(rt);
}
//单点修改
void modify(int l,int r,int lp,int val,int rt){
    if(l==r){
        seg_gcd[rt]+=val,seg_sum[rt]+=val;
        return ;
    }
    if(lp<=mid) modify(1,mid,lp,val,lson);
    else modify(mid+1,r,lp,val,rson);
    update(rt);
}
int querysum(int l,int r,int lp,int rp,int rt){
    if(lp<=l && r<=rp) return seg_sum[rt];
    int ans=0;
    if(lp<=mid) ans+=querysum(l,mid,lp,rp,lson);
    if(rp>mid) ans+=querysum(mid+1,r,lp,rp,rson);
    return ans;
}
int querygcd(int l,int r,int lp,int rp,int rt){
    if(lp<=l && r<=rp) return seg_gcd[rt];
    int ans=0;
    if(lp<=mid) ans=__gcd(ans,querygcd(l,mid,lp,rp,lson));
    if(rp>mid) ans=__gcd(ans,querygcd(mid+1,r,lp,rp,rson));
    return ans;
}
int main(){
    cin >> n >> q;
    for(int i=1;i<=n;i++) cin >> a[i];
    build(1,n,1);
    while(q--){
        char op;
        cin >> op;
        if(op=='C'){
            int l,r,d;
            cin >> l >> r >> d;
            //update
            modify(1,n,l,d,1);
            if(r!=n){
                modify(1,n,r+1,-d,1);
            }
        }else{
            int l,r;
            cin >> l >> r;
            // cout query
            cout << abs(__gcd(querygcd(1,n,l+1,r,1),querysum(1,n,1,l,1))) << endl;
        }
    }

    return 0;
}
2025/7/23 21:06
加载中...