47872023-03-31 12:26:28gortomiHegyi levegőcpp17Time limit exceeded 70/1003.084s234204 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);
    int n, m, q;
    cin >> 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++)
        {
            cin >> 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++)
    {
        cin >> 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++) cout << comp[r[i]] << "\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1896 KiB
2Accepted3ms2244 KiB
subtask219/19
3Accepted8ms3632 KiB
4Accepted10ms3684 KiB
5Accepted10ms3896 KiB
6Accepted12ms3988 KiB
7Accepted12ms4100 KiB
8Accepted12ms4384 KiB
9Accepted9ms4480 KiB
10Accepted8ms5496 KiB
subtask320/20
11Accepted3ms3372 KiB
12Accepted4ms3700 KiB
13Accepted21ms7696 KiB
14Accepted300ms46428 KiB
15Accepted1.758s164604 KiB
subtask40/20
16Accepted1.09s152088 KiB
17Time limit exceeded3.071s109560 KiB
18Accepted1.718s121276 KiB
19Accepted1.845s118796 KiB
20Accepted1.73s118028 KiB
subtask531/31
21Accepted753ms79848 KiB
22Accepted602ms63828 KiB
23Accepted633ms66768 KiB
24Accepted657ms67024 KiB
25Accepted393ms65728 KiB
26Accepted370ms56732 KiB
27Accepted294ms56832 KiB
28Accepted272ms61116 KiB
29Accepted266ms59912 KiB
subtask60/10
30Time limit exceeded3.065s134316 KiB
31Time limit exceeded3.084s106424 KiB
32Time limit exceeded3.068s108236 KiB
33Time limit exceeded3.052s101896 KiB
34Accepted2.328s234204 KiB
35Accepted2.971s215000 KiB
36Accepted1.536s179064 KiB
37Accepted1.144s171876 KiB
38Accepted1.087s205724 KiB
39Accepted1.12s198760 KiB