#include <iostream>
#include <vector>
using namespace std;
#define int long long
const int max_n=5e4+3,max_k=5,logn=17,INF=1e18;
int up[max_n][logn][max_k];
int num(int n,int j,int mod,int k){
int ans=0;
n-=(n%k);
n-=k*(1<<j);
n+=mod;
return max(ans,n);
}
int kth(int x,int y,int k){
int ans=INF,dif=x/k-y/k;
vector<int> sol(k,INF);
sol[x%k]=0;
x=x/k*k;
//cout<<x<<' ';
if(dif<1)return INF;
for(int j=logn-1;j>=0;j--){
if(dif&(1<<j)){
vector<int> sol2(k,INF);
for(int to=0;to<k;to++){
for(int cur=0;cur<k;cur++){
sol2[to]=min(sol2[to],sol[cur]+up[x+cur][j][to]);
}
}
x-=k*(1<<j);
sol=sol2;
}
}
return sol[y%k];
}
signed main(){
int k,n,m,o,x,y,w;
cin>>k>>n>>m>>o;
for(int i=0;i<n;i++)for(int j=0;j<logn;j++)for(int q=0;q<k;q++)up[i][j][q]=INF;
while(m--){
cin>>x>>y>>w;
up[y][0][x%k]=w;
}
for(int i=0;i<n;i++){
for(int j=1;j<logn;j++){
for(int to=0;to<k;to++){
for(int mid=0;mid<k;mid++){
up[i][j][to]=min(up[i][j-1][mid]+up[num(i,j-1,mid,k)][j-1][to],up[i][j][to]);
}
}
}
}
/*for(int i=0;i<n;i++){
for(int j=0;j<1;j++){
for(int to=0;to<k;to++)cout<<up[i][j][to]<<' ';
cout<<'\n';
}
}*/
while(o--){
cin>>x>>y;
int ans=kth(y,x,k);
if(ans==1e18)cout<<-1<<'\n';
else cout<<ans<<'\n';
}
}
Compilation message
toll.cpp: In function 'long long int kth(long long int, long long int, long long int)':
toll.cpp:15:9: warning: unused variable 'ans' [-Wunused-variable]
15 | int ans=INF,dif=x/k-y/k;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
33568 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
3 ms |
896 KB |
Output is correct |
7 |
Correct |
3 ms |
852 KB |
Output is correct |
8 |
Correct |
91 ms |
33548 KB |
Output is correct |
9 |
Correct |
90 ms |
33560 KB |
Output is correct |
10 |
Correct |
49 ms |
33580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
33560 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
19 ms |
980 KB |
Output is correct |
8 |
Correct |
20 ms |
1108 KB |
Output is correct |
9 |
Correct |
83 ms |
33904 KB |
Output is correct |
10 |
Correct |
198 ms |
35320 KB |
Output is correct |
11 |
Correct |
133 ms |
34716 KB |
Output is correct |
12 |
Correct |
135 ms |
34244 KB |
Output is correct |
13 |
Correct |
159 ms |
22116 KB |
Output is correct |
14 |
Correct |
95 ms |
21292 KB |
Output is correct |
15 |
Correct |
106 ms |
20964 KB |
Output is correct |
16 |
Correct |
104 ms |
20960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
4 ms |
980 KB |
Output is correct |
9 |
Correct |
4 ms |
980 KB |
Output is correct |
10 |
Correct |
63 ms |
33776 KB |
Output is correct |
11 |
Correct |
100 ms |
33968 KB |
Output is correct |
12 |
Correct |
142 ms |
34048 KB |
Output is correct |
13 |
Correct |
157 ms |
34104 KB |
Output is correct |
14 |
Correct |
124 ms |
34140 KB |
Output is correct |
15 |
Correct |
92 ms |
20756 KB |
Output is correct |
16 |
Correct |
86 ms |
20756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
4 ms |
980 KB |
Output is correct |
9 |
Correct |
4 ms |
980 KB |
Output is correct |
10 |
Correct |
63 ms |
33776 KB |
Output is correct |
11 |
Correct |
100 ms |
33968 KB |
Output is correct |
12 |
Correct |
142 ms |
34048 KB |
Output is correct |
13 |
Correct |
157 ms |
34104 KB |
Output is correct |
14 |
Correct |
124 ms |
34140 KB |
Output is correct |
15 |
Correct |
92 ms |
20756 KB |
Output is correct |
16 |
Correct |
86 ms |
20756 KB |
Output is correct |
17 |
Correct |
117 ms |
34124 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
7 ms |
1000 KB |
Output is correct |
24 |
Correct |
8 ms |
980 KB |
Output is correct |
25 |
Correct |
10 ms |
1040 KB |
Output is correct |
26 |
Correct |
9 ms |
980 KB |
Output is correct |
27 |
Correct |
74 ms |
33872 KB |
Output is correct |
28 |
Correct |
151 ms |
35164 KB |
Output is correct |
29 |
Correct |
173 ms |
35460 KB |
Output is correct |
30 |
Correct |
132 ms |
34892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
33568 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
3 ms |
896 KB |
Output is correct |
7 |
Correct |
3 ms |
852 KB |
Output is correct |
8 |
Correct |
91 ms |
33548 KB |
Output is correct |
9 |
Correct |
90 ms |
33560 KB |
Output is correct |
10 |
Correct |
49 ms |
33580 KB |
Output is correct |
11 |
Correct |
121 ms |
33560 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
19 ms |
980 KB |
Output is correct |
18 |
Correct |
20 ms |
1108 KB |
Output is correct |
19 |
Correct |
83 ms |
33904 KB |
Output is correct |
20 |
Correct |
198 ms |
35320 KB |
Output is correct |
21 |
Correct |
133 ms |
34716 KB |
Output is correct |
22 |
Correct |
135 ms |
34244 KB |
Output is correct |
23 |
Correct |
159 ms |
22116 KB |
Output is correct |
24 |
Correct |
95 ms |
21292 KB |
Output is correct |
25 |
Correct |
106 ms |
20964 KB |
Output is correct |
26 |
Correct |
104 ms |
20960 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
980 KB |
Output is correct |
33 |
Correct |
2 ms |
980 KB |
Output is correct |
34 |
Correct |
4 ms |
980 KB |
Output is correct |
35 |
Correct |
4 ms |
980 KB |
Output is correct |
36 |
Correct |
63 ms |
33776 KB |
Output is correct |
37 |
Correct |
100 ms |
33968 KB |
Output is correct |
38 |
Correct |
142 ms |
34048 KB |
Output is correct |
39 |
Correct |
157 ms |
34104 KB |
Output is correct |
40 |
Correct |
124 ms |
34140 KB |
Output is correct |
41 |
Correct |
92 ms |
20756 KB |
Output is correct |
42 |
Correct |
86 ms |
20756 KB |
Output is correct |
43 |
Correct |
117 ms |
34124 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
7 ms |
1000 KB |
Output is correct |
50 |
Correct |
8 ms |
980 KB |
Output is correct |
51 |
Correct |
10 ms |
1040 KB |
Output is correct |
52 |
Correct |
9 ms |
980 KB |
Output is correct |
53 |
Correct |
74 ms |
33872 KB |
Output is correct |
54 |
Correct |
151 ms |
35164 KB |
Output is correct |
55 |
Correct |
173 ms |
35460 KB |
Output is correct |
56 |
Correct |
132 ms |
34892 KB |
Output is correct |
57 |
Correct |
225 ms |
36300 KB |
Output is correct |
58 |
Correct |
94 ms |
33916 KB |
Output is correct |
59 |
Correct |
134 ms |
34764 KB |
Output is correct |