题解:P17141 [NOI 2026] 传送
首先,你走至少一步到一个传送门一定不优。所以对于
令
考虑贪心,每次加入当前距离
继续观察到一个显然的性质是,每次总是会加入一个深度的所有点。
进一步,实际上答案是
继续考虑,下面的式子与
- 若
d < \operatorname{dis}(x, y) ,那么直接取t 作为答案; - 否则直接取
\operatorname{dis}(x, y) 作为答案。
做到
瓶颈是对于每个
使用点分树查询邻域信息即可,就是
写完之后咋感觉跑不过根号。
#include <bits/stdc++.h>
#include "teleport.h"
#define ll long long
using namespace std;
namespace qwq {
const int N = 500005;
int n, pos[N], rk[N], dep[N], siz[N], tfa[N], cnto, o[N << 1];
bool vis[N];
vector<int> e[N];
namespace ST {
int lg[N << 1], f[20][N << 1];
void Init(int n, int *a) {
int i, j;
for(i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
for(i = 1; i <= n; i++) f[0][i] = a[i];
for(i = 1; i <= lg[n]; i++) for(j = 1; j + (1 << i) - 1 <= n; j++) {
f[i][j] = min(f[i - 1][j], f[i - 1][j + (1 << (i - 1))]);
}
}
}
inline int Dis(int u, int v) {
int l = pos[u], r = pos[v]; if(l > r) swap(l, r);
int x = ST::lg[r - l + 1]; return dep[u] + dep[v] - min(ST::f[x][l], ST::f[x][r - (1 << x) + 1]) * 2;
}
void Dfs(int u, int fa) {
dep[u] = dep[fa] + 1, o[++cnto] = dep[u], pos[u] = cnto;
for(auto v : e[u]) if(v != fa) Dfs(v, u), o[++cnto] = dep[u];
}
void Count(int &tot, int u, int fa) { tot++; for(auto v : e[u]) if(v != fa && !vis[v]) Count(tot, v, u); }
void Find_Center(int tot, int &c, int u, int fa) {
int maxsiz = 0; siz[u] = 1;
for(auto v : e[u]) if(v != fa && !vis[v]) {
Find_Center(tot, c, v, u), siz[u] += siz[v], maxsiz = max(maxsiz, siz[v]);
}
if(max(maxsiz, tot - siz[u]) <= tot / 2) c = u;
}
vector<int> a[N], c[N];
vector<ll> b[N], d[N];
inline void Add(vector<int> &vec, int x, int d) {
while(vec.size() <= x) vec.emplace_back(0);
vec[x] += d;
}
void DfsAdd(int u, int fa, vector<int> &f, int cdep) {
Add(f, cdep, 1);
for(auto v : e[u]) if(v != fa && !vis[v]) DfsAdd(v, u, f, cdep + 1);
}
int Build(int u) {
int tot = 0; Count(tot, u, 0), Find_Center(tot, u, u, 0), Add(a[u], 0, 1), vis[u] = true;
for(auto v : e[u]) if(!vis[v]) {
vector<int> tmp; DfsAdd(v, u, tmp, 1);
int w = Build(v), i; tfa[w] = u, c[w].resize(tmp.size() - 1);
for(i = tmp.size() - 1; i >= 0; i--) {
if(i) c[w][i - 1] = tmp[i];
Add(a[u], i, tmp[i]);
}
}
return u;
}
struct Frac {
ll a, b;
friend bool operator <=(Frac x, Frac y) {
return x.a * y.b <= y.a * x.b;
}
friend bool operator <(Frac x, Frac y) {
return x.a * y.b < y.a * x.b;
}
};
inline int Get(vector<int> &vec, int x) { return x < 0 || vec.empty() ? 0ll : (vec.size() <= x ? vec.back() : vec[x]); }
inline ll Get(vector<ll> &vec, int x) { return x < 0 || vec.empty() ? 0ll : (vec.size() <= x ? vec.back() : vec[x]); }
unordered_map<ll, Frac> mp;
Frac Query(int s, int t) {
if(mp.count(1ll * s * N + t)) return mp[1ll * s * N + t];
Frac res = {n, 0};
int u = s, v = 0;
while(u) {
int x = Dis(s, u), y;
y = Get(a[u], t - x), res.b += y, res.a += Get(b[u], t - x) + 1ll * x * y;
y = Get(c[v], t - x - 1), res.b -= y, res.a -= Get(d[v], t - x - 1) + 1ll * (x + 1) * y;
v = u, u = tfa[u];
}
// cerr << s << ' ' << t << ' ' << res.a << ' ' << res.b << endl;
return mp[1ll * s * N + t] = res;
}
bool Check(int s, int x) { return Query(s, x) <= Query(s, x + 1); }
int f[N];
Frac g[N];
void Dfs2(int u, int fa) {
g[u] = Query(u, f[u]);
for(auto v : e[u]) if(v != fa) {
f[v] = max(f[u] - 1, 0);
while(f[v] < n && !Check(v, f[v])) f[v]++;
Dfs2(v, u);
}
}
inline void Set(vector<ll> &x, vector<int> &y) {
int i, siz = y.size(); x.resize(siz);
for(i = 1; i < siz; i++) x[i] = x[i - 1] + 1ll * y[i] * i, y[i] += y[i - 1];
}
void Solve() {
int i; Dfs(1, 0), ST::Init(cnto, o), Build(1), f[1] = 0;
for(i = 1; i <= n; i++) Set(b[i], a[i]), Set(d[i], c[i]);
// for(i = 1; i <= n; i++) {
// cerr << "node " << i << ' ' << tfa[i] << endl;
// cerr << "a : "; for(auto v : a[i]) cerr << v << ' '; cerr << endl;
// cerr << "b : "; for(auto v : b[i]) cerr << v << ' '; cerr << endl;
// cerr << "c : "; for(auto v : c[i]) cerr << v << ' '; cerr << endl;
// cerr << "d : "; for(auto v : d[i]) cerr << v << ' '; cerr << endl;
// }
while(f[1] < n && !Check(1, f[1])) f[1]++;
Dfs2(1, 0);
// for(i = 1; i <= n; i++) cerr << i << " : " << f[i] << " | " << g[i].a << '/' << g[i].b << endl;
}
pair<ll, int> Report(int x, int y) { Frac res = min(Frac({Dis(x, y), 1}), g[y]); return make_pair(res.a, int(res.b)); }
}
vector<pair<ll, int> > teleport(int c, int n, int m, vector<int> u, vector<int> v, vector<int> x, vector<int> y) {
int i; vector<pair<ll, int> > vec; qwq::n = n;
for(i = 0; i < n - 1; i++) qwq::e[u[i] + 1].emplace_back(v[i] + 1), qwq::e[v[i] + 1].emplace_back(u[i] + 1);
qwq::Solve();
for(i = 0; i < m; i++) vec.emplace_back(qwq::Report(x[i] + 1, y[i] + 1));
for(auto &v : vec) { ll g = __gcd(v.first, (ll)v.second); v.first /= g, v.second /= g; }
return vec;
}