#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e4 + 12, MOD = (int)1e9 + 7;
int k, n, m, q, b;
vector<pair<int,int>> g[N], o;
vector<ll> di[N];
const int inf = 1e9 + 1;
vector<ll> tr(vector<ll> prev,int l, int r) {
vector<ll> ret(k,inf);
assert(l % k == 0);
for(int i = l; i <= r; i++) {
int f = i % k;
for(auto [to,w]:g[i]) {
ret[to % k] = min(ret[to % k],prev[f] + w);
}
}
return ret;
}
void prec() {
for(int i = (int)o.size() - 1; i >= 0; i--) {
if(i % b == 0) {
int nx = i + b;
int l = o[i].first, r = o[i].second;
for(int j = l; j <= r; j++) {
vector<ll> dd(k,inf);
dd[j%k] = 0;
// cout << j << '\n';
for(int _i = i + 1; _i < (int)o.size(); _i++) {
vector<ll> nv = tr(dd,o[_i - 1].first,o[_i - 1].second);
for(auto f:nv) {
di[j].push_back(f);
// cout << f << ' ';
}
dd = nv;
}
// cout << "_____________\n";
}
}
}
}
void test() {
cin >> k >> n >> m >> q;
for(int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
g[a].push_back({b,c});
}
for(int i = 0; i <= n; i++) {
int l = i * k, r = (i + 1) * k - 1;
if(l >= n) break;
if(r >= n) {
r = n - 1;
}
o.push_back({l,r});
}
for(int i = 0; i < n; i++) {
sort(g[i].begin(),g[i].end());
vector<pair<int,int>> nv;
for(auto [x,y]:g[i]) {
if(nv.empty() || nv.back().first != x) {
nv.emplace_back(x,y);
}
}
g[i].swap(nv);
assert((int)g[i].size() <= k);
}
b = 600;
// b = 1;
prec();
// return;
while(q--) {
int a, r;
cin >> a >> r;
if(a / k >= r / k) {
cout << -1 << '\n';
continue;
}
int x = a / k, y = r / k;
vector<ll> cur(k,inf);
cur[a%k] = 0;
while(x % b != 0) {
x++;
cur = tr(cur,o[x - 1].first,o[x - 1].second);
if(x == y) {
break;
}
}
ll res = inf;
if(x == y) {
res = cur[r % k];
} else {
for(int i = 0; i < k; i++) {
int nx = (x + 1) * k;
res = min(res,di[o[x].first + i][r - nx] + cur[i]);
}
}
if(res == inf) res = -1;
cout << res << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
Compilation message
toll.cpp: In function 'void prec()':
toll.cpp:26:17: warning: unused variable 'nx' [-Wunused-variable]
26 | int nx = i + b;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
246 ms |
22128 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2772 KB |
Output is correct |
5 |
Correct |
8 ms |
2648 KB |
Output is correct |
6 |
Correct |
8 ms |
2652 KB |
Output is correct |
7 |
Correct |
8 ms |
2652 KB |
Output is correct |
8 |
Correct |
246 ms |
22060 KB |
Output is correct |
9 |
Correct |
250 ms |
21976 KB |
Output is correct |
10 |
Correct |
161 ms |
20432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
21804 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
3 ms |
2908 KB |
Output is correct |
9 |
Correct |
117 ms |
21932 KB |
Output is correct |
10 |
Correct |
97 ms |
23244 KB |
Output is correct |
11 |
Correct |
110 ms |
21576 KB |
Output is correct |
12 |
Correct |
87 ms |
21848 KB |
Output is correct |
13 |
Correct |
63 ms |
12304 KB |
Output is correct |
14 |
Correct |
46 ms |
11084 KB |
Output is correct |
15 |
Correct |
38 ms |
10956 KB |
Output is correct |
16 |
Correct |
37 ms |
10936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
3 ms |
2908 KB |
Output is correct |
9 |
Correct |
4 ms |
2920 KB |
Output is correct |
10 |
Correct |
117 ms |
21972 KB |
Output is correct |
11 |
Correct |
114 ms |
23116 KB |
Output is correct |
12 |
Correct |
118 ms |
25212 KB |
Output is correct |
13 |
Correct |
100 ms |
25696 KB |
Output is correct |
14 |
Correct |
92 ms |
24524 KB |
Output is correct |
15 |
Correct |
41 ms |
12064 KB |
Output is correct |
16 |
Correct |
40 ms |
12048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
3 ms |
2908 KB |
Output is correct |
9 |
Correct |
4 ms |
2920 KB |
Output is correct |
10 |
Correct |
117 ms |
21972 KB |
Output is correct |
11 |
Correct |
114 ms |
23116 KB |
Output is correct |
12 |
Correct |
118 ms |
25212 KB |
Output is correct |
13 |
Correct |
100 ms |
25696 KB |
Output is correct |
14 |
Correct |
92 ms |
24524 KB |
Output is correct |
15 |
Correct |
41 ms |
12064 KB |
Output is correct |
16 |
Correct |
40 ms |
12048 KB |
Output is correct |
17 |
Correct |
167 ms |
23284 KB |
Output is correct |
18 |
Correct |
1 ms |
2648 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
22 ms |
2908 KB |
Output is correct |
24 |
Correct |
20 ms |
2908 KB |
Output is correct |
25 |
Correct |
17 ms |
2908 KB |
Output is correct |
26 |
Correct |
16 ms |
2908 KB |
Output is correct |
27 |
Correct |
178 ms |
22840 KB |
Output is correct |
28 |
Correct |
153 ms |
25288 KB |
Output is correct |
29 |
Correct |
160 ms |
25916 KB |
Output is correct |
30 |
Correct |
158 ms |
24388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
246 ms |
22128 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2772 KB |
Output is correct |
5 |
Correct |
8 ms |
2648 KB |
Output is correct |
6 |
Correct |
8 ms |
2652 KB |
Output is correct |
7 |
Correct |
8 ms |
2652 KB |
Output is correct |
8 |
Correct |
246 ms |
22060 KB |
Output is correct |
9 |
Correct |
250 ms |
21976 KB |
Output is correct |
10 |
Correct |
161 ms |
20432 KB |
Output is correct |
11 |
Correct |
114 ms |
21804 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
3 ms |
2908 KB |
Output is correct |
19 |
Correct |
117 ms |
21932 KB |
Output is correct |
20 |
Correct |
97 ms |
23244 KB |
Output is correct |
21 |
Correct |
110 ms |
21576 KB |
Output is correct |
22 |
Correct |
87 ms |
21848 KB |
Output is correct |
23 |
Correct |
63 ms |
12304 KB |
Output is correct |
24 |
Correct |
46 ms |
11084 KB |
Output is correct |
25 |
Correct |
38 ms |
10956 KB |
Output is correct |
26 |
Correct |
37 ms |
10936 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
3 ms |
2908 KB |
Output is correct |
35 |
Correct |
4 ms |
2920 KB |
Output is correct |
36 |
Correct |
117 ms |
21972 KB |
Output is correct |
37 |
Correct |
114 ms |
23116 KB |
Output is correct |
38 |
Correct |
118 ms |
25212 KB |
Output is correct |
39 |
Correct |
100 ms |
25696 KB |
Output is correct |
40 |
Correct |
92 ms |
24524 KB |
Output is correct |
41 |
Correct |
41 ms |
12064 KB |
Output is correct |
42 |
Correct |
40 ms |
12048 KB |
Output is correct |
43 |
Correct |
167 ms |
23284 KB |
Output is correct |
44 |
Correct |
1 ms |
2648 KB |
Output is correct |
45 |
Correct |
1 ms |
2652 KB |
Output is correct |
46 |
Correct |
1 ms |
2652 KB |
Output is correct |
47 |
Correct |
1 ms |
2648 KB |
Output is correct |
48 |
Correct |
1 ms |
2652 KB |
Output is correct |
49 |
Correct |
22 ms |
2908 KB |
Output is correct |
50 |
Correct |
20 ms |
2908 KB |
Output is correct |
51 |
Correct |
17 ms |
2908 KB |
Output is correct |
52 |
Correct |
16 ms |
2908 KB |
Output is correct |
53 |
Correct |
178 ms |
22840 KB |
Output is correct |
54 |
Correct |
153 ms |
25288 KB |
Output is correct |
55 |
Correct |
160 ms |
25916 KB |
Output is correct |
56 |
Correct |
158 ms |
24388 KB |
Output is correct |
57 |
Correct |
403 ms |
26700 KB |
Output is correct |
58 |
Correct |
269 ms |
22988 KB |
Output is correct |
59 |
Correct |
295 ms |
23440 KB |
Output is correct |