浅谈线段树
前言
线段树我在初一的暑假最初接触,并写了一些黄绿的模板。
线段树作为一个常用的数据结构,在很多题里都有应用。
线段树博大精深,这篇文章只能介绍其冰山一角。
线段树干了什么?
线段树维护的是半群信息。在很多情况下也是交换半群信息。
半群
给定一个集合
该运算满足结合律,即
幺半群
比起半群,它多出来的条件是:
存在幺元满足
交换半群
比起半群,它多出来的条件是:
该运算满足交换律,即
而对于线段树,我们需要维护两种半群:
记录信息的半群
那么首先我们应该考虑的是,我们需要维护什么内容?
后文均以 P8868 举例。
转化为有两个序列
既然要区间历史和,肯定需要
要维护这个的话,考虑如何应对修改操作:
假设修改了
更普遍地,如果需要维护的东西是一个多项式,可以以此为最高项,拆成若干多项式,或者分析一个操作后,为了维护某一个值,需要其他哪些辅助值。本题同时还要维护常数项
对于 Tag 半群的构造,首先有
然后给一个套路做法,记录在推平操作前
为什么要推平操作前呢,原因和区间加、区间推平操作线段树先加后推平一样,加操作需要依赖修改前的值,若先推平了就不好搞了。
现在我们构造完了信息的半群和 Tag 的半群。
然后我们需要考虑三个事:
- 将两个信息半群合并
- 将两个 Tag 半群合并
- 将一个 Tag 半群作用于一个信息的半群上;
第一个是容易维护的,对应项相加即可。
第二个的话,考虑本来是 Tag
我们需要维护新的
发现我们需要知道
如果都有,则对于
最后,用
对于第三个,先用 Tag 中的各项系数更新 Info 的
只要这三个东西解决了,线段树就可以轻松的实现了。
比如此题,如果 Info 和 Tag 较为复杂,可以写成单独的结构体。
代码:
#include <bits/stdc++.h>
#define int unsigned long long
using namespace std;
void File(string s) { freopen((s + ".in").c_str(), "r", stdin), freopen((s + ".out").c_str(), "w", stdout); }
inline void chkmax(int &a, int b) { if (a < b) a = b; }
inline void chkmin(int &a, int b) { if (a > b) a = b; }
bool b1;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int N = 250005;
int n, a[N], b[N], q;
struct Info {
int sxy, sx, sy, len, s;
Info (int _sxy = 0, int _sx = 0, int _sy = 0, int _len = 0, int _s = 0) {
sxy = _sxy, sx = _sx, sy = _sy, len = _len, s = _s;
}
friend Info operator + (Info x, Info y) {
return Info(x.sxy + y.sxy, x.sx + y.sx, x.sy + y.sy, x.len + y.len, x.s + y.s);
}
};
struct Tag {
int vx, vy; // 区间覆盖
int cxy, cx, cy, c;
Tag (int _vx = 0, int _vy = 0, int _cxy = 0, int _cx = 0, int _cy = 0, int _c = 0) {
vx = _vx, vy = _vy, cxy = _cxy, cx = _cx, cy = _cy, c = _c;
}
bool empty() { return !(vx | vy | cxy | cx | cy | c); }
friend Info operator + (Info x, Tag y) {
Info res = x;
res.s += y.cxy * x.sxy + y.cx * x.sx + y.cy * x.sy + y.c * x.len;
if (y.vx && y.vy) {
res = Info(y.vx * y.vy * x.len, y.vx * x.len, y.vy * x.len, x.len, res.s);
}
else if (y.vx) {
res = Info(y.vx * x.sy, y.vx * x.len, x.sy, x.len, res.s);
}
else if (y.vy) {
res = Info(x.sx * y.vy, x.sx, x.len * y.vy, x.len, res.s);
}
// else res = x;
return res;
}
friend Tag operator + (Tag y, Tag x) { // put x on y, x is new
if (y.vx && y.vy) {
y.c += x.cxy * y.vx * y.vy + x.cx * y.vx + x.cy * y.vy + x.c;
}
else if (y.vx) {
y.cy += x.cxy * y.vx + x.cy;
y.c += x.cx * y.vx + x.c;
}
else if (y.vy) {
y.cx += x.cxy * y.vy + x.cx;
y.c += x.cy * y.vy + x.c;
}
else {
y.cxy += x.cxy;
y.cx += x.cx;
y.cy += x.cy;
y.c += x.c;
}
if (x.vx) y.vx = x.vx;
if (x.vy) y.vy = x.vy;
return y;
}
};
struct Segment_tree {
Info tr[N << 2];
Tag tg[N << 2];
#define ls (p << 1)
#define rs (p << 1 | 1)
void build(int s = 1, int t = n, int p = 1) {
if (s == t) { tr[p].len = 1; return; }
int m = (s + t) >> 1;
build(s, m, ls), build(m + 1, t, rs);
tr[p] = tr[ls] + tr[rs];
}
void pushdown(int p) {
if (tg[p].empty()) return ;
tr[ls] = tr[ls] + tg[p], tg[ls] = tg[ls] + tg[p];
tr[rs] = tr[rs] + tg[p], tg[rs] = tg[rs] + tg[p];
tg[p] = Tag();
}
void modify(int l, int r, Tag v, int s = 1, int t = n, int p = 1) {
if (l <= s && r >= t) {
tr[p] = tr[p] + v;
tg[p] = tg[p] + v;
return;
}
int m = (s + t) >> 1;
pushdown(p);
if (l <= m) modify(l, r, v, s, m, ls);
if (r > m) modify(l, r, v, m + 1, t, rs);
tr[p] = tr[ls] + tr[rs];
}
int query(int l, int r, int s = 1, int t = n, int p = 1) {
if (l <= s && r >= t) return tr[p].s;
int m = (s + t) >> 1, res = 0; pushdown(p);
if (l <= m) res += query(l, r, s, m, ls);
if (r > m) res += query(l, r, m + 1, t, rs);
return res;
}
} seg;
int astk[N], atp, bstk[N], btp;
vector<pair<int, int> > qd[N];
int ans[N];
bool b2;
signed main() {
// cout << (&b2 - &b1) / 1024.0 / 1024;return 0;
// File("match");
int TTT;
cin >> TTT >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
seg.build();
cin >> q;
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
qd[r].push_back(make_pair(l, i));
}
for (int i = 1; i <= n; i++) {
while (atp && a[astk[atp]] < a[i]) atp--;
while (btp && b[bstk[btp]] < b[i]) btp--;
seg.modify(astk[atp] + 1, i, Tag(a[i], 0));
seg.modify(bstk[btp] + 1, i, Tag(0, b[i]));
astk[++atp] = i, bstk[++btp] = i;
seg.modify(1, i, Tag(0, 0, 1));
for (auto [l, id] : qd[i]) {
ans[id] = seg.query(l, i);
}
}
for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
return 0;
}
最后丢一些草稿。
For a tag
- If
y_{vx},y_{vy} both have value, then all x-s here arey_{vx} , all y-s arey_{vy} . Then forxy ,s addsx_{cxy}\times y_{cx}\times y_{cy}\times len ; forx ,s addsx_{cx}\times y_{vx}\times len , fory ,s addsx_{cy}\times y_{vy}\times len ; forlen ,s addsx_c\times len . So we put them iny_c , withoutlen . - If
y_{vx} has value, then all x-s arey_{vx} . Then forxy ,s addsx_{cxy}\times v_{sy}\times y_{vx} , put inv_{sy} ; forx ,s addsx_{cx}\times y_{vx}\times len , put inlen ; fory ,s addsx_{cy}\times v_{sy} , put inv_{sy} ; forlen ,s addsx_c\times len , put inlen . -
- If neither has value. Then add the corresponding value.