#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 |
124 ms |
74852 KB |
Output is correct |
2 |
Correct |
11 ms |
21608 KB |
Output is correct |
3 |
Correct |
12 ms |
21652 KB |
Output is correct |
4 |
Correct |
12 ms |
21608 KB |
Output is correct |
5 |
Correct |
12 ms |
22176 KB |
Output is correct |
6 |
Correct |
17 ms |
22120 KB |
Output is correct |
7 |
Correct |
14 ms |
22120 KB |
Output is correct |
8 |
Correct |
124 ms |
75008 KB |
Output is correct |
9 |
Correct |
122 ms |
74988 KB |
Output is correct |
10 |
Correct |
116 ms |
74148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
70240 KB |
Output is correct |
2 |
Correct |
12 ms |
21608 KB |
Output is correct |
3 |
Correct |
12 ms |
21476 KB |
Output is correct |
4 |
Correct |
12 ms |
21480 KB |
Output is correct |
5 |
Correct |
12 ms |
21608 KB |
Output is correct |
6 |
Correct |
11 ms |
21608 KB |
Output is correct |
7 |
Correct |
17 ms |
22376 KB |
Output is correct |
8 |
Correct |
17 ms |
22376 KB |
Output is correct |
9 |
Correct |
115 ms |
74932 KB |
Output is correct |
10 |
Correct |
138 ms |
65376 KB |
Output is correct |
11 |
Correct |
109 ms |
71008 KB |
Output is correct |
12 |
Correct |
121 ms |
64096 KB |
Output is correct |
13 |
Correct |
75 ms |
45580 KB |
Output is correct |
14 |
Correct |
73 ms |
46732 KB |
Output is correct |
15 |
Correct |
72 ms |
44588 KB |
Output is correct |
16 |
Correct |
66 ms |
44372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
21592 KB |
Output is correct |
2 |
Correct |
11 ms |
21596 KB |
Output is correct |
3 |
Correct |
11 ms |
21640 KB |
Output is correct |
4 |
Correct |
11 ms |
21596 KB |
Output is correct |
5 |
Correct |
11 ms |
21640 KB |
Output is correct |
6 |
Correct |
15 ms |
22108 KB |
Output is correct |
7 |
Correct |
14 ms |
22024 KB |
Output is correct |
8 |
Correct |
14 ms |
22108 KB |
Output is correct |
9 |
Correct |
13 ms |
22104 KB |
Output is correct |
10 |
Correct |
100 ms |
74796 KB |
Output is correct |
11 |
Correct |
90 ms |
70640 KB |
Output is correct |
12 |
Correct |
114 ms |
65104 KB |
Output is correct |
13 |
Correct |
119 ms |
65364 KB |
Output is correct |
14 |
Correct |
102 ms |
64668 KB |
Output is correct |
15 |
Correct |
57 ms |
44424 KB |
Output is correct |
16 |
Correct |
58 ms |
44368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
21592 KB |
Output is correct |
2 |
Correct |
11 ms |
21596 KB |
Output is correct |
3 |
Correct |
11 ms |
21640 KB |
Output is correct |
4 |
Correct |
11 ms |
21596 KB |
Output is correct |
5 |
Correct |
11 ms |
21640 KB |
Output is correct |
6 |
Correct |
15 ms |
22108 KB |
Output is correct |
7 |
Correct |
14 ms |
22024 KB |
Output is correct |
8 |
Correct |
14 ms |
22108 KB |
Output is correct |
9 |
Correct |
13 ms |
22104 KB |
Output is correct |
10 |
Correct |
100 ms |
74796 KB |
Output is correct |
11 |
Correct |
90 ms |
70640 KB |
Output is correct |
12 |
Correct |
114 ms |
65104 KB |
Output is correct |
13 |
Correct |
119 ms |
65364 KB |
Output is correct |
14 |
Correct |
102 ms |
64668 KB |
Output is correct |
15 |
Correct |
57 ms |
44424 KB |
Output is correct |
16 |
Correct |
58 ms |
44368 KB |
Output is correct |
17 |
Correct |
101 ms |
70768 KB |
Output is correct |
18 |
Correct |
10 ms |
21596 KB |
Output is correct |
19 |
Correct |
10 ms |
21652 KB |
Output is correct |
20 |
Correct |
11 ms |
21608 KB |
Output is correct |
21 |
Correct |
10 ms |
21608 KB |
Output is correct |
22 |
Correct |
10 ms |
21608 KB |
Output is correct |
23 |
Correct |
13 ms |
22376 KB |
Output is correct |
24 |
Correct |
13 ms |
22120 KB |
Output is correct |
25 |
Correct |
13 ms |
22120 KB |
Output is correct |
26 |
Correct |
13 ms |
22120 KB |
Output is correct |
27 |
Correct |
102 ms |
74884 KB |
Output is correct |
28 |
Correct |
111 ms |
65028 KB |
Output is correct |
29 |
Correct |
121 ms |
65376 KB |
Output is correct |
30 |
Correct |
112 ms |
64864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
74852 KB |
Output is correct |
2 |
Correct |
11 ms |
21608 KB |
Output is correct |
3 |
Correct |
12 ms |
21652 KB |
Output is correct |
4 |
Correct |
12 ms |
21608 KB |
Output is correct |
5 |
Correct |
12 ms |
22176 KB |
Output is correct |
6 |
Correct |
17 ms |
22120 KB |
Output is correct |
7 |
Correct |
14 ms |
22120 KB |
Output is correct |
8 |
Correct |
124 ms |
75008 KB |
Output is correct |
9 |
Correct |
122 ms |
74988 KB |
Output is correct |
10 |
Correct |
116 ms |
74148 KB |
Output is correct |
11 |
Correct |
114 ms |
70240 KB |
Output is correct |
12 |
Correct |
12 ms |
21608 KB |
Output is correct |
13 |
Correct |
12 ms |
21476 KB |
Output is correct |
14 |
Correct |
12 ms |
21480 KB |
Output is correct |
15 |
Correct |
12 ms |
21608 KB |
Output is correct |
16 |
Correct |
11 ms |
21608 KB |
Output is correct |
17 |
Correct |
17 ms |
22376 KB |
Output is correct |
18 |
Correct |
17 ms |
22376 KB |
Output is correct |
19 |
Correct |
115 ms |
74932 KB |
Output is correct |
20 |
Correct |
138 ms |
65376 KB |
Output is correct |
21 |
Correct |
109 ms |
71008 KB |
Output is correct |
22 |
Correct |
121 ms |
64096 KB |
Output is correct |
23 |
Correct |
75 ms |
45580 KB |
Output is correct |
24 |
Correct |
73 ms |
46732 KB |
Output is correct |
25 |
Correct |
72 ms |
44588 KB |
Output is correct |
26 |
Correct |
66 ms |
44372 KB |
Output is correct |
27 |
Correct |
12 ms |
21592 KB |
Output is correct |
28 |
Correct |
11 ms |
21596 KB |
Output is correct |
29 |
Correct |
11 ms |
21640 KB |
Output is correct |
30 |
Correct |
11 ms |
21596 KB |
Output is correct |
31 |
Correct |
11 ms |
21640 KB |
Output is correct |
32 |
Correct |
15 ms |
22108 KB |
Output is correct |
33 |
Correct |
14 ms |
22024 KB |
Output is correct |
34 |
Correct |
14 ms |
22108 KB |
Output is correct |
35 |
Correct |
13 ms |
22104 KB |
Output is correct |
36 |
Correct |
100 ms |
74796 KB |
Output is correct |
37 |
Correct |
90 ms |
70640 KB |
Output is correct |
38 |
Correct |
114 ms |
65104 KB |
Output is correct |
39 |
Correct |
119 ms |
65364 KB |
Output is correct |
40 |
Correct |
102 ms |
64668 KB |
Output is correct |
41 |
Correct |
57 ms |
44424 KB |
Output is correct |
42 |
Correct |
58 ms |
44368 KB |
Output is correct |
43 |
Correct |
101 ms |
70768 KB |
Output is correct |
44 |
Correct |
10 ms |
21596 KB |
Output is correct |
45 |
Correct |
10 ms |
21652 KB |
Output is correct |
46 |
Correct |
11 ms |
21608 KB |
Output is correct |
47 |
Correct |
10 ms |
21608 KB |
Output is correct |
48 |
Correct |
10 ms |
21608 KB |
Output is correct |
49 |
Correct |
13 ms |
22376 KB |
Output is correct |
50 |
Correct |
13 ms |
22120 KB |
Output is correct |
51 |
Correct |
13 ms |
22120 KB |
Output is correct |
52 |
Correct |
13 ms |
22120 KB |
Output is correct |
53 |
Correct |
102 ms |
74884 KB |
Output is correct |
54 |
Correct |
111 ms |
65028 KB |
Output is correct |
55 |
Correct |
121 ms |
65376 KB |
Output is correct |
56 |
Correct |
112 ms |
64864 KB |
Output is correct |
57 |
Correct |
128 ms |
65872 KB |
Output is correct |
58 |
Correct |
126 ms |
74836 KB |
Output is correct |
59 |
Correct |
113 ms |
71088 KB |
Output is correct |