답안 #1023752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1023752 2024-07-15T05:54:49 Z zNatsumi Passport (JOI23_passport) C++17
100 / 100
594 ms 83392 KB
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5,
          INF = 1e9;

using ii = pair<int, int>;
#define fi first
#define se second

int n, q, id[N], f[2][N << 2], dp[N << 2];
vector<ii> ad[N << 2];

void build(int tl, int tr, int i){
  if(i>>1) ad[i].push_back({i>>1, 0});
  f[0][i] = f[1][i] = INF;

  if(tl == tr){
    id[tl] = i;
    return;
  }
  int tm = tl + tr >> 1;
  build(tl, tm, i<<1), build(tm+1, tr, i<<1|1);
}

void upd(int l, int r, int u, int tl, int tr, int i){
  if(r < tl || tr < l || l > r) return;
  if(l <= tl && tr <= r){
    ad[i].push_back({u, 1});
    return;
  }
  int tm = tl + tr >> 1;
  upd(l, r, u, tl, tm, i<<1), upd(l, r, u, tm+1, tr, i<<1|1);
}

void bfs(int u){
  int t = (u == n);
  u = id[u];
  deque<int> dq;
  dq.push_back(u);
  f[t][u] = 0;

  while(dq.size()){
    auto u = dq.front(); dq.pop_front();

    for(auto [v, w] : ad[u]) if(f[t][v] == INF){
      if(w == 1){
        f[t][v] = f[t][u] + 1;
        dq.push_back(v);
      }else{
        f[t][v] = f[t][u];
        dq.push_front(v);
      }
    }
  }
}

void dij(){
  priority_queue<ii, vector<ii>, greater<>> pq;
  for(int i = 1; i <= 4 * n; i++) if(dp[i] != INF) pq.push({dp[i], i});

  while(pq.size()){
    int u, d; tie(d, u) = pq.top(); pq.pop();

    if(d != dp[u]) continue;

    for(auto [v, w] : ad[u])
      if(d + w < dp[v])
        pq.push({dp[v] = d + w, v});
  }
}

int32_t main(){
  cin.tie(0)->sync_with_stdio(0);
  if(fopen("test.inp", "r")){
    freopen("test.inp", "r", stdin);
    freopen("test.out", "w", stdout);
  }

  cin >> n;

  build(1, n, 1);

  for(int i = 1; i <= n; i++){
    int l, r; cin >> l >> r;
    upd(l, r, id[i], 1, n, 1);
  }

  bfs(1), bfs(n);
  for(int i = 2; i <= n - 1; i++) if(f[0][id[i]] != INF) f[0][id[i]]--;

  for(int i = 1; i <= 4 * n; i++){
    if(f[0][i] == INF || f[1][i] == INF) dp[i] = INF;
    else dp[i] = f[0][i] + f[1][i];
  }
  dij();

  cin >> q;
  while(q--){
    int i; cin >> i;
    cout << (dp[id[i]] == INF ? -1 : dp[id[i]]) << "\n";
  }

  return 0;
}

Compilation message

passport.cpp: In function 'void build(int, int, int)':
passport.cpp:22:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   22 |   int tm = tl + tr >> 1;
      |            ~~~^~~~
passport.cpp: In function 'void upd(int, int, int, int, int, int)':
passport.cpp:32:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   32 |   int tm = tl + tr >> 1;
      |            ~~~^~~~
passport.cpp: In function 'int32_t main()':
passport.cpp:76:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     freopen("test.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:77:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |     freopen("test.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19036 KB Output is correct
2 Correct 9 ms 19036 KB Output is correct
3 Correct 8 ms 19036 KB Output is correct
4 Correct 534 ms 78524 KB Output is correct
5 Correct 297 ms 58560 KB Output is correct
6 Correct 180 ms 53600 KB Output is correct
7 Correct 154 ms 50752 KB Output is correct
8 Correct 109 ms 53484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19036 KB Output is correct
2 Correct 9 ms 19036 KB Output is correct
3 Correct 9 ms 19036 KB Output is correct
4 Correct 7 ms 19204 KB Output is correct
5 Correct 13 ms 19036 KB Output is correct
6 Correct 9 ms 19036 KB Output is correct
7 Correct 9 ms 19036 KB Output is correct
8 Correct 9 ms 19036 KB Output is correct
9 Correct 10 ms 19144 KB Output is correct
10 Correct 9 ms 19036 KB Output is correct
11 Correct 9 ms 19292 KB Output is correct
12 Correct 9 ms 19292 KB Output is correct
13 Correct 9 ms 19268 KB Output is correct
14 Correct 10 ms 19296 KB Output is correct
15 Correct 10 ms 19292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19036 KB Output is correct
2 Correct 9 ms 19036 KB Output is correct
3 Correct 9 ms 19036 KB Output is correct
4 Correct 7 ms 19204 KB Output is correct
5 Correct 13 ms 19036 KB Output is correct
6 Correct 9 ms 19036 KB Output is correct
7 Correct 9 ms 19036 KB Output is correct
8 Correct 9 ms 19036 KB Output is correct
9 Correct 10 ms 19144 KB Output is correct
10 Correct 9 ms 19036 KB Output is correct
11 Correct 9 ms 19292 KB Output is correct
12 Correct 9 ms 19292 KB Output is correct
13 Correct 9 ms 19268 KB Output is correct
14 Correct 10 ms 19296 KB Output is correct
15 Correct 10 ms 19292 KB Output is correct
16 Correct 16 ms 19804 KB Output is correct
17 Correct 12 ms 19804 KB Output is correct
18 Correct 13 ms 20060 KB Output is correct
19 Correct 12 ms 20060 KB Output is correct
20 Correct 13 ms 20056 KB Output is correct
21 Correct 11 ms 19696 KB Output is correct
22 Correct 10 ms 19548 KB Output is correct
23 Correct 12 ms 19844 KB Output is correct
24 Correct 11 ms 19804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19036 KB Output is correct
2 Correct 9 ms 19036 KB Output is correct
3 Correct 9 ms 19036 KB Output is correct
4 Correct 7 ms 19204 KB Output is correct
5 Correct 13 ms 19036 KB Output is correct
6 Correct 9 ms 19036 KB Output is correct
7 Correct 9 ms 19036 KB Output is correct
8 Correct 9 ms 19036 KB Output is correct
9 Correct 10 ms 19144 KB Output is correct
10 Correct 9 ms 19036 KB Output is correct
11 Correct 9 ms 19292 KB Output is correct
12 Correct 9 ms 19292 KB Output is correct
13 Correct 9 ms 19268 KB Output is correct
14 Correct 10 ms 19296 KB Output is correct
15 Correct 10 ms 19292 KB Output is correct
16 Correct 16 ms 19804 KB Output is correct
17 Correct 12 ms 19804 KB Output is correct
18 Correct 13 ms 20060 KB Output is correct
19 Correct 12 ms 20060 KB Output is correct
20 Correct 13 ms 20056 KB Output is correct
21 Correct 11 ms 19696 KB Output is correct
22 Correct 10 ms 19548 KB Output is correct
23 Correct 12 ms 19844 KB Output is correct
24 Correct 11 ms 19804 KB Output is correct
25 Correct 9 ms 19036 KB Output is correct
26 Correct 9 ms 19036 KB Output is correct
27 Correct 13 ms 19952 KB Output is correct
28 Correct 12 ms 19804 KB Output is correct
29 Correct 12 ms 19776 KB Output is correct
30 Correct 12 ms 19804 KB Output is correct
31 Correct 11 ms 19804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19036 KB Output is correct
2 Correct 9 ms 19036 KB Output is correct
3 Correct 8 ms 19036 KB Output is correct
4 Correct 534 ms 78524 KB Output is correct
5 Correct 297 ms 58560 KB Output is correct
6 Correct 180 ms 53600 KB Output is correct
7 Correct 154 ms 50752 KB Output is correct
8 Correct 109 ms 53484 KB Output is correct
9 Correct 9 ms 19036 KB Output is correct
10 Correct 9 ms 19036 KB Output is correct
11 Correct 9 ms 19036 KB Output is correct
12 Correct 7 ms 19204 KB Output is correct
13 Correct 13 ms 19036 KB Output is correct
14 Correct 9 ms 19036 KB Output is correct
15 Correct 9 ms 19036 KB Output is correct
16 Correct 9 ms 19036 KB Output is correct
17 Correct 10 ms 19144 KB Output is correct
18 Correct 9 ms 19036 KB Output is correct
19 Correct 9 ms 19292 KB Output is correct
20 Correct 9 ms 19292 KB Output is correct
21 Correct 9 ms 19268 KB Output is correct
22 Correct 10 ms 19296 KB Output is correct
23 Correct 10 ms 19292 KB Output is correct
24 Correct 16 ms 19804 KB Output is correct
25 Correct 12 ms 19804 KB Output is correct
26 Correct 13 ms 20060 KB Output is correct
27 Correct 12 ms 20060 KB Output is correct
28 Correct 13 ms 20056 KB Output is correct
29 Correct 11 ms 19696 KB Output is correct
30 Correct 10 ms 19548 KB Output is correct
31 Correct 12 ms 19844 KB Output is correct
32 Correct 11 ms 19804 KB Output is correct
33 Correct 9 ms 19036 KB Output is correct
34 Correct 9 ms 19036 KB Output is correct
35 Correct 13 ms 19952 KB Output is correct
36 Correct 12 ms 19804 KB Output is correct
37 Correct 12 ms 19776 KB Output is correct
38 Correct 12 ms 19804 KB Output is correct
39 Correct 11 ms 19804 KB Output is correct
40 Correct 594 ms 78828 KB Output is correct
41 Correct 337 ms 59012 KB Output is correct
42 Correct 417 ms 83300 KB Output is correct
43 Correct 385 ms 83392 KB Output is correct
44 Correct 205 ms 53704 KB Output is correct
45 Correct 254 ms 60880 KB Output is correct
46 Correct 100 ms 35848 KB Output is correct
47 Correct 314 ms 62892 KB Output is correct
48 Correct 261 ms 69368 KB Output is correct