求助站外线段树题目
查看原帖
求助站外线段树题目
363006
wangyibo201026楼主2022/2/19 14:36

网址:https://www.acwing.com/problem/content/description/247/

代码:

#include<bits/stdc++.h>

using namespace std;

const int N = 5e5 + 5;

int n, m, a[N], diff[N];
int d[N * 4];

void push_up(int node){
    d[node] = __gcd(d[node << 1], d[(node << 1) + 1]);
}

void build(int l, int r, int node){
    if(l == r){
        d[node] = diff[l];
        return ;
    }
    int mid = l + r >> 1;
    build(l, mid, node << 1);
    build(mid + 1, r, (node << 1) + 1);
    push_up(node);
}

void update(int s, int t, int x, int k, int node){
    if(s == t){
        d[node] += k;
        return ;
    }
    int mid = s + t >> 1;
    if(s <= x && x <= mid){
        update(s, mid, x, k, node << 1);
    }
    if(mid + 1 <= x && x <= t){
        update(mid + 1, t, x, k, (node << 1) + 1);
    }
    push_up(node);
}

int query(int l, int r, int s, int t, int node){
    if(l <= s && t <= r){
        return d[node];
    }
    int mid = s + t >> 1;
    return __gcd(query(l, r, s, mid, node << 1), query(l, r, mid + 1, t, (node << 1) + 1));
}

int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        diff[i] = a[i] - a[i - 1];
    }
    build(1, n, 1);
    while(m--){
        char op;
        cin >> op;
        if(op == 'C'){
            int x, y, k;
            cin >> x >> y >> k;
            update(1, n, x, k, 1);
            update(1, n, y + 1, -k, 1);
        }
        else{
            int x, y;
            cin >> x >> y;
            cout << __gcd(a[x], query(x + 1, y, 1, n, 1)) << endl;
        }
    }
    return 0;
}

代码为什么 RE 了,求助各位 dalao。

2022/2/19 14:36
加载中...