样例没过求调

P3372 【模板】线段树 1

shifeiTy @ 2024-08-01 15:58:45

码风有点奇怪,不好意思

#include <bits/stdc++.h>
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#define int long long

using namespace std;

const int N = 1e5 + 2;

inline void read(int &an) {
    int x = 0, f = 1;char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - 48;ch = getchar();}
    an = x * f; return ;
}//快读

int n, p[N];
struct node {
    int l, r, sum, tag;
} tree[N << 2];

struct ff {
    inline void build(int l, int r, int num) {
        tree[num].l = l, tree[num].r = r;
        int mid = (l + r - 1) >> 1;

        if (l == r) {   tree[num].sum=p[l]; return ;}
        build(l, mid, num << 1); build(mid + 1, r, num << 1 | 1);

        tree[num].sum = tree[num << 1].sum + tree[num << 1 | 1].sum;
        return ;
    }

    inline void up(int x, int z) {
        while (x)
            x = x >> 1, tree[x].sum += z;
        return ;
    }

    inline void down(int x) {
        tree[x << 1].tag +=  tree[x].tag;
        tree[x << 1 | 1].tag += tree[x].tag;
        tree[x].sum += tree[x].tag;
        tree[x].tag = 0;
        return ;
    }

    inline void toj(int num, int l, int r, int k) {
        if (tree[num].l > r || tree[num].r < l) return ;
        if (tree[num].l >= l && tree[num].r <= r) {
            up(num, (tree[num].r - tree[num].l + 1)*k), tree[num].tag += k;
            return ;
        }
        toj(num << 1, l, r, k), toj(num << 1 | 1, l, r, k);
        return ;
    }

    inline int q(int num, int l, int r) {
        if (tree[num].l > r || tree[num].r < l) return 0;
        if (tree[num].tag)  down(num);
        if (tree[num].l >= l && tree[num].r <= r)   return tree[num].sum;

        int ans = q(num << 1, l, r) + q(num << 1 | 1, l, r);
        return ans;
    }
} xds;

signed main() {

    int m;
    read(n);read(m);
    for (register int i = 1; i <= n; i++)   read(p[i]);
    xds.build(1, n, 1);

    int t, x, y, k;
    while (m--) {
        read(t);
        if (t & 1) { //t==1
            read(x);
            read(y);
            read(k);
            xds.toj(1, x, y, k);
        } else {    //t==2
            read(x);
            read(y);
            printf("%lld\n", xds.q(1, x, y));
        }
    }

    return 0;
}

by shifeiTy @ 2024-08-02 08:07:29

此贴结,

死因:pushdown中给自己传值时应该加长度


|