#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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
492 KB |
Output is correct |
5 |
Correct |
4 ms |
748 KB |
Output is correct |
6 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
184 ms |
8680 KB |
Output is correct |
4 |
Correct |
235 ms |
12140 KB |
Output is correct |
5 |
Correct |
209 ms |
12140 KB |
Output is correct |
6 |
Correct |
222 ms |
11656 KB |
Output is correct |
7 |
Correct |
210 ms |
12012 KB |
Output is correct |
8 |
Correct |
75 ms |
10092 KB |
Output is correct |
9 |
Correct |
215 ms |
12140 KB |
Output is correct |
10 |
Correct |
226 ms |
12140 KB |
Output is correct |
11 |
Correct |
245 ms |
11528 KB |
Output is correct |
12 |
Correct |
141 ms |
11244 KB |
Output is correct |
13 |
Correct |
152 ms |
12032 KB |
Output is correct |
14 |
Correct |
150 ms |
12012 KB |
Output is correct |
15 |
Correct |
144 ms |
11500 KB |
Output is correct |
16 |
Correct |
191 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
218 ms |
139884 KB |
Output is correct |
3 |
Correct |
155 ms |
130796 KB |
Output is correct |
4 |
Correct |
160 ms |
133612 KB |
Output is correct |
5 |
Correct |
164 ms |
124012 KB |
Output is correct |
6 |
Correct |
154 ms |
105196 KB |
Output is correct |
7 |
Correct |
173 ms |
113292 KB |
Output is correct |
8 |
Correct |
74 ms |
13676 KB |
Output is correct |
9 |
Correct |
74 ms |
14700 KB |
Output is correct |
10 |
Correct |
85 ms |
56812 KB |
Output is correct |
11 |
Correct |
117 ms |
71916 KB |
Output is correct |
12 |
Correct |
204 ms |
139888 KB |
Output is correct |
13 |
Correct |
166 ms |
130796 KB |
Output is correct |
14 |
Correct |
158 ms |
133612 KB |
Output is correct |
15 |
Correct |
191 ms |
124012 KB |
Output is correct |
16 |
Correct |
158 ms |
103660 KB |
Output is correct |
17 |
Correct |
167 ms |
113260 KB |
Output is correct |
18 |
Correct |
199 ms |
139756 KB |
Output is correct |
19 |
Correct |
198 ms |
135404 KB |
Output is correct |
20 |
Correct |
181 ms |
135404 KB |
Output is correct |
21 |
Correct |
73 ms |
13548 KB |
Output is correct |
22 |
Correct |
73 ms |
14700 KB |
Output is correct |
23 |
Correct |
85 ms |
56712 KB |
Output is correct |
24 |
Correct |
118 ms |
71868 KB |
Output is correct |
25 |
Correct |
204 ms |
140012 KB |
Output is correct |
26 |
Correct |
159 ms |
130924 KB |
Output is correct |
27 |
Correct |
169 ms |
133740 KB |
Output is correct |
28 |
Correct |
163 ms |
124012 KB |
Output is correct |
29 |
Correct |
163 ms |
103792 KB |
Output is correct |
30 |
Correct |
175 ms |
113260 KB |
Output is correct |
31 |
Correct |
211 ms |
139884 KB |
Output is correct |
32 |
Correct |
200 ms |
135404 KB |
Output is correct |
33 |
Correct |
193 ms |
135432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
492 KB |
Output is correct |
5 |
Correct |
4 ms |
748 KB |
Output is correct |
6 |
Correct |
0 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 |
1573 ms |
22892 KB |
Output is correct |
19 |
Correct |
175 ms |
4716 KB |
Output is correct |
20 |
Correct |
187 ms |
3948 KB |
Output is correct |
21 |
Correct |
180 ms |
4076 KB |
Output is correct |
22 |
Correct |
209 ms |
4332 KB |
Output is correct |
23 |
Correct |
153 ms |
4460 KB |
Output is correct |
24 |
Correct |
215 ms |
4204 KB |
Output is correct |
25 |
Correct |
224 ms |
4272 KB |
Output is correct |
26 |
Correct |
226 ms |
4460 KB |
Output is correct |
27 |
Correct |
662 ms |
20844 KB |
Output is correct |
28 |
Correct |
537 ms |
15724 KB |
Output is correct |
29 |
Correct |
590 ms |
19820 KB |
Output is correct |
30 |
Correct |
1295 ms |
34540 KB |
Output is correct |
31 |
Correct |
3 ms |
492 KB |
Output is correct |
32 |
Correct |
1284 ms |
20588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
492 KB |
Output is correct |
5 |
Correct |
4 ms |
748 KB |
Output is correct |
6 |
Correct |
0 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 |
1573 ms |
22892 KB |
Output is correct |
19 |
Correct |
175 ms |
4716 KB |
Output is correct |
20 |
Correct |
187 ms |
3948 KB |
Output is correct |
21 |
Correct |
180 ms |
4076 KB |
Output is correct |
22 |
Correct |
209 ms |
4332 KB |
Output is correct |
23 |
Correct |
153 ms |
4460 KB |
Output is correct |
24 |
Correct |
215 ms |
4204 KB |
Output is correct |
25 |
Correct |
224 ms |
4272 KB |
Output is correct |
26 |
Correct |
226 ms |
4460 KB |
Output is correct |
27 |
Correct |
662 ms |
20844 KB |
Output is correct |
28 |
Correct |
537 ms |
15724 KB |
Output is correct |
29 |
Correct |
590 ms |
19820 KB |
Output is correct |
30 |
Correct |
1295 ms |
34540 KB |
Output is correct |
31 |
Correct |
3 ms |
492 KB |
Output is correct |
32 |
Correct |
1284 ms |
20588 KB |
Output is correct |
33 |
Correct |
218 ms |
139884 KB |
Output is correct |
34 |
Correct |
155 ms |
130796 KB |
Output is correct |
35 |
Correct |
160 ms |
133612 KB |
Output is correct |
36 |
Correct |
164 ms |
124012 KB |
Output is correct |
37 |
Correct |
154 ms |
105196 KB |
Output is correct |
38 |
Correct |
173 ms |
113292 KB |
Output is correct |
39 |
Correct |
74 ms |
13676 KB |
Output is correct |
40 |
Correct |
74 ms |
14700 KB |
Output is correct |
41 |
Correct |
85 ms |
56812 KB |
Output is correct |
42 |
Correct |
117 ms |
71916 KB |
Output is correct |
43 |
Correct |
204 ms |
139888 KB |
Output is correct |
44 |
Correct |
166 ms |
130796 KB |
Output is correct |
45 |
Correct |
158 ms |
133612 KB |
Output is correct |
46 |
Correct |
191 ms |
124012 KB |
Output is correct |
47 |
Correct |
158 ms |
103660 KB |
Output is correct |
48 |
Correct |
167 ms |
113260 KB |
Output is correct |
49 |
Correct |
199 ms |
139756 KB |
Output is correct |
50 |
Correct |
198 ms |
135404 KB |
Output is correct |
51 |
Correct |
181 ms |
135404 KB |
Output is correct |
52 |
Correct |
73 ms |
13548 KB |
Output is correct |
53 |
Correct |
73 ms |
14700 KB |
Output is correct |
54 |
Correct |
85 ms |
56712 KB |
Output is correct |
55 |
Correct |
118 ms |
71868 KB |
Output is correct |
56 |
Correct |
204 ms |
140012 KB |
Output is correct |
57 |
Correct |
159 ms |
130924 KB |
Output is correct |
58 |
Correct |
169 ms |
133740 KB |
Output is correct |
59 |
Correct |
163 ms |
124012 KB |
Output is correct |
60 |
Correct |
163 ms |
103792 KB |
Output is correct |
61 |
Correct |
175 ms |
113260 KB |
Output is correct |
62 |
Correct |
211 ms |
139884 KB |
Output is correct |
63 |
Correct |
200 ms |
135404 KB |
Output is correct |
64 |
Correct |
193 ms |
135432 KB |
Output is correct |
65 |
Correct |
184 ms |
8680 KB |
Output is correct |
66 |
Correct |
235 ms |
12140 KB |
Output is correct |
67 |
Correct |
209 ms |
12140 KB |
Output is correct |
68 |
Correct |
222 ms |
11656 KB |
Output is correct |
69 |
Correct |
210 ms |
12012 KB |
Output is correct |
70 |
Correct |
75 ms |
10092 KB |
Output is correct |
71 |
Correct |
215 ms |
12140 KB |
Output is correct |
72 |
Correct |
226 ms |
12140 KB |
Output is correct |
73 |
Correct |
245 ms |
11528 KB |
Output is correct |
74 |
Correct |
141 ms |
11244 KB |
Output is correct |
75 |
Correct |
152 ms |
12032 KB |
Output is correct |
76 |
Correct |
150 ms |
12012 KB |
Output is correct |
77 |
Correct |
144 ms |
11500 KB |
Output is correct |
78 |
Correct |
191 ms |
9984 KB |
Output is correct |
79 |
Correct |
352 ms |
17104 KB |
Output is correct |
80 |
Correct |
380 ms |
18284 KB |
Output is correct |
81 |
Correct |
375 ms |
60204 KB |
Output is correct |
82 |
Correct |
695 ms |
75340 KB |
Output is correct |
83 |
Correct |
583 ms |
143340 KB |
Output is correct |
84 |
Correct |
1594 ms |
134560 KB |
Output is correct |
85 |
Correct |
794 ms |
137196 KB |
Output is correct |
86 |
Correct |
863 ms |
127580 KB |
Output is correct |
87 |
Correct |
467 ms |
107244 KB |
Output is correct |
88 |
Correct |
429 ms |
116844 KB |
Output is correct |
89 |
Correct |
417 ms |
143340 KB |
Output is correct |
90 |
Correct |
1134 ms |
138988 KB |
Output is correct |
91 |
Correct |
1471 ms |
139328 KB |
Output is correct |