53632023-04-26 15:29:11zsomborHegyi levegőcpp17Wrong answer 0/1003.063s227684 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
1Accepted26ms64376 KiB
2Wrong answer25ms64856 KiB
subtask20/19
3Wrong answer35ms65268 KiB
4Wrong answer29ms65352 KiB
5Wrong answer35ms65584 KiB
6Wrong answer29ms65816 KiB
7Wrong answer28ms66024 KiB
8Wrong answer28ms65984 KiB
9Wrong answer35ms66188 KiB
10Wrong answer37ms66836 KiB
subtask30/20
11Accepted27ms66184 KiB
12Wrong answer28ms66572 KiB
13Wrong answer57ms68852 KiB
14Wrong answer368ms88720 KiB
15Wrong answer931ms119996 KiB
subtask40/20
16Wrong answer908ms119940 KiB
17Wrong answer1.069s170588 KiB
18Wrong answer1.12s119748 KiB
19Wrong answer1.169s120008 KiB
20Wrong answer1.192s120164 KiB
subtask50/31
21Wrong answer814ms117852 KiB
22Wrong answer875ms107920 KiB
23Wrong answer916ms107836 KiB
24Wrong answer910ms107836 KiB
25Wrong answer839ms107920 KiB
26Wrong answer1.024s107984 KiB
27Wrong answer648ms108032 KiB
28Wrong answer615ms107988 KiB
29Wrong answer882ms107856 KiB
subtask60/10
30Wrong answer2.71s227684 KiB
31Wrong answer2.329s176784 KiB
32Time limit exceeded3.063s90340 KiB
33Wrong answer2.4s176900 KiB
34Wrong answer2.178s177076 KiB
35Wrong answer2.473s177240 KiB
36Wrong answer2.269s176988 KiB
37Wrong answer2.44s177076 KiB
38Wrong answer2.121s177352 KiB
39Wrong answer2.223s177300 KiB