#include "rainbow.h"
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <utility>
#include <vector>
#include <set>
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
class PersistentSegtree {
public:
int st[20000000], lc[20000000], rc[20000000], cnt = 1;
int heads[200005];
void seg_inc(int n, int pn, int lo, int hi, int i, int v) {
if (lo + 1 == hi) {
st[n] = st[pn] + v;
return;
}
lc[n] = lc[pn], rc[n] = rc[pn];
int mid = (lo + hi) / 2;
if (mid > i) {
lc[n] = cnt++;
seg_inc(lc[n], lc[pn], lo, mid, i, v);
} else {
rc[n] = cnt++;
seg_inc(rc[n], rc[pn], mid, hi, i, v);
}
st[n] = st[lc[n]] + st[rc[n]];
}
int seg_sum(int n, int pn, int lo, int hi, int l, int r) {
if (lo >= r || hi <= l) return 0;
if (lo >= l && hi <= r) {
return st[n] - st[pn];
}
int mid = (lo + hi) / 2;
return seg_sum(lc[n], lc[pn], lo, mid, l, r) + seg_sum(rc[n], rc[pn], mid, hi, l, r);
}
} st[4];
vector<int> rec[4][200005];
set<pii> water, vedges, hedges, verts;
void init(int R, int C, int sr, int sc, int M, char *S) {
water.insert({ sr, sc });
for (int i = 0; i < M; i++) {
if (S[i] == 'N') {
sr -= 1;
} else if (S[i] == 'S') {
sr += 1;
} else if (S[i] == 'E') {
sc += 1;
} else {
sc -= 1;
}
water.insert({ sr, sc });
}
for (pii p : water) {
verts.insert(p);
verts.insert({ p.fi + 1, p.se });
verts.insert({ p.fi, p.se + 1 });
verts.insert({ p.fi + 1, p.se + 1 });
vedges.insert(p);
vedges.insert({ p.fi, p.se + 1 });
hedges.insert(p);
hedges.insert({ p.fi + 1, p.se });
rec[0][p.fi].push_back(p.se);
}
for (pii p : verts) rec[1][p.fi].push_back(p.se);
for (pii p : hedges) rec[2][p.fi].push_back(p.se);
for (pii p : vedges) rec[3][p.fi].push_back(p.se);
for (int j = 0; j < 4; j++) {
for (int i = 1; i <= R; i++) {
int node = st[j].heads[i-1];
for (int x : rec[j][i]) {
int nn = st[j].cnt++;
st[j].seg_inc(nn, node, 1, 200005, x, 1);
node = nn;
}
st[j].heads[i] = node;
}
}
}
int colour(int ar, int ac, int br, int bc) {
int a = st[3].seg_sum(st[3].heads[br], st[3].heads[ar-1], 1, 200005, ac + 1, bc + 1);
int b = st[2].seg_sum(st[2].heads[br], st[2].heads[ar], 1, 200005, ac, bc + 1);
int c = st[1].seg_sum(st[1].heads[br], st[1].heads[ar], 1, 200005, ac + 1, bc + 1);
int d = st[0].seg_sum(st[0].heads[br], st[0].heads[ar-1], 1, 200005, ac, bc + 1);
//printf("Vertical edges: %d\nHorizontal edges: %d\nVertices: %d\nWater: %d\n", a, b, c, d);
if (c == verts.size()) return 2 + a + b - c - d;
return 1 + a + b - c - d;
}
Compilation message
rainbow.cpp: In function 'int colour(int, int, int, int)':
rainbow.cpp:93:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (c == verts.size()) return 2 + a + b - c - d;
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
19456 KB |
Output is correct |
2 |
Correct |
19 ms |
20608 KB |
Output is correct |
3 |
Correct |
18 ms |
19584 KB |
Output is correct |
4 |
Correct |
17 ms |
19712 KB |
Output is correct |
5 |
Correct |
19 ms |
20864 KB |
Output is correct |
6 |
Correct |
17 ms |
19200 KB |
Output is correct |
7 |
Correct |
15 ms |
19200 KB |
Output is correct |
8 |
Correct |
15 ms |
19328 KB |
Output is correct |
9 |
Correct |
17 ms |
19200 KB |
Output is correct |
10 |
Correct |
15 ms |
19200 KB |
Output is correct |
11 |
Correct |
18 ms |
19840 KB |
Output is correct |
12 |
Correct |
20 ms |
20352 KB |
Output is correct |
13 |
Correct |
20 ms |
21376 KB |
Output is correct |
14 |
Correct |
23 ms |
21888 KB |
Output is correct |
15 |
Correct |
15 ms |
19200 KB |
Output is correct |
16 |
Correct |
16 ms |
19200 KB |
Output is correct |
17 |
Correct |
21 ms |
19200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
19200 KB |
Output is correct |
2 |
Correct |
21 ms |
19200 KB |
Output is correct |
3 |
Correct |
515 ms |
102508 KB |
Output is correct |
4 |
Correct |
730 ms |
160240 KB |
Output is correct |
5 |
Correct |
746 ms |
158448 KB |
Output is correct |
6 |
Correct |
548 ms |
124532 KB |
Output is correct |
7 |
Correct |
628 ms |
125556 KB |
Output is correct |
8 |
Correct |
230 ms |
20088 KB |
Output is correct |
9 |
Correct |
722 ms |
160880 KB |
Output is correct |
10 |
Correct |
742 ms |
158828 KB |
Output is correct |
11 |
Correct |
696 ms |
124276 KB |
Output is correct |
12 |
Correct |
476 ms |
146288 KB |
Output is correct |
13 |
Correct |
465 ms |
160368 KB |
Output is correct |
14 |
Correct |
483 ms |
158320 KB |
Output is correct |
15 |
Correct |
377 ms |
124408 KB |
Output is correct |
16 |
Correct |
635 ms |
124912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
19200 KB |
Output is correct |
2 |
Correct |
426 ms |
184812 KB |
Output is correct |
3 |
Correct |
404 ms |
197028 KB |
Output is correct |
4 |
Correct |
462 ms |
188536 KB |
Output is correct |
5 |
Correct |
303 ms |
150268 KB |
Output is correct |
6 |
Correct |
103 ms |
52984 KB |
Output is correct |
7 |
Correct |
169 ms |
83460 KB |
Output is correct |
8 |
Correct |
375 ms |
146976 KB |
Output is correct |
9 |
Correct |
346 ms |
135092 KB |
Output is correct |
10 |
Correct |
121 ms |
52216 KB |
Output is correct |
11 |
Correct |
180 ms |
93180 KB |
Output is correct |
12 |
Correct |
417 ms |
184844 KB |
Output is correct |
13 |
Correct |
424 ms |
197096 KB |
Output is correct |
14 |
Correct |
484 ms |
188652 KB |
Output is correct |
15 |
Correct |
306 ms |
150344 KB |
Output is correct |
16 |
Correct |
80 ms |
46968 KB |
Output is correct |
17 |
Correct |
175 ms |
83576 KB |
Output is correct |
18 |
Correct |
374 ms |
185464 KB |
Output is correct |
19 |
Correct |
397 ms |
188660 KB |
Output is correct |
20 |
Correct |
421 ms |
188528 KB |
Output is correct |
21 |
Correct |
358 ms |
146964 KB |
Output is correct |
22 |
Correct |
277 ms |
135032 KB |
Output is correct |
23 |
Correct |
87 ms |
52216 KB |
Output is correct |
24 |
Correct |
169 ms |
93056 KB |
Output is correct |
25 |
Correct |
410 ms |
184924 KB |
Output is correct |
26 |
Correct |
387 ms |
197112 KB |
Output is correct |
27 |
Correct |
449 ms |
188664 KB |
Output is correct |
28 |
Correct |
310 ms |
150392 KB |
Output is correct |
29 |
Correct |
85 ms |
46868 KB |
Output is correct |
30 |
Correct |
177 ms |
83580 KB |
Output is correct |
31 |
Correct |
371 ms |
185480 KB |
Output is correct |
32 |
Correct |
417 ms |
188668 KB |
Output is correct |
33 |
Correct |
385 ms |
188528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
19456 KB |
Output is correct |
2 |
Correct |
19 ms |
20608 KB |
Output is correct |
3 |
Correct |
18 ms |
19584 KB |
Output is correct |
4 |
Correct |
17 ms |
19712 KB |
Output is correct |
5 |
Correct |
19 ms |
20864 KB |
Output is correct |
6 |
Correct |
17 ms |
19200 KB |
Output is correct |
7 |
Correct |
15 ms |
19200 KB |
Output is correct |
8 |
Correct |
15 ms |
19328 KB |
Output is correct |
9 |
Correct |
17 ms |
19200 KB |
Output is correct |
10 |
Correct |
15 ms |
19200 KB |
Output is correct |
11 |
Correct |
18 ms |
19840 KB |
Output is correct |
12 |
Correct |
20 ms |
20352 KB |
Output is correct |
13 |
Correct |
20 ms |
21376 KB |
Output is correct |
14 |
Correct |
23 ms |
21888 KB |
Output is correct |
15 |
Correct |
15 ms |
19200 KB |
Output is correct |
16 |
Correct |
16 ms |
19200 KB |
Output is correct |
17 |
Correct |
21 ms |
19200 KB |
Output is correct |
18 |
Correct |
613 ms |
104952 KB |
Output is correct |
19 |
Correct |
201 ms |
26360 KB |
Output is correct |
20 |
Correct |
154 ms |
23288 KB |
Output is correct |
21 |
Correct |
170 ms |
23928 KB |
Output is correct |
22 |
Correct |
180 ms |
24696 KB |
Output is correct |
23 |
Correct |
200 ms |
26236 KB |
Output is correct |
24 |
Correct |
167 ms |
23804 KB |
Output is correct |
25 |
Correct |
170 ms |
24568 KB |
Output is correct |
26 |
Correct |
186 ms |
25084 KB |
Output is correct |
27 |
Correct |
410 ms |
89336 KB |
Output is correct |
28 |
Correct |
323 ms |
54920 KB |
Output is correct |
29 |
Correct |
381 ms |
83596 KB |
Output is correct |
30 |
Correct |
679 ms |
185848 KB |
Output is correct |
31 |
Correct |
20 ms |
19584 KB |
Output is correct |
32 |
Correct |
708 ms |
90232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
19456 KB |
Output is correct |
2 |
Correct |
19 ms |
20608 KB |
Output is correct |
3 |
Correct |
18 ms |
19584 KB |
Output is correct |
4 |
Correct |
17 ms |
19712 KB |
Output is correct |
5 |
Correct |
19 ms |
20864 KB |
Output is correct |
6 |
Correct |
17 ms |
19200 KB |
Output is correct |
7 |
Correct |
15 ms |
19200 KB |
Output is correct |
8 |
Correct |
15 ms |
19328 KB |
Output is correct |
9 |
Correct |
17 ms |
19200 KB |
Output is correct |
10 |
Correct |
15 ms |
19200 KB |
Output is correct |
11 |
Correct |
18 ms |
19840 KB |
Output is correct |
12 |
Correct |
20 ms |
20352 KB |
Output is correct |
13 |
Correct |
20 ms |
21376 KB |
Output is correct |
14 |
Correct |
23 ms |
21888 KB |
Output is correct |
15 |
Correct |
15 ms |
19200 KB |
Output is correct |
16 |
Correct |
16 ms |
19200 KB |
Output is correct |
17 |
Correct |
21 ms |
19200 KB |
Output is correct |
18 |
Correct |
613 ms |
104952 KB |
Output is correct |
19 |
Correct |
201 ms |
26360 KB |
Output is correct |
20 |
Correct |
154 ms |
23288 KB |
Output is correct |
21 |
Correct |
170 ms |
23928 KB |
Output is correct |
22 |
Correct |
180 ms |
24696 KB |
Output is correct |
23 |
Correct |
200 ms |
26236 KB |
Output is correct |
24 |
Correct |
167 ms |
23804 KB |
Output is correct |
25 |
Correct |
170 ms |
24568 KB |
Output is correct |
26 |
Correct |
186 ms |
25084 KB |
Output is correct |
27 |
Correct |
410 ms |
89336 KB |
Output is correct |
28 |
Correct |
323 ms |
54920 KB |
Output is correct |
29 |
Correct |
381 ms |
83596 KB |
Output is correct |
30 |
Correct |
679 ms |
185848 KB |
Output is correct |
31 |
Correct |
20 ms |
19584 KB |
Output is correct |
32 |
Correct |
708 ms |
90232 KB |
Output is correct |
33 |
Correct |
426 ms |
184812 KB |
Output is correct |
34 |
Correct |
404 ms |
197028 KB |
Output is correct |
35 |
Correct |
462 ms |
188536 KB |
Output is correct |
36 |
Correct |
303 ms |
150268 KB |
Output is correct |
37 |
Correct |
103 ms |
52984 KB |
Output is correct |
38 |
Correct |
169 ms |
83460 KB |
Output is correct |
39 |
Correct |
375 ms |
146976 KB |
Output is correct |
40 |
Correct |
346 ms |
135092 KB |
Output is correct |
41 |
Correct |
121 ms |
52216 KB |
Output is correct |
42 |
Correct |
180 ms |
93180 KB |
Output is correct |
43 |
Correct |
417 ms |
184844 KB |
Output is correct |
44 |
Correct |
424 ms |
197096 KB |
Output is correct |
45 |
Correct |
484 ms |
188652 KB |
Output is correct |
46 |
Correct |
306 ms |
150344 KB |
Output is correct |
47 |
Correct |
80 ms |
46968 KB |
Output is correct |
48 |
Correct |
175 ms |
83576 KB |
Output is correct |
49 |
Correct |
374 ms |
185464 KB |
Output is correct |
50 |
Correct |
397 ms |
188660 KB |
Output is correct |
51 |
Correct |
421 ms |
188528 KB |
Output is correct |
52 |
Correct |
358 ms |
146964 KB |
Output is correct |
53 |
Correct |
277 ms |
135032 KB |
Output is correct |
54 |
Correct |
87 ms |
52216 KB |
Output is correct |
55 |
Correct |
169 ms |
93056 KB |
Output is correct |
56 |
Correct |
410 ms |
184924 KB |
Output is correct |
57 |
Correct |
387 ms |
197112 KB |
Output is correct |
58 |
Correct |
449 ms |
188664 KB |
Output is correct |
59 |
Correct |
310 ms |
150392 KB |
Output is correct |
60 |
Correct |
85 ms |
46868 KB |
Output is correct |
61 |
Correct |
177 ms |
83580 KB |
Output is correct |
62 |
Correct |
371 ms |
185480 KB |
Output is correct |
63 |
Correct |
417 ms |
188668 KB |
Output is correct |
64 |
Correct |
385 ms |
188528 KB |
Output is correct |
65 |
Correct |
969 ms |
150432 KB |
Output is correct |
66 |
Correct |
1002 ms |
138484 KB |
Output is correct |
67 |
Correct |
449 ms |
55688 KB |
Output is correct |
68 |
Correct |
485 ms |
96504 KB |
Output is correct |
69 |
Correct |
852 ms |
188400 KB |
Output is correct |
70 |
Correct |
675 ms |
200568 KB |
Output is correct |
71 |
Correct |
752 ms |
192120 KB |
Output is correct |
72 |
Correct |
646 ms |
153848 KB |
Output is correct |
73 |
Correct |
376 ms |
50440 KB |
Output is correct |
74 |
Correct |
430 ms |
87160 KB |
Output is correct |
75 |
Correct |
627 ms |
188920 KB |
Output is correct |
76 |
Correct |
856 ms |
192172 KB |
Output is correct |
77 |
Correct |
762 ms |
192112 KB |
Output is correct |
78 |
Correct |
515 ms |
102508 KB |
Output is correct |
79 |
Correct |
730 ms |
160240 KB |
Output is correct |
80 |
Correct |
746 ms |
158448 KB |
Output is correct |
81 |
Correct |
548 ms |
124532 KB |
Output is correct |
82 |
Correct |
628 ms |
125556 KB |
Output is correct |
83 |
Correct |
230 ms |
20088 KB |
Output is correct |
84 |
Correct |
722 ms |
160880 KB |
Output is correct |
85 |
Correct |
742 ms |
158828 KB |
Output is correct |
86 |
Correct |
696 ms |
124276 KB |
Output is correct |
87 |
Correct |
476 ms |
146288 KB |
Output is correct |
88 |
Correct |
465 ms |
160368 KB |
Output is correct |
89 |
Correct |
483 ms |
158320 KB |
Output is correct |
90 |
Correct |
377 ms |
124408 KB |
Output is correct |
91 |
Correct |
635 ms |
124912 KB |
Output is correct |