50262023-04-10 00:28:39rmlanHegyi levegőcpp14Wrong answer 0/1001.498s113592 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[100001][21]={0};
int mx[100001][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 find_set(p[v]);
}

bool unite(int a, int b){
    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 = 20; i >= 0; i--){
        if(na(b, cr+(1 << i)) != na(a, cr+(1<<i)-dis[b]+dis[a])){

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

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

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];
            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)) ek.push_back(pa);
        }else{

            if(unite(pa.second/10, pa.second/10-m)) ek.push_back(pa);
        }
    }
    for(pair<int, int> pa:ek){
        if(pa.second%10){
            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{
            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(n*m/2, i);
    }


    while(q--){
        int ai,bi,ci,di;
        cin >> ai >> bi >> ci >> di;

        cout << solve(ai*m+bi-m-1, ci*m+di-m-1) << endl;

    }

}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer3ms1824 KiB
2Wrong answer3ms2176 KiB
subtask20/19
3Wrong answer14ms4984 KiB
4Wrong answer16ms5112 KiB
5Wrong answer16ms5336 KiB
6Wrong answer16ms5376 KiB
7Wrong answer16ms5432 KiB
8Wrong answer16ms5560 KiB
9Wrong answer16ms5604 KiB
10Wrong answer14ms5980 KiB
subtask30/20
11Accepted3ms3240 KiB
12Wrong answer8ms4280 KiB
13Wrong answer59ms9508 KiB
14Wrong answer731ms63208 KiB
15Runtime error423ms98212 KiB
subtask40/20
16Runtime error310ms98048 KiB
17Runtime error317ms98200 KiB
18Runtime error477ms110076 KiB
19Runtime error446ms110608 KiB
20Runtime error485ms110512 KiB
subtask50/31
21Wrong answer1.289s63468 KiB
22Wrong answer1.407s60872 KiB
23Wrong answer1.498s58728 KiB
24Wrong answer1.445s58648 KiB
25Wrong answer1.233s63404 KiB
26Wrong answer1.281s58196 KiB
27Wrong answer1.225s58640 KiB
28Accepted1.498s63400 KiB
29Accepted670ms72224 KiB
subtask60/10
30Runtime error488ms98416 KiB
31Runtime error598ms110208 KiB
32Runtime error606ms111060 KiB
33Runtime error611ms110992 KiB
34Runtime error456ms98472 KiB
35Runtime error493ms107160 KiB
36Runtime error338ms113592 KiB
37Runtime error485ms110772 KiB
38Runtime error358ms98760 KiB
39Runtime error361ms102632 KiB