萌新求调悬棺

P3372 【模板】线段树 1

_Kimi_ @ 2024-07-31 14:05:06

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, m;
long long a[maxn], T[maxn << 2], lazy[maxn << 2];
void bee(int id, int l, int r, int k){
    lazy[id] += k;
    T[id] += k * (r - l + 1);
}
void pushup(int id){
    T[id] = T[id << 1] + T[id << 1 | 1];
}
void pushdown(int id, int l, int r){
    int mid = l + r >> 1;
    bee(id << 1, l, mid, lazy[id]);
    bee(id << 1 | 1, mid + 1, r, lazy[id]);
    lazy[id] = 0;
}
void build(int id, int l, int r){
    if(l == r){
        T[id] = a[l];
        return;
    }
    int mid = l + r >> 1;
    build(id << 1, l, mid);
    build(id << 1 | 1, mid + 1, r);
    pushup(id);
}
void update(int id, int l, int r, int x, int y, int k){
    if(x <= l && r <= y){
        bee(id, l, r, k);
        return;
    }
    pushdown(id, l, r);
    int mid = l + r >> 1;
    if(x <= mid) update(id << 1, l, mid, x, y, k);
    if(y > mid) update(id << 1 | 1, mid + 1, r, x, y, k);
    pushup(id);
}
long long query(int id, int l, int r, int x, int y){
    if(x <= l && r <= y){
        return T[id];
    }
    pushdown(id, l, r);
    int mid = l + r >> 1;
    long long ans = 0;
    if(mid >= x) ans += query(id << 1, l, mid, x, y);
    if(mid < y) ans += query(id << 1 | 1, mid + 1, r, x, y);
    return ans;
}
int main(){
    scanf("%lld%lld", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    build(1, 1, n);
    for(int i = 1; i <= m; i++){
        long long op, x, y, k;
        scanf("%lld", &op);
        if(op == 1){
           scanf("%lld%lld%lld", &x, &y, &k);
           update(1, 1, n, x, y, k);
        }else{
            scanf("%lld%lld", &x, &y);
            printf("%lld\n", query(1, 1, n, x, y));
        }
    }
    return 0;
}

之前AC了,重新交莫名其妙MLE


by CNS_5t0_0r2 @ 2024-07-31 14:08:41

@Kimi 为什么你 int 的变量读入用 %lld 啊


by CNS_5t0_0r2 @ 2024-07-31 14:09:24

@Kimi 我改成 %d 就过了


by _Kimi_ @ 2024-07-31 14:09:33

@5t0_0r2 %%%dalao灌注了


by _Kimi_ @ 2024-07-31 14:10:44

@5t0_0r2 那为啥我之前可以AC


by CNS_5t0_0r2 @ 2024-07-31 14:12:13

@Kimi 可能是语言的问题或者是 O2 的问题


by CNS_5t0_0r2 @ 2024-07-31 14:12:36

刚测,确实是 O2 的问题


|