Submission #873003

# Submission time Handle Problem Language Result Execution time Memory
873003 2023-11-14T09:31:02 Z boris_mihov Passport (JOI23_passport) C++17
0 / 100
1783 ms 1048576 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>

typedef long long llong;
const int MAXN = 200000 + 10;
const int INF = 1e9;

int n;
int queries;
int l[MAXN];
int r[MAXN];
int dist[MAXN];
int distA[MAXN];
int distB[MAXN];
std::vector <int> g[MAXN];
std::vector <int> rev[MAXN];
std::queue <int> q[2 * MAXN];
std::queue <int> bfsQueue;
bool vis[MAXN];

void solve()
{
    for (int i = 1 ; i <= n ; ++i)
    {
        for (int j = l[i] ; j <= r[i] ; ++j)
        {
            // std::cout << "edge: " << i << ' ' << j << '\n';
            g[i].push_back(j);
            rev[j].push_back(i);
        }
    }

    std::fill(distA + 1, distA + 1 + n, INF);
    std::fill(distB + 1, distB + 1 + n, INF);
    bfsQueue.push(1);
    distA[1] = 0;

    while (!bfsQueue.empty())
    {
        int top = bfsQueue.front();
        bfsQueue.pop();

        for (const int &u : rev[top])
        {
            if (distA[u] > distA[top] + 1)
            {
                distA[u] = distA[top] + 1;
                bfsQueue.push(u);
            }
        }   
    }

    bfsQueue.push(n);
    distB[n] = 0;

    while (!bfsQueue.empty())
    {
        int top = bfsQueue.front();
        bfsQueue.pop();

        for (const int &u : rev[top])
        {
            if (distB[u] > distB[top] + 1)
            {
                distB[u] = distB[top] + 1;
                bfsQueue.push(u);
            }
        }   
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        dist[i] = distA[i] + distB[i];
        if (l[i] == 1 && r[i] == n) dist[i] = 1;
        if (dist[i] <= 2 * n) q[dist[i]].push(i);
    }

    for (int d = 0 ; d <= n ; ++d)
    {
        while (!q[d].empty())
        {
            int top = q[d].front();
            q[d].pop();

            if (vis[top])
            {
                continue;
            }

            vis[top] = true;
            for (const int &u : rev[top])
            {
                if (dist[u] > dist[top] + 1)
                {
                    dist[u] = dist[top] + 1;
                    q[dist[u]].push(u);
                }
            }
        }
    }

    for (int i = 1 ; i <= queries ; ++i)
    {
        int node;
        std::cin >> node;
        if (dist[node] <= n) std::cout << dist[node] << '\n';
        else std::cout << -1 << '\n';
    }
}

void input()
{
    std::cin >> n;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> l[i] >> r[i];
    }

    std::cin >> queries;
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 152 ms 281452 KB Output is correct
2 Correct 165 ms 281408 KB Output is correct
3 Correct 150 ms 281448 KB Output is correct
4 Runtime error 1783 ms 1048576 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 142 ms 281424 KB Output is correct
2 Correct 178 ms 281376 KB Output is correct
3 Correct 142 ms 281468 KB Output is correct
4 Correct 142 ms 281424 KB Output is correct
5 Correct 142 ms 281256 KB Output is correct
6 Correct 151 ms 281508 KB Output is correct
7 Correct 141 ms 281428 KB Output is correct
8 Incorrect 143 ms 281428 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 142 ms 281424 KB Output is correct
2 Correct 178 ms 281376 KB Output is correct
3 Correct 142 ms 281468 KB Output is correct
4 Correct 142 ms 281424 KB Output is correct
5 Correct 142 ms 281256 KB Output is correct
6 Correct 151 ms 281508 KB Output is correct
7 Correct 141 ms 281428 KB Output is correct
8 Incorrect 143 ms 281428 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 142 ms 281424 KB Output is correct
2 Correct 178 ms 281376 KB Output is correct
3 Correct 142 ms 281468 KB Output is correct
4 Correct 142 ms 281424 KB Output is correct
5 Correct 142 ms 281256 KB Output is correct
6 Correct 151 ms 281508 KB Output is correct
7 Correct 141 ms 281428 KB Output is correct
8 Incorrect 143 ms 281428 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 152 ms 281452 KB Output is correct
2 Correct 165 ms 281408 KB Output is correct
3 Correct 150 ms 281448 KB Output is correct
4 Runtime error 1783 ms 1048576 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -