#include "rainbow.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <set>
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());
}
int pos(vector<int> &vec, int x) { return int(upper_bound(vec.begin(), vec.end(), x) - vec.begin() - 1); }
struct SegmentTree2D
{
int nx;
vector<int> valx;
vector<vector<int>> valy, it;
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);
}
}
void modify(int xx, int yy, int zz)
{
for (int x = pos(valx, xx) + nx; x >= 1; x >>= 1) {
int ny = (int) valy[x].size();
for (int y = pos(valy[x], yy) + ny; y >= 1; y >>= 1) {
it[x][y] += zz;
}
}
}
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;
}
} st2d;
struct SegmentTree1D
{
int n;
vector<int> vals, it;
void build()
{
normalize(vals);
n = (int) vals.size();
it.assign(n * 2, 0);
}
void modify(int i, int x)
{
for (i = pos(vals, i) + n; i >= 1; i >>= 1) {
it[i] += x;
}
}
int get(int l, int r)
{
int ans = 0;
for (l = pos(vals, l - 1) + n + 1, r = pos(vals, r) + n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) ans += it[l++];
if (r & 1) ans += it[--r];
}
return ans;
}
};
vector<SegmentTree1D> rowst, colst;
int minr, maxr, minc, maxc;
set<pair<int, int>> vxset;
bool ck(int r, int c) { return vxset.find(make_pair(r, c)) == vxset.end(); }
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) {
// cerr << S[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);
}
// cerr << endl;
normalize(vxvec);
auto sqvec = vxvec;
for (auto p : vxvec) {
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(sqvec);
st2d.build(sqvec);
vxset = set<pair<int, int>>(vxvec.begin(), vxvec.end());
for (auto p : sqvec) {
bool b0 = (vxset.find(make_pair(p.first, p.second)) == vxset.end());
bool b1 = (vxset.find(make_pair(p.first + 1, p.second)) == vxset.end());
bool b2 = (vxset.find(make_pair(p.first, p.second + 1)) == vxset.end());
bool b3 = (vxset.find(make_pair(p.first + 1, p.second + 1)) == vxset.end());
int cnt = (int) b0 + b1 + b2 + b3;
if (cnt == 3) st2d.modify(p.first, p.second, -1);
else if (cnt == 1) st2d.modify(p.first, p.second, +1);
else if (cnt == 2 && ((b0 && b3) || (b1 && b2))) st2d.modify(p.first, p.second, +2);
}
rowst.resize(R + 1), colst.resize(C + 1);
for (auto p : vxvec) rowst[p.first].vals.push_back(p.second), colst[p.second].vals.push_back(p.first);
for (auto p : vxvec) rowst[p.first].vals.push_back(p.second - 1), colst[p.second].vals.push_back(p.first - 1);
for (int i = 1; i <= R; ++i) rowst[i].build();
for (int i = 1; i <= C; ++i) colst[i].build();
for (auto p : vxvec) {
if (vxset.find(make_pair(p.first, p.second - 1)) == vxset.end()) rowst[p.first].modify(p.second - 1, 1);
if (vxset.find(make_pair(p.first - 1, p.second)) == vxset.end()) colst[p.second].modify(p.first - 1, 1);
if (vxset.find(make_pair(p.first, p.second + 1)) == vxset.end()) rowst[p.first].modify(p.second, 1);
if (vxset.find(make_pair(p.first + 1, p.second)) == vxset.end()) colst[p.second].modify(p.first, 1);
}
// cout << "T " << colst[4].get(3, 3) << endl;
}
int colour(int ar, int ac, int br, int bc)
{
int ans = st2d.get(ar, br - 1, ac, bc - 1);
// cout << "S " << ans << endl;
ans += rowst[ar].get(ac, bc - 1);
// cout << "S " << ans << endl;
ans += rowst[br].get(ac, bc - 1);
// cout << "S " << ans << endl;
ans += colst[ac].get(ar, br - 1);
// cout << "S " << ans << endl;
ans += colst[bc].get(ar, br - 1);
// cout << "S " << ans << endl;
ans += ck(ar, ac) + ck(ar, bc) + ck(br, ac) + ck(br, bc);
// cout << "S " << ans << endl;
assert(ans % 4 == 0);
ans /= 4;
if (ar < minr && maxr < br && ac < minc && maxc < bc) ++ans;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
640 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 |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
14 |
Correct |
6 ms |
768 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 |
312 ms |
24992 KB |
Output is correct |
4 |
Correct |
465 ms |
33868 KB |
Output is correct |
5 |
Correct |
469 ms |
33112 KB |
Output is correct |
6 |
Correct |
382 ms |
27456 KB |
Output is correct |
7 |
Correct |
436 ms |
26924 KB |
Output is correct |
8 |
Correct |
94 ms |
12160 KB |
Output is correct |
9 |
Correct |
470 ms |
33868 KB |
Output is correct |
10 |
Correct |
510 ms |
33112 KB |
Output is correct |
11 |
Correct |
439 ms |
27456 KB |
Output is correct |
12 |
Correct |
307 ms |
32500 KB |
Output is correct |
13 |
Correct |
309 ms |
33868 KB |
Output is correct |
14 |
Correct |
317 ms |
33180 KB |
Output is correct |
15 |
Correct |
285 ms |
27456 KB |
Output is correct |
16 |
Correct |
355 ms |
27272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
226 ms |
43596 KB |
Output is correct |
3 |
Correct |
351 ms |
73112 KB |
Output is correct |
4 |
Correct |
525 ms |
67368 KB |
Output is correct |
5 |
Correct |
277 ms |
58648 KB |
Output is correct |
6 |
Correct |
77 ms |
28460 KB |
Output is correct |
7 |
Correct |
136 ms |
33376 KB |
Output is correct |
8 |
Correct |
207 ms |
22584 KB |
Output is correct |
9 |
Correct |
190 ms |
20128 KB |
Output is correct |
10 |
Correct |
54 ms |
10940 KB |
Output is correct |
11 |
Correct |
136 ms |
22148 KB |
Output is correct |
12 |
Correct |
222 ms |
43724 KB |
Output is correct |
13 |
Correct |
334 ms |
73164 KB |
Output is correct |
14 |
Correct |
535 ms |
67528 KB |
Output is correct |
15 |
Correct |
272 ms |
58520 KB |
Output is correct |
16 |
Correct |
66 ms |
27288 KB |
Output is correct |
17 |
Correct |
138 ms |
33764 KB |
Output is correct |
18 |
Correct |
263 ms |
48200 KB |
Output is correct |
19 |
Correct |
408 ms |
70732 KB |
Output is correct |
20 |
Correct |
399 ms |
70604 KB |
Output is correct |
21 |
Correct |
193 ms |
22440 KB |
Output is correct |
22 |
Correct |
186 ms |
20128 KB |
Output is correct |
23 |
Correct |
55 ms |
10940 KB |
Output is correct |
24 |
Correct |
141 ms |
22136 KB |
Output is correct |
25 |
Correct |
221 ms |
43724 KB |
Output is correct |
26 |
Correct |
337 ms |
73236 KB |
Output is correct |
27 |
Correct |
534 ms |
67656 KB |
Output is correct |
28 |
Correct |
271 ms |
58648 KB |
Output is correct |
29 |
Correct |
67 ms |
27288 KB |
Output is correct |
30 |
Correct |
139 ms |
33760 KB |
Output is correct |
31 |
Correct |
264 ms |
48204 KB |
Output is correct |
32 |
Correct |
399 ms |
70892 KB |
Output is correct |
33 |
Correct |
417 ms |
70616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
640 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 |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
14 |
Correct |
6 ms |
768 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 |
636 ms |
14924 KB |
Output is correct |
19 |
Correct |
134 ms |
1784 KB |
Output is correct |
20 |
Correct |
117 ms |
1144 KB |
Output is correct |
21 |
Correct |
123 ms |
1272 KB |
Output is correct |
22 |
Correct |
129 ms |
1400 KB |
Output is correct |
23 |
Correct |
124 ms |
1656 KB |
Output is correct |
24 |
Correct |
153 ms |
1272 KB |
Output is correct |
25 |
Correct |
150 ms |
4220 KB |
Output is correct |
26 |
Correct |
141 ms |
4344 KB |
Output is correct |
27 |
Correct |
314 ms |
13484 KB |
Output is correct |
28 |
Correct |
263 ms |
8736 KB |
Output is correct |
29 |
Correct |
312 ms |
12448 KB |
Output is correct |
30 |
Correct |
573 ms |
26380 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
551 ms |
13852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
640 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 |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
14 |
Correct |
6 ms |
768 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 |
636 ms |
14924 KB |
Output is correct |
19 |
Correct |
134 ms |
1784 KB |
Output is correct |
20 |
Correct |
117 ms |
1144 KB |
Output is correct |
21 |
Correct |
123 ms |
1272 KB |
Output is correct |
22 |
Correct |
129 ms |
1400 KB |
Output is correct |
23 |
Correct |
124 ms |
1656 KB |
Output is correct |
24 |
Correct |
153 ms |
1272 KB |
Output is correct |
25 |
Correct |
150 ms |
4220 KB |
Output is correct |
26 |
Correct |
141 ms |
4344 KB |
Output is correct |
27 |
Correct |
314 ms |
13484 KB |
Output is correct |
28 |
Correct |
263 ms |
8736 KB |
Output is correct |
29 |
Correct |
312 ms |
12448 KB |
Output is correct |
30 |
Correct |
573 ms |
26380 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
551 ms |
13852 KB |
Output is correct |
33 |
Correct |
226 ms |
43596 KB |
Output is correct |
34 |
Correct |
351 ms |
73112 KB |
Output is correct |
35 |
Correct |
525 ms |
67368 KB |
Output is correct |
36 |
Correct |
277 ms |
58648 KB |
Output is correct |
37 |
Correct |
77 ms |
28460 KB |
Output is correct |
38 |
Correct |
136 ms |
33376 KB |
Output is correct |
39 |
Correct |
207 ms |
22584 KB |
Output is correct |
40 |
Correct |
190 ms |
20128 KB |
Output is correct |
41 |
Correct |
54 ms |
10940 KB |
Output is correct |
42 |
Correct |
136 ms |
22148 KB |
Output is correct |
43 |
Correct |
222 ms |
43724 KB |
Output is correct |
44 |
Correct |
334 ms |
73164 KB |
Output is correct |
45 |
Correct |
535 ms |
67528 KB |
Output is correct |
46 |
Correct |
272 ms |
58520 KB |
Output is correct |
47 |
Correct |
66 ms |
27288 KB |
Output is correct |
48 |
Correct |
138 ms |
33764 KB |
Output is correct |
49 |
Correct |
263 ms |
48200 KB |
Output is correct |
50 |
Correct |
408 ms |
70732 KB |
Output is correct |
51 |
Correct |
399 ms |
70604 KB |
Output is correct |
52 |
Correct |
193 ms |
22440 KB |
Output is correct |
53 |
Correct |
186 ms |
20128 KB |
Output is correct |
54 |
Correct |
55 ms |
10940 KB |
Output is correct |
55 |
Correct |
141 ms |
22136 KB |
Output is correct |
56 |
Correct |
221 ms |
43724 KB |
Output is correct |
57 |
Correct |
337 ms |
73236 KB |
Output is correct |
58 |
Correct |
534 ms |
67656 KB |
Output is correct |
59 |
Correct |
271 ms |
58648 KB |
Output is correct |
60 |
Correct |
67 ms |
27288 KB |
Output is correct |
61 |
Correct |
139 ms |
33760 KB |
Output is correct |
62 |
Correct |
264 ms |
48204 KB |
Output is correct |
63 |
Correct |
399 ms |
70892 KB |
Output is correct |
64 |
Correct |
417 ms |
70616 KB |
Output is correct |
65 |
Correct |
561 ms |
22584 KB |
Output is correct |
66 |
Correct |
583 ms |
20612 KB |
Output is correct |
67 |
Correct |
284 ms |
13384 KB |
Output is correct |
68 |
Correct |
383 ms |
23280 KB |
Output is correct |
69 |
Correct |
355 ms |
43852 KB |
Output is correct |
70 |
Correct |
586 ms |
73292 KB |
Output is correct |
71 |
Correct |
736 ms |
67788 KB |
Output is correct |
72 |
Correct |
457 ms |
59068 KB |
Output is correct |
73 |
Correct |
186 ms |
29448 KB |
Output is correct |
74 |
Correct |
265 ms |
34992 KB |
Output is correct |
75 |
Correct |
394 ms |
48344 KB |
Output is correct |
76 |
Correct |
608 ms |
70860 KB |
Output is correct |
77 |
Correct |
621 ms |
70604 KB |
Output is correct |
78 |
Correct |
312 ms |
24992 KB |
Output is correct |
79 |
Correct |
465 ms |
33868 KB |
Output is correct |
80 |
Correct |
469 ms |
33112 KB |
Output is correct |
81 |
Correct |
382 ms |
27456 KB |
Output is correct |
82 |
Correct |
436 ms |
26924 KB |
Output is correct |
83 |
Correct |
94 ms |
12160 KB |
Output is correct |
84 |
Correct |
470 ms |
33868 KB |
Output is correct |
85 |
Correct |
510 ms |
33112 KB |
Output is correct |
86 |
Correct |
439 ms |
27456 KB |
Output is correct |
87 |
Correct |
307 ms |
32500 KB |
Output is correct |
88 |
Correct |
309 ms |
33868 KB |
Output is correct |
89 |
Correct |
317 ms |
33180 KB |
Output is correct |
90 |
Correct |
285 ms |
27456 KB |
Output is correct |
91 |
Correct |
355 ms |
27272 KB |
Output is correct |