#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e4 + 10;
const int INF = 0x3f3f3f3f;
array<vector<pair<int, int> >, MAXN> grafo;
int K, N, M, O;
struct node
{
//Niveis do comeco e do fim, respectivamente
int X, Y;
array<array<int, 5>, 5> dist;
node(int x = 0, int y = 0) : X(x), Y(y)
{
dist[0] = {0, INF, INF, INF, INF};
dist[1] = {INF, 0, INF, INF, INF};
dist[2] = {INF, INF, 0, INF, INF};
dist[3] = {INF, INF, INF, 0, INF};
dist[4] = {INF, INF, INF, INF, 0};
}
//O(K⁴) ≃ 625
node operator+(node outro) const
{
if (X == -1) return outro;
if (outro.X == -1) return *this;
node resp;
// cerr << X << " " << Y << " " << outro.X << " " << outro.Y << '\n';
//Novo X
for (int k = 0; k < K; k++)
{
//Novo Y
for (int k2 = 0; k2 < K; k2++)
{
resp.dist[k][k2] = INF;
//Intermediario da esquerda (final)
for (int i = 0, v = Y*K; i < K; i++, v++)
{
//Intermediario da direira (comeco)
for (auto [viz, p] : grafo[v])
{
// cerr << k << " " << i << " " << viz << " " << k2 << '\n';
// k -> k2 = k -> v ---------> viz -> k2
resp.dist[k][k2] = min(resp.dist[k][k2], dist[k][i] + p + outro.dist[viz%K][k2]);
}
}
}
}
resp.X = X; resp.Y = outro.Y;
/*for (int i = 0; i < K; i++)
{
for (int j = 0; j < K; j++)
{
cerr << (resp.dist[i][j] == INF ? -1 : resp.dist[i][j]) << " ";
}
cerr << '\n';
}*/
// cerr << '\n';
return resp;
}
} seg[4*MAXN];
node nulo(-1, -1);
//O(4*N*K⁴) ≃ 1.25e8
void build(int pos, int ini, int fim)
{
if (ini == fim)
{
seg[pos] = node(ini, ini);
return;
}
int m = (ini + fim) >> 1;
int e = 2*pos; int d = 2*pos + 1;
build(e, ini, m );
build(d, m+1, fim);
seg[pos] = seg[e] + seg[d];
// cerr << "Soma funciona!" << '\n';
}
//O(logN*K⁴) ≃ 1e4 p/ query
node query(int pos, int ini, int fim, int p, int q)
{
if (q < ini || p > fim) return nulo;
if (p <= ini && fim <= q) return seg[pos];
int m = (ini + fim) >> 1;
int e = 2*pos; int d = 2*pos + 1;
return query(e, ini, m, p, q) + query(d, m+1, fim, p, q);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> K >> N >> M >> O;
// cerr << "PENIS" << '\n';
for (int i = 0; i < M; i++)
{
int X, Y, P;
cin >> X >> Y >> P;
grafo[X].emplace_back(Y, P);
}
// cerr << "PENIS" << '\n';
build(1, 0, N/K);
// cerr << "PENIS" << '\n';
for (int i = 0; i < O; i++)
{
int X, Y;
cin >> X >> Y;
if (X/K >= Y/K) {cout << "-1" << '\n'; continue;}
// cerr << "||" << X << " " << X/K << " " << Y << " " << Y/K << '\n';
node resp = query(1, 0, N/K, X/K, Y/K);
/*for (int i = 0; i < K; i++)
{
for (int j = 0; j < K; j++)
{
cerr << (resp.dist[i][j] == INF ? -1 : resp.dist[i][j]) << " ";
}
cerr << '\n';
}*/
cout << (resp.dist[X%K][Y%K] == INF ? -1 : resp.dist[X%K][Y%K]) << '\n';
}
// cerr << "PENIS" << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
25192 KB |
Output is correct |
2 |
Correct |
10 ms |
22628 KB |
Output is correct |
3 |
Correct |
10 ms |
22636 KB |
Output is correct |
4 |
Correct |
10 ms |
22636 KB |
Output is correct |
5 |
Correct |
10 ms |
22640 KB |
Output is correct |
6 |
Correct |
11 ms |
22644 KB |
Output is correct |
7 |
Correct |
11 ms |
22692 KB |
Output is correct |
8 |
Correct |
37 ms |
25136 KB |
Output is correct |
9 |
Correct |
37 ms |
24980 KB |
Output is correct |
10 |
Correct |
23 ms |
22756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
25828 KB |
Output is correct |
2 |
Correct |
10 ms |
22608 KB |
Output is correct |
3 |
Correct |
10 ms |
22632 KB |
Output is correct |
4 |
Correct |
10 ms |
22588 KB |
Output is correct |
5 |
Correct |
12 ms |
22612 KB |
Output is correct |
6 |
Correct |
10 ms |
22632 KB |
Output is correct |
7 |
Correct |
14 ms |
22644 KB |
Output is correct |
8 |
Correct |
16 ms |
22756 KB |
Output is correct |
9 |
Correct |
32 ms |
25072 KB |
Output is correct |
10 |
Correct |
84 ms |
27348 KB |
Output is correct |
11 |
Correct |
52 ms |
25948 KB |
Output is correct |
12 |
Correct |
59 ms |
25384 KB |
Output is correct |
13 |
Correct |
135 ms |
27828 KB |
Output is correct |
14 |
Correct |
48 ms |
25676 KB |
Output is correct |
15 |
Correct |
69 ms |
25140 KB |
Output is correct |
16 |
Correct |
70 ms |
25140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
22612 KB |
Output is correct |
2 |
Correct |
9 ms |
22636 KB |
Output is correct |
3 |
Correct |
9 ms |
22612 KB |
Output is correct |
4 |
Correct |
10 ms |
22524 KB |
Output is correct |
5 |
Correct |
10 ms |
22628 KB |
Output is correct |
6 |
Correct |
10 ms |
22612 KB |
Output is correct |
7 |
Correct |
13 ms |
22612 KB |
Output is correct |
8 |
Correct |
15 ms |
22740 KB |
Output is correct |
9 |
Correct |
14 ms |
22652 KB |
Output is correct |
10 |
Correct |
31 ms |
24988 KB |
Output is correct |
11 |
Correct |
35 ms |
25636 KB |
Output is correct |
12 |
Correct |
52 ms |
27200 KB |
Output is correct |
13 |
Correct |
55 ms |
27692 KB |
Output is correct |
14 |
Correct |
44 ms |
26392 KB |
Output is correct |
15 |
Correct |
42 ms |
25004 KB |
Output is correct |
16 |
Correct |
41 ms |
25100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
22612 KB |
Output is correct |
2 |
Correct |
9 ms |
22636 KB |
Output is correct |
3 |
Correct |
9 ms |
22612 KB |
Output is correct |
4 |
Correct |
10 ms |
22524 KB |
Output is correct |
5 |
Correct |
10 ms |
22628 KB |
Output is correct |
6 |
Correct |
10 ms |
22612 KB |
Output is correct |
7 |
Correct |
13 ms |
22612 KB |
Output is correct |
8 |
Correct |
15 ms |
22740 KB |
Output is correct |
9 |
Correct |
14 ms |
22652 KB |
Output is correct |
10 |
Correct |
31 ms |
24988 KB |
Output is correct |
11 |
Correct |
35 ms |
25636 KB |
Output is correct |
12 |
Correct |
52 ms |
27200 KB |
Output is correct |
13 |
Correct |
55 ms |
27692 KB |
Output is correct |
14 |
Correct |
44 ms |
26392 KB |
Output is correct |
15 |
Correct |
42 ms |
25004 KB |
Output is correct |
16 |
Correct |
41 ms |
25100 KB |
Output is correct |
17 |
Correct |
41 ms |
25676 KB |
Output is correct |
18 |
Correct |
9 ms |
22612 KB |
Output is correct |
19 |
Correct |
11 ms |
22748 KB |
Output is correct |
20 |
Correct |
10 ms |
22640 KB |
Output is correct |
21 |
Correct |
10 ms |
22636 KB |
Output is correct |
22 |
Correct |
10 ms |
22648 KB |
Output is correct |
23 |
Correct |
12 ms |
22680 KB |
Output is correct |
24 |
Correct |
14 ms |
22740 KB |
Output is correct |
25 |
Correct |
52 ms |
22780 KB |
Output is correct |
26 |
Correct |
30 ms |
22764 KB |
Output is correct |
27 |
Correct |
28 ms |
25032 KB |
Output is correct |
28 |
Correct |
69 ms |
27212 KB |
Output is correct |
29 |
Correct |
82 ms |
27828 KB |
Output is correct |
30 |
Correct |
59 ms |
26532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
25192 KB |
Output is correct |
2 |
Correct |
10 ms |
22628 KB |
Output is correct |
3 |
Correct |
10 ms |
22636 KB |
Output is correct |
4 |
Correct |
10 ms |
22636 KB |
Output is correct |
5 |
Correct |
10 ms |
22640 KB |
Output is correct |
6 |
Correct |
11 ms |
22644 KB |
Output is correct |
7 |
Correct |
11 ms |
22692 KB |
Output is correct |
8 |
Correct |
37 ms |
25136 KB |
Output is correct |
9 |
Correct |
37 ms |
24980 KB |
Output is correct |
10 |
Correct |
23 ms |
22756 KB |
Output is correct |
11 |
Correct |
47 ms |
25828 KB |
Output is correct |
12 |
Correct |
10 ms |
22608 KB |
Output is correct |
13 |
Correct |
10 ms |
22632 KB |
Output is correct |
14 |
Correct |
10 ms |
22588 KB |
Output is correct |
15 |
Correct |
12 ms |
22612 KB |
Output is correct |
16 |
Correct |
10 ms |
22632 KB |
Output is correct |
17 |
Correct |
14 ms |
22644 KB |
Output is correct |
18 |
Correct |
16 ms |
22756 KB |
Output is correct |
19 |
Correct |
32 ms |
25072 KB |
Output is correct |
20 |
Correct |
84 ms |
27348 KB |
Output is correct |
21 |
Correct |
52 ms |
25948 KB |
Output is correct |
22 |
Correct |
59 ms |
25384 KB |
Output is correct |
23 |
Correct |
135 ms |
27828 KB |
Output is correct |
24 |
Correct |
48 ms |
25676 KB |
Output is correct |
25 |
Correct |
69 ms |
25140 KB |
Output is correct |
26 |
Correct |
70 ms |
25140 KB |
Output is correct |
27 |
Correct |
10 ms |
22612 KB |
Output is correct |
28 |
Correct |
9 ms |
22636 KB |
Output is correct |
29 |
Correct |
9 ms |
22612 KB |
Output is correct |
30 |
Correct |
10 ms |
22524 KB |
Output is correct |
31 |
Correct |
10 ms |
22628 KB |
Output is correct |
32 |
Correct |
10 ms |
22612 KB |
Output is correct |
33 |
Correct |
13 ms |
22612 KB |
Output is correct |
34 |
Correct |
15 ms |
22740 KB |
Output is correct |
35 |
Correct |
14 ms |
22652 KB |
Output is correct |
36 |
Correct |
31 ms |
24988 KB |
Output is correct |
37 |
Correct |
35 ms |
25636 KB |
Output is correct |
38 |
Correct |
52 ms |
27200 KB |
Output is correct |
39 |
Correct |
55 ms |
27692 KB |
Output is correct |
40 |
Correct |
44 ms |
26392 KB |
Output is correct |
41 |
Correct |
42 ms |
25004 KB |
Output is correct |
42 |
Correct |
41 ms |
25100 KB |
Output is correct |
43 |
Correct |
41 ms |
25676 KB |
Output is correct |
44 |
Correct |
9 ms |
22612 KB |
Output is correct |
45 |
Correct |
11 ms |
22748 KB |
Output is correct |
46 |
Correct |
10 ms |
22640 KB |
Output is correct |
47 |
Correct |
10 ms |
22636 KB |
Output is correct |
48 |
Correct |
10 ms |
22648 KB |
Output is correct |
49 |
Correct |
12 ms |
22680 KB |
Output is correct |
50 |
Correct |
14 ms |
22740 KB |
Output is correct |
51 |
Correct |
52 ms |
22780 KB |
Output is correct |
52 |
Correct |
30 ms |
22764 KB |
Output is correct |
53 |
Correct |
28 ms |
25032 KB |
Output is correct |
54 |
Correct |
69 ms |
27212 KB |
Output is correct |
55 |
Correct |
82 ms |
27828 KB |
Output is correct |
56 |
Correct |
59 ms |
26532 KB |
Output is correct |
57 |
Correct |
257 ms |
28716 KB |
Output is correct |
58 |
Correct |
36 ms |
25100 KB |
Output is correct |
59 |
Correct |
64 ms |
25928 KB |
Output is correct |