Submission #671361

# Submission time Handle Problem Language Result Execution time Memory
671361 2022-12-12T22:32:00 Z rainboy Hamburg Steak (JOI20_hamburg) C
21 / 100
3000 ms 163804 KB
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#define N	200000
#define LN	18	/* N_ = pow2(ceil(log2(N))) */
#define N_	(N * (LN + 1) + 1)
#define K	4
#define INF	0x3f3f3f3f3f3f3f3fLL

long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

long long xxl[N], xxr[N], yyl[N], yyr[N], xx[K], yy[K]; int n, k;

long long *zz;

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (zz[ii[j]] == zz[i_])
				j++;
			else if (zz[ii[j]] < zz[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}

long long *xl1, *xr1, *yl1, *yr1;

int ll_[N_], rr_[N_]; long long xxl_[N_], xxr_[N_], yyl_[N_], yyr_[N_];

int update(int t, int l, int r, int i, int i_) {
	static int _ = 1;
	int t_ = _++;

	ll_[t_] = ll_[t], rr_[t_] = rr_[t], xxl_[t_] = min(xxl_[t], xxr[i_]), xxr_[t_] = max(xxr_[t], xxl[i_]), yyl_[t_] = min(yyl_[t], yyr[i_]), yyr_[t_] = max(yyr_[t], yyl[i_]);
	if (r - l > 1) {
		int m = (l + r) / 2;

		if (i < m)
			ll_[t_] = update(ll_[t_], l, m, i, i_);
		else
			rr_[t_] = update(rr_[t_], m, r, i, i_);
	}
	return t_;
}

void query(int t, int l, int r, int ql, int qr) {
	int m;

	if (qr <= l || r <= ql || t == 0)
		return;
	if (ql <= l && r <= qr) {
		*xl1 = min(*xl1, xxl_[t]), *xr1 = max(*xr1, xxr_[t]), *yl1 = min(*yl1, yyl_[t]), *yr1 = max(*yr1, yyr_[t]);
		return;
	}
	m = (l + r) / 2;
	query(ll_[t], l, m, ql, qr), query(rr_[t], m, r, ql, qr);
}

int pierced(int i, int h_) {
	int h;

	for (h = 0; h < h_; h++)
		if (xxl[i] <= xx[h] && xx[h] <= xxr[i] && yyl[i] <= yy[h] && yy[h] <= yyr[i])
			return 1;
	return 0;
}

void solve(int h_) {
	int h, i;
	long long xl, xr, yl, yr;

	xl = INF, xr = -1, yl = INF, yr = -1;
	for (i = 0; i < n; i++)
		if (!pierced(i, h_))
			xl = min(xl, xxr[i]), xr = max(xr, xxl[i]), yl = min(yl, yyr[i]), yr = max(yr, yyl[i]);
	if (xl >= xr && yl >= yr) {
		xx[h_] = xl, yy[h_] = yl;
		for (h = h_ + 1; h < k; h++)
			xx[h] = 1, yy[h] = 1;
		for (h = 0; h < k; h++)
			printf("%lld %lld\n", xx[h], yy[h]);
		exit(0);
	} else if (h_ == k - 2) {
		xx[h_] = xl, yy[h_] = yl, solve(h_ + 1);
		xx[h_] = xl, yy[h_] = yr, solve(h_ + 1);
	} else if (h_ <= k - 3) {
		xx[h_] = xl, yy[h_] = yl, solve(h_ + 1);
		xx[h_] = xl, yy[h_] = yr, solve(h_ + 1);
		xx[h_] = xr, yy[h_] = yl, solve(h_ + 1);
		xx[h_] = xr, yy[h_] = yr, solve(h_ + 1);
	}
}

long long ll[N], rr[N], rr2[3][N]; int rr1[N];
int ii[3][N], nn[3], nn_[3], tt[3][N + 1]; long long xl, xr, yl, yr, z1, z2, z3, z4;

void query_(int g, long long l, long long r) {
	int lower, upper, l_, r_;

	lower = -1, upper = nn[g];
	while (upper - lower > 1) {
		int i = (lower + upper) / 2;

		if (ll[ii[g][i]] >= l)
			upper = i;
		else
			lower = i;
	}
	l_ = upper;
	lower = -1, upper = nn_[g];
	while (upper - lower > 1) {
		int i = (lower + upper) / 2;

		if (rr2[g][i] <= r)
			lower = i;
		else
			upper = i;
	}
	r_ = upper;
	query(tt[g][l_], 0, nn_[g], 0, r_);
}

void query1(long long *xx, int n, int g) {
	static int ii_[K + 2];
	int i;

	for (i = 0; i < n; i++)
		ii_[i] = i;
	zz = xx, sort(ii_, 0, n);
	for (i = 0; i + 1 < n; i++)
		query_(g, xx[ii_[i]] + 1, xx[ii_[i + 1]] - 1);
}

long long idx(long long x, long long y) {
	if (y == yl)
		return x - xl;
	if (x == xr)
		return z1 + y - yl;
	if (y == yr)
		return z2 + xr - x;
	if (x == xl)
		return z3 + yr - y;
	return -1;
}

void get_bounds(int h_) {
	static long long xx_[K + 2];
	int h;
	long long x_;

	if (h_ == 0) {
		*xl1 = xl, *xr1 = xr, *yl1 = yl, *yr1 = yr;
		return;
	}
	*xl1 = INF, *xr1 = -1, *yl1 = INF, *yr1 = -1;
	xx_[0] = -1;
	x_ = z4;
	for (h = 0; h < h_; h++)
		x_ = min(x_, xx_[h + 1] = idx(xx[h], yy[h]));
	xx_[h_ + 1] = x_ + z4;
	query1(xx_, h_ + 2, 0);
	xx_[0] = -1;
	for (h = 0; h < h_; h++)
		xx_[h + 1] = xx[h];
	xx_[h_ + 1] = INF;
	query1(xx_, h_ + 2, 1);
	xx_[0] = -1;
	for (h = 0; h < h_; h++)
		xx_[h + 1] = yy[h];
	xx_[h_ + 1] = INF;
	query1(xx_, h_ + 2, 2);
}

void solve_(int h_) {
	int h, i;
	long long xl_, yl_, xr_, yr_;

	if (h_ > 0 && idx(xx[h_ - 1], yy[h_ - 1]) == -1)
		return;
	xl1 = &xl_, xr1 = &xr_, yl1 = &yl_, yr1 = &yr_, get_bounds(h_);
	if (xl_ >= xr_ && yl_ >= yr_) {
		xx[h_] = xl_, yy[h_] = yl_;
		for (h = h_ + 1; h < k; h++)
			xx[h] = 1, yy[h] = 1;
		for (h = 0; h < k; h++)
			printf("%lld %lld\n", xx[h], yy[h]);
		exit(0);
	} else if (h_ == 0) {
		xx[h_] = xl_;
		for (i = 0; i < n; i++)
			yy[h_] = yyl[i], solve_(h_ + 1);
	} else if (h_ == 1) {
		xx[h_] = xl_, yy[h_] = yl_, solve_(h_ + 1);
		xx[h_] = xl_, yy[h_] = yr_, solve_(h_ + 1);
	} else if (h_ == 2) {
		xx[h_] = xl_, yy[h_] = yl_, solve_(h_ + 1);
		xx[h_] = xl_, yy[h_] = yr_, solve_(h_ + 1);
	}
}

int main() {
	int g, i, i_;
	long long l, r;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%lld%lld%lld%lld", &xxl[i], &yyl[i], &xxr[i], &yyr[i]);
	solve(0);
	xl = INF, xr = -1, yl = INF, yr = -1;
	for (i = 0; i < n; i++)
		xl = min(xl, xxr[i]), xr = max(xr, xxl[i]), yl = min(yl, yyr[i]), yr = max(yr, yyl[i]);
	assert(k == 4 && xl < xr && yl < yr);
	z1 = xr - xl, z2 = z1 + yr - yl, z3 = z2 + xr - xl, z4 = z3 + yr - yl;
	for (i = 0; i < n; i++)
		xxl[i] = max(xxl[i], xl), xxr[i] = min(xxr[i], xr), yyl[i] = max(yyl[i], yl), yyr[i] = min(yyr[i], yr);
	for (i = 0; i < n; i++)
		if (xl < xxl[i] && xxr[i] < xr && yyl[i] == yl && yyr[i] == yr)
			ll[i] = xxl[i], rr[i] = xxr[i], ii[1][nn[1]++] = i;
		else if (yl < yyl[i] && yyr[i] < yr && xxl[i] == xl && xxr[i] == xr)
			ll[i] = yyl[i], rr[i] = yyr[i], ii[2][nn[2]++] = i;
		else {
			l = INF, r = -1;
			if (yyl[i] == yl)
				l = min(l, xxl[i] - xl), r = max(r, xxr[i] - xl);
			if (xxr[i] == xr)
				l = min(l, z1 + yyl[i] - yl), r = max(r, z1 + yyr[i] - yl);
			if (xxl[i] == xl && yyl[i] == yl)
				l += z4, r += z4;
			if (yyr[i] == yr)
				l = min(l, z2 + xr - xxr[i]), r = max(r, z2 + xr - xxl[i]);
			if (xxl[i] == xl)
				l = min(l, z3 + yr - yyr[i]), r = max(r, z3 + yr - yyl[i]);
			if (l >= z4)
				l -= z4, r -= z4;
			ll[i] = l, rr[i] = r, ii[0][nn[0]++] = i;
		}
	xxl_[0] = INF, xxr_[0] = -1, yyl_[0] = INF, yyr_[0] = -1;
	for (g = 0; g < 3; g++) {
		zz = rr, sort(ii[g], 0, nn[g]);
		for (i = 0; i < nn[g]; i++) {
			i_ = ii[g][i];
			rr1[i_] = nn_[g];
			if (i + 1 == nn[g] || rr[ii[g][i + 1]] != rr[i_])
				rr2[g][nn_[g]++] = rr[i_];
		}
		zz = ll, sort(ii[g], 0, nn[g]);
		for (i = nn[g] - 1; i >= 0; i--) {
			i_ = ii[g][i];
			tt[g][i] = update(tt[g][i + 1], 0, nn_[g], rr1[i_], i_);
		}
	}
	solve_(0);
	return 0;
}

Compilation message

hamburg.c: In function 'main':
hamburg.c:222:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  222 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
hamburg.c:224:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  224 |   scanf("%lld%lld%lld%lld", &xxl[i], &yyl[i], &xxr[i], &yyr[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 7 ms 1364 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 7 ms 1364 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 10 ms 1492 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 4 ms 1364 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 7 ms 1412 KB Output is correct
32 Correct 3 ms 1364 KB Output is correct
33 Correct 4 ms 1364 KB Output is correct
34 Correct 4 ms 1364 KB Output is correct
35 Correct 4 ms 1364 KB Output is correct
36 Correct 6 ms 1364 KB Output is correct
37 Correct 15 ms 1364 KB Output is correct
38 Correct 5 ms 1364 KB Output is correct
39 Correct 9 ms 1364 KB Output is correct
40 Correct 9 ms 1364 KB Output is correct
41 Correct 10 ms 1364 KB Output is correct
42 Correct 6 ms 1364 KB Output is correct
43 Correct 6 ms 1364 KB Output is correct
44 Correct 9 ms 1364 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 9 ms 1364 KB Output is correct
47 Correct 5 ms 1364 KB Output is correct
48 Correct 4 ms 1364 KB Output is correct
49 Correct 7 ms 1364 KB Output is correct
50 Correct 5 ms 1364 KB Output is correct
51 Correct 13 ms 1364 KB Output is correct
52 Correct 8 ms 1352 KB Output is correct
53 Correct 5 ms 1364 KB Output is correct
54 Correct 6 ms 1364 KB Output is correct
55 Correct 6 ms 1364 KB Output is correct
56 Correct 7 ms 1364 KB Output is correct
57 Correct 6 ms 1396 KB Output is correct
58 Correct 4 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 73 ms 6480 KB Output is correct
6 Correct 77 ms 6452 KB Output is correct
7 Correct 76 ms 6452 KB Output is correct
8 Correct 74 ms 6476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 80 ms 6512 KB Output is correct
6 Correct 79 ms 6572 KB Output is correct
7 Correct 77 ms 6512 KB Output is correct
8 Correct 77 ms 6416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 80 ms 6596 KB Output is correct
14 Correct 82 ms 6464 KB Output is correct
15 Correct 76 ms 6444 KB Output is correct
16 Correct 77 ms 6468 KB Output is correct
17 Correct 77 ms 6500 KB Output is correct
18 Correct 82 ms 6476 KB Output is correct
19 Correct 79 ms 6548 KB Output is correct
20 Correct 80 ms 6508 KB Output is correct
21 Correct 98 ms 6412 KB Output is correct
22 Correct 87 ms 6472 KB Output is correct
23 Correct 83 ms 6444 KB Output is correct
24 Correct 85 ms 6472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 7 ms 1364 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 7 ms 1364 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 10 ms 1492 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 4 ms 1364 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 7 ms 1412 KB Output is correct
32 Correct 3 ms 1364 KB Output is correct
33 Correct 4 ms 1364 KB Output is correct
34 Correct 4 ms 1364 KB Output is correct
35 Correct 4 ms 1364 KB Output is correct
36 Correct 6 ms 1364 KB Output is correct
37 Correct 15 ms 1364 KB Output is correct
38 Correct 5 ms 1364 KB Output is correct
39 Correct 9 ms 1364 KB Output is correct
40 Correct 9 ms 1364 KB Output is correct
41 Correct 10 ms 1364 KB Output is correct
42 Correct 6 ms 1364 KB Output is correct
43 Correct 6 ms 1364 KB Output is correct
44 Correct 9 ms 1364 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 9 ms 1364 KB Output is correct
47 Correct 5 ms 1364 KB Output is correct
48 Correct 4 ms 1364 KB Output is correct
49 Correct 7 ms 1364 KB Output is correct
50 Correct 5 ms 1364 KB Output is correct
51 Correct 13 ms 1364 KB Output is correct
52 Correct 8 ms 1352 KB Output is correct
53 Correct 5 ms 1364 KB Output is correct
54 Correct 6 ms 1364 KB Output is correct
55 Correct 6 ms 1364 KB Output is correct
56 Correct 7 ms 1364 KB Output is correct
57 Correct 6 ms 1396 KB Output is correct
58 Correct 4 ms 1364 KB Output is correct
59 Correct 84 ms 6452 KB Output is correct
60 Correct 81 ms 6420 KB Output is correct
61 Correct 82 ms 6604 KB Output is correct
62 Correct 82 ms 6528 KB Output is correct
63 Correct 80 ms 6412 KB Output is correct
64 Correct 85 ms 6680 KB Output is correct
65 Correct 80 ms 6456 KB Output is correct
66 Correct 84 ms 6476 KB Output is correct
67 Correct 110 ms 6632 KB Output is correct
68 Correct 98 ms 6516 KB Output is correct
69 Correct 88 ms 6484 KB Output is correct
70 Correct 93 ms 6532 KB Output is correct
71 Correct 212 ms 6468 KB Output is correct
72 Correct 1697 ms 155864 KB Output is correct
73 Correct 213 ms 14288 KB Output is correct
74 Correct 149 ms 14276 KB Output is correct
75 Correct 773 ms 163580 KB Output is correct
76 Correct 157 ms 14276 KB Output is correct
77 Correct 187 ms 14284 KB Output is correct
78 Correct 1272 ms 163680 KB Output is correct
79 Correct 207 ms 14252 KB Output is correct
80 Correct 181 ms 14436 KB Output is correct
81 Correct 717 ms 163420 KB Output is correct
82 Correct 166 ms 14188 KB Output is correct
83 Correct 120 ms 14272 KB Output is correct
84 Correct 126 ms 14196 KB Output is correct
85 Correct 154 ms 14228 KB Output is correct
86 Correct 134 ms 14268 KB Output is correct
87 Correct 128 ms 14204 KB Output is correct
88 Correct 137 ms 14272 KB Output is correct
89 Execution timed out 3020 ms 163804 KB Time limit exceeded
90 Halted 0 ms 0 KB -