QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#481197 | #5439. Meet in the Middle | duankaidi | RE | 0ms | 3888kb | C++23 | 5.5kb | 2024-07-16 21:21:05 | 2024-07-16 21:21:06 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
class Tree {
public:
int n, s, root;
std::vector<int> fa, top, dfn, id, sz, son;
std::vector<i64> dep;
std::vector<std::vector<std::pair<int, int>>> adj;
Tree() {Tree(0);}
Tree(int _n) {
Tree(_n, 0);
}
Tree(int _n, int rt) {
init(_n, rt);
}
void init(int _n, int rt) {
n = _n;
dep.assign(n, 0);
fa.assign(n, 0);
top.assign(n, 0);
dfn.assign(n, 0);
id.assign(n, 0);
sz.assign(n, 1);
son.assign(n, -1);
adj.assign(n, {});
root = rt;
}
void add(int u, int v, int w = 1) {
adj[u].push_back(std::pair(v, w));
adj[v].push_back(std::pair(u, w));
}
void dfs(int x, int fath) {
for (auto [v, w] : adj[x])
if (v != fath) {
fa[v] = x;
dep[v] = dep[x] + w;
dfs(v, x);
sz[x] += sz[v];
if (son[x] == -1 || sz[son[x]] < sz[v])
son[x] = v;
}
}
void dfs2(int x, int topf) {
top[x] = topf;
static int tot = 0;
id[dfn[x] = tot++] = x;
if (son[x] == -1) {
return ;
}
dfs2(son[x], topf);
for (auto [v, w] : adj[x]) {
if (v == son[x] || v == fa[x])
continue;
dfs2(v, v);
}
}
int lca(int u, int v) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]])
std::swap(u, v);
u = fa[top[u]];
}
return (dep[u] < dep[v] ? u : v);
}
i64 dist(int u, int v) {
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
bool isAcestor(int u, int v) {
return lca(u, v) == u;
}
void init() {
dfs(root, root);
dfs2(root, root);
}
} ;
template<class Info>
class SegmentTree {
public:
int n;
std::vector<Info> info;
SegmentTree() : n{} {}
SegmentTree(int _n) {
Build(_n, std::vector<Info> (_n, {}));
}
SegmentTree(int _n, std::vector<Info> a) {
Build(_n, a);
}
void Build(int _n, std::vector<Info> a) {
n = _n;
info.assign(n << 2, {});
std::function<void(int, int, int)> build = [&](int x, int l, int r) {
if (l + 1 == r) {
info[x] = a[l];
return ;
}
int mid = (l + r) >> 1;
build(x << 1, l, mid);
build(x << 1 | 1, mid, r);
info[x] = info[x << 1] + info[x << 1 | 1];
} ;
build(1, 0, n);
}
void Modify(int x, int l, int r, int pos/*, Info val*/) {
if (l + 1 == r) {
// info[x] = val;
return ;
}
int mid = (l + r) >> 1;
if (pos < mid) {
Modify(x << 1, l, mid, pos/*, val*/);
} else {
Modify(x << 1 | 1, mid, r, pos/*, val*/);
}
info[x] = info[x << 1] + info[x << 1 | 1];
}
void Modify(int pos/*, Info val*/) {
Modify(1, 0, n, pos/*, val*/);
}
Info RangeQuery(int x, int l, int r, int L, int R) {
if (l >= R || r <= L)
return {};
if (l >= L && r <= R)
return info[x];
int mid = (l + r) >> 1;
return RangeQuery(x << 1, l, mid, L, R) + RangeQuery(x << 1 | 1, mid, r, L, R);
}
Info RangeQuery(int L, int R) {
return RangeQuery(1, 0, n, L, R);
}
Info Query(int pos) {
return RangeQuery(pos, pos + 1);
}
} ;
struct Info {
int l = 0, r = 0;
} ;
Tree A, B;
int now;
i64 getdis(int x, int y) {
return A.dist(now, x) + A.dist(now, y) + B.dist(x, y);
}
Info operator + (Info lhs, Info rhs) {
Info res = lhs; i64 R = getdis(lhs.l, lhs.r);
vector<int> vec = {lhs.l, lhs.r, rhs.l, rhs.r};
for (int i = 0; i < 4; ++i) for (int j = i + 1; j < 4; ++j) {
if (getdis(vec[i], vec[j]) > R) {
R = getdis(vec[i], vec[j]);
res.l = vec[i], res.r = vec[j];
}
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q; cin >> n >> q;
A.init(n, 0); B.init(n, 0);
vector<vector<array<int, 2>>> vec(n), t1(n);
vector<i64> ans(q);
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w; --u, --v;
A.add(u, v, w);
t1[u].push_back({v, w});
t1[v].push_back({u, w});
}
// return 0;
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w, --u, --v;
B.add(u, v, w);
} A.init(); B.init();
for (int i = 0; i < q; ++i) {
int a, b; cin >> a >> b, --a, --b;
vec[a].push_back({b, i});
}
vector<Info> init(n);
for (int i = 0; i < n; ++i) init[i].l = init[i].r = A.id[i];
SegmentTree<Info> seg(n, init);
auto upd = [&](int x, int type) {
if (type == 0) now = x;
else now = A.fa[x];
seg.Modify(A.dfn[now]); seg.Modify(A.dfn[now] + A.sz[now] - 1);
} ;
function<void(int, int)> dfs = [&](int x, int fa) {
if (x > 1) upd(x, 0);
int l = seg.RangeQuery(0, n).l, r = seg.RangeQuery(0, n).r;
// cerr << x << ' ' << l << ' '<< r << "cao\n";
for (auto [v, i] : vec[x]) {
i64 vx = A.dist(l, x) + B.dist(l, v);
i64 vy = A.dist(r, x) + B.dist(r, v);
ans[i] = max(vx, vy);
}
for (auto [v, w] : t1[x]) if (v != fa) dfs(v, x);
if (x > 1) upd(x, 1);
} ;
dfs(0, 0);
for (int i = 0; i < q; ++i) cout << ans[i] << '\n';
}
/*
3 4
1 2 2
1 3 2
1 2 3
2 3 2
3 1
1 2
1 3
3 1
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3692kb
input:
3 4 1 2 1 2 3 2 1 2 2 2 3 1 1 1 1 2 2 1 2 2
output:
6 4 5 3
result:
ok 4 number(s): "6 4 5 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3888kb
input:
2 1 1 2 1 1 2 1 1 1
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
2 1 1 2 1 1 2 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Runtime Error
input:
10000 50000 8101 5996 108427744 5996 7605 870838849 5996 5599 603303696 8101 3006 339377417 8101 6463 442516687 6463 5560 109174079 5560 4063 127596224 3006 1682 947915262 5996 1986 130416311 6463 5455 279771516 6463 2634 516688796 4063 3034 217886863 7605 5092 742375061 5599 2266 193804402 5092 140...
output:
647838384844 626539793176 514273941704 637066393138 472546379596 645842915960 641537859258 573604504956 644081575470 803875451466 674370549986 734764046916 744862815441 763778393516 553499885160 526743824759 610373719514 689550247042 549161302822 726811438160 653134244120 666761991962 701575393972 6...