53652023-04-26 16:08:26zsomborHegyi levegőcpp17Time limit exceeded 90/1003.108s464148 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].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] << endl;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted32ms64372 KiB
2Accepted27ms64892 KiB
subtask219/19
3Accepted30ms65504 KiB
4Accepted30ms65700 KiB
5Accepted29ms65944 KiB
6Accepted29ms66144 KiB
7Accepted35ms66184 KiB
8Accepted35ms66284 KiB
9Accepted37ms66624 KiB
10Accepted37ms67356 KiB
subtask320/20
11Accepted30ms66108 KiB
12Accepted34ms66768 KiB
13Accepted74ms74816 KiB
14Accepted746ms174924 KiB
15Accepted1.782s314528 KiB
subtask420/20
16Accepted1.539s207648 KiB
17Accepted1.3s258616 KiB
18Accepted1.949s250824 KiB
19Accepted1.633s202944 KiB
20Accepted1.628s191332 KiB
subtask531/31
21Accepted1.473s292992 KiB
22Accepted1.371s214644 KiB
23Accepted1.274s176936 KiB
24Accepted1.243s176940 KiB
25Accepted1.496s283152 KiB
26Accepted1.271s224456 KiB
27Accepted1.804s350616 KiB
28Accepted1.636s369588 KiB
29Accepted1.741s351400 KiB
subtask60/10
30Time limit exceeded3.105s363464 KiB
31Time limit exceeded3.072s250188 KiB
32Time limit exceeded3.063s193828 KiB
33Time limit exceeded3.108s178240 KiB
34Time limit exceeded3.075s341412 KiB
35Time limit exceeded3.092s322344 KiB
36Time limit exceeded3.069s263372 KiB
37Time limit exceeded3.107s372876 KiB
38Time limit exceeded3.063s464148 KiB
39Time limit exceeded3.101s412744 KiB