#include "rainbow.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <typename T>
void normalize(vector<T> &vec)
{
sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end());
}
struct SegmentTree2D
{
int nx;
vector<int> valx;
vector<vector<int>> valy, it;
int pos(vector<int> &vec, int x) { return int(upper_bound(vec.begin(), vec.end(), x) - vec.begin() - 1); }
void build(vector<pair<int, int>> vals)
{
valx.clear();
for (auto p : vals) valx.push_back(p.first);
normalize(valx);
nx = (int) valx.size();
valy.assign(nx * 2, vector<int>(0));
it.resize(nx * 2);
for (auto p : vals) {
for (int i = pos(valx, p.first) + nx; i >= 1; i >>= 1) valy[i].push_back(p.second);
}
for (int i = nx * 2 - 1; i >= 1; --i) {
normalize(valy[i]);
int ny = (int) valy[i].size();
it[i].assign(ny * 2, 0);
}
for (auto p : vals) {
for (int x = pos(valx, p.first) + nx; x >= 1; x >>= 1) {
int ny = (int) valy[x].size();
for (int y = pos(valy[x], p.second) + ny; y >= 1; y >>= 1) {
++it[x][y];
}
}
}
}
int get1d(int x, int ly, int ry)
{
int ans = 0, ny = (int) valy[x].size();
for (ly = pos(valy[x], ly - 1) + ny + 1, ry = pos(valy[x], ry) + ny + 1; ly < ry; ly >>= 1, ry >>= 1) {
if (ly & 1) ans += it[x][ly++];
if (ry & 1) ans += it[x][--ry];
}
return ans;
}
int get(int lx, int rx, int ly, int ry)
{
int ans = 0;
for (lx = pos(valx, lx - 1) + nx + 1, rx = pos(valx, rx) + nx + 1; lx < rx; lx >>= 1, rx >>= 1) {
if (lx & 1) ans += get1d(lx++, ly, ry);
if (rx & 1) ans += get1d(--rx, ly, ry);
}
return ans;
}
} hr, vt, sq, vx;
int minr, maxr, minc, maxc;
void init(int R, int C, int sr, int sc, int M, char *S)
{
vector<pair<int, int>> vxvec(M + 1);
vxvec[0] = make_pair(sr, sc);
minr = maxr = sr, minc = maxc = sc;
for (int i = 0; i < M; ++i) {
if (S[i] == 'N') --sr;
else if (S[i] == 'S') ++sr;
else if (S[i] == 'W') --sc;
else ++sc;
minr = min(minr, sr), maxr = max(maxr, sr);
minc = min(minc, sc), maxc = max(maxc, sc);
vxvec[i + 1] = make_pair(sr, sc);
}
normalize(vxvec);
auto hrvec = vxvec, vtvec = vxvec, sqvec = vxvec;
for (auto p : vxvec) {
hrvec.emplace_back(p.first, p.second - 1);
vtvec.emplace_back(p.first - 1, p.second);
sqvec.emplace_back(p.first, p.second - 1);
sqvec.emplace_back(p.first - 1, p.second);
sqvec.emplace_back(p.first - 1, p.second - 1);
}
normalize(hrvec), normalize(vtvec), normalize(sqvec);
hr.build(hrvec), vt.build(vtvec), sq.build(sqvec), vx.build(vxvec);
// for (auto p : sqvec) cout << p.first << ' ' << p.second << endl;
}
int colour(int ar, int ac, int br, int bc)
{
int64_t hrcnt = 1ll * (br - ar + 1) * (bc - ac) - hr.get(ar, br, ac, bc - 1);
int64_t vtcnt = 1ll * (br - ar) * (bc - ac + 1) - vt.get(ar, br - 1, ac, bc);
int64_t sqcnt = 1ll * (br - ar) * (bc - ac) - sq.get(ar, br - 1, ac, bc - 1);
int64_t vxcnt = 1ll * (br - ar + 1) * (bc - ac + 1) - vx.get(ar, br, ac, bc);
// cerr << hrcnt << ' ' << vtcnt << ' ' << sqcnt << ' ' << vxcnt << endl;
return (int) (vxcnt + sqcnt - hrcnt - vtcnt) + (ar < minr && maxr < br && ac < minc && maxc < bc);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
512 KB |
Output is correct |
5 |
Correct |
8 ms |
896 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
640 KB |
Output is correct |
13 |
Correct |
9 ms |
1152 KB |
Output is correct |
14 |
Correct |
10 ms |
1152 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
295 ms |
19100 KB |
Output is correct |
4 |
Correct |
417 ms |
31404 KB |
Output is correct |
5 |
Correct |
438 ms |
30780 KB |
Output is correct |
6 |
Correct |
328 ms |
23960 KB |
Output is correct |
7 |
Correct |
344 ms |
22784 KB |
Output is correct |
8 |
Correct |
76 ms |
3932 KB |
Output is correct |
9 |
Correct |
419 ms |
31424 KB |
Output is correct |
10 |
Correct |
421 ms |
30780 KB |
Output is correct |
11 |
Correct |
346 ms |
23960 KB |
Output is correct |
12 |
Correct |
332 ms |
29508 KB |
Output is correct |
13 |
Correct |
327 ms |
31276 KB |
Output is correct |
14 |
Correct |
338 ms |
30780 KB |
Output is correct |
15 |
Correct |
275 ms |
23964 KB |
Output is correct |
16 |
Correct |
310 ms |
22320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
201 ms |
26192 KB |
Output is correct |
3 |
Correct |
640 ms |
136236 KB |
Output is correct |
4 |
Correct |
1055 ms |
121132 KB |
Output is correct |
5 |
Correct |
657 ms |
94080 KB |
Output is correct |
6 |
Correct |
133 ms |
12452 KB |
Output is correct |
7 |
Correct |
263 ms |
21856 KB |
Output is correct |
8 |
Correct |
300 ms |
33208 KB |
Output is correct |
9 |
Correct |
307 ms |
31160 KB |
Output is correct |
10 |
Correct |
87 ms |
9148 KB |
Output is correct |
11 |
Correct |
314 ms |
29048 KB |
Output is correct |
12 |
Correct |
202 ms |
26156 KB |
Output is correct |
13 |
Correct |
653 ms |
136200 KB |
Output is correct |
14 |
Correct |
1052 ms |
121304 KB |
Output is correct |
15 |
Correct |
625 ms |
94084 KB |
Output is correct |
16 |
Correct |
108 ms |
9816 KB |
Output is correct |
17 |
Correct |
263 ms |
23632 KB |
Output is correct |
18 |
Correct |
530 ms |
52780 KB |
Output is correct |
19 |
Correct |
997 ms |
124204 KB |
Output is correct |
20 |
Correct |
1002 ms |
124168 KB |
Output is correct |
21 |
Correct |
295 ms |
33276 KB |
Output is correct |
22 |
Correct |
315 ms |
31160 KB |
Output is correct |
23 |
Correct |
86 ms |
9020 KB |
Output is correct |
24 |
Correct |
300 ms |
29176 KB |
Output is correct |
25 |
Correct |
197 ms |
26076 KB |
Output is correct |
26 |
Correct |
642 ms |
136236 KB |
Output is correct |
27 |
Correct |
1036 ms |
121364 KB |
Output is correct |
28 |
Correct |
668 ms |
94080 KB |
Output is correct |
29 |
Correct |
107 ms |
9916 KB |
Output is correct |
30 |
Correct |
262 ms |
23796 KB |
Output is correct |
31 |
Correct |
533 ms |
52652 KB |
Output is correct |
32 |
Correct |
978 ms |
124332 KB |
Output is correct |
33 |
Correct |
1014 ms |
124284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
512 KB |
Output is correct |
5 |
Correct |
8 ms |
896 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
640 KB |
Output is correct |
13 |
Correct |
9 ms |
1152 KB |
Output is correct |
14 |
Correct |
10 ms |
1152 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1061 ms |
29864 KB |
Output is correct |
19 |
Correct |
203 ms |
5112 KB |
Output is correct |
20 |
Correct |
171 ms |
3960 KB |
Output is correct |
21 |
Correct |
179 ms |
4344 KB |
Output is correct |
22 |
Correct |
191 ms |
4728 KB |
Output is correct |
23 |
Correct |
189 ms |
5112 KB |
Output is correct |
24 |
Correct |
241 ms |
4216 KB |
Output is correct |
25 |
Correct |
223 ms |
4472 KB |
Output is correct |
26 |
Correct |
214 ms |
4808 KB |
Output is correct |
27 |
Correct |
561 ms |
23508 KB |
Output is correct |
28 |
Correct |
499 ms |
13196 KB |
Output is correct |
29 |
Correct |
559 ms |
21456 KB |
Output is correct |
30 |
Correct |
1064 ms |
52780 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
852 ms |
23908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
512 KB |
Output is correct |
5 |
Correct |
8 ms |
896 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
640 KB |
Output is correct |
13 |
Correct |
9 ms |
1152 KB |
Output is correct |
14 |
Correct |
10 ms |
1152 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1061 ms |
29864 KB |
Output is correct |
19 |
Correct |
203 ms |
5112 KB |
Output is correct |
20 |
Correct |
171 ms |
3960 KB |
Output is correct |
21 |
Correct |
179 ms |
4344 KB |
Output is correct |
22 |
Correct |
191 ms |
4728 KB |
Output is correct |
23 |
Correct |
189 ms |
5112 KB |
Output is correct |
24 |
Correct |
241 ms |
4216 KB |
Output is correct |
25 |
Correct |
223 ms |
4472 KB |
Output is correct |
26 |
Correct |
214 ms |
4808 KB |
Output is correct |
27 |
Correct |
561 ms |
23508 KB |
Output is correct |
28 |
Correct |
499 ms |
13196 KB |
Output is correct |
29 |
Correct |
559 ms |
21456 KB |
Output is correct |
30 |
Correct |
1064 ms |
52780 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
852 ms |
23908 KB |
Output is correct |
33 |
Correct |
201 ms |
26192 KB |
Output is correct |
34 |
Correct |
640 ms |
136236 KB |
Output is correct |
35 |
Correct |
1055 ms |
121132 KB |
Output is correct |
36 |
Correct |
657 ms |
94080 KB |
Output is correct |
37 |
Correct |
133 ms |
12452 KB |
Output is correct |
38 |
Correct |
263 ms |
21856 KB |
Output is correct |
39 |
Correct |
300 ms |
33208 KB |
Output is correct |
40 |
Correct |
307 ms |
31160 KB |
Output is correct |
41 |
Correct |
87 ms |
9148 KB |
Output is correct |
42 |
Correct |
314 ms |
29048 KB |
Output is correct |
43 |
Correct |
202 ms |
26156 KB |
Output is correct |
44 |
Correct |
653 ms |
136200 KB |
Output is correct |
45 |
Correct |
1052 ms |
121304 KB |
Output is correct |
46 |
Correct |
625 ms |
94084 KB |
Output is correct |
47 |
Correct |
108 ms |
9816 KB |
Output is correct |
48 |
Correct |
263 ms |
23632 KB |
Output is correct |
49 |
Correct |
530 ms |
52780 KB |
Output is correct |
50 |
Correct |
997 ms |
124204 KB |
Output is correct |
51 |
Correct |
1002 ms |
124168 KB |
Output is correct |
52 |
Correct |
295 ms |
33276 KB |
Output is correct |
53 |
Correct |
315 ms |
31160 KB |
Output is correct |
54 |
Correct |
86 ms |
9020 KB |
Output is correct |
55 |
Correct |
300 ms |
29176 KB |
Output is correct |
56 |
Correct |
197 ms |
26076 KB |
Output is correct |
57 |
Correct |
642 ms |
136236 KB |
Output is correct |
58 |
Correct |
1036 ms |
121364 KB |
Output is correct |
59 |
Correct |
668 ms |
94080 KB |
Output is correct |
60 |
Correct |
107 ms |
9916 KB |
Output is correct |
61 |
Correct |
262 ms |
23796 KB |
Output is correct |
62 |
Correct |
533 ms |
52652 KB |
Output is correct |
63 |
Correct |
978 ms |
124332 KB |
Output is correct |
64 |
Correct |
1014 ms |
124284 KB |
Output is correct |
65 |
Correct |
626 ms |
33384 KB |
Output is correct |
66 |
Correct |
694 ms |
31288 KB |
Output is correct |
67 |
Correct |
469 ms |
10564 KB |
Output is correct |
68 |
Correct |
648 ms |
29176 KB |
Output is correct |
69 |
Correct |
309 ms |
26284 KB |
Output is correct |
70 |
Correct |
978 ms |
136364 KB |
Output is correct |
71 |
Correct |
1403 ms |
121500 KB |
Output is correct |
72 |
Correct |
884 ms |
94180 KB |
Output is correct |
73 |
Correct |
254 ms |
11196 KB |
Output is correct |
74 |
Correct |
380 ms |
23848 KB |
Output is correct |
75 |
Correct |
609 ms |
52780 KB |
Output is correct |
76 |
Correct |
1325 ms |
124376 KB |
Output is correct |
77 |
Correct |
1466 ms |
124300 KB |
Output is correct |
78 |
Correct |
295 ms |
19100 KB |
Output is correct |
79 |
Correct |
417 ms |
31404 KB |
Output is correct |
80 |
Correct |
438 ms |
30780 KB |
Output is correct |
81 |
Correct |
328 ms |
23960 KB |
Output is correct |
82 |
Correct |
344 ms |
22784 KB |
Output is correct |
83 |
Correct |
76 ms |
3932 KB |
Output is correct |
84 |
Correct |
419 ms |
31424 KB |
Output is correct |
85 |
Correct |
421 ms |
30780 KB |
Output is correct |
86 |
Correct |
346 ms |
23960 KB |
Output is correct |
87 |
Correct |
332 ms |
29508 KB |
Output is correct |
88 |
Correct |
327 ms |
31276 KB |
Output is correct |
89 |
Correct |
338 ms |
30780 KB |
Output is correct |
90 |
Correct |
275 ms |
23964 KB |
Output is correct |
91 |
Correct |
310 ms |
22320 KB |
Output is correct |