#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;
///Land cells is a vertex, and two adjacent land cells
///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
///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 |
38136 KB |
Output is correct |
2 |
Correct |
48 ms |
38392 KB |
Output is correct |
3 |
Correct |
46 ms |
38008 KB |
Output is correct |
4 |
Correct |
46 ms |
38008 KB |
Output is correct |
5 |
Correct |
48 ms |
38520 KB |
Output is correct |
6 |
Correct |
44 ms |
37880 KB |
Output is correct |
7 |
Correct |
43 ms |
38008 KB |
Output is correct |
8 |
Correct |
43 ms |
37852 KB |
Output is correct |
9 |
Correct |
43 ms |
37880 KB |
Output is correct |
10 |
Correct |
43 ms |
37880 KB |
Output is correct |
11 |
Correct |
54 ms |
38188 KB |
Output is correct |
12 |
Correct |
48 ms |
38264 KB |
Output is correct |
13 |
Correct |
49 ms |
38520 KB |
Output is correct |
14 |
Correct |
50 ms |
38708 KB |
Output is correct |
15 |
Correct |
43 ms |
37880 KB |
Output is correct |
16 |
Correct |
43 ms |
37880 KB |
Output is correct |
17 |
Correct |
43 ms |
37880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
37880 KB |
Output is correct |
2 |
Correct |
43 ms |
37880 KB |
Output is correct |
3 |
Correct |
318 ms |
69872 KB |
Output is correct |
4 |
Correct |
454 ms |
89152 KB |
Output is correct |
5 |
Correct |
436 ms |
88700 KB |
Output is correct |
6 |
Correct |
318 ms |
77320 KB |
Output is correct |
7 |
Correct |
384 ms |
76932 KB |
Output is correct |
8 |
Correct |
169 ms |
45952 KB |
Output is correct |
9 |
Correct |
490 ms |
89184 KB |
Output is correct |
10 |
Correct |
461 ms |
88400 KB |
Output is correct |
11 |
Correct |
365 ms |
77364 KB |
Output is correct |
12 |
Correct |
215 ms |
85856 KB |
Output is correct |
13 |
Correct |
247 ms |
89164 KB |
Output is correct |
14 |
Correct |
247 ms |
88444 KB |
Output is correct |
15 |
Correct |
230 ms |
77272 KB |
Output is correct |
16 |
Correct |
327 ms |
75232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
37880 KB |
Output is correct |
2 |
Correct |
129 ms |
87904 KB |
Output is correct |
3 |
Correct |
163 ms |
95436 KB |
Output is correct |
4 |
Correct |
148 ms |
87928 KB |
Output is correct |
5 |
Correct |
137 ms |
78944 KB |
Output is correct |
6 |
Correct |
87 ms |
51064 KB |
Output is correct |
7 |
Correct |
104 ms |
58748 KB |
Output is correct |
8 |
Correct |
127 ms |
79072 KB |
Output is correct |
9 |
Correct |
117 ms |
74988 KB |
Output is correct |
10 |
Correct |
80 ms |
48504 KB |
Output is correct |
11 |
Correct |
134 ms |
63560 KB |
Output is correct |
12 |
Correct |
129 ms |
87760 KB |
Output is correct |
13 |
Correct |
193 ms |
95344 KB |
Output is correct |
14 |
Correct |
154 ms |
88056 KB |
Output is correct |
15 |
Correct |
146 ms |
78944 KB |
Output is correct |
16 |
Correct |
90 ms |
49272 KB |
Output is correct |
17 |
Correct |
103 ms |
58104 KB |
Output is correct |
18 |
Correct |
123 ms |
82936 KB |
Output is correct |
19 |
Correct |
143 ms |
89568 KB |
Output is correct |
20 |
Correct |
143 ms |
89432 KB |
Output is correct |
21 |
Correct |
128 ms |
79116 KB |
Output is correct |
22 |
Correct |
126 ms |
74860 KB |
Output is correct |
23 |
Correct |
77 ms |
48476 KB |
Output is correct |
24 |
Correct |
134 ms |
63448 KB |
Output is correct |
25 |
Correct |
128 ms |
87752 KB |
Output is correct |
26 |
Correct |
174 ms |
95468 KB |
Output is correct |
27 |
Correct |
145 ms |
87904 KB |
Output is correct |
28 |
Correct |
138 ms |
78944 KB |
Output is correct |
29 |
Correct |
98 ms |
49236 KB |
Output is correct |
30 |
Correct |
112 ms |
58232 KB |
Output is correct |
31 |
Correct |
120 ms |
82920 KB |
Output is correct |
32 |
Correct |
142 ms |
89352 KB |
Output is correct |
33 |
Correct |
144 ms |
89340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
38136 KB |
Output is correct |
2 |
Correct |
48 ms |
38392 KB |
Output is correct |
3 |
Correct |
46 ms |
38008 KB |
Output is correct |
4 |
Correct |
46 ms |
38008 KB |
Output is correct |
5 |
Correct |
48 ms |
38520 KB |
Output is correct |
6 |
Correct |
44 ms |
37880 KB |
Output is correct |
7 |
Correct |
43 ms |
38008 KB |
Output is correct |
8 |
Correct |
43 ms |
37852 KB |
Output is correct |
9 |
Correct |
43 ms |
37880 KB |
Output is correct |
10 |
Correct |
43 ms |
37880 KB |
Output is correct |
11 |
Correct |
54 ms |
38188 KB |
Output is correct |
12 |
Correct |
48 ms |
38264 KB |
Output is correct |
13 |
Correct |
49 ms |
38520 KB |
Output is correct |
14 |
Correct |
50 ms |
38708 KB |
Output is correct |
15 |
Correct |
43 ms |
37880 KB |
Output is correct |
16 |
Correct |
43 ms |
37880 KB |
Output is correct |
17 |
Correct |
43 ms |
37880 KB |
Output is correct |
18 |
Correct |
1134 ms |
66584 KB |
Output is correct |
19 |
Correct |
279 ms |
42400 KB |
Output is correct |
20 |
Correct |
228 ms |
41568 KB |
Output is correct |
21 |
Correct |
245 ms |
41884 KB |
Output is correct |
22 |
Correct |
266 ms |
42000 KB |
Output is correct |
23 |
Correct |
253 ms |
42288 KB |
Output is correct |
24 |
Correct |
313 ms |
41720 KB |
Output is correct |
25 |
Correct |
306 ms |
42020 KB |
Output is correct |
26 |
Correct |
320 ms |
42236 KB |
Output is correct |
27 |
Correct |
518 ms |
62840 KB |
Output is correct |
28 |
Correct |
482 ms |
54380 KB |
Output is correct |
29 |
Correct |
529 ms |
61728 KB |
Output is correct |
30 |
Correct |
836 ms |
86648 KB |
Output is correct |
31 |
Correct |
47 ms |
38020 KB |
Output is correct |
32 |
Correct |
963 ms |
62812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
38136 KB |
Output is correct |
2 |
Correct |
48 ms |
38392 KB |
Output is correct |
3 |
Correct |
46 ms |
38008 KB |
Output is correct |
4 |
Correct |
46 ms |
38008 KB |
Output is correct |
5 |
Correct |
48 ms |
38520 KB |
Output is correct |
6 |
Correct |
44 ms |
37880 KB |
Output is correct |
7 |
Correct |
43 ms |
38008 KB |
Output is correct |
8 |
Correct |
43 ms |
37852 KB |
Output is correct |
9 |
Correct |
43 ms |
37880 KB |
Output is correct |
10 |
Correct |
43 ms |
37880 KB |
Output is correct |
11 |
Correct |
54 ms |
38188 KB |
Output is correct |
12 |
Correct |
48 ms |
38264 KB |
Output is correct |
13 |
Correct |
49 ms |
38520 KB |
Output is correct |
14 |
Correct |
50 ms |
38708 KB |
Output is correct |
15 |
Correct |
43 ms |
37880 KB |
Output is correct |
16 |
Correct |
43 ms |
37880 KB |
Output is correct |
17 |
Correct |
43 ms |
37880 KB |
Output is correct |
18 |
Correct |
1134 ms |
66584 KB |
Output is correct |
19 |
Correct |
279 ms |
42400 KB |
Output is correct |
20 |
Correct |
228 ms |
41568 KB |
Output is correct |
21 |
Correct |
245 ms |
41884 KB |
Output is correct |
22 |
Correct |
266 ms |
42000 KB |
Output is correct |
23 |
Correct |
253 ms |
42288 KB |
Output is correct |
24 |
Correct |
313 ms |
41720 KB |
Output is correct |
25 |
Correct |
306 ms |
42020 KB |
Output is correct |
26 |
Correct |
320 ms |
42236 KB |
Output is correct |
27 |
Correct |
518 ms |
62840 KB |
Output is correct |
28 |
Correct |
482 ms |
54380 KB |
Output is correct |
29 |
Correct |
529 ms |
61728 KB |
Output is correct |
30 |
Correct |
836 ms |
86648 KB |
Output is correct |
31 |
Correct |
47 ms |
38020 KB |
Output is correct |
32 |
Correct |
963 ms |
62812 KB |
Output is correct |
33 |
Correct |
129 ms |
87904 KB |
Output is correct |
34 |
Correct |
163 ms |
95436 KB |
Output is correct |
35 |
Correct |
148 ms |
87928 KB |
Output is correct |
36 |
Correct |
137 ms |
78944 KB |
Output is correct |
37 |
Correct |
87 ms |
51064 KB |
Output is correct |
38 |
Correct |
104 ms |
58748 KB |
Output is correct |
39 |
Correct |
127 ms |
79072 KB |
Output is correct |
40 |
Correct |
117 ms |
74988 KB |
Output is correct |
41 |
Correct |
80 ms |
48504 KB |
Output is correct |
42 |
Correct |
134 ms |
63560 KB |
Output is correct |
43 |
Correct |
129 ms |
87760 KB |
Output is correct |
44 |
Correct |
193 ms |
95344 KB |
Output is correct |
45 |
Correct |
154 ms |
88056 KB |
Output is correct |
46 |
Correct |
146 ms |
78944 KB |
Output is correct |
47 |
Correct |
90 ms |
49272 KB |
Output is correct |
48 |
Correct |
103 ms |
58104 KB |
Output is correct |
49 |
Correct |
123 ms |
82936 KB |
Output is correct |
50 |
Correct |
143 ms |
89568 KB |
Output is correct |
51 |
Correct |
143 ms |
89432 KB |
Output is correct |
52 |
Correct |
128 ms |
79116 KB |
Output is correct |
53 |
Correct |
126 ms |
74860 KB |
Output is correct |
54 |
Correct |
77 ms |
48476 KB |
Output is correct |
55 |
Correct |
134 ms |
63448 KB |
Output is correct |
56 |
Correct |
128 ms |
87752 KB |
Output is correct |
57 |
Correct |
174 ms |
95468 KB |
Output is correct |
58 |
Correct |
145 ms |
87904 KB |
Output is correct |
59 |
Correct |
138 ms |
78944 KB |
Output is correct |
60 |
Correct |
98 ms |
49236 KB |
Output is correct |
61 |
Correct |
112 ms |
58232 KB |
Output is correct |
62 |
Correct |
120 ms |
82920 KB |
Output is correct |
63 |
Correct |
142 ms |
89352 KB |
Output is correct |
64 |
Correct |
144 ms |
89340 KB |
Output is correct |
65 |
Correct |
689 ms |
82688 KB |
Output is correct |
66 |
Correct |
792 ms |
78392 KB |
Output is correct |
67 |
Correct |
493 ms |
52096 KB |
Output is correct |
68 |
Correct |
659 ms |
66884 KB |
Output is correct |
69 |
Correct |
524 ms |
91340 KB |
Output is correct |
70 |
Correct |
818 ms |
99020 KB |
Output is correct |
71 |
Correct |
542 ms |
91404 KB |
Output is correct |
72 |
Correct |
603 ms |
82788 KB |
Output is correct |
73 |
Correct |
339 ms |
52776 KB |
Output is correct |
74 |
Correct |
344 ms |
61872 KB |
Output is correct |
75 |
Correct |
351 ms |
86448 KB |
Output is correct |
76 |
Correct |
716 ms |
92980 KB |
Output is correct |
77 |
Correct |
834 ms |
92960 KB |
Output is correct |
78 |
Correct |
318 ms |
69872 KB |
Output is correct |
79 |
Correct |
454 ms |
89152 KB |
Output is correct |
80 |
Correct |
436 ms |
88700 KB |
Output is correct |
81 |
Correct |
318 ms |
77320 KB |
Output is correct |
82 |
Correct |
384 ms |
76932 KB |
Output is correct |
83 |
Correct |
169 ms |
45952 KB |
Output is correct |
84 |
Correct |
490 ms |
89184 KB |
Output is correct |
85 |
Correct |
461 ms |
88400 KB |
Output is correct |
86 |
Correct |
365 ms |
77364 KB |
Output is correct |
87 |
Correct |
215 ms |
85856 KB |
Output is correct |
88 |
Correct |
247 ms |
89164 KB |
Output is correct |
89 |
Correct |
247 ms |
88444 KB |
Output is correct |
90 |
Correct |
230 ms |
77272 KB |
Output is correct |
91 |
Correct |
327 ms |
75232 KB |
Output is correct |