#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define pb push_back
const int N = 5e4+2;
const int inf = 1e9;
int k, n, b;
vector<vector<int>> mat[N][16];
vector<int> mul(vector<int> & f, vector<vector<int>> & s){
vector<int> ret(k, inf);
for(int i=0; i<k;i++){
for(int j=0; j<k; j++){
ret[i]=min(ret[i], f[j]+s[j][i]);
}
}
return ret;
}
vector<vector<int>> mul(vector<vector<int>> & f, vector<vector<int>> & s){
vector<vector<int>> ret;
for(int i=0; i<k; i++)ret.pb(mul(f[i], s));
return ret;
}
void smin(int & f, int s){
if(s<f)f=s;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int m, q;
cin >> k >> n >> m >> q;
b=(n+k-1)/k;
for(int j=0; j<16; j++){
for(int i=0; i+(1<<j)<=b; i++){
mat[i][j].resize(k);
for(int l=0; l<k; l++)mat[i][j][l].resize(k, inf);
}
}
for(int i=0; i< m; i++){
int x, y, c;
cin >> x >> y >> c;
smin(mat[x/k][0][x%k][y%k], c);
}
for(int j=1; j<16; j++){
for(int i=0; i+(1<<j)<=b; i++){
mat[i][j]=mul(mat[i][j-1], mat[i+(1<<(j-1))][j-1]);
}
}
while(q--){
int l, r;
cin >> l >> r;
if(l==r){
cout << 0 << '\n';
continue;
}
int ly = l/k;
int ry = r/k;
if(ly>=ry){
cout << -1 << '\n';
continue;
}
vector<int> st(k, inf);
st[l%k]=0;
int str=ly;
for(int i=15; i>=0; i--){
if(ly+(1<<i)<=ry){
st=mul(st, mat[ly][i]);
ly+=(1<<i);
}
}
if(st[r%k]>=inf)cout << -1 << '\n';
else cout << st[r%k] << '\n';
}
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:75:13: warning: unused variable 'str' [-Wunused-variable]
75 | int str=ly;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
66212 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
6 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
6 ms |
19804 KB |
Output is correct |
6 |
Correct |
7 ms |
19804 KB |
Output is correct |
7 |
Correct |
7 ms |
19804 KB |
Output is correct |
8 |
Correct |
137 ms |
66104 KB |
Output is correct |
9 |
Correct |
131 ms |
65904 KB |
Output is correct |
10 |
Correct |
127 ms |
65616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
63692 KB |
Output is correct |
2 |
Correct |
5 ms |
19220 KB |
Output is correct |
3 |
Correct |
5 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
5 ms |
19036 KB |
Output is correct |
6 |
Correct |
6 ms |
19036 KB |
Output is correct |
7 |
Correct |
9 ms |
19676 KB |
Output is correct |
8 |
Correct |
11 ms |
19804 KB |
Output is correct |
9 |
Correct |
132 ms |
66000 KB |
Output is correct |
10 |
Correct |
161 ms |
80788 KB |
Output is correct |
11 |
Correct |
135 ms |
63824 KB |
Output is correct |
12 |
Correct |
139 ms |
80024 KB |
Output is correct |
13 |
Correct |
106 ms |
59728 KB |
Output is correct |
14 |
Correct |
85 ms |
54288 KB |
Output is correct |
15 |
Correct |
92 ms |
58572 KB |
Output is correct |
16 |
Correct |
96 ms |
58752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19036 KB |
Output is correct |
2 |
Correct |
5 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19036 KB |
Output is correct |
4 |
Correct |
5 ms |
19036 KB |
Output is correct |
5 |
Correct |
5 ms |
19036 KB |
Output is correct |
6 |
Correct |
6 ms |
19804 KB |
Output is correct |
7 |
Correct |
6 ms |
19548 KB |
Output is correct |
8 |
Correct |
7 ms |
19804 KB |
Output is correct |
9 |
Correct |
6 ms |
19804 KB |
Output is correct |
10 |
Correct |
125 ms |
65892 KB |
Output is correct |
11 |
Correct |
109 ms |
63616 KB |
Output is correct |
12 |
Correct |
146 ms |
80684 KB |
Output is correct |
13 |
Correct |
143 ms |
80980 KB |
Output is correct |
14 |
Correct |
149 ms |
80464 KB |
Output is correct |
15 |
Correct |
89 ms |
58536 KB |
Output is correct |
16 |
Correct |
115 ms |
58480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19036 KB |
Output is correct |
2 |
Correct |
5 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19036 KB |
Output is correct |
4 |
Correct |
5 ms |
19036 KB |
Output is correct |
5 |
Correct |
5 ms |
19036 KB |
Output is correct |
6 |
Correct |
6 ms |
19804 KB |
Output is correct |
7 |
Correct |
6 ms |
19548 KB |
Output is correct |
8 |
Correct |
7 ms |
19804 KB |
Output is correct |
9 |
Correct |
6 ms |
19804 KB |
Output is correct |
10 |
Correct |
125 ms |
65892 KB |
Output is correct |
11 |
Correct |
109 ms |
63616 KB |
Output is correct |
12 |
Correct |
146 ms |
80684 KB |
Output is correct |
13 |
Correct |
143 ms |
80980 KB |
Output is correct |
14 |
Correct |
149 ms |
80464 KB |
Output is correct |
15 |
Correct |
89 ms |
58536 KB |
Output is correct |
16 |
Correct |
115 ms |
58480 KB |
Output is correct |
17 |
Correct |
110 ms |
63480 KB |
Output is correct |
18 |
Correct |
4 ms |
19032 KB |
Output is correct |
19 |
Correct |
4 ms |
19036 KB |
Output is correct |
20 |
Correct |
6 ms |
19036 KB |
Output is correct |
21 |
Correct |
5 ms |
19036 KB |
Output is correct |
22 |
Correct |
6 ms |
19248 KB |
Output is correct |
23 |
Correct |
7 ms |
19800 KB |
Output is correct |
24 |
Correct |
7 ms |
19804 KB |
Output is correct |
25 |
Correct |
8 ms |
20068 KB |
Output is correct |
26 |
Correct |
7 ms |
19804 KB |
Output is correct |
27 |
Correct |
124 ms |
66064 KB |
Output is correct |
28 |
Correct |
154 ms |
81124 KB |
Output is correct |
29 |
Correct |
150 ms |
81020 KB |
Output is correct |
30 |
Correct |
147 ms |
80304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
66212 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
6 ms |
19036 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
6 ms |
19804 KB |
Output is correct |
6 |
Correct |
7 ms |
19804 KB |
Output is correct |
7 |
Correct |
7 ms |
19804 KB |
Output is correct |
8 |
Correct |
137 ms |
66104 KB |
Output is correct |
9 |
Correct |
131 ms |
65904 KB |
Output is correct |
10 |
Correct |
127 ms |
65616 KB |
Output is correct |
11 |
Correct |
117 ms |
63692 KB |
Output is correct |
12 |
Correct |
5 ms |
19220 KB |
Output is correct |
13 |
Correct |
5 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19036 KB |
Output is correct |
15 |
Correct |
5 ms |
19036 KB |
Output is correct |
16 |
Correct |
6 ms |
19036 KB |
Output is correct |
17 |
Correct |
9 ms |
19676 KB |
Output is correct |
18 |
Correct |
11 ms |
19804 KB |
Output is correct |
19 |
Correct |
132 ms |
66000 KB |
Output is correct |
20 |
Correct |
161 ms |
80788 KB |
Output is correct |
21 |
Correct |
135 ms |
63824 KB |
Output is correct |
22 |
Correct |
139 ms |
80024 KB |
Output is correct |
23 |
Correct |
106 ms |
59728 KB |
Output is correct |
24 |
Correct |
85 ms |
54288 KB |
Output is correct |
25 |
Correct |
92 ms |
58572 KB |
Output is correct |
26 |
Correct |
96 ms |
58752 KB |
Output is correct |
27 |
Correct |
4 ms |
19036 KB |
Output is correct |
28 |
Correct |
5 ms |
19036 KB |
Output is correct |
29 |
Correct |
4 ms |
19036 KB |
Output is correct |
30 |
Correct |
5 ms |
19036 KB |
Output is correct |
31 |
Correct |
5 ms |
19036 KB |
Output is correct |
32 |
Correct |
6 ms |
19804 KB |
Output is correct |
33 |
Correct |
6 ms |
19548 KB |
Output is correct |
34 |
Correct |
7 ms |
19804 KB |
Output is correct |
35 |
Correct |
6 ms |
19804 KB |
Output is correct |
36 |
Correct |
125 ms |
65892 KB |
Output is correct |
37 |
Correct |
109 ms |
63616 KB |
Output is correct |
38 |
Correct |
146 ms |
80684 KB |
Output is correct |
39 |
Correct |
143 ms |
80980 KB |
Output is correct |
40 |
Correct |
149 ms |
80464 KB |
Output is correct |
41 |
Correct |
89 ms |
58536 KB |
Output is correct |
42 |
Correct |
115 ms |
58480 KB |
Output is correct |
43 |
Correct |
110 ms |
63480 KB |
Output is correct |
44 |
Correct |
4 ms |
19032 KB |
Output is correct |
45 |
Correct |
4 ms |
19036 KB |
Output is correct |
46 |
Correct |
6 ms |
19036 KB |
Output is correct |
47 |
Correct |
5 ms |
19036 KB |
Output is correct |
48 |
Correct |
6 ms |
19248 KB |
Output is correct |
49 |
Correct |
7 ms |
19800 KB |
Output is correct |
50 |
Correct |
7 ms |
19804 KB |
Output is correct |
51 |
Correct |
8 ms |
20068 KB |
Output is correct |
52 |
Correct |
7 ms |
19804 KB |
Output is correct |
53 |
Correct |
124 ms |
66064 KB |
Output is correct |
54 |
Correct |
154 ms |
81124 KB |
Output is correct |
55 |
Correct |
150 ms |
81020 KB |
Output is correct |
56 |
Correct |
147 ms |
80304 KB |
Output is correct |
57 |
Correct |
170 ms |
69988 KB |
Output is correct |
58 |
Correct |
156 ms |
66156 KB |
Output is correct |
59 |
Correct |
124 ms |
63672 KB |
Output is correct |