#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) static_cast<int>((x).size())
template<class T, size_t D>
struct vec : vector<vec<T, D - 1>> {
template<class... Args>
vec(size_t n = 0, Args... args)
: vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template<class T>
struct vec<T, 1> : vector<T> {
template<class... Args>
vec(Args... args)
: vector<T>(args...) {}
};
template<class T>
inline bool Minimize(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T>
inline bool Maximize(T& a, const T& b) { return a < b ? a = b, true : false; }
inline int Next(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int Prev(int i, int n) { return !i ? n - 1 : i - 1; }
mt19937 rng(static_cast<uint32_t>(chrono::steady_clock::now().time_since_epoch().count()));
#include "rainbow.h"
template<class X, class Y, class T>
struct BIT2D {
vec<X, 1> compressed_xs;
vec<Y, 2> compressed_ys_groups;
vec<T, 2> a;
BIT2D(const vec<pair<X, Y>, 1>& points = vec<pair<X, Y>, 1>()) {
for (auto& point : points) {
compressed_xs.emplace_back(point.first);
}
sort(ALL(compressed_xs)); compressed_xs.erase(unique(ALL(compressed_xs)), compressed_xs.end());
compressed_ys_groups.resize(SZ(compressed_xs));
for (auto& point : points) {
for (int i = static_cast<int>(lower_bound(ALL(compressed_xs), point.first) - compressed_xs.begin()); i < SZ(compressed_xs); i |= i + 1) {
compressed_ys_groups[i].emplace_back(point.second);
}
}
for (auto& compressed_ys : compressed_ys_groups) {
sort(ALL(compressed_ys)); compressed_ys.erase(unique(ALL(compressed_ys)), compressed_ys.end());
}
a = compressed_ys_groups;
for (auto& i : a) {
fill(ALL(i), T());
}
}
template<class U>
void Add(X x, Y y, U value) {
for (int i = static_cast<int>(lower_bound(ALL(compressed_xs), x) - compressed_xs.begin()); i < SZ(compressed_xs); i |= i + 1) {
for (int j = static_cast<int>(lower_bound(ALL(compressed_ys_groups[i]), y) - compressed_ys_groups[i].begin()); j < SZ(compressed_ys_groups[i]); j |= j + 1) {
a[i][j] += value;
}
}
}
T Get(X x, Y y) const {
T ret{};
for (int i = static_cast<int>(upper_bound(ALL(compressed_xs), x) - compressed_xs.begin()) - 1; ~i; i = (i & (i + 1)) - 1) {
for (int j = static_cast<int>(upper_bound(ALL(compressed_ys_groups[i]), y) - compressed_ys_groups[i].begin()) - 1; ~j; j = (j & (j + 1)) - 1) {
ret += a[i][j];
}
}
return ret;
}
T Get(X x_1, Y y_1, X x_2, Y y_2) const {
return x_1 > x_2 || y_1 > y_2 ? 0 : Get(x_2, y_2) + Get(x_1 - 1, y_1 - 1) - Get(x_2, y_1 - 1) - Get(x_1 - 1, y_2);
}
};
BIT2D<int, int, int> bit2D_cell, bit2D_vertex, bit2D_vertical_edge, bit2D_horizontal_edge;
void init(int n_rows, int n_columns, int starting_row, int starting_column, int n_steps, char *steps) {
vec<pair<int, int>, 1> cells, vertices, vertical_edges, horizontal_edges;
for (int i = 0, x = starting_row, y = starting_column; i <= n_steps; x += steps[i] == 'N' ? -1 : (steps[i] == 'S' ? 1 : 0), y += steps[i] == 'W' ? -1 : (steps[i] == 'E' ? 1 : 0), ++i) {
cells.emplace_back(x, y);
vertices.emplace_back(x, y);
vertices.emplace_back(x - 1, y);
vertices.emplace_back(x, y - 1);
vertices.emplace_back(x - 1, y - 1);
vertical_edges.emplace_back(x, y);
vertical_edges.emplace_back(x, y - 1);
horizontal_edges.emplace_back(x, y);
horizontal_edges.emplace_back(x - 1, y);
}
sort(ALL(cells)); cells.erase(unique(ALL(cells)), cells.end());
sort(ALL(vertices)); vertices.erase(unique(ALL(vertices)), vertices.end());
sort(ALL(vertical_edges)); vertical_edges.erase(unique(ALL(vertical_edges)), vertical_edges.end());
sort(ALL(horizontal_edges)); horizontal_edges.erase(unique(ALL(horizontal_edges)), horizontal_edges.end());
bit2D_cell = BIT2D<int, int, int>(cells);
bit2D_vertex = BIT2D<int, int, int>(vertices);
bit2D_vertical_edge = BIT2D<int, int, int>(vertical_edges);
bit2D_horizontal_edge = BIT2D<int, int, int>(horizontal_edges);
for (auto& cell : cells) {
bit2D_cell.Add(cell.first, cell.second, 1);
}
for (auto& vertex : vertices) {
bit2D_vertex.Add(vertex.first, vertex.second, 1);
}
for (auto& vertical_edge : vertical_edges) {
bit2D_vertical_edge.Add(vertical_edge.first, vertical_edge.second, 1);
}
for (auto& horizontal_edge : horizontal_edges) {
bit2D_horizontal_edge.Add(horizontal_edge.first, horizontal_edge.second, 1);
}
}
int colour(int r_1, int c_1, int r_2, int c_2) {
int n_vertices = ((r_2 - r_1 + 1 + c_2 - c_1 + 1) << 1) + bit2D_vertex.Get(r_1, c_1, r_2 - 1, c_2 - 1);
int n_edges = ((r_2 - r_1 + 1 + c_2 - c_1 + 1) << 1) + bit2D_vertical_edge.Get(r_1, c_1, r_2, c_2 - 1) + bit2D_horizontal_edge.Get(r_1, c_1, r_2 - 1, c_2);
int tmp_1 = bit2D_vertex.Get(r_1 - 1, c_1 - 1, r_2, c_2), tmp_2 = bit2D_vertex.Get(r_1, c_1, r_2 - 1, c_2 - 1);
int n_connected_components = 1 + (tmp_1 == tmp_2 && tmp_1);
return 1 + n_connected_components - n_vertices + n_edges - (1 + bit2D_cell.Get(r_1, c_1, r_2, c_2));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
9 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
9 ms |
696 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
7 ms |
640 KB |
Output is correct |
13 |
Correct |
9 ms |
640 KB |
Output is correct |
14 |
Correct |
11 ms |
908 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
351 ms |
10284 KB |
Output is correct |
4 |
Correct |
446 ms |
18064 KB |
Output is correct |
5 |
Correct |
440 ms |
18192 KB |
Output is correct |
6 |
Correct |
365 ms |
15632 KB |
Output is correct |
7 |
Correct |
430 ms |
15888 KB |
Output is correct |
8 |
Correct |
135 ms |
9616 KB |
Output is correct |
9 |
Correct |
446 ms |
18064 KB |
Output is correct |
10 |
Correct |
459 ms |
18192 KB |
Output is correct |
11 |
Correct |
392 ms |
15632 KB |
Output is correct |
12 |
Correct |
256 ms |
16752 KB |
Output is correct |
13 |
Correct |
250 ms |
18196 KB |
Output is correct |
14 |
Correct |
274 ms |
18064 KB |
Output is correct |
15 |
Correct |
259 ms |
15504 KB |
Output is correct |
16 |
Correct |
375 ms |
15272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
172 ms |
17116 KB |
Output is correct |
3 |
Correct |
364 ms |
71260 KB |
Output is correct |
4 |
Correct |
529 ms |
56668 KB |
Output is correct |
5 |
Correct |
487 ms |
55900 KB |
Output is correct |
6 |
Correct |
135 ms |
12408 KB |
Output is correct |
7 |
Correct |
204 ms |
15452 KB |
Output is correct |
8 |
Correct |
216 ms |
18524 KB |
Output is correct |
9 |
Correct |
227 ms |
18528 KB |
Output is correct |
10 |
Correct |
80 ms |
6352 KB |
Output is correct |
11 |
Correct |
174 ms |
14812 KB |
Output is correct |
12 |
Correct |
177 ms |
17244 KB |
Output is correct |
13 |
Correct |
386 ms |
71400 KB |
Output is correct |
14 |
Correct |
528 ms |
56676 KB |
Output is correct |
15 |
Correct |
467 ms |
55900 KB |
Output is correct |
16 |
Correct |
113 ms |
11228 KB |
Output is correct |
17 |
Correct |
191 ms |
16092 KB |
Output is correct |
18 |
Correct |
334 ms |
26588 KB |
Output is correct |
19 |
Correct |
265 ms |
43612 KB |
Output is correct |
20 |
Correct |
268 ms |
43612 KB |
Output is correct |
21 |
Correct |
206 ms |
18524 KB |
Output is correct |
22 |
Correct |
222 ms |
18396 KB |
Output is correct |
23 |
Correct |
81 ms |
6372 KB |
Output is correct |
24 |
Correct |
165 ms |
14812 KB |
Output is correct |
25 |
Correct |
175 ms |
17248 KB |
Output is correct |
26 |
Correct |
350 ms |
71388 KB |
Output is correct |
27 |
Correct |
526 ms |
56668 KB |
Output is correct |
28 |
Correct |
468 ms |
56028 KB |
Output is correct |
29 |
Correct |
116 ms |
11228 KB |
Output is correct |
30 |
Correct |
196 ms |
16092 KB |
Output is correct |
31 |
Correct |
351 ms |
26716 KB |
Output is correct |
32 |
Correct |
276 ms |
43612 KB |
Output is correct |
33 |
Correct |
269 ms |
43616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
9 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
9 ms |
696 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
7 ms |
640 KB |
Output is correct |
13 |
Correct |
9 ms |
640 KB |
Output is correct |
14 |
Correct |
11 ms |
908 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1376 ms |
19240 KB |
Output is correct |
19 |
Correct |
583 ms |
4712 KB |
Output is correct |
20 |
Correct |
460 ms |
3864 KB |
Output is correct |
21 |
Correct |
524 ms |
4444 KB |
Output is correct |
22 |
Correct |
564 ms |
4472 KB |
Output is correct |
23 |
Correct |
552 ms |
4516 KB |
Output is correct |
24 |
Correct |
517 ms |
4128 KB |
Output is correct |
25 |
Correct |
562 ms |
4264 KB |
Output is correct |
26 |
Correct |
586 ms |
4356 KB |
Output is correct |
27 |
Correct |
621 ms |
16284 KB |
Output is correct |
28 |
Correct |
575 ms |
12688 KB |
Output is correct |
29 |
Correct |
588 ms |
15496 KB |
Output is correct |
30 |
Correct |
1164 ms |
27152 KB |
Output is correct |
31 |
Correct |
6 ms |
512 KB |
Output is correct |
32 |
Correct |
965 ms |
17232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
9 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
9 ms |
696 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
7 ms |
640 KB |
Output is correct |
13 |
Correct |
9 ms |
640 KB |
Output is correct |
14 |
Correct |
11 ms |
908 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1376 ms |
19240 KB |
Output is correct |
19 |
Correct |
583 ms |
4712 KB |
Output is correct |
20 |
Correct |
460 ms |
3864 KB |
Output is correct |
21 |
Correct |
524 ms |
4444 KB |
Output is correct |
22 |
Correct |
564 ms |
4472 KB |
Output is correct |
23 |
Correct |
552 ms |
4516 KB |
Output is correct |
24 |
Correct |
517 ms |
4128 KB |
Output is correct |
25 |
Correct |
562 ms |
4264 KB |
Output is correct |
26 |
Correct |
586 ms |
4356 KB |
Output is correct |
27 |
Correct |
621 ms |
16284 KB |
Output is correct |
28 |
Correct |
575 ms |
12688 KB |
Output is correct |
29 |
Correct |
588 ms |
15496 KB |
Output is correct |
30 |
Correct |
1164 ms |
27152 KB |
Output is correct |
31 |
Correct |
6 ms |
512 KB |
Output is correct |
32 |
Correct |
965 ms |
17232 KB |
Output is correct |
33 |
Correct |
172 ms |
17116 KB |
Output is correct |
34 |
Correct |
364 ms |
71260 KB |
Output is correct |
35 |
Correct |
529 ms |
56668 KB |
Output is correct |
36 |
Correct |
487 ms |
55900 KB |
Output is correct |
37 |
Correct |
135 ms |
12408 KB |
Output is correct |
38 |
Correct |
204 ms |
15452 KB |
Output is correct |
39 |
Correct |
216 ms |
18524 KB |
Output is correct |
40 |
Correct |
227 ms |
18528 KB |
Output is correct |
41 |
Correct |
80 ms |
6352 KB |
Output is correct |
42 |
Correct |
174 ms |
14812 KB |
Output is correct |
43 |
Correct |
177 ms |
17244 KB |
Output is correct |
44 |
Correct |
386 ms |
71400 KB |
Output is correct |
45 |
Correct |
528 ms |
56676 KB |
Output is correct |
46 |
Correct |
467 ms |
55900 KB |
Output is correct |
47 |
Correct |
113 ms |
11228 KB |
Output is correct |
48 |
Correct |
191 ms |
16092 KB |
Output is correct |
49 |
Correct |
334 ms |
26588 KB |
Output is correct |
50 |
Correct |
265 ms |
43612 KB |
Output is correct |
51 |
Correct |
268 ms |
43612 KB |
Output is correct |
52 |
Correct |
206 ms |
18524 KB |
Output is correct |
53 |
Correct |
222 ms |
18396 KB |
Output is correct |
54 |
Correct |
81 ms |
6372 KB |
Output is correct |
55 |
Correct |
165 ms |
14812 KB |
Output is correct |
56 |
Correct |
175 ms |
17248 KB |
Output is correct |
57 |
Correct |
350 ms |
71388 KB |
Output is correct |
58 |
Correct |
526 ms |
56668 KB |
Output is correct |
59 |
Correct |
468 ms |
56028 KB |
Output is correct |
60 |
Correct |
116 ms |
11228 KB |
Output is correct |
61 |
Correct |
196 ms |
16092 KB |
Output is correct |
62 |
Correct |
351 ms |
26716 KB |
Output is correct |
63 |
Correct |
276 ms |
43612 KB |
Output is correct |
64 |
Correct |
269 ms |
43616 KB |
Output is correct |
65 |
Correct |
883 ms |
18832 KB |
Output is correct |
66 |
Correct |
1039 ms |
18780 KB |
Output is correct |
67 |
Correct |
508 ms |
8436 KB |
Output is correct |
68 |
Correct |
663 ms |
15304 KB |
Output is correct |
69 |
Correct |
450 ms |
17556 KB |
Output is correct |
70 |
Correct |
954 ms |
71836 KB |
Output is correct |
71 |
Correct |
1315 ms |
57188 KB |
Output is correct |
72 |
Correct |
996 ms |
56964 KB |
Output is correct |
73 |
Correct |
343 ms |
11664 KB |
Output is correct |
74 |
Correct |
607 ms |
16656 KB |
Output is correct |
75 |
Correct |
727 ms |
27084 KB |
Output is correct |
76 |
Correct |
924 ms |
44264 KB |
Output is correct |
77 |
Correct |
733 ms |
44204 KB |
Output is correct |
78 |
Correct |
351 ms |
10284 KB |
Output is correct |
79 |
Correct |
446 ms |
18064 KB |
Output is correct |
80 |
Correct |
440 ms |
18192 KB |
Output is correct |
81 |
Correct |
365 ms |
15632 KB |
Output is correct |
82 |
Correct |
430 ms |
15888 KB |
Output is correct |
83 |
Correct |
135 ms |
9616 KB |
Output is correct |
84 |
Correct |
446 ms |
18064 KB |
Output is correct |
85 |
Correct |
459 ms |
18192 KB |
Output is correct |
86 |
Correct |
392 ms |
15632 KB |
Output is correct |
87 |
Correct |
256 ms |
16752 KB |
Output is correct |
88 |
Correct |
250 ms |
18196 KB |
Output is correct |
89 |
Correct |
274 ms |
18064 KB |
Output is correct |
90 |
Correct |
259 ms |
15504 KB |
Output is correct |
91 |
Correct |
375 ms |
15272 KB |
Output is correct |