RE 求调

P3372 【模板】线段树 1

Eason_cyx @ 2024-06-22 16:19:09

#include <bits/stdc++.h>
using namespace std;
struct seg {
    int l,r,x;
} tree[800005];
int a[100005],tag[100005];
int ls(int x) {return x << 1;}
int rs(int x) {return x << 1 | 1;}
void pushup(int x) {
    tree[x].x = tree[ls(x)].x + tree[rs(x)].x;
}
void build(int x,int l,int r) {
    tag[x] = 0;
    if(l == r) {
        tree[x].x = a[l];
        return ;
    }
    int mid = (l+r) >> 1;
    build(ls(x),l,mid);
    build(rs(x),mid+1,r);
}
void addtag(int x,int l,int r,int k) {
    tree[x].x += (r-l+1) * k;
    tag[x] += k;
}
void pushdown(int x,int l,int r) {
    int mid = (l+r) >> 1;
    addtag(ls(x),l,mid,tag[x]);
    addtag(rs(x),mid+1,r,tag[x]);
    tag[x] = 0;
}
void update(int nl,int nr,int x,int l,int r,int k) {
    //nlnr 是需要更新的
    if(nl <= l && r <= nr) {
        tag[x] += k;
        tree[x].x += k * (r-l+1);
        return ;
    }
    pushdown(x,l,r);
    int mid = (l+r) >> 1;
    if(nl <= mid) update(nl,nr,ls(x),l,mid,k);
    if(mid+1 <= nr) update(nl,nr,rs(x),mid+1,r,k);
}
int query(int ql,int qr,int x,int l,int r) {
    int ans = 0;
    if(ql <= l && r <= qr) {
        return tree[x].x;
    }
    int mid = (l+r) >> 1;
    pushdown(x,l,r);
    if(ql <= mid) ans += query(ql,qr,ls(x),l,mid);
    if(qr > mid) ans += query(ql,qr,rs(x),mid+1,r);
    return ans;
}
int main() {
    int n,m; cin >> n >> m;
    for(int i = 1;i <= n;i++) cin >> a[i];
    build(1,1,n);
    while(m--) {
        int opt; cin >> opt;
        if(opt == 1) {
            int l,r,x; cin >> l >> r >> x;
            // update(1,1,n,l,r,x); 
        }
        else {
            int x,y; cin >> x >> y;
            cout << query(1,1,n,x,y) << endl;
        }
    }
    return 0;
}

by Eason_OIer @ 2024-06-22 16:37:53

@qnqfff 关注辣


上一页 |