#include "rainbow.h"
#include <stdlib.h>
#define N 200000
#define L 100000
#define N_ (1 << 18) /* N_ = pow2(ceil(log2(N))) */
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
void sort(int *aa, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, a = aa[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa[j] == a)
j++;
else if (aa[j] < a) {
tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
i++, j++;
} else {
k--;
tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
}
sort(aa, l, i);
l = k;
}
}
void merge(int *aa, int n, int *bb, int m, int *cc) {
int i, j, k;
i = 0, j = 0, k = 0;
while (i < n && j < m)
cc[k++] = aa[i] < bb[j] ? aa[i++] : bb[j++];
while (i < n)
cc[k++] = aa[i++];
while (j < m)
cc[k++] = bb[j++];
}
int n_;
void build(int **st, int *kk, int *ii, int *jj, int l) {
int h, i, j;
for (h = 0; h < l; h++)
if (ii[h] < n_)
kk[n_ + ii[h]]++;
for (i = 0; i < n_; i++)
st[n_ + i] = (int *) malloc(kk[n_ + i] * sizeof *st[n_ + i]), kk[n_ + i] = 0;
for (h = 0; h < l; h++) {
i = ii[h], j = jj[h];
if (i < n_)
st[n_ + i][kk[n_ + i]++] = j;
}
for (i = n_; i < n_ * 2; i++) {
int k;
sort(st[i], 0, kk[i]);
k = 0;
for (h = 0; h < kk[i]; h++)
if (k == 0 || st[i][h] != st[i][k - 1])
st[i][k++] = st[i][h];
kk[i] = k;
}
for (i = n_ - 1; i > 0; i--) {
int l = i << 1, r = l | 1;
st[i] = (int *) malloc((kk[i] = kk[l] + kk[r]) * sizeof *st[i]);
merge(st[l], kk[l], st[r], kk[r], st[i]);
}
}
int *stv[N_ * 2], kkv[N_ * 2], *ste1[N_ * 2], kke1[N_ * 2], *ste2[N_ * 2], kke2[N_ * 2], *stf[N_ * 2], kkf[N_ * 2], imin, imax, jmin, jmax;
void init(int n, int m, int i_, int j_, int l, char *dd) {
static int ii[(L + 1) * 4], jj[(L + 1) * 4];
int h;
i_--, j_--;
imin = imax = i_, jmin = jmax = j_;
ii[0] = i_, jj[0] = j_;
for (h = 0; h < l; h++) {
if (dd[h] == 'N')
i_--;
else if (dd[h] == 'S')
i_++;
else if (dd[h] == 'W')
j_--;
else
j_++;
imin = min(imin, i_), imax = max(imax, i_);
jmin = min(jmin, j_), jmax = max(jmax, j_);
ii[h + 1] = i_, jj[h + 1] = j_;
}
l++;
n_ = 1;
while (n_ < n)
n_ <<= 1;
build(stv, kkv, ii, jj, l);
for (h = 0; h < l; h++)
ii[l + h] = ii[h], jj[l + h] = jj[h] + 1;
build(ste1, kke1, ii, jj, l * 2);
for (h = 0; h < l; h++)
ii[l + h] = ii[h] + 1, jj[l + h] = jj[h];
build(ste2, kke2, ii, jj, l * 2);
for (h = 0; h < l; h++) {
ii[l + h] = ii[h], jj[l + h] = jj[h] + 1;
ii[l + l + h] = ii[h] + 1, jj[l + l + h] = jj[h];
ii[l + l + l + h] = ii[h] + 1, jj[l + l + l + h] = jj[h] + 1;
}
build(stf, kkf, ii, jj, l * 4);
}
int query_(int *aa, int n, int a) {
int lower = -1, upper = n;
while (upper - lower > 1) {
int i = (lower + upper) / 2;
if (aa[i] <= a)
lower = i;
else
upper = i;
}
return upper;
}
long long query(int **st, int *kk, int il, int ir, int jl, int jr) {
long long ans;
if (il > ir || jl > jr)
return 0;
ans = (long long) (ir - il + 1) * (jr - jl + 1);
for (il += n_, ir += n_; il <= ir; il >>= 1, ir >>= 1) {
if ((il & 1) == 1)
ans -= query_(st[il], kk[il], jr) - query_(st[il], kk[il], jl - 1), il++;
if ((ir & 1) == 0)
ans -= query_(st[ir], kk[ir], jr) - query_(st[ir], kk[ir], jl - 1), ir--;
}
return ans;
}
int colour(int il, int jl, int ir, int jr) {
long long v, e, f;
il--, jl--, ir--, jr--;
v = query(stv, kkv, il, ir, jl, jr);
e = query(ste1, kke1, il, ir, jl + 1, jr) + query(ste2, kke2, il + 1, ir, jl, jr);
f = query(stf, kkf, il + 1, ir, jl + 1, jr);
if (il < imin && imax < ir && jl < jmin && jmax < jr)
f++;
return v - e + f;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
5 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
620 KB |
Output is correct |
12 |
Correct |
3 ms |
620 KB |
Output is correct |
13 |
Correct |
4 ms |
748 KB |
Output is correct |
14 |
Correct |
5 ms |
876 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
170 ms |
8684 KB |
Output is correct |
4 |
Correct |
217 ms |
12140 KB |
Output is correct |
5 |
Correct |
249 ms |
12044 KB |
Output is correct |
6 |
Correct |
183 ms |
11628 KB |
Output is correct |
7 |
Correct |
209 ms |
11884 KB |
Output is correct |
8 |
Correct |
75 ms |
10092 KB |
Output is correct |
9 |
Correct |
211 ms |
12140 KB |
Output is correct |
10 |
Correct |
247 ms |
12268 KB |
Output is correct |
11 |
Correct |
194 ms |
11628 KB |
Output is correct |
12 |
Correct |
136 ms |
11244 KB |
Output is correct |
13 |
Correct |
169 ms |
12012 KB |
Output is correct |
14 |
Correct |
147 ms |
12012 KB |
Output is correct |
15 |
Correct |
146 ms |
11500 KB |
Output is correct |
16 |
Correct |
174 ms |
9964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
204 ms |
139884 KB |
Output is correct |
3 |
Correct |
161 ms |
130796 KB |
Output is correct |
4 |
Correct |
156 ms |
133612 KB |
Output is correct |
5 |
Correct |
162 ms |
124012 KB |
Output is correct |
6 |
Correct |
169 ms |
105324 KB |
Output is correct |
7 |
Correct |
170 ms |
113260 KB |
Output is correct |
8 |
Correct |
73 ms |
13548 KB |
Output is correct |
9 |
Correct |
72 ms |
14724 KB |
Output is correct |
10 |
Correct |
90 ms |
56812 KB |
Output is correct |
11 |
Correct |
128 ms |
71916 KB |
Output is correct |
12 |
Correct |
211 ms |
139932 KB |
Output is correct |
13 |
Correct |
166 ms |
131052 KB |
Output is correct |
14 |
Correct |
161 ms |
133612 KB |
Output is correct |
15 |
Correct |
166 ms |
124012 KB |
Output is correct |
16 |
Correct |
156 ms |
103788 KB |
Output is correct |
17 |
Correct |
166 ms |
113260 KB |
Output is correct |
18 |
Correct |
198 ms |
139756 KB |
Output is correct |
19 |
Correct |
188 ms |
135552 KB |
Output is correct |
20 |
Correct |
197 ms |
135404 KB |
Output is correct |
21 |
Correct |
73 ms |
13548 KB |
Output is correct |
22 |
Correct |
72 ms |
14700 KB |
Output is correct |
23 |
Correct |
86 ms |
56812 KB |
Output is correct |
24 |
Correct |
119 ms |
71916 KB |
Output is correct |
25 |
Correct |
214 ms |
139920 KB |
Output is correct |
26 |
Correct |
159 ms |
130796 KB |
Output is correct |
27 |
Correct |
166 ms |
133700 KB |
Output is correct |
28 |
Correct |
172 ms |
124012 KB |
Output is correct |
29 |
Correct |
154 ms |
103660 KB |
Output is correct |
30 |
Correct |
174 ms |
113260 KB |
Output is correct |
31 |
Correct |
206 ms |
139756 KB |
Output is correct |
32 |
Correct |
185 ms |
135404 KB |
Output is correct |
33 |
Correct |
185 ms |
135404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
5 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
620 KB |
Output is correct |
12 |
Correct |
3 ms |
620 KB |
Output is correct |
13 |
Correct |
4 ms |
748 KB |
Output is correct |
14 |
Correct |
5 ms |
876 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1449 ms |
19868 KB |
Output is correct |
19 |
Correct |
214 ms |
1772 KB |
Output is correct |
20 |
Correct |
146 ms |
1388 KB |
Output is correct |
21 |
Correct |
183 ms |
1388 KB |
Output is correct |
22 |
Correct |
224 ms |
1644 KB |
Output is correct |
23 |
Correct |
151 ms |
1644 KB |
Output is correct |
24 |
Correct |
193 ms |
1388 KB |
Output is correct |
25 |
Correct |
226 ms |
1608 KB |
Output is correct |
26 |
Correct |
235 ms |
1772 KB |
Output is correct |
27 |
Correct |
637 ms |
17772 KB |
Output is correct |
28 |
Correct |
574 ms |
12784 KB |
Output is correct |
29 |
Correct |
504 ms |
17180 KB |
Output is correct |
30 |
Correct |
1293 ms |
31640 KB |
Output is correct |
31 |
Correct |
3 ms |
364 KB |
Output is correct |
32 |
Correct |
1213 ms |
18184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
5 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
620 KB |
Output is correct |
12 |
Correct |
3 ms |
620 KB |
Output is correct |
13 |
Correct |
4 ms |
748 KB |
Output is correct |
14 |
Correct |
5 ms |
876 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1449 ms |
19868 KB |
Output is correct |
19 |
Correct |
214 ms |
1772 KB |
Output is correct |
20 |
Correct |
146 ms |
1388 KB |
Output is correct |
21 |
Correct |
183 ms |
1388 KB |
Output is correct |
22 |
Correct |
224 ms |
1644 KB |
Output is correct |
23 |
Correct |
151 ms |
1644 KB |
Output is correct |
24 |
Correct |
193 ms |
1388 KB |
Output is correct |
25 |
Correct |
226 ms |
1608 KB |
Output is correct |
26 |
Correct |
235 ms |
1772 KB |
Output is correct |
27 |
Correct |
637 ms |
17772 KB |
Output is correct |
28 |
Correct |
574 ms |
12784 KB |
Output is correct |
29 |
Correct |
504 ms |
17180 KB |
Output is correct |
30 |
Correct |
1293 ms |
31640 KB |
Output is correct |
31 |
Correct |
3 ms |
364 KB |
Output is correct |
32 |
Correct |
1213 ms |
18184 KB |
Output is correct |
33 |
Correct |
204 ms |
139884 KB |
Output is correct |
34 |
Correct |
161 ms |
130796 KB |
Output is correct |
35 |
Correct |
156 ms |
133612 KB |
Output is correct |
36 |
Correct |
162 ms |
124012 KB |
Output is correct |
37 |
Correct |
169 ms |
105324 KB |
Output is correct |
38 |
Correct |
170 ms |
113260 KB |
Output is correct |
39 |
Correct |
73 ms |
13548 KB |
Output is correct |
40 |
Correct |
72 ms |
14724 KB |
Output is correct |
41 |
Correct |
90 ms |
56812 KB |
Output is correct |
42 |
Correct |
128 ms |
71916 KB |
Output is correct |
43 |
Correct |
211 ms |
139932 KB |
Output is correct |
44 |
Correct |
166 ms |
131052 KB |
Output is correct |
45 |
Correct |
161 ms |
133612 KB |
Output is correct |
46 |
Correct |
166 ms |
124012 KB |
Output is correct |
47 |
Correct |
156 ms |
103788 KB |
Output is correct |
48 |
Correct |
166 ms |
113260 KB |
Output is correct |
49 |
Correct |
198 ms |
139756 KB |
Output is correct |
50 |
Correct |
188 ms |
135552 KB |
Output is correct |
51 |
Correct |
197 ms |
135404 KB |
Output is correct |
52 |
Correct |
73 ms |
13548 KB |
Output is correct |
53 |
Correct |
72 ms |
14700 KB |
Output is correct |
54 |
Correct |
86 ms |
56812 KB |
Output is correct |
55 |
Correct |
119 ms |
71916 KB |
Output is correct |
56 |
Correct |
214 ms |
139920 KB |
Output is correct |
57 |
Correct |
159 ms |
130796 KB |
Output is correct |
58 |
Correct |
166 ms |
133700 KB |
Output is correct |
59 |
Correct |
172 ms |
124012 KB |
Output is correct |
60 |
Correct |
154 ms |
103660 KB |
Output is correct |
61 |
Correct |
174 ms |
113260 KB |
Output is correct |
62 |
Correct |
206 ms |
139756 KB |
Output is correct |
63 |
Correct |
185 ms |
135404 KB |
Output is correct |
64 |
Correct |
185 ms |
135404 KB |
Output is correct |
65 |
Correct |
170 ms |
8684 KB |
Output is correct |
66 |
Correct |
217 ms |
12140 KB |
Output is correct |
67 |
Correct |
249 ms |
12044 KB |
Output is correct |
68 |
Correct |
183 ms |
11628 KB |
Output is correct |
69 |
Correct |
209 ms |
11884 KB |
Output is correct |
70 |
Correct |
75 ms |
10092 KB |
Output is correct |
71 |
Correct |
211 ms |
12140 KB |
Output is correct |
72 |
Correct |
247 ms |
12268 KB |
Output is correct |
73 |
Correct |
194 ms |
11628 KB |
Output is correct |
74 |
Correct |
136 ms |
11244 KB |
Output is correct |
75 |
Correct |
169 ms |
12012 KB |
Output is correct |
76 |
Correct |
147 ms |
12012 KB |
Output is correct |
77 |
Correct |
146 ms |
11500 KB |
Output is correct |
78 |
Correct |
174 ms |
9964 KB |
Output is correct |
79 |
Correct |
352 ms |
17096 KB |
Output is correct |
80 |
Correct |
385 ms |
18156 KB |
Output is correct |
81 |
Correct |
383 ms |
60268 KB |
Output is correct |
82 |
Correct |
677 ms |
75372 KB |
Output is correct |
83 |
Correct |
546 ms |
143596 KB |
Output is correct |
84 |
Correct |
1516 ms |
134532 KB |
Output is correct |
85 |
Correct |
708 ms |
137196 KB |
Output is correct |
86 |
Correct |
848 ms |
127628 KB |
Output is correct |
87 |
Correct |
485 ms |
107364 KB |
Output is correct |
88 |
Correct |
402 ms |
116780 KB |
Output is correct |
89 |
Correct |
414 ms |
143212 KB |
Output is correct |
90 |
Correct |
1055 ms |
138980 KB |
Output is correct |
91 |
Correct |
1401 ms |
138940 KB |
Output is correct |