48152023-03-31 12:47:16gortomiHegyi levegőcpp17Time limit exceeded 70/1003.066s234644 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)
    {
        p.assign(n + 1, 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
1Accepted3ms2028 KiB
2Accepted3ms2392 KiB
subtask219/19
3Accepted8ms4024 KiB
4Accepted10ms3904 KiB
5Accepted10ms4116 KiB
6Accepted12ms4216 KiB
7Accepted12ms4604 KiB
8Accepted12ms4660 KiB
9Accepted10ms5036 KiB
10Accepted8ms5752 KiB
subtask320/20
11Accepted2ms3716 KiB
12Accepted4ms4096 KiB
13Accepted23ms8128 KiB
14Accepted287ms46840 KiB
15Accepted1.733s164664 KiB
subtask40/20
16Accepted1.228s152048 KiB
17Time limit exceeded3.061s109768 KiB
18Accepted1.812s121396 KiB
19Accepted1.886s118760 KiB
20Accepted1.687s118016 KiB
subtask531/31
21Accepted913ms79968 KiB
22Accepted670ms63980 KiB
23Accepted675ms66840 KiB
24Accepted646ms66824 KiB
25Accepted402ms65292 KiB
26Accepted375ms56536 KiB
27Accepted301ms56744 KiB
28Accepted294ms60808 KiB
29Accepted279ms59216 KiB
subtask60/10
30Time limit exceeded3.062s136284 KiB
31Time limit exceeded3.065s106612 KiB
32Time limit exceeded3.062s107648 KiB
33Time limit exceeded3.066s105976 KiB
34Accepted2.595s234644 KiB
35Accepted2.903s215284 KiB
36Accepted1.608s179252 KiB
37Accepted1.184s172032 KiB
38Accepted1.23s205876 KiB
39Accepted1.23s198828 KiB