5366 2023. 04. 26 16:11:52 zsombor Hegyi levegő cpp17 Time limit exceeded 90/100 3.095s 463788 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";
}
Subtask Sum Test Verdict Time Memory
subtask1 0/0
1 Accepted 32ms 64648 KiB
2 Accepted 30ms 64772 KiB
subtask2 19/19
3 Accepted 35ms 65692 KiB
4 Accepted 35ms 65788 KiB
5 Accepted 35ms 66148 KiB
6 Accepted 35ms 66092 KiB
7 Accepted 35ms 66088 KiB
8 Accepted 29ms 66428 KiB
9 Accepted 35ms 66560 KiB
10 Accepted 35ms 66996 KiB
subtask3 20/20
11 Accepted 32ms 65972 KiB
12 Accepted 28ms 66928 KiB
13 Accepted 64ms 74936 KiB
14 Accepted 586ms 174808 KiB
15 Accepted 1.546s 314192 KiB
subtask4 20/20
16 Accepted 962ms 207604 KiB
17 Accepted 1.039s 258700 KiB
18 Accepted 1.324s 250844 KiB
19 Accepted 1.22s 203128 KiB
20 Accepted 1.299s 191592 KiB
subtask5 31/31
21 Accepted 1.226s 293340 KiB
22 Accepted 1.167s 214676 KiB
23 Accepted 1.023s 177096 KiB
24 Accepted 1.023s 176984 KiB
25 Accepted 1.217s 283260 KiB
26 Accepted 1.029s 224600 KiB
27 Accepted 1.468s 350552 KiB
28 Accepted 1.389s 369548 KiB
29 Accepted 1.396s 351148 KiB
subtask6 0/10
30 Time limit exceeded 3.095s 360484 KiB
31 Time limit exceeded 3.082s 250196 KiB
32 Time limit exceeded 3.061s 193800 KiB
33 Accepted 2.805s 352312 KiB
34 Time limit exceeded 3.078s 336900 KiB
35 Time limit exceeded 3.075s 301004 KiB
36 Time limit exceeded 3.062s 263028 KiB
37 Time limit exceeded 3.094s 441196 KiB
38 Time limit exceeded 3.078s 463788 KiB
39 Time limit exceeded 3.085s 441680 KiB