QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#715189 | #5439. Meet in the Middle | Corzica | WA | 216ms | 23896kb | C++20 | 4.9kb | 2024-11-06 10:45:03 | 2024-11-06 10:45:04 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int>> to[100005], tto[100005], ques[100005];
int st[21][200005], dfn[100005], cnt, siz[100005], dfns[100005], fa[100005], tab[100005];
long long d[100005], dep[100005], aans[500005];
inline void dfs1(int p, int q) {
dfn[p] = ++cnt;
tab[dfn[p]] = p;
siz[p] = 1;
for (pair<int, int> op : to[p]) {
if (op.first == q) continue;
d[op.first] = d[p] + op.second;
dfs1(op.first, p);
siz[p] += siz[op.first];
}
}
inline void dfs2(int p, int q) {
fa[p] = q;
dfns[p] = ++cnt;
st[0][cnt] = p;
for (pair<int, int> op : tto[p]) {
if (op.first == q) continue;
dep[op.first] = dep[p] + op.second;
dfs2(op.first, p);
st[0][++cnt] = p;
}
}
inline int gets(int p, int q) {
return (dep[p] < dep[q]) ? p : q;
}
inline int lca(int p, int q) {
if (dfns[p] > dfns[q]) swap(p, q);
static int mid;
mid = __lg(dfns[q] - dfns[p] + 1);
return gets(st[mid][dfns[p]], st[mid][dfns[q] - (1 << mid) + 1]);
}
inline long long getdiss(int p, int q) {
int w = lca(p, q);
return dep[p] + dep[q] - dep[w] * 2;
}
struct val {
int fir, sec;
long long vala, valb, ans;
};
struct node {
int l, r;
long long tag;
val ans;
} b[400005];
inline void update(val& ans, int p, int q, long long w, long long ww) {
if (ans.ans < getdiss(p, q) + w + ww) {
ans = val{p, q, w, ww, getdiss(p, q) + w + ww};
}
}
inline void update(int p) {
b[p].ans = (b[2 * p].ans.ans > b[2 * p + 1].ans.ans) ? b[2 * p].ans : b[2 * p + 1].ans;
update(b[p].ans, b[2 * p].ans.fir, b[2 * p + 1].ans.fir, b[2 * p].ans.vala, b[2 * p + 1].ans.vala);
update(b[p].ans, b[2 * p].ans.sec, b[2 * p + 1].ans.fir, b[2 * p].ans.valb, b[2 * p + 1].ans.vala);
update(b[p].ans, b[2 * p].ans.fir, b[2 * p + 1].ans.sec, b[2 * p].ans.vala, b[2 * p + 1].ans.valb);
update(b[p].ans, b[2 * p].ans.sec, b[2 * p + 1].ans.sec, b[2 * p].ans.valb, b[2 * p + 1].ans.valb);
}
inline void build(int p, int l, int r) {
b[p].l = l, b[p].r = r;
if (l == r) {
b[p].ans = val{tab[l], tab[l], d[tab[l]], d[tab[l]], d[tab[l]]};
return;
}
build(2 * p, l, (l + r) >> 1);
build(2 * p + 1, ((l + r) >> 1) + 1, r);
update(p);
}
inline void ads(int p, long long q) {
b[p].tag += q;
if (b[p].ans.fir == b[p].ans.sec) {
b[p].ans.ans += q;
} else {
b[p].ans.ans += q + q;
}
b[p].ans.vala += q, b[p].ans.valb += q;
}
inline void push_down(int p) {
if (b[p].tag) {
ads(2 * p, b[p].tag), ads(2 * p + 1, b[p].tag);
b[p].tag = 0;
}
}
inline void change(int p, int l, int r, long long w) {
if (b[p].l >= l && b[p].r <= r) {
ads(p, w);
return;
}
push_down(p);
int mid = (b[p].l + b[p].r) >> 1;
if (l <= mid) change(2 * p, l, r, w);
if (r > mid) change(2 * p + 1, l, r, w);
update(p);
}
inline long long query(int p, int q) {
if (b[p].l == b[p].r) {
return b[p].ans.vala;
}
push_down(p);
if (q <= ((b[p].l + b[p].r) >> 1)) return query(2 * p, q);
return query(2 * p + 1, q);
}
inline long long getdis(int p, int q) {
int w = lca(p, q);
return query(1, dfns[q]) + dep[p] + dep[q] - 2 * dep[w];
}
inline void print(int p) {
// cout << b[p].l << ' ' << b[p].r << ' ' << b[p].tag << ' ' << b[p].ans.vala << ' ' << b[p].ans.valb << " " << b[p].ans.fir << ' ' << b[p].ans.sec << ' ' << b[p].ans.ans << "\n";
if (b[p].l == b[p].r) return;
print(2 * p), print(2 * p + 1);
}
inline void getans(int p, int q) {
for (pair<int, int> op : to[p]) {
if (op.first == q) continue;
change(1, 1, n, op.second);
change(1, dfn[op.first], dfn[op.first] + siz[op.first] - 1, -2 * op.second);
getans(op.first, p);
change(1, 1, n, - op.second);
change(1, dfn[op.first], dfn[op.first] + siz[op.first] - 1, 2 * op.second);
}
int op1 = b[1].ans.fir, op2 = b[1].ans.sec;
// cout << p << ' ' << op1 << " " << op2 << ' ' << b[1].ans.ans << ' ' << b[1].ans.vala << " " << b[1].ans.valb << "@\n";
print(1);
for (pair<int, int> op : ques[p]) {
aans[op.second] = max(getdis(op.first, op1), getdis(op.first, op2));
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
int p, q, w;
for (int i = 1; i < n; i++) {
cin >> p >> q >> w;
to[p].push_back(make_pair(q, w)), to[q].push_back(make_pair(p, w));
}
for (int i = 1; i < n; i++) {
cin >> p >> q >> w;
tto[p].push_back(make_pair(q, w)), tto[q].push_back(make_pair(p, w));
}
dfs1(1, 0);
cnt = 0;
dfs2(1, 0);
for (int i = 1; (1 << i) <= cnt; i++) {
for (int j = 1; j + (1 << i) - 1 <= cnt; j++) {
st[i][j] = gets(st[i - 1][j + (1 << (i - 1))], st[i - 1][j]);
}
}
for (int i = 1; i <= m; i++) {
cin >> p >> q;
ques[p].push_back(make_pair(q, i));
}
build(1, 1, n);
getans(1, 0);
for (int i = 1; i <= m; i++) {
cout << aans[i] << "\n";
}
// cout << getdiss(2, 3) << " " << lca(2, 3) << ' ' << dfns[2] << ' ' << dfns[3] << "@\n";
}
//线段树动态维护直径,好陌生的玩意
//这玩意我确实想不到/没见过,给大神磕头了
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7812kb
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: 2ms
memory: 7744kb
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: 2ms
memory: 9748kb
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: 216ms
memory: 23896kb
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:
293850772176 546592202460 449793409146 280838771396 401595486358 289615294218 485620172337 427770819876 399061740412 348588476580 518452863065 407768217884 694803494319 713719072394 397582198239 413437791403 560314398392 533632560121 394786546354 340727450944 603074922998 437500727000 345347772230 3...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '293850772176'