답안 #895298

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895298 2023-12-29T18:02:20 Z rainboy 조화행렬 (KOI18_matrix) C
100 / 100
3032 ms 101620 KB
#include <stdio.h>

#define M	3
#define N	200000
#define L	18	/* L = ceil(log2(N + 1)) */
#define N_	(1 << L)
#define N1	(N * (L + 1) + 1)

int max(int a, int b) { return a > b ? a : b; }

unsigned int Z = 12345;

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

int *xx_;

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 (xx_[ii[j]] == xx_[i_])
				j++;
			else if (xx_[ii[j]] < xx_[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;
	}
}

int ft[N];

void ft_update(int i, int n, int x) {
	while (i < n) {
		ft[i] = max(ft[i], x);
		i |= i + 1;
	}
}

int ft_query(int i) {
	int x = 0;

	while (i >= 0) {
		x = max(x, ft[i]);
		i &= i + 1, i--;
	}
	return x;
}

int data[N1][6], l_, r_;

int node(int y, int x) {
	static int _ = 1;

	data[_][0] = rand_();
	data[_][3] = y, data[_][5] = data[_][4] = x;
	return _++;
}

void pul(int u) {
	data[u][5] = max(data[u][4], max(data[data[u][1]][5], data[data[u][2]][5]));
}

void split(int u, int y) {
	if (u == 0) {
		l_ = r_ = 0;
		return;
	}
	if (data[u][3] < y) {
		split(data[u][2], y);
		data[u][2] = l_, l_ = u;
	} else if (data[u][3] > y) {
		split(data[u][1], y);
		data[u][1] = r_, r_ = u;
	} else {
		l_ = data[u][1], r_ = data[u][2];
		data[u][1] = data[u][2] = 0;
	}
	pul(u);
}

int merge(int u, int v) {
	if (u == 0)
		return v;
	if (v == 0)
		return u;
	if (data[u][0] < data[v][0]) {
		data[u][2] = merge(data[u][2], v), pul(u);
		return u;
	} else {
		data[v][1] = merge(u, data[v][1]), pul(v);
		return v;
	}
}

int tr_update(int u, int y, int x) {
	split(u, y);
	return merge(merge(l_, node(y, x)), r_);
}

int tr_query(int u, int y) {
	int x = 0;

	while (u)
		if (data[u][3] <= y)
			x = max(max(x, data[u][4]), data[data[u][1]][5]), u = data[u][2];
		else
			u = data[u][1];
	return x;
}

int uu[N_ * 2], n_;

void st_update(int i, int y, int x) {
	for (i += n_; i > 0; i >>= 1)
		uu[i] = tr_update(uu[i], y, x);
}

int st_query(int r, int y) {
	int l = 0, x = 0;

	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
		if ((r & 1) == 0)
			x = max(x, tr_query(uu[r--], y));
	return x;
}

int main() {
	static int ii[N], xx[M][N];
	int n, m, h, i, x, y;

	scanf("%d%d", &m, &n);
	for (h = 0; h < m; h++) {
		for (i = 0; i < n; i++)
			scanf("%d", &xx[h][i]);
		for (i = 0; i < n; i++)
			ii[i] = i;
		xx_ = xx[h], sort(ii, 0, n);
		for (i = 0; i < n; i++)
			xx[h][ii[i]] = i;
	}
	if (m == 2) {
		for (h = 0; h < n; h++) {
			x = xx[0][ii[h]];
			ft_update(x, n, ft_query(x) + 1);
		}
		printf("%d\n", ft_query(n - 1));
	} else {
		n_ = 1;
		while (n_ <= n)
			n_ <<= 1;
		for (h = 0; h < n; h++) {
			x = xx[0][ii[h]], y = xx[1][ii[h]];
			st_update(x, y, st_query(x, y) + 1);
		}
		printf("%d\n", st_query(n - 1, n - 1));
	}
	return 0;
}

Compilation message

matrix.c: In function 'main':
matrix.c:139:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |  scanf("%d%d", &m, &n);
      |  ^~~~~~~~~~~~~~~~~~~~~
matrix.c:142:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  142 |    scanf("%d", &xx[h][i]);
      |    ^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2392 KB Output is correct
2 Correct 4 ms 2396 KB Output is correct
3 Correct 4 ms 2396 KB Output is correct
4 Correct 4 ms 2396 KB Output is correct
5 Correct 4 ms 2480 KB Output is correct
6 Correct 4 ms 2396 KB Output is correct
7 Correct 4 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 8780 KB Output is correct
2 Correct 24 ms 8784 KB Output is correct
3 Correct 36 ms 8532 KB Output is correct
4 Correct 41 ms 8780 KB Output is correct
5 Correct 31 ms 8748 KB Output is correct
6 Correct 18 ms 8796 KB Output is correct
7 Correct 39 ms 8532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2392 KB Output is correct
2 Correct 4 ms 2396 KB Output is correct
3 Correct 4 ms 2396 KB Output is correct
4 Correct 4 ms 2396 KB Output is correct
5 Correct 4 ms 2480 KB Output is correct
6 Correct 4 ms 2396 KB Output is correct
7 Correct 4 ms 2396 KB Output is correct
8 Correct 94 ms 3408 KB Output is correct
9 Correct 94 ms 3384 KB Output is correct
10 Correct 91 ms 3368 KB Output is correct
11 Correct 93 ms 3388 KB Output is correct
12 Correct 93 ms 3376 KB Output is correct
13 Correct 90 ms 3408 KB Output is correct
14 Correct 92 ms 3312 KB Output is correct
15 Correct 91 ms 3420 KB Output is correct
16 Correct 95 ms 3408 KB Output is correct
17 Correct 90 ms 3412 KB Output is correct
18 Correct 92 ms 3392 KB Output is correct
19 Correct 92 ms 3312 KB Output is correct
20 Correct 98 ms 3408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 8780 KB Output is correct
2 Correct 24 ms 8784 KB Output is correct
3 Correct 36 ms 8532 KB Output is correct
4 Correct 41 ms 8780 KB Output is correct
5 Correct 31 ms 8748 KB Output is correct
6 Correct 18 ms 8796 KB Output is correct
7 Correct 39 ms 8532 KB Output is correct
8 Correct 932 ms 95332 KB Output is correct
9 Correct 1110 ms 95552 KB Output is correct
10 Correct 698 ms 95600 KB Output is correct
11 Correct 522 ms 95316 KB Output is correct
12 Correct 2103 ms 95056 KB Output is correct
13 Correct 821 ms 95572 KB Output is correct
14 Correct 1186 ms 95536 KB Output is correct
15 Correct 595 ms 95064 KB Output is correct
16 Correct 594 ms 95260 KB Output is correct
17 Correct 1167 ms 95336 KB Output is correct
18 Correct 3032 ms 95128 KB Output is correct
19 Correct 1302 ms 100788 KB Output is correct
20 Correct 1026 ms 101180 KB Output is correct
21 Correct 612 ms 100864 KB Output is correct
22 Correct 2453 ms 101620 KB Output is correct
23 Correct 1113 ms 101088 KB Output is correct
24 Correct 1264 ms 100716 KB Output is correct
25 Correct 1526 ms 101096 KB Output is correct
26 Correct 834 ms 101052 KB Output is correct
27 Correct 2180 ms 101360 KB Output is correct
28 Correct 620 ms 100940 KB Output is correct
29 Correct 1111 ms 101164 KB Output is correct
30 Correct 1202 ms 101180 KB Output is correct
31 Correct 838 ms 101172 KB Output is correct
32 Correct 1142 ms 100944 KB Output is correct