QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#100255 | #4924. 蜘蛛爬树 | jp01HUANGZI | 0 | 7ms | 12988kb | C++14 | 4.6kb | 2023-04-25 11:14:24 | 2023-04-25 11:14:26 |
Judging History
answer
// my name is huangzirui, i (jp01) will ak ioi 2024
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(...) fprintf (stderr, __VA_ARGS__)
#define fi first
#define se second
#define lep(i, l, r) for (int i = (l), i##_ext = (r); i <= i##_ext; ++ i)
#define rep(i, r, l) for (int i = (r), i##_ext = (l); i >= i##_ext; -- i)
char _c; bool _f; template <class T> inline void IN (T & x) {
_f = 0, x = 0; while (_c = getchar (), ! isdigit (_c)) if (_c == '-') _f = 1;
while (isdigit (_c)) x = x * 10 + _c - '0', _c = getchar (); if (_f) x = -x;
}
template <class T> inline void chkmin (T & x, T y) { if (x > y) x = y; }
template <class T> inline void chkmax (T & x, T y) { if (x < y) x = y; }
const int N = 2e5 + 5;
const int LogN = 20;
const ll INF = 4e18;
int n, m, q;
ll w[N];
vector <pair <int, ll> > to[N];
// {{{ tree
int st[LogN][N], Log2[N];
int tim, par[N], dfn[N], lay[N];
ll len[N];
inline int chktop (int a, int b) {
return lay[a] < lay[b] ? a : b;
}
void dfs (int u, int pre) {
par[u] = pre, st[0][dfn[u] = ++ tim] = pre, lay[u] = lay[pre] + 1;
for (auto & v : to[u]) if (v.fi != pre) {
len[v.fi] = len[u] + v.se, dfs (v.fi, u);
}
}
inline void build_st () {
Log2[0] = -1; lep (i, 1, n) Log2[i] = Log2[i >> 1] + 1;
dfs (1, 0);
lep (j, 1, 19) lep (i, 1, n - (1 << j) + 1) {
st[j][i] = chktop (st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
}
}
inline int lca (int x, int y) {
if (x == y) return x;
if (x = dfn[x], y = dfn[y], x > y) swap (x, y); ++ x;
int ret = Log2[y - x + 1];
return chktop (st[ret][x], st[ret][y - (1 << ret) + 1]);
}
inline ll dist (int x, int y) {
return len[x] + len[y] - (len[lca (x, y)] << 1ll);
}
// }}}
// {{{ divide tree
int cen[N][LogN], adj[N][LogN], lst_rt[N], dep[N], siz[N];
bool vis[N];
int rt, mx;
vector <int> pt[N];
void gsiz (int u, int pre) {
siz[u] = 1;
for (auto & v : to[u]) if (v.fi != pre && ! vis[v.fi]) {
gsiz (v.fi, u), siz[u] += siz[v.fi];
}
}
void find_rt (int u, int pre, int & all) {
int tp = all - siz[u];
for (auto & v : to[u]) if (v.fi != pre && ! vis[v.fi]) {
find_rt (v.fi, u, all), chkmax (tp, siz[v.fi]);
}
if (tp < mx) mx = tp, rt = u;
}
void color (int u, int pre, int & _cen, int & _adj) {
pt[_cen].emplace_back (u);
cen[u][dep[_cen]] = _cen;
adj[u][dep[_cen]] = _adj;
for (auto & v : to[u]) if (v.fi != pre && ! vis[v.fi]) color (v.fi, u, _cen, _adj);
}
int build_tr (int u, int all, int _dep = 0) {
rt = 0, mx = all + 1, gsiz (u, 0), find_rt (u, 0, all); // find rt
dep[rt] = _dep, vis[rt] = true, color (rt, 0, rt, u); // cen & adj
/* */
int _rt = rt; gsiz (_rt, 0);
for (auto & v : to[_rt]) if (! vis[v.fi]) build_tr (v.fi, siz[v.fi], _dep + 1);
return _rt;
}
/* ll query_1p (int c, int a, int & d, int _dep) {
ll ret = INF;
for (int & k : pt[c]) chkmin (ret, dist (k, c) + w[k] * d + dist (k, cen[c][_dep - 1]));
ret += dist (c, a);
if (a != c) chkmin (ret, dist (c, cen[c][_dep - 1]) + query_1p (cen[a][_dep + 1], a, d, _dep + 1));
return ret;
} */
ll query_1p (int c, int a, int & d, int _dep, int aim) {
ll ret = INF;
for (int & k : pt[c]) chkmin (ret, dist (a, k) + w[k] * d + dist (k, cen[c][aim]));
ret += dist (c, a);
if (a != c) chkmin (ret, query_1p (cen[a][_dep + 1], a, d, _dep + 1, aim));
return ret;
}
ll query_2p (int c, int a, int b, int & d, int _dep) {
ll ret = INF;
for (int & k : pt[c]) chkmin (ret, dist (c, k) * 2 + w[k] * d);
ret += dist (a, c) + dist (b, c);
if (adj[a][_dep + 1] == adj[b][_dep + 1]) {
if (a == c && b == c) return ret;
return min (ret, query_2p (cen[a][_dep + 1], a, b, d, _dep + 1));
}
if (a != c) chkmin (ret, dist (b, c) + query_1p (cen[a][_dep + 1], a, d, _dep + 1, _dep));
if (b != c) chkmin (ret, dist (a, c) + query_1p (cen[b][_dep + 1], b, d, _dep + 1, _dep));
return ret;
}
// }}}
inline void input () {
IN (n), IN (m), IN (q);
lep (i, 1, n) IN (w[i]);
int a, b; ll v;
lep (i, 1, n - 1) {
IN (a), IN (b),IN (v);
to[a].emplace_back ( b, v );
to[b].emplace_back ( a, v );
}
}
signed main () {
input (), build_st ();
rt = build_tr (1, n);
ll t1, t2; int u, v, d;
while (q -- ) {
IN (t1), IN (t2), u = (t1 - 1) % n + 1, v = (t2 - 1) % n + 1, d = abs ((t1 - 1) / n - (t2 - 1) / n);
printf ("%lld\n", query_2p (rt, u, v, d, 0));
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 7ms
memory: 12988kb
input:
97 99 1000 763118300 558295517 80676328 362318619 473105413 468927175 311496507 936935430 176032966 304576040 308583326 681580095 549392233 518152994 829474320 751189715 542810029 587869244 878512027 530678371 832766129 535259635 799122942 596982955 884696876 605325753 495661541 506105495 561218313 ...
output:
6136462105881 10806758605627 3440559556796 5426989115608 4458875959622 1566659300400 7908007295597 1858310964948 5119945897643 6973812673144 4710763803420 5282103769818 4642025427098 3178392425395 2331646415266 961435137842 14354872116920 9688279832092 4259573672967 7333255321628 8376795894537 12323...
result:
wrong answer 1st lines differ - expected: '6130845802041', found: '6136462105881'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #20:
score: 0
Time Limit Exceeded
input:
200000 20 200000 679416469 548913625 468159997 137709609 883140368 682558021 473174374 400192350 124143873 825920417 372498686 851213321 822264481 78195915 5427143 453304163 233551905 810910186 810046144 52603791 282167184 385032797 81387991 747194790 917579656 585184539 12659388 249218417 158295502...
output:
928863475 282048434 391852052 375510181 533652790 749071354 91929500 463561627 220590578 999272801 395255708 877549412 346797205 368700872 202205951 580458887 180298911 583931529 1080812812 259808235 648203658 296174674 740242712 573783312 409633519 515563141 948227151 1043085204 785470566 714011686...
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #28:
score: 0
Time Limit Exceeded
input:
200000 1000000000 200000 28270302 472359923 262785485 923929785 393684160 761485431 72038469 116384740 426631758 437934930 610834083 455314140 734276543 903544756 220163018 756113376 732404264 947339315 109784905 625451008 794076307 818852312 758007217 124450858 674924509 311761991 507260538 7032362...
output:
29380366674557271 9293949229605384 39616267530461282 37802232548586294 57473413500804743 54531734711883189 19483952256652449 25957520014801964 2768081488579138 32575272786368252 5940734606029288 65794756460077527 57948943452304567 2001505201769413 19376343816054596 30301893052885637 3693822308900230...
result:
Subtask #5:
score: 0
Time Limit Exceeded
Test #36:
score: 0
Time Limit Exceeded
input:
199918 1000000000 199903 1496 2382 3896 3664 1177 1627 2821 4200 3074 3783 2069 4403 629 2610 4991 4074 3033 2798 4333 3501 3667 3064 663 2821 2818 458 2950 4020 2665 3578 63 4855 4941 3492 2423 4510 1489 1018 4829 1912 3133 3174 309 287 2909 4102 4296 4526 3170 3683 4960 4863 4738 2927 2405 3600 44...
output:
1352416884531 1380463318391 923920163167 1525224977139 1405019709299 869269749781 715671043328 876194052054 1358007874327 127994985855 1230162209719 1532026808855 611656467332 1023855959729 414792924571 1316679734677 827308370883 1265411315424 821484360433 1051517948640 837509712760 582943943131 457...
result:
Subtask #6:
score: 0
Time Limit Exceeded
Test #43:
score: 0
Time Limit Exceeded
input:
200000 1000000000 200000 81882094 47220813 43282454 17633207 52769165 4830673 31396360 64793163 9174729 36727563 71268262 24662923 40146030 1430053 62926106 30042905 1330107 81817720 98841078 87766129 51155045 23216268 79896310 66625868 87854925 42976560 86542933 28336449 34932261 19698851 584453 90...
output:
542215973291695 397010925310736 184838994843601 66008155753660 363537424086650 199452968727828 190369743433589 435268486987805 117453984781554 322178395762331 232018407269857 379668378690052 224684352477008 265225310951287 497120904808769 400735253693394 220780544614802 370955378013390 4486185140945...
result:
Subtask #7:
score: 0
Skipped
Dependency #1:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%