Submission #1152144

#TimeUsernameProblemLanguageResultExecution timeMemory
1152144jakubmz2Road Service 2 (JOI24_ho_t5)C++20
27 / 100
338 ms159796 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pi pair<int,int>

struct d{
    int mi = 1e9;
    int ma = 0;
};

vector<d> przedzialy;

const int MAXN = 1e6 + 5;

int ile[22];
int koszt[MAXN];
int nast[MAXN][22];

void dfs(int a, int b, int nr, vector<vector<int>> &kolorek, vector<vector<vector<pair<int,int>>>> &graf){
    if(kolorek[a][b] != -1) return;
    kolorek[a][b] = nr;
    przedzialy[nr].mi = min(przedzialy.back().mi, a);
    przedzialy[nr].ma = max(przedzialy.back().ma, a);
    for(auto u : graf[a][b]){
        if(kolorek[u.first][u.second] == -1) dfs(u.first, u.second, nr, kolorek, graf);
    }
    return;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> kolorek;
    vector<vector<vector<pair<int,int>>>> graf;
    kolorek.resize(n + 1);
    graf.resize(n + 1);
    for(int i = 0; i <= n; ++i){
        kolorek[i].resize(m + 1);
        graf[i].resize(m + 1);
    }
    for(int i = 0; i <= n; ++i){
        for(int j = 0; j <= m; ++j){
            kolorek[i][j] = -1;
            graf[i][j].clear();
        }
    }  
    char x;
    string s;
    for(int i = 1; i <= n; ++i){
        cin >> s;
        for(int j = 1; j < m; ++j){
            x = s[j-1];
            //cout << x << " x\n";
            if(x == '1') {
                graf[i][j].push_back({i,j+1});
                graf[i][j+1].push_back({i,j});
            }
        }
    }
    //cout << "#\n";
    for(int i = 1; i < n; ++i){
        cin >> s;
        for(int j = 1; j <= m; ++j){
            x = s[j-1];
            if(x == '1') {
                graf[i][j].push_back({i + 1, j});
                graf[i+1][j].push_back({i,j});
            }
        }
    }
    //cout << "$\n";
    int nr = 1;
    przedzialy.push_back((d){1000000000, 0});
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= m; ++j){
            if(kolorek[i][j] == -1){
                przedzialy.push_back((d){i, i});
                dfs(i, j, nr, kolorek, graf);
                nr++;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        for(int l = 1; l <= 21; ++l){
            nast[i][l] = 1e9;
        }
    }
    for(int i = 1; i <= n; ++i){
        nast[i][0] = i;
        for(int j = 1; j <= m; ++j){
            nast[i][0] = max(nast[i][0], przedzialy[kolorek[i][j]].ma);
        }
    }
    for(int l = 1; l <= 21; ++l){
        for(int i = 1; i <= n; ++i){
            nast[i][l] = nast[nast[i][l - 1]][l - 1];
        }
    }
    for(int i = 1; i <= n; ++i){
        cin >> koszt[i];
        if(koszt[i] != 1) exit(0);
    }
    // for(int i = 1; i <= n; ++i){
    //     for(int j = 1; j <= m; ++j){
    //         cout << i << " " << j << " " << "ij\n";
    //         cout << kolorek[i][j] << "\n";
    //     }
    // }
    // for(int i = 1; i < przedzialy.size(); ++i){
    //     cout << i << " " << przedzialy[i].mi << " " << przedzialy[i].ma << " p\n";
    // }
    pi a, b;
    int r;
    for(int i = 0; i < q; ++i){
        cin >> r;
        //cout << r << " r\n";
        if(r != 2) cout << "HU\n";
        cin >> a.first >> a.second >> b.first >> b.second;
        //cout << a.first << " " << a.second << " " << b.first << " " << b.second << " ab\n";
        if(a.first >= b.first){
            swap(a.first, b.first);
            swap(a.second, b.second);
        }
        if(kolorek[a.first][a.second] == kolorek[b.first][b.second]){
            cout << 0 << "\n";
            continue;
        }
        a.first = przedzialy[kolorek[a.first][a.second]].ma;
        b.first = przedzialy[kolorek[b.first][b.second]].mi;
        //cout << a.first << " " << b.first << " first\n";
        if(a.first >= b.first){
            cout << 1 << "\n";
            continue;
        }

        ll w = 0;
        for(ll l = 21; l >= 0; --l){
            if(nast[a.first][l] >= b.first or nast[a.first][l] == -1) continue;
            w += (1 << l);
            //cout << w << " w\n";
            a.first = nast[a.first][l];
        }
        if(w == (1 << 22) - 1) cout << -1 << "\n";
        else cout << w + 2 << "\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...