QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#89515 | #5439. Meet in the Middle | Shukuang | WA | 51ms | 32232kb | C++14 | 3.6kb | 2023-03-20 12:00:46 | 2023-03-20 12:00:48 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define eb emplace_back
#define vv vector
#define fi first
#define se second
using pii = pair<int, int>;
using ll = long long;
const int N = 1e5 + 10;
const int M = 5e5 + 10;
int n, m;
vv<pii> q[M];
ll ans[M];
struct Tree {
vv<pii> G[N];
int fa[N], dep[N], siz[N], son[N];
ll dis[N];
int top[N], dfn[N], id[N], tim;
void dfs(int u, int fath) {
siz[u] = 1;
fa[u] = fath;
dep[u] = dep[fath] + 1;
for (auto it : G[u]) {
int v = it.fi, w = it.se;
if (v == fath) continue;
dis[v] = dis[u] + w;
dfs(v, u);
siz[u] += siz[v];
if (siz[v] > siz[son[u]]) son[u] = v;
}
return;
}
void dfs2(int u, int tp) {
top[u] = tp;
dfn[u] = ++tim;
id[tim] = u;
if (son[u]) dfs2(son[u], tp);
for (auto it : G[u]) {
int v = it.fi;
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
return;
}
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] ? x : y;
}
ll dist(int x, int y) {
if (!x || !y) return 0;
return dis[x] + dis[y] - 2ll * dis[LCA(x, y)];
}
} A, B;
struct Path {
pii s, t; ll dis;
Path(pii _s = {0, 0}, pii _t = {0, 0}) {s = _s, t = _t, dis = B.dist(s.fi, t.fi) + s.se + t.se;}
bool operator < (const Path &rhs) const {return dis < rhs.dis;}
friend Path operator + (Path x, Path y) {
Path z = max(x, y);
z = max({z, Path(x.s, y.s), Path(x.s, y.t)});
z = max({z, Path(x.t, y.s), Path(x.t, y.t)});
return z;
}
};
#define ls (p << 1)
#define rs (p << 1 | 1)
#define mid ((l + r) >> 1)
Path t[N << 2];
ll tag[N << 2];
void build(int p, int l, int r) {
if (l == r) {
t[p].s = pii(A.id[l], A.dis[A.id[l]]);
return;
}
build(ls, l, mid); build(rs, mid + 1, r);
t[p] = t[ls] + t[rs];
}
void push(int p, ll v) {
t[p].s.se += v; t[p].t.se += v;
if (t[p].s.fi && t[p].t.fi) t[p].dis += 2 * v;
tag[p] += v;
return;
}
void pushdown(int p) {
if (!tag[p]) return;
push(ls, tag[p]); push(rs, tag[p]);
tag[p] = 0;
}
void modify(int p, int l, int r, int L, int R, ll v) {
if (L > R || l > R || r < L) return;
if (l >= L && r <= R) return push(p, v);
pushdown(p);
modify(ls, l, mid, L, R, v); modify(rs, mid + 1, r, L, R, v);
t[p] = t[ls] + t[rs];
}
void update(int l, int r, ll v) {
modify(1, 1, n, 1, n, v);
modify(1, 1, n, l, r, -2 * v);
}
ll qry(int x) {
return max(B.dist(x, t[1].s.fi) + t[1].s.se, B.dist(x, t[1].t.fi) + t[1].t.se);
}
void dfs3(int u) {
for (auto it : q[u]) ans[it.se] = qry(it.fi);
for (auto it : A.G[u]) {
int v = it.fi, w = it.se;
if (v == A.fa[u]) continue;
update(A.dfn[v], A.dfn[v] + A.siz[v] - 1, w);
dfs3(v);
update(A.dfn[v], A.dfn[v] + A.siz[v] - 1, -w);
}
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
ios :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w;
A.G[u].eb(v, w);
A.G[v].eb(u, w);
}
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w;
B.G[u].eb(v, w);
B.G[v].eb(u, w);
}
for (int i = 1; i <= m; ++i) {
int a, b; cin >> a >> b;
q[a].eb(b, i);
}
A.dfs(1, 0); B.dfs(1, 0);
A.dfs2(1, 1); B.dfs2(1, 1);
build(1, 1, n);
dfs3(1);
for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 29232kb
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: 4ms
memory: 29176kb
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: 13ms
memory: 29268kb
input:
2 1 1 2 1 1 2 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Wrong Answer
time: 51ms
memory: 32232kb
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:
282766164684 351661886232 251668544485 260828264968 197668472652 247435442184 292814762184 272595628175 279009355310 308924963556 351417256688 338492372103 336584527015 256715857282 273496264822 208085498757 249340071344 327942248080 230502976820 297221408939 309280465134 320980240963 183603345034 2...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '282766164684'