线段树模板
Posted nublity
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线段树模板相关的知识,希望对你有一定的参考价值。
void pushdown(int l, int r, int rt) { if (lazy[rt]) { sum[rt << 1] += l * lazy[rt]; sum[(rt << 1) + 1] += r * lazy[rt]; lazy[rt << 1] += lazy[rt]; lazy[(rt << 1) + 1] += lazy[rt]; lazy[rt] = 0; } } void build(int l, int r, int rt) { if (l == r) { sum[rt] = tmp[l]; return; } int mid = (l + r) >> 1; build(l, mid, rt << 1); build(mid + 1, r, (rt << 1) + 1); sum[rt] = sum[rt << 1] + sum[(rt << 1) + 1]; } void update(int l, int r, int rt, int l1, int r1, int v) { if (l1 <= l && r <= r1) { sum[rt] += v * (r - l + 1); lazy[rt] += v; return; } int mid = (l + r) >> 1; pushdown(mid - l + 1, r - mid, rt); if (l1 <= mid) update(l, mid, rt << 1, l1, r1, v); if (r1 > mid) update(mid + 1, r, (rt << 1) + 1, l1, r1, v); sum[rt] = sum[rt << 1] + sum[(rt << 1) + 1]; } int query(int l, int r, int rt, int l1, int r1) { if (l1 <= l && r <= r1) return (ll)sum[rt]; int mid = (l + r) >> 1; int ret = 0; pushdown(mid - l + 1, r - mid, rt); if (l1 <= mid) ret += query(l, mid, rt << 1, l1, r1); if (r1 > mid) ret += query(mid + 1, r, (rt << 1) + 1, l1, r1); sum[rt] = sum[rt << 1] + sum[(rt << 1) + 1]; return ret; }
以上是关于线段树模板的主要内容,如果未能解决你的问题,请参考以下文章