53662023-04-26 16:11:52zsomborHegyi levegőcpp17Time limit exceeded 90/1003.095s463788 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] << "\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted32ms64648 KiB
2Accepted30ms64772 KiB
subtask219/19
3Accepted35ms65692 KiB
4Accepted35ms65788 KiB
5Accepted35ms66148 KiB
6Accepted35ms66092 KiB
7Accepted35ms66088 KiB
8Accepted29ms66428 KiB
9Accepted35ms66560 KiB
10Accepted35ms66996 KiB
subtask320/20
11Accepted32ms65972 KiB
12Accepted28ms66928 KiB
13Accepted64ms74936 KiB
14Accepted586ms174808 KiB
15Accepted1.546s314192 KiB
subtask420/20
16Accepted962ms207604 KiB
17Accepted1.039s258700 KiB
18Accepted1.324s250844 KiB
19Accepted1.22s203128 KiB
20Accepted1.299s191592 KiB
subtask531/31
21Accepted1.226s293340 KiB
22Accepted1.167s214676 KiB
23Accepted1.023s177096 KiB
24Accepted1.023s176984 KiB
25Accepted1.217s283260 KiB
26Accepted1.029s224600 KiB
27Accepted1.468s350552 KiB
28Accepted1.389s369548 KiB
29Accepted1.396s351148 KiB
subtask60/10
30Time limit exceeded3.095s360484 KiB
31Time limit exceeded3.082s250196 KiB
32Time limit exceeded3.061s193800 KiB
33Accepted2.805s352312 KiB
34Time limit exceeded3.078s336900 KiB
35Time limit exceeded3.075s301004 KiB
36Time limit exceeded3.062s263028 KiB
37Time limit exceeded3.094s441196 KiB
38Time limit exceeded3.078s463788 KiB
39Time limit exceeded3.085s441680 KiB