版本比较
奶龙学习笔记之数据结构 Ⅰ
正文
增加 256 行删除 49 行
73 行未改动74757677787879808182360 行未改动全部展开443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662408 行未改动全部展开107110721073107486310751076107710781079552 行未改动全部展开1632163316341635142416361637163814271428163916401641164214311643164414331434143514361645164616471648143914401441164916501443144414451446144714481449165116521653165416551656165714541658165916601661166216631460146114621463166414651466166516661667166816691670167116721673167416757 行未改动1683168416851686148214831484148514861687168816891690169116921693169414891695169616971698169914941700170117021497149814991500170317041705170617071708170915031504150515061507150815091510171017111712171317141715171617171512151317181719172017211515172217231724172517263 行未改动73 行未改动> #### 思想:
先求出树上每个点的 $dfn$,然后发现 $u$ 和 $v$ 的 $lca$ 即为 $dfn_{l+1} \sim dfn_{r}$ 上 **深度最小的点**(假设 $dfn_{l}<dfn_{r}$)。可用 $Sparse \quad Table$ 实现 $O(1)$ 查询。
优势:时间复杂度:$O(n \log_{n})$预处理,**【$O(1)$查询!】**优势:时间复杂度:$O(n \log{n})$预处理,**【$O(1)$查询!】**
::::info[代码]
```cpp360 行未改动全部展开```
::::
---
> ### ④P5314 [Ynoi2011] ODT
[题目链接](https://www.luogu.com.cn/problem/P5314)
考察树剖的本质思想,算是一个经典 trick。
第二种操作可以用平衡树处理,但第一个操作与其他题不一样。对于每个点我们都需维护一棵平衡树,但如果维护所有距离小于等于一的点我们都加入平衡树那么操作一就无法处理。所以我们只维护每个点**轻儿子**的信息,查询时在加入这个点的父亲、自己、重儿子。因为这样修改时就可以只修改这个点到根路径上链顶的信息,即只用修改 $\log n$ 次。
这个思想在动态 dp 中也有应用。
::::info[代码]
```cpp#include <bits/stdc++.h>
#define eb emplace_back#define uint unsigned int
using namespace std;
char *p1, *p2, buf[1 << 20];#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
template <typename T> void read(T &x) { x = 0; T f = 1; char c = gc(); while(c < '0' || '9' < c) f = (c == '-') ? -1 : 1, c = gc(); while('0' <= c && c <= '9') x = x * 10 + (c & 15), c = gc(); x *= f;}
template <typename T> void write(T x) { if(x < 0) putchar('-'), x = -x; static short st[40], top(0); do st[++top] = x % 10; while(x /= 10); while(top) putchar(st[top--] | 48);}
const int maxn = 1e6 + 5;
mt19937 rng(time(0));
int n, m, a[maxn];vector <int> e[maxn];
int cnt;struct node { int ls, rs, val, siz; uint pri;} tr[maxn << 5];
struct BBT { int rt; inline void push_up(int p) { tr[p].siz = tr[tr[p].ls].siz + tr[tr[p].rs].siz + 1; } inline void newnode(int x) { tr[++cnt] = {0, 0, x, 1, rng()}; } void splitv(int p, int v, int &l, int &r) { if(!p) return (void)(l = r = 0); if(tr[p].val <= v) { l = p; splitv(tr[p].rs, v, tr[p].rs, r); } else { r = p; splitv(tr[p].ls, v, l, tr[p].ls); } push_up(p); } void splitk(int p, int k, int &l, int &r) { if(!p) return (void)(l = r = 0); int lsiz = tr[tr[p].ls].siz; if(k <= lsiz) { r = p; splitk(tr[p].ls, k, l, tr[p].ls); } else { l = p; splitk(tr[p].rs, k - lsiz - 1, tr[p].rs, r); } push_up(p); } int merge(int l, int r) { if(!l || !r) return l | r; if(tr[l].pri < tr[r].pri) { tr[l].rs = merge(tr[l].rs, r); push_up(l); return l; } else { tr[r].ls = merge(l, tr[r].ls); push_up(r); return r; } } void insert(int v) { int x, y; splitv(rt, v, x, y); newnode(v); rt = merge(merge(x, cnt), y); } void del(int v) { int x, y, z; splitv(rt, v - 1, x, y); splitk(y, 1, y, z); rt = merge(x, z); } int qry_kth(int k) { int x, y, z; splitk(rt, k - 1, x, y); splitk(y, 1, y, z); int res = tr[y].val; rt = merge(merge(x, y), z); return res; }} bbt[maxn];
struct BIT { int tr[maxn]; void add(int x, int v) { for(; x <= n; x += (x & -x)) tr[x] += v; } int qry(int x) { int res = 0; for(; x; x -= (x & -x)) res += tr[x]; return res; }} bit;
int tim, fa[maxn], dep[maxn], siz[maxn], son[maxn], top[maxn], dfn[maxn];
void dfs1(int x, int dad) { siz[x] = 1, fa[x] = dad, dep[x] = dep[dad] + 1; for(int y : e[x]) { if(y == fa[x]) continue; dfs1(y, x); siz[x] += siz[y]; if(siz[y] > siz[son[x]]) son[x] = y; }}
void dfs2(int x, int topf) { dfn[x] = ++tim, top[x] = topf; if(son[x]) dfs2(son[x], topf); for(int y : e[x]) { if(y == fa[x] || y == son[x]) continue; dfs2(y, y); bbt[x].insert(a[y]); }}
inline int val(int x) { return a[x] + bit.qry(dfn[x]);}
void update(int x, int y, int z) { while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); int u = top[x], v = val(top[x]); bbt[fa[u]].del(v); bbt[fa[u]].insert(v + z); bit.add(dfn[u], z), bit.add(dfn[x] + 1, -z); x = fa[u]; } if(dep[x] > dep[y]) swap(x, y); if(x == top[x] && fa[x]) { int v = val(x); bbt[fa[x]].del(v); bbt[fa[x]].insert(v + z); } bit.add(dfn[x], z), bit.add(dfn[y] + 1, -z);}
int main() { read(n), read(m); for(int i = 1; i <= n; i++) read(a[i]); int u, v; for(int i = 1; i < n; i++) { read(u), read(v); e[u].eb(v), e[v].eb(u); } dfs1(1, 0), dfs2(1, 1); int opt, x, y, z; while(m--) { read(opt), read(x), read(y); if(opt == 1) read(z), update(x, y, z); else { bbt[x].insert(val(x)); if(fa[x]) bbt[x].insert(val(fa[x])); if(son[x]) bbt[x].insert(val(son[x])); write(bbt[x].qry_kth(y)), putchar('\n'); bbt[x].del(val(x)); if(fa[x]) bbt[x].del(val(fa[x])); if(son[x]) bbt[x].del(val(son[x])); } } return 0;}```
::::
---
# 四、线性基
> ## 1.概念:
408 行未改动全部展开void newnode(int x) { tr[++cnt] = {rand(), x, 1, 0, 0}; }
// 2.push_up(更新当前节点的 siz)
void push_up(int p) { tr[p].siz = tr[tr[p].ls].siz + tr[tr[p].rs].siz; }void push_up(int p) { tr[p].siz = tr[tr[p].ls].siz + tr[tr[p].rs].siz + 1; }
// 3.splitv(权值分裂)
void splitv(int p, int x, int &l, int &r) {552 行未改动全部展开::::info[代码]
```cpp#include <bits/stdc++.h>#define db double
using namespace std;
const int maxn = 1e5 + 5;
const int maxn = 1e5 + 5, maxt = 4e4 + 5;const int mod1 = 39989, mod2 = 1e9;const double eps = 1e-9;
const db eps = 1e-9;int m, tot;
int n, m;
struct segment { db k, b;struct Line { double k, b;} a[maxn];
db get(int x, int i) { return a[i].k * x + a[i].b;}double calc(int i, int x) { return a[i].k * x + a[i].b; }
int cmp(int l, int r, int i, int j) { if(!j) return 1; db p = get(l, i) - get(l, j); db q = get(r, i) - get(r, j); if((p > eps) & (q > eps)) return 1; if((p < -eps) & (q < -eps)) return -1; if((fabs(p) < eps) & (fabs(q) < eps)) return 2;int cmp(double x, double y) { if(x - y > eps) return 1; if(y - x > eps) return -1; return 0;}
struct SGT { int tr[(mod1 + 5) << 2]; int tr[maxt << 2]; inline int ls(int p) { return p << 1; } inline int rs(int p) { return p << 1 | 1; } void change(int p, int l, int r, int k) { int t = cmp(l, r, k, tr[p]); if(t == 1) return (void)(tr[p] = k); if(t == 2) return (void)(tr[p] = min(tr[p], k)); if(t == -1) return; int mid = (l + r) >> 1; change(ls(p), l, mid, k); change(rs(p), mid + 1, r, k); int o = cmp(calc(k, mid), calc(tr[p], mid)); if(o == 1 || (!o && k < tr[p])) swap(tr[p], k); if(l == r) return; o = cmp(calc(k, l), calc(tr[p], l)); if(o == 1 || (!o && k < tr[p])) change(ls(p), l, mid, k); o = cmp(calc(k, r), calc(tr[p], r)); if(o == 1 || (!o && k < tr[p])) change(rs(p), mid + 1, r, k); } void update(int l, int r, int p, int pl, int pr, int k) { if(l <= pl && pr <= r) return change(p, pl, pr, k);7 行未改动 int mid = (l + r) >> 1; int res = tr[p], t; if(k <= mid) t = qry(k, ls(p), l, mid); else t = qry(k, rs(p), mid + 1, r); if(!res) res = t; else { db vt = get(k, t), vres = get(k, res); if(vt - vres > eps) res = t; else if(fabs(vt - vres) < eps) res = min(res, t); if(t) { if(!res) res = t; else { int o = cmp(calc(t, k), calc(tr[p], k)); if(o == 1 || (!o && t < res)) res = t; } } return res; } }} sgt;
int main() { scanf("%d", &m); int opt, x1, y1, x2, y2, last = 0; int opt, k, xa, ya, xb, yb, lastans = 0; while(m--) { scanf("%d", &opt); if(opt == 0) { scanf("%d", &x1); x1 = (x1 + last - 1) % mod1 + 1; printf("%d\n", (last = sgt.qry(x1, 1, 1, mod1))); if(!opt) { scanf("%d", &k); k = (k + lastans - 1) % mod1 + 1; lastans = sgt.qry(k, 1, 1, mod1); printf("%d\n", lastans); } else { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); x1 = (x1 + last - 1) % mod1 + 1; y1 = (y1 + last - 1) % mod2 + 1; x2 = (x2 + last - 1) % mod1 + 1; y2 = (y2 + last - 1) % mod2 + 1; if(x1 > x2) swap(x1, x2), swap(y1, y2); n++; if(x1 == x2) a[n] = (segment){0, max(y1, y2)}; scanf("%d%d%d%d", &xa, &ya, &xb, &yb); xa = (xa + lastans - 1) % mod1 + 1; ya = (ya + lastans - 1) % mod2 + 1; xb = (xb + lastans - 1) % mod1 + 1; yb = (yb + lastans - 1) % mod2 + 1; tot++; if(xa == xb) a[tot] = {0, max(ya, yb)}; else { a[n].k = 1.0 * (y2 - y1) / (x2 - x1); a[n].b = y1 - a[n].k * x1; if(xa > xb) swap(xa, xb), swap(ya, yb); a[tot].k = 1.0 * (yb - ya) / (xb - xa); a[tot].b = ya - a[tot].k * xa; } sgt.update(x1, x2, 1, 1, mod1, n); sgt.update(xa, xb, 1, 1, mod1, tot); } } return 0;}3 行未改动