QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#100251 | #4924. 蜘蛛爬树 | jp01HUANGZI | 0 | 2737ms | 123344kb | C++14 | 5.3kb | 2023-04-25 11:01:20 | 2023-04-25 11:01:22 |
Judging History
answer
// my name is huangzirui, i (jp01) will ak ioi 2024
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 i128;
#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);
}
// }}}
// {{{ convex
struct convex {
int top;
vector <pair <ll, ll> > stk;
inline void init () { top = -1; }
inline void push_back (pair <ll, ll> & e) {
while (top > 0 && (i128) (stk[top - 1].se - e.se) * (stk[top].fi - stk[top - 1].fi) >=
(i128) (stk[top - 1].se - stk[top].se) * (e.fi - stk[top - 1].fi)) -- top, stk.pop_back ();
stk.emplace_back ( e ), ++ top;
}
inline ll query (ll d) {
int l = 0, r = top - 1, mid, p = -1;
while (l <= r) {
mid = l + r >> 1;
if ((i128) d * (stk[mid + 1].fi - stk[mid].fi) >= (stk[mid].se - stk[mid + 1].se)) p = mid, r = mid - 1;
else l = mid + 1;
}
if (! ~ p) return INF;
return stk[p].fi * d + stk[p].se;
}
};
// }}}
// {{{ divide tree
int cen[N][LogN], adj[N][LogN], lst_rt[N], dep[N], siz[N];
bool vis[N];
convex c1[N], c2[N];
int rt, mx;
vector <pair <ll, ll> > pt1, pt2;
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) {
pt1.emplace_back ( w[u], dist (u, _cen) + dist (u, cen[_cen][dep[_cen] - 1]));
pt2.emplace_back ( w[u], dist (_cen, u) * 2 );
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
pt1.clear (), pt2.clear ();
dep[rt] = _dep, vis[rt] = true, color (rt, 0, rt, u); // cen & adj
sort (pt1.begin (), pt1.end ()), c1[rt].init ();
for (auto & e : pt1) c1[rt].push_back (e);
sort (pt2.begin (), pt2.end ()), c2[rt].init ();
for (auto & e : pt2) c2[rt].push_back (e);
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 = dist (c, a) + c1[c].query (d);
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 = dist (a, c) + dist (b, c) + c2[c].query (d);
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: 6ms
memory: 20860kb
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 10807904631326 3440559556796 5431186517423 4458875959622 1566659300400 7908007295597 1858310964948 5113840012487 6968388472340 4706970599850 5270158948178 4633066810868 3176122148295 2369520344240 965219608895 14356904769815 9678824287684 4259573672967 7335705726284 8380301414777 12319...
result:
wrong answer 2nd lines differ - expected: '10806758605627', found: '10807904631326'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #20:
score: 0
Wrong Answer
time: 2147ms
memory: 110648kb
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:
wrong answer 1st lines differ - expected: '920563165', found: '922546910'
Subtask #4:
score: 0
Wrong Answer
Test #28:
score: 0
Wrong Answer
time: 2737ms
memory: 123344kb
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:
wrong answer 6th lines differ - expected: '54425124319330092', found: '54425449850886139'
Subtask #5:
score: 0
Wrong Answer
Test #36:
score: 0
Wrong Answer
time: 389ms
memory: 100684kb
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:
wrong answer 258th lines differ - expected: '1251426204676', found: '1251613120990'
Subtask #6:
score: 0
Wrong Answer
Test #43:
score: 0
Wrong Answer
time: 1724ms
memory: 110580kb
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:
wrong answer 2nd lines differ - expected: '380880451347644', found: '384382937186346'
Subtask #7:
score: 0
Skipped
Dependency #1:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%