Submission #379989

# Submission time Handle Problem Language Result Execution time Memory
379989 2021-03-19T21:38:17 Z rainboy Land of the Rainbow Gold (APIO17_rainbow) C
50 / 100
1275 ms 1048576 KB
#include "rainbow.h"
#include <stdlib.h>

#define N	100000
#define L	100000
#define N_	(1 << 17)	/* 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 3 ms 512 KB Output is correct
5 Correct 3 ms 748 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 3 ms 620 KB Output is correct
13 Correct 4 ms 768 KB Output is correct
14 Correct 5 ms 876 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 89 ms 364 KB Output is correct
17 Correct 45 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 364 KB Output is correct
2 Correct 45 ms 364 KB Output is correct
3 Correct 263 ms 8044 KB Output is correct
4 Correct 285 ms 12120 KB Output is correct
5 Correct 255 ms 12012 KB Output is correct
6 Correct 192 ms 11756 KB Output is correct
7 Correct 204 ms 11884 KB Output is correct
8 Correct 81 ms 10092 KB Output is correct
9 Correct 246 ms 12268 KB Output is correct
10 Correct 216 ms 12268 KB Output is correct
11 Correct 199 ms 11628 KB Output is correct
12 Correct 140 ms 11244 KB Output is correct
13 Correct 149 ms 12012 KB Output is correct
14 Correct 149 ms 12148 KB Output is correct
15 Correct 144 ms 11500 KB Output is correct
16 Correct 171 ms 9964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Runtime error 992 ms 1048576 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 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 3 ms 512 KB Output is correct
5 Correct 3 ms 748 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 3 ms 620 KB Output is correct
13 Correct 4 ms 768 KB Output is correct
14 Correct 5 ms 876 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 89 ms 364 KB Output is correct
17 Correct 45 ms 364 KB Output is correct
18 Correct 1275 ms 22832 KB Output is correct
19 Correct 166 ms 4460 KB Output is correct
20 Correct 151 ms 4076 KB Output is correct
21 Correct 177 ms 4076 KB Output is correct
22 Correct 204 ms 4332 KB Output is correct
23 Correct 154 ms 4460 KB Output is correct
24 Correct 195 ms 4204 KB Output is correct
25 Correct 214 ms 4332 KB Output is correct
26 Correct 227 ms 4460 KB Output is correct
27 Correct 586 ms 20588 KB Output is correct
28 Correct 462 ms 15596 KB Output is correct
29 Correct 580 ms 19820 KB Output is correct
30 Correct 1185 ms 34540 KB Output is correct
31 Correct 3 ms 492 KB Output is correct
32 Correct 1125 ms 20716 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 3 ms 512 KB Output is correct
5 Correct 3 ms 748 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 3 ms 620 KB Output is correct
13 Correct 4 ms 768 KB Output is correct
14 Correct 5 ms 876 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 89 ms 364 KB Output is correct
17 Correct 45 ms 364 KB Output is correct
18 Correct 1275 ms 22832 KB Output is correct
19 Correct 166 ms 4460 KB Output is correct
20 Correct 151 ms 4076 KB Output is correct
21 Correct 177 ms 4076 KB Output is correct
22 Correct 204 ms 4332 KB Output is correct
23 Correct 154 ms 4460 KB Output is correct
24 Correct 195 ms 4204 KB Output is correct
25 Correct 214 ms 4332 KB Output is correct
26 Correct 227 ms 4460 KB Output is correct
27 Correct 586 ms 20588 KB Output is correct
28 Correct 462 ms 15596 KB Output is correct
29 Correct 580 ms 19820 KB Output is correct
30 Correct 1185 ms 34540 KB Output is correct
31 Correct 3 ms 492 KB Output is correct
32 Correct 1125 ms 20716 KB Output is correct
33 Runtime error 992 ms 1048576 KB Execution killed with signal 9
34 Halted 0 ms 0 KB -