Submission #1176112

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

const int MAXN = 1e6 + 5;

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

vector<d> przedzialy;
int koszt[MAXN];
int reach[MAXN];

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 nxt[MAXN][22][3];

vector<int> jed;
vector<int> dwa;

int p1[MAXN];
int p2[MAXN];

int cl1(int a, int b){
    if(p1[b] <= a) return a;
    else return p1[b];
}

int cl2(int a, int b){
    if(p2[b] <= a) return a;
    else return p2[b];
}

bool comp(const pair<int,int> &p1, const pair<int,int> &p2){
    if(p1.second != p2.second) return p1.second < p2.second;
    if(p1.first != p2.first) return p1.first < p2.first;
    return false;
}

int jmp(int start, int a, int b){
    if(reach[start] >= a){
        if(p1[min(reach[start], b)] >= a) return 1;
        else return 2;
    }
    int w = 0;
    int curr = start;
    for(int i = 21; i >= 0; --i){
        if(reach[nxt[curr][i][2]] < a and nxt[curr][i][2] != curr and reach[nxt[curr][i][2]] != reach[curr]){
            w += (1 << i) + 1;
            curr = nxt[curr][i][2];
        }
        else if(reach[nxt[curr][i][1]] < a and nxt[curr][i][1] != curr and reach[nxt[curr][i][1]] != reach[curr]){
            w += (1 << i);
            curr = nxt[curr][i][1];
        }
        else if(reach[nxt[curr][i][0]] < a and nxt[curr][i][0] != curr and reach[nxt[curr][i][0]] != reach[curr]){
            w += (1 << i) - 1;
            curr = nxt[curr][i][0];
        }
    }
    //cout << curr << " po\n";
    int w2 = 1e9;
    int w1 = cl1(curr, reach[curr]);
    if(w1 != curr){
        if(p1[min(reach[w1], b)] >= a) w2 = min(w2, w + 2);
        if(p2[min(reach[w1], b)] >= a) w2 = min(w2, w + 3);
    }
    w1 = cl2(curr, reach[curr]);
    if(w1 != curr){
        if(p1[min(reach[w1], b)] >= a) w2 = min(w2, w + 3);
        if(p2[min(reach[w1], b)] >= a) w2 = min(w2, w + 4);
    }
    return w2;
}

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});
            }
        }
    }
    int c1 = -1, c2 = -1;
    for(int i = 1; i <= n; ++i){
        cin >> koszt[i];
        if(koszt[i] == 1){
            jed.push_back(i);
            c1 = i;
        }
        else{
            dwa.push_back(i);
            c2 = i;
        }
        p1[i] = c1;
        p2[i] = c2;
    }
    reach[n + 1] = 1e9;
    //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 l = 0; l <= 21; ++l){
        for(int i = 1; i <= n; ++i){
            nxt[i][l][0] = n+1;
            nxt[i][l][1] = n+1;
            nxt[i][l][2] = n+1;
        }
    }

    //jump pointery

    for(int i = n; i > 0; --i){
        int r = i;
        for(int j = 1; j <= m; ++j){
            r = max(r, przedzialy[kolorek[i][j]].ma);
        }
        reach[i] = r;
        nxt[i][0][0] = i;
        int w1 = cl1(i, r);
        int w2 = cl2(i, r);
        nxt[i][0][1] = w1;
        nxt[i][0][2] = w2;
        if(w1 == i) nxt[i][0][1] = 0;
        if(w2 == i) nxt[i][0][2] = 0;
        w2 = max(w2, nxt[w1][0][1]);
    }

    for(int l = 1; l <= 21; ++l){
        for(int i = 1; i <= n; ++i){
            nxt[i][l][0] = max(nxt[nxt[i][l-1][1]][l-1][0], nxt[nxt[i][l-1][0]][l-1][1]);
            nxt[i][l][1] = max({nxt[nxt[i][l-1][1]][l-1][1], nxt[nxt[i][l-1][2]][l-1][0], nxt[nxt[i][l-1][0]][l-1][2]});
            nxt[i][l][2] = max(nxt[nxt[i][l-1][1]][l-1][2], nxt[nxt[i][l-1][2]][l-1][1]);
        }
    }

    for(int i = 1; i <= n; ++i){
        for(int l = 0; l <= 21; ++l){
            for(int k = 0; k < 3; ++k){
                if(nxt[i][l][k] == 0) nxt[i][l][k] = n+1;
            }
        }
    }
    


    pair<int,int> a;
    for(int i = 1; i <= q; ++i){
        int r;
        cin >> r;
        if(r != 2) exit(0);
        set<int> num;
        for(int i = 0; i < r; ++i){
            cin >> a.first >> a.second;
            num.insert(kolorek[a.first][a.second]);
        }
        if(num.size() == 1){
            cout << 0 << "\n";
            continue;
        }
        vector<pair<int,int>> v;
        for(auto u : num){
            v.push_back({przedzialy[u].mi, przedzialy[u].ma});
        }
        sort(v.begin(),v.end(), comp);
        pair<int,int> para1 = v[0];
        pair<int,int> para2 = v[1];
        //cout << para1.first << " " << para1.second << " " << para2.first << " " << para2.second << " pary\n";
        
        if(para1.second >= para2.first){
            if(p1[para1.second] >= para2.first and p1[para1.second] >= para1.first) cout << 1 << "\n";
            else cout << 2 << "\n";
            continue;
        }
        int w = 1e9;

        if(p1[para1.second] >= para1.first){
            w = min(w, jmp(p1[para1.second], para2.first, para2.second) + 1);
        }
        if(p2[para1.second] >= para1.first){
            w = min(w, jmp(p2[para1.second], para2.first, para2.second) + 2);
        }
        

        if(w > 2 * n){
            cout << -1 << "\n";
        }
        else{
            cout << w << "\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...