线段树 RE 0分求助

P3372 【模板】线段树 1

yhylivedream @ 2023-07-07 21:36:09

#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
typedef long long ll;
ll a[N], w[N * 4], lzy[N * 4];
void push(int u){ w[u] = w[u << 1] + w[u << 1 + 1]; }
void build(int u, int l, int r){
    if(l == r){ w[u] = a[l]; return; }
    int m = (l + r) / 2;
    build(u * 2, l, m); build(u * 2 + 1, m + 1, r); push(u);
}
void mtag(int u, int len, ll x){
    lzy[u] += x;
    w[u] += len * x; 
}
void pushd(int u, int l, int r){
    int m = (l + r) / 2;
    mtag(u * 2, m - l + 1, lzy[u]);
    mtag(u * 2 + 1, r - m, lzy[u]);
    lzy[u] = 0;
}
ll query(int u, int n, int m, int l, int r){
    if((l <= n) && (m <= r)) return w[u];
    else if(!((n > r) || (m < l))){
        int t = (l + r) / 2;
        pushd(u, n, m);
        return query(u * 2, n, t, l, r) + query(u * 2 + 1, t + 1, m, l, r);
    }
    else return 0;
}
void update(int u, int n, int m, int l, int r, ll x){
    if((l <= n) && (m <= r))
        mtag(u, m - n + 1, x);
    else if(!((n > r) || (m < l))){
        int t = (l + r) / 2;
        pushd(u, n, m);
        update(u * 2, n, m, l, r, x);
        update(u * 2 + 1, t + 1, m, l, r, x);
        push(u);
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n, m;
    cin >> n >> m; 
    for(int i=1; i<=n; i++)
        cin >> a[i];
    build(1, 1, n);
    for(int i=1; i<=m; i++){
        ll k;
        int type, x, y;
        cin >> type;
        if(type == 1){
            cin >> x >> y >> k;
            update(1, 1, n, x, y, k);
        }
        else{
            cin >> x >> y;
            cout << query(1, 1, n, x, y) << "\n";
        }
    }
    return 0;
}

by __HHX__ @ 2023-07-07 21:59:17

az,你这连样例都过不了的罢 (雾 @livedreamyhy


by yhylivedream @ 2023-07-07 22:00:14

@HHX 不知道为什么会错。


by __HHX__ @ 2023-07-07 22:03:24

@livedreamyhy 你自己不会查卡在拿了吗


|