#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define F first
#define S second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define forbn(i, b, n) for(int i = b; i < n; ++i)
#define sz(v) (int)v.size()
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define mp make_pair
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
//typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
template <class T> inline void mineq(T &a, T b) { a = min(a, b); }
template <class T> inline void maxeq(T &a, T b) { a = max(a, b); }
const int INF = 1000 * 1000 * 1000;
const int MX = 50 * 1000 + 10;
const int mxpw = 20;
int k, n, m, o;
vii gr[MX][mxpw];
void next_pos(vii& positions, vii& npositions, int pw) {
vi mns(k, INF);
int fac = -1;
for(ii pos_t: positions) {
int pos = pos_t.F, t = pos_t.S;
for(ii nb2_t: gr[pos][pw]) {
int nb2 = nb2_t.F, t2 = nb2_t.S;
mineq(mns[nb2 % k], t + t2);
fac = nb2 / k;
}
}
npositions.clear();
forn(t, k) {
if(mns[t] != INF) {
npositions.eb(k * fac + t, mns[t]);
}
}
}
void solve() {
cin >> k >> n >> m >> o;
forn(_, m) {
int a, b, t;
cin >> a >> b >> t;
gr[a][0].eb(b, t);
}
forbn(d, 1, mxpw)
forn(a, n)
next_pos(gr[a][d - 1], gr[a][d], d - 1);
forn(_, o) {
int a, b;
cin >> a >> b;
bool printed = false;
int diff = b / k - a / k;
if(diff >= 0) {
vii positions;
positions.eb(a, 0);
for(int pw = 0; diff; pw++, diff >>= 1)
if(diff & 1)
next_pos(positions, positions, pw);
for(ii pos: positions) {
if(pos.F == b) {
cout << pos.S << '\n';
printed = true;
}
}
}
if(!printed)
cout << -1 << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// cout.tie(nullptr);
// freopen("262144.in", "r", stdin);
// freopen("262144.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
46776 KB |
Output is correct |
2 |
Correct |
12 ms |
23788 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
11 ms |
23756 KB |
Output is correct |
5 |
Correct |
13 ms |
23884 KB |
Output is correct |
6 |
Correct |
12 ms |
23868 KB |
Output is correct |
7 |
Correct |
12 ms |
23936 KB |
Output is correct |
8 |
Correct |
111 ms |
38816 KB |
Output is correct |
9 |
Correct |
84 ms |
30076 KB |
Output is correct |
10 |
Correct |
51 ms |
23840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
43604 KB |
Output is correct |
2 |
Correct |
11 ms |
23684 KB |
Output is correct |
3 |
Correct |
13 ms |
23756 KB |
Output is correct |
4 |
Correct |
11 ms |
23756 KB |
Output is correct |
5 |
Correct |
12 ms |
23844 KB |
Output is correct |
6 |
Correct |
13 ms |
23784 KB |
Output is correct |
7 |
Correct |
23 ms |
24028 KB |
Output is correct |
8 |
Correct |
27 ms |
24140 KB |
Output is correct |
9 |
Correct |
92 ms |
38760 KB |
Output is correct |
10 |
Correct |
242 ms |
54516 KB |
Output is correct |
11 |
Correct |
171 ms |
45356 KB |
Output is correct |
12 |
Correct |
119 ms |
32220 KB |
Output is correct |
13 |
Correct |
233 ms |
51588 KB |
Output is correct |
14 |
Correct |
128 ms |
41456 KB |
Output is correct |
15 |
Correct |
185 ms |
46976 KB |
Output is correct |
16 |
Correct |
195 ms |
46844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23796 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
13 ms |
23760 KB |
Output is correct |
5 |
Correct |
12 ms |
23800 KB |
Output is correct |
6 |
Correct |
13 ms |
23884 KB |
Output is correct |
7 |
Correct |
13 ms |
24004 KB |
Output is correct |
8 |
Correct |
16 ms |
24268 KB |
Output is correct |
9 |
Correct |
15 ms |
24140 KB |
Output is correct |
10 |
Correct |
80 ms |
39716 KB |
Output is correct |
11 |
Correct |
165 ms |
46644 KB |
Output is correct |
12 |
Correct |
227 ms |
56384 KB |
Output is correct |
13 |
Correct |
255 ms |
54860 KB |
Output is correct |
14 |
Correct |
264 ms |
54504 KB |
Output is correct |
15 |
Correct |
185 ms |
47948 KB |
Output is correct |
16 |
Correct |
189 ms |
47992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23796 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
13 ms |
23760 KB |
Output is correct |
5 |
Correct |
12 ms |
23800 KB |
Output is correct |
6 |
Correct |
13 ms |
23884 KB |
Output is correct |
7 |
Correct |
13 ms |
24004 KB |
Output is correct |
8 |
Correct |
16 ms |
24268 KB |
Output is correct |
9 |
Correct |
15 ms |
24140 KB |
Output is correct |
10 |
Correct |
80 ms |
39716 KB |
Output is correct |
11 |
Correct |
165 ms |
46644 KB |
Output is correct |
12 |
Correct |
227 ms |
56384 KB |
Output is correct |
13 |
Correct |
255 ms |
54860 KB |
Output is correct |
14 |
Correct |
264 ms |
54504 KB |
Output is correct |
15 |
Correct |
185 ms |
47948 KB |
Output is correct |
16 |
Correct |
189 ms |
47992 KB |
Output is correct |
17 |
Correct |
166 ms |
46716 KB |
Output is correct |
18 |
Correct |
11 ms |
23808 KB |
Output is correct |
19 |
Correct |
12 ms |
23808 KB |
Output is correct |
20 |
Correct |
11 ms |
23800 KB |
Output is correct |
21 |
Correct |
12 ms |
23688 KB |
Output is correct |
22 |
Correct |
12 ms |
23800 KB |
Output is correct |
23 |
Correct |
13 ms |
24012 KB |
Output is correct |
24 |
Correct |
17 ms |
24064 KB |
Output is correct |
25 |
Correct |
19 ms |
24396 KB |
Output is correct |
26 |
Correct |
19 ms |
24192 KB |
Output is correct |
27 |
Correct |
83 ms |
39388 KB |
Output is correct |
28 |
Correct |
253 ms |
56424 KB |
Output is correct |
29 |
Correct |
255 ms |
56832 KB |
Output is correct |
30 |
Correct |
220 ms |
52404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
46776 KB |
Output is correct |
2 |
Correct |
12 ms |
23788 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
11 ms |
23756 KB |
Output is correct |
5 |
Correct |
13 ms |
23884 KB |
Output is correct |
6 |
Correct |
12 ms |
23868 KB |
Output is correct |
7 |
Correct |
12 ms |
23936 KB |
Output is correct |
8 |
Correct |
111 ms |
38816 KB |
Output is correct |
9 |
Correct |
84 ms |
30076 KB |
Output is correct |
10 |
Correct |
51 ms |
23840 KB |
Output is correct |
11 |
Correct |
177 ms |
43604 KB |
Output is correct |
12 |
Correct |
11 ms |
23684 KB |
Output is correct |
13 |
Correct |
13 ms |
23756 KB |
Output is correct |
14 |
Correct |
11 ms |
23756 KB |
Output is correct |
15 |
Correct |
12 ms |
23844 KB |
Output is correct |
16 |
Correct |
13 ms |
23784 KB |
Output is correct |
17 |
Correct |
23 ms |
24028 KB |
Output is correct |
18 |
Correct |
27 ms |
24140 KB |
Output is correct |
19 |
Correct |
92 ms |
38760 KB |
Output is correct |
20 |
Correct |
242 ms |
54516 KB |
Output is correct |
21 |
Correct |
171 ms |
45356 KB |
Output is correct |
22 |
Correct |
119 ms |
32220 KB |
Output is correct |
23 |
Correct |
233 ms |
51588 KB |
Output is correct |
24 |
Correct |
128 ms |
41456 KB |
Output is correct |
25 |
Correct |
185 ms |
46976 KB |
Output is correct |
26 |
Correct |
195 ms |
46844 KB |
Output is correct |
27 |
Correct |
12 ms |
23756 KB |
Output is correct |
28 |
Correct |
12 ms |
23796 KB |
Output is correct |
29 |
Correct |
12 ms |
23756 KB |
Output is correct |
30 |
Correct |
13 ms |
23760 KB |
Output is correct |
31 |
Correct |
12 ms |
23800 KB |
Output is correct |
32 |
Correct |
13 ms |
23884 KB |
Output is correct |
33 |
Correct |
13 ms |
24004 KB |
Output is correct |
34 |
Correct |
16 ms |
24268 KB |
Output is correct |
35 |
Correct |
15 ms |
24140 KB |
Output is correct |
36 |
Correct |
80 ms |
39716 KB |
Output is correct |
37 |
Correct |
165 ms |
46644 KB |
Output is correct |
38 |
Correct |
227 ms |
56384 KB |
Output is correct |
39 |
Correct |
255 ms |
54860 KB |
Output is correct |
40 |
Correct |
264 ms |
54504 KB |
Output is correct |
41 |
Correct |
185 ms |
47948 KB |
Output is correct |
42 |
Correct |
189 ms |
47992 KB |
Output is correct |
43 |
Correct |
166 ms |
46716 KB |
Output is correct |
44 |
Correct |
11 ms |
23808 KB |
Output is correct |
45 |
Correct |
12 ms |
23808 KB |
Output is correct |
46 |
Correct |
11 ms |
23800 KB |
Output is correct |
47 |
Correct |
12 ms |
23688 KB |
Output is correct |
48 |
Correct |
12 ms |
23800 KB |
Output is correct |
49 |
Correct |
13 ms |
24012 KB |
Output is correct |
50 |
Correct |
17 ms |
24064 KB |
Output is correct |
51 |
Correct |
19 ms |
24396 KB |
Output is correct |
52 |
Correct |
19 ms |
24192 KB |
Output is correct |
53 |
Correct |
83 ms |
39388 KB |
Output is correct |
54 |
Correct |
253 ms |
56424 KB |
Output is correct |
55 |
Correct |
255 ms |
56832 KB |
Output is correct |
56 |
Correct |
220 ms |
52404 KB |
Output is correct |
57 |
Correct |
359 ms |
56092 KB |
Output is correct |
58 |
Correct |
93 ms |
39540 KB |
Output is correct |
59 |
Correct |
208 ms |
46848 KB |
Output is correct |