洛谷P4069 [SDOI2016]游戏(李超线段树)
程序员文章站
2022-11-22 08:22:57
题意 "题目链接" Sol 这题细节好多啊qwq。。稍不留神写出一个小bug就要调1h+。。 思路就不多说了,把询问区间拆成两段就是李超线段树板子题了。 关于dis的问题可以直接维护。 cpp // luogu judger enable o2 / 李超线段树板子题 / include define ......
题意
sol
这题细节好多啊qwq。。稍不留神写出一个小bug就要调1h+。。
思路就不多说了,把询问区间拆成两段就是李超线段树板子题了。
关于dis的问题可以直接维护。
// luogu-judger-enable-o2 /* 李超线段树板子题 */ #include<bits/stdc++.h> #define pair pair<int, int> #define mp(x, y) make_pair(x, y) #define fi first #define se second #define int long long #define ll long long #define fin(x) {freopen(#x".in","r",stdin);} #define fout(x) {freopen(#x".out","w",stdout);} using namespace std; const int maxn = 1e6 + 10; template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;} template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;} inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int n, m, dis[maxn], son[maxn], fa[maxn], top[maxn], dep[maxn], siz[maxn], id[maxn], rev[maxn], root, tot; ll val[maxn]; vector<pair> v[maxn]; struct line { int k, b; }s[maxn]; int ls[maxn], rs[maxn], ll[maxn], rr[maxn], mn[maxn]; void dfs1(int x, int _fa) { fa[x] = _fa; dep[x] = dep[_fa] + 1; siz[x] = 1; for(auto &to : v[x]) { if(to.fi == _fa) continue; dis[to.fi] = dis[x] + to.se; dfs1(to.fi, x); siz[x] += siz[to.fi]; if(siz[to.fi] > siz[son[x]]) son[x] = to.fi; } } void dfs2(int x, int topf) { top[x] = topf; id[x] = ++id[0]; rev[id[0]] = x; if(!son[x]) return ; dfs2(son[x], topf); for(auto &to : v[x]) { if(top[to.fi]) continue; dfs2(to.fi, to.fi); } } int lca(int x, int y) { while(top[x] ^ top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); x = fa[top[x]]; } return dep[x] > dep[y] ? y : x; } int calc(line s, int x) { return s.k * dis[rev[x]] + s.b; } double getpoint(line x, line y) { return (double) (y.b - x.b) / (x.k - y.k); } void update(int k) { if(s[k].k || s[k].b) chmin(mn[k], min(calc(s[k], ll[k]), calc(s[k], rr[k]))); chmin(mn[k], min(mn[ls[k]], mn[rs[k]])); } void intadd(int &k, int l, int r, int ql, int qr, line seg) { if(!k) k = ++tot, mn[k] = val[0], ll[k] = l, rr[k] = r; int mid = l + r >> 1; if(ql <= l && r <= qr) { int pl = calc(s[k], l), pr = calc(s[k], r), nl = calc(seg, l), nr = calc(seg, r); if(!s[k].k && !s[k].b) {s[k] = seg; update(k); return ;} if(pl <= nl && pr <= nr) return ;//这里必须要写等号 if(pl > nl && pr > nr) {s[k] = seg; update(k); return ;} double xx = getpoint(s[k], seg); int m = dis[rev[mid]]; if(pl > nl) { if(xx > m) intadd(rs[k], mid + 1, r, ql, qr, s[k]), s[k] = seg; else intadd(ls[k], l, mid, ql, qr, seg); } else { if(xx > m) intadd(rs[k], mid + 1, r, ql, qr, seg); else intadd(ls[k], l, mid, ql, qr, s[k]), s[k] = seg; } update(k); return ; } if(ql <= mid) intadd(ls[k], l, mid, ql, qr, seg); if(qr > mid) intadd(rs[k], mid + 1, r, ql, qr, seg); update(k); } int intmin(int k, int l, int r, int ql, int qr) { int ret = val[0]; if(ql <= l && r <= qr) return mn[k]; int mid = l + r >> 1; if(s[k].k || s[k].b) chmin(ret, min(calc(s[k], max(l, ql)), calc(s[k], min(r, qr)))); if(ql <= mid) chmin(ret, intmin(ls[k], l, mid, ql, qr)); if(qr > mid) chmin(ret, intmin(rs[k], mid + 1, r, ql, qr)); return ret; } void modify(int x, int y, int k, int b) { while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); intadd(root, 1, n, id[top[x]], id[x], {k, b}); x = fa[top[x]]; } if(dep[x] < dep[y]) swap(x, y); intadd(root, 1, n, id[y], id[x], {k, b}); } void change(int s, int t, int a, int b) { int lca = lca(s, t); modify(s, lca, -a, b + a * dis[s]); modify(t, lca, a, -a * dis[lca] + b + a * (dis[s] - dis[lca])); } ll query(int x, int y) { int ans = val[0]; while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); chmin(ans, intmin(root, 1, n, id[top[x]], id[x])); x = fa[top[x]]; } if(dep[x] < dep[y]) swap(x, y); chmin(ans, intmin(root, 1, n, id[y], id[x])); return ans; } signed main() { n = read(); m = read(); for(int i = 0; i <= n; i++) mn[i] = val[i] = 123456789123456789ll; for(int i = 1; i <= n - 1; i++) { int x = read(), y = read(), z = read(); v[x].push_back(mp(y, z)); v[y].push_back(mp(x, z)); } dfs1(1, 0); dfs2(1, 1); int gg =0; for(int i = 1; i <= m; i++) { int opt = read(), s = read(), t = read(); if(opt == 1) { int a = read(), b = read(); change(s, t, a, b); } else { cout << query(s, t) << '\n'; } } return 0; }