0pts求调

P3372 【模板】线段树 1

CEFqwq @ 2024-03-23 15:34:20

upd 没写 pup 10 分,写了变 0 分/jk

#include<bits/stdc++.h>
#define int long long
using namespace std;
#define mxn 200005
struct node{
    int l, r, vl, lzy;
}tr[mxn << 2 | 3];
int a[mxn], n, q;
int ls(int u){
    return (u << 1);
}
int rs(int u){
    return (u << 1 | 1);
}
void glz(int u, int k){
    tr[u].lzy += k;
}
void gv(int u, int k){
    tr[u].vl += k;
}
int md(int l, int r){
    return (l + r) >> 1;
}
void pup(int u){
    tr[u].vl = tr[ls(u)].vl + tr[rs(u)].vl;
}
void build(int u, int l, int r){
    tr[u].l = l;
    tr[u].r = r;
    if (l == r){
        tr[u].vl = a[l];
        return;
    }
    int mid = md(l, r);
    build(ls(u), l, mid);
    build(rs(u), mid + 1, r);
    pup(u);
}
bool inr(int l, int r, int ql, int qr){
    return (ql <= l && qr >= r);
}
bool otr(int l, int r, int ql, int qr){
    return (ql > r || qr < l);
}
void pdn(int u, int l, int r){
    int mid = md(l, r);
    glz(ls(u), tr[u].lzy);
    gv(ls(u), tr[u].lzy * (mid - l + 1));
    glz(rs(u), tr[u].lzy);
    gv(rs(u), tr[u].lzy * (r - (mid + 1) + 1));
    tr[u].lzy = 0;
}
void upd(int u, int ql, int qr, int x){
//  cout << u << " " << tr[u].l << " " << tr[u].r << " " << tr[u].vl << " " << ql << " " << qr << " " << x << "\n";
    if (inr(tr[u].l, tr[u].r, ql, qr)){
        glz(u, x);
        gv(u, x * (tr[u].r - tr[u].l + 1));
//      cout << u << " " << tr[u].l << " " << tr[u].r << " " << tr[u].vl << " " << ql << " " << qr << " " << x << "\n";
        return;
    }
//  cout << u << " " << tr[u].l << " " << tr[u].r << " " << tr[u].vl << " " << ql << " " << qr << " " << x << "\n";
    if (otr(tr[u].l, tr[u].r, ql, qr))
        return;
    pdn(u, ql, qr);
//  int mid = md(tr[u].l, tr[u].r);
    upd(ls(u), ql, qr, x);
    upd(rs(u), ql, qr, x);
    pup(u);
}
int qry(int u, int ql, int qr){
    int res = 0;
    if (inr(tr[u].l, tr[u].r, ql, qr))
        return tr[u].vl;
    if (otr(tr[u].l, tr[u].r, ql, qr))
        return 0;
    pdn(u, ql, qr);
//  int mid = md(tr[u].l, tr[u].r);
    res += (qry(ls(u), ql, qr) + qry(rs(u), ql, qr));;
    return res;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    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 l, r, x;
            cin >> l >> r >> x;
            upd(1, l, r, x); 
        }else{
            int l, r;
            cin >> l >> r;
            cout << qry(1, l, r) << "\n"; 
        }
    //  for (int i = 1; i <= n; i++)
    //      cout << qry(1, i, i) << " ";
    //  cout << "\n";
    }
}

by Terrible @ 2024-03-23 16:42:05

@爱肝大模拟的tlxjy

pdn(u, tr[u].l, tr[u].r);

将你上面贴的代码改过就 AC 了


|