#include <bits/stdc++.h>
#include "rainbow.h"
using namespace std;
const int MAXN = 5e6 + 25;
#define mid ((l + r) >> 1)
struct PersistentSegmentTree {
set <pair <int, int>> points;
const int L = 200000;
int tree[MAXN], tl[MAXN], tr[MAXN], cnt;
vector <pair <int, int>> roots;
int new_node (int x, int y, int z) {
++cnt; assert(cnt < MAXN); tl[cnt] = x; tr[cnt] = y;
tree[cnt] = z;
return cnt;
}
int build (int l, int r) {
if (l == r) {
cnt++; assert(cnt < MAXN);
return cnt;
}
int left = build(l, mid), right = build(mid + 1, r);
return new_node(left, right, 0);
}
int update (int l, int r, int a, int b, int node) {
if (l == r) {
int x = tree[node]; x += b;
cnt++; assert(cnt < MAXN);
tree[cnt] = x; return cnt;
}
if (a <= mid) {
int z = update(l, mid, a, b, tl[node]);
return new_node(z, tr[node], tree[z] + tree[tr[node]]);
} else {
int z = update(mid + 1, r, a, b, tr[node]);
return new_node(tl[node], z, tree[z] + tree[tl[node]]);
}
}
int get (int l, int r, int a, int b, int node) {
if (l > b || r < a) return 0;
if (l >= a && r <= b) return tree[node];
return get(l, mid, a, b, tl[node]) + get(mid + 1, r, a, b, tr[node]);
}
void init () {
cnt = 0;
int root = build(0, L);
int prev = -1;
for (auto [x, y] : points) {
int z = update(0, L, y, 1, root);
if (x != prev) {
roots.push_back({prev, root});
root = z; prev = x;
} else root = z;
}
roots.push_back({prev, root});
}
int getroot (int x) {
int l = 0, r = (int)roots.size() - 1, ans = -1;
while (l <= r) {
if (roots[mid].first <= x) {
ans = mid; l = mid + 1;
} else {
r = mid - 1;
}
}
return ans == -1 ? -1 : roots[ans].second;
}
int query (int a, int b, int c, int d) {
auto g = getroot(a - 1), h = getroot(c);
int ret = 0;
ret += get(0, L, b, d, h);
ret -= get(0, L, b, d, g);
return ret;
}
} cur[4];
typedef long long ll;
int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
int mny, mxy, mnx, mxx;
map <char, int> lk = {
{'E', 0},
{'N', 1},
{'W', 2},
{'S', 3}
};
int n, m;
set <pair <int, int>> badcells, badvertedges, badhoredges, badfaces;
void insert (int x, int y) {
if (badcells.count({x, y})) return;
badcells.insert({x, y});
if (y > 0 && !badhoredges.count({x, y - 1})) {
badhoredges.insert({x, y - 1});
}
if (x > 0 && !badvertedges.count({x - 1, y})) {
badvertedges.insert({x - 1, y});
}
if (y < m && !badhoredges.count({x, y})) {
badhoredges.insert({x, y});
}
if (x < n && !badvertedges.count({x, y})) {
badvertedges.insert({x, y});
}
if (y > 0 && x > 0 && !badfaces.count({x - 1, y - 1})) {
badfaces.insert({x - 1, y - 1});
}
if (y > 0 && x + 1 < n && !badfaces.count({x, y - 1})) {
badfaces.insert({x, y - 1});
}
if (y + 1 < m && x > 0 && !badfaces.count({x - 1, y})) {
badfaces.insert({x - 1, y});
}
if (y + 1 < m && x + 1 < n && !badfaces.count({x, y})) {
badfaces.insert({x, y});
}
}
void init (int N, int M, int x, int y, int z, char *s) {
n = N; m = M;
x--; y--;
mny = mxy = y; mnx = mxx = x;
insert(x, y);
for (int i = 0; i < z; i++) {
x += dx[lk[s[i]]]; y += dy[lk[s[i]]];
mny = min(mny, y);
mxy = max(mxy, y);
mnx = min(mnx, x);
mxx = max(mxx, x);
insert(x, y);
}
cur[0].points = badcells; cur[0].init();
cur[1].points = badvertedges; cur[1].init();
cur[2].points = badhoredges; cur[2].init();
cur[3].points = badfaces; cur[3].init();
}
ll querycell (int a, int b, int c, int d) {
return cur[0].query(a, b, c, d);
}
ll queryvert (int a, int b, int c, int d) {
return cur[1].query(a, b, c, d);
}
ll queryhor (int a, int b, int c, int d) {
return cur[2].query(a, b, c, d);
}
ll queryface (int a, int b, int c, int d) {
return cur[3].query(a, b, c, d);
}
int colour (int ar, int ac, int br, int bc) {
ar--; br--; ac--; bc--;
if (ar > br) swap(ar, br);
if (ac > bc) swap(ac, bc);
ll v = 0, e = 0, f = 0;
v = (br - ar + 1) * 1ll * (bc - ac + 1);
v -= querycell(ar, ac, br, bc);
ll e1 = (br - ar) * 1ll * (bc - ac + 1);
if (ar < br) e1 -= queryvert(ar, ac, br - 1, bc);
ll e2 = (br - ar + 1) * 1ll * (bc - ac);
if (ac < bc) e2 -= queryhor(ar, ac, br, bc - 1);
e = e1 + e2;
f = (br - ar) * 1ll * (bc - ac);
if (ar < br && ac < bc) f -= queryface(ar, ac, br - 1, bc - 1);
if (ar < mnx && br > mxx && ac < mny && bc > mxy) f++;
return v - e + f;
}
Compilation message
rainbow.cpp: In member function 'void PersistentSegmentTree::init()':
rainbow.cpp:47:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
47 | for (auto [x, y] : points) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
41564 KB |
Output is correct |
2 |
Correct |
11 ms |
44124 KB |
Output is correct |
3 |
Correct |
10 ms |
41680 KB |
Output is correct |
4 |
Correct |
9 ms |
41564 KB |
Output is correct |
5 |
Correct |
11 ms |
44124 KB |
Output is correct |
6 |
Correct |
7 ms |
41564 KB |
Output is correct |
7 |
Correct |
8 ms |
41596 KB |
Output is correct |
8 |
Correct |
7 ms |
41564 KB |
Output is correct |
9 |
Correct |
7 ms |
41820 KB |
Output is correct |
10 |
Correct |
7 ms |
41396 KB |
Output is correct |
11 |
Correct |
11 ms |
41816 KB |
Output is correct |
12 |
Correct |
10 ms |
43868 KB |
Output is correct |
13 |
Correct |
12 ms |
44380 KB |
Output is correct |
14 |
Correct |
13 ms |
44576 KB |
Output is correct |
15 |
Correct |
8 ms |
41616 KB |
Output is correct |
16 |
Correct |
7 ms |
41564 KB |
Output is correct |
17 |
Correct |
7 ms |
41564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
41564 KB |
Output is correct |
2 |
Correct |
7 ms |
41564 KB |
Output is correct |
3 |
Correct |
497 ms |
130128 KB |
Output is correct |
4 |
Correct |
637 ms |
180728 KB |
Output is correct |
5 |
Correct |
665 ms |
177840 KB |
Output is correct |
6 |
Correct |
497 ms |
144312 KB |
Output is correct |
7 |
Correct |
537 ms |
141436 KB |
Output is correct |
8 |
Correct |
223 ms |
45368 KB |
Output is correct |
9 |
Correct |
679 ms |
179040 KB |
Output is correct |
10 |
Correct |
691 ms |
176868 KB |
Output is correct |
11 |
Correct |
527 ms |
144212 KB |
Output is correct |
12 |
Correct |
320 ms |
175700 KB |
Output is correct |
13 |
Correct |
298 ms |
179156 KB |
Output is correct |
14 |
Correct |
283 ms |
176464 KB |
Output is correct |
15 |
Correct |
259 ms |
144208 KB |
Output is correct |
16 |
Correct |
529 ms |
138904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41616 KB |
Output is correct |
2 |
Correct |
376 ms |
222116 KB |
Output is correct |
3 |
Correct |
297 ms |
226124 KB |
Output is correct |
4 |
Correct |
300 ms |
224452 KB |
Output is correct |
5 |
Correct |
236 ms |
180800 KB |
Output is correct |
6 |
Correct |
73 ms |
76880 KB |
Output is correct |
7 |
Correct |
136 ms |
111956 KB |
Output is correct |
8 |
Correct |
225 ms |
165712 KB |
Output is correct |
9 |
Correct |
208 ms |
160852 KB |
Output is correct |
10 |
Correct |
72 ms |
81184 KB |
Output is correct |
11 |
Correct |
155 ms |
127116 KB |
Output is correct |
12 |
Correct |
367 ms |
222164 KB |
Output is correct |
13 |
Correct |
300 ms |
226276 KB |
Output is correct |
14 |
Correct |
303 ms |
224664 KB |
Output is correct |
15 |
Correct |
226 ms |
180824 KB |
Output is correct |
16 |
Correct |
62 ms |
70740 KB |
Output is correct |
17 |
Correct |
135 ms |
111952 KB |
Output is correct |
18 |
Correct |
318 ms |
222548 KB |
Output is correct |
19 |
Correct |
295 ms |
194540 KB |
Output is correct |
20 |
Correct |
274 ms |
195524 KB |
Output is correct |
21 |
Correct |
244 ms |
165716 KB |
Output is correct |
22 |
Correct |
213 ms |
161008 KB |
Output is correct |
23 |
Correct |
73 ms |
81236 KB |
Output is correct |
24 |
Correct |
147 ms |
127268 KB |
Output is correct |
25 |
Correct |
396 ms |
222268 KB |
Output is correct |
26 |
Correct |
292 ms |
226376 KB |
Output is correct |
27 |
Correct |
303 ms |
224848 KB |
Output is correct |
28 |
Correct |
231 ms |
180824 KB |
Output is correct |
29 |
Correct |
62 ms |
70880 KB |
Output is correct |
30 |
Correct |
136 ms |
111940 KB |
Output is correct |
31 |
Correct |
315 ms |
222436 KB |
Output is correct |
32 |
Correct |
272 ms |
197452 KB |
Output is correct |
33 |
Correct |
281 ms |
197460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
41564 KB |
Output is correct |
2 |
Correct |
11 ms |
44124 KB |
Output is correct |
3 |
Correct |
10 ms |
41680 KB |
Output is correct |
4 |
Correct |
9 ms |
41564 KB |
Output is correct |
5 |
Correct |
11 ms |
44124 KB |
Output is correct |
6 |
Correct |
7 ms |
41564 KB |
Output is correct |
7 |
Correct |
8 ms |
41596 KB |
Output is correct |
8 |
Correct |
7 ms |
41564 KB |
Output is correct |
9 |
Correct |
7 ms |
41820 KB |
Output is correct |
10 |
Correct |
7 ms |
41396 KB |
Output is correct |
11 |
Correct |
11 ms |
41816 KB |
Output is correct |
12 |
Correct |
10 ms |
43868 KB |
Output is correct |
13 |
Correct |
12 ms |
44380 KB |
Output is correct |
14 |
Correct |
13 ms |
44576 KB |
Output is correct |
15 |
Correct |
8 ms |
41616 KB |
Output is correct |
16 |
Correct |
7 ms |
41564 KB |
Output is correct |
17 |
Correct |
7 ms |
41564 KB |
Output is correct |
18 |
Correct |
691 ms |
140772 KB |
Output is correct |
19 |
Correct |
193 ms |
48340 KB |
Output is correct |
20 |
Correct |
156 ms |
47188 KB |
Output is correct |
21 |
Correct |
169 ms |
47448 KB |
Output is correct |
22 |
Correct |
182 ms |
47652 KB |
Output is correct |
23 |
Correct |
197 ms |
48212 KB |
Output is correct |
24 |
Correct |
159 ms |
47456 KB |
Output is correct |
25 |
Correct |
174 ms |
47704 KB |
Output is correct |
26 |
Correct |
183 ms |
47824 KB |
Output is correct |
27 |
Correct |
395 ms |
125500 KB |
Output is correct |
28 |
Correct |
254 ms |
82876 KB |
Output is correct |
29 |
Correct |
370 ms |
115284 KB |
Output is correct |
30 |
Correct |
675 ms |
225980 KB |
Output is correct |
31 |
Correct |
11 ms |
41564 KB |
Output is correct |
32 |
Correct |
555 ms |
121892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
41564 KB |
Output is correct |
2 |
Correct |
11 ms |
44124 KB |
Output is correct |
3 |
Correct |
10 ms |
41680 KB |
Output is correct |
4 |
Correct |
9 ms |
41564 KB |
Output is correct |
5 |
Correct |
11 ms |
44124 KB |
Output is correct |
6 |
Correct |
7 ms |
41564 KB |
Output is correct |
7 |
Correct |
8 ms |
41596 KB |
Output is correct |
8 |
Correct |
7 ms |
41564 KB |
Output is correct |
9 |
Correct |
7 ms |
41820 KB |
Output is correct |
10 |
Correct |
7 ms |
41396 KB |
Output is correct |
11 |
Correct |
11 ms |
41816 KB |
Output is correct |
12 |
Correct |
10 ms |
43868 KB |
Output is correct |
13 |
Correct |
12 ms |
44380 KB |
Output is correct |
14 |
Correct |
13 ms |
44576 KB |
Output is correct |
15 |
Correct |
8 ms |
41616 KB |
Output is correct |
16 |
Correct |
7 ms |
41564 KB |
Output is correct |
17 |
Correct |
7 ms |
41564 KB |
Output is correct |
18 |
Correct |
691 ms |
140772 KB |
Output is correct |
19 |
Correct |
193 ms |
48340 KB |
Output is correct |
20 |
Correct |
156 ms |
47188 KB |
Output is correct |
21 |
Correct |
169 ms |
47448 KB |
Output is correct |
22 |
Correct |
182 ms |
47652 KB |
Output is correct |
23 |
Correct |
197 ms |
48212 KB |
Output is correct |
24 |
Correct |
159 ms |
47456 KB |
Output is correct |
25 |
Correct |
174 ms |
47704 KB |
Output is correct |
26 |
Correct |
183 ms |
47824 KB |
Output is correct |
27 |
Correct |
395 ms |
125500 KB |
Output is correct |
28 |
Correct |
254 ms |
82876 KB |
Output is correct |
29 |
Correct |
370 ms |
115284 KB |
Output is correct |
30 |
Correct |
675 ms |
225980 KB |
Output is correct |
31 |
Correct |
11 ms |
41564 KB |
Output is correct |
32 |
Correct |
555 ms |
121892 KB |
Output is correct |
33 |
Correct |
376 ms |
222116 KB |
Output is correct |
34 |
Correct |
297 ms |
226124 KB |
Output is correct |
35 |
Correct |
300 ms |
224452 KB |
Output is correct |
36 |
Correct |
236 ms |
180800 KB |
Output is correct |
37 |
Correct |
73 ms |
76880 KB |
Output is correct |
38 |
Correct |
136 ms |
111956 KB |
Output is correct |
39 |
Correct |
225 ms |
165712 KB |
Output is correct |
40 |
Correct |
208 ms |
160852 KB |
Output is correct |
41 |
Correct |
72 ms |
81184 KB |
Output is correct |
42 |
Correct |
155 ms |
127116 KB |
Output is correct |
43 |
Correct |
367 ms |
222164 KB |
Output is correct |
44 |
Correct |
300 ms |
226276 KB |
Output is correct |
45 |
Correct |
303 ms |
224664 KB |
Output is correct |
46 |
Correct |
226 ms |
180824 KB |
Output is correct |
47 |
Correct |
62 ms |
70740 KB |
Output is correct |
48 |
Correct |
135 ms |
111952 KB |
Output is correct |
49 |
Correct |
318 ms |
222548 KB |
Output is correct |
50 |
Correct |
295 ms |
194540 KB |
Output is correct |
51 |
Correct |
274 ms |
195524 KB |
Output is correct |
52 |
Correct |
244 ms |
165716 KB |
Output is correct |
53 |
Correct |
213 ms |
161008 KB |
Output is correct |
54 |
Correct |
73 ms |
81236 KB |
Output is correct |
55 |
Correct |
147 ms |
127268 KB |
Output is correct |
56 |
Correct |
396 ms |
222268 KB |
Output is correct |
57 |
Correct |
292 ms |
226376 KB |
Output is correct |
58 |
Correct |
303 ms |
224848 KB |
Output is correct |
59 |
Correct |
231 ms |
180824 KB |
Output is correct |
60 |
Correct |
62 ms |
70880 KB |
Output is correct |
61 |
Correct |
136 ms |
111940 KB |
Output is correct |
62 |
Correct |
315 ms |
222436 KB |
Output is correct |
63 |
Correct |
272 ms |
197452 KB |
Output is correct |
64 |
Correct |
281 ms |
197460 KB |
Output is correct |
65 |
Correct |
497 ms |
130128 KB |
Output is correct |
66 |
Correct |
637 ms |
180728 KB |
Output is correct |
67 |
Correct |
665 ms |
177840 KB |
Output is correct |
68 |
Correct |
497 ms |
144312 KB |
Output is correct |
69 |
Correct |
537 ms |
141436 KB |
Output is correct |
70 |
Correct |
223 ms |
45368 KB |
Output is correct |
71 |
Correct |
679 ms |
179040 KB |
Output is correct |
72 |
Correct |
691 ms |
176868 KB |
Output is correct |
73 |
Correct |
527 ms |
144212 KB |
Output is correct |
74 |
Correct |
320 ms |
175700 KB |
Output is correct |
75 |
Correct |
298 ms |
179156 KB |
Output is correct |
76 |
Correct |
283 ms |
176464 KB |
Output is correct |
77 |
Correct |
259 ms |
144208 KB |
Output is correct |
78 |
Correct |
529 ms |
138904 KB |
Output is correct |
79 |
Correct |
895 ms |
169260 KB |
Output is correct |
80 |
Correct |
865 ms |
164692 KB |
Output is correct |
81 |
Correct |
355 ms |
84820 KB |
Output is correct |
82 |
Correct |
488 ms |
130748 KB |
Output is correct |
83 |
Correct |
867 ms |
226116 KB |
Output is correct |
84 |
Correct |
754 ms |
229856 KB |
Output is correct |
85 |
Correct |
770 ms |
228436 KB |
Output is correct |
86 |
Correct |
630 ms |
184288 KB |
Output is correct |
87 |
Correct |
348 ms |
74348 KB |
Output is correct |
88 |
Correct |
432 ms |
115552 KB |
Output is correct |
89 |
Correct |
600 ms |
226116 KB |
Output is correct |
90 |
Correct |
855 ms |
201016 KB |
Output is correct |
91 |
Correct |
641 ms |
200788 KB |
Output is correct |