其他分享
首页 > 其他分享> > 线段树

线段树

作者:互联网

区间修改

#define int long long

const int maxn = 3e5 + 7;
int a[maxn];
struct node {
    int l, r, sum, lz;
} tr[maxn << 4];

void push_up(int p) {
    tr[p].sum = tr[p << 1].sum + tr[p << 1 | 1].sum;
}

void push_down(int p) {
    if (tr[p].lz != 0) {
        tr[p << 1].sum += (tr[p << 1].r - tr[p << 1].l + 1) * tr[p].lz;
        tr[p << 1 | 1].sum += (tr[p << 1 | 1].r - tr[p << 1 | 1].l + 1) * tr[p].lz;
        tr[p << 1].lz += tr[p].lz;
        tr[p << 1 | 1].lz += tr[p].lz;
        tr[p].lz = 0;
    }
}

void build(int p, int l, int r) {
    tr[p].l = l, tr[p].r = r;
    if (l == r) {
        tr[p].lz = 0;
        tr[p].sum = a[l];
        return;
    }
    int mi = (l + r) >> 1;
    build(p << 1, l, mi);
    build(p << 1 | 1, mi + 1, r);
    push_up(p);
}

void update(int p, int l, int r, int val) {
    if (l <= tr[p].l && tr[p].r <= r) {
        tr[p].lz += val;
        tr[p].sum += (tr[p].r - tr[p].l + 1) * val;
        return;
    }
    push_down(p);
    int mi = (tr[p].l + tr[p].r) >> 1;
    if (l <= mi) update(p << 1, l, r, val);
    if (r > mi) update(p << 1 | 1, l, r, val);
    push_up(p);
}

int res;

void query(int p, int l, int r) {
    if (l <= tr[p].l && tr[p].r <= r) {
        res += tr[p].sum;
        return;
    }
    push_down(p);
    int mi = (tr[p].l + tr[p].r) >> 1;
    if (l <= mi) query(p << 1, l, r);
    if (r > mi) query(p << 1 | 1, l, r);
    push_up(p);
}

int n, t, m;

void solve() {
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
    build(1, 1, n);
    for (int i = 1; i <= m; i++) {
        char op;
        int l, r, val;
        scanf(" %c", &op);
        if (op == 'Q') {
            scanf("%lld%lld", &l, &r);
            res = 0;
            query(1, l, r);
            printf("%lld", res);
            if (i != m) puts("");
        } else {
            scanf("%lld%lld%lld", &l, &r, &val);
            update(1, l, r, val);
        }
    }
}

标签:node,3e5,int,线段,mi,long,maxn
来源: https://www.cnblogs.com/maymi/p/15129812.html