답안 #896625

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
896625 2024-01-01T19:11:18 Z MateiKing80 Passport (JOI23_passport) C++17
48 / 100
2000 ms 876856 KB
#include <bits/stdc++.h>

using namespace std;

#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define fr first
#define sc second
#define pb push_back

int l[200005], r[200005], x[200005];
int nrpas1[200005], nrpasn[200005], ans[200005];
vector<int> vec[200005];
int n, Q;
int dist[2505][2505];

void bfs1()
{
    queue<int> q;
    q.push(1);
    while(!q.empty())
    {
        int nod = q.front();
        q.pop();
        for(auto i : vec[nod])
        {
            if(i != 1 && nrpas1[i] == 0)
            {
                nrpas1[i] = nrpas1[nod] + 1;
                q.push(i);
            }
        }
    }
    for(int i = 2; i <= n; i ++)
        if(nrpas1[i] == 0)
            nrpas1[i] = -1;
}


void bfsn()
{
    queue<int> q;
    q.push(n);
    while(!q.empty())
    {
        int nod = q.front();
        q.pop();
        for(auto i : vec[nod])
        {
            if(i != n && nrpasn[i] == 0)
            {
                nrpasn[i] = nrpasn[nod] + 1;
                q.push(i);
            }
        }
    }
    for(int i = 1; i < n; i ++)
        if(nrpasn[i] == 0)
            nrpasn[i] = -1;
}

void calcdist(int nod)
{
    int st = l[nod], dr = r[nod], stp = nod, drp = nod, sus = 0;
    ans[nod] = nrpas1[nod] + nrpasn[nod];

    if(nrpas1[nod] == -1 || nrpasn[nod] == -1)
    {
        ans[nod] = -1;
        return;
    }
//    cout << '\n';
    while(st != stp || dr != drp)
    {
        sus ++;
//        cout << st << " " << dr << " " << stp << " " << drp << " " << sus << '\n';
        int sta = stp, dra = drp;
        stp = st, drp = dr;
        for(int i = sta - 1; i >= stp; i --)
            st = min(st, l[i]), dr = max(dr, r[i]), dist[nod][i] = sus;
        for(int i = dra + 1; i <= drp; i ++)
            dr = max(dr, r[i]), st = min(st, l[i]), dist[nod][i] = sus;
    }
    for(int i = 1; i < st; i ++)
        dist[nod][i] = 1e9;
    for(int i = dr + 1; i <= n; i ++)
        dist[nod][i] = 1e9;
    for(int i = 1; i <= n; i ++)
        if(nrpas1[i] != -1 &&  nrpasn[i] != -1)
        {
            int sus = nrpas1[i] + nrpasn[i] + dist[nod][i];
            if(nrpas1[i] != 0 && nrpasn[i] != 0)
                sus --;
            ans[nod] = min(ans[nod], sus);
        }
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        cin >> l[i] >> r[i];
        for(int j = l[i]; j <= r[i]; j ++)
            vec[j].pb(i);
    }
    cin >> Q;
    for(int i = 1; i <= Q; i ++)
        cin >> x[i];
    bfs1();
    bfsn();
    for(int i = 1; i <= n; i ++)
        calcdist(i);
    for(int i = 1; i <= Q; i ++)
        cout << ans[x[i]] << '\n';
//    for(int i = 1; i <= n; i ++)
//        cout << i << " " << nrpas1[i] << " " << nrpasn[i] << '\n';
//    for(int i = 1; i <= n; i ++)
//    {
//        for(int j = 1; j <= n; j ++)
//            cout << dist[i][j] << " ";
//        cout << '\n';
//    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Execution timed out 2115 ms 876856 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 10784 KB Output is correct
9 Correct 2 ms 10592 KB Output is correct
10 Correct 2 ms 10596 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15052 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 10784 KB Output is correct
9 Correct 2 ms 10592 KB Output is correct
10 Correct 2 ms 10596 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15052 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 30 ms 41720 KB Output is correct
17 Correct 22 ms 33624 KB Output is correct
18 Correct 42 ms 50476 KB Output is correct
19 Correct 40 ms 49428 KB Output is correct
20 Correct 25 ms 33376 KB Output is correct
21 Correct 16 ms 27488 KB Output is correct
22 Correct 23 ms 30816 KB Output is correct
23 Correct 37 ms 47696 KB Output is correct
24 Correct 27 ms 40032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 10784 KB Output is correct
9 Correct 2 ms 10592 KB Output is correct
10 Correct 2 ms 10596 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15052 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 30 ms 41720 KB Output is correct
17 Correct 22 ms 33624 KB Output is correct
18 Correct 42 ms 50476 KB Output is correct
19 Correct 40 ms 49428 KB Output is correct
20 Correct 25 ms 33376 KB Output is correct
21 Correct 16 ms 27488 KB Output is correct
22 Correct 23 ms 30816 KB Output is correct
23 Correct 37 ms 47696 KB Output is correct
24 Correct 27 ms 40032 KB Output is correct
25 Correct 2 ms 10592 KB Output is correct
26 Correct 2 ms 10592 KB Output is correct
27 Correct 29 ms 42272 KB Output is correct
28 Correct 24 ms 33884 KB Output is correct
29 Correct 25 ms 33376 KB Output is correct
30 Correct 15 ms 27232 KB Output is correct
31 Correct 28 ms 43348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Execution timed out 2115 ms 876856 KB Time limit exceeded
5 Halted 0 ms 0 KB -