Submission #1108271

# Submission time Handle Problem Language Result Execution time Memory
1108271 2024-11-03T15:17:00 Z BuzzyBeez Toll (BOI17_toll) C++17
100 / 100
164 ms 68956 KB
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;

const int inf = 1e9 + 7;

struct matrix {
	vector<vector<int>> data;
	
	matrix() {}
	matrix(int n, int m) : data(n, vector<int>(m)) {}
	matrix(vector<vector<int>>& v) : data(v) {}
	
	int row() const {return data.size();}
	int col() const {return data[0].size();}
	auto & operator [] (int i) {return data[i];}
	const auto & operator [] (int i) const {return data[i];}
	
	friend matrix operator * (const matrix& a, const matrix& b) {
		matrix res(a.row(), b.col());
		for (int i = 0; i < a.row(); ++i)
			for (int j = 0; j < b.col(); ++j) {
				res[i][j] = inf;
				for (int k = 0; k < b.row(); ++k)
					res[i][j] = min(res[i][j], a[i][k] + b[k][j]);
			}
		return res;
	}	
	
	friend ostream& operator << (ostream& out, const matrix& o) {
		for (auto r : o.data) {
			for (int i : r) out << i << ' ';
			out << '\n';
		}
		out << '\n';
		return out;
	}
};

vector<pair<int, int>> radj[50008];
matrix trans[50008][16];


void build_binlift(int n) {
	for (int lg = 1; (1 << lg) <= n; ++lg) for (int i = 0; i + (1 << lg) - 1 < n; ++i)
		trans[i][lg] = trans[i][lg - 1] * trans[i + (1 << (lg - 1))][lg - 1];
}

matrix identity_matrix(int n) {
	matrix I(n, n);
	for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) I[i][j] = (i == j ? 0 : inf);
	return I;
}

int k;
matrix res; int pt, dist;
matrix query(int l, int r) {
	res = identity_matrix(k); pt = l; dist = r - l + 1;
	for (int bit = 0; bit < 16; ++bit) if (dist & (1 << bit)) {
		res = res * trans[pt][bit];
		pt += (1 << bit);
	}
	return res;
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n, m, q, u, v, w; cin >> k >> n >> m >> q; 
	if (n % k) n = n - n % k + k;
	while (m--) cin >> u >> v >> w, radj[v].push_back({u, w});
	for (int b = 0; b < n / k; ++b) {
		trans[b][0] = matrix(k, k);
		for (int i = 0; i < k; ++i) {
			for (int j = 0; j < k; ++j) trans[b][0][j][i] = inf;
			for (pair<int, int> r_edge : radj[b * k + i]) trans[b][0][r_edge.first % k][i] = r_edge.second;
		}
	}
	build_binlift(n / k); matrix T, dp;
	while (q--) {
		cin >> u >> v;
		if (u == v) cout << 0 << '\n';
		else if (u / k + 1 > v / k) cout << -1 << '\n';
		else {
			T = query(u / k + 1, v / k);
			dp = matrix(1, k);
			for (int i = u - u % k; i < u - u % k + k; ++i) dp[0][i % k] = (i == u ? 0 : inf);
			dp = dp * T;
			cout << (dp[0][v % k] == inf ? -1 : dp[0][v % k]) << '\n';
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 119 ms 68884 KB Output is correct
2 Correct 8 ms 20304 KB Output is correct
3 Correct 8 ms 20224 KB Output is correct
4 Correct 9 ms 20304 KB Output is correct
5 Correct 10 ms 20816 KB Output is correct
6 Correct 9 ms 20816 KB Output is correct
7 Correct 15 ms 21040 KB Output is correct
8 Correct 126 ms 68904 KB Output is correct
9 Correct 118 ms 68680 KB Output is correct
10 Correct 115 ms 66416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 150 ms 66424 KB Output is correct
2 Correct 7 ms 20304 KB Output is correct
3 Correct 8 ms 20304 KB Output is correct
4 Correct 8 ms 20304 KB Output is correct
5 Correct 7 ms 20228 KB Output is correct
6 Correct 7 ms 20304 KB Output is correct
7 Correct 15 ms 21072 KB Output is correct
8 Correct 16 ms 21072 KB Output is correct
9 Correct 114 ms 68956 KB Output is correct
10 Correct 141 ms 62228 KB Output is correct
11 Correct 113 ms 66552 KB Output is correct
12 Correct 124 ms 60488 KB Output is correct
13 Correct 102 ms 44896 KB Output is correct
14 Correct 94 ms 44616 KB Output is correct
15 Correct 84 ms 42312 KB Output is correct
16 Correct 91 ms 42312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 20388 KB Output is correct
2 Correct 7 ms 20232 KB Output is correct
3 Correct 8 ms 20304 KB Output is correct
4 Correct 8 ms 20424 KB Output is correct
5 Correct 7 ms 20304 KB Output is correct
6 Correct 11 ms 21072 KB Output is correct
7 Correct 8 ms 20816 KB Output is correct
8 Correct 9 ms 20944 KB Output is correct
9 Correct 13 ms 20816 KB Output is correct
10 Correct 111 ms 68680 KB Output is correct
11 Correct 102 ms 66120 KB Output is correct
12 Correct 132 ms 62024 KB Output is correct
13 Correct 135 ms 62668 KB Output is correct
14 Correct 137 ms 61432 KB Output is correct
15 Correct 71 ms 42316 KB Output is correct
16 Correct 75 ms 42324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 20388 KB Output is correct
2 Correct 7 ms 20232 KB Output is correct
3 Correct 8 ms 20304 KB Output is correct
4 Correct 8 ms 20424 KB Output is correct
5 Correct 7 ms 20304 KB Output is correct
6 Correct 11 ms 21072 KB Output is correct
7 Correct 8 ms 20816 KB Output is correct
8 Correct 9 ms 20944 KB Output is correct
9 Correct 13 ms 20816 KB Output is correct
10 Correct 111 ms 68680 KB Output is correct
11 Correct 102 ms 66120 KB Output is correct
12 Correct 132 ms 62024 KB Output is correct
13 Correct 135 ms 62668 KB Output is correct
14 Correct 137 ms 61432 KB Output is correct
15 Correct 71 ms 42316 KB Output is correct
16 Correct 75 ms 42324 KB Output is correct
17 Correct 118 ms 66376 KB Output is correct
18 Correct 8 ms 20304 KB Output is correct
19 Correct 8 ms 20304 KB Output is correct
20 Correct 8 ms 20472 KB Output is correct
21 Correct 7 ms 20304 KB Output is correct
22 Correct 8 ms 20304 KB Output is correct
23 Correct 10 ms 20816 KB Output is correct
24 Correct 10 ms 20816 KB Output is correct
25 Correct 13 ms 20952 KB Output is correct
26 Correct 12 ms 20816 KB Output is correct
27 Correct 96 ms 68584 KB Output is correct
28 Correct 142 ms 62172 KB Output is correct
29 Correct 128 ms 62792 KB Output is correct
30 Correct 132 ms 61520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 68884 KB Output is correct
2 Correct 8 ms 20304 KB Output is correct
3 Correct 8 ms 20224 KB Output is correct
4 Correct 9 ms 20304 KB Output is correct
5 Correct 10 ms 20816 KB Output is correct
6 Correct 9 ms 20816 KB Output is correct
7 Correct 15 ms 21040 KB Output is correct
8 Correct 126 ms 68904 KB Output is correct
9 Correct 118 ms 68680 KB Output is correct
10 Correct 115 ms 66416 KB Output is correct
11 Correct 150 ms 66424 KB Output is correct
12 Correct 7 ms 20304 KB Output is correct
13 Correct 8 ms 20304 KB Output is correct
14 Correct 8 ms 20304 KB Output is correct
15 Correct 7 ms 20228 KB Output is correct
16 Correct 7 ms 20304 KB Output is correct
17 Correct 15 ms 21072 KB Output is correct
18 Correct 16 ms 21072 KB Output is correct
19 Correct 114 ms 68956 KB Output is correct
20 Correct 141 ms 62228 KB Output is correct
21 Correct 113 ms 66552 KB Output is correct
22 Correct 124 ms 60488 KB Output is correct
23 Correct 102 ms 44896 KB Output is correct
24 Correct 94 ms 44616 KB Output is correct
25 Correct 84 ms 42312 KB Output is correct
26 Correct 91 ms 42312 KB Output is correct
27 Correct 8 ms 20388 KB Output is correct
28 Correct 7 ms 20232 KB Output is correct
29 Correct 8 ms 20304 KB Output is correct
30 Correct 8 ms 20424 KB Output is correct
31 Correct 7 ms 20304 KB Output is correct
32 Correct 11 ms 21072 KB Output is correct
33 Correct 8 ms 20816 KB Output is correct
34 Correct 9 ms 20944 KB Output is correct
35 Correct 13 ms 20816 KB Output is correct
36 Correct 111 ms 68680 KB Output is correct
37 Correct 102 ms 66120 KB Output is correct
38 Correct 132 ms 62024 KB Output is correct
39 Correct 135 ms 62668 KB Output is correct
40 Correct 137 ms 61432 KB Output is correct
41 Correct 71 ms 42316 KB Output is correct
42 Correct 75 ms 42324 KB Output is correct
43 Correct 118 ms 66376 KB Output is correct
44 Correct 8 ms 20304 KB Output is correct
45 Correct 8 ms 20304 KB Output is correct
46 Correct 8 ms 20472 KB Output is correct
47 Correct 7 ms 20304 KB Output is correct
48 Correct 8 ms 20304 KB Output is correct
49 Correct 10 ms 20816 KB Output is correct
50 Correct 10 ms 20816 KB Output is correct
51 Correct 13 ms 20952 KB Output is correct
52 Correct 12 ms 20816 KB Output is correct
53 Correct 96 ms 68584 KB Output is correct
54 Correct 142 ms 62172 KB Output is correct
55 Correct 128 ms 62792 KB Output is correct
56 Correct 132 ms 61520 KB Output is correct
57 Correct 164 ms 63316 KB Output is correct
58 Correct 109 ms 68904 KB Output is correct
59 Correct 103 ms 66632 KB Output is correct