#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int NMAX(50005);
const ll INF(LLONG_MAX / 2 - 5);
vector<pair<int, int> > G[NMAX];
int k;
struct matrice
{
vector<vector<ll> > mat;
matrice()
{
mat = vector<vector<ll> > (k, vector<ll>(k, INF));
}
matrice operator+(matrice b)
{
matrice rez;
for(int i = 0; i < k; ++i)
for(int j = 0; j < k; ++j)
for(int interm = 0; interm < k; ++interm)
rez.mat[i][j] = min(rez.mat[i][j], mat[i][interm] + b.mat[interm][j]);
return rez;
}
} v[NMAX];
struct SegmentTree
{
vector<matrice> Arb;
SegmentTree(int n)
{
int put = 1;
while(put <= n)
put <<= 1;
put <<= 1;
Arb.resize(put);
}
inline void build(int nod, int st, int dr)
{
if(st > dr)
return;
if(st == dr)
{
Arb[nod] = v[st];
return;
}
int mij = (st + dr) / 2;
build(2 * nod, st, mij);
build(2 * nod + 1, mij + 1, dr);
Arb[nod] = Arb[2 * nod] + Arb[2 * nod + 1];
}
matrice Query(int nod, int st, int dr, int a, int b)
{
if(a <= st && dr <= b)
return Arb[nod];
int mij = (st + dr) / 2;
matrice rez1, rez2;
bool ok1 = 0, ok2 = 0;
if(a <= mij)
rez1 = Query(2 * nod, st, mij, a, b), ok1 = 1;
if(b > mij)
rez2 = Query(2 * nod + 1, mij + 1, dr, a, b), ok2 = 1;
if(ok1 && ok2)
return rez1 + rez2;
if(ok1)
return rez1;
return rez2;
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m, o;
cin >> k >> n >> m >> o;
for(int i = 1; i <= m; ++i)
{
int a, b, t;
cin >> a >> b >> t;
G[a].push_back({b, t});
}
for(int segm = 0; segm <= n / k; ++segm)
{
matrice aux;
for(int j = 0; j < k; ++j)
{
int nod = segm * k + j;
for(auto it: G[nod])
aux.mat[j][it.first % k] = it.second;
}
v[segm] = aux;
}
SegmentTree Seg(n / k);
Seg.build(1, 0, n / k);
for(int i = 1; i <= o; ++i)
{
int a, b;
cin >> a >> b;
if(a / k == b / k){
cout << -1 << '\n';
continue;
}
auto rez = Seg.Query(1, 0, n / k, a / k, b / k - 1);
cout << (rez.mat[a % k][b % k] == INF ? -1 : rez.mat[a % k][b % k]) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
19008 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2692 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
6 ms |
2892 KB |
Output is correct |
6 |
Correct |
6 ms |
2844 KB |
Output is correct |
7 |
Correct |
6 ms |
2892 KB |
Output is correct |
8 |
Correct |
150 ms |
19692 KB |
Output is correct |
9 |
Correct |
119 ms |
19388 KB |
Output is correct |
10 |
Correct |
99 ms |
17136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
17436 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2664 KB |
Output is correct |
5 |
Correct |
3 ms |
2764 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
28 ms |
2924 KB |
Output is correct |
8 |
Correct |
29 ms |
3012 KB |
Output is correct |
9 |
Correct |
86 ms |
19472 KB |
Output is correct |
10 |
Correct |
138 ms |
22840 KB |
Output is correct |
11 |
Correct |
122 ms |
18788 KB |
Output is correct |
12 |
Correct |
116 ms |
21044 KB |
Output is correct |
13 |
Correct |
96 ms |
15952 KB |
Output is correct |
14 |
Correct |
66 ms |
13696 KB |
Output is correct |
15 |
Correct |
69 ms |
13560 KB |
Output is correct |
16 |
Correct |
76 ms |
13636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
2668 KB |
Output is correct |
6 |
Correct |
3 ms |
2892 KB |
Output is correct |
7 |
Correct |
3 ms |
2796 KB |
Output is correct |
8 |
Correct |
5 ms |
3020 KB |
Output is correct |
9 |
Correct |
4 ms |
2892 KB |
Output is correct |
10 |
Correct |
50 ms |
19324 KB |
Output is correct |
11 |
Correct |
55 ms |
18576 KB |
Output is correct |
12 |
Correct |
94 ms |
22668 KB |
Output is correct |
13 |
Correct |
92 ms |
23272 KB |
Output is correct |
14 |
Correct |
64 ms |
22000 KB |
Output is correct |
15 |
Correct |
40 ms |
13620 KB |
Output is correct |
16 |
Correct |
39 ms |
13548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
2668 KB |
Output is correct |
6 |
Correct |
3 ms |
2892 KB |
Output is correct |
7 |
Correct |
3 ms |
2796 KB |
Output is correct |
8 |
Correct |
5 ms |
3020 KB |
Output is correct |
9 |
Correct |
4 ms |
2892 KB |
Output is correct |
10 |
Correct |
50 ms |
19324 KB |
Output is correct |
11 |
Correct |
55 ms |
18576 KB |
Output is correct |
12 |
Correct |
94 ms |
22668 KB |
Output is correct |
13 |
Correct |
92 ms |
23272 KB |
Output is correct |
14 |
Correct |
64 ms |
22000 KB |
Output is correct |
15 |
Correct |
40 ms |
13620 KB |
Output is correct |
16 |
Correct |
39 ms |
13548 KB |
Output is correct |
17 |
Correct |
85 ms |
18640 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
3 ms |
2560 KB |
Output is correct |
23 |
Correct |
14 ms |
2976 KB |
Output is correct |
24 |
Correct |
17 ms |
2968 KB |
Output is correct |
25 |
Correct |
27 ms |
3080 KB |
Output is correct |
26 |
Correct |
25 ms |
3020 KB |
Output is correct |
27 |
Correct |
70 ms |
19432 KB |
Output is correct |
28 |
Correct |
101 ms |
22812 KB |
Output is correct |
29 |
Correct |
137 ms |
23260 KB |
Output is correct |
30 |
Correct |
134 ms |
22060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
19008 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2692 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
6 ms |
2892 KB |
Output is correct |
6 |
Correct |
6 ms |
2844 KB |
Output is correct |
7 |
Correct |
6 ms |
2892 KB |
Output is correct |
8 |
Correct |
150 ms |
19692 KB |
Output is correct |
9 |
Correct |
119 ms |
19388 KB |
Output is correct |
10 |
Correct |
99 ms |
17136 KB |
Output is correct |
11 |
Correct |
101 ms |
17436 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2664 KB |
Output is correct |
15 |
Correct |
3 ms |
2764 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
28 ms |
2924 KB |
Output is correct |
18 |
Correct |
29 ms |
3012 KB |
Output is correct |
19 |
Correct |
86 ms |
19472 KB |
Output is correct |
20 |
Correct |
138 ms |
22840 KB |
Output is correct |
21 |
Correct |
122 ms |
18788 KB |
Output is correct |
22 |
Correct |
116 ms |
21044 KB |
Output is correct |
23 |
Correct |
96 ms |
15952 KB |
Output is correct |
24 |
Correct |
66 ms |
13696 KB |
Output is correct |
25 |
Correct |
69 ms |
13560 KB |
Output is correct |
26 |
Correct |
76 ms |
13636 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
3 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
3 ms |
2668 KB |
Output is correct |
32 |
Correct |
3 ms |
2892 KB |
Output is correct |
33 |
Correct |
3 ms |
2796 KB |
Output is correct |
34 |
Correct |
5 ms |
3020 KB |
Output is correct |
35 |
Correct |
4 ms |
2892 KB |
Output is correct |
36 |
Correct |
50 ms |
19324 KB |
Output is correct |
37 |
Correct |
55 ms |
18576 KB |
Output is correct |
38 |
Correct |
94 ms |
22668 KB |
Output is correct |
39 |
Correct |
92 ms |
23272 KB |
Output is correct |
40 |
Correct |
64 ms |
22000 KB |
Output is correct |
41 |
Correct |
40 ms |
13620 KB |
Output is correct |
42 |
Correct |
39 ms |
13548 KB |
Output is correct |
43 |
Correct |
85 ms |
18640 KB |
Output is correct |
44 |
Correct |
2 ms |
2636 KB |
Output is correct |
45 |
Correct |
2 ms |
2636 KB |
Output is correct |
46 |
Correct |
2 ms |
2636 KB |
Output is correct |
47 |
Correct |
2 ms |
2636 KB |
Output is correct |
48 |
Correct |
3 ms |
2560 KB |
Output is correct |
49 |
Correct |
14 ms |
2976 KB |
Output is correct |
50 |
Correct |
17 ms |
2968 KB |
Output is correct |
51 |
Correct |
27 ms |
3080 KB |
Output is correct |
52 |
Correct |
25 ms |
3020 KB |
Output is correct |
53 |
Correct |
70 ms |
19432 KB |
Output is correct |
54 |
Correct |
101 ms |
22812 KB |
Output is correct |
55 |
Correct |
137 ms |
23260 KB |
Output is correct |
56 |
Correct |
134 ms |
22060 KB |
Output is correct |
57 |
Correct |
274 ms |
23032 KB |
Output is correct |
58 |
Correct |
130 ms |
19580 KB |
Output is correct |
59 |
Correct |
165 ms |
18920 KB |
Output is correct |