#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 |
1 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 |
596 KB |
Output is correct |
14 |
Correct |
4 ms |
724 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 |
191 ms |
10060 KB |
Output is correct |
4 |
Correct |
279 ms |
16988 KB |
Output is correct |
5 |
Correct |
267 ms |
16480 KB |
Output is correct |
6 |
Correct |
201 ms |
12248 KB |
Output is correct |
7 |
Correct |
213 ms |
11880 KB |
Output is correct |
8 |
Correct |
60 ms |
4768 KB |
Output is correct |
9 |
Correct |
266 ms |
16868 KB |
Output is correct |
10 |
Correct |
283 ms |
16468 KB |
Output is correct |
11 |
Correct |
221 ms |
12380 KB |
Output is correct |
12 |
Correct |
190 ms |
16232 KB |
Output is correct |
13 |
Correct |
183 ms |
16892 KB |
Output is correct |
14 |
Correct |
189 ms |
16372 KB |
Output is correct |
15 |
Correct |
160 ms |
12244 KB |
Output is correct |
16 |
Correct |
215 ms |
12476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
653 ms |
122572 KB |
Output is correct |
3 |
Correct |
266 ms |
104368 KB |
Output is correct |
4 |
Correct |
562 ms |
121236 KB |
Output is correct |
5 |
Correct |
331 ms |
100788 KB |
Output is correct |
6 |
Correct |
157 ms |
75068 KB |
Output is correct |
7 |
Correct |
229 ms |
80952 KB |
Output is correct |
8 |
Correct |
134 ms |
17324 KB |
Output is correct |
9 |
Correct |
144 ms |
16220 KB |
Output is correct |
10 |
Correct |
80 ms |
37992 KB |
Output is correct |
11 |
Correct |
136 ms |
44600 KB |
Output is correct |
12 |
Correct |
645 ms |
122676 KB |
Output is correct |
13 |
Correct |
259 ms |
104368 KB |
Output is correct |
14 |
Correct |
547 ms |
121356 KB |
Output is correct |
15 |
Correct |
341 ms |
100916 KB |
Output is correct |
16 |
Correct |
164 ms |
73764 KB |
Output is correct |
17 |
Correct |
274 ms |
84456 KB |
Output is correct |
18 |
Correct |
643 ms |
117760 KB |
Output is correct |
19 |
Correct |
462 ms |
113036 KB |
Output is correct |
20 |
Correct |
522 ms |
119796 KB |
Output is correct |
21 |
Correct |
133 ms |
17324 KB |
Output is correct |
22 |
Correct |
141 ms |
16324 KB |
Output is correct |
23 |
Correct |
79 ms |
37896 KB |
Output is correct |
24 |
Correct |
140 ms |
44528 KB |
Output is correct |
25 |
Correct |
666 ms |
122612 KB |
Output is correct |
26 |
Correct |
264 ms |
104452 KB |
Output is correct |
27 |
Correct |
556 ms |
121224 KB |
Output is correct |
28 |
Correct |
331 ms |
100924 KB |
Output is correct |
29 |
Correct |
151 ms |
73756 KB |
Output is correct |
30 |
Correct |
273 ms |
84400 KB |
Output is correct |
31 |
Correct |
645 ms |
117828 KB |
Output is correct |
32 |
Correct |
461 ms |
113180 KB |
Output is correct |
33 |
Correct |
529 ms |
119788 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 |
1 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 |
596 KB |
Output is correct |
14 |
Correct |
4 ms |
724 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 |
605 ms |
16356 KB |
Output is correct |
19 |
Correct |
234 ms |
4532 KB |
Output is correct |
20 |
Correct |
164 ms |
3828 KB |
Output is correct |
21 |
Correct |
188 ms |
3972 KB |
Output is correct |
22 |
Correct |
199 ms |
4300 KB |
Output is correct |
23 |
Correct |
217 ms |
4428 KB |
Output is correct |
24 |
Correct |
200 ms |
3936 KB |
Output is correct |
25 |
Correct |
206 ms |
4204 KB |
Output is correct |
26 |
Correct |
220 ms |
4372 KB |
Output is correct |
27 |
Correct |
301 ms |
13500 KB |
Output is correct |
28 |
Correct |
262 ms |
10136 KB |
Output is correct |
29 |
Correct |
294 ms |
13012 KB |
Output is correct |
30 |
Correct |
564 ms |
29488 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
432 ms |
14692 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 |
1 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 |
596 KB |
Output is correct |
14 |
Correct |
4 ms |
724 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 |
605 ms |
16356 KB |
Output is correct |
19 |
Correct |
234 ms |
4532 KB |
Output is correct |
20 |
Correct |
164 ms |
3828 KB |
Output is correct |
21 |
Correct |
188 ms |
3972 KB |
Output is correct |
22 |
Correct |
199 ms |
4300 KB |
Output is correct |
23 |
Correct |
217 ms |
4428 KB |
Output is correct |
24 |
Correct |
200 ms |
3936 KB |
Output is correct |
25 |
Correct |
206 ms |
4204 KB |
Output is correct |
26 |
Correct |
220 ms |
4372 KB |
Output is correct |
27 |
Correct |
301 ms |
13500 KB |
Output is correct |
28 |
Correct |
262 ms |
10136 KB |
Output is correct |
29 |
Correct |
294 ms |
13012 KB |
Output is correct |
30 |
Correct |
564 ms |
29488 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
432 ms |
14692 KB |
Output is correct |
33 |
Correct |
653 ms |
122572 KB |
Output is correct |
34 |
Correct |
266 ms |
104368 KB |
Output is correct |
35 |
Correct |
562 ms |
121236 KB |
Output is correct |
36 |
Correct |
331 ms |
100788 KB |
Output is correct |
37 |
Correct |
157 ms |
75068 KB |
Output is correct |
38 |
Correct |
229 ms |
80952 KB |
Output is correct |
39 |
Correct |
134 ms |
17324 KB |
Output is correct |
40 |
Correct |
144 ms |
16220 KB |
Output is correct |
41 |
Correct |
80 ms |
37992 KB |
Output is correct |
42 |
Correct |
136 ms |
44600 KB |
Output is correct |
43 |
Correct |
645 ms |
122676 KB |
Output is correct |
44 |
Correct |
259 ms |
104368 KB |
Output is correct |
45 |
Correct |
547 ms |
121356 KB |
Output is correct |
46 |
Correct |
341 ms |
100916 KB |
Output is correct |
47 |
Correct |
164 ms |
73764 KB |
Output is correct |
48 |
Correct |
274 ms |
84456 KB |
Output is correct |
49 |
Correct |
643 ms |
117760 KB |
Output is correct |
50 |
Correct |
462 ms |
113036 KB |
Output is correct |
51 |
Correct |
522 ms |
119796 KB |
Output is correct |
52 |
Correct |
133 ms |
17324 KB |
Output is correct |
53 |
Correct |
141 ms |
16324 KB |
Output is correct |
54 |
Correct |
79 ms |
37896 KB |
Output is correct |
55 |
Correct |
140 ms |
44528 KB |
Output is correct |
56 |
Correct |
666 ms |
122612 KB |
Output is correct |
57 |
Correct |
264 ms |
104452 KB |
Output is correct |
58 |
Correct |
556 ms |
121224 KB |
Output is correct |
59 |
Correct |
331 ms |
100924 KB |
Output is correct |
60 |
Correct |
151 ms |
73756 KB |
Output is correct |
61 |
Correct |
273 ms |
84400 KB |
Output is correct |
62 |
Correct |
645 ms |
117828 KB |
Output is correct |
63 |
Correct |
461 ms |
113180 KB |
Output is correct |
64 |
Correct |
529 ms |
119788 KB |
Output is correct |
65 |
Correct |
191 ms |
10060 KB |
Output is correct |
66 |
Correct |
279 ms |
16988 KB |
Output is correct |
67 |
Correct |
267 ms |
16480 KB |
Output is correct |
68 |
Correct |
201 ms |
12248 KB |
Output is correct |
69 |
Correct |
213 ms |
11880 KB |
Output is correct |
70 |
Correct |
60 ms |
4768 KB |
Output is correct |
71 |
Correct |
266 ms |
16868 KB |
Output is correct |
72 |
Correct |
283 ms |
16468 KB |
Output is correct |
73 |
Correct |
221 ms |
12380 KB |
Output is correct |
74 |
Correct |
190 ms |
16232 KB |
Output is correct |
75 |
Correct |
183 ms |
16892 KB |
Output is correct |
76 |
Correct |
189 ms |
16372 KB |
Output is correct |
77 |
Correct |
160 ms |
12244 KB |
Output is correct |
78 |
Correct |
215 ms |
12476 KB |
Output is correct |
79 |
Correct |
446 ms |
17540 KB |
Output is correct |
80 |
Correct |
544 ms |
16444 KB |
Output is correct |
81 |
Correct |
354 ms |
39900 KB |
Output is correct |
82 |
Correct |
395 ms |
45728 KB |
Output is correct |
83 |
Correct |
965 ms |
122772 KB |
Output is correct |
84 |
Correct |
558 ms |
104496 KB |
Output is correct |
85 |
Correct |
914 ms |
121320 KB |
Output is correct |
86 |
Correct |
609 ms |
101064 KB |
Output is correct |
87 |
Correct |
369 ms |
76496 KB |
Output is correct |
88 |
Correct |
473 ms |
85580 KB |
Output is correct |
89 |
Correct |
837 ms |
117996 KB |
Output is correct |
90 |
Correct |
843 ms |
113180 KB |
Output is correct |
91 |
Correct |
749 ms |
120112 KB |
Output is correct |