#include "rainbow.h"
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
int ans[1<<17];
struct seg {
struct node {
int sum = 0;
node *l = 0, *r = 0;
};
node *rt;
int n;
seg(int N = 0) : rt(new node()), n(N) {}
void update(node*& v, int l, int r, int p) {
if(p < l || r < p) return;
if(!v) v = new node();
v->sum++;
if(l == r) return;
int mid = (l+r)/2;
update(v->l, l, mid, p);
update(v->r, mid+1, r, p);
}
int query(node *v, int l, int r, int ql, int qr) {
if(!v || r < ql || qr < l) return 0;
if(ql <= l && r <= qr) return v->sum;
int mid = (l+r)/2;
return query(v->l, l, mid, ql, qr) + query(v->r, mid+1, r, ql, qr);
}
void update(int p) { update(rt, 1, n, p); }
int query(int ql, int qr) { return query(rt, 1, n, ql, qr); }
};
struct rect_query_online {
vector<seg> st;
int n;
rect_query_online(int n = 0, int m = 0) : n(n), st(n+1) {
for(int i = 1; i <= n; i++) st[i] = seg(m);
}
void add_point(int x, int y) {
for(; x <= n; x += x&-x) st[x].update(y);
}
int query(int x, int yl, int yr) {
int ans = 0;
//cout << x << " " << yl << " " << yr << ": " << endl;
for(; x; x -= x&-x) ans += st[x].query(yl, yr);
//cout << ans << endl;
return ans;
}
int query(int xl, int xr, int yl, int yr) {
if(xl > xr || yl > yr) return 0;
return query(xr, yl, yr) - query(xl-1, yl, yr);
}
};
using pt = array<int, 2>;
int n, m;//segments are identified by their top left point
map<pt, int> points, segx, segy;
set<pt> marked;
void add_block(int x, int y) {
if(!marked.insert({x, y}).second) return;
points[{x, y}]++;
points[{x+1, y}]++;
points[{x, y+1}]++;
points[{x+1, y+1}]++;
segx[{x, y}]++;
segx[{x, y+1}]++;
segy[{x, y}]++;
segy[{x+1, y}]++;
}
rect_query_online rp, rb, rx, ry;
int minx = 1<<30, miny = 1<<30, maxy = -1, maxx = -1;
void init(int R, int C, int sr, int sc, int M, char *S) {
n = R+1, m = C+1;
add_block(sr, sc);
minx = maxx = sr, miny = maxy = sc;
for(int i = 0; i < M; i++) {
if(S[i] == 'N') sr--;
if(S[i] == 'S') sr++;
if(S[i] == 'W') sc--;
if(S[i] == 'E') sc++;
add_block(sr, sc);
minx = min(minx, sr);
maxx = max(maxx, sr);
miny = min(miny, sc);
maxy = max(maxy, sc);
}
rp = rect_query_online(n, m);
rx = rect_query_online(n, m);
ry = rect_query_online(n, m);
rb = rect_query_online(n, m);
for(auto i : points) {
rp.add_point(i.first[0], i.first[1]);
}
for(auto i : segx) {
rx.add_point(i.first[0], i.first[1]);
}
for(auto i : segy) {
ry.add_point(i.first[0], i.first[1]);
}
for(auto i : marked) {
rb.add_point(i[0], i[1]);
}
}
int colour(int xl, int yl, int xr, int yr) {
xr++, yr++;
//cout << xl << " " << xr << " " << yl << " " << yr << endl;
int v, e, b;
v = e = 2*(xr+yr-xl-yl);
//cout << v << endl;
v += rp.query(xl+1, xr-1, yl+1, yr-1);
e += rx.query(xl, xr-1, yl+1, yr-1) + ry.query(xl+1, xr-1, yl, yr-1);
b = rb.query(xl, xr-1, yl, yr-1)+1;
int f = e-v+2-b;
f += xl < minx && maxx < xr-1 && yl < miny && maxy < yr-1;
return f;
}
Compilation message
rainbow.cpp: In constructor 'rect_query_online::rect_query_online(int, int)':
rainbow.cpp:37:6: warning: 'rect_query_online::n' will be initialized after [-Wreorder]
37 | int n;
| ^
rainbow.cpp:36:14: warning: 'std::vector<seg> rect_query_online::st' [-Wreorder]
36 | vector<seg> st;
| ^~
rainbow.cpp:38:2: warning: when initialized here [-Wreorder]
38 | rect_query_online(int n = 0, int m = 0) : n(n), st(n+1) {
| ^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
9 ms |
1152 KB |
Output is correct |
3 |
Correct |
4 ms |
620 KB |
Output is correct |
4 |
Correct |
4 ms |
620 KB |
Output is correct |
5 |
Correct |
7 ms |
1132 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
4 ms |
748 KB |
Output is correct |
12 |
Correct |
5 ms |
876 KB |
Output is correct |
13 |
Correct |
8 ms |
1388 KB |
Output is correct |
14 |
Correct |
9 ms |
1644 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
483 ms |
48336 KB |
Output is correct |
4 |
Correct |
797 ms |
81516 KB |
Output is correct |
5 |
Correct |
742 ms |
83948 KB |
Output is correct |
6 |
Correct |
599 ms |
61548 KB |
Output is correct |
7 |
Correct |
655 ms |
61548 KB |
Output is correct |
8 |
Correct |
91 ms |
4076 KB |
Output is correct |
9 |
Correct |
765 ms |
84588 KB |
Output is correct |
10 |
Correct |
844 ms |
84076 KB |
Output is correct |
11 |
Correct |
646 ms |
61560 KB |
Output is correct |
12 |
Correct |
492 ms |
76908 KB |
Output is correct |
13 |
Correct |
518 ms |
84460 KB |
Output is correct |
14 |
Correct |
545 ms |
84076 KB |
Output is correct |
15 |
Correct |
485 ms |
61736 KB |
Output is correct |
16 |
Correct |
528 ms |
61948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1510 ms |
362240 KB |
Output is correct |
3 |
Correct |
1277 ms |
330988 KB |
Output is correct |
4 |
Correct |
1467 ms |
352104 KB |
Output is correct |
5 |
Correct |
1049 ms |
274924 KB |
Output is correct |
6 |
Correct |
301 ms |
79724 KB |
Output is correct |
7 |
Correct |
445 ms |
93804 KB |
Output is correct |
8 |
Correct |
507 ms |
81900 KB |
Output is correct |
9 |
Correct |
448 ms |
79896 KB |
Output is correct |
10 |
Correct |
150 ms |
47596 KB |
Output is correct |
11 |
Correct |
358 ms |
91500 KB |
Output is correct |
12 |
Correct |
1517 ms |
362432 KB |
Output is correct |
13 |
Correct |
1272 ms |
330932 KB |
Output is correct |
14 |
Correct |
1540 ms |
352392 KB |
Output is correct |
15 |
Correct |
1043 ms |
275052 KB |
Output is correct |
16 |
Correct |
255 ms |
76524 KB |
Output is correct |
17 |
Correct |
523 ms |
94664 KB |
Output is correct |
18 |
Correct |
1467 ms |
145420 KB |
Output is correct |
19 |
Correct |
1355 ms |
334052 KB |
Output is correct |
20 |
Correct |
1494 ms |
371436 KB |
Output is correct |
21 |
Correct |
508 ms |
81900 KB |
Output is correct |
22 |
Correct |
437 ms |
79724 KB |
Output is correct |
23 |
Correct |
177 ms |
47664 KB |
Output is correct |
24 |
Correct |
346 ms |
91384 KB |
Output is correct |
25 |
Correct |
1463 ms |
362460 KB |
Output is correct |
26 |
Correct |
1312 ms |
331012 KB |
Output is correct |
27 |
Correct |
1449 ms |
352384 KB |
Output is correct |
28 |
Correct |
1060 ms |
274924 KB |
Output is correct |
29 |
Correct |
256 ms |
76396 KB |
Output is correct |
30 |
Correct |
527 ms |
94572 KB |
Output is correct |
31 |
Correct |
1454 ms |
145348 KB |
Output is correct |
32 |
Correct |
1303 ms |
334044 KB |
Output is correct |
33 |
Correct |
1565 ms |
371536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
9 ms |
1152 KB |
Output is correct |
3 |
Correct |
4 ms |
620 KB |
Output is correct |
4 |
Correct |
4 ms |
620 KB |
Output is correct |
5 |
Correct |
7 ms |
1132 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
4 ms |
748 KB |
Output is correct |
12 |
Correct |
5 ms |
876 KB |
Output is correct |
13 |
Correct |
8 ms |
1388 KB |
Output is correct |
14 |
Correct |
9 ms |
1644 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
2310 ms |
65236 KB |
Output is correct |
19 |
Correct |
390 ms |
7148 KB |
Output is correct |
20 |
Correct |
280 ms |
4400 KB |
Output is correct |
21 |
Correct |
331 ms |
5100 KB |
Output is correct |
22 |
Correct |
376 ms |
5868 KB |
Output is correct |
23 |
Correct |
376 ms |
7000 KB |
Output is correct |
24 |
Correct |
306 ms |
4716 KB |
Output is correct |
25 |
Correct |
339 ms |
5228 KB |
Output is correct |
26 |
Correct |
383 ms |
6124 KB |
Output is correct |
27 |
Correct |
677 ms |
37612 KB |
Output is correct |
28 |
Correct |
422 ms |
21100 KB |
Output is correct |
29 |
Correct |
616 ms |
33900 KB |
Output is correct |
30 |
Correct |
1349 ms |
84972 KB |
Output is correct |
31 |
Correct |
4 ms |
492 KB |
Output is correct |
32 |
Correct |
1362 ms |
35704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
9 ms |
1152 KB |
Output is correct |
3 |
Correct |
4 ms |
620 KB |
Output is correct |
4 |
Correct |
4 ms |
620 KB |
Output is correct |
5 |
Correct |
7 ms |
1132 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
4 ms |
748 KB |
Output is correct |
12 |
Correct |
5 ms |
876 KB |
Output is correct |
13 |
Correct |
8 ms |
1388 KB |
Output is correct |
14 |
Correct |
9 ms |
1644 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
2310 ms |
65236 KB |
Output is correct |
19 |
Correct |
390 ms |
7148 KB |
Output is correct |
20 |
Correct |
280 ms |
4400 KB |
Output is correct |
21 |
Correct |
331 ms |
5100 KB |
Output is correct |
22 |
Correct |
376 ms |
5868 KB |
Output is correct |
23 |
Correct |
376 ms |
7000 KB |
Output is correct |
24 |
Correct |
306 ms |
4716 KB |
Output is correct |
25 |
Correct |
339 ms |
5228 KB |
Output is correct |
26 |
Correct |
383 ms |
6124 KB |
Output is correct |
27 |
Correct |
677 ms |
37612 KB |
Output is correct |
28 |
Correct |
422 ms |
21100 KB |
Output is correct |
29 |
Correct |
616 ms |
33900 KB |
Output is correct |
30 |
Correct |
1349 ms |
84972 KB |
Output is correct |
31 |
Correct |
4 ms |
492 KB |
Output is correct |
32 |
Correct |
1362 ms |
35704 KB |
Output is correct |
33 |
Correct |
1510 ms |
362240 KB |
Output is correct |
34 |
Correct |
1277 ms |
330988 KB |
Output is correct |
35 |
Correct |
1467 ms |
352104 KB |
Output is correct |
36 |
Correct |
1049 ms |
274924 KB |
Output is correct |
37 |
Correct |
301 ms |
79724 KB |
Output is correct |
38 |
Correct |
445 ms |
93804 KB |
Output is correct |
39 |
Correct |
507 ms |
81900 KB |
Output is correct |
40 |
Correct |
448 ms |
79896 KB |
Output is correct |
41 |
Correct |
150 ms |
47596 KB |
Output is correct |
42 |
Correct |
358 ms |
91500 KB |
Output is correct |
43 |
Correct |
1517 ms |
362432 KB |
Output is correct |
44 |
Correct |
1272 ms |
330932 KB |
Output is correct |
45 |
Correct |
1540 ms |
352392 KB |
Output is correct |
46 |
Correct |
1043 ms |
275052 KB |
Output is correct |
47 |
Correct |
255 ms |
76524 KB |
Output is correct |
48 |
Correct |
523 ms |
94664 KB |
Output is correct |
49 |
Correct |
1467 ms |
145420 KB |
Output is correct |
50 |
Correct |
1355 ms |
334052 KB |
Output is correct |
51 |
Correct |
1494 ms |
371436 KB |
Output is correct |
52 |
Correct |
508 ms |
81900 KB |
Output is correct |
53 |
Correct |
437 ms |
79724 KB |
Output is correct |
54 |
Correct |
177 ms |
47664 KB |
Output is correct |
55 |
Correct |
346 ms |
91384 KB |
Output is correct |
56 |
Correct |
1463 ms |
362460 KB |
Output is correct |
57 |
Correct |
1312 ms |
331012 KB |
Output is correct |
58 |
Correct |
1449 ms |
352384 KB |
Output is correct |
59 |
Correct |
1060 ms |
274924 KB |
Output is correct |
60 |
Correct |
256 ms |
76396 KB |
Output is correct |
61 |
Correct |
527 ms |
94572 KB |
Output is correct |
62 |
Correct |
1454 ms |
145348 KB |
Output is correct |
63 |
Correct |
1303 ms |
334044 KB |
Output is correct |
64 |
Correct |
1565 ms |
371536 KB |
Output is correct |
65 |
Correct |
483 ms |
48336 KB |
Output is correct |
66 |
Correct |
797 ms |
81516 KB |
Output is correct |
67 |
Correct |
742 ms |
83948 KB |
Output is correct |
68 |
Correct |
599 ms |
61548 KB |
Output is correct |
69 |
Correct |
655 ms |
61548 KB |
Output is correct |
70 |
Correct |
91 ms |
4076 KB |
Output is correct |
71 |
Correct |
765 ms |
84588 KB |
Output is correct |
72 |
Correct |
844 ms |
84076 KB |
Output is correct |
73 |
Correct |
646 ms |
61560 KB |
Output is correct |
74 |
Correct |
492 ms |
76908 KB |
Output is correct |
75 |
Correct |
518 ms |
84460 KB |
Output is correct |
76 |
Correct |
545 ms |
84076 KB |
Output is correct |
77 |
Correct |
485 ms |
61736 KB |
Output is correct |
78 |
Correct |
528 ms |
61948 KB |
Output is correct |
79 |
Correct |
1407 ms |
85484 KB |
Output is correct |
80 |
Correct |
1710 ms |
83436 KB |
Output is correct |
81 |
Correct |
774 ms |
51180 KB |
Output is correct |
82 |
Correct |
1047 ms |
94956 KB |
Output is correct |
83 |
Correct |
2428 ms |
366040 KB |
Output is correct |
84 |
Correct |
2125 ms |
334424 KB |
Output is correct |
85 |
Correct |
2312 ms |
355692 KB |
Output is correct |
86 |
Correct |
1827 ms |
278608 KB |
Output is correct |
87 |
Correct |
878 ms |
79980 KB |
Output is correct |
88 |
Correct |
1144 ms |
98168 KB |
Output is correct |
89 |
Correct |
2048 ms |
149044 KB |
Output is correct |
90 |
Correct |
2393 ms |
337660 KB |
Output is correct |
91 |
Correct |
2397 ms |
374796 KB |
Output is correct |