Submission #1261156

#TimeUsernameProblemLanguageResultExecution timeMemory
1261156SzymonKrzywdaPassport (JOI23_passport)C++20
0 / 100
2099 ms114448 KiB
#include <iostream>
#include <vector>
#include <queue>

using namespace std;
using pii = pair<int, int>;

const int base = 1 << 18;
const int MAXN = 2 * base;

int wygrana = 0;

vector<pair<int, int>> graf[MAXN];
vector<pair<int, int>> graf2[MAXN];

vector<int> dijkstra(int v, bool typ) {
    priority_queue<pii, vector<pii>, less<pii>> kolejka;
    kolejka.push({0, v});
    vector<int> wyniki(MAXN, 1e9);
    wyniki[v] = 0;
    while (!kolejka.empty()) {
        int v = kolejka.top().second;
        int cost = kolejka.top().first;
        kolejka.pop();
        if (wyniki[v] < cost) continue;
        
        if (typ == 0) {
            for (auto & [s, w] : graf[v]) {
                if (cost + w < wyniki[s]) {
                    wyniki[s] = cost + w;
                    kolejka.push({cost + w, s});
                }
            }
        }
        else {
            for (auto & [s, w] : graf2[v]) {
                if (cost + w < wyniki[s]) {
                    wyniki[s] = cost + w;
                    kolejka.push({cost + w, s});
                }
            }
        }
        
    }

    return wyniki;
}


void build(int v){
    if (v >= base) return;
    graf2[v * 2].push_back({v, 0});
    graf[v].push_back({v * 2, 0});
    graf2[v * 2 + 1].push_back({v, 0});
    graf[v].push_back({v * 2 + 1, 0});
    build(v * 2);
    build(v * 2 + 1);
}

void dodaj(int idx, int a, int b) {
    a += base - 1;
    b += base + 1;
    idx += base;

    while (a / 2 != b / 2) {
        if (a % 2 == 0) {
            //cout << "DODJAEA: " << idx << ' ' << a << ' ' << b << '\n';
            graf[idx].push_back({a + 1, 1});
            graf2[a + 1].push_back({idx, 1});
        }
        if (b % 2 == 1) {
            //cout << "DODJAEB: " << idx << ' ' << a << ' ' << b << '\n';
            graf[idx].push_back({b - 1, 1});
            graf2[b - 1].push_back({idx, 1});
        }
        a /= 2;
        b /= 2;
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, a, b;
    cin >> n;


    build(1);

    for (int i = 0; i < n; i++) {
        cin >> a >> b;
        a--;
        b--;
        dodaj(i, a, b);
    }

    vector<int> odl1 = dijkstra(base, 1);
    vector<int> odln = dijkstra(base + n - 1, 1);
    // for (int i = 0; i < MAXN; i++) {
    //     cout << i << ' ' << odl1[i] << ' ' << odln[i] << '\n';
    //     cout << i << ": ";
    //     for (auto & [s, w] : graf2[i]) cout << s << ' ';
    //     cout << '\n';
    // }
    for (int i = base; i < base + n; i++) {
        graf[i].push_back({wygrana, odl1[i] + odln[i]});
        graf2[wygrana].push_back({i, odl1[i] + odln[i]});
        //cout << "DODJAE WYGRANA: " << i << ' ' << wygrana << ' ' << odl1[i] + odln[i] << '\n';
    }

    vector<int> odp = dijkstra(wygrana, 1);

    int q, val;
    cin >> q;
    while (q--) {
        cin >> val;
        if (odp[base + val - 1] >= 1e9) cout << -1 << '\n';
        else cout << odp[base + val - 1] << '\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...