Submission #757784

# Submission time Handle Problem Language Result Execution time Memory
757784 2023-06-13T18:27:34 Z taher Toll (BOI17_toll) C++17
100 / 100
222 ms 49640 KB
#include <bits/stdc++.h>

using namespace std;

template<typename T, class F = function<T(const T&, const T&)>>
class SparseTable {
  public:
  int n;
  vector<vector<T>> mat;
  F func;

  SparseTable(const vector<int>& a, const F& f) : func(f) {
    n = (int) a.size();
    mat.resize(n);
    int max_log = 32 - __builtin_clz(n);
    mat[0] = a;
    for (int j = 1; j < max_log; j++) {
      mat[j].resize(n - (1 << j) + 1);
      for (int i = 0; i <= (n - (1 << j)); i++) {
        mat[j][i] = func(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]);
      }
    }
  }

  T get(int from, int to) {
    assert(from <= to && from >= 0 && to <= n - 1);
    int lg = 32 - __builtin_clz(to - from + 1) - 1;
    return func(mat[lg][from], mat[lg][to - (1 << lg) + 1]);
  }
};

const int N = 50000;
const int inf = 1000000000;

int dp[N];
bool inQue[N];
vector<array<int, 2>> adj[N];
vector<array<int, 2>> rAdj[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int k, n, m, q;
  cin >> k >> n >> m >> q;

  int p = (n + k - 1) / k;
  vector<vector<int>> bucket(p);

  for (int i = 0; i < n; i++) {
    bucket[i / k].push_back(i);
  }

  for (int i = 0; i < n; i++) {
    inQue[i] = false;
    dp[i] = inf;
  }

  for (int i = 0; i < m; i++) {
    int u, v, t;
    cin >> u >> v >> t;
    adj[u].push_back({v, t});
    rAdj[v].push_back({u, t});
  }

  vector<vector<vector<vector<pair<int, int>>>>> pts(p * 2, vector<vector<vector<pair<int, int>>>> (k, vector<vector<pair<int, int>>> (2)));

  auto Solve = [&](int midPoint, int cnt) {
    for (int each = 0; each < (int) bucket[midPoint].size(); each++) {
      int x = bucket[midPoint][each];
      dp[x] = 0;
      pts[cnt][each][0].emplace_back(x, 0);
      pts[cnt][each][1].emplace_back(x, 0);
      for (int it = x - 1; it >= 0; it--) {
        if (inQue[it / k]) {
          break;
        }
        for (auto y : adj[it]) {
          dp[it] = min(dp[it], dp[y[0]] + y[1]);
        }
        pts[cnt][each][0].emplace_back(it, dp[it]);
      }
      reverse(pts[cnt][each][0].begin(), pts[cnt][each][0].end());
      for (int it = x - 1; it >= 0; it--) {
        if (inQue[it / k]) {
          break;
        }
        dp[it] = inf;
      }
      for (int it = x + 1; it < n; it++) {
        if (inQue[it / k]) {
          break;
        }
        for (auto y : rAdj[it]) {
          dp[it] = min(dp[it], dp[y[0]] + y[1]);
        }
        pts[cnt][each][1].emplace_back(it, dp[it]);
      }
      for (int it = x; it < n; it++) {
        if (inQue[it / k]) {
          break;
        }
        dp[it] = inf;
      }
    }
  };

  vector<int> at(p, inf);

  auto Divide = [&]() {
    int cnt = 0;
    queue<array<int, 2>> que;
    que.push({0, p - 1});
    while (!que.empty()) {
      auto t = que.front();
      que.pop();
      int low = t[0];
      int high = t[1];
      if (low >= high) {
        continue;
      }
      int midPoint = low + (high - low) / 2;
      Solve(midPoint, cnt);
      inQue[midPoint] = true;
      at[midPoint] = cnt;
      cnt++;
      que.push({low, midPoint});
      que.push({midPoint + 1, high});
    }
  };

  Divide();

  SparseTable<int> st(at, [&](int i, int j) {
    return min(i, j);
  });

  while (q--) {
    int u, v;
    cin >> u >> v;
    int b0 = u / k;
    int b1 = v / k;
    if (b0 == b1) {
      cout << -1 << '\n';
    } else {
      int ans = inf;
      int cnt = st.get(b0, b1);
      for (int each = 0; each < k; each++) {
        if (pts[cnt][each][0].empty()) {
          break;
        }
        int it0 = lower_bound(pts[cnt][each][0].begin(), pts[cnt][each][0].end(), make_pair(u, -inf)) - pts[cnt][each][0].begin();
        int it1 = lower_bound(pts[cnt][each][1].begin(), pts[cnt][each][1].end(), make_pair(v, -inf)) - pts[cnt][each][1].begin();
        if (it0 < (int) pts[cnt][each][0].size() && it1 < (int) pts[cnt][each][1].size()) {
          if (pts[cnt][each][0][it0].first == u && pts[cnt][each][1][it1].first == v) {
            ans = min(ans, pts[cnt][each][0][it0].second + pts[cnt][each][1][it1].second);
          }
        }
      }
      if (ans < inf) {
        cout << ans << '\n';
      } else {
        cout << -1 << '\n';
      }
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 79 ms 35500 KB Output is correct
2 Correct 2 ms 2664 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Correct 2 ms 2676 KB Output is correct
5 Correct 3 ms 3196 KB Output is correct
6 Correct 3 ms 3156 KB Output is correct
7 Correct 3 ms 3156 KB Output is correct
8 Correct 100 ms 35404 KB Output is correct
9 Correct 87 ms 35172 KB Output is correct
10 Correct 50 ms 31432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 37192 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 2 ms 2672 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 8 ms 3324 KB Output is correct
8 Correct 7 ms 3336 KB Output is correct
9 Correct 78 ms 35360 KB Output is correct
10 Correct 174 ms 42900 KB Output is correct
11 Correct 143 ms 37364 KB Output is correct
12 Correct 136 ms 40388 KB Output is correct
13 Correct 138 ms 33292 KB Output is correct
14 Correct 92 ms 25500 KB Output is correct
15 Correct 111 ms 29536 KB Output is correct
16 Correct 125 ms 29476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2676 KB Output is correct
2 Correct 2 ms 2680 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 4 ms 3156 KB Output is correct
7 Correct 3 ms 3152 KB Output is correct
8 Correct 6 ms 3432 KB Output is correct
9 Correct 5 ms 3284 KB Output is correct
10 Correct 71 ms 35232 KB Output is correct
11 Correct 105 ms 37132 KB Output is correct
12 Correct 148 ms 42708 KB Output is correct
13 Correct 182 ms 43744 KB Output is correct
14 Correct 150 ms 41604 KB Output is correct
15 Correct 95 ms 29516 KB Output is correct
16 Correct 99 ms 29464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2676 KB Output is correct
2 Correct 2 ms 2680 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 4 ms 3156 KB Output is correct
7 Correct 3 ms 3152 KB Output is correct
8 Correct 6 ms 3432 KB Output is correct
9 Correct 5 ms 3284 KB Output is correct
10 Correct 71 ms 35232 KB Output is correct
11 Correct 105 ms 37132 KB Output is correct
12 Correct 148 ms 42708 KB Output is correct
13 Correct 182 ms 43744 KB Output is correct
14 Correct 150 ms 41604 KB Output is correct
15 Correct 95 ms 29516 KB Output is correct
16 Correct 99 ms 29464 KB Output is correct
17 Correct 121 ms 37196 KB Output is correct
18 Correct 2 ms 2672 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 4 ms 3156 KB Output is correct
24 Correct 6 ms 3156 KB Output is correct
25 Correct 7 ms 3412 KB Output is correct
26 Correct 7 ms 3344 KB Output is correct
27 Correct 88 ms 35300 KB Output is correct
28 Correct 167 ms 42736 KB Output is correct
29 Correct 171 ms 43648 KB Output is correct
30 Correct 186 ms 41876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 35500 KB Output is correct
2 Correct 2 ms 2664 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Correct 2 ms 2676 KB Output is correct
5 Correct 3 ms 3196 KB Output is correct
6 Correct 3 ms 3156 KB Output is correct
7 Correct 3 ms 3156 KB Output is correct
8 Correct 100 ms 35404 KB Output is correct
9 Correct 87 ms 35172 KB Output is correct
10 Correct 50 ms 31432 KB Output is correct
11 Correct 116 ms 37192 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 2 ms 2672 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 8 ms 3324 KB Output is correct
18 Correct 7 ms 3336 KB Output is correct
19 Correct 78 ms 35360 KB Output is correct
20 Correct 174 ms 42900 KB Output is correct
21 Correct 143 ms 37364 KB Output is correct
22 Correct 136 ms 40388 KB Output is correct
23 Correct 138 ms 33292 KB Output is correct
24 Correct 92 ms 25500 KB Output is correct
25 Correct 111 ms 29536 KB Output is correct
26 Correct 125 ms 29476 KB Output is correct
27 Correct 2 ms 2676 KB Output is correct
28 Correct 2 ms 2680 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 4 ms 3156 KB Output is correct
33 Correct 3 ms 3152 KB Output is correct
34 Correct 6 ms 3432 KB Output is correct
35 Correct 5 ms 3284 KB Output is correct
36 Correct 71 ms 35232 KB Output is correct
37 Correct 105 ms 37132 KB Output is correct
38 Correct 148 ms 42708 KB Output is correct
39 Correct 182 ms 43744 KB Output is correct
40 Correct 150 ms 41604 KB Output is correct
41 Correct 95 ms 29516 KB Output is correct
42 Correct 99 ms 29464 KB Output is correct
43 Correct 121 ms 37196 KB Output is correct
44 Correct 2 ms 2672 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 4 ms 3156 KB Output is correct
50 Correct 6 ms 3156 KB Output is correct
51 Correct 7 ms 3412 KB Output is correct
52 Correct 7 ms 3344 KB Output is correct
53 Correct 88 ms 35300 KB Output is correct
54 Correct 167 ms 42736 KB Output is correct
55 Correct 171 ms 43648 KB Output is correct
56 Correct 186 ms 41876 KB Output is correct
57 Correct 222 ms 49640 KB Output is correct
58 Correct 85 ms 35308 KB Output is correct
59 Correct 142 ms 37328 KB Output is correct