//In the name of God
#include<bits/stdc++.h>
#define lc (id * 2)
#define rc (id * 2 + 1)
#define md ((s + e) / 2)
#define dm ((s + e) / 2 + 1)
#define ln (e - s + 1)
using namespace std;
typedef long long ll;
const ll MXN = 5e4 + 10;
const ll MXS = MXN * 4;
const ll MXZ = 10;
const ll INF = 1e10;
struct Matrix{
ll n, m;
ll M[MXZ][MXZ];
Matrix(ll _n = 0, ll _m = 0, ll f = 0){
n = _n, m = _m;
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++){
if(f == -1) M[i][j] = (i == j ? 0 : INF);
else M[i][j] = f;
}
}
}
void Print(){
cout << "======= N.N =======\n";
for(int i = 0; i < n; i ++, cout << '\n'){
for(int j = 0; j < m; j ++) cout << M[i][j] << ' ';
}
cout << "======= N.N =======\n";
}
Matrix operator * (const Matrix &T){
assert(m == T.n);
Matrix R = Matrix(n, T.m, INF);
for(int i = 0; i < n; i ++){
for(int k = 0; k < m; k ++){
for(int j = 0; j < T.m; j ++){
R.M[i][j] = min(R.M[i][j], M[i][k] + T.M[k][j]);
}
}
}
return R;
}
};
ll k, n, m, q;
ll ANS[MXN];
vector<pair<ll, ll>> adj[MXN], adt[MXN], Q[MXN];
Matrix seg[MXS], Now, Res, lz, base[MXZ];
void Build(ll id = 1, ll s = 1, ll e = n){
seg[id] = Matrix(k, k, -1);
if(ln > 1){
Build(lc, s, md), Build(rc, dm, e);
}
}
void Set(ll p, ll id = 1, ll s = 1, ll e = n){
if(e < p || s > p) return;
if(ln == 1){
seg[id] = Now; return;
}
Set(p, lc, s, md), Set(p, rc, dm, e);
//seg[id] = seg[lc] * seg[rc];
seg[id] = seg[rc] * seg[lc];
}
void fetch(ll l, ll r, ll id = 1, ll s = 1, ll e = n){
if(e < l || s > r) return;
if(l <= s && e <= r){
//lz = lz * seg[id];
lz = seg[id] * lz;
return;
}
fetch(l, r, lc, s, md), fetch(l, r, rc, dm, e);
}
int main(){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
cin >> k >> n >> m >> q;
for(int i = 0; i < m; i ++){
ll u, v, w; cin >> u >> v >> w;
u ++, v ++;
adj[u].push_back({v, w});
adt[v].push_back({u, w});
}
for(int i = 0; i < q; i ++){
ll u, v; cin >> u >> v; u ++, v ++;
Q[v].push_back({u, i});
}
Build();
for(int i = 0; i < k; i ++) base[i] = Matrix(k, 1, INF), base[i].M[i][0] = 0;
for(int f = 0; f <= (n - 1) / k; f ++){
ll s = f * k + 1, e = min((f + 1) * k, n);
if(f){
Now = Matrix(k, k, INF);
for(int v = s; v <= e; v ++){
for(auto X : adt[v]){
ll u, w; tie(u, w) = X;
ll vp = (v - 1) % k, up = (u - 1) % k;
Now.M[vp][up] = min(Now.M[vp][up], w);
}
}
Set(s - 1);
}
for(int v = s; v <= e; v ++){
for(auto X : Q[v]){
ll u, id; tie(u, id) = X;
Res = base[(u - 1) % k];
lz = Matrix(k, k, -1); fetch(u, n);
Res = lz * Res;
ANS[id] = Res.M[(v - 1) % k][0];
}
}
}
for(int i = 0; i < q; i ++) cout << (ANS[i] == INF ? -1 : ANS[i]) << '\n';
//cout << "Ok\n";
return 0;
}
//! This is NIMA !
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
167132 KB |
Output is correct |
2 |
Correct |
64 ms |
163572 KB |
Output is correct |
3 |
Correct |
65 ms |
163528 KB |
Output is correct |
4 |
Correct |
74 ms |
163448 KB |
Output is correct |
5 |
Correct |
67 ms |
163608 KB |
Output is correct |
6 |
Correct |
79 ms |
163652 KB |
Output is correct |
7 |
Correct |
67 ms |
163540 KB |
Output is correct |
8 |
Correct |
246 ms |
167104 KB |
Output is correct |
9 |
Correct |
211 ms |
166924 KB |
Output is correct |
10 |
Correct |
170 ms |
163912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
168644 KB |
Output is correct |
2 |
Correct |
61 ms |
163524 KB |
Output is correct |
3 |
Correct |
61 ms |
163564 KB |
Output is correct |
4 |
Correct |
63 ms |
163484 KB |
Output is correct |
5 |
Correct |
63 ms |
163632 KB |
Output is correct |
6 |
Correct |
65 ms |
163432 KB |
Output is correct |
7 |
Correct |
71 ms |
164020 KB |
Output is correct |
8 |
Correct |
73 ms |
164108 KB |
Output is correct |
9 |
Correct |
215 ms |
167940 KB |
Output is correct |
10 |
Correct |
249 ms |
173772 KB |
Output is correct |
11 |
Correct |
211 ms |
170664 KB |
Output is correct |
12 |
Correct |
201 ms |
169168 KB |
Output is correct |
13 |
Correct |
195 ms |
175428 KB |
Output is correct |
14 |
Correct |
167 ms |
170292 KB |
Output is correct |
15 |
Correct |
156 ms |
168768 KB |
Output is correct |
16 |
Correct |
147 ms |
168776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
163524 KB |
Output is correct |
2 |
Correct |
65 ms |
163520 KB |
Output is correct |
3 |
Correct |
63 ms |
163472 KB |
Output is correct |
4 |
Correct |
63 ms |
163476 KB |
Output is correct |
5 |
Correct |
61 ms |
163524 KB |
Output is correct |
6 |
Correct |
65 ms |
163596 KB |
Output is correct |
7 |
Correct |
65 ms |
163628 KB |
Output is correct |
8 |
Correct |
65 ms |
163852 KB |
Output is correct |
9 |
Correct |
65 ms |
163780 KB |
Output is correct |
10 |
Correct |
202 ms |
167400 KB |
Output is correct |
11 |
Correct |
191 ms |
170052 KB |
Output is correct |
12 |
Correct |
223 ms |
173464 KB |
Output is correct |
13 |
Correct |
254 ms |
174660 KB |
Output is correct |
14 |
Correct |
201 ms |
171692 KB |
Output is correct |
15 |
Correct |
147 ms |
168696 KB |
Output is correct |
16 |
Correct |
148 ms |
168732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
163524 KB |
Output is correct |
2 |
Correct |
65 ms |
163520 KB |
Output is correct |
3 |
Correct |
63 ms |
163472 KB |
Output is correct |
4 |
Correct |
63 ms |
163476 KB |
Output is correct |
5 |
Correct |
61 ms |
163524 KB |
Output is correct |
6 |
Correct |
65 ms |
163596 KB |
Output is correct |
7 |
Correct |
65 ms |
163628 KB |
Output is correct |
8 |
Correct |
65 ms |
163852 KB |
Output is correct |
9 |
Correct |
65 ms |
163780 KB |
Output is correct |
10 |
Correct |
202 ms |
167400 KB |
Output is correct |
11 |
Correct |
191 ms |
170052 KB |
Output is correct |
12 |
Correct |
223 ms |
173464 KB |
Output is correct |
13 |
Correct |
254 ms |
174660 KB |
Output is correct |
14 |
Correct |
201 ms |
171692 KB |
Output is correct |
15 |
Correct |
147 ms |
168696 KB |
Output is correct |
16 |
Correct |
148 ms |
168732 KB |
Output is correct |
17 |
Correct |
221 ms |
170188 KB |
Output is correct |
18 |
Correct |
75 ms |
163548 KB |
Output is correct |
19 |
Correct |
68 ms |
163620 KB |
Output is correct |
20 |
Correct |
64 ms |
163516 KB |
Output is correct |
21 |
Correct |
63 ms |
163532 KB |
Output is correct |
22 |
Correct |
64 ms |
163508 KB |
Output is correct |
23 |
Correct |
66 ms |
163672 KB |
Output is correct |
24 |
Correct |
68 ms |
163724 KB |
Output is correct |
25 |
Correct |
71 ms |
163904 KB |
Output is correct |
26 |
Correct |
70 ms |
163980 KB |
Output is correct |
27 |
Correct |
210 ms |
167544 KB |
Output is correct |
28 |
Correct |
248 ms |
173504 KB |
Output is correct |
29 |
Correct |
282 ms |
174784 KB |
Output is correct |
30 |
Correct |
204 ms |
171716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
167132 KB |
Output is correct |
2 |
Correct |
64 ms |
163572 KB |
Output is correct |
3 |
Correct |
65 ms |
163528 KB |
Output is correct |
4 |
Correct |
74 ms |
163448 KB |
Output is correct |
5 |
Correct |
67 ms |
163608 KB |
Output is correct |
6 |
Correct |
79 ms |
163652 KB |
Output is correct |
7 |
Correct |
67 ms |
163540 KB |
Output is correct |
8 |
Correct |
246 ms |
167104 KB |
Output is correct |
9 |
Correct |
211 ms |
166924 KB |
Output is correct |
10 |
Correct |
170 ms |
163912 KB |
Output is correct |
11 |
Correct |
198 ms |
168644 KB |
Output is correct |
12 |
Correct |
61 ms |
163524 KB |
Output is correct |
13 |
Correct |
61 ms |
163564 KB |
Output is correct |
14 |
Correct |
63 ms |
163484 KB |
Output is correct |
15 |
Correct |
63 ms |
163632 KB |
Output is correct |
16 |
Correct |
65 ms |
163432 KB |
Output is correct |
17 |
Correct |
71 ms |
164020 KB |
Output is correct |
18 |
Correct |
73 ms |
164108 KB |
Output is correct |
19 |
Correct |
215 ms |
167940 KB |
Output is correct |
20 |
Correct |
249 ms |
173772 KB |
Output is correct |
21 |
Correct |
211 ms |
170664 KB |
Output is correct |
22 |
Correct |
201 ms |
169168 KB |
Output is correct |
23 |
Correct |
195 ms |
175428 KB |
Output is correct |
24 |
Correct |
167 ms |
170292 KB |
Output is correct |
25 |
Correct |
156 ms |
168768 KB |
Output is correct |
26 |
Correct |
147 ms |
168776 KB |
Output is correct |
27 |
Correct |
64 ms |
163524 KB |
Output is correct |
28 |
Correct |
65 ms |
163520 KB |
Output is correct |
29 |
Correct |
63 ms |
163472 KB |
Output is correct |
30 |
Correct |
63 ms |
163476 KB |
Output is correct |
31 |
Correct |
61 ms |
163524 KB |
Output is correct |
32 |
Correct |
65 ms |
163596 KB |
Output is correct |
33 |
Correct |
65 ms |
163628 KB |
Output is correct |
34 |
Correct |
65 ms |
163852 KB |
Output is correct |
35 |
Correct |
65 ms |
163780 KB |
Output is correct |
36 |
Correct |
202 ms |
167400 KB |
Output is correct |
37 |
Correct |
191 ms |
170052 KB |
Output is correct |
38 |
Correct |
223 ms |
173464 KB |
Output is correct |
39 |
Correct |
254 ms |
174660 KB |
Output is correct |
40 |
Correct |
201 ms |
171692 KB |
Output is correct |
41 |
Correct |
147 ms |
168696 KB |
Output is correct |
42 |
Correct |
148 ms |
168732 KB |
Output is correct |
43 |
Correct |
221 ms |
170188 KB |
Output is correct |
44 |
Correct |
75 ms |
163548 KB |
Output is correct |
45 |
Correct |
68 ms |
163620 KB |
Output is correct |
46 |
Correct |
64 ms |
163516 KB |
Output is correct |
47 |
Correct |
63 ms |
163532 KB |
Output is correct |
48 |
Correct |
64 ms |
163508 KB |
Output is correct |
49 |
Correct |
66 ms |
163672 KB |
Output is correct |
50 |
Correct |
68 ms |
163724 KB |
Output is correct |
51 |
Correct |
71 ms |
163904 KB |
Output is correct |
52 |
Correct |
70 ms |
163980 KB |
Output is correct |
53 |
Correct |
210 ms |
167544 KB |
Output is correct |
54 |
Correct |
248 ms |
173504 KB |
Output is correct |
55 |
Correct |
282 ms |
174784 KB |
Output is correct |
56 |
Correct |
204 ms |
171716 KB |
Output is correct |
57 |
Correct |
316 ms |
175656 KB |
Output is correct |
58 |
Correct |
222 ms |
167984 KB |
Output is correct |
59 |
Correct |
227 ms |
170708 KB |
Output is correct |