53672023-04-26 16:17:03zsomborHegyi levegőcpp17Time limit exceeded 90/1003.108s523192 KiB
#include <bits/stdc++.h>
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].erase(i); }
        else qi[A].insert(i);
    }
    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] << "\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted25ms64372 KiB
2Accepted30ms64560 KiB
subtask219/19
3Accepted29ms65648 KiB
4Accepted28ms65732 KiB
5Accepted35ms65908 KiB
6Accepted35ms66120 KiB
7Accepted29ms66448 KiB
8Accepted28ms66556 KiB
9Accepted35ms66748 KiB
10Accepted30ms67480 KiB
subtask320/20
11Accepted25ms66252 KiB
12Accepted32ms67092 KiB
13Accepted57ms75152 KiB
14Accepted583ms175232 KiB
15Accepted1.33s314616 KiB
subtask420/20
16Accepted1.166s207812 KiB
17Accepted1.258s258972 KiB
18Accepted1.621s251316 KiB
19Accepted1.121s203628 KiB
20Accepted1.289s192108 KiB
subtask531/31
21Accepted1.093s293820 KiB
22Accepted1.106s215216 KiB
23Accepted855ms177696 KiB
24Accepted996ms177720 KiB
25Accepted1.294s283832 KiB
26Accepted1.149s225104 KiB
27Accepted1.297s351208 KiB
28Accepted1.2s370132 KiB
29Accepted1.449s351704 KiB
subtask60/10
30Time limit exceeded3.084s366912 KiB
31Time limit exceeded3.086s250564 KiB
32Time limit exceeded3.065s194196 KiB
33Time limit exceeded3.062s178412 KiB
34Time limit exceeded3.084s341604 KiB
35Time limit exceeded3.073s302636 KiB
36Accepted2.816s523192 KiB
37Time limit exceeded3.081s370548 KiB
38Time limit exceeded3.108s404632 KiB
39Time limit exceeded3.104s441712 KiB