Submission #790331

# Submission time Handle Problem Language Result Execution time Memory
790331 2023-07-22T14:47:43 Z tch1cherin Toll (BOI17_toll) C++17
100 / 100
80 ms 31824 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 50005;
int K, D[MAX_N][5][5];
const int INF = 5e8 + 5;

void chmin(int& a, int b) {
  a = a < b ? a : b;
}

struct segment_tree {
  struct node {
    int DP[5][5], nxt[5][5];
    
    node() {
      for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
          DP[i][j] = nxt[i][j] = INF;
        }
      }
    }
  };

  node merge(node& a, node& b) {
    node c;
    memcpy(c.nxt, b.nxt, sizeof b.nxt);
    for (int s1 = 0; s1 < K; s1++)
      for (int t1 = 0; t1 < K; t1++)
        for (int s2 = 0; s2 < K; s2++)
          for (int t2 = 0; t2 < K; t2++)
            chmin(c.DP[s1][t2], a.DP[s1][t1] + a.nxt[t1][s2] + b.DP[s2][t2]);

    return c;
  }
  
  int size;
  vector<node> tree;

  segment_tree(int n) : size(2 << __lg(n)), tree(4 << __lg(n)) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 5; j++) {
        tree[size + i].DP[j][j] = 0;
      }
    }
    for (int i = 0; i < n; i++) {
      memcpy(tree[size + i].nxt, D[i], sizeof D[i]);
    }
    for (int i = size - 1; i > 0; i--) {
      tree[i] = merge(tree[i * 2], tree[i * 2 + 1]);
    }
  }

  node query(int l, int r) {
    return query(l, r + 1, 1, 0, size);
  }

  node query(int l, int r, int x, int lx, int rx) {
    if (lx >= l && rx <= r) {
      return tree[x];
    } else {
      int mid = (lx + rx) / 2;
      if (mid > l && mid < r) {
        node left = query(l, r, x * 2, lx, mid);
        node right = query(l, r, x * 2 + 1, mid, rx);
        return merge(left, right); 
      } else if (mid > l) {
        return query(l, r, x * 2, lx, mid);
      } else {
        return query(l, r, x * 2 + 1, mid, rx);
      }
    }
  }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int N, M, Q;
  cin >> K >> N >> M >> Q;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 5; j++) {
      for (int k = 0; k < 5; k++) {
        D[i][j][k] = INF;
      }
    }
  }
  for (int i = 0; i < M; i++) {
    int A, B, T;
    cin >> A >> B >> T;
    D[A / K][A % K][B % K] = T;
  }
  segment_tree tree((N + K - 1) / K);
  while (Q--) {
    int A, B;
    cin >> A >> B;
    if (A / K < B / K) {
      segment_tree::node value = tree.query(A / K, B / K);
      cout << (value.DP[A % K][B % K] >= INF ? -1 : value.DP[A % K][B % K]) << "\n";
    } else {
      cout << -1 << "\n";
    }
  }
}
# Verdict Execution time Memory Grader output
1 Correct 42 ms 31824 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 848 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 37 ms 31684 KB Output is correct
9 Correct 38 ms 31744 KB Output is correct
10 Correct 30 ms 30936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 19644 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 324 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 4 ms 852 KB Output is correct
8 Correct 6 ms 788 KB Output is correct
9 Correct 38 ms 31684 KB Output is correct
10 Correct 47 ms 20372 KB Output is correct
11 Correct 35 ms 19724 KB Output is correct
12 Correct 41 ms 19328 KB Output is correct
13 Correct 48 ms 8876 KB Output is correct
14 Correct 35 ms 11080 KB Output is correct
15 Correct 34 ms 7596 KB Output is correct
16 Correct 33 ms 7688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 324 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 28 ms 31548 KB Output is correct
11 Correct 32 ms 19520 KB Output is correct
12 Correct 45 ms 20152 KB Output is correct
13 Correct 41 ms 20412 KB Output is correct
14 Correct 35 ms 19780 KB Output is correct
15 Correct 24 ms 7636 KB Output is correct
16 Correct 21 ms 7636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 324 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 28 ms 31548 KB Output is correct
11 Correct 32 ms 19520 KB Output is correct
12 Correct 45 ms 20152 KB Output is correct
13 Correct 41 ms 20412 KB Output is correct
14 Correct 35 ms 19780 KB Output is correct
15 Correct 24 ms 7636 KB Output is correct
16 Correct 21 ms 7636 KB Output is correct
17 Correct 31 ms 19512 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 328 KB Output is correct
23 Correct 2 ms 852 KB Output is correct
24 Correct 3 ms 596 KB Output is correct
25 Correct 12 ms 604 KB Output is correct
26 Correct 7 ms 596 KB Output is correct
27 Correct 32 ms 31700 KB Output is correct
28 Correct 49 ms 20164 KB Output is correct
29 Correct 46 ms 20420 KB Output is correct
30 Correct 37 ms 19856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 31824 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 848 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 37 ms 31684 KB Output is correct
9 Correct 38 ms 31744 KB Output is correct
10 Correct 30 ms 30936 KB Output is correct
11 Correct 38 ms 19644 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 324 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 4 ms 852 KB Output is correct
18 Correct 6 ms 788 KB Output is correct
19 Correct 38 ms 31684 KB Output is correct
20 Correct 47 ms 20372 KB Output is correct
21 Correct 35 ms 19724 KB Output is correct
22 Correct 41 ms 19328 KB Output is correct
23 Correct 48 ms 8876 KB Output is correct
24 Correct 35 ms 11080 KB Output is correct
25 Correct 34 ms 7596 KB Output is correct
26 Correct 33 ms 7688 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 324 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 328 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 852 KB Output is correct
33 Correct 1 ms 596 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 28 ms 31548 KB Output is correct
37 Correct 32 ms 19520 KB Output is correct
38 Correct 45 ms 20152 KB Output is correct
39 Correct 41 ms 20412 KB Output is correct
40 Correct 35 ms 19780 KB Output is correct
41 Correct 24 ms 7636 KB Output is correct
42 Correct 21 ms 7636 KB Output is correct
43 Correct 31 ms 19512 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 328 KB Output is correct
49 Correct 2 ms 852 KB Output is correct
50 Correct 3 ms 596 KB Output is correct
51 Correct 12 ms 604 KB Output is correct
52 Correct 7 ms 596 KB Output is correct
53 Correct 32 ms 31700 KB Output is correct
54 Correct 49 ms 20164 KB Output is correct
55 Correct 46 ms 20420 KB Output is correct
56 Correct 37 ms 19856 KB Output is correct
57 Correct 80 ms 14908 KB Output is correct
58 Correct 35 ms 31804 KB Output is correct
59 Correct 38 ms 19780 KB Output is correct