QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#100252 | #4924. 蜘蛛爬树 | jp01HUANGZI | 0 | 1ms | 12976kb | C++14 | 4.3kb | 2023-04-25 11:03:05 | 2023-04-25 11:03:09 |
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_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));
if (b != c) chkmin (ret, dist (a, c) + query_1p (cen[b][_dep + 1], b, d, _dep + 1));
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: 1ms
memory: 12976kb
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:
6130845802041 10806758605627 3440559556796 5431186517423 4458875959622 1566659300400 7908007295597 1858310964948 5113840012487 6968388472340 4706970599850 5270158948178 4633066810868 3176122148295 2369520344240 965219608895 14354872116920 9678824287684 4259573672967 7335705726284 8380301414777 12319...
result:
wrong answer 4th lines differ - expected: '5426989115608', found: '5431186517423'
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:
922546910 311275746 355012553 363898450 522002382 734168762 94706954 448355845 235267336 966151314 377621564 856252543 311456222 368700872 202205951 567302636 202317481 579171621 1043838058 294860411 621435809 296174674 727463012 573783312 396857222 500677226 891900111 1031612062 771021332 693901421...
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:
29294995135992468 9003943574137677 39324997066279292 37544709020512848 57388992119827952 54425449850886139 19583723937762122 25861528987285983 2608104102967357 32395369352281774 5765752637876701 65609495812941401 57820177390587134 1971831067795873 19376343816054596 30245024126291359 3701656000584488...
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:
516260625003899 384382937186346 187715294109758 56975236749493 351699587115398 188845759476214 188011317678919 414887287533565 111834744858133 305218494040213 227254762884282 368408912919426 201761449059479 246263150359463 468212144364502 389353276591541 207814284476264 341801277159919 4270404442188...
result:
Subtask #7:
score: 0
Skipped
Dependency #1:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%