洛谷P4069 [SDOI2016]游戏(李超线段树)

时间:2021-09-09 07:46:04

题意

题目链接

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;
}