5367 | 2023-04-26 16:17:03 | zsombor | Hegyi levegő | cpp17 | Time limit exceeded 90/100 | 3.108s | 523192 KiB |
#include <bits/stdc++.h>
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 | 25ms | 64372 KiB | ||||
2 | Accepted | 30ms | 64560 KiB | ||||
subtask2 | 19/19 | ||||||
3 | Accepted | 29ms | 65648 KiB | ||||
4 | Accepted | 28ms | 65732 KiB | ||||
5 | Accepted | 35ms | 65908 KiB | ||||
6 | Accepted | 35ms | 66120 KiB | ||||
7 | Accepted | 29ms | 66448 KiB | ||||
8 | Accepted | 28ms | 66556 KiB | ||||
9 | Accepted | 35ms | 66748 KiB | ||||
10 | Accepted | 30ms | 67480 KiB | ||||
subtask3 | 20/20 | ||||||
11 | Accepted | 25ms | 66252 KiB | ||||
12 | Accepted | 32ms | 67092 KiB | ||||
13 | Accepted | 57ms | 75152 KiB | ||||
14 | Accepted | 583ms | 175232 KiB | ||||
15 | Accepted | 1.33s | 314616 KiB | ||||
subtask4 | 20/20 | ||||||
16 | Accepted | 1.166s | 207812 KiB | ||||
17 | Accepted | 1.258s | 258972 KiB | ||||
18 | Accepted | 1.621s | 251316 KiB | ||||
19 | Accepted | 1.121s | 203628 KiB | ||||
20 | Accepted | 1.289s | 192108 KiB | ||||
subtask5 | 31/31 | ||||||
21 | Accepted | 1.093s | 293820 KiB | ||||
22 | Accepted | 1.106s | 215216 KiB | ||||
23 | Accepted | 855ms | 177696 KiB | ||||
24 | Accepted | 996ms | 177720 KiB | ||||
25 | Accepted | 1.294s | 283832 KiB | ||||
26 | Accepted | 1.149s | 225104 KiB | ||||
27 | Accepted | 1.297s | 351208 KiB | ||||
28 | Accepted | 1.2s | 370132 KiB | ||||
29 | Accepted | 1.449s | 351704 KiB | ||||
subtask6 | 0/10 | ||||||
30 | Time limit exceeded | 3.084s | 366912 KiB | ||||
31 | Time limit exceeded | 3.086s | 250564 KiB | ||||
32 | Time limit exceeded | 3.065s | 194196 KiB | ||||
33 | Time limit exceeded | 3.062s | 178412 KiB | ||||
34 | Time limit exceeded | 3.084s | 341604 KiB | ||||
35 | Time limit exceeded | 3.073s | 302636 KiB | ||||
36 | Accepted | 2.816s | 523192 KiB | ||||
37 | Time limit exceeded | 3.081s | 370548 KiB | ||||
38 | Time limit exceeded | 3.108s | 404632 KiB | ||||
39 | Time limit exceeded | 3.104s | 441712 KiB |