53642023-04-26 16:01:22zsomborHegyi levegőcpp17Time limit exceeded 90/1003.075s169792 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted32ms64412 KiB
2Accepted27ms64660 KiB
subtask219/19
3Accepted35ms65168 KiB
4Accepted35ms65032 KiB
5Accepted37ms65308 KiB
6Accepted37ms65456 KiB
7Accepted37ms65548 KiB
8Accepted30ms65780 KiB
9Accepted37ms65712 KiB
10Accepted32ms66328 KiB
subtask320/20
11Accepted30ms65652 KiB
12Accepted35ms65636 KiB
13Accepted75ms68012 KiB
14Accepted782ms88188 KiB
15Accepted2.128s119620 KiB
subtask420/20
16Accepted1.481s119112 KiB
17Accepted1.57s169792 KiB
18Accepted2.234s121812 KiB
19Accepted1.539s121188 KiB
20Accepted1.483s123072 KiB
subtask531/31
21Accepted1.871s118428 KiB
22Accepted1.636s108180 KiB
23Accepted1.205s110044 KiB
24Accepted1.409s110088 KiB
25Accepted1.598s108404 KiB
26Accepted1.546s109964 KiB
27Accepted2.362s116572 KiB
28Accepted1.873s116640 KiB
29Accepted1.861s116780 KiB
subtask60/10
30Time limit exceeded3.068s115580 KiB
31Time limit exceeded3.072s91940 KiB
32Time limit exceeded3.075s92992 KiB
33Time limit exceeded3.052s91860 KiB
34Time limit exceeded3.061s90884 KiB
35Time limit exceeded3.075s90464 KiB
36Time limit exceeded3.072s93316 KiB
37Time limit exceeded3.046s90012 KiB
38Time limit exceeded3.065s90404 KiB
39Time limit exceeded3.073s91076 KiB