#include <bits/stdc++.h>
#include "rainbow.h"
using namespace std;
#define ii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define F0R(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
const int maxn = 2e5+10;
const int maxst = maxn*22*4*10;
int st[maxst], L[maxst], R[maxst], ctr = 0;
struct SegTree {
map<int, set<int>> points;
int roots[maxn];
void add(int r, int c) {
points[r].insert(c);
}
void build() {
F0R(i, maxn) {
roots[i] = i==0?0:roots[i-1];
for (int p : points[i]) {
update(roots[i], 0, maxn-1, p);
}
}
}
void update(int &node, int i, int j, int k) {
st[ctr+1] = st[node];
L[ctr+1] = L[node];
R[ctr+1] = R[node];
node = ctr+1;
ctr++;
st[node]++;
if (i == j) return;
int m = (i+j)/2;
if (m >= k) {
update(L[node], i, m, k);
} else {
update(R[node], m+1, j, k);
}
}
int query(int r1, int c1, int r2, int c2) {
//cout << r1 << " " << c1 << "; " << r2 << " "<< c2 << endl;
//cout << query(roots[r2], c1, c2, 0, maxn-1) << ", " << (r1>0?query(roots[r1-1], c1, c2, 0, maxn-1):0) << endl;
return query(roots[r2], c1, c2, 0, maxn-1) - (r1>0?query(roots[r1-1], c1, c2, 0, maxn-1):0);
}
int query(int node, int l, int r, int i, int j) {
if (l <= i && j <= r) return st[node];
if (l > j || r < i) return 0;
return query(L[node], l, r, i, (i+j)/2) + query(R[node], l, r, (i+j)/2+1, j);
}
} vertices, edges_horiz, edges_vert, rivers;
int min_r = maxn, min_c = maxn, max_r = 0, max_c = 0;
void addRiver(int r, int c) {
min_r = min(min_r, r);
max_r = max(max_r, r);
min_c = min(min_c, c);
max_c = max(max_c, c);
vertices.add(r, c);
vertices.add(r+1, c);
vertices.add(r, c+1);
vertices.add(r+1, c+1);
edges_horiz.add(r, c);
edges_horiz.add(r+1, c);
edges_vert.add(r, c);
edges_vert.add(r, c+1);
rivers.add(r, c);
//cout << "adding " << r << ", " << c << endl;
}
void init(int R, int C, int sr, int sc, int M, char *S) {
--sr; --sc;
addRiver(sr, sc);
F0R(i, M) {
char c = S[i];
if (c == 'N') sr--;
else if (c == 'E') sc++;
else if (c == 'S') sr++;
else if (c == 'W') sc--;
else assert(false);
addRiver(sr, sc);
}
vertices.build();
edges_horiz.build();
edges_vert.build();
rivers.build();
}
int colour(int ar, int ac, int br, int bc) {
--ar; --ac;
int riverCt = rivers.query(ar, ac, br-1, bc-1);
int vertexCt = vertices.query(ar+1, ac+1, br-1, bc-1) + (br-ar-1)*2 + (bc-ac-1)*2 + 4;
int edgeCt = edges_horiz.query(ar+1, ac, br-1, bc-1) + edges_vert.query(ar, ac+1, br-1, bc-1) + (br-ar)*2 + (bc-ac)*2;
int connectedComponents = (ar < min_r && br-1 > max_r && ac < min_c && bc-1 > max_c) ? 2 : 1;
int ans = edgeCt - vertexCt + 1 + connectedComponents - 1 - riverCt;
//cout << ar << " "<< ac << " " << br << " " << bc << " " << riverCt << " " << vertexCt << " " << edgeCt << " " << connectedComponents << endl;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
223 ms |
78956 KB |
Output is correct |
2 |
Correct |
221 ms |
79980 KB |
Output is correct |
3 |
Correct |
218 ms |
78956 KB |
Output is correct |
4 |
Correct |
215 ms |
79084 KB |
Output is correct |
5 |
Correct |
222 ms |
80364 KB |
Output is correct |
6 |
Correct |
226 ms |
78700 KB |
Output is correct |
7 |
Correct |
217 ms |
78572 KB |
Output is correct |
8 |
Correct |
217 ms |
78700 KB |
Output is correct |
9 |
Correct |
211 ms |
78828 KB |
Output is correct |
10 |
Correct |
217 ms |
78700 KB |
Output is correct |
11 |
Correct |
222 ms |
79084 KB |
Output is correct |
12 |
Correct |
218 ms |
79596 KB |
Output is correct |
13 |
Correct |
223 ms |
80748 KB |
Output is correct |
14 |
Correct |
222 ms |
81260 KB |
Output is correct |
15 |
Correct |
216 ms |
78700 KB |
Output is correct |
16 |
Correct |
214 ms |
78700 KB |
Output is correct |
17 |
Correct |
216 ms |
78572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
214 ms |
78700 KB |
Output is correct |
2 |
Correct |
216 ms |
78572 KB |
Output is correct |
3 |
Correct |
856 ms |
177388 KB |
Output is correct |
4 |
Correct |
1155 ms |
242156 KB |
Output is correct |
5 |
Correct |
1176 ms |
240876 KB |
Output is correct |
6 |
Correct |
1008 ms |
204396 KB |
Output is correct |
7 |
Correct |
1118 ms |
202220 KB |
Output is correct |
8 |
Correct |
520 ms |
82284 KB |
Output is correct |
9 |
Correct |
1154 ms |
242156 KB |
Output is correct |
10 |
Correct |
1184 ms |
241132 KB |
Output is correct |
11 |
Correct |
1044 ms |
204268 KB |
Output is correct |
12 |
Correct |
708 ms |
230764 KB |
Output is correct |
13 |
Correct |
744 ms |
242028 KB |
Output is correct |
14 |
Correct |
757 ms |
240876 KB |
Output is correct |
15 |
Correct |
740 ms |
204524 KB |
Output is correct |
16 |
Correct |
933 ms |
195308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
78700 KB |
Output is correct |
2 |
Correct |
563 ms |
238700 KB |
Output is correct |
3 |
Correct |
534 ms |
240876 KB |
Output is correct |
4 |
Correct |
555 ms |
238700 KB |
Output is correct |
5 |
Correct |
458 ms |
198660 KB |
Output is correct |
6 |
Correct |
335 ms |
108780 KB |
Output is correct |
7 |
Correct |
420 ms |
138604 KB |
Output is correct |
8 |
Correct |
539 ms |
221420 KB |
Output is correct |
9 |
Correct |
538 ms |
205548 KB |
Output is correct |
10 |
Correct |
318 ms |
110356 KB |
Output is correct |
11 |
Correct |
424 ms |
157176 KB |
Output is correct |
12 |
Correct |
562 ms |
238848 KB |
Output is correct |
13 |
Correct |
527 ms |
240876 KB |
Output is correct |
14 |
Correct |
557 ms |
238736 KB |
Output is correct |
15 |
Correct |
465 ms |
198624 KB |
Output is correct |
16 |
Correct |
329 ms |
102764 KB |
Output is correct |
17 |
Correct |
407 ms |
138732 KB |
Output is correct |
18 |
Correct |
538 ms |
238700 KB |
Output is correct |
19 |
Correct |
548 ms |
239724 KB |
Output is correct |
20 |
Correct |
552 ms |
239660 KB |
Output is correct |
21 |
Correct |
562 ms |
221428 KB |
Output is correct |
22 |
Correct |
550 ms |
205548 KB |
Output is correct |
23 |
Correct |
331 ms |
110316 KB |
Output is correct |
24 |
Correct |
427 ms |
157164 KB |
Output is correct |
25 |
Correct |
558 ms |
238828 KB |
Output is correct |
26 |
Correct |
535 ms |
240876 KB |
Output is correct |
27 |
Correct |
547 ms |
238700 KB |
Output is correct |
28 |
Correct |
461 ms |
198764 KB |
Output is correct |
29 |
Correct |
314 ms |
102764 KB |
Output is correct |
30 |
Correct |
402 ms |
138732 KB |
Output is correct |
31 |
Correct |
525 ms |
238872 KB |
Output is correct |
32 |
Correct |
535 ms |
239852 KB |
Output is correct |
33 |
Correct |
532 ms |
239692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
223 ms |
78956 KB |
Output is correct |
2 |
Correct |
221 ms |
79980 KB |
Output is correct |
3 |
Correct |
218 ms |
78956 KB |
Output is correct |
4 |
Correct |
215 ms |
79084 KB |
Output is correct |
5 |
Correct |
222 ms |
80364 KB |
Output is correct |
6 |
Correct |
226 ms |
78700 KB |
Output is correct |
7 |
Correct |
217 ms |
78572 KB |
Output is correct |
8 |
Correct |
217 ms |
78700 KB |
Output is correct |
9 |
Correct |
211 ms |
78828 KB |
Output is correct |
10 |
Correct |
217 ms |
78700 KB |
Output is correct |
11 |
Correct |
222 ms |
79084 KB |
Output is correct |
12 |
Correct |
218 ms |
79596 KB |
Output is correct |
13 |
Correct |
223 ms |
80748 KB |
Output is correct |
14 |
Correct |
222 ms |
81260 KB |
Output is correct |
15 |
Correct |
216 ms |
78700 KB |
Output is correct |
16 |
Correct |
214 ms |
78700 KB |
Output is correct |
17 |
Correct |
216 ms |
78572 KB |
Output is correct |
18 |
Correct |
1404 ms |
162888 KB |
Output is correct |
19 |
Correct |
551 ms |
85868 KB |
Output is correct |
20 |
Correct |
452 ms |
82668 KB |
Output is correct |
21 |
Correct |
496 ms |
83564 KB |
Output is correct |
22 |
Correct |
506 ms |
84208 KB |
Output is correct |
23 |
Correct |
530 ms |
85624 KB |
Output is correct |
24 |
Correct |
472 ms |
83276 KB |
Output is correct |
25 |
Correct |
494 ms |
84076 KB |
Output is correct |
26 |
Correct |
502 ms |
84460 KB |
Output is correct |
27 |
Correct |
799 ms |
147564 KB |
Output is correct |
28 |
Correct |
685 ms |
113644 KB |
Output is correct |
29 |
Correct |
794 ms |
141932 KB |
Output is correct |
30 |
Correct |
1051 ms |
242284 KB |
Output is correct |
31 |
Correct |
225 ms |
78828 KB |
Output is correct |
32 |
Correct |
1125 ms |
148588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
223 ms |
78956 KB |
Output is correct |
2 |
Correct |
221 ms |
79980 KB |
Output is correct |
3 |
Correct |
218 ms |
78956 KB |
Output is correct |
4 |
Correct |
215 ms |
79084 KB |
Output is correct |
5 |
Correct |
222 ms |
80364 KB |
Output is correct |
6 |
Correct |
226 ms |
78700 KB |
Output is correct |
7 |
Correct |
217 ms |
78572 KB |
Output is correct |
8 |
Correct |
217 ms |
78700 KB |
Output is correct |
9 |
Correct |
211 ms |
78828 KB |
Output is correct |
10 |
Correct |
217 ms |
78700 KB |
Output is correct |
11 |
Correct |
222 ms |
79084 KB |
Output is correct |
12 |
Correct |
218 ms |
79596 KB |
Output is correct |
13 |
Correct |
223 ms |
80748 KB |
Output is correct |
14 |
Correct |
222 ms |
81260 KB |
Output is correct |
15 |
Correct |
216 ms |
78700 KB |
Output is correct |
16 |
Correct |
214 ms |
78700 KB |
Output is correct |
17 |
Correct |
216 ms |
78572 KB |
Output is correct |
18 |
Correct |
1404 ms |
162888 KB |
Output is correct |
19 |
Correct |
551 ms |
85868 KB |
Output is correct |
20 |
Correct |
452 ms |
82668 KB |
Output is correct |
21 |
Correct |
496 ms |
83564 KB |
Output is correct |
22 |
Correct |
506 ms |
84208 KB |
Output is correct |
23 |
Correct |
530 ms |
85624 KB |
Output is correct |
24 |
Correct |
472 ms |
83276 KB |
Output is correct |
25 |
Correct |
494 ms |
84076 KB |
Output is correct |
26 |
Correct |
502 ms |
84460 KB |
Output is correct |
27 |
Correct |
799 ms |
147564 KB |
Output is correct |
28 |
Correct |
685 ms |
113644 KB |
Output is correct |
29 |
Correct |
794 ms |
141932 KB |
Output is correct |
30 |
Correct |
1051 ms |
242284 KB |
Output is correct |
31 |
Correct |
225 ms |
78828 KB |
Output is correct |
32 |
Correct |
1125 ms |
148588 KB |
Output is correct |
33 |
Correct |
563 ms |
238700 KB |
Output is correct |
34 |
Correct |
534 ms |
240876 KB |
Output is correct |
35 |
Correct |
555 ms |
238700 KB |
Output is correct |
36 |
Correct |
458 ms |
198660 KB |
Output is correct |
37 |
Correct |
335 ms |
108780 KB |
Output is correct |
38 |
Correct |
420 ms |
138604 KB |
Output is correct |
39 |
Correct |
539 ms |
221420 KB |
Output is correct |
40 |
Correct |
538 ms |
205548 KB |
Output is correct |
41 |
Correct |
318 ms |
110356 KB |
Output is correct |
42 |
Correct |
424 ms |
157176 KB |
Output is correct |
43 |
Correct |
562 ms |
238848 KB |
Output is correct |
44 |
Correct |
527 ms |
240876 KB |
Output is correct |
45 |
Correct |
557 ms |
238736 KB |
Output is correct |
46 |
Correct |
465 ms |
198624 KB |
Output is correct |
47 |
Correct |
329 ms |
102764 KB |
Output is correct |
48 |
Correct |
407 ms |
138732 KB |
Output is correct |
49 |
Correct |
538 ms |
238700 KB |
Output is correct |
50 |
Correct |
548 ms |
239724 KB |
Output is correct |
51 |
Correct |
552 ms |
239660 KB |
Output is correct |
52 |
Correct |
562 ms |
221428 KB |
Output is correct |
53 |
Correct |
550 ms |
205548 KB |
Output is correct |
54 |
Correct |
331 ms |
110316 KB |
Output is correct |
55 |
Correct |
427 ms |
157164 KB |
Output is correct |
56 |
Correct |
558 ms |
238828 KB |
Output is correct |
57 |
Correct |
535 ms |
240876 KB |
Output is correct |
58 |
Correct |
547 ms |
238700 KB |
Output is correct |
59 |
Correct |
461 ms |
198764 KB |
Output is correct |
60 |
Correct |
314 ms |
102764 KB |
Output is correct |
61 |
Correct |
402 ms |
138732 KB |
Output is correct |
62 |
Correct |
525 ms |
238872 KB |
Output is correct |
63 |
Correct |
535 ms |
239852 KB |
Output is correct |
64 |
Correct |
532 ms |
239692 KB |
Output is correct |
65 |
Correct |
1672 ms |
225004 KB |
Output is correct |
66 |
Correct |
1740 ms |
209004 KB |
Output is correct |
67 |
Correct |
859 ms |
113856 KB |
Output is correct |
68 |
Correct |
933 ms |
160876 KB |
Output is correct |
69 |
Correct |
1179 ms |
242300 KB |
Output is correct |
70 |
Correct |
1082 ms |
244368 KB |
Output is correct |
71 |
Correct |
1052 ms |
242328 KB |
Output is correct |
72 |
Correct |
933 ms |
202348 KB |
Output is correct |
73 |
Correct |
712 ms |
106348 KB |
Output is correct |
74 |
Correct |
831 ms |
142188 KB |
Output is correct |
75 |
Correct |
922 ms |
242284 KB |
Output is correct |
76 |
Correct |
1155 ms |
243528 KB |
Output is correct |
77 |
Correct |
993 ms |
243308 KB |
Output is correct |
78 |
Correct |
856 ms |
177388 KB |
Output is correct |
79 |
Correct |
1155 ms |
242156 KB |
Output is correct |
80 |
Correct |
1176 ms |
240876 KB |
Output is correct |
81 |
Correct |
1008 ms |
204396 KB |
Output is correct |
82 |
Correct |
1118 ms |
202220 KB |
Output is correct |
83 |
Correct |
520 ms |
82284 KB |
Output is correct |
84 |
Correct |
1154 ms |
242156 KB |
Output is correct |
85 |
Correct |
1184 ms |
241132 KB |
Output is correct |
86 |
Correct |
1044 ms |
204268 KB |
Output is correct |
87 |
Correct |
708 ms |
230764 KB |
Output is correct |
88 |
Correct |
744 ms |
242028 KB |
Output is correct |
89 |
Correct |
757 ms |
240876 KB |
Output is correct |
90 |
Correct |
740 ms |
204524 KB |
Output is correct |
91 |
Correct |
933 ms |
195308 KB |
Output is correct |