5363 2023. 04. 26 15:29:11 zsombor Hegyi levegő cpp17 Wrong answer 0/100 3.063s 227684 KiB
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

int n, m, q, x, y;
vector <vector <int>> h;
vector <pair<int, pair<int, int>>> srt;
vector <int> p(5e5, 0);
vector <int> sz(5e5, 1);
vector <int> ind(5e5, 0);
vector <set <int>> qi(5e5);
vector <int> ans(5e5, 0);

int holvan(int a) {
    return (a == p[a] ? a : p[a] = holvan(p[a]));
}

void unio(int a, int b) {
    a = holvan(a);
    b = holvan(b);
    if (a == b) return;
    if (sz[a] < sz[b]) swap(a, b);
    p[b] = a;
    sz[a] += sz[b];

    int A = ind[a], B = ind[b];
    if (qi[A].size() < qi[B].size()) swap(A, B);
    for (int i : qi[b]) {
        if (qi[A].count(i)) ans[i] = h[x][y];
        qi[A].insert(i);
    }
    qi[B].clear();
    ind[a] = A;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m >> q;
    h.resize(n);
    for (int i = 0; i < n; i++) {
        h[i].resize(m);
        for (int j = 0; j < m; j++) {
            cin >> h[i][j];
            srt.push_back({ h[i][j],{i,j} });
            p[i * m + j] = ind[i * m + j] = i * m + j;
        }
    }
    sort(srt.begin(), srt.end());
    for (int i = 0; i < q; i++) {
        cin >> x >> y; x--; y--;
        qi[x * m + y].insert(i);
        cin >> x >> y; x--; y--;
        qi[x * m + y].insert(i);
    }
    for (int i = 0; i < n * m; i++) {
        x = srt[i].second.first;
        y = srt[i].second.second;
        if (x && h[x][y] >= h[x - 1][y]) unio(x * m + y, (x - 1) * m + y);
        if (x < n - 1 && h[x][y] >= h[x + 1][y]) unio(x * m + y, (x + 1) * m + y);
        if (y && h[x][y] >= h[x][y - 1]) unio(x * m + y, x * m + y - 1);
        if (y < m - 1 && h[x][y] >= h[x][y + 1]) unio(x * m + y, x * m + y + 1);
    }
    for (int i = 0; i < q; i++) cout << ans[i] << endl;
}
Subtask Sum Test Verdict Time Memory
subtask1 0/0
1 Accepted 26ms 64376 KiB
2 Wrong answer 25ms 64856 KiB
subtask2 0/19
3 Wrong answer 35ms 65268 KiB
4 Wrong answer 29ms 65352 KiB
5 Wrong answer 35ms 65584 KiB
6 Wrong answer 29ms 65816 KiB
7 Wrong answer 28ms 66024 KiB
8 Wrong answer 28ms 65984 KiB
9 Wrong answer 35ms 66188 KiB
10 Wrong answer 37ms 66836 KiB
subtask3 0/20
11 Accepted 27ms 66184 KiB
12 Wrong answer 28ms 66572 KiB
13 Wrong answer 57ms 68852 KiB
14 Wrong answer 368ms 88720 KiB
15 Wrong answer 931ms 119996 KiB
subtask4 0/20
16 Wrong answer 908ms 119940 KiB
17 Wrong answer 1.069s 170588 KiB
18 Wrong answer 1.12s 119748 KiB
19 Wrong answer 1.169s 120008 KiB
20 Wrong answer 1.192s 120164 KiB
subtask5 0/31
21 Wrong answer 814ms 117852 KiB
22 Wrong answer 875ms 107920 KiB
23 Wrong answer 916ms 107836 KiB
24 Wrong answer 910ms 107836 KiB
25 Wrong answer 839ms 107920 KiB
26 Wrong answer 1.024s 107984 KiB
27 Wrong answer 648ms 108032 KiB
28 Wrong answer 615ms 107988 KiB
29 Wrong answer 882ms 107856 KiB
subtask6 0/10
30 Wrong answer 2.71s 227684 KiB
31 Wrong answer 2.329s 176784 KiB
32 Time limit exceeded 3.063s 90340 KiB
33 Wrong answer 2.4s 176900 KiB
34 Wrong answer 2.178s 177076 KiB
35 Wrong answer 2.473s 177240 KiB
36 Wrong answer 2.269s 176988 KiB
37 Wrong answer 2.44s 177076 KiB
38 Wrong answer 2.121s 177352 KiB
39 Wrong answer 2.223s 177300 KiB