AC#1 10pts 求调

P3372 【模板】线段树 1

yi105011 @ 2024-11-21 20:26:20

#include<bits/stdc++.h>
using namespace std;
long long n , m , a[100010] , op , l , r , k;
struct node {
    long long l , r , num , sum;
} ans[1000010];
void build (long long l , long long r ,long long id) {
    ans[id].num = ans[id].sum = 0;
    ans[id].l = l; ans[id].r = r;
    if (l == r) {
        ans[id].num += a[l];
        return;
    }
    long long mid = (l + r) / 2;
    build (l , mid , id * 2);
    build (mid + 1 , r , id * 2 + 1);
    ans[id].num = ans[id * 2].num + ans[id * 2 + 1].num;
    return;
}
void f1 (long long id) {
    if (ans[id].r < l || r < ans[id].l) return;
    if (ans[id].l == ans[id].r) {
        ans[id].num += k;
        return;
    }
    if (l <= ans[id].l && ans[id].r <= r) {
        ans[id].sum = k;
        ans[id].num += (ans[id].r - ans[id].l + 1) * k;
        return;
    }
    f1 (id * 2); f1 (id * 2 + 1);
    ans[id].num = ans[id * 2].num + ans[id * 2 + 1].num;
    return;
}
void f2 (long long id , long long sum) {
    ans[id].sum += sum;
    ans[id].num += (ans[id].r - ans[id].l + 1) * sum;
    if (ans[id].r < l || r < ans[id].l) return;
    if (l <= ans[id].l && ans[id].r <= r) {
        k += ans[id].num;
        return;
    }
    f2 (id * 2 , ans[id].sum);
    f2 (id * 2 + 1, ans[id].sum);
    ans[id].sum = 0;
    return;
}
int main() {
    cin >> n >> m;
    for (int i = 1;i <= n;i ++) cin >> a[i];
    build (1 , n , 1);
    while (m --) {
        cin >> op >> l >> r;
        if (op == 1) {
            cin >> k;
            f1 ( 1 );
        }
        else {
            k = 0;
            f2 (1 , 0);
            /*for (int i = 1;i <= 9;i ++) {
                cout << ans[i].l << " ~ " << ans[i].r << " = " << ans[i].num << '\n';
            }*/
            cout << k << '\n';
        }
    }
    return 0;
};

提交记录


by chenxi2009 @ 2024-11-21 21:11:58

@yi105011 对了,有用的话求关 QwQ。


by yi105011 @ 2024-11-22 18:52:43

@chenxi2009 调好了,已关,求互关


上一页 |