QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#785750 | #4401. Prize | liuziao | 0 | 0ms | 0kb | C++23 | 3.6kb | 2024-11-26 19:03:52 | 2024-11-26 19:03:54 |
answer
#include <bits/stdc++.h>
// #define int int64_t
const int kMaxN = 1e6 + 5;
int n, k, q, t, rt1, rt2;
int p1[kMaxN], p2[kMaxN], dfn1[kMaxN], dfn2[kMaxN], idx1[kMaxN], idx2[kMaxN];
int lg[kMaxN], st1[kMaxN][20], st2[kMaxN][20], id[kMaxN];
int dis1[kMaxN], dis2[kMaxN];
std::vector<int> G1[kMaxN], G2[kMaxN];
std::vector<std::pair<int, int>> T1[kMaxN], T2[kMaxN];
int get(int *dfn, int x, int y) { return dfn[x] < dfn[y] ? x : y; }
void dfs1(int u, int fa) {
static int cnt = 0;
st1[dfn1[u] = ++cnt][0] = fa, idx1[cnt] = u;
for (auto v : G1[u]) {
if (v == fa) continue;
dfs1(v, u);
}
}
void dfs2(int u, int fa) {
static int cnt = 0;
st2[dfn2[u] = ++cnt][0] = fa, idx2[cnt] = u;
for (auto v : G2[u]) {
if (v == fa) continue;
dfs2(v, u);
}
}
int LCA1(int x, int y) {
if (x == y) return x;
if (dfn1[x] > dfn1[y]) std::swap(x, y);
int k = lg[dfn1[y] - dfn1[x]];
return get(dfn1, st1[dfn1[x] + 1][k], st1[dfn1[y] - (1 << k) + 1][k]);
}
int LCA2(int x, int y) {
if (x == y) return x;
if (dfn2[x] > dfn2[y]) std::swap(x, y);
int k = lg[dfn2[y] - dfn2[x]];
return get(dfn2, st2[dfn2[x] + 1][k], st2[dfn2[y] - (1 << k) + 1][k]);
}
void dfs3(int u) {
static bool vis[kMaxN] = {0};
vis[u] = 1;
for (auto [v, w] : T1[u]) {
if (vis[v]) continue;
dis1[v] = dis1[u] + w;
dfs3(v);
}
}
void dfs4(int u) {
static bool vis[kMaxN] = {0};
vis[u] = 1;
for (auto [v, w] : T2[u]) {
if (vis[v]) continue;
dis2[v] = dis2[u] + w;
dfs4(v);
}
}
void prework() {
dfs1(rt1, 0), dfs2(rt2, 0);
lg[0] = -1;
for (int i = 1; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= lg[n]; ++i) {
for (int j = 1; j <= n - (1 << i) + 1; ++j) {
st1[j][i] = get(dfn1, st1[j][i - 1], st1[j + (1 << (i - 1))][i - 1]);
st2[j][i] = get(dfn2, st2[j][i - 1], st2[j + (1 << (i - 1))][i - 1]);
}
}
}
void dickdreamer() {
std::cin >> n >> k >> q >> t;
for (int i = 1; i <= n; ++i) {
std::cin >> p1[i];
if (~p1[i]) G1[p1[i]].emplace_back(i), G1[i].emplace_back(p1[i]);
else rt1 = i;
}
for (int i = 1; i <= n; ++i) {
std::cin >> p2[i];
if (~p2[i]) G2[p2[i]].emplace_back(i), G2[i].emplace_back(p2[i]);
else rt2 = i;
}
prework();
for (int i = 1; i <= k; ++i) {
id[i] = idx1[i];
std::cout << id[i] << ' ';
}
std::cout << '\n';
std::cout.flush();
std::sort(id + 1, id + 1 + k, [&] (int x, int y) { return dfn2[x] < dfn2[y]; });
for (int i = 1; i < k; ++i)
std::cout << "? " << id[i] << ' ' << id[i + 1] << '\n';
std::cout.flush();
for (int i = 1; i < k; ++i) {
int x = id[i], y = id[i + 1], lca1 = LCA1(x, y), lca2 = LCA2(x, y);
int d1, d2, d3, d4;
std::cin >> d1 >> d2 >> d3 >> d4;
T1[lca1].emplace_back(x, d1), T1[x].emplace_back(lca1, -d1);
T1[lca1].emplace_back(y, d2), T1[y].emplace_back(lca1, -d2);
T2[lca2].emplace_back(x, d3), T2[x].emplace_back(lca2, -d3);
T2[lca2].emplace_back(y, d4), T2[y].emplace_back(lca2, -d4);
}
dfs3(id[1]), dfs4(id[1]);
std::vector<std::pair<int, int>> res;
for (int i = 1; i <= t; ++i) {
int x, y;
std::cin >> x >> y;
int lca1 = LCA1(x, y), lca2 = LCA2(x, y);
res.emplace_back(dis1[x] + dis1[y] - 2 * dis1[lca1], dis2[x] + dis2[y] - 2 * dis2[lca2]);
}
for (auto [x, y] : res) std::cout << x << ' ' << y << '\n';
std::cout.flush();
}
int32_t main() {
std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
int T = 1;
// std::cin >> T;
while (T--) dickdreamer();
// std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
500000 64682 64681 100000 46115 470589 209303 2979 473162 343535 79503 299539 404621 102085 237721 279170 392890 165201 441593 456314 218991 358478 86614 410800 159785 169761 95368 285837 297549 370283 378974 26449 444381 39320 149913 404523 144109 174828 263837 49847 468694 478535 152644 216598 301...
output:
422989 414496 290928 388223 160563 301045 470257 259625 222733 231286 345214 169817 435263 277447 386014 210139 455433 225855 264772 199736 355788 288506 233893 146148 454958 267562 498596 183745 352665 151125 266374 43142 9414 204593 212097 311775 25324 300764 6643 94847 396968 428563 311355 255767...
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #13:
score: 0
Time Limit Exceeded
input:
500000 88721 177440 100000 30974 23891 211201 125199 180489 387190 218020 498838 230147 307989 484136 257785 353027 304420 311738 169842 334090 486070 126212 328609 174959 368840 238722 418092 488389 226349 427271 457322 332454 12958 197530 264474 355717 482774 221286 282148 216441 266659 213750 628...
output:
63742 11431 300071 157785 268420 71772 84553 267656 174540 21500 451751 82419 58833 165916 94199 78203 263216 146169 306934 50728 338250 199716 469441 135516 133967 123248 375309 17045 459156 413018 49645 73720 188292 322328 493921 152164 219927 140202 236207 266137 180568 32077 371348 66876 354136 ...
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #25:
score: 0
Time Limit Exceeded
input:
500000 200 199 40000 76296 130139 291501 292412 139543 433345 372726 451574 18315 465578 324564 477223 237354 81532 65170 465332 342130 9670 193303 193680 129668 149532 268907 89969 398275 356210 324593 433492 482232 466692 135343 433758 102545 287283 432859 351864 305769 489532 101532 450535 295762...
output:
20242 414878 185020 125537 353357 496468 308518 188057 254952 120898 414314 11748 435424 326112 345902 271794 473882 337923 135188 438050 45188 88306 260313 116954 457474 435919 366460 431766 397351 392326 178950 199724 227083 282259 70917 121346 109196 193669 242154 12225 466790 155481 287973 15749...
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #37:
score: 0
Time Limit Exceeded
input:
1000000 1000 999 100000 678746 439069 32542 85937 936926 284219 461661 203235 533462 940676 230275 621140 780674 254931 562355 229273 201341 493976 358955 963527 880412 91220 474599 160086 698841 591551 718276 844558 39859 765917 34722 401724 219774 443004 682244 545401 968419 968020 354030 411187 1...
output:
545967 706162 53597 107558 776536 230611 572458 293457 390487 241653 638541 42868 433774 438059 293014 739962 25440 503383 628342 573629 887812 909797 805385 862282 382785 706534 190319 439139 648412 626240 131005 848982 269684 840650 376086 933701 18720 749474 336321 160119 795534 671698 201133 610...
result:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%