Submission #1370435

#TimeUsernameProblemLanguageResultExecution timeMemory
1370435pirmyratgPassport (JOI23_passport)C++20
100 / 100
464 ms127480 KiB
#include "bits/stdc++.h"
using namespace std;

const int N = 6000005;
long long n;
vector<pair<long long, long long>> z[N];
long long cnt[N][3];
long long bruh = 1e16;

void update(int id, int l, int r, int x, int y, int t){
    if(x > r || y < l) return;
    if(l >= x && y >= r){
        z[id + n].push_back({t, 1});
        return;
    }
    int mid = (l + r) / 2;
    update(id * 2, l, mid, x, y, t);
    update(id * 2 + 1, mid + 1, r, x, y, t);
}

void build(int id, int l, int r){
    if(l == r){
        z[l].push_back({id + n, 0});
        return;
    }
    int mid = (l + r) / 2;
    build(id * 2, l, mid);
    build(id * 2 + 1, mid + 1, r);
    z[id * 2 + n].push_back({id + n, 0});
    z[id * 2 + 1 + n].push_back({id + n, 0});
}

void bfs(int sta, int id){
    deque<int> q;
    q.push_front(sta);
    cnt[sta][id] = 0;
    while(q.size()){
        int pos = q.front();
        q.pop_front();
        for(auto p : z[pos]){
            if(cnt[p.first][id] == bruh){
                cnt[p.first][id] = p.second + cnt[pos][id];
                if(p.second) 
                	q.push_back(p.first);
                else 
                	q.push_front(p.first);
            }
        }
    }
}

void dijkstra(){
    priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> q;
    for(int i = 1; i <= n; ++i){
        if(cnt[i][2] < bruh)
            q.push({cnt[i][2], i});
    }
    while(q.size()){
        pair<long long, int> pos = q.top();
        q.pop();
        long long val = pos.first;
        int pos1 = pos.second;
        if(val > cnt[pos1][2]) 
        	continue;
        for(auto p : z[pos1]){
            if(cnt[p.first][2] > val + p.second){
                cnt[p.first][2] = val + p.second;
                q.push({cnt[p.first][2], (int)p.first});
            }
        }
    }
}

int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i){
        int x, y;
        scanf("%d%d", &x, &y);
        update(1, 1, n, x, y, i);
    }
    build(1, 1, n);
    for(int i = 1; i <= n + n * 4; ++i){
        for(int j = 0; j <= 2; ++j)
            cnt[i][j] = bruh;
    }
    bfs(1, 0);
    bfs(n, 1);
    for(int i = 1; i <= n; ++i){
        if(cnt[i][1] + cnt[i][0] >= bruh) 
        	continue;
        cnt[i][2] = cnt[i][1] + cnt[i][0] - (i != 1 && i != n);
    }
    dijkstra();
    int c;
    scanf("%d", &c);
    while(c--){
        int x;
        scanf("%d", &x);
        if(cnt[x][2] >= bruh) 
        	puts("-1");
        else 
        printf("%lld\n", cnt[x][2]);
    }
    return 0;
}

Compilation message (stderr)

passport.cpp: In function 'int main()':
passport.cpp:75:13: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
   75 |     scanf("%d", &n);
      |            ~^   ~~
      |             |   |
      |             |   long long int*
      |             int*
      |            %lld
passport.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
passport.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         scanf("%d%d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~
passport.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |     scanf("%d", &c);
      |     ~~~~~^~~~~~~~~~
passport.cpp:98:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |         scanf("%d", &x);
      |         ~~~~~^~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...