QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#463675 | #4401. Prize | shiomusubi496 | 0 | 0ms | 0kb | C++14 | 7.3kb | 2024-07-05 11:36:11 | 2024-07-05 11:36:12 |
answer
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define all(v) begin(v), end(v)
using namespace std;
using ll = long long;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
class LowestCommonAncestor {
vector<vector<int>> G;
vector<int> dep;
vector<vector<int>> par;
void dfs(int v, int p) {
par[0][v] = p;
for (int u : G[v]) {
if (u == p) continue;
dep[u] = dep[v] + 1;
dfs(u, v);
}
}
public:
void init(vector<vector<int>> G_, int r) {
G = G_;
int N = G.size();
dep.assign(N, 0);
par.assign(22, vector<int>(N, -1));
dfs(r, -1);
rep (i, 21) rep (v, N) {
par[i + 1][v] = par[i][v] == -1 ? -1 : par[i][par[i][v]];
}
}
int lca(int a, int b) const {
if (dep[a] > dep[b]) swap(a, b);
rrep (i, 22) {
if ((dep[b] - dep[a]) >> i & 1) b = par[i][b];
}
if (a == b) return a;
rrep (i, 22) {
if (par[i][a] != par[i][b]) {
a = par[i][a];
b = par[i][b];
}
}
return par[0][a];
}
int depth(int v) const { return dep[v]; }
};
// class LowestCommonAncestor {
// class SegmentTree {
// using T = pair<int, int>;
// int n;
// vector<T> dat;
// public:
// void init(vector<T> v) {
// n = 1;
// while (n < (int)v.size()) n <<= 1;
// dat.assign(2 * n, {1e9, -1});
// rep (i, v.size()) dat[i + n] = v[i];
// for (int i = n - 1; i > 0; --i) dat[i] = min(dat[i << 1], dat[i << 1 | 1]);
// }
// T prod(int l, int r) const {
// T res = {1e9, -1};
// l += n; r += n;
// while (l < r) {
// if (l & 1) res = min(res, dat[l++]);
// if (r & 1) res = min(res, dat[--r]);
// l >>= 1; r >>= 1;
// }
// return res;
// }
// };
// const vector<vector<int>>& G;
// vector<int> in, ord;
// vector<int> dep;
// SegmentTree seg;
// void dfs(int v) {
// in[v] = ord.size();
// ord.push_back(v);
// for (int u : G[v]) {
// dep[u] = dep[v] + 1;
// dfs(u);
// ord.push_back(v);
// }
// }
// public:
// LowestCommonAncestor(const vector<vector<int>>& G_, int r) : G(G_) {
// int N = G.size();
// dep.assign(N, 0);
// in.assign(N, -1);
// ord.reserve(2 * N);
// dfs(r);
// vector<pair<int, int>> v;
// for (int i : ord) v.emplace_back(dep[i], i);
// seg.init(v);
// }
// int lca(int a, int b) const {
// int l = in[a], r = in[b];
// if (l > r) swap(l, r);
// return seg.prod(l, r + 1).second;
// }
// int depth(int v) const { return dep[v]; }
// };
class WeightedUnionFind {
vector<int> par;
vector<ll> wei;
public:
void init(int n) {
par.resize(n, -1);
wei.resize(n, 0);
}
int find(int v) {
if (par[v] < 0) return v;
int r = find(par[v]);
wei[v] += wei[par[v]];
return par[v] = r;
}
ll weight(int v) {
find(v);
return wei[v];
}
void merge(int u, int v, ll w) {
w += weight(u);
w -= weight(v);
u = find(u);
v = find(v);
if (u == v) {
assert(w == 0);
return;
}
par[u] += par[v];
par[v] = u;
wei[v] = w;
}
};
int N, K, Q, T;
vector<int> P1, P2, ord;
vector<vector<int>> G1, G2, Gaux;
LowestCommonAncestor lca1, lca2;
WeightedUnionFind uf1, uf2;
int main() {
scanf("%d%d%d%d", &N, &K, &Q, &T);
int r1 = -1, r2 = -1;
P1.resize(N); P2.resize(N);
G1.resize(N); G2.resize(N);
rep (i, N) {
scanf("%d", &P1[i]);
if (P1[i] == -1) r1 = i;
else G1[--P1[i]].push_back(i);
}
rep (i, N) {
scanf("%d", &P2[i]);
if (P2[i] == -1) r2 = i;
else G2[--P2[i]].push_back(i);
}
lca1.init(G1, r1);
lca2.init(G2, r2);
vector<int> verts;
{
auto dfs = [&](auto&& self, int v) -> void {
if ((int)verts.size() == K) return;
verts.push_back(v);
for (int u : G1[v]) self(self, u);
};
dfs(dfs, r1);
}
int raux;
vector<int> aux;
Gaux.resize(N);
{
// G2 における verts の auxiliary tree を作る
ord.resize(N);
{
int cnt = 0;
auto dfs = [&](auto&& self, int v) -> void {
ord[v] = cnt++;
for (int u : G2[v]) self(self, u);
};
dfs(dfs, r2);
}
sort(all(verts), [&](int a, int b) { return ord[a] < ord[b]; });
for (auto i : verts) {
aux.push_back(i);
}
rep (i, K - 1) {
aux.push_back(lca2.lca(verts[i], verts[i + 1]));
}
sort(all(aux), [&](int a, int b) { return ord[a] < ord[b]; });
aux.erase(unique(all(aux)), aux.end());
raux = aux.front();
stack<int> st;
for (int v : aux) {
if (st.empty()) {
st.push(v);
continue;
}
while (lca2.lca(st.top(), v) != st.top()) st.pop();
Gaux[st.top()].push_back(v);
st.push(v);
}
}
vector<bool> is_aux(N, false);
for (auto v : verts) is_aux[v] = true;
sort(all(verts));
rep (i, K) {
printf("%d", verts[i] + 1);
if (i < K - 1) printf(" ");
else printf("\n");
}
vector<pair<int, int>> qs;
{
auto dfs = [&](auto&& self, int v) -> int {
int res = -1;
if (is_aux[v]) res = v;
for (int u : Gaux[v]) {
int r = self(self, u);
if (res == -1) res = r;
else {
qs.emplace_back(res, r);
}
}
return res;
};
dfs(dfs, raux);
}
assert((int)qs.size() == K - 1);
for (auto [a, b] : qs) {
printf("? %d %d\n", a + 1, b + 1);
}
printf("!\n");
fflush(stdout);
uf1.init(N); uf2.init(N);
for (auto [a, b] : qs) {
ll d1a, d1b, d2a, d2b;
scanf("%lld%lld%lld%lld", &d1a, &d1b, &d2a, &d2b);
int l1 = lca1.lca(a, b), l2 = lca2.lca(a, b);
uf1.merge(l1, a, d1a); uf1.merge(l1, b, d1b);
uf2.merge(l2, a, d2a); uf2.merge(l2, b, d2b);
}
rep (_, T) {
int a, b; scanf("%d%d", &a, &b);
--a, --b;
int l1 = lca1.lca(a, b), l2 = lca2.lca(a, b);
ll d1 = uf1.weight(a) + uf1.weight(b) - 2 * uf1.weight(l1);
ll d2 = uf2.weight(a) + uf2.weight(b) - 2 * uf2.weight(l2);
printf("%lld %lld\n", d1, d2);
}
fflush(stdout);
}
详细
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:
10 12 22 35 36 38 46 51 57 72 87 92 96 101 112 129 130 132 180 186 206 208 209 217 219 224 228 229 244 245 247 251 255 256 285 294 310 314 319 344 346 351 355 365 368 376 378 383 388 391 406 412 413 414 425 426 427 440 443 448 449 459 461 463 466 474 480 485 498 506 507 515 520 543 544 554 566 578 5...
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:
16 22 25 34 38 54 55 56 61 65 67 78 81 101 117 119 120 121 123 124 130 134 149 155 163 169 170 177 189 196 204 205 214 232 234 239 265 269 273 281 282 289 311 318 335 338 339 341 354 358 377 380 383 389 403 414 427 435 437 438 448 451 455 459 468 469 475 479 485 495 496 506 508 511 521 543 544 551 5...
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:
1214 2143 3735 4030 7129 8253 11748 12225 17034 19060 20242 30813 32330 34833 45188 48206 50829 52835 55343 59162 60737 65783 68301 70140 70498 70917 71690 81733 81917 85913 88306 88328 88823 90620 92449 95551 96560 109196 112780 113741 116227 116954 120898 121346 124294 124622 125508 125537 128363 ...
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:
2107 2333 3694 4616 5440 6866 6983 7525 9720 10062 10226 11086 11280 11649 12530 12977 15895 18501 18543 18720 18751 19029 19970 23318 23665 25440 27083 28832 29922 34062 36058 36066 36918 37375 37551 37681 37693 38230 42470 42868 42991 45539 46443 46606 47245 49131 49762 51122 51277 52728 53512 535...
result:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%