5365 2023. 04. 26 16:08:26 zsombor Hegyi levegő cpp17 Time limit exceeded 90/100 3.108s 464148 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;
}
Subtask Sum Test Verdict Time Memory
subtask1 0/0
1 Accepted 32ms 64372 KiB
2 Accepted 27ms 64892 KiB
subtask2 19/19
3 Accepted 30ms 65504 KiB
4 Accepted 30ms 65700 KiB
5 Accepted 29ms 65944 KiB
6 Accepted 29ms 66144 KiB
7 Accepted 35ms 66184 KiB
8 Accepted 35ms 66284 KiB
9 Accepted 37ms 66624 KiB
10 Accepted 37ms 67356 KiB
subtask3 20/20
11 Accepted 30ms 66108 KiB
12 Accepted 34ms 66768 KiB
13 Accepted 74ms 74816 KiB
14 Accepted 746ms 174924 KiB
15 Accepted 1.782s 314528 KiB
subtask4 20/20
16 Accepted 1.539s 207648 KiB
17 Accepted 1.3s 258616 KiB
18 Accepted 1.949s 250824 KiB
19 Accepted 1.633s 202944 KiB
20 Accepted 1.628s 191332 KiB
subtask5 31/31
21 Accepted 1.473s 292992 KiB
22 Accepted 1.371s 214644 KiB
23 Accepted 1.274s 176936 KiB
24 Accepted 1.243s 176940 KiB
25 Accepted 1.496s 283152 KiB
26 Accepted 1.271s 224456 KiB
27 Accepted 1.804s 350616 KiB
28 Accepted 1.636s 369588 KiB
29 Accepted 1.741s 351400 KiB
subtask6 0/10
30 Time limit exceeded 3.105s 363464 KiB
31 Time limit exceeded 3.072s 250188 KiB
32 Time limit exceeded 3.063s 193828 KiB
33 Time limit exceeded 3.108s 178240 KiB
34 Time limit exceeded 3.075s 341412 KiB
35 Time limit exceeded 3.092s 322344 KiB
36 Time limit exceeded 3.069s 263372 KiB
37 Time limit exceeded 3.107s 372876 KiB
38 Time limit exceeded 3.063s 464148 KiB
39 Time limit exceeded 3.101s 412744 KiB