52962023-04-25 16:46:23gortomiHegyi levegőcpp17Time limit exceeded 70/1003.072s241948 KiB
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > p;
vector<vector<int> > r;
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)
    {
        if(r[a.first][a.second] > r[b.first][b.second]) swap(a, b);
        p[a.first][a.second] = b;
        if(r[a.first][a.second] == r[b.first][b.second]) r[a.first][a.second]++;
    }
}
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)));
    r.resize(n + 1, vector<int> (m + 1));
    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]]);
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1904 KiB
2Accepted3ms2232 KiB
subtask219/19
3Accepted8ms3812 KiB
4Accepted12ms3780 KiB
5Accepted10ms4024 KiB
6Accepted12ms4208 KiB
7Accepted13ms4408 KiB
8Accepted13ms4680 KiB
9Accepted12ms5076 KiB
10Accepted10ms6572 KiB
subtask320/20
11Accepted3ms3880 KiB
12Accepted4ms4204 KiB
13Accepted23ms7900 KiB
14Accepted331ms48568 KiB
15Accepted2.444s172748 KiB
subtask40/20
16Accepted1.273s159500 KiB
17Time limit exceeded3.068s130528 KiB
18Accepted1.96s125428 KiB
19Accepted1.947s122412 KiB
20Accepted1.986s121668 KiB
subtask531/31
21Accepted750ms90580 KiB
22Accepted609ms64436 KiB
23Accepted707ms67576 KiB
24Accepted708ms67532 KiB
25Accepted446ms66816 KiB
26Accepted377ms57012 KiB
27Accepted314ms57268 KiB
28Accepted289ms62040 KiB
29Accepted286ms60164 KiB
subtask60/10
30Time limit exceeded3.059s167472 KiB
31Time limit exceeded3.072s100296 KiB
32Time limit exceeded3.071s99548 KiB
33Time limit exceeded3.069s101008 KiB
34Accepted2.46s241948 KiB
35Time limit exceeded3.068s111920 KiB
36Accepted1.585s182736 KiB
37Accepted1.241s175548 KiB
38Accepted1.146s212968 KiB
39Accepted1.19s204224 KiB