#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e15 + 7;
#ifndef LOCAL
#define cerr if(false) cerr
#endif
typedef vector<vector<ll> > mat;
const ll MAX_N = 1e5 + 10;
ll n, m, k, q;
mat mult(const mat &a, const mat &b) {
mat ret = mat(k, vector<ll>(k, mod));
if(a.empty()) { return b; }
if(b.empty()) { return a; }
for(ll i = 0; i < k; i ++) {
for(ll j = 0; j < k; j ++) {
for(ll mid = 0; mid < k; mid ++) {
chkmin(ret[i][j], a[i][mid] + b[mid][j]);
}
}
}
return ret;
}
vector<pair<ll, ll> > g[MAX_N];
mat prec[MAX_N];
mat tree[4 * MAX_N];
void build(int curr, int l, int r) {
if(l == r) {
tree[curr] = prec[l];
return;
}
int m = (l + r) / 2ll;
build(curr * 2, l, m);
build(curr * 2 + 1, m + 1, r);
tree[curr] = mult(tree[curr * 2], tree[curr * 2 + 1]);
}
mat quer(int curr, int l, int r, int ql, int qr) {
if(ql <= l && r <= qr) {
return tree[curr];
} else if(ql > r || l > qr) {
return {};
}
int m = (l + r) / 2ll;
return mult(
quer(curr * 2, l, m, ql, qr),
quer(curr * 2 + 1, m + 1, r, ql, qr)
);
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> k >> n >> m >> q;
for(ll i = 0; i < m; i ++) {
ll a, b, c;
cin >> a >> b >> c;
g[a].push_back({b, c});
}
for(ll i = 0; i < n; i += k) {
prec[i / k] = mat(k, vector<ll>(k, mod));
for(ll j = i; j < i + k; j ++) {
for(auto it : g[j]) {
chkmin(prec[i / k][j % k][it.first % k], it.second);
}
}
}
build(1, 0, MAX_N - 1);
while(q --) {
ll a, b;
cin >> a >> b;
if(a / k >= b / k) {
cout << -1 << endl;
continue;
}
mat ret = quer(1, 0, MAX_N - 1, a / k, b / k - 1);
if(ret[a % k][b % k] == mod) {
cout << -1 << endl;
} else {
cout << ret[a % k][b % k] << endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
25320 KB |
Output is correct |
2 |
Correct |
18 ms |
14412 KB |
Output is correct |
3 |
Correct |
17 ms |
14376 KB |
Output is correct |
4 |
Correct |
20 ms |
14376 KB |
Output is correct |
5 |
Correct |
23 ms |
14628 KB |
Output is correct |
6 |
Correct |
24 ms |
14540 KB |
Output is correct |
7 |
Correct |
25 ms |
14568 KB |
Output is correct |
8 |
Correct |
105 ms |
25372 KB |
Output is correct |
9 |
Correct |
102 ms |
25280 KB |
Output is correct |
10 |
Correct |
86 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
26172 KB |
Output is correct |
2 |
Correct |
17 ms |
14344 KB |
Output is correct |
3 |
Correct |
19 ms |
14284 KB |
Output is correct |
4 |
Correct |
22 ms |
14408 KB |
Output is correct |
5 |
Correct |
25 ms |
14392 KB |
Output is correct |
6 |
Correct |
28 ms |
14284 KB |
Output is correct |
7 |
Correct |
41 ms |
14700 KB |
Output is correct |
8 |
Correct |
54 ms |
14768 KB |
Output is correct |
9 |
Correct |
107 ms |
26276 KB |
Output is correct |
10 |
Correct |
143 ms |
28852 KB |
Output is correct |
11 |
Correct |
138 ms |
27936 KB |
Output is correct |
12 |
Correct |
114 ms |
26180 KB |
Output is correct |
13 |
Correct |
128 ms |
27784 KB |
Output is correct |
14 |
Correct |
81 ms |
23420 KB |
Output is correct |
15 |
Correct |
92 ms |
23920 KB |
Output is correct |
16 |
Correct |
91 ms |
23996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
14400 KB |
Output is correct |
2 |
Correct |
20 ms |
14284 KB |
Output is correct |
3 |
Correct |
21 ms |
14284 KB |
Output is correct |
4 |
Correct |
27 ms |
14408 KB |
Output is correct |
5 |
Correct |
29 ms |
14408 KB |
Output is correct |
6 |
Correct |
19 ms |
14616 KB |
Output is correct |
7 |
Correct |
28 ms |
14548 KB |
Output is correct |
8 |
Correct |
33 ms |
14756 KB |
Output is correct |
9 |
Correct |
28 ms |
14708 KB |
Output is correct |
10 |
Correct |
55 ms |
25304 KB |
Output is correct |
11 |
Correct |
76 ms |
26044 KB |
Output is correct |
12 |
Correct |
89 ms |
26524 KB |
Output is correct |
13 |
Correct |
109 ms |
26984 KB |
Output is correct |
14 |
Correct |
85 ms |
25796 KB |
Output is correct |
15 |
Correct |
66 ms |
22724 KB |
Output is correct |
16 |
Correct |
69 ms |
22712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
14400 KB |
Output is correct |
2 |
Correct |
20 ms |
14284 KB |
Output is correct |
3 |
Correct |
21 ms |
14284 KB |
Output is correct |
4 |
Correct |
27 ms |
14408 KB |
Output is correct |
5 |
Correct |
29 ms |
14408 KB |
Output is correct |
6 |
Correct |
19 ms |
14616 KB |
Output is correct |
7 |
Correct |
28 ms |
14548 KB |
Output is correct |
8 |
Correct |
33 ms |
14756 KB |
Output is correct |
9 |
Correct |
28 ms |
14708 KB |
Output is correct |
10 |
Correct |
55 ms |
25304 KB |
Output is correct |
11 |
Correct |
76 ms |
26044 KB |
Output is correct |
12 |
Correct |
89 ms |
26524 KB |
Output is correct |
13 |
Correct |
109 ms |
26984 KB |
Output is correct |
14 |
Correct |
85 ms |
25796 KB |
Output is correct |
15 |
Correct |
66 ms |
22724 KB |
Output is correct |
16 |
Correct |
69 ms |
22712 KB |
Output is correct |
17 |
Correct |
97 ms |
26104 KB |
Output is correct |
18 |
Correct |
17 ms |
14408 KB |
Output is correct |
19 |
Correct |
21 ms |
14404 KB |
Output is correct |
20 |
Correct |
22 ms |
14284 KB |
Output is correct |
21 |
Correct |
29 ms |
14284 KB |
Output is correct |
22 |
Correct |
28 ms |
14284 KB |
Output is correct |
23 |
Correct |
29 ms |
14540 KB |
Output is correct |
24 |
Correct |
36 ms |
14540 KB |
Output is correct |
25 |
Correct |
62 ms |
14700 KB |
Output is correct |
26 |
Correct |
54 ms |
14696 KB |
Output is correct |
27 |
Correct |
70 ms |
25316 KB |
Output is correct |
28 |
Correct |
122 ms |
26564 KB |
Output is correct |
29 |
Correct |
129 ms |
27184 KB |
Output is correct |
30 |
Correct |
114 ms |
25924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
25320 KB |
Output is correct |
2 |
Correct |
18 ms |
14412 KB |
Output is correct |
3 |
Correct |
17 ms |
14376 KB |
Output is correct |
4 |
Correct |
20 ms |
14376 KB |
Output is correct |
5 |
Correct |
23 ms |
14628 KB |
Output is correct |
6 |
Correct |
24 ms |
14540 KB |
Output is correct |
7 |
Correct |
25 ms |
14568 KB |
Output is correct |
8 |
Correct |
105 ms |
25372 KB |
Output is correct |
9 |
Correct |
102 ms |
25280 KB |
Output is correct |
10 |
Correct |
86 ms |
23756 KB |
Output is correct |
11 |
Correct |
126 ms |
26172 KB |
Output is correct |
12 |
Correct |
17 ms |
14344 KB |
Output is correct |
13 |
Correct |
19 ms |
14284 KB |
Output is correct |
14 |
Correct |
22 ms |
14408 KB |
Output is correct |
15 |
Correct |
25 ms |
14392 KB |
Output is correct |
16 |
Correct |
28 ms |
14284 KB |
Output is correct |
17 |
Correct |
41 ms |
14700 KB |
Output is correct |
18 |
Correct |
54 ms |
14768 KB |
Output is correct |
19 |
Correct |
107 ms |
26276 KB |
Output is correct |
20 |
Correct |
143 ms |
28852 KB |
Output is correct |
21 |
Correct |
138 ms |
27936 KB |
Output is correct |
22 |
Correct |
114 ms |
26180 KB |
Output is correct |
23 |
Correct |
128 ms |
27784 KB |
Output is correct |
24 |
Correct |
81 ms |
23420 KB |
Output is correct |
25 |
Correct |
92 ms |
23920 KB |
Output is correct |
26 |
Correct |
91 ms |
23996 KB |
Output is correct |
27 |
Correct |
17 ms |
14400 KB |
Output is correct |
28 |
Correct |
20 ms |
14284 KB |
Output is correct |
29 |
Correct |
21 ms |
14284 KB |
Output is correct |
30 |
Correct |
27 ms |
14408 KB |
Output is correct |
31 |
Correct |
29 ms |
14408 KB |
Output is correct |
32 |
Correct |
19 ms |
14616 KB |
Output is correct |
33 |
Correct |
28 ms |
14548 KB |
Output is correct |
34 |
Correct |
33 ms |
14756 KB |
Output is correct |
35 |
Correct |
28 ms |
14708 KB |
Output is correct |
36 |
Correct |
55 ms |
25304 KB |
Output is correct |
37 |
Correct |
76 ms |
26044 KB |
Output is correct |
38 |
Correct |
89 ms |
26524 KB |
Output is correct |
39 |
Correct |
109 ms |
26984 KB |
Output is correct |
40 |
Correct |
85 ms |
25796 KB |
Output is correct |
41 |
Correct |
66 ms |
22724 KB |
Output is correct |
42 |
Correct |
69 ms |
22712 KB |
Output is correct |
43 |
Correct |
97 ms |
26104 KB |
Output is correct |
44 |
Correct |
17 ms |
14408 KB |
Output is correct |
45 |
Correct |
21 ms |
14404 KB |
Output is correct |
46 |
Correct |
22 ms |
14284 KB |
Output is correct |
47 |
Correct |
29 ms |
14284 KB |
Output is correct |
48 |
Correct |
28 ms |
14284 KB |
Output is correct |
49 |
Correct |
29 ms |
14540 KB |
Output is correct |
50 |
Correct |
36 ms |
14540 KB |
Output is correct |
51 |
Correct |
62 ms |
14700 KB |
Output is correct |
52 |
Correct |
54 ms |
14696 KB |
Output is correct |
53 |
Correct |
70 ms |
25316 KB |
Output is correct |
54 |
Correct |
122 ms |
26564 KB |
Output is correct |
55 |
Correct |
129 ms |
27184 KB |
Output is correct |
56 |
Correct |
114 ms |
25924 KB |
Output is correct |
57 |
Correct |
238 ms |
32964 KB |
Output is correct |
58 |
Correct |
112 ms |
26320 KB |
Output is correct |
59 |
Correct |
149 ms |
28024 KB |
Output is correct |