dalao求调,玄关

P3372 【模板】线段树 1

jinyanshao @ 2024-09-08 18:08:24

样例都过不去,无语

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, a[N];
int ans[N * 2], tag[N * 2];

void push_up(int x) {
    ans[x] = ans[x * 2] + ans[x * 2 + 1];
}

void lazy_tag(int l, int r, int x, int k) {
    tag[x] = tag[x] + k;
    ans[x] = ans[x] + k * (r - l + 1);
}

void push_down(int l, int r, int x) {
    int mid = (l + r) / 2;
    lazy_tag(l, mid, x * 2, tag[x]);
    lazy_tag(mid + 1, r, x * 2 + 1, tag[x]);
    tag[x] = 0;
}

void build(int l, int r, int x) {
    if (l == r) {
        ans[x] = a[l];
        return;
    }
    int mid = (l + r) / 2;
    build(l, mid, x * 2);
    build(mid + 1, r, x * 2 + 1);
    push_up(x);
}

void update(int ll, int rr, int l, int r, int x, int k) {
    if (ll <= l && r <= rr) {
        ans[x] += k * (r - l + 1);
        tag[x] += k;
        return;
    }
    push_down(l, r, x);
    int mid = (l + r) / 2;
    if (ll <= mid)update(ll, rr, l, mid, x * 2, k);
    if (rr > mid)update(ll, rr, mid + 1, r, x * 2 + 1, k);
    push_up(x);
}

int query(int ll, int rr, int l, int r, int x) {
    int res = 0;
    if (ll <= l && r <= rr)return ans[x];
    int mid = (l + r) / 2;
    push_down(l, r, x);
    if (ll <= mid)res += query(ll, rr, l, mid, x * 2);
    if (rr > mid)res += query(ll, rr, mid + 1, r, x * 2 + 1);
    return res;
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    build(1, n, 1);
    while (m--) {
        int op;
        cin >> op;
        if (op == 1) {
            int x, y, k;
            cin >> x >> y >> k;
            update(x, y, 1, n, 1, k);
        }
        if (op == 2) {
            int x, y;
            cout << query(x, y, 1, n, 1) << endl;
        }
    }
}

by ryf_loser @ 2024-09-08 18:27:23

op==2 的输入呢


by jinyanshao @ 2024-09-08 19:13:42

@ryf20100124 70


by ryf_loser @ 2024-09-08 19:27:00

@jinyanshao 十年oi一场空,不开long long见祖宗


by jinyanshao @ 2024-09-12 14:12:19

@ryf20100124 开了,70


|