#include <bits/stdc++.h>
//#include "debug.h"
//// #pragma GCC optimize("-O3")
//// #pragma GCC optimize("no-stack-protector")
//// #pragma GCC optimize("fast-math")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define f first
#define s second
#define se second
#define vec vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define sz(x) (int)x.size()
#define pw(x) (1ll<<x)
#define p_b push_back
#define m_p make_pair
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define sq(x) (x)*(x)
//#define int long long
using namespace std;
//using namespace __gnu_pbds;
typedef pair<long long, long long> PII;
typedef pair<int, int> pii;
typedef long double ld;
typedef long long ll;
typedef pair<long long, long long > pll;
//template <typename T> umax(T &a, T b){return (a<b ? a=b,1 : 0);}
//template <typename T> umin(T &a, T b){return (a>b ? a=b,1 : 0);}
//typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> o_set;
const int N=5e4+1;
const int K=5;
const ll inf=1e18;
const ld eps = 1e-9;
const int ppp=73;
const int M=1e9+7;
const ll tx[4]={-1,1,0,0};
const ll ty[4]={0,0,-1,1};
const int lg=21;
const char rev_to[4]={'U','D','L','R'};
auto rnd=bind(uniform_int_distribution<ll>(1,1e9),mt19937(time(0)));
ll up[N][lg][K][K];
void umin(ll &a,ll b){
a=min(a,b);
}
signed main()
{
fast_io;
int k,n,m,q;
cin>>k>>n>>m>>q;
for(int i=0;i<N;i++){
for(int j=0;j<lg;j++){
for(int t=0;t<K;t++){
for(int p=0;p<K;p++)
up[i][j][t][p]=inf;
}
}
}
for(int i=0;i<m;i++){
int a,b,t;
cin>>a>>b>>t;
umin(up[a/k][0][a%k][b%k],t);
}
for(int j=1;j<lg;j++){
for(int i=0;i<N;i++){
for(int t=0;t<k;t++){
if((i+(1<<j))>N)break;
vec<ll>dst(k,inf);
for(int pt=0;pt<k;pt++){
for(int nt=0;nt<k;nt++){
umin(dst[nt],up[i][j-1][t][pt]+up[(i+(1<<(j-1)))][j-1][pt][nt]);
}
}
for(int pt=0;pt<k;pt++)
up[i][j][t][pt]=dst[pt];
}
}
}
while(q--){
int a,b;
cin>>a>>b;
vec<ll>dst(k,inf);
dst[a%k]=0;
int na=a/k,nb=b/k;
for(int j=lg-1;j>=0;j--){
if((na+(1<<j))>nb)continue;
// debug()<<imie(dst);
vec<ll>nd(k,inf);
for(int t=0;t<k;t++){
for(int nt=0;nt<k;nt++){
umin(nd[nt],dst[t]+up[na][j][t][nt]);
}
}
dst=nd;
na+=(1<<j);
// debug()<<imie(dst);
}
cout<<(dst[b%k]==inf?-1:dst[b%k])<<"\n";
}
return 0;
}
/*
5 14 5 1
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
206828 KB |
Output is correct |
2 |
Correct |
141 ms |
205804 KB |
Output is correct |
3 |
Correct |
136 ms |
205804 KB |
Output is correct |
4 |
Correct |
139 ms |
205804 KB |
Output is correct |
5 |
Correct |
157 ms |
205804 KB |
Output is correct |
6 |
Correct |
154 ms |
205860 KB |
Output is correct |
7 |
Correct |
140 ms |
205932 KB |
Output is correct |
8 |
Correct |
177 ms |
206828 KB |
Output is correct |
9 |
Correct |
174 ms |
206760 KB |
Output is correct |
10 |
Correct |
160 ms |
206060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
289 ms |
207568 KB |
Output is correct |
2 |
Correct |
138 ms |
205804 KB |
Output is correct |
3 |
Correct |
241 ms |
205932 KB |
Output is correct |
4 |
Correct |
303 ms |
206060 KB |
Output is correct |
5 |
Correct |
322 ms |
205804 KB |
Output is correct |
6 |
Correct |
397 ms |
205932 KB |
Output is correct |
7 |
Correct |
146 ms |
205932 KB |
Output is correct |
8 |
Correct |
237 ms |
205932 KB |
Output is correct |
9 |
Correct |
172 ms |
206828 KB |
Output is correct |
10 |
Correct |
350 ms |
208132 KB |
Output is correct |
11 |
Correct |
279 ms |
207596 KB |
Output is correct |
12 |
Correct |
321 ms |
207212 KB |
Output is correct |
13 |
Correct |
463 ms |
208236 KB |
Output is correct |
14 |
Correct |
318 ms |
207340 KB |
Output is correct |
15 |
Correct |
423 ms |
207212 KB |
Output is correct |
16 |
Correct |
437 ms |
207084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
205804 KB |
Output is correct |
2 |
Correct |
250 ms |
205804 KB |
Output is correct |
3 |
Correct |
277 ms |
205804 KB |
Output is correct |
4 |
Correct |
319 ms |
205804 KB |
Output is correct |
5 |
Correct |
431 ms |
205916 KB |
Output is correct |
6 |
Correct |
140 ms |
205804 KB |
Output is correct |
7 |
Correct |
231 ms |
205804 KB |
Output is correct |
8 |
Correct |
436 ms |
205864 KB |
Output is correct |
9 |
Correct |
356 ms |
205932 KB |
Output is correct |
10 |
Correct |
170 ms |
206700 KB |
Output is correct |
11 |
Correct |
265 ms |
207592 KB |
Output is correct |
12 |
Correct |
325 ms |
208108 KB |
Output is correct |
13 |
Correct |
335 ms |
208364 KB |
Output is correct |
14 |
Correct |
326 ms |
207596 KB |
Output is correct |
15 |
Correct |
417 ms |
207084 KB |
Output is correct |
16 |
Correct |
433 ms |
206956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
205804 KB |
Output is correct |
2 |
Correct |
250 ms |
205804 KB |
Output is correct |
3 |
Correct |
277 ms |
205804 KB |
Output is correct |
4 |
Correct |
319 ms |
205804 KB |
Output is correct |
5 |
Correct |
431 ms |
205916 KB |
Output is correct |
6 |
Correct |
140 ms |
205804 KB |
Output is correct |
7 |
Correct |
231 ms |
205804 KB |
Output is correct |
8 |
Correct |
436 ms |
205864 KB |
Output is correct |
9 |
Correct |
356 ms |
205932 KB |
Output is correct |
10 |
Correct |
170 ms |
206700 KB |
Output is correct |
11 |
Correct |
265 ms |
207592 KB |
Output is correct |
12 |
Correct |
325 ms |
208108 KB |
Output is correct |
13 |
Correct |
335 ms |
208364 KB |
Output is correct |
14 |
Correct |
326 ms |
207596 KB |
Output is correct |
15 |
Correct |
417 ms |
207084 KB |
Output is correct |
16 |
Correct |
433 ms |
206956 KB |
Output is correct |
17 |
Correct |
285 ms |
207596 KB |
Output is correct |
18 |
Correct |
164 ms |
205804 KB |
Output is correct |
19 |
Correct |
229 ms |
205804 KB |
Output is correct |
20 |
Correct |
285 ms |
205804 KB |
Output is correct |
21 |
Correct |
325 ms |
205852 KB |
Output is correct |
22 |
Correct |
395 ms |
205804 KB |
Output is correct |
23 |
Correct |
139 ms |
205932 KB |
Output is correct |
24 |
Correct |
239 ms |
205932 KB |
Output is correct |
25 |
Correct |
404 ms |
205976 KB |
Output is correct |
26 |
Correct |
329 ms |
205932 KB |
Output is correct |
27 |
Correct |
164 ms |
206700 KB |
Output is correct |
28 |
Correct |
331 ms |
208108 KB |
Output is correct |
29 |
Correct |
370 ms |
208348 KB |
Output is correct |
30 |
Correct |
328 ms |
207852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
206828 KB |
Output is correct |
2 |
Correct |
141 ms |
205804 KB |
Output is correct |
3 |
Correct |
136 ms |
205804 KB |
Output is correct |
4 |
Correct |
139 ms |
205804 KB |
Output is correct |
5 |
Correct |
157 ms |
205804 KB |
Output is correct |
6 |
Correct |
154 ms |
205860 KB |
Output is correct |
7 |
Correct |
140 ms |
205932 KB |
Output is correct |
8 |
Correct |
177 ms |
206828 KB |
Output is correct |
9 |
Correct |
174 ms |
206760 KB |
Output is correct |
10 |
Correct |
160 ms |
206060 KB |
Output is correct |
11 |
Correct |
289 ms |
207568 KB |
Output is correct |
12 |
Correct |
138 ms |
205804 KB |
Output is correct |
13 |
Correct |
241 ms |
205932 KB |
Output is correct |
14 |
Correct |
303 ms |
206060 KB |
Output is correct |
15 |
Correct |
322 ms |
205804 KB |
Output is correct |
16 |
Correct |
397 ms |
205932 KB |
Output is correct |
17 |
Correct |
146 ms |
205932 KB |
Output is correct |
18 |
Correct |
237 ms |
205932 KB |
Output is correct |
19 |
Correct |
172 ms |
206828 KB |
Output is correct |
20 |
Correct |
350 ms |
208132 KB |
Output is correct |
21 |
Correct |
279 ms |
207596 KB |
Output is correct |
22 |
Correct |
321 ms |
207212 KB |
Output is correct |
23 |
Correct |
463 ms |
208236 KB |
Output is correct |
24 |
Correct |
318 ms |
207340 KB |
Output is correct |
25 |
Correct |
423 ms |
207212 KB |
Output is correct |
26 |
Correct |
437 ms |
207084 KB |
Output is correct |
27 |
Correct |
137 ms |
205804 KB |
Output is correct |
28 |
Correct |
250 ms |
205804 KB |
Output is correct |
29 |
Correct |
277 ms |
205804 KB |
Output is correct |
30 |
Correct |
319 ms |
205804 KB |
Output is correct |
31 |
Correct |
431 ms |
205916 KB |
Output is correct |
32 |
Correct |
140 ms |
205804 KB |
Output is correct |
33 |
Correct |
231 ms |
205804 KB |
Output is correct |
34 |
Correct |
436 ms |
205864 KB |
Output is correct |
35 |
Correct |
356 ms |
205932 KB |
Output is correct |
36 |
Correct |
170 ms |
206700 KB |
Output is correct |
37 |
Correct |
265 ms |
207592 KB |
Output is correct |
38 |
Correct |
325 ms |
208108 KB |
Output is correct |
39 |
Correct |
335 ms |
208364 KB |
Output is correct |
40 |
Correct |
326 ms |
207596 KB |
Output is correct |
41 |
Correct |
417 ms |
207084 KB |
Output is correct |
42 |
Correct |
433 ms |
206956 KB |
Output is correct |
43 |
Correct |
285 ms |
207596 KB |
Output is correct |
44 |
Correct |
164 ms |
205804 KB |
Output is correct |
45 |
Correct |
229 ms |
205804 KB |
Output is correct |
46 |
Correct |
285 ms |
205804 KB |
Output is correct |
47 |
Correct |
325 ms |
205852 KB |
Output is correct |
48 |
Correct |
395 ms |
205804 KB |
Output is correct |
49 |
Correct |
139 ms |
205932 KB |
Output is correct |
50 |
Correct |
239 ms |
205932 KB |
Output is correct |
51 |
Correct |
404 ms |
205976 KB |
Output is correct |
52 |
Correct |
329 ms |
205932 KB |
Output is correct |
53 |
Correct |
164 ms |
206700 KB |
Output is correct |
54 |
Correct |
331 ms |
208108 KB |
Output is correct |
55 |
Correct |
370 ms |
208348 KB |
Output is correct |
56 |
Correct |
328 ms |
207852 KB |
Output is correct |
57 |
Correct |
428 ms |
209260 KB |
Output is correct |
58 |
Correct |
178 ms |
206828 KB |
Output is correct |
59 |
Correct |
284 ms |
207724 KB |
Output is correct |