#include <bits/stdc++.h>
#include "rainbow.h"
template <class T>
using Vec = std::vector<T>;
struct Count2D {
int size;
Vec<Vec<int>> pts;
Vec<Vec<int>> left, right;
Count2D() = default;
Count2D(const int range) {
size = 1;
while (size < range) {
size <<= 1;
}
pts.resize(2 * size);
left.resize(size);
right.resize(size);
}
void add(const int x, const int y) {
pts[x + size].push_back(y);
}
void build() {
for (int i = size; i < 2 * size; ++i) {
std::sort(pts[i].begin(), pts[i].end());
pts[i].erase(std::unique(pts[i].begin(), pts[i].end()), pts[i].end());
}
for (int i = size - 1; i > 0; --i) {
const auto l = 2 * i;
const auto r = 2 * i + 1;
const auto l_sz = (int) pts[l].size();
const auto r_sz = (int) pts[r].size();
const auto sz = l_sz + r_sz;
pts[i].reserve(sz);
left[i].reserve(sz + 1);
right[i].reserve(sz + 1);
int s = 0, t = 0;
while (s < l_sz || t < r_sz) {
left[i].push_back(s);
right[i].push_back(t);
if (t == r_sz || (s < l_sz && pts[l][s] < pts[r][t])) {
pts[i].push_back(pts[l][s]);
s += 1;
}
else {
pts[i].push_back(pts[r][t]);
t += 1;
}
}
left[i].push_back(l_sz);
right[i].push_back(r_sz);
}
}
int count(const int xl, const int xr, int yl, int yr) const {
yl = std::lower_bound(pts[1].begin(), pts[1].end(), yl) - pts[1].begin();
yr = std::lower_bound(pts[1].begin(), pts[1].end(), yr) - pts[1].begin();
return count(xl, xr, yl, yr, 1, 0, size);
}
int count(const int xl, const int xr, const int yl, const int yr, const int k, const int l, const int r) const {
if (xl <= l && r <= xr) {
return yr - yl;
}
if (xr <= l || r <= xl) {
return 0;
}
const auto m = (l + r) / 2;
return count(xl, xr, left[k][yl], left[k][yr], 2 * k, l, m) + count(xl, xr, right[k][yl], right[k][yr], 2 * k + 1, m, r);
}
};
int Row, Column;
int maxX, minX, maxY, minY;
Count2D vert, edgeR, edgeC, cycle;
void add(const int x, const int y) {
maxX = std::max(maxX, x);
minX = std::min(minX, x);
maxY = std::max(maxY, y);
minY = std::min(minY, y);
vert.add(x, y);
edgeR.add(x, y);
edgeC.add(x, y);
cycle.add(x, y);
if (x > 0) {
edgeC.add(x - 1, y);
cycle.add(x - 1, y);
}
if (y > 0) {
edgeR.add(x, y - 1);
cycle.add(x, y - 1);
}
if (x > 0 && y > 0) {
cycle.add(x - 1, y - 1);
}
}
void init(int R, int C, int sr, int sc, int M, char *S) {
Row = R;
Column = C;
maxX = -1, minX = R, maxY = -1, minY = C;
vert = Count2D(R);
edgeR = Count2D(R);
edgeC = Count2D(R);
cycle = Count2D(R);
sr -= 1;
sc -= 1;
add(sr, sc);
for (int i = 0; i < M; ++i) {
if (S[i] == 'N') sr -= 1;
if (S[i] == 'S') sr += 1;
if (S[i] == 'E') sc += 1;
if (S[i] == 'W') sc -= 1;
add(sr, sc);
}
vert.build();
edgeR.build();
edgeC.build();
cycle.build();
}
int colour(int ar, int ac, int br, int bc) {
ar -= 1;
ac -= 1;
long long V = 0, E = 0, C = 0;
V += (long long) (br - ar) * (bc - ac);
V -= vert.count(ar, br, ac, bc);
E += (long long) (br - ar) * (bc - ac - 1);
E -= edgeR.count(ar, br, ac, bc - 1);
E += (long long) (br - ar - 1) * (bc - ac);
E -= edgeC.count(ar, br - 1, ac, bc);
C += (long long) (br - ar - 1) * (bc - ac - 1);
C -= cycle.count(ar, br - 1, ac, bc - 1);
if (ar < minX && br > maxX + 1 && ac < minY && bc > maxY + 1) {
C += 1;
}
return V + C - E;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
4 ms |
748 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
4 |
Correct |
3 ms |
492 KB |
Output is correct |
5 |
Correct |
4 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
652 KB |
Output is correct |
12 |
Correct |
4 ms |
748 KB |
Output is correct |
13 |
Correct |
4 ms |
1004 KB |
Output is correct |
14 |
Correct |
5 ms |
1408 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
153 ms |
9568 KB |
Output is correct |
4 |
Correct |
211 ms |
13264 KB |
Output is correct |
5 |
Correct |
202 ms |
13048 KB |
Output is correct |
6 |
Correct |
164 ms |
11952 KB |
Output is correct |
7 |
Correct |
193 ms |
11212 KB |
Output is correct |
8 |
Correct |
86 ms |
7764 KB |
Output is correct |
9 |
Correct |
202 ms |
12952 KB |
Output is correct |
10 |
Correct |
198 ms |
13136 KB |
Output is correct |
11 |
Correct |
169 ms |
11604 KB |
Output is correct |
12 |
Correct |
116 ms |
13212 KB |
Output is correct |
13 |
Correct |
115 ms |
13392 KB |
Output is correct |
14 |
Correct |
116 ms |
13136 KB |
Output is correct |
15 |
Correct |
115 ms |
11596 KB |
Output is correct |
16 |
Correct |
161 ms |
10140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
466 ms |
295368 KB |
Output is correct |
3 |
Correct |
543 ms |
300560 KB |
Output is correct |
4 |
Correct |
505 ms |
296172 KB |
Output is correct |
5 |
Correct |
459 ms |
265668 KB |
Output is correct |
6 |
Correct |
306 ms |
193516 KB |
Output is correct |
7 |
Correct |
354 ms |
217068 KB |
Output is correct |
8 |
Correct |
70 ms |
20116 KB |
Output is correct |
9 |
Correct |
79 ms |
23896 KB |
Output is correct |
10 |
Correct |
180 ms |
108652 KB |
Output is correct |
11 |
Correct |
263 ms |
145692 KB |
Output is correct |
12 |
Correct |
467 ms |
295316 KB |
Output is correct |
13 |
Correct |
534 ms |
300396 KB |
Output is correct |
14 |
Correct |
508 ms |
296172 KB |
Output is correct |
15 |
Correct |
458 ms |
265636 KB |
Output is correct |
16 |
Correct |
301 ms |
188388 KB |
Output is correct |
17 |
Correct |
354 ms |
217452 KB |
Output is correct |
18 |
Correct |
481 ms |
295916 KB |
Output is correct |
19 |
Correct |
457 ms |
278164 KB |
Output is correct |
20 |
Correct |
452 ms |
277924 KB |
Output is correct |
21 |
Correct |
70 ms |
19988 KB |
Output is correct |
22 |
Correct |
81 ms |
24024 KB |
Output is correct |
23 |
Correct |
181 ms |
108524 KB |
Output is correct |
24 |
Correct |
264 ms |
145820 KB |
Output is correct |
25 |
Correct |
466 ms |
295444 KB |
Output is correct |
26 |
Correct |
541 ms |
300524 KB |
Output is correct |
27 |
Correct |
507 ms |
296172 KB |
Output is correct |
28 |
Correct |
454 ms |
265764 KB |
Output is correct |
29 |
Correct |
298 ms |
188396 KB |
Output is correct |
30 |
Correct |
361 ms |
217452 KB |
Output is correct |
31 |
Correct |
482 ms |
296044 KB |
Output is correct |
32 |
Correct |
459 ms |
277924 KB |
Output is correct |
33 |
Correct |
457 ms |
278052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
4 ms |
748 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
4 |
Correct |
3 ms |
492 KB |
Output is correct |
5 |
Correct |
4 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
652 KB |
Output is correct |
12 |
Correct |
4 ms |
748 KB |
Output is correct |
13 |
Correct |
4 ms |
1004 KB |
Output is correct |
14 |
Correct |
5 ms |
1408 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1016 ms |
44724 KB |
Output is correct |
19 |
Correct |
248 ms |
5228 KB |
Output is correct |
20 |
Correct |
199 ms |
4076 KB |
Output is correct |
21 |
Correct |
245 ms |
4460 KB |
Output is correct |
22 |
Correct |
269 ms |
5228 KB |
Output is correct |
23 |
Correct |
248 ms |
5144 KB |
Output is correct |
24 |
Correct |
214 ms |
4332 KB |
Output is correct |
25 |
Correct |
253 ms |
4844 KB |
Output is correct |
26 |
Correct |
275 ms |
5228 KB |
Output is correct |
27 |
Correct |
569 ms |
38324 KB |
Output is correct |
28 |
Correct |
495 ms |
23532 KB |
Output is correct |
29 |
Correct |
609 ms |
36076 KB |
Output is correct |
30 |
Correct |
859 ms |
79596 KB |
Output is correct |
31 |
Correct |
4 ms |
492 KB |
Output is correct |
32 |
Correct |
981 ms |
38220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
4 ms |
748 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
4 |
Correct |
3 ms |
492 KB |
Output is correct |
5 |
Correct |
4 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
652 KB |
Output is correct |
12 |
Correct |
4 ms |
748 KB |
Output is correct |
13 |
Correct |
4 ms |
1004 KB |
Output is correct |
14 |
Correct |
5 ms |
1408 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1016 ms |
44724 KB |
Output is correct |
19 |
Correct |
248 ms |
5228 KB |
Output is correct |
20 |
Correct |
199 ms |
4076 KB |
Output is correct |
21 |
Correct |
245 ms |
4460 KB |
Output is correct |
22 |
Correct |
269 ms |
5228 KB |
Output is correct |
23 |
Correct |
248 ms |
5144 KB |
Output is correct |
24 |
Correct |
214 ms |
4332 KB |
Output is correct |
25 |
Correct |
253 ms |
4844 KB |
Output is correct |
26 |
Correct |
275 ms |
5228 KB |
Output is correct |
27 |
Correct |
569 ms |
38324 KB |
Output is correct |
28 |
Correct |
495 ms |
23532 KB |
Output is correct |
29 |
Correct |
609 ms |
36076 KB |
Output is correct |
30 |
Correct |
859 ms |
79596 KB |
Output is correct |
31 |
Correct |
4 ms |
492 KB |
Output is correct |
32 |
Correct |
981 ms |
38220 KB |
Output is correct |
33 |
Correct |
466 ms |
295368 KB |
Output is correct |
34 |
Correct |
543 ms |
300560 KB |
Output is correct |
35 |
Correct |
505 ms |
296172 KB |
Output is correct |
36 |
Correct |
459 ms |
265668 KB |
Output is correct |
37 |
Correct |
306 ms |
193516 KB |
Output is correct |
38 |
Correct |
354 ms |
217068 KB |
Output is correct |
39 |
Correct |
70 ms |
20116 KB |
Output is correct |
40 |
Correct |
79 ms |
23896 KB |
Output is correct |
41 |
Correct |
180 ms |
108652 KB |
Output is correct |
42 |
Correct |
263 ms |
145692 KB |
Output is correct |
43 |
Correct |
467 ms |
295316 KB |
Output is correct |
44 |
Correct |
534 ms |
300396 KB |
Output is correct |
45 |
Correct |
508 ms |
296172 KB |
Output is correct |
46 |
Correct |
458 ms |
265636 KB |
Output is correct |
47 |
Correct |
301 ms |
188388 KB |
Output is correct |
48 |
Correct |
354 ms |
217452 KB |
Output is correct |
49 |
Correct |
481 ms |
295916 KB |
Output is correct |
50 |
Correct |
457 ms |
278164 KB |
Output is correct |
51 |
Correct |
452 ms |
277924 KB |
Output is correct |
52 |
Correct |
70 ms |
19988 KB |
Output is correct |
53 |
Correct |
81 ms |
24024 KB |
Output is correct |
54 |
Correct |
181 ms |
108524 KB |
Output is correct |
55 |
Correct |
264 ms |
145820 KB |
Output is correct |
56 |
Correct |
466 ms |
295444 KB |
Output is correct |
57 |
Correct |
541 ms |
300524 KB |
Output is correct |
58 |
Correct |
507 ms |
296172 KB |
Output is correct |
59 |
Correct |
454 ms |
265764 KB |
Output is correct |
60 |
Correct |
298 ms |
188396 KB |
Output is correct |
61 |
Correct |
361 ms |
217452 KB |
Output is correct |
62 |
Correct |
482 ms |
296044 KB |
Output is correct |
63 |
Correct |
459 ms |
277924 KB |
Output is correct |
64 |
Correct |
457 ms |
278052 KB |
Output is correct |
65 |
Correct |
153 ms |
9568 KB |
Output is correct |
66 |
Correct |
211 ms |
13264 KB |
Output is correct |
67 |
Correct |
202 ms |
13048 KB |
Output is correct |
68 |
Correct |
164 ms |
11952 KB |
Output is correct |
69 |
Correct |
193 ms |
11212 KB |
Output is correct |
70 |
Correct |
86 ms |
7764 KB |
Output is correct |
71 |
Correct |
202 ms |
12952 KB |
Output is correct |
72 |
Correct |
198 ms |
13136 KB |
Output is correct |
73 |
Correct |
169 ms |
11604 KB |
Output is correct |
74 |
Correct |
116 ms |
13212 KB |
Output is correct |
75 |
Correct |
115 ms |
13392 KB |
Output is correct |
76 |
Correct |
116 ms |
13136 KB |
Output is correct |
77 |
Correct |
115 ms |
11596 KB |
Output is correct |
78 |
Correct |
161 ms |
10140 KB |
Output is correct |
79 |
Correct |
459 ms |
23444 KB |
Output is correct |
80 |
Correct |
544 ms |
27352 KB |
Output is correct |
81 |
Correct |
1313 ms |
112364 KB |
Output is correct |
82 |
Correct |
1287 ms |
149344 KB |
Output is correct |
83 |
Correct |
1815 ms |
299196 KB |
Output is correct |
84 |
Correct |
1853 ms |
304240 KB |
Output is correct |
85 |
Correct |
1759 ms |
299884 KB |
Output is correct |
86 |
Correct |
1705 ms |
269348 KB |
Output is correct |
87 |
Correct |
1392 ms |
192288 KB |
Output is correct |
88 |
Correct |
1508 ms |
221164 KB |
Output is correct |
89 |
Correct |
1604 ms |
299756 KB |
Output is correct |
90 |
Correct |
1909 ms |
281552 KB |
Output is correct |
91 |
Correct |
1450 ms |
281552 KB |
Output is correct |