0pts求调

P3372 【模板】线段树 1

XURUIFAN @ 2024-10-21 22:31:36

rt,全wa样例能过

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int N = 2e5 + 5;
struct node {
    int l, r;
    ll s;
    ll lazy;
} tr[4 * N];
int w[N];
void push_up(int u) {
    tr[u].s = tr[u << 1].s + tr[u << 1 | 1].s;
}
inline void build(int u, int l, int r) {
    tr[u].l = l, tr[u].r = r, tr[u].lazy = 0;
    if (l == r) tr[u].s = w[l];
    else {
        int mid = (l + r) >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        push_up(u);
    }
}
ll query(int u, int l, int r) {
    if (l <= tr[u].l && r >= tr[u].r) return tr[u].s;
    int mid = (tr[u].l  + tr[u].r) >> 1;
    if (tr[u].lazy) {
        int ls = u << 1, rs = u << 1 | 1;
        tr[ls].lazy += tr[u].lazy;
        tr[rs].lazy += tr[u].lazy;
        tr[ls].s += tr[u].lazy * (mid - l + 1);
        tr[rs].s += tr[u].lazy * (r - mid);
        tr[u].lazy = 0;
    }
    ll ans = 0;
    if (l <= mid) ans += query(u << 1, l, mid);
    if (r > mid) ans += query(u << 1 | 1, mid + 1, r);
    return ans;
}

inline void change(int u, int l, int r, int x) {
    if (l <= tr[u].l && r >= tr[u].r) {
        tr[u].s += x * (tr[u].l - tr[u].r + 1);
        tr[u].lazy += x;
        return ;
    }
    int ls = u << 1, rs = u << 1 | 1, mid = (tr[u].l + tr[u].r) >> 1;
    if (tr[u].lazy && tr[u].l != tr[u].r) {
        tr[ls].s += tr[u].lazy * (mid - l + 1), tr[rs].s += tr[u].lazy * (r - mid);
        tr[ls].lazy += tr[u].lazy, tr[rs].lazy += tr[u].lazy;
        tr[u].lazy = 0;
    }
    if (l <= mid) change(ls, l, mid, x);
    if (r > mid) change(rs, mid + 1, r, x);
    push_up(u);
}

int n, q;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> q;
    for (int i = 1; i <= n; i++) cin >> w[i];
    build(1, 1, n);
    while (q--) {
        int l, r, t;
        cin >> t >> l >> r;
        if (t == 1) {
            int k;
            cin >> k;
            change(1, l, r, k);
        } else cout << query(1, l, r) << "\n";
    }
    return 0;
}

by KeQingDog @ 2024-10-22 19:35:31

帮您改过了,错的好多,您自己对着看看吧

//help https://www.luogu.com.cn/discuss/969010
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int N = 2e5 + 5;
struct node {
    int l, r;
    ll s;
    ll lazy;
} tr[4 * N];
int w[N];
void push_up(int u) {
    tr[u].s = tr[u << 1].s + tr[u << 1 | 1].s;
}
inline void build(int u, int l, int r) {
    tr[u].l = l, tr[u].r = r, tr[u].lazy = 0;
    if (l == r) tr[u].s = w[l];
    else {
        int mid = (l + r) >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        push_up(u);
    }
}
ll query(int u, int l, int r) {
    if (l <= tr[u].l && r >= tr[u].r) return tr[u].s;
    int mid = (tr[u].l  + tr[u].r) >> 1;
    if (tr[u].lazy) {
        int ls = u << 1, rs = u << 1 | 1;
        tr[ls].lazy += tr[u].lazy;
        tr[rs].lazy += tr[u].lazy;
        tr[ls].s += tr[u].lazy * (mid - tr[u].l + 1);
        tr[rs].s += tr[u].lazy * (tr[u].r - mid);
        tr[u].lazy = 0;
    }
    ll ans = 0;
    if (l <= mid) ans += query(u << 1, l, r);
    if (r > mid) ans += query(u << 1 | 1, l, r);
    return ans;
}

inline void change(int u, int l, int r, int x) {
    if (l <= tr[u].l && r >= tr[u].r) {
        tr[u].s += x * (tr[u].r - tr[u].l + 1);
        tr[u].lazy += x;
        return ;
    }
    int ls = u << 1, rs = u << 1 | 1, mid = (tr[u].l + tr[u].r) >> 1;
    if (tr[u].lazy && tr[u].l != tr[u].r) {
        tr[ls].s += tr[u].lazy * (mid - tr[u].l + 1), tr[rs].s += tr[u].lazy * (tr[u].r - mid);
        tr[ls].lazy += tr[u].lazy, tr[rs].lazy += tr[u].lazy;
        tr[u].lazy = 0;
    }
    if (l <= mid) change(ls, l, r, x);
    if (r > mid) change(rs, l, r, x);
    push_up(u);
}

int n, q;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> q;
    for (int i = 1; i <= n; i++) cin >> w[i];
    build(1, 1, n);
    while (q--) {
        int l, r, t;
        cin >> t >> l >> r;
        if (t == 1) {
            int k;
            cin >> k;
            change(1, l, r, k);
        } else cout << query(1, l, r) << "\n";
    }
    return 0;
}

by KeQingDog @ 2024-10-22 19:35:42


by XURUIFAN @ 2024-10-22 20:49:34

@KeQingDog 感谢大佬,已关注
(好细啊我勒个,这么细节的吗)
帖结


|