답안 #404770

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404770 2021-05-15T01:01:01 Z rama_pang Toll (BOI17_toll) C++17
100 / 100
95 ms 15984 KB
#include <bits/stdc++.h>
using namespace std;

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

  int K, N, M, O;
  cin >> K >> N >> M >> O;
  while (N % K != 0) N += 1;

  const int INF = 1e9;
  vector<array<array<int, 5>, 5>> A(N / K);
  for (int i = 0; i < N / K; i++) {
    for (int x = 0; x < K; x++) {
      for (int y = 0; y < K; y++) {
        A[i][x][y] = INF;
      }
    }
  }

  while (M--) {
    int a, b, t;
    cin >> a >> b >> t;
    assert(a / K + 1 == b / K);
    A[a / K][a % K][b % K] = min(A[a / K][a % K][b % K], t);
  }

  vector<array<array<int, 5>, 5>> tree(N / K * 2);

  const auto Merge = [&](array<array<int, 5>, 5> p, array<array<int, 5>, 5> q) {
    array<array<int, 5>, 5> res;
    for (int i = 0; i < K; i++) {
      for (int j = 0; j < K; j++) {
        res[i][j] = INF;
        for (int k = 0; k < K; k++) {
          res[i][j] = min(res[i][j], p[i][k] + q[k][j]);
        }
      }
    }
    return res;
  };

  for (int i = 0; i < N / K; i++) {
    tree[i + N / K] = A[i];
  }
  for (int i = N / K - 1; i > 0; i--) {
    tree[i] = Merge(tree[i * 2], tree[i * 2 + 1]);
  }

  const auto Query = [&](int l, int r) {
    array<array<int, 5>, 5> lft;
    array<array<int, 5>, 5> rgt;
    for (int i = 0; i < K; i++) {
      for (int j = 0; j < K; j++) {
        lft[i][j] = i == j ? 0 : INF;
        rgt[i][j] = i == j ? 0 : INF;
      }
    }
    for (l += N / K, r += N / K; l < r; l /= 2, r /= 2) {
      if (l & 1) lft = Merge(lft, tree[l++]);
      if (r & 1) rgt = Merge(tree[--r], rgt);
    }
    return Merge(lft, rgt);
  };

  while (O--) {
    int a, b;
    cin >> a >> b;
    if (b / K <= a / K) {
      cout << -1 << '\n';
      continue;
    }
    assert(a / K + 1 <= b / K);
    int ans = Query(a / K, b / K)[a % K][b % K];
    if (ans == INF) {
      cout << -1 << '\n';
    } else {
      cout << ans << '\n';
    }
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 15984 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 2 ms 636 KB Output is correct
7 Correct 2 ms 572 KB Output is correct
8 Correct 40 ms 15816 KB Output is correct
9 Correct 46 ms 15816 KB Output is correct
10 Correct 21 ms 15124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 9388 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 6 ms 716 KB Output is correct
8 Correct 7 ms 728 KB Output is correct
9 Correct 41 ms 15816 KB Output is correct
10 Correct 66 ms 7516 KB Output is correct
11 Correct 49 ms 9408 KB Output is correct
12 Correct 40 ms 6420 KB Output is correct
13 Correct 62 ms 4372 KB Output is correct
14 Correct 40 ms 4600 KB Output is correct
15 Correct 33 ms 3208 KB Output is correct
16 Correct 33 ms 3192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 3 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 36 ms 15688 KB Output is correct
11 Correct 38 ms 9156 KB Output is correct
12 Correct 49 ms 7308 KB Output is correct
13 Correct 61 ms 7492 KB Output is correct
14 Correct 55 ms 6980 KB Output is correct
15 Correct 34 ms 3196 KB Output is correct
16 Correct 28 ms 3200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 3 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 36 ms 15688 KB Output is correct
11 Correct 38 ms 9156 KB Output is correct
12 Correct 49 ms 7308 KB Output is correct
13 Correct 61 ms 7492 KB Output is correct
14 Correct 55 ms 6980 KB Output is correct
15 Correct 34 ms 3196 KB Output is correct
16 Correct 28 ms 3200 KB Output is correct
17 Correct 40 ms 9148 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 3 ms 600 KB Output is correct
24 Correct 3 ms 456 KB Output is correct
25 Correct 9 ms 460 KB Output is correct
26 Correct 5 ms 456 KB Output is correct
27 Correct 33 ms 15844 KB Output is correct
28 Correct 57 ms 7356 KB Output is correct
29 Correct 61 ms 7568 KB Output is correct
30 Correct 59 ms 7040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 15984 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 2 ms 636 KB Output is correct
7 Correct 2 ms 572 KB Output is correct
8 Correct 40 ms 15816 KB Output is correct
9 Correct 46 ms 15816 KB Output is correct
10 Correct 21 ms 15124 KB Output is correct
11 Correct 49 ms 9388 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 6 ms 716 KB Output is correct
18 Correct 7 ms 728 KB Output is correct
19 Correct 41 ms 15816 KB Output is correct
20 Correct 66 ms 7516 KB Output is correct
21 Correct 49 ms 9408 KB Output is correct
22 Correct 40 ms 6420 KB Output is correct
23 Correct 62 ms 4372 KB Output is correct
24 Correct 40 ms 4600 KB Output is correct
25 Correct 33 ms 3208 KB Output is correct
26 Correct 33 ms 3192 KB Output is correct
27 Correct 1 ms 296 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 2 ms 588 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 3 ms 332 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 36 ms 15688 KB Output is correct
37 Correct 38 ms 9156 KB Output is correct
38 Correct 49 ms 7308 KB Output is correct
39 Correct 61 ms 7492 KB Output is correct
40 Correct 55 ms 6980 KB Output is correct
41 Correct 34 ms 3196 KB Output is correct
42 Correct 28 ms 3200 KB Output is correct
43 Correct 40 ms 9148 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 312 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 3 ms 600 KB Output is correct
50 Correct 3 ms 456 KB Output is correct
51 Correct 9 ms 460 KB Output is correct
52 Correct 5 ms 456 KB Output is correct
53 Correct 33 ms 15844 KB Output is correct
54 Correct 57 ms 7356 KB Output is correct
55 Correct 61 ms 7568 KB Output is correct
56 Correct 59 ms 7040 KB Output is correct
57 Correct 95 ms 7284 KB Output is correct
58 Correct 39 ms 15816 KB Output is correct
59 Correct 50 ms 9400 KB Output is correct