悬2观,求改

P3372 【模板】线段树 1

chen0717 @ 2024-07-24 11:30:34

代码如下:

#include<bits/stdc++.h>
#define ls rt<<1
#define rs rt<1|1
#define lson ls,l,mid
#define rson rs,mid+1,r
using namespace std;

long long n,m;
vector<long long> tr,a,lazy;

void push_up(long long rt){
    tr[rt]=tr[ls]+tr[rs];
}

void push_down(long long rt,long long l,long long r){
    if(lazy[rt]==0) return;
    long long mid=l+r>>1;
    lazy[ls]+=lazy[rt];
    lazy[rs]+=lazy[rt];
    tr[ls]+=lazy[rt]*(mid-l+1);
    tr[rs]+=lazy[rt]*(r-mid);
    lazy[rt]=0;
}

void build(long long rt,long long l,long long r){
    lazy[rt]=0;
    if(l==r){
        tr[rt]=a[l];
        return;
    }
    long long mid=l+r>>1;
    build(lson);
    build(rson);
    push_up(rt);
}
long long query(long long rt,long long l,long long r,long long ql,long long qr){
    if(ql<=l&&r<=qr){
        return tr[rt];
    }
    long long mid=l+r>>1;
    push_down(rt,l,r);
    if(qr<=mid) return query(lson,ql,qr);
    if(mid<ql) return query(rson,ql,qr);
    return query(lson,ql,qr)+query(rson,ql,qr);
}

void update(long long rt,long long l,long long r,long long ql,long long qr,long long k){
    if(ql<=l&&r<=qr){
        lazy[rt]+=k;
        tr[rt]+=k*(r-l+1);
        return;
    }
    long long mid=l+r>>1;
    push_down(rt,l,r);
    if(ql<=mid) update(lson,ql,qr,k);
    if(mid<qr) update(rson,ql,qr,k);
    push_up(rt);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m;
    lazy=tr=vector<long long>(n*4+1,0);
    a=vector<long long>(n+1,0);
    for(long long i=1;i<=n;i++)
        cin>>a[i];
    build(1,1,n);
    for(long long i=1;i<=m;i++){
        long long op;
        cin>>op;
        if(op==1){
            long long l,r,x;
            cin>>l>>r>>x;
            update(1,1,n,l,r,x);
        }
        if(op==2){
            long long l,r;
            cin>>l>>r;
            cout<<query(1,1,n,l,r)<<'\n';
        }
    }
}

by 枫原万叶 @ 2024-07-24 12:56:20

@chen0717

这是改好了的代码

#include <iostream>
#include <vector>

using namespace std;

const int MAXN = 1e5 + 5;
long long n, m;
vector<long long> tr, a, lazy;

// 向上更新节点值
void pushUp(int rt) {
    tr[rt] = tr[rt << 1] + tr[rt << 1 | 1];
}

// 向下传递懒标记
void pushDown(int rt, int l, int r) {
    if (lazy[rt] == 0) return;
    int mid = (l + r) >> 1;
    lazy[rt << 1] += lazy[rt];
    lazy[rt << 1 | 1] += lazy[rt];
    tr[rt << 1] += lazy[rt] * (mid - l + 1);
    tr[rt << 1 | 1] += lazy[rt] * (r - mid);
    lazy[rt] = 0;
}

// 构建线段树
void build(int rt, int l, int r) {
    lazy[rt] = 0;
    if (l == r) {
        tr[rt] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    pushUp(rt);
}

// 区间查询
long long query(int rt, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) {
        return tr[rt];
    }
    int mid = (l + r) >> 1;
    pushDown(rt, l, r);
    long long res = 0;
    if (ql <= mid) res += query(rt << 1, l, mid, ql, qr);
    if (mid < qr) res += query(rt << 1 | 1, mid + 1, r, ql, qr);
    return res;
}

// 区间更新
void update(int rt, int l, int r, int ql, int qr, long long k) {
    if (ql <= l && r <= qr) {
        lazy[rt] += k;
        tr[rt] += k * (r - l + 1);
        return;
    }
    int mid = (l + r) >> 1;
    pushDown(rt, l, r);
    if (ql <= mid) update(rt << 1, l, mid, ql, qr, k);
    if (mid < qr) update(rt << 1 | 1, mid + 1, r, ql, qr, k);
    pushUp(rt);
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m;
    tr.resize(4 * n + 1, 0);
    a.resize(n + 1, 0);
    lazy.resize(4 * n + 1, 0);
    for (long long i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    for (long long i = 1; i <= m; i++) {
        long long op;
        cin >> op;
        if (op == 1) {
            long long l, r, x;
            cin >> l >> r >> x;
            update(1, 1, n, l, r, x);
        }
        if (op == 2) {
            long long l, r;
            cin >> l >> r;
            cout << query(1, 1, n, l, r) << '\n';
        }
    }
}

求关


by chen0717 @ 2024-07-24 13:05:29

bushi哥们 我想看看我错哪了QWQ


by SwethessPotion @ 2024-07-24 21:40:53

你真会用long long啊。。。。

教你一招:

typedef long long ll;

/*
或者
#define ll long long
但是#define会导致你的printf("%lld", sth)变成"%long longd"
*/

|