0分求调

P3372 【模板】线段树 1

zhengjingtian @ 2024-11-27 22:39:21


#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5;
int n,m;
int a[MAXN],t[MAXN*4],lzy[MAXN*4];
bool check1(int l,int r,int L,int R) {
    return ((L<=l) && (r<=R));
}
bool check2(int l,int r,int L,int R) {
    return ((l>R) || (r<L));
}
void maketag(int u,int l,int r,int x) {
    lzy[u]+=x;
    t[u]+=((r-l+1)*x);
}
void pushdown(int u,int l,int r) {
    int mid=((l+r)>>1);
    maketag(u*2,l,mid,lzy[u]);
    maketag(u*2+1,mid+1,r,lzy[u]);
    lzy[u]=0;
}
void build(int u,int l,int r) {
    if(l==r)
        t[u]=a[l];
    else {
        int mid=((l+r)>>1);
        build(2*u,l,mid);
        build(2*u+1,mid+1,r);
        t[u]=t[2*u]+t[2*u+1];
    }
}
void update(int u,int l,int r,int L,int R,int x) {
    if(check1(l,r,L,R))
        maketag(u,L,R,x);
    else if(!check2(l,r,L,R)) {
        int mid=((l+r)>>1);
        pushdown(u,l,r);
        update(u*2,l,mid,L,R,x);
        update(u*2+1,mid+1,r,L,R,x);
        t[u]=t[2*u]+t[2*u+1];
    }
}
int query(int u,int l,int r,int L,int R) {
    if(check1(l,r,L,R))
        return t[u];
    else if(!check2(l,r,L,R)) {
        int mid=((l+r)>>1);
        pushdown(u,l,r);
        return query(u*2,l,mid,L,R)+query(u*2+1,mid+1,r,L,R);
    } else
        return 0;
}
int main() {
    cin>>n>>m;
    for(int i=1; i<=n; i++)
        cin>>a[i];
    build(1,1,n);
    for(int i=1; i<=m; i++) {
        int a;
        cin>>a;
        if(a==1) {
            int x,y,k;
            cin>>x>>y>>k;
            update(1,1,n,x,y,k);
        } else {
            int x,y;
            cin>>x>>y;
            cout<<query(1,1,n,x,y)<<endl;
        }
    }
    return 0;
}

by szh_AK_all @ 2024-11-27 22:44:11

@zhengjingtian

首先,你没开long long把


by zhengjingtian @ 2024-11-27 22:45:46

@szh_AK_all开了也不对


by szh_AK_all @ 2024-11-27 22:50:57

@zhengjingtian

你的query写错了把


by zhengjingtian @ 2024-11-28 12:31:56

哪里


by Lg9919 @ 2024-12-01 14:15:56


```cpp
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lc x<<1
#define rc x<<1|1
struct node {
    ll val, l, r, lazy;
} tr[100005 << 2];
ll a[100005], n, q, op, x, y, z;
using namespace std;
void pushUp(int x) {
    tr[x].val = tr[lc].val + tr[rc].val;
}
void build(int x, int l, int r) {
    tr[x].l = l, tr[x].r = r;
    if (l == r) {
        tr[x].val = a[l];
        return ;
    }
    int m = (l + r) >> 1;
    build(lc, l, m);
    build(rc, m + 1, r);
    pushUp(x);
}
void pushDown(int x) {
    if (!tr[x].lazy) return ;
    tr[lc].val += tr[x].lazy * (tr[lc].r - tr[lc].l + 1);
    tr[rc].val += tr[x].lazy * (tr[rc].r - tr[rc].l + 1);
    tr[lc].lazy += tr[x].lazy;
    tr[rc].lazy += tr[x].lazy;
    tr[x].lazy = 0;
}
void update(int x, int l, int r, int k) {
    if (l <= tr[x].l && tr[x].r <= r) {
        tr[x].val += k * (tr[x].r - tr[x].l + 1);
        tr[x].lazy += k;
        return ;
    }
    pushDown(x);
    int m = (tr[x].l + tr[x].r) >> 1;
    if (l <= m) update(lc, l, r, k);
    if (r > m) update(rc, l, r, k);
    pushUp(x);
}
ll query(int x, int l, int r) {
    if (l <= tr[x].l && tr[x].r <= r)
        return tr[x].val;
    pushDown(x);
    ll sum = 0;
    int m = (tr[x].l + tr[x].r) >> 1;
    if (l <= m) sum += query(lc, l, r);
    if (r > m) sum += query(rc, l, r);
    return sum;
}
int main() {
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    build(1, 1, n);
    while (q--) {
        cin >> op >> x >> y;
        if (op == 1) {
            cin >> z;
            update(1, x, y, z);
        } else
            cout << query(1, x, y) << endl;
    }
    return 0;
}

|