标签:pushd 线段 query lse += turn date hup lazy
struct SegmentTree {
#define ls (o<<1)
#define rs (o<<1|1)
static const int MAXN = 100000;
ll a[MAXN + 5];
ll st[(MAXN << 2) + 5];
void PushUp(int o) {
st[o] = st[ls] + st[rs];
}
void Build(int o, int l, int r) {
if(l == r)
st[o] = a[l];
else {
int m = l + r >> 1;
Build(ls, l, m);
Build(rs, m + 1, r);
PushUp(o);
}
lazy[o] = 0;
}
void Update(int o, int l, int r, int p, ll v) {
if(l == r) {
st[o] += v;
return;
} else {
int m = l + r >> 1;
if(p <= m)
Update(ls, l, m, p, v);
if(p >= m + 1)
Update(rs, m + 1, r, p, v);
PushUp(o);
}
}
ll Query(int o, int l, int r, int ql, int qr) {
if(ql <= l && r <= qr) {
return st[o];
} else {
int m = l + r >> 1;
ll res = 0;
if(ql <= m)
res = res + Query(ls, l, m, ql, qr);
if(qr >= m + 1)
res = res + Query(rs, m + 1, r, ql, qr);
return res;
}
}
#undef ls
#undef rs
};
struct SegmentTree {
#define ls (o<<1)
#define rs (o<<1|1)
static const int MAXN = 100000;
ll a[MAXN + 5];
ll st[(MAXN << 2) + 5], lazy[(MAXN << 2) + 5];
void PushUp(int o) {
st[o] = st[ls] + st[rs];
}
void PushDown(int o, int l, int r) {
if(lazy[o]) {
lazy[ls] += lazy[o];
lazy[rs] += lazy[o];
int m = l + r >> 1;
st[ls] += lazy[o] * (m - l + 1);
st[rs] += lazy[o] * (r - m);
lazy[o] = 0;
}
}
void Build(int o, int l, int r) {
if(l == r)
st[o] = a[l];
else {
int m = l + r >> 1;
Build(ls, l, m);
Build(rs, m + 1, r);
PushUp(o);
}
lazy[o] = 0;
}
void Update(int o, int l, int r, int ql, int qr, ll v) {
if(ql <= l && r <= qr) {
lazy[o] += v;
st[o] += v * (r - l + 1);
return;
} else {
PushDown(o, l, r);
int m = l + r >> 1;
if(ql <= m)
Update(ls, l, m, ql, qr, v);
if(qr >= m + 1)
Update(rs, m + 1, r, ql, qr, v);
PushUp(o);
}
}
ll Query(int o, int l, int r, int ql, int qr) {
if(ql <= l && r <= qr) {
return st[o];
} else {
PushDown(o, l, r);
int m = l + r >> 1;
ll res = 0;
if(ql <= m)
res = res + Query(ls, l, m, ql, qr);
if(qr >= m + 1)
res = res + Query(rs, m + 1, r, ql, qr);
return res;
}
}
#undef ls
#undef rs
};
标签:pushd 线段 query lse += turn date hup lazy
原文地址:https://www.cnblogs.com/KisekiPurin2019/p/11877098.html