| Task: | Island |
| Sender: | frederikvase |
| Submission time: | 2026-04-16 11:23:24 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | 34 |
| subtask | verdict | score |
|---|---|---|
| #1 | RUNTIME ERROR | 0 |
| #2 | ACCEPTED | 6 |
| #3 | RUNTIME ERROR | 0 |
| #4 | ACCEPTED | 28 |
| #5 | RUNTIME ERROR | 0 |
| test | verdict | time | subtask | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | 1, 5 | details |
| #2 | ACCEPTED | 0.00 s | 1, 2, 3, 4, 5 | details |
| #3 | ACCEPTED | 0.01 s | 1, 5 | details |
| #4 | ACCEPTED | 0.01 s | 1, 5 | details |
| #5 | ACCEPTED | 0.01 s | 1, 3, 5 | details |
| #6 | ACCEPTED | 0.01 s | 1, 2, 4, 5 | details |
| #7 | ACCEPTED | 0.01 s | 1, 2, 4, 5 | details |
| #8 | ACCEPTED | 0.01 s | 1, 5 | details |
| #9 | ACCEPTED | 0.01 s | 1, 5 | details |
| #10 | ACCEPTED | 0.01 s | 1, 3, 4, 5 | details |
| #11 | RUNTIME ERROR | 0.01 s | 1, 3, 5 | details |
| #12 | ACCEPTED | 0.01 s | 1, 4, 5 | details |
| #13 | ACCEPTED | 0.01 s | 1, 3, 4, 5 | details |
| #14 | ACCEPTED | 0.01 s | 1, 4, 5 | details |
| #15 | RUNTIME ERROR | 0.01 s | 1, 5 | details |
| #16 | RUNTIME ERROR | 0.01 s | 1, 5 | details |
| #17 | RUNTIME ERROR | 0.01 s | 1, 5 | details |
| #18 | RUNTIME ERROR | 0.01 s | 1, 5 | details |
| #19 | ACCEPTED | 0.22 s | 2, 4, 5 | details |
| #20 | ACCEPTED | 0.21 s | 2, 4, 5 | details |
| #21 | ACCEPTED | 0.20 s | 2, 4, 5 | details |
| #22 | ACCEPTED | 0.25 s | 2, 4, 5 | details |
| #23 | RUNTIME ERROR | 0.12 s | 3, 5 | details |
| #24 | RUNTIME ERROR | 0.12 s | 3, 5 | details |
| #25 | RUNTIME ERROR | 0.12 s | 3, 5 | details |
| #26 | RUNTIME ERROR | 0.16 s | 3, 5 | details |
| #27 | ACCEPTED | 0.24 s | 3, 4, 5 | details |
| #28 | ACCEPTED | 0.52 s | 3, 4, 5 | details |
| #29 | ACCEPTED | 0.25 s | 4, 5 | details |
| #30 | ACCEPTED | 0.25 s | 4, 5 | details |
| #31 | ACCEPTED | 0.28 s | 4, 5 | details |
| #32 | ACCEPTED | 0.25 s | 4, 5 | details |
| #33 | ACCEPTED | 0.25 s | 4, 5 | details |
| #34 | ACCEPTED | 0.52 s | 4, 5 | details |
| #35 | RUNTIME ERROR | 0.11 s | 5 | details |
| #36 | RUNTIME ERROR | 0.11 s | 5 | details |
| #37 | RUNTIME ERROR | 0.12 s | 5 | details |
| #38 | RUNTIME ERROR | 0.12 s | 5 | details |
| #39 | RUNTIME ERROR | 0.09 s | 5 | details |
| #40 | RUNTIME ERROR | 0.09 s | 5 | details |
| #41 | RUNTIME ERROR | 0.20 s | 5 | details |
| #42 | RUNTIME ERROR | 0.16 s | 5 | details |
| #43 | RUNTIME ERROR | 0.14 s | 5 | details |
| #44 | RUNTIME ERROR | 0.12 s | 5 | details |
Code
#include <bits/stdc++.h>
using namespace std;
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<string> g(n);
for (auto &s : g) cin >> s;
int t = -1;
vector<vector<int>> comp(n, vector<int>(n, -1));
vector<pair<int, int>> r;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) if (g[i][j] == '#') {
if (g[i][j - 1] == '.') {
comp[i][j] = ++t;
r.emplace_back(j, j);
}
else {
comp[i][j] = comp[i][j - 1];
r.back().second = j;
}
}
}
t++;
vector<set<int>> adj(t);
for (int i = 0; i + 1 < n; i++) {
for (int j = 0; j < n; j++) if (g[i][j] == '#' && g[i + 1][j] == '#') {
int u = comp[i][j], v = comp[i + 1][j];
adj[u].insert(v);
adj[v].insert(u);
}
}
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cerr << comp[i][j] << " \n"[j == n - 1];
}
}*/
vector<vector<int>> jmp(t, vector<int>(10));
vector<int> dpth(t);
auto dfs = [&](auto &oink, int u, int last) -> void {
jmp[u][0] = last;
for (int j = 0; j + 1 < 10; j++) {
jmp[u][j + 1] = jmp[jmp[u][j]][j];
}
for (int v : adj[u]) if (v != last) {
dpth[v] = dpth[u] + 1;
oink(oink, v, u);
}
};
dfs(dfs, 0, 0);
auto lca = [&](int u, int v) -> int {
if (u == v) return u;
if (dpth[u] < dpth[v]) swap(u, v);
for (int j = 10; j--; ) {
if (dpth[jmp[u][j]] >= dpth[v]) {
u = jmp[u][j];
}
}
assert(dpth[u] == dpth[v]);
if (u == v) return u;
for (int j = 10; j--; ) {
if (jmp[u][j] != jmp[v][j]) {
u = jmp[u][j];
v = jmp[v][j];
}
}
u = jmp[u][0];
v = jmp[v][0];
assert(u == v);
return u;
};
while (q--) {
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
r1--, c1--, r2--, c2--;
int u = comp[r1][c1];
int v = comp[r2][c2];
int l = lca(u, v);
int res = 0;
while (u != l) {
res++;
u = jmp[u][0];
auto [lo, hi] = r[u];
if (c1 > hi) {
res += c1 - hi;
c1 = hi;
} else if (c1 < lo) {
res += lo - c1;
c1 = lo;
}
}
while (v != l) {
res++;
v = jmp[v][0];
auto [lo, hi] = r[v];
if (c2 > hi) {
res += c2 - hi;
c2 = hi;
} else if (c2 < lo) {
res += lo - c2;
c2 = lo;
}
}
res += abs(c2 - c1);
cout << res << "\n";
}
return 0;
}
Test details
Test 1
Subtask: 1, 5
Verdict: ACCEPTED
| input |
|---|
| 8 4 ........ ..####.. .##.###. .##.###. ... |
| correct output |
|---|
| 5 0 17 3 |
| user output |
|---|
| 5 0 17 3 |
Test 2
Subtask: 1, 2, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 3 1 ... .#. ... 2 2 2 2 |
| correct output |
|---|
| 0 |
| user output |
|---|
| 0 |
Test 3
Subtask: 1, 5
Verdict: ACCEPTED
| input |
|---|
| 199 196 ................................. |
| correct output |
|---|
| 468 605 825 532 496 ... |
| user output |
|---|
| 468 605 825 532 496 ... |
Test 4
Subtask: 1, 5
Verdict: ACCEPTED
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 112 347 142 459 239 ... |
| user output |
|---|
| 112 347 142 459 239 ... |
Test 5
Subtask: 1, 3, 5
Verdict: ACCEPTED
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 381 544 94 532 98 ... |
| user output |
|---|
| 381 544 94 532 98 ... |
Test 6
Subtask: 1, 2, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 133 73 81 82 53 ... |
| user output |
|---|
| 133 73 81 82 53 ... |
Test 7
Subtask: 1, 2, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 139 52 101 14 144 ... |
| user output |
|---|
| 139 52 101 14 144 ... |
Test 8
Subtask: 1, 5
Verdict: ACCEPTED
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 236 555 878 632 829 ... |
| user output |
|---|
| 236 555 878 632 829 ... |
Test 9
Subtask: 1, 5
Verdict: ACCEPTED
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 425 296 698 577 422 ... |
| user output |
|---|
| 425 296 698 577 422 ... |
Test 10
Subtask: 1, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 1365 7284 11808 6136 9283 ... |
| user output |
|---|
| 1365 7284 11808 6136 9283 ... |
Test 11
Subtask: 1, 3, 5
Verdict: RUNTIME ERROR
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 6292 17954 16728 8938 1335 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 12
Subtask: 1, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 27 141 269 127 61 ... |
| user output |
|---|
| 27 141 269 127 61 ... |
Test 13
Subtask: 1, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 19552 19544 19478 19402 19456 ... |
| user output |
|---|
| 19552 19544 19478 19402 19456 ... |
Test 14
Subtask: 1, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 17624 17515 17468 17689 17510 ... |
| user output |
|---|
| 17624 17515 17468 17689 17510 ... |
Test 15
Subtask: 1, 5
Verdict: RUNTIME ERROR
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 1584 1433 567 2248 1030 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 16
Subtask: 1, 5
Verdict: RUNTIME ERROR
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 5872 6374 60 323 5311 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 17
Subtask: 1, 5
Verdict: RUNTIME ERROR
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 1852 213 252 3861 1835 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 18
Subtask: 1, 5
Verdict: RUNTIME ERROR
| input |
|---|
| 200 200 ................................. |
| correct output |
|---|
| 1564 2709 866 1318 1758 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 19
Subtask: 2, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 997 100000 ................................. |
| correct output |
|---|
| 150 531 370 518 508 ... |
| user output |
|---|
| 150 531 370 518 508 ... |
Test 20
Subtask: 2, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 390 278 783 1269 249 ... |
| user output |
|---|
| 390 278 783 1269 249 ... |
Test 21
Subtask: 2, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 63 142 813 683 731 ... |
| user output |
|---|
| 63 142 813 683 731 ... |
Test 22
Subtask: 2, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 949 876 1209 494 1033 ... |
| user output |
|---|
| 949 876 1209 494 1033 ... |
Test 23
Subtask: 3, 5
Verdict: RUNTIME ERROR
| input |
|---|
| 997 100000 ................................. |
| correct output |
|---|
| 714 2683 3699 2085 7850 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 24
Subtask: 3, 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 5081 1819 1050 4610 528 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 25
Subtask: 3, 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 3554 6322 6648 2882 1490 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 26
Subtask: 3, 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 433976 81646 87810 48080 110879 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 27
Subtask: 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 207982 140036 208364 51912 56826 ... |
| user output |
|---|
| 207982 140036 208364 51912 56826 ... |
Test 28
Subtask: 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 497525 497563 498000 496804 497335 ... |
| user output |
|---|
| 497525 497563 498000 496804 497335 ... |
Test 29
Subtask: 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 38580 2097 9795 38033 1639 ... |
| user output |
|---|
| 38580 2097 9795 38033 1639 ... |
Test 30
Subtask: 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 33 20900 25028 1782 13599 ... |
| user output |
|---|
| 33 20900 25028 1782 13599 ... |
Test 31
Subtask: 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 1421 1122 1840 834 443 ... |
| user output |
|---|
| 1421 1122 1840 834 443 ... |
Test 32
Subtask: 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 1378 1751 2274 250 811 ... |
| user output |
|---|
| 1378 1751 2274 250 811 ... |
Test 33
Subtask: 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 1126 886 544 223 272 ... |
| user output |
|---|
| 1126 886 544 223 272 ... |
Test 34
Subtask: 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 327286 447779 447534 448307 446997 ... |
| user output |
|---|
| 327286 447779 447534 448307 446997 ... |
Test 35
Subtask: 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 2597 1473 1933 2691 1837 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 36
Subtask: 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 553 4357 3147 6951 1573 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 37
Subtask: 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 1723 2039 1871 5638 4256 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:78: main()::<lambda(int, int)>: Assertion `u == v' failed.
Test 38
Subtask: 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 1546 704 2796 3802 1870 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 39
Subtask: 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 3115 2042 2083 3227 740 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 40
Subtask: 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 5222 3211 5230 1772 2310 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 41
Subtask: 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 159214 68851 200821 141404 145704 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 42
Subtask: 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 1843 25028 124430 84542 131339 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 43
Subtask: 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 111206 75799 12026 142133 20483 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
Test 44
Subtask: 5
Verdict: RUNTIME ERROR
| input |
|---|
| 1000 100000 ................................. |
| correct output |
|---|
| 20360 9075 12187 54923 54574 ... |
| user output |
|---|
| (empty) |
Error:
code: input/code.cpp:66: main()::<lambda(int, int)>: Assertion `dpth[u] == dpth[v]' failed.
