50352023-04-10 12:02:40rmlanHegyi levegőcpp14Time limit exceeded 70/1003.088s337328 KiB
#include<bits/stdc++.h>
using namespace std;

int n,m,q,timer=0;
int m2;
vector<vector<pair<int, int> > > g;
vector<pair<int, int> > e;
vector<int> p,sz,tin,tout,dis;
vector<bool> vis;
int anc[500001][21]={0};
int mx[500001][21]={0};

void make_set(int v){
    p[v]=v;
    sz[v]=1;
}

int find_set(int v){
    if(p[v] == v) return v;
    return p[v]=find_set(p[v]);
}

bool unite(int a, int b){
    if(n== 1) return 1;
    a = find_set(a);
    b = find_set(b);
    if(a == b) return 0;
    if(sz[a] < sz[b]) swap(a,b);
    p[b]=a;
    sz[a]+=sz[b];
    return 1;
}

int na(int u, int nn, int b=20){
    if(!nn) return u;
    for(int i = b; i >= 0; i--){
        if(nn-(1 << i) >= 0) return na(anc[u][i], nn-(1<<i), i);
    }
}
int nm(int u, int nn, int b=20){
    if(!nn) return 0;
    if(nn==1) return mx[u][0];
    for(int i = b; i >= 0; i--){
        if(nn-(1 << i) >= 0) return max(mx[u][i], nm(anc[u][i], nn-(1<<i), i));
    }
}


void dfs(int u, int it){

    if(vis[u]) return;
    tin[u]=timer++;
    vis[u]=1;
    if(it > 0){
        anc[u][it] = anc[anc[u][it-1]][it-1];
        mx[u][it]= max(mx[u][it-1], mx[anc[u][it-1]][it-1]);
    }
    for(pair<int, int> pr:g[u]){
        if(vis[pr.second]) continue;

        if(!it){
            dis[pr.second]=dis[u]+1;
            anc[pr.second][0]=u;
            mx[pr.second][0]=pr.first;
        }
        dfs(pr.second, it);
    }
    tout[u] = timer++;
}

int solve(int a, int b){
    if(dis[a] > dis[b]) swap(a,b);

    if(tin[a] < tin[b] && tout[a] > tout[b]){
        return nm(b, dis[b]-dis[a]);
    }
    int cr=0;
    for(int i = 19; i >= 0; i--){
        if(na(b, cr+(1 << i)+dis[b]-dis[a]) != na(a, cr+(1<<i))){

            cr +=(1<<i);
        }
    }
    cr++;

    return max(nm(b,cr+dis[b]-dis[a]), nm(a,cr));
}

int main(){

    cin >> n >> m >> q;
    int mp[n][m];

    g.resize(n*m);
    p.resize(n*m);
    sz.resize(n*m);
    tin.resize(n*m);
    tout.resize(n*m);
    dis.resize(n*m);
    vector<pair<int, int> > ek;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            //cin >> mp[i][j];
            scanf("%d", &mp[i][j]);
            if(i>0){
                e.push_back({max(mp[i][j], mp[i-1][j]), (i*m+j)*10});
            }
            if(j > 0){
                e.push_back({max(mp[i][j], mp[i][j-1]), (i*m+j)*10+1});
            }
        }
    }
    sort(e.begin(), e.end());
    for(int i = 0; i < n*m; i++){
        make_set(i);
    }
    for(pair<int, int> pa:e){
        if(pa.second%10){

            if(unite(pa.second/10, pa.second/10-1)){
                g[pa.second/10].push_back({pa.first, pa.second/10-1});
            g[pa.second/10-1].push_back({pa.first, pa.second/10});
            }
        }else{

            if(unite(pa.second/10, pa.second/10-m)){g[pa.second/10].push_back({pa.first, pa.second/10-m});
            g[pa.second/10-m].push_back({pa.first, pa.second/10});

            }
        }
    }
    anc[n*m/2][0]=n*m/2;
    dis[n*m/2]=0;
    for(int i = 0; i < 20; i++){
        vis.assign(n*m, 0);
        dfs(0, i);
    }


    while(q--){
        int ai,bi,ci,di;
        //cin >> ai >> bi >> ci >> di;
        scanf("%d %d %d %d",&ai, &bi, &ci, &di);
        cout << solve(ai*m+bi-m-1, ci*m+di-m-1) << endl;

    }

}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1884 KiB
2Accepted3ms2272 KiB
subtask219/19
3Accepted12ms5624 KiB
4Accepted14ms5380 KiB
5Accepted14ms5416 KiB
6Accepted14ms5576 KiB
7Accepted14ms5772 KiB
8Accepted14ms5852 KiB
9Accepted13ms5976 KiB
10Accepted10ms6652 KiB
subtask320/20
11Accepted3ms3428 KiB
12Accepted4ms4112 KiB
13Accepted34ms10164 KiB
14Accepted428ms69672 KiB
15Accepted2.154s332336 KiB
subtask40/20
16Accepted1.756s332488 KiB
17Accepted1.631s332596 KiB
18Time limit exceeded3.071s141708 KiB
19Accepted2.967s269076 KiB
20Accepted2.927s267760 KiB
subtask531/31
21Accepted887ms69892 KiB
22Accepted1.355s59360 KiB
23Accepted1.126s57056 KiB
24Accepted1.343s57080 KiB
25Accepted615ms70084 KiB
26Accepted1.039s57000 KiB
27Accepted1.032s57396 KiB
28Accepted688ms70548 KiB
29Accepted560ms71120 KiB
subtask60/10
30Time limit exceeded3.079s168208 KiB
31Time limit exceeded3.065s142016 KiB
32Time limit exceeded3.063s136588 KiB
33Time limit exceeded3.088s135940 KiB
34Accepted2.98s333060 KiB
35Time limit exceeded3.072s157508 KiB
36Time limit exceeded3.072s133200 KiB
37Time limit exceeded3.059s134088 KiB
38Accepted2.585s333548 KiB
39Accepted2.355s337328 KiB