#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2")
#include<bits/stdc++.h>
#define all(x) (x).begin() , (x).end()
#define pll pair<long long, long long>
#define fi first
#define se second
#define bit(i,j) ((j >> i) & 1)
#define lowbit(x) (x & (-x))
#define sigma main
using namespace std;
const long long inf = 1e9+1;
const int mod = 998244353;
const int MAXN = 5e4+100;
struct Matrix{
vector<vector<int>> d;
void init(int n , int m , int v){
d.resize(n , vector<int>(m , v));
}
};
Matrix operator* (const Matrix &a , const Matrix &b){
Matrix c;
c.init(a.d.size() , b.d[0].size() , inf);
for(int i = 0 ; i < a.d.size() ; i++){
for(int k = 0 ; k < a.d[0].size() ; k++){
for(int j = 0 ; j < b.d[0].size() ; j++){
c.d[i][j] = min(c.d[i][j] , a.d[i][k] + b.d[k][j]);
}
}
}
return c;
}
Matrix M[MAXN];
Matrix Sum[MAXN][17];
int up[MAXN][17];
int32_t sigma(){
//freopen("COLOREDBALLS.inp", "r", stdin);
//freopen("COLOREDBALLS.out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
int k , n , m , o; cin >> k >> n >> m >> o;
int sum = n / k;
for(int i = 0 ; i <= sum + 1 ; i++) M[i].init(k , k , inf);
for(int i = 1 ; i <= m ; i++){
int a , b , t; cin >> a >> b >> t;
M[a/k].d[a%k][b%k] = t;
}
for(int i = 0 ; i < k ; i++) M[sum + 1].d[i][i] = 0;
for(int i = 0 ; i <= sum ; i++) up[i][0] = i + 1 , Sum[i][0] = M[i];
up[sum + 1][0] = sum + 1;
for(int j = 1 ; j < 17 ; j++){
for(int i = 0 ; i + (1 << j) - 1 <= sum ; i++){
up[i][j] = up[up[i][j-1]][j-1];
Sum[i][j] = Sum[i][j-1] * Sum[up[i][j-1]][j-1];
}
}
while(o--){
int a , b; cin >> a >> b;
int s = a/k , t = b/k;
if(a > b){
cout << -1 << "\n"; continue;
}
Matrix T;
T.init(1 , k , inf);
T.d[0][a % k] = 0;
int D = t - s , pos = s;
for(int j = 16 ; j >= 0 ; j--){
if(bit(j , D)) T = T * Sum[pos][j] , pos = up[pos][j];
}
cout << (T.d[0][b % k] >= inf ? -1 : T.d[0][b % k]) << "\n";
}
return 0;
}
Compilation message
toll.cpp: In function 'Matrix operator*(const Matrix&, const Matrix&)':
toll.cpp:26:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i = 0 ; i < a.d.size() ; i++){
| ~~^~~~~~~~~~~~
toll.cpp:27:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int k = 0 ; k < a.d[0].size() ; k++){
| ~~^~~~~~~~~~~~~~~
toll.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int j = 0 ; j < b.d[0].size() ; j++){
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
74064 KB |
Output is correct |
2 |
Correct |
10 ms |
21592 KB |
Output is correct |
3 |
Correct |
11 ms |
21592 KB |
Output is correct |
4 |
Correct |
12 ms |
21596 KB |
Output is correct |
5 |
Correct |
12 ms |
22108 KB |
Output is correct |
6 |
Correct |
12 ms |
22108 KB |
Output is correct |
7 |
Correct |
12 ms |
22332 KB |
Output is correct |
8 |
Correct |
114 ms |
74128 KB |
Output is correct |
9 |
Correct |
121 ms |
74064 KB |
Output is correct |
10 |
Correct |
107 ms |
74068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
69204 KB |
Output is correct |
2 |
Correct |
11 ms |
21592 KB |
Output is correct |
3 |
Correct |
11 ms |
21592 KB |
Output is correct |
4 |
Correct |
11 ms |
21596 KB |
Output is correct |
5 |
Correct |
12 ms |
21592 KB |
Output is correct |
6 |
Correct |
12 ms |
21596 KB |
Output is correct |
7 |
Correct |
17 ms |
22216 KB |
Output is correct |
8 |
Correct |
19 ms |
22232 KB |
Output is correct |
9 |
Correct |
123 ms |
74092 KB |
Output is correct |
10 |
Correct |
129 ms |
63060 KB |
Output is correct |
11 |
Correct |
107 ms |
69200 KB |
Output is correct |
12 |
Correct |
123 ms |
62988 KB |
Output is correct |
13 |
Correct |
90 ms |
43348 KB |
Output is correct |
14 |
Correct |
75 ms |
45284 KB |
Output is correct |
15 |
Correct |
68 ms |
43344 KB |
Output is correct |
16 |
Correct |
68 ms |
43344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21596 KB |
Output is correct |
2 |
Correct |
14 ms |
21636 KB |
Output is correct |
3 |
Correct |
12 ms |
21596 KB |
Output is correct |
4 |
Correct |
15 ms |
21676 KB |
Output is correct |
5 |
Correct |
12 ms |
21592 KB |
Output is correct |
6 |
Correct |
13 ms |
22328 KB |
Output is correct |
7 |
Correct |
12 ms |
22104 KB |
Output is correct |
8 |
Correct |
13 ms |
21916 KB |
Output is correct |
9 |
Correct |
16 ms |
22108 KB |
Output is correct |
10 |
Correct |
110 ms |
74068 KB |
Output is correct |
11 |
Correct |
97 ms |
69176 KB |
Output is correct |
12 |
Correct |
119 ms |
62780 KB |
Output is correct |
13 |
Correct |
124 ms |
62900 KB |
Output is correct |
14 |
Correct |
112 ms |
62800 KB |
Output is correct |
15 |
Correct |
62 ms |
43348 KB |
Output is correct |
16 |
Correct |
62 ms |
43348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21596 KB |
Output is correct |
2 |
Correct |
14 ms |
21636 KB |
Output is correct |
3 |
Correct |
12 ms |
21596 KB |
Output is correct |
4 |
Correct |
15 ms |
21676 KB |
Output is correct |
5 |
Correct |
12 ms |
21592 KB |
Output is correct |
6 |
Correct |
13 ms |
22328 KB |
Output is correct |
7 |
Correct |
12 ms |
22104 KB |
Output is correct |
8 |
Correct |
13 ms |
21916 KB |
Output is correct |
9 |
Correct |
16 ms |
22108 KB |
Output is correct |
10 |
Correct |
110 ms |
74068 KB |
Output is correct |
11 |
Correct |
97 ms |
69176 KB |
Output is correct |
12 |
Correct |
119 ms |
62780 KB |
Output is correct |
13 |
Correct |
124 ms |
62900 KB |
Output is correct |
14 |
Correct |
112 ms |
62800 KB |
Output is correct |
15 |
Correct |
62 ms |
43348 KB |
Output is correct |
16 |
Correct |
62 ms |
43348 KB |
Output is correct |
17 |
Correct |
98 ms |
69200 KB |
Output is correct |
18 |
Correct |
11 ms |
21592 KB |
Output is correct |
19 |
Correct |
12 ms |
21596 KB |
Output is correct |
20 |
Correct |
11 ms |
21596 KB |
Output is correct |
21 |
Correct |
11 ms |
21640 KB |
Output is correct |
22 |
Correct |
11 ms |
21596 KB |
Output is correct |
23 |
Correct |
14 ms |
22220 KB |
Output is correct |
24 |
Correct |
15 ms |
22104 KB |
Output is correct |
25 |
Correct |
14 ms |
21912 KB |
Output is correct |
26 |
Correct |
13 ms |
22108 KB |
Output is correct |
27 |
Correct |
106 ms |
74068 KB |
Output is correct |
28 |
Correct |
130 ms |
62808 KB |
Output is correct |
29 |
Correct |
122 ms |
62844 KB |
Output is correct |
30 |
Correct |
127 ms |
63060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
74064 KB |
Output is correct |
2 |
Correct |
10 ms |
21592 KB |
Output is correct |
3 |
Correct |
11 ms |
21592 KB |
Output is correct |
4 |
Correct |
12 ms |
21596 KB |
Output is correct |
5 |
Correct |
12 ms |
22108 KB |
Output is correct |
6 |
Correct |
12 ms |
22108 KB |
Output is correct |
7 |
Correct |
12 ms |
22332 KB |
Output is correct |
8 |
Correct |
114 ms |
74128 KB |
Output is correct |
9 |
Correct |
121 ms |
74064 KB |
Output is correct |
10 |
Correct |
107 ms |
74068 KB |
Output is correct |
11 |
Correct |
106 ms |
69204 KB |
Output is correct |
12 |
Correct |
11 ms |
21592 KB |
Output is correct |
13 |
Correct |
11 ms |
21592 KB |
Output is correct |
14 |
Correct |
11 ms |
21596 KB |
Output is correct |
15 |
Correct |
12 ms |
21592 KB |
Output is correct |
16 |
Correct |
12 ms |
21596 KB |
Output is correct |
17 |
Correct |
17 ms |
22216 KB |
Output is correct |
18 |
Correct |
19 ms |
22232 KB |
Output is correct |
19 |
Correct |
123 ms |
74092 KB |
Output is correct |
20 |
Correct |
129 ms |
63060 KB |
Output is correct |
21 |
Correct |
107 ms |
69200 KB |
Output is correct |
22 |
Correct |
123 ms |
62988 KB |
Output is correct |
23 |
Correct |
90 ms |
43348 KB |
Output is correct |
24 |
Correct |
75 ms |
45284 KB |
Output is correct |
25 |
Correct |
68 ms |
43344 KB |
Output is correct |
26 |
Correct |
68 ms |
43344 KB |
Output is correct |
27 |
Correct |
13 ms |
21596 KB |
Output is correct |
28 |
Correct |
14 ms |
21636 KB |
Output is correct |
29 |
Correct |
12 ms |
21596 KB |
Output is correct |
30 |
Correct |
15 ms |
21676 KB |
Output is correct |
31 |
Correct |
12 ms |
21592 KB |
Output is correct |
32 |
Correct |
13 ms |
22328 KB |
Output is correct |
33 |
Correct |
12 ms |
22104 KB |
Output is correct |
34 |
Correct |
13 ms |
21916 KB |
Output is correct |
35 |
Correct |
16 ms |
22108 KB |
Output is correct |
36 |
Correct |
110 ms |
74068 KB |
Output is correct |
37 |
Correct |
97 ms |
69176 KB |
Output is correct |
38 |
Correct |
119 ms |
62780 KB |
Output is correct |
39 |
Correct |
124 ms |
62900 KB |
Output is correct |
40 |
Correct |
112 ms |
62800 KB |
Output is correct |
41 |
Correct |
62 ms |
43348 KB |
Output is correct |
42 |
Correct |
62 ms |
43348 KB |
Output is correct |
43 |
Correct |
98 ms |
69200 KB |
Output is correct |
44 |
Correct |
11 ms |
21592 KB |
Output is correct |
45 |
Correct |
12 ms |
21596 KB |
Output is correct |
46 |
Correct |
11 ms |
21596 KB |
Output is correct |
47 |
Correct |
11 ms |
21640 KB |
Output is correct |
48 |
Correct |
11 ms |
21596 KB |
Output is correct |
49 |
Correct |
14 ms |
22220 KB |
Output is correct |
50 |
Correct |
15 ms |
22104 KB |
Output is correct |
51 |
Correct |
14 ms |
21912 KB |
Output is correct |
52 |
Correct |
13 ms |
22108 KB |
Output is correct |
53 |
Correct |
106 ms |
74068 KB |
Output is correct |
54 |
Correct |
130 ms |
62808 KB |
Output is correct |
55 |
Correct |
122 ms |
62844 KB |
Output is correct |
56 |
Correct |
127 ms |
63060 KB |
Output is correct |
57 |
Correct |
135 ms |
62516 KB |
Output is correct |
58 |
Correct |
127 ms |
74064 KB |
Output is correct |
59 |
Correct |
116 ms |
69200 KB |
Output is correct |