#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;
///Land cells are vertices, and two adjacent land cells form an edge
///All faces formed are 2x2 squares, where all 4 cells are land cells
///Except possibly when the entire rectangle surrounds the rivers, then there is 1 more additonal face on the inside
///As the graph is planar, No. of components = V - E + F (We ignore the outer face as a face)
///As the V, E and F are large, we instead use a segment tree to count how many vertices, edges and faces are absent.
const int N = 200002;
int totalRivers;
struct segment{
vector<int> tree[2*N+5];
inline void update(int i, int v){
tree[i+N].push_back(v);
}
inline void getReady(){
for(int i = N;i < 2*N;i++){
sort(tree[i].begin(),tree[i].end());
tree[i].erase(unique(tree[i].begin(),tree[i].end()),tree[i].end());
}
for(int i = N-1;i > 0;i--){ ///basically merge sort
tree[i] = vector<int>(tree[i<<1].size()+tree[i<<1|1].size());
merge(tree[i<<1].begin(),tree[i<<1].end(), tree[i<<1|1].begin(),tree[i<<1|1].end(),tree[i].begin());
}
}
inline int query(int l, int r, int x, int y){
int ans = 0;
for(l += N, r += (N+1);l < r;l >>= 1, r >>= 1){
if(l&1){
ans += upper_bound(tree[l].begin(),tree[l].end(),y) - lower_bound(tree[l].begin(),tree[l].end(),x);
l++;
}
if(r&1){
r--;
ans += upper_bound(tree[r].begin(),tree[r].end(),y) - lower_bound(tree[r].begin(),tree[r].end(),x);
}
}
return ans;
}
} vertices, horizontal, vertical, faces;
///Vertices store vertices which are rivers
///Edges store edges which are not between two land cells, horizontal is (r,c) to (r,c+1), vertical is (r,c) to (r+1,c)
///Squares store which group of 4 sqaures with at least 1 river. It stores the top left corner (r,c)
void addRiver(int r, int c){
vertices.update(r,c);
horizontal.update(r,c-1);
horizontal.update(r,c);
vertical.update(c,r-1);
vertical.update(c,r);
faces.update(r-1,c-1);
faces.update(r,c-1);
faces.update(r-1,c);
faces.update(r,c);
}
void init(int R, int C, int sr, int sc, int M, char *S) {
addRiver(sr,sc);
for(int i = 0;i < M;i++){
if(S[i] == 'N') sr--;
else if(S[i] == 'S') sr++;
else if(S[i] == 'E') sc++;
else sc--;
addRiver(sr,sc);
}
vertices.getReady(); horizontal.getReady(); vertical.getReady(); faces.getReady();
totalRivers = vertices.query(0,N-1,0,N-1);
}
int colour(int T, int L, int B, int R) {
long long H = B - T + 1, W = R - L + 1;
long long V = H * W - vertices.query(T,B,L,R);
long long E = (H-1)*W + (W-1)*H - horizontal.query(T,B,L,R-1) - vertical.query(L,R,T,B-1);
long long F = (H-1)*(W-1) - faces.query(T,B-1,L,R-1);
if(vertices.query(T+1,B-1,L+1,R-1) == totalRivers) F++;
return V - E + F;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
38008 KB |
Output is correct |
2 |
Correct |
44 ms |
38264 KB |
Output is correct |
3 |
Correct |
45 ms |
38136 KB |
Output is correct |
4 |
Correct |
47 ms |
38008 KB |
Output is correct |
5 |
Correct |
49 ms |
38396 KB |
Output is correct |
6 |
Correct |
44 ms |
37880 KB |
Output is correct |
7 |
Correct |
44 ms |
37948 KB |
Output is correct |
8 |
Correct |
44 ms |
37880 KB |
Output is correct |
9 |
Correct |
43 ms |
37928 KB |
Output is correct |
10 |
Correct |
48 ms |
37936 KB |
Output is correct |
11 |
Correct |
48 ms |
38136 KB |
Output is correct |
12 |
Correct |
47 ms |
38264 KB |
Output is correct |
13 |
Correct |
48 ms |
38520 KB |
Output is correct |
14 |
Correct |
50 ms |
38648 KB |
Output is correct |
15 |
Correct |
43 ms |
37984 KB |
Output is correct |
16 |
Correct |
43 ms |
37880 KB |
Output is correct |
17 |
Correct |
42 ms |
37880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
37880 KB |
Output is correct |
2 |
Correct |
42 ms |
37880 KB |
Output is correct |
3 |
Correct |
318 ms |
67108 KB |
Output is correct |
4 |
Correct |
458 ms |
86328 KB |
Output is correct |
5 |
Correct |
476 ms |
85472 KB |
Output is correct |
6 |
Correct |
318 ms |
74456 KB |
Output is correct |
7 |
Correct |
378 ms |
73944 KB |
Output is correct |
8 |
Correct |
170 ms |
43104 KB |
Output is correct |
9 |
Correct |
511 ms |
86556 KB |
Output is correct |
10 |
Correct |
538 ms |
85736 KB |
Output is correct |
11 |
Correct |
411 ms |
74456 KB |
Output is correct |
12 |
Correct |
240 ms |
83168 KB |
Output is correct |
13 |
Correct |
242 ms |
86364 KB |
Output is correct |
14 |
Correct |
248 ms |
85536 KB |
Output is correct |
15 |
Correct |
228 ms |
74492 KB |
Output is correct |
16 |
Correct |
320 ms |
72160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
37984 KB |
Output is correct |
2 |
Correct |
128 ms |
87644 KB |
Output is correct |
3 |
Correct |
191 ms |
95340 KB |
Output is correct |
4 |
Correct |
165 ms |
87744 KB |
Output is correct |
5 |
Correct |
136 ms |
78808 KB |
Output is correct |
6 |
Correct |
95 ms |
50932 KB |
Output is correct |
7 |
Correct |
105 ms |
58652 KB |
Output is correct |
8 |
Correct |
137 ms |
79128 KB |
Output is correct |
9 |
Correct |
124 ms |
74732 KB |
Output is correct |
10 |
Correct |
86 ms |
48412 KB |
Output is correct |
11 |
Correct |
123 ms |
63448 KB |
Output is correct |
12 |
Correct |
130 ms |
87824 KB |
Output is correct |
13 |
Correct |
171 ms |
95428 KB |
Output is correct |
14 |
Correct |
145 ms |
87840 KB |
Output is correct |
15 |
Correct |
135 ms |
78816 KB |
Output is correct |
16 |
Correct |
92 ms |
49216 KB |
Output is correct |
17 |
Correct |
103 ms |
58076 KB |
Output is correct |
18 |
Correct |
122 ms |
82808 KB |
Output is correct |
19 |
Correct |
141 ms |
89304 KB |
Output is correct |
20 |
Correct |
142 ms |
89312 KB |
Output is correct |
21 |
Correct |
127 ms |
78944 KB |
Output is correct |
22 |
Correct |
126 ms |
74728 KB |
Output is correct |
23 |
Correct |
82 ms |
48508 KB |
Output is correct |
24 |
Correct |
116 ms |
63320 KB |
Output is correct |
25 |
Correct |
127 ms |
87628 KB |
Output is correct |
26 |
Correct |
163 ms |
95392 KB |
Output is correct |
27 |
Correct |
143 ms |
87772 KB |
Output is correct |
28 |
Correct |
138 ms |
78880 KB |
Output is correct |
29 |
Correct |
90 ms |
49204 KB |
Output is correct |
30 |
Correct |
102 ms |
58044 KB |
Output is correct |
31 |
Correct |
122 ms |
82936 KB |
Output is correct |
32 |
Correct |
142 ms |
89316 KB |
Output is correct |
33 |
Correct |
143 ms |
89312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
38008 KB |
Output is correct |
2 |
Correct |
44 ms |
38264 KB |
Output is correct |
3 |
Correct |
45 ms |
38136 KB |
Output is correct |
4 |
Correct |
47 ms |
38008 KB |
Output is correct |
5 |
Correct |
49 ms |
38396 KB |
Output is correct |
6 |
Correct |
44 ms |
37880 KB |
Output is correct |
7 |
Correct |
44 ms |
37948 KB |
Output is correct |
8 |
Correct |
44 ms |
37880 KB |
Output is correct |
9 |
Correct |
43 ms |
37928 KB |
Output is correct |
10 |
Correct |
48 ms |
37936 KB |
Output is correct |
11 |
Correct |
48 ms |
38136 KB |
Output is correct |
12 |
Correct |
47 ms |
38264 KB |
Output is correct |
13 |
Correct |
48 ms |
38520 KB |
Output is correct |
14 |
Correct |
50 ms |
38648 KB |
Output is correct |
15 |
Correct |
43 ms |
37984 KB |
Output is correct |
16 |
Correct |
43 ms |
37880 KB |
Output is correct |
17 |
Correct |
42 ms |
37880 KB |
Output is correct |
18 |
Correct |
1162 ms |
63732 KB |
Output is correct |
19 |
Correct |
274 ms |
39692 KB |
Output is correct |
20 |
Correct |
225 ms |
38776 KB |
Output is correct |
21 |
Correct |
242 ms |
39032 KB |
Output is correct |
22 |
Correct |
261 ms |
39284 KB |
Output is correct |
23 |
Correct |
251 ms |
39664 KB |
Output is correct |
24 |
Correct |
306 ms |
39236 KB |
Output is correct |
25 |
Correct |
293 ms |
39208 KB |
Output is correct |
26 |
Correct |
292 ms |
39416 KB |
Output is correct |
27 |
Correct |
501 ms |
60024 KB |
Output is correct |
28 |
Correct |
479 ms |
51516 KB |
Output is correct |
29 |
Correct |
488 ms |
58664 KB |
Output is correct |
30 |
Correct |
841 ms |
83588 KB |
Output is correct |
31 |
Correct |
47 ms |
37904 KB |
Output is correct |
32 |
Correct |
973 ms |
59928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
38008 KB |
Output is correct |
2 |
Correct |
44 ms |
38264 KB |
Output is correct |
3 |
Correct |
45 ms |
38136 KB |
Output is correct |
4 |
Correct |
47 ms |
38008 KB |
Output is correct |
5 |
Correct |
49 ms |
38396 KB |
Output is correct |
6 |
Correct |
44 ms |
37880 KB |
Output is correct |
7 |
Correct |
44 ms |
37948 KB |
Output is correct |
8 |
Correct |
44 ms |
37880 KB |
Output is correct |
9 |
Correct |
43 ms |
37928 KB |
Output is correct |
10 |
Correct |
48 ms |
37936 KB |
Output is correct |
11 |
Correct |
48 ms |
38136 KB |
Output is correct |
12 |
Correct |
47 ms |
38264 KB |
Output is correct |
13 |
Correct |
48 ms |
38520 KB |
Output is correct |
14 |
Correct |
50 ms |
38648 KB |
Output is correct |
15 |
Correct |
43 ms |
37984 KB |
Output is correct |
16 |
Correct |
43 ms |
37880 KB |
Output is correct |
17 |
Correct |
42 ms |
37880 KB |
Output is correct |
18 |
Correct |
1162 ms |
63732 KB |
Output is correct |
19 |
Correct |
274 ms |
39692 KB |
Output is correct |
20 |
Correct |
225 ms |
38776 KB |
Output is correct |
21 |
Correct |
242 ms |
39032 KB |
Output is correct |
22 |
Correct |
261 ms |
39284 KB |
Output is correct |
23 |
Correct |
251 ms |
39664 KB |
Output is correct |
24 |
Correct |
306 ms |
39236 KB |
Output is correct |
25 |
Correct |
293 ms |
39208 KB |
Output is correct |
26 |
Correct |
292 ms |
39416 KB |
Output is correct |
27 |
Correct |
501 ms |
60024 KB |
Output is correct |
28 |
Correct |
479 ms |
51516 KB |
Output is correct |
29 |
Correct |
488 ms |
58664 KB |
Output is correct |
30 |
Correct |
841 ms |
83588 KB |
Output is correct |
31 |
Correct |
47 ms |
37904 KB |
Output is correct |
32 |
Correct |
973 ms |
59928 KB |
Output is correct |
33 |
Correct |
128 ms |
87644 KB |
Output is correct |
34 |
Correct |
191 ms |
95340 KB |
Output is correct |
35 |
Correct |
165 ms |
87744 KB |
Output is correct |
36 |
Correct |
136 ms |
78808 KB |
Output is correct |
37 |
Correct |
95 ms |
50932 KB |
Output is correct |
38 |
Correct |
105 ms |
58652 KB |
Output is correct |
39 |
Correct |
137 ms |
79128 KB |
Output is correct |
40 |
Correct |
124 ms |
74732 KB |
Output is correct |
41 |
Correct |
86 ms |
48412 KB |
Output is correct |
42 |
Correct |
123 ms |
63448 KB |
Output is correct |
43 |
Correct |
130 ms |
87824 KB |
Output is correct |
44 |
Correct |
171 ms |
95428 KB |
Output is correct |
45 |
Correct |
145 ms |
87840 KB |
Output is correct |
46 |
Correct |
135 ms |
78816 KB |
Output is correct |
47 |
Correct |
92 ms |
49216 KB |
Output is correct |
48 |
Correct |
103 ms |
58076 KB |
Output is correct |
49 |
Correct |
122 ms |
82808 KB |
Output is correct |
50 |
Correct |
141 ms |
89304 KB |
Output is correct |
51 |
Correct |
142 ms |
89312 KB |
Output is correct |
52 |
Correct |
127 ms |
78944 KB |
Output is correct |
53 |
Correct |
126 ms |
74728 KB |
Output is correct |
54 |
Correct |
82 ms |
48508 KB |
Output is correct |
55 |
Correct |
116 ms |
63320 KB |
Output is correct |
56 |
Correct |
127 ms |
87628 KB |
Output is correct |
57 |
Correct |
163 ms |
95392 KB |
Output is correct |
58 |
Correct |
143 ms |
87772 KB |
Output is correct |
59 |
Correct |
138 ms |
78880 KB |
Output is correct |
60 |
Correct |
90 ms |
49204 KB |
Output is correct |
61 |
Correct |
102 ms |
58044 KB |
Output is correct |
62 |
Correct |
122 ms |
82936 KB |
Output is correct |
63 |
Correct |
142 ms |
89316 KB |
Output is correct |
64 |
Correct |
143 ms |
89312 KB |
Output is correct |
65 |
Correct |
706 ms |
79644 KB |
Output is correct |
66 |
Correct |
725 ms |
75500 KB |
Output is correct |
67 |
Correct |
491 ms |
49260 KB |
Output is correct |
68 |
Correct |
643 ms |
64016 KB |
Output is correct |
69 |
Correct |
584 ms |
88424 KB |
Output is correct |
70 |
Correct |
894 ms |
96204 KB |
Output is correct |
71 |
Correct |
596 ms |
88560 KB |
Output is correct |
72 |
Correct |
567 ms |
79576 KB |
Output is correct |
73 |
Correct |
338 ms |
49836 KB |
Output is correct |
74 |
Correct |
343 ms |
58784 KB |
Output is correct |
75 |
Correct |
375 ms |
83688 KB |
Output is correct |
76 |
Correct |
740 ms |
90088 KB |
Output is correct |
77 |
Correct |
820 ms |
90072 KB |
Output is correct |
78 |
Correct |
318 ms |
67108 KB |
Output is correct |
79 |
Correct |
458 ms |
86328 KB |
Output is correct |
80 |
Correct |
476 ms |
85472 KB |
Output is correct |
81 |
Correct |
318 ms |
74456 KB |
Output is correct |
82 |
Correct |
378 ms |
73944 KB |
Output is correct |
83 |
Correct |
170 ms |
43104 KB |
Output is correct |
84 |
Correct |
511 ms |
86556 KB |
Output is correct |
85 |
Correct |
538 ms |
85736 KB |
Output is correct |
86 |
Correct |
411 ms |
74456 KB |
Output is correct |
87 |
Correct |
240 ms |
83168 KB |
Output is correct |
88 |
Correct |
242 ms |
86364 KB |
Output is correct |
89 |
Correct |
248 ms |
85536 KB |
Output is correct |
90 |
Correct |
228 ms |
74492 KB |
Output is correct |
91 |
Correct |
320 ms |
72160 KB |
Output is correct |