跳到正文

个人记录

U599317

chao_tao2约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=500005;
int a[maxn],w[maxn*3],lzy[maxn*3];
void pushup(int u){
    w[u]=w[u*2]+w[u*2+1];
}
void build(int u,int L,int R){
    if (L == R){
        w[u]=a[L];
        return;
    }
    int mid = (L+R) >> 1;
    build(u*2,L,mid),build(u*2+1,mid+1,R);
    pushup(u);
}
bool inrange(int L,int R,int l,int r){
    if (l <= L && r >= R) return true;
    return false;
}
bool outrange(int L,int R,int l,int r){
    if (L > r || R < l) return true;
    return false;
}
void maketag(int u,int len,int x){
    lzy[u] += x;
    w[u] += len*x;
}
void pushdown(int u,int L,int R){
    int mid = (L+R)/2;
    maketag(u*2,mid-L+1,lzy[u]);
    maketag(u*2+1,R-mid,lzy[u]);
    lzy[u]=0;
}
int query(int u,int L,int R,int l,int r){
    if (inrange(L,R,l,r)) return w[u];
    else if (!outrange(L,R,l,r)){
        int mid=(L+R)/2;
        pushdown(u,L,R);
        return query(u*2,L,mid,l,r) + query(u*2+1,mid+1,R,l,r);
    }else return 0;
}
void update(int u,int L,int R,int l,int r,int x){
    if (inrange(L,R,l,r)) maketag(u,R-L+1,x);
    else if (!outrange(L,R,l,r)){
        int mid=(L+R)/2;
        pushdown(u,L,R);
        update(u*2,L,mid,l,r,x);
        update(u*2+1,mid+1,R,l,r,x);
        pushup(u);
    }
}
signed main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int n,q;
    cin >> n >> q;
    for (int i = 1; i <= n; i++) cin >> a[i];
    build(1,1,n);
    while (q--){
        int op;
        cin >> op;
        if (op == 1){
            int x,y,k;
            cin >> x >> y >> k;
            update(1,1,n,x,y,k);
        }else{
            int x;
            cin >> x;
            cout << query(1,1,n,x,n) << "\n";
        }
    }
    return 0;
}
原文