5364 | 2023-04-26 16:01:22 | zsombor | Hegyi levegő | cpp17 | Time limit exceeded 90/100 | 3.075s | 169792 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 32ms | 64412 KiB | ||||
2 | Accepted | 27ms | 64660 KiB | ||||
subtask2 | 19/19 | ||||||
3 | Accepted | 35ms | 65168 KiB | ||||
4 | Accepted | 35ms | 65032 KiB | ||||
5 | Accepted | 37ms | 65308 KiB | ||||
6 | Accepted | 37ms | 65456 KiB | ||||
7 | Accepted | 37ms | 65548 KiB | ||||
8 | Accepted | 30ms | 65780 KiB | ||||
9 | Accepted | 37ms | 65712 KiB | ||||
10 | Accepted | 32ms | 66328 KiB | ||||
subtask3 | 20/20 | ||||||
11 | Accepted | 30ms | 65652 KiB | ||||
12 | Accepted | 35ms | 65636 KiB | ||||
13 | Accepted | 75ms | 68012 KiB | ||||
14 | Accepted | 782ms | 88188 KiB | ||||
15 | Accepted | 2.128s | 119620 KiB | ||||
subtask4 | 20/20 | ||||||
16 | Accepted | 1.481s | 119112 KiB | ||||
17 | Accepted | 1.57s | 169792 KiB | ||||
18 | Accepted | 2.234s | 121812 KiB | ||||
19 | Accepted | 1.539s | 121188 KiB | ||||
20 | Accepted | 1.483s | 123072 KiB | ||||
subtask5 | 31/31 | ||||||
21 | Accepted | 1.871s | 118428 KiB | ||||
22 | Accepted | 1.636s | 108180 KiB | ||||
23 | Accepted | 1.205s | 110044 KiB | ||||
24 | Accepted | 1.409s | 110088 KiB | ||||
25 | Accepted | 1.598s | 108404 KiB | ||||
26 | Accepted | 1.546s | 109964 KiB | ||||
27 | Accepted | 2.362s | 116572 KiB | ||||
28 | Accepted | 1.873s | 116640 KiB | ||||
29 | Accepted | 1.861s | 116780 KiB | ||||
subtask6 | 0/10 | ||||||
30 | Time limit exceeded | 3.068s | 115580 KiB | ||||
31 | Time limit exceeded | 3.072s | 91940 KiB | ||||
32 | Time limit exceeded | 3.075s | 92992 KiB | ||||
33 | Time limit exceeded | 3.052s | 91860 KiB | ||||
34 | Time limit exceeded | 3.061s | 90884 KiB | ||||
35 | Time limit exceeded | 3.075s | 90464 KiB | ||||
36 | Time limit exceeded | 3.072s | 93316 KiB | ||||
37 | Time limit exceeded | 3.046s | 90012 KiB | ||||
38 | Time limit exceeded | 3.065s | 90404 KiB | ||||
39 | Time limit exceeded | 3.073s | 91076 KiB |