#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;
struct Fenwick {
int n;
vector<vector<int> > ys;
vector<vector<long long> > f;
vector<int> nn;
bool built;
void fake_add(int x, int y) {
assert(!built);
for (int i = x + 1; i <= n; i += i & -i) {
ys[i].emplace_back(y);
}
}
void build() {
assert(!built);
for (int i = 0; i <= n; ++i) {
sort(ys[i].begin(), ys[i].end());
ys[i].resize(unique(ys[i].begin(), ys[i].end()) - ys[i].begin());
nn[i] = ys[i].size();
f[i].resize(nn[i] + 1);
}
built = true;
}
void add(int x, int y) {
assert(built);
for (int i = x + 1; i <= n; i += i & -i) {
for (int j = lower_bound(ys[i].begin(), ys[i].end(), y) - ys[i].begin() + 1; j <= nn[i]; j += j & -j) {
f[i][j] += 1;
}
}
}
long long get(int x, int y) {
assert(built);
long long res = 0;
for (int i = x; i > 0; i -= i & -i) {
for (int j = lower_bound(ys[i].begin(), ys[i].end(), y) - ys[i].begin(); j > 0; j -= j & -j) {
res += f[i][j];
}
}
return res;
}
long long get(int x1, int y1, int x2, int y2) {
return get(x2, y2) - get(x1, y2) - get(x2, y1) + get(x1, y1);
}
Fenwick(int n = 0) : n(n), ys(n + 1), f(n + 1), nn(n + 1), built(false) {}
};
int n, m;
int k;
vector<pair<int, int> > a;
Fenwick fv, fex, fey, ff;
int mnx, mny, mxx, mxy;
bool blocked(int x, int y) {
const auto p = make_pair(x, y);
auto it = lower_bound(a.begin(), a.end(), p);
return it != a.end() && *it == p;
}
void init(int R, int C, int sr, int sc, int M, char *S) {
n = R;
m = C;
int x = sr - 1, y = sc - 1;
a.emplace_back(x, y);
for (int i = 0; i < M; ++i) {
if (S[i] == 'N') {
--x;
}
if (S[i] == 'S') {
++x;
}
if (S[i] == 'E') {
++y;
}
if (S[i] == 'W') {
--y;
}
a.emplace_back(x, y);
}
sort(a.begin(), a.end());
a.resize(unique(a.begin(), a.end()) - a.begin());
mnx = numeric_limits<int>::max();
mxx = numeric_limits<int>::min();
mny = numeric_limits<int>::max();
mxy = numeric_limits<int>::min();
vector<pair<int, int>> v, ex, ey, f;
for (const auto &[x, y] : a) {
mnx = min(mnx, x);
mxx = max(mxx, x);
mny = min(mny, y);
mxy = max(mxy, y);
v.emplace_back(x, y);
ex.emplace_back(x, y);
ey.emplace_back(x, y);
f.emplace_back(x, y);
if (x > 0) {
ex.emplace_back(x - 1, y);
f.emplace_back(x - 1, y);
}
if (y > 0) {
ey.emplace_back(x, y - 1);
f.emplace_back(x, y - 1);
}
if (x > 0 && y > 0) {
f.emplace_back(x - 1, y - 1);
}
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
sort(ex.begin(), ex.end());
ex.resize(unique(ex.begin(), ex.end()) - ex.begin());
sort(ey.begin(), ey.end());
ey.resize(unique(ey.begin(), ey.end()) - ey.begin());
sort(f.begin(), f.end());
f.resize(unique(f.begin(), f.end()) - f.begin());
fv = {n};
fex = {n};
fey = {n};
ff = {n};
for (const auto &[x, y] : v) {
fv.fake_add(x, y);
}
for (const auto &[x, y] : ex) {
fex.fake_add(x, y);
}
for (const auto &[x, y] : ey) {
fey.fake_add(x, y);
}
for (const auto &[x, y] : f) {
ff.fake_add(x, y);
}
fv.build();
fex.build();
fey.build();
ff.build();
for (const auto &[x, y] : v) {
fv.add(x, y);
}
for (const auto &[x, y] : ex) {
fex.add(x, y);
}
for (const auto &[x, y] : ey) {
fey.add(x, y);
}
for (const auto &[x, y] : f) {
ff.add(x, y);
}
}
int colour(int ar, int ac, int br, int bc) {
int x1 = ar - 1, y1 = ac - 1, x2 = br, y2 = bc;
long long v = (long long) (x2 - x1) * (y2 - y1) - fv.get(x1, y1, x2, y2);
long long e = (long long) (x2 - x1 - 1) * (y2 - y1) - fex.get(x1, y1, x2 - 1, y2) + (long long) (x2 - x1) * (y2 - y1 - 1) - fey.get(x1, y1, x2, y2 - 1);
long long f = (long long) (x2 - x1 - 1) * (y2 - y1 - 1) - ff.get(x1, y1, x2 - 1, y2 - 1) + (mnx > x1 && mny > y1 && mxx < x2 - 1 && mxy < y2 - 1);
if (v == 0) {
return 0;
}
long long c = f - e + v;
return (int) c;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
620 KB |
Output is correct |
14 |
Correct |
4 ms |
744 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
183 ms |
9936 KB |
Output is correct |
4 |
Correct |
272 ms |
16756 KB |
Output is correct |
5 |
Correct |
269 ms |
16236 KB |
Output is correct |
6 |
Correct |
201 ms |
12016 KB |
Output is correct |
7 |
Correct |
209 ms |
11644 KB |
Output is correct |
8 |
Correct |
58 ms |
1976 KB |
Output is correct |
9 |
Correct |
255 ms |
16636 KB |
Output is correct |
10 |
Correct |
260 ms |
16176 KB |
Output is correct |
11 |
Correct |
213 ms |
11932 KB |
Output is correct |
12 |
Correct |
193 ms |
16176 KB |
Output is correct |
13 |
Correct |
177 ms |
16668 KB |
Output is correct |
14 |
Correct |
177 ms |
16200 KB |
Output is correct |
15 |
Correct |
160 ms |
11964 KB |
Output is correct |
16 |
Correct |
210 ms |
12348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
646 ms |
122440 KB |
Output is correct |
3 |
Correct |
288 ms |
104496 KB |
Output is correct |
4 |
Correct |
550 ms |
121136 KB |
Output is correct |
5 |
Correct |
337 ms |
100736 KB |
Output is correct |
6 |
Correct |
158 ms |
75048 KB |
Output is correct |
7 |
Correct |
241 ms |
80796 KB |
Output is correct |
8 |
Correct |
156 ms |
17280 KB |
Output is correct |
9 |
Correct |
139 ms |
16200 KB |
Output is correct |
10 |
Correct |
82 ms |
37916 KB |
Output is correct |
11 |
Correct |
137 ms |
44432 KB |
Output is correct |
12 |
Correct |
650 ms |
122728 KB |
Output is correct |
13 |
Correct |
270 ms |
104440 KB |
Output is correct |
14 |
Correct |
607 ms |
121120 KB |
Output is correct |
15 |
Correct |
358 ms |
100868 KB |
Output is correct |
16 |
Correct |
152 ms |
73548 KB |
Output is correct |
17 |
Correct |
290 ms |
84360 KB |
Output is correct |
18 |
Correct |
700 ms |
117668 KB |
Output is correct |
19 |
Correct |
501 ms |
113008 KB |
Output is correct |
20 |
Correct |
577 ms |
119680 KB |
Output is correct |
21 |
Correct |
152 ms |
17252 KB |
Output is correct |
22 |
Correct |
145 ms |
16204 KB |
Output is correct |
23 |
Correct |
82 ms |
37956 KB |
Output is correct |
24 |
Correct |
138 ms |
44396 KB |
Output is correct |
25 |
Correct |
745 ms |
122496 KB |
Output is correct |
26 |
Correct |
292 ms |
104264 KB |
Output is correct |
27 |
Correct |
624 ms |
121116 KB |
Output is correct |
28 |
Correct |
359 ms |
100884 KB |
Output is correct |
29 |
Correct |
150 ms |
73564 KB |
Output is correct |
30 |
Correct |
289 ms |
84360 KB |
Output is correct |
31 |
Correct |
719 ms |
117720 KB |
Output is correct |
32 |
Correct |
507 ms |
113076 KB |
Output is correct |
33 |
Correct |
565 ms |
119668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
620 KB |
Output is correct |
14 |
Correct |
4 ms |
744 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
742 ms |
15372 KB |
Output is correct |
19 |
Correct |
282 ms |
1792 KB |
Output is correct |
20 |
Correct |
186 ms |
1096 KB |
Output is correct |
21 |
Correct |
217 ms |
1240 KB |
Output is correct |
22 |
Correct |
235 ms |
1536 KB |
Output is correct |
23 |
Correct |
261 ms |
1712 KB |
Output is correct |
24 |
Correct |
234 ms |
1168 KB |
Output is correct |
25 |
Correct |
238 ms |
1372 KB |
Output is correct |
26 |
Correct |
254 ms |
1580 KB |
Output is correct |
27 |
Correct |
364 ms |
12348 KB |
Output is correct |
28 |
Correct |
320 ms |
7356 KB |
Output is correct |
29 |
Correct |
365 ms |
11852 KB |
Output is correct |
30 |
Correct |
659 ms |
29260 KB |
Output is correct |
31 |
Correct |
5 ms |
340 KB |
Output is correct |
32 |
Correct |
510 ms |
13596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
620 KB |
Output is correct |
14 |
Correct |
4 ms |
744 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
742 ms |
15372 KB |
Output is correct |
19 |
Correct |
282 ms |
1792 KB |
Output is correct |
20 |
Correct |
186 ms |
1096 KB |
Output is correct |
21 |
Correct |
217 ms |
1240 KB |
Output is correct |
22 |
Correct |
235 ms |
1536 KB |
Output is correct |
23 |
Correct |
261 ms |
1712 KB |
Output is correct |
24 |
Correct |
234 ms |
1168 KB |
Output is correct |
25 |
Correct |
238 ms |
1372 KB |
Output is correct |
26 |
Correct |
254 ms |
1580 KB |
Output is correct |
27 |
Correct |
364 ms |
12348 KB |
Output is correct |
28 |
Correct |
320 ms |
7356 KB |
Output is correct |
29 |
Correct |
365 ms |
11852 KB |
Output is correct |
30 |
Correct |
659 ms |
29260 KB |
Output is correct |
31 |
Correct |
5 ms |
340 KB |
Output is correct |
32 |
Correct |
510 ms |
13596 KB |
Output is correct |
33 |
Correct |
646 ms |
122440 KB |
Output is correct |
34 |
Correct |
288 ms |
104496 KB |
Output is correct |
35 |
Correct |
550 ms |
121136 KB |
Output is correct |
36 |
Correct |
337 ms |
100736 KB |
Output is correct |
37 |
Correct |
158 ms |
75048 KB |
Output is correct |
38 |
Correct |
241 ms |
80796 KB |
Output is correct |
39 |
Correct |
156 ms |
17280 KB |
Output is correct |
40 |
Correct |
139 ms |
16200 KB |
Output is correct |
41 |
Correct |
82 ms |
37916 KB |
Output is correct |
42 |
Correct |
137 ms |
44432 KB |
Output is correct |
43 |
Correct |
650 ms |
122728 KB |
Output is correct |
44 |
Correct |
270 ms |
104440 KB |
Output is correct |
45 |
Correct |
607 ms |
121120 KB |
Output is correct |
46 |
Correct |
358 ms |
100868 KB |
Output is correct |
47 |
Correct |
152 ms |
73548 KB |
Output is correct |
48 |
Correct |
290 ms |
84360 KB |
Output is correct |
49 |
Correct |
700 ms |
117668 KB |
Output is correct |
50 |
Correct |
501 ms |
113008 KB |
Output is correct |
51 |
Correct |
577 ms |
119680 KB |
Output is correct |
52 |
Correct |
152 ms |
17252 KB |
Output is correct |
53 |
Correct |
145 ms |
16204 KB |
Output is correct |
54 |
Correct |
82 ms |
37956 KB |
Output is correct |
55 |
Correct |
138 ms |
44396 KB |
Output is correct |
56 |
Correct |
745 ms |
122496 KB |
Output is correct |
57 |
Correct |
292 ms |
104264 KB |
Output is correct |
58 |
Correct |
624 ms |
121116 KB |
Output is correct |
59 |
Correct |
359 ms |
100884 KB |
Output is correct |
60 |
Correct |
150 ms |
73564 KB |
Output is correct |
61 |
Correct |
289 ms |
84360 KB |
Output is correct |
62 |
Correct |
719 ms |
117720 KB |
Output is correct |
63 |
Correct |
507 ms |
113076 KB |
Output is correct |
64 |
Correct |
565 ms |
119668 KB |
Output is correct |
65 |
Correct |
183 ms |
9936 KB |
Output is correct |
66 |
Correct |
272 ms |
16756 KB |
Output is correct |
67 |
Correct |
269 ms |
16236 KB |
Output is correct |
68 |
Correct |
201 ms |
12016 KB |
Output is correct |
69 |
Correct |
209 ms |
11644 KB |
Output is correct |
70 |
Correct |
58 ms |
1976 KB |
Output is correct |
71 |
Correct |
255 ms |
16636 KB |
Output is correct |
72 |
Correct |
260 ms |
16176 KB |
Output is correct |
73 |
Correct |
213 ms |
11932 KB |
Output is correct |
74 |
Correct |
193 ms |
16176 KB |
Output is correct |
75 |
Correct |
177 ms |
16668 KB |
Output is correct |
76 |
Correct |
177 ms |
16200 KB |
Output is correct |
77 |
Correct |
160 ms |
11964 KB |
Output is correct |
78 |
Correct |
210 ms |
12348 KB |
Output is correct |
79 |
Correct |
542 ms |
17312 KB |
Output is correct |
80 |
Correct |
609 ms |
16080 KB |
Output is correct |
81 |
Correct |
446 ms |
37888 KB |
Output is correct |
82 |
Correct |
468 ms |
44432 KB |
Output is correct |
83 |
Correct |
1280 ms |
122420 KB |
Output is correct |
84 |
Correct |
844 ms |
104348 KB |
Output is correct |
85 |
Correct |
1184 ms |
121116 KB |
Output is correct |
86 |
Correct |
805 ms |
100740 KB |
Output is correct |
87 |
Correct |
407 ms |
73632 KB |
Output is correct |
88 |
Correct |
530 ms |
84356 KB |
Output is correct |
89 |
Correct |
922 ms |
117636 KB |
Output is correct |
90 |
Correct |
935 ms |
112944 KB |
Output is correct |
91 |
Correct |
842 ms |
119680 KB |
Output is correct |