4824 | 2023-03-31 12:49:57 | gortomi | Hegyi levegő | cpp17 | Time limit exceeded 70/100 | 3.076s | 233744 KiB |
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > p;
pair<int, int> get(pair<int, int> n)
{
return p[n.first][n.second] == make_pair(0, 0) ? n : p[n.first][n.second] = get(p[n.first][n.second]);
}
void unio(pair<int, int> a, pair<int, int> b)
{
a = get(a);
b = get(b);
if(a != b) p[a.first][a.second] = b;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
p.resize(n + 1, vector<pair<int, int> >(m + 1, make_pair(0, 0)));
vector<vector<int> > v(n + 2, vector<int>(m + 2, INT_MAX));
vector<int> comp;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
scanf("%d", &v[i][j]);
comp.push_back(v[i][j]);
}
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
vector<vector<pair<int, int> > > add(n * m + 1);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
v[i][j] = lower_bound(comp.begin(), comp.end(), v[i][j]) - comp.begin();
add[v[i][j]].push_back({i, j});
}
}
vector<int> l(q), r(q);
vector<pair<int, int> > a(q), b(q);
vector<vector<int> > que(n * m + 1);
for(int i = 0; i < q; i++)
{
scanf("%d %d %d %d", &a[i].first, &a[i].second, &b[i].first, &b[i].second);
l[i] = -1;
r[i] = n * m;
que[(l[i] + r[i]) / 2].push_back(i);
}
int db = 0;
while(db != q)
{
fill(p.begin(), p.end(), vector<pair<int, int> >(m + 1, make_pair(0, 0)));
for(int i = 0; i <= n * m; i++)
{
for(auto z : add[i])
{
int x = z.first, y = z.second;
if(v[x - 1][y] <= i) unio(z, {x - 1, y});
if(v[x + 1][y] <= i) unio(z, {x + 1, y});
if(v[x][y - 1] <= i) unio(z, {x, y - 1});
if(v[x][y + 1] <= i) unio(z, {x, y + 1});
}
for(auto x : que[i])
{
if(get(a[x]) == get(b[x])) r[x] = i;
else l[x] = i;
if(l[x] + 1 == r[x])
{
db++;
}
else
{
que[(l[x] + r[x]) / 2].push_back(x);
}
}
que[i].clear();
}
}
for(int i = 0; i < q; i++) printf("%d\n", comp[r[i]]);
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1708 KiB | ||||
2 | Accepted | 3ms | 1880 KiB | ||||
subtask2 | 19/19 | ||||||
3 | Accepted | 8ms | 3524 KiB | ||||
4 | Accepted | 10ms | 3528 KiB | ||||
5 | Accepted | 10ms | 3796 KiB | ||||
6 | Accepted | 12ms | 4012 KiB | ||||
7 | Accepted | 12ms | 4164 KiB | ||||
8 | Accepted | 12ms | 4448 KiB | ||||
9 | Accepted | 10ms | 4560 KiB | ||||
10 | Accepted | 8ms | 5564 KiB | ||||
subtask3 | 20/20 | ||||||
11 | Accepted | 3ms | 3584 KiB | ||||
12 | Accepted | 4ms | 3944 KiB | ||||
13 | Accepted | 23ms | 7928 KiB | ||||
14 | Accepted | 289ms | 46708 KiB | ||||
15 | Accepted | 2.2s | 164704 KiB | ||||
subtask4 | 0/20 | ||||||
16 | Accepted | 1.11s | 151784 KiB | ||||
17 | Time limit exceeded | 3.076s | 105288 KiB | ||||
18 | Accepted | 1.758s | 121152 KiB | ||||
19 | Accepted | 1.881s | 118264 KiB | ||||
20 | Accepted | 1.894s | 117536 KiB | ||||
subtask5 | 31/31 | ||||||
21 | Accepted | 910ms | 79460 KiB | ||||
22 | Accepted | 620ms | 63596 KiB | ||||
23 | Accepted | 685ms | 66464 KiB | ||||
24 | Accepted | 672ms | 66532 KiB | ||||
25 | Accepted | 428ms | 64896 KiB | ||||
26 | Accepted | 379ms | 55852 KiB | ||||
27 | Accepted | 307ms | 56276 KiB | ||||
28 | Accepted | 287ms | 60300 KiB | ||||
29 | Accepted | 279ms | 58736 KiB | ||||
subtask6 | 0/10 | ||||||
30 | Time limit exceeded | 3.075s | 133724 KiB | ||||
31 | Time limit exceeded | 3.069s | 106960 KiB | ||||
32 | Time limit exceeded | 3.039s | 101028 KiB | ||||
33 | Time limit exceeded | 3.069s | 101096 KiB | ||||
34 | Accepted | 2.857s | 233744 KiB | ||||
35 | Accepted | 2.91s | 214404 KiB | ||||
36 | Accepted | 1.569s | 178500 KiB | ||||
37 | Accepted | 1.256s | 171268 KiB | ||||
38 | Accepted | 1.222s | 205096 KiB | ||||
39 | Accepted | 1.22s | 198312 KiB |