Submission details
Task:Island
Sender:Sebastian
Submission time:2026-04-16 13:33:54 +0300
Language:C++ (C++20)
Status:READY
Result:0
Feedback
subtaskverdictscore
#10
#20
#30
#40
#50
Test results
testverdicttimesubtask
#10.60 s1, 5details
#2ACCEPTED0.00 s1, 2, 3, 4, 5details
#30.72 s1, 5details
#40.71 s1, 5details
#50.79 s1, 3, 5details
#60.69 s1, 2, 4, 5details
#70.77 s1, 2, 4, 5details
#80.01 s1, 5details
#90.01 s1, 5details
#100.70 s1, 3, 4, 5details
#110.01 s1, 3, 5details
#12ACCEPTED0.01 s1, 4, 5details
#130.76 s1, 3, 4, 5details
#140.76 s1, 4, 5details
#150.01 s1, 5details
#160.66 s1, 5details
#170.82 s1, 5details
#180.80 s1, 5details
#190.21 s2, 4, 5details
#200.65 s2, 4, 5details
#210.49 s2, 4, 5details
#22ACCEPTED0.19 s2, 4, 5details
#230.83 s3, 5details
#240.83 s3, 5details
#250.79 s3, 5details
#260.19 s3, 5details
#270.79 s3, 4, 5details
#280.80 s3, 4, 5details
#290.73 s4, 5details
#300.77 s4, 5details
#31ACCEPTED0.53 s4, 5details
#320.81 s4, 5details
#33ACCEPTED0.35 s4, 5details
#340.76 s4, 5details
#350.86 s5details
#360.75 s5details
#370.20 s5details
#380.20 s5details
#390.76 s5details
#400.86 s5details
#410.84 s5details
#420.77 s5details
#430.76 s5details
#440.77 s5details

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef __int128_t i128;
#define all(x) begin(x), end(x)
#define sz(x) ((int)(x).size())
const ll inf = 4e18;

struct segtree {
    ll nt = 0;
    vector<ll> tree;

    segtree(vector<ll> &a) {
        while (sz(a) & (sz(a)-1)) a.push_back(0);
        nt = sz(a);
        tree = vector<ll>(2*nt);
        for (ll i = 0; i < nt; i++) tree[nt+i] = a[i];
        for (ll i = nt; i--;) tree[i] = max(tree[2*i], tree[2*i+1]);
    }

    ll first_gr(ll l, ll v) { ll res = inf; first_gr(1, 0, nt-1, l, v, res); return res; }
    bool first_gr(ll k, ll tl, ll tr, ll l, ll v, ll &res) {
        if (l > tr) return false;
        if (l <= tl && tree[k] <= v) return false;
        if (tl == tr) return res = tl, true;
        ll mid = tl + (tr - tl) / 2;
        return first_gr(2*k, tl, mid, l, v, res) || first_gr(2*k+1, mid+1, tr, l, v, res);
    }
};

void solve() {
    ll n, q; cin >> n >> q;
    vector<vector<bool>> grid(n, vector<bool>(n));
    vector<ll> ls(n, inf), rs(n, inf);
    for (ll i = 0; i < n; i++) {
        for (ll j = 0; j < n; j++) {
            char c; cin >> c;
            grid[i][j] = (c == '#');
            if (!grid[i][j]) continue;
            ls[i] = min(ls[i], j);
            rs[i] = -j;
        }
    }
    segtree lt(ls), rt(rs);
    
    map<pair<pll, pll>, ll> dist;
    auto get_dist = [&](auto &oink, pll a, pll b) -> ll {
        if (a > b) swap(a, b);
        if (dist.count({a, b})) return dist[{a, b}];
        if (a.first == b.first) return dist[{a, b}] = abs(a.second - b.second);
        ll i = lt.first_gr(a.first, a.second);
        ll j = rt.first_gr(a.first, -a.second);
        if (j < i) i = j;
        if (i >= b.first) return dist[{a, b}] = abs(a.first - b.first) + abs(a.second - b.second);
        pll p = (a.second < ls[i]) ? pll(i, ls[i]) : pll(i, -rs[n-1-i]);
        return dist[{a, b}] = oink(oink, p, b) + abs(a.first - p.first) + abs(a.second - p.second);
    };

    while (q--) {
        pll a, b;
        cin >> a.first >> a.second >> b.first >> b.second;
        a.first--; a.second--; b.first--; b.second--;
        cout << get_dist(get_dist, a, b) << '\n';
    }
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    solve();
}

Test details

Test 1

Subtask: 1, 5

Verdict:

input
8 4
........
..####..
.##.###.
.##.###.
...

correct output
5
0
17
3

user output
(empty)

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:

input
199 196
.................................

correct output
468
605
825
532
496
...

user output
(empty)

Test 4

Subtask: 1, 5

Verdict:

input
200 200
.................................

correct output
112
347
142
459
239
...

user output
(empty)

Test 5

Subtask: 1, 3, 5

Verdict:

input
200 200
.................................

correct output
381
544
94
532
98
...

user output
(empty)

Test 6

Subtask: 1, 2, 4, 5

Verdict:

input
200 200
.................................

correct output
133
73
81
82
53
...

user output
(empty)

Test 7

Subtask: 1, 2, 4, 5

Verdict:

input
200 200
.................................

correct output
139
52
101
14
144
...

user output
(empty)

Test 8

Subtask: 1, 5

Verdict:

input
200 200
.................................

correct output
236
555
878
632
829
...

user output
110
89
126
216
95
...

Feedback: Incorrect character on line 1 col 1: expected "236", got "110"

Test 9

Subtask: 1, 5

Verdict:

input
200 200
.................................

correct output
425
296
698
577
422
...

user output
187
142
100
169
120
...

Feedback: Incorrect character on line 1 col 1: expected "425", got "187"

Test 10

Subtask: 1, 3, 4, 5

Verdict:

input
200 200
.................................

correct output
1365
7284
11808
6136
9283
...

user output
(empty)

Test 11

Subtask: 1, 3, 5

Verdict:

input
200 200
.................................

correct output
6292
17954
16728
8938
1335
...

user output
44
98
168
112
87
...

Feedback: Incorrect character on line 1 col 1: expected "6292", got "44"

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:

input
200 200
.................................

correct output
19552
19544
19478
19402
19456
...

user output
(empty)

Test 14

Subtask: 1, 4, 5

Verdict:

input
200 200
.................................

correct output
17624
17515
17468
17689
17510
...

user output
(empty)

Test 15

Subtask: 1, 5

Verdict:

input
200 200
.................................

correct output
1584
1433
567
2248
1030
...

user output
112
115
105
44
58
...

Feedback: Incorrect character on line 1 col 2: expected "1584", got "112"

Test 16

Subtask: 1, 5

Verdict:

input
200 200
.................................

correct output
5872
6374
60
323
5311
...

user output
(empty)

Test 17

Subtask: 1, 5

Verdict:

input
200 200
.................................

correct output
1852
213
252
3861
1835
...

user output
(empty)

Test 18

Subtask: 1, 5

Verdict:

input
200 200
.................................

correct output
1564
2709
866
1318
1758
...

user output
(empty)

Test 19

Subtask: 2, 4, 5

Verdict:

input
997 100000
.................................

correct output
150
531
370
518
508
...

user output
150
531
370
518
508
...

Feedback: Incorrect character on line 100 col 1: expected "394", got "672"

Test 20

Subtask: 2, 4, 5

Verdict:

input
1000 100000
.................................

correct output
390
278
783
1269
249
...

user output
390
278
783
1269
249
...

Feedback: Incorrect character on line 9 col 1: expected "444", got "626"

Test 21

Subtask: 2, 4, 5

Verdict:

input
1000 100000
.................................

correct output
63
142
813
683
731
...

user output
63
142
813
683
731
...

Feedback: Incorrect character on line 8 col 1: expected "457", got "669"

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:

input
997 100000
.................................

correct output
714
2683
3699
2085
7850
...

user output
(empty)

Test 24

Subtask: 3, 5

Verdict:

input
1000 100000
.................................

correct output
5081
1819
1050
4610
528
...

user output
(empty)

Test 25

Subtask: 3, 5

Verdict:

input
1000 100000
.................................

correct output
3554
6322
6648
2882
1490
...

user output
(empty)

Test 26

Subtask: 3, 5

Verdict:

input
1000 100000
.................................

correct output
433976
81646
87810
48080
110879
...

user output
848
1280
950
320
233
...

Feedback: Incorrect character on line 1 col 1: expected "433976", got "848"

Test 27

Subtask: 3, 4, 5

Verdict:

input
1000 100000
.................................

correct output
207982
140036
208364
51912
56826
...

user output
(empty)

Test 28

Subtask: 3, 4, 5

Verdict:

input
1000 100000
.................................

correct output
497525
497563
498000
496804
497335
...

user output
(empty)

Test 29

Subtask: 4, 5

Verdict:

input
1000 100000
.................................

correct output
38580
2097
9795
38033
1639
...

user output
(empty)

Test 30

Subtask: 4, 5

Verdict:

input
1000 100000
.................................

correct output
33
20900
25028
1782
13599
...

user output
(empty)

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:

input
1000 100000
.................................

correct output
1378
1751
2274
250
811
...

user output
(empty)

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:

input
1000 100000
.................................

correct output
327286
447779
447534
448307
446997
...

user output
(empty)

Test 35

Subtask: 5

Verdict:

input
1000 100000
.................................

correct output
2597
1473
1933
2691
1837
...

user output
(empty)

Test 36

Subtask: 5

Verdict:

input
1000 100000
.................................

correct output
553
4357
3147
6951
1573
...

user output
(empty)

Test 37

Subtask: 5

Verdict:

input
1000 100000
.................................

correct output
1723
2039
1871
5638
4256
...

user output
253
1539
719
364
712
...

Feedback: Incorrect character on line 1 col 1: expected "1723", got "253"

Test 38

Subtask: 5

Verdict:

input
1000 100000
.................................

correct output
1546
704
2796
3802
1870
...

user output
932
366
386
928
332
...

Feedback: Incorrect character on line 1 col 1: expected "1546", got "932"

Test 39

Subtask: 5

Verdict:

input
1000 100000
.................................

correct output
3115
2042
2083
3227
740
...

user output
(empty)

Test 40

Subtask: 5

Verdict:

input
1000 100000
.................................

correct output
5222
3211
5230
1772
2310
...

user output
(empty)

Test 41

Subtask: 5

Verdict:

input
1000 100000
.................................

correct output
159214
68851
200821
141404
145704
...

user output
(empty)

Test 42

Subtask: 5

Verdict:

input
1000 100000
.................................

correct output
1843
25028
124430
84542
131339
...

user output
113
354
640
702
785
...

Test 43

Subtask: 5

Verdict:

input
1000 100000
.................................

correct output
111206
75799
12026
142133
20483
...

user output
(empty)

Test 44

Subtask: 5

Verdict:

input
1000 100000
.................................

correct output
20360
9075
12187
54923
54574
...

user output
(empty)