#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define taskname "Rainbow"
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 2e5 + 1;
struct segmentTree{
vector <int> d[N * 4];
void add(int id, int l, int r, int x, int y){
if (x < l || r < x)
return;
d[id].push_back(y);
if (l == r)
return;
int mid = (l + r) >> 1;
add(id << 1, l, mid, x, y);
add(id << 1 | 1, mid + 1, r, x, y);
}
void normalize(int id, int l, int r){
if (d[id].empty())
return;
sort(d[id].begin(), d[id].end());
if (l == r)
return;
int mid = (l + r) >> 1;
normalize(id << 1, l, mid);
normalize(id << 1 | 1, mid + 1, r);
}
int get(int id, int l, int r, int x1, int y1, int x2, int y2){
if (x2 < l || r < x1 || x1 > x2 || y1 > y2)
return 0;
if (d[id].empty())
return 0;
if (x1 <= l && r <= x2){
return upper_bound(d[id].begin(), d[id].end(), y2) -
lower_bound(d[id].begin(), d[id].end(), y1);
}
int mid = (l + r) >> 1;
return get(id << 1, l, mid, x1, y1, x2, y2) +
get(id << 1 | 1, mid + 1, r, x1, y1, x2, y2);
}
} cntV, cntErow, cntEcol, cntEcrs, cntF;
int r, c;
string s;
set <ii> river;
map<int, bool> isRiver[N];
void init(int R, int C, int sr, int sc, int M, char *S) {
r = R;
c = C;
isRiver[sr][sc] = 1;
river.insert(mp(sr, sc));
for(int i = 0; i < M; i++){
char ch = S[i];
if (ch == 'N')
sr--;
if (ch == 'S')
sr++;
if (ch == 'W')
sc--;
if (ch == 'E')
sc++;
isRiver[sr][sc] = 1;
river.insert(mp(sr, sc));
}
for(ii p : river){
int x = p.X;
int y = p.Y;
cntV.add(1, 1, r, x, y);
if (isRiver[x + 1][y])
cntErow.add(1, 1, r, x, y);
if (isRiver[x][y + 1])
cntEcol.add(1, 1, r, x, y);
if (isRiver[x + 1][y + 1] && !isRiver[x][y + 1] && !isRiver[x + 1][y])
cntEcrs.add(1, 1, r, x, y);
if (isRiver[x + 1][y - 1] && !isRiver[x][y - 1] && !isRiver[x + 1][y])
cntEcrs.add(1, 1, r, x, y - 1);
if (isRiver[x + 1][y] && isRiver[x][y + 1] && isRiver[x + 1][y + 1])
cntF.add(1, 1, r, x, y);
}
cntV.normalize(1, 1, r);
cntErow.normalize(1, 1, r);
cntEcol.normalize(1, 1, r);
cntEcrs.normalize(1, 1, r);
cntF.normalize(1, 1, r);
}
int colour(int ar, int ac, int br, int bc){
if (cntV.get(1, 1, r, ar, ac, br, bc) == 0)
return 1;
if (cntV.get(1, 1, r, ar, ac, br, bc) == (ll) (br - ar + 1) * (bc - ac + 1))
return 0;
int v = 2 * (br - ar + bc - ac + 4) + cntV.get(1, 1, r, ar, ac, br, bc);
int e = 2 * (br - ar + bc - ac + 4) +
cntErow.get(1, 1, r, ar, ac, br - 1, bc) +
cntEcol.get(1, 1, r, ar, ac, br, bc - 1) +
cntEcrs.get(1, 1, r, ar, ac, br - 1, bc - 1);
int f = cntF.get(1, 1, r, ar, ac, br - 1, bc - 1) + 1;
int c = 1;
if (cntV.get(1, 1, r, ar, ac, br, bc) - cntV.get(1, 1, r, ar + 1, ac + 1, br - 1, bc - 1) == 0
&& cntV.get(1, 1, r, ar + 1, ac + 1, br - 1, bc - 1) > 0)
c++;
if (ar == br && ac == bc){
if (isRiver[ar][ac]){
e += 4;
f += 4;
}
} else if (ar == br){
if (isRiver[ar][ac]){
e += 3;
f += 2;
}
if (isRiver[ar][bc]){
e += 3;
f += 2;
}
e += cntV.get(1, 1, r, ar, ac + 1, br, bc - 1) * 2;
f += 2 * cntEcol.get(1, 1, r, ar, ac, br, bc - 1);
} else if (ac == bc){
if (isRiver[ar][ac]){
e += 3;
f += 2;
}
if (isRiver[br][ac]){
e += 3;
f += 2;
}
e += cntV.get(1, 1, r, ar + 1, ac, br - 1, bc) * 2;
f += 2 * cntErow.get(1, 1, r, ar, ac, br - 1, bc);
} else {
if (isRiver[ar][ac]){
e += 2;
f += 1;
}
if (isRiver[ar][bc]){
e += 2;
f += 1;
}
if (isRiver[br][ac]){
e += 2;
f += 1;
}
if (isRiver[br][bc]){
e += 2;
f += 1;
}
e += cntV.get(1, 1, r, ar, ac + 1, ar, bc - 1) + cntV.get(1, 1, r, br, ac + 1, br, bc - 1) +
cntV.get(1, 1, r, ar + 1, ac, br - 1, ac) + cntV.get(1, 1, r, ar + 1, bc, br - 1, bc);
f += cntEcol.get(1, 1, r, ar, ac, ar, bc - 1) + cntEcol.get(1, 1, r, br, ac, br, bc - 1) +
cntErow.get(1, 1, r, ar, ac, br - 1, ac) + cntErow.get(1, 1, r, ar, bc, br - 1, bc);
}
return e + c - v + 1 - f;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
103800 KB |
Output is correct |
2 |
Correct |
76 ms |
103960 KB |
Output is correct |
3 |
Correct |
72 ms |
103800 KB |
Output is correct |
4 |
Correct |
72 ms |
103756 KB |
Output is correct |
5 |
Correct |
77 ms |
104060 KB |
Output is correct |
6 |
Correct |
69 ms |
103676 KB |
Output is correct |
7 |
Correct |
70 ms |
103700 KB |
Output is correct |
8 |
Correct |
69 ms |
103652 KB |
Output is correct |
9 |
Correct |
69 ms |
103800 KB |
Output is correct |
10 |
Correct |
70 ms |
103672 KB |
Output is correct |
11 |
Correct |
74 ms |
103928 KB |
Output is correct |
12 |
Correct |
76 ms |
104056 KB |
Output is correct |
13 |
Correct |
76 ms |
103928 KB |
Output is correct |
14 |
Correct |
78 ms |
104088 KB |
Output is correct |
15 |
Correct |
69 ms |
103672 KB |
Output is correct |
16 |
Correct |
70 ms |
103672 KB |
Output is correct |
17 |
Correct |
69 ms |
103672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
103672 KB |
Output is correct |
2 |
Correct |
69 ms |
103672 KB |
Output is correct |
3 |
Correct |
428 ms |
121328 KB |
Output is correct |
4 |
Correct |
621 ms |
130652 KB |
Output is correct |
5 |
Correct |
632 ms |
130016 KB |
Output is correct |
6 |
Correct |
444 ms |
123104 KB |
Output is correct |
7 |
Correct |
500 ms |
121704 KB |
Output is correct |
8 |
Correct |
216 ms |
113144 KB |
Output is correct |
9 |
Correct |
604 ms |
127972 KB |
Output is correct |
10 |
Correct |
605 ms |
127840 KB |
Output is correct |
11 |
Correct |
510 ms |
123532 KB |
Output is correct |
12 |
Correct |
288 ms |
121956 KB |
Output is correct |
13 |
Correct |
270 ms |
123060 KB |
Output is correct |
14 |
Correct |
284 ms |
123232 KB |
Output is correct |
15 |
Correct |
285 ms |
119904 KB |
Output is correct |
16 |
Correct |
431 ms |
123740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
103672 KB |
Output is correct |
2 |
Correct |
315 ms |
132404 KB |
Output is correct |
3 |
Correct |
350 ms |
149592 KB |
Output is correct |
4 |
Correct |
353 ms |
144612 KB |
Output is correct |
5 |
Correct |
267 ms |
134364 KB |
Output is correct |
6 |
Correct |
189 ms |
114756 KB |
Output is correct |
7 |
Correct |
303 ms |
124440 KB |
Output is correct |
8 |
Correct |
190 ms |
119396 KB |
Output is correct |
9 |
Correct |
191 ms |
118756 KB |
Output is correct |
10 |
Correct |
178 ms |
113016 KB |
Output is correct |
11 |
Correct |
234 ms |
120280 KB |
Output is correct |
12 |
Correct |
315 ms |
132404 KB |
Output is correct |
13 |
Correct |
350 ms |
149472 KB |
Output is correct |
14 |
Correct |
349 ms |
144480 KB |
Output is correct |
15 |
Correct |
265 ms |
134376 KB |
Output is correct |
16 |
Correct |
172 ms |
112760 KB |
Output is correct |
17 |
Correct |
297 ms |
124988 KB |
Output is correct |
18 |
Correct |
341 ms |
135332 KB |
Output is correct |
19 |
Correct |
321 ms |
141528 KB |
Output is correct |
20 |
Correct |
325 ms |
141528 KB |
Output is correct |
21 |
Correct |
189 ms |
119484 KB |
Output is correct |
22 |
Correct |
193 ms |
118900 KB |
Output is correct |
23 |
Correct |
177 ms |
113144 KB |
Output is correct |
24 |
Correct |
232 ms |
120280 KB |
Output is correct |
25 |
Correct |
315 ms |
132524 KB |
Output is correct |
26 |
Correct |
349 ms |
149544 KB |
Output is correct |
27 |
Correct |
351 ms |
144480 KB |
Output is correct |
28 |
Correct |
269 ms |
134376 KB |
Output is correct |
29 |
Correct |
169 ms |
112888 KB |
Output is correct |
30 |
Correct |
302 ms |
124856 KB |
Output is correct |
31 |
Correct |
343 ms |
135324 KB |
Output is correct |
32 |
Correct |
322 ms |
141532 KB |
Output is correct |
33 |
Correct |
325 ms |
141400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
103800 KB |
Output is correct |
2 |
Correct |
76 ms |
103960 KB |
Output is correct |
3 |
Correct |
72 ms |
103800 KB |
Output is correct |
4 |
Correct |
72 ms |
103756 KB |
Output is correct |
5 |
Correct |
77 ms |
104060 KB |
Output is correct |
6 |
Correct |
69 ms |
103676 KB |
Output is correct |
7 |
Correct |
70 ms |
103700 KB |
Output is correct |
8 |
Correct |
69 ms |
103652 KB |
Output is correct |
9 |
Correct |
69 ms |
103800 KB |
Output is correct |
10 |
Correct |
70 ms |
103672 KB |
Output is correct |
11 |
Correct |
74 ms |
103928 KB |
Output is correct |
12 |
Correct |
76 ms |
104056 KB |
Output is correct |
13 |
Correct |
76 ms |
103928 KB |
Output is correct |
14 |
Correct |
78 ms |
104088 KB |
Output is correct |
15 |
Correct |
69 ms |
103672 KB |
Output is correct |
16 |
Correct |
70 ms |
103672 KB |
Output is correct |
17 |
Correct |
69 ms |
103672 KB |
Output is correct |
18 |
Correct |
1903 ms |
135280 KB |
Output is correct |
19 |
Correct |
252 ms |
109344 KB |
Output is correct |
20 |
Correct |
239 ms |
107640 KB |
Output is correct |
21 |
Correct |
225 ms |
108408 KB |
Output is correct |
22 |
Correct |
217 ms |
108536 KB |
Output is correct |
23 |
Correct |
236 ms |
109176 KB |
Output is correct |
24 |
Correct |
380 ms |
107896 KB |
Output is correct |
25 |
Correct |
311 ms |
108796 KB |
Output is correct |
26 |
Correct |
260 ms |
109176 KB |
Output is correct |
27 |
Correct |
692 ms |
126052 KB |
Output is correct |
28 |
Correct |
889 ms |
125688 KB |
Output is correct |
29 |
Correct |
866 ms |
128220 KB |
Output is correct |
30 |
Correct |
1151 ms |
138848 KB |
Output is correct |
31 |
Correct |
75 ms |
103800 KB |
Output is correct |
32 |
Correct |
1330 ms |
118764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
103800 KB |
Output is correct |
2 |
Correct |
76 ms |
103960 KB |
Output is correct |
3 |
Correct |
72 ms |
103800 KB |
Output is correct |
4 |
Correct |
72 ms |
103756 KB |
Output is correct |
5 |
Correct |
77 ms |
104060 KB |
Output is correct |
6 |
Correct |
69 ms |
103676 KB |
Output is correct |
7 |
Correct |
70 ms |
103700 KB |
Output is correct |
8 |
Correct |
69 ms |
103652 KB |
Output is correct |
9 |
Correct |
69 ms |
103800 KB |
Output is correct |
10 |
Correct |
70 ms |
103672 KB |
Output is correct |
11 |
Correct |
74 ms |
103928 KB |
Output is correct |
12 |
Correct |
76 ms |
104056 KB |
Output is correct |
13 |
Correct |
76 ms |
103928 KB |
Output is correct |
14 |
Correct |
78 ms |
104088 KB |
Output is correct |
15 |
Correct |
69 ms |
103672 KB |
Output is correct |
16 |
Correct |
70 ms |
103672 KB |
Output is correct |
17 |
Correct |
69 ms |
103672 KB |
Output is correct |
18 |
Correct |
1903 ms |
135280 KB |
Output is correct |
19 |
Correct |
252 ms |
109344 KB |
Output is correct |
20 |
Correct |
239 ms |
107640 KB |
Output is correct |
21 |
Correct |
225 ms |
108408 KB |
Output is correct |
22 |
Correct |
217 ms |
108536 KB |
Output is correct |
23 |
Correct |
236 ms |
109176 KB |
Output is correct |
24 |
Correct |
380 ms |
107896 KB |
Output is correct |
25 |
Correct |
311 ms |
108796 KB |
Output is correct |
26 |
Correct |
260 ms |
109176 KB |
Output is correct |
27 |
Correct |
692 ms |
126052 KB |
Output is correct |
28 |
Correct |
889 ms |
125688 KB |
Output is correct |
29 |
Correct |
866 ms |
128220 KB |
Output is correct |
30 |
Correct |
1151 ms |
138848 KB |
Output is correct |
31 |
Correct |
75 ms |
103800 KB |
Output is correct |
32 |
Correct |
1330 ms |
118764 KB |
Output is correct |
33 |
Correct |
315 ms |
132404 KB |
Output is correct |
34 |
Correct |
350 ms |
149592 KB |
Output is correct |
35 |
Correct |
353 ms |
144612 KB |
Output is correct |
36 |
Correct |
267 ms |
134364 KB |
Output is correct |
37 |
Correct |
189 ms |
114756 KB |
Output is correct |
38 |
Correct |
303 ms |
124440 KB |
Output is correct |
39 |
Correct |
190 ms |
119396 KB |
Output is correct |
40 |
Correct |
191 ms |
118756 KB |
Output is correct |
41 |
Correct |
178 ms |
113016 KB |
Output is correct |
42 |
Correct |
234 ms |
120280 KB |
Output is correct |
43 |
Correct |
315 ms |
132404 KB |
Output is correct |
44 |
Correct |
350 ms |
149472 KB |
Output is correct |
45 |
Correct |
349 ms |
144480 KB |
Output is correct |
46 |
Correct |
265 ms |
134376 KB |
Output is correct |
47 |
Correct |
172 ms |
112760 KB |
Output is correct |
48 |
Correct |
297 ms |
124988 KB |
Output is correct |
49 |
Correct |
341 ms |
135332 KB |
Output is correct |
50 |
Correct |
321 ms |
141528 KB |
Output is correct |
51 |
Correct |
325 ms |
141528 KB |
Output is correct |
52 |
Correct |
189 ms |
119484 KB |
Output is correct |
53 |
Correct |
193 ms |
118900 KB |
Output is correct |
54 |
Correct |
177 ms |
113144 KB |
Output is correct |
55 |
Correct |
232 ms |
120280 KB |
Output is correct |
56 |
Correct |
315 ms |
132524 KB |
Output is correct |
57 |
Correct |
349 ms |
149544 KB |
Output is correct |
58 |
Correct |
351 ms |
144480 KB |
Output is correct |
59 |
Correct |
269 ms |
134376 KB |
Output is correct |
60 |
Correct |
169 ms |
112888 KB |
Output is correct |
61 |
Correct |
302 ms |
124856 KB |
Output is correct |
62 |
Correct |
343 ms |
135324 KB |
Output is correct |
63 |
Correct |
322 ms |
141532 KB |
Output is correct |
64 |
Correct |
325 ms |
141400 KB |
Output is correct |
65 |
Correct |
970 ms |
126556 KB |
Output is correct |
66 |
Correct |
1130 ms |
127468 KB |
Output is correct |
67 |
Correct |
913 ms |
119288 KB |
Output is correct |
68 |
Correct |
1008 ms |
135124 KB |
Output is correct |
69 |
Correct |
453 ms |
140588 KB |
Output is correct |
70 |
Correct |
733 ms |
157784 KB |
Output is correct |
71 |
Correct |
504 ms |
149216 KB |
Output is correct |
72 |
Correct |
456 ms |
140392 KB |
Output is correct |
73 |
Correct |
283 ms |
120184 KB |
Output is correct |
74 |
Correct |
394 ms |
129464 KB |
Output is correct |
75 |
Correct |
407 ms |
138916 KB |
Output is correct |
76 |
Correct |
653 ms |
151256 KB |
Output is correct |
77 |
Correct |
1233 ms |
158552 KB |
Output is correct |
78 |
Correct |
428 ms |
121328 KB |
Output is correct |
79 |
Correct |
621 ms |
130652 KB |
Output is correct |
80 |
Correct |
632 ms |
130016 KB |
Output is correct |
81 |
Correct |
444 ms |
123104 KB |
Output is correct |
82 |
Correct |
500 ms |
121704 KB |
Output is correct |
83 |
Correct |
216 ms |
113144 KB |
Output is correct |
84 |
Correct |
604 ms |
127972 KB |
Output is correct |
85 |
Correct |
605 ms |
127840 KB |
Output is correct |
86 |
Correct |
510 ms |
123532 KB |
Output is correct |
87 |
Correct |
288 ms |
121956 KB |
Output is correct |
88 |
Correct |
270 ms |
123060 KB |
Output is correct |
89 |
Correct |
284 ms |
123232 KB |
Output is correct |
90 |
Correct |
285 ms |
119904 KB |
Output is correct |
91 |
Correct |
431 ms |
123740 KB |
Output is correct |