답안 #1101134

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1101134 2024-10-15T15:22:28 Z rainboy 트리 (IOI24_tree) C++17
100 / 100
182 ms 57492 KB
#include "tree.h"
#include <cstdlib>
#include <cstring>

using namespace std;

typedef vector<int> vi;

const int N = 200000, M = N * 4;

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

unsigned int Z = 12345;

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

int pp[N], ww[N];
int *ej[N], eo[N], ii[N], n;

void append(int i, int j) {
	int o = eo[i]++;
	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

int *vv;

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 (vv[ii[j]] == vv[i_])
				j++;
			else if (vv[ii[j]] < vv[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 tt[M]; long long aa[M], bb[M];
int tt_[M]; long long aa_[M], bb_[M];
int hh[M], m;

char active[N]; int ds[N], ss[N], l, r;

int find(int i) {
	return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}

void join(int i, int j, int w) {
	i = find(i);
	j = find(j);
	if (i == j)
		return;
	tt[m] = ss[i], aa[m] = (long long) -(ss[i] + 1) * w, bb[m] = w, m++;
	tt[m] = ss[j], aa[m] = (long long) -(ss[j] + 1) * w, bb[m] = w, m++;
	tt[m] = ss[i] + ss[j], aa[m] = (long long) (ss[i] + ss[j] + 1) * w, bb[m] = -w, m++;
	if (ds[i] > ds[j])
		ds[i] = j, ss[j] += ss[i];
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i, ss[i] += ss[j];
	}
}

long long c;

void init(vi pp_, vi ww_) {
	n = pp_.size();
	for (int i = 0; i < n; i++)
		pp[i] = pp_[i], ww[i] = ww_[i];
	for (int i = 0; i < n; i++)
		ii[i] = i;
	vv = ww, sort(ii, 0, n);
	for (int i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (int j = 1; j < n; j++)
		append(pp[j], j);
	c = 0;
	for (int i = 0; i < n; i++)
		if (eo[i] == 0)
			c += ww[i];
	memset(active, 0, n * sizeof *active), memset(ds, -1, n * sizeof *ds);
	for (int i = 0; i < n; i++)
		ss[i] = max(eo[i] - 1, 0);
	for (int h = n - 1; h >= 0; h--) {
		int i = ii[h], w = ww[i];
		active[i] = 1;
		tt[m] = ss[i], aa[m] = (long long) (ss[i] + 1) * w, bb[m] = -w, m++;
		if (i > 0 && active[pp[i]] == 1)
			join(i, pp[i], w);
		for (int o = eo[i]; o--; ) {
			int j = ej[i][o];
			if (active[j] == 1)
				join(i, j, w);
		}
	}
	for (int h = 0; h < m; h++)
		hh[h] = h;
	vv = tt, sort(hh, 0, m);
	for (int h = m - 1; h >= 0; h--) {
		int h_ = hh[h];
		tt_[h] = tt[h_], aa_[h] = aa_[h + 1] + aa[h_], bb_[h] = bb_[h + 1] + bb[h_];
	}
}

long long query(int l, int r) {
	int lower = -1, upper = m;
	while (upper - lower > 1) {
		int h = (lower + upper) / 2;
		if (tt_[h] >= r / l)
			upper = h;
		else
			lower = h;
	}
	return aa_[upper] * l + bb_[upper] * r + c * l;
}

Compilation message

tree.cpp: In function 'void append(int, int)':
tree.cpp:24:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   24 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 53448 KB Output is correct
2 Correct 60 ms 52648 KB Output is correct
3 Correct 102 ms 53812 KB Output is correct
4 Correct 101 ms 53320 KB Output is correct
5 Correct 135 ms 53168 KB Output is correct
6 Correct 60 ms 52296 KB Output is correct
7 Correct 124 ms 53228 KB Output is correct
8 Correct 92 ms 53156 KB Output is correct
9 Correct 115 ms 53064 KB Output is correct
10 Correct 68 ms 53164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16980 KB Output is correct
2 Correct 3 ms 16980 KB Output is correct
3 Correct 3 ms 16976 KB Output is correct
4 Correct 4 ms 16976 KB Output is correct
5 Correct 4 ms 17088 KB Output is correct
6 Correct 3 ms 16976 KB Output is correct
7 Correct 3 ms 16980 KB Output is correct
8 Correct 4 ms 16980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16980 KB Output is correct
2 Correct 3 ms 16980 KB Output is correct
3 Correct 3 ms 16976 KB Output is correct
4 Correct 4 ms 16976 KB Output is correct
5 Correct 4 ms 17088 KB Output is correct
6 Correct 3 ms 16976 KB Output is correct
7 Correct 3 ms 16980 KB Output is correct
8 Correct 4 ms 16980 KB Output is correct
9 Correct 25 ms 34756 KB Output is correct
10 Correct 35 ms 34680 KB Output is correct
11 Correct 40 ms 34636 KB Output is correct
12 Correct 27 ms 34672 KB Output is correct
13 Correct 36 ms 34632 KB Output is correct
14 Correct 28 ms 34640 KB Output is correct
15 Correct 46 ms 34632 KB Output is correct
16 Correct 32 ms 34640 KB Output is correct
17 Correct 31 ms 34640 KB Output is correct
18 Correct 46 ms 34580 KB Output is correct
19 Correct 40 ms 34668 KB Output is correct
20 Correct 32 ms 34640 KB Output is correct
21 Correct 35 ms 34696 KB Output is correct
22 Correct 28 ms 34896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 54820 KB Output is correct
2 Correct 139 ms 54824 KB Output is correct
3 Correct 95 ms 54836 KB Output is correct
4 Correct 89 ms 54832 KB Output is correct
5 Correct 117 ms 54824 KB Output is correct
6 Correct 105 ms 54560 KB Output is correct
7 Correct 83 ms 54832 KB Output is correct
8 Correct 68 ms 54824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 54820 KB Output is correct
2 Correct 139 ms 54824 KB Output is correct
3 Correct 95 ms 54836 KB Output is correct
4 Correct 89 ms 54832 KB Output is correct
5 Correct 117 ms 54824 KB Output is correct
6 Correct 105 ms 54560 KB Output is correct
7 Correct 83 ms 54832 KB Output is correct
8 Correct 68 ms 54824 KB Output is correct
9 Correct 112 ms 55336 KB Output is correct
10 Correct 116 ms 54728 KB Output is correct
11 Correct 120 ms 54836 KB Output is correct
12 Correct 115 ms 54824 KB Output is correct
13 Correct 102 ms 54568 KB Output is correct
14 Correct 104 ms 54692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 53560 KB Output is correct
2 Correct 151 ms 55564 KB Output is correct
3 Correct 132 ms 55336 KB Output is correct
4 Correct 136 ms 55276 KB Output is correct
5 Correct 123 ms 55340 KB Output is correct
6 Correct 116 ms 55252 KB Output is correct
7 Correct 126 ms 55216 KB Output is correct
8 Correct 106 ms 55084 KB Output is correct
9 Correct 138 ms 55336 KB Output is correct
10 Correct 137 ms 55328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14816 KB Output is correct
2 Correct 94 ms 53448 KB Output is correct
3 Correct 60 ms 52648 KB Output is correct
4 Correct 102 ms 53812 KB Output is correct
5 Correct 101 ms 53320 KB Output is correct
6 Correct 135 ms 53168 KB Output is correct
7 Correct 60 ms 52296 KB Output is correct
8 Correct 124 ms 53228 KB Output is correct
9 Correct 92 ms 53156 KB Output is correct
10 Correct 115 ms 53064 KB Output is correct
11 Correct 68 ms 53164 KB Output is correct
12 Correct 3 ms 16980 KB Output is correct
13 Correct 3 ms 16980 KB Output is correct
14 Correct 3 ms 16976 KB Output is correct
15 Correct 4 ms 16976 KB Output is correct
16 Correct 4 ms 17088 KB Output is correct
17 Correct 3 ms 16976 KB Output is correct
18 Correct 3 ms 16980 KB Output is correct
19 Correct 4 ms 16980 KB Output is correct
20 Correct 25 ms 34756 KB Output is correct
21 Correct 35 ms 34680 KB Output is correct
22 Correct 40 ms 34636 KB Output is correct
23 Correct 27 ms 34672 KB Output is correct
24 Correct 36 ms 34632 KB Output is correct
25 Correct 28 ms 34640 KB Output is correct
26 Correct 46 ms 34632 KB Output is correct
27 Correct 32 ms 34640 KB Output is correct
28 Correct 31 ms 34640 KB Output is correct
29 Correct 46 ms 34580 KB Output is correct
30 Correct 40 ms 34668 KB Output is correct
31 Correct 32 ms 34640 KB Output is correct
32 Correct 35 ms 34696 KB Output is correct
33 Correct 28 ms 34896 KB Output is correct
34 Correct 90 ms 54820 KB Output is correct
35 Correct 139 ms 54824 KB Output is correct
36 Correct 95 ms 54836 KB Output is correct
37 Correct 89 ms 54832 KB Output is correct
38 Correct 117 ms 54824 KB Output is correct
39 Correct 105 ms 54560 KB Output is correct
40 Correct 83 ms 54832 KB Output is correct
41 Correct 68 ms 54824 KB Output is correct
42 Correct 112 ms 55336 KB Output is correct
43 Correct 116 ms 54728 KB Output is correct
44 Correct 120 ms 54836 KB Output is correct
45 Correct 115 ms 54824 KB Output is correct
46 Correct 102 ms 54568 KB Output is correct
47 Correct 104 ms 54692 KB Output is correct
48 Correct 80 ms 53560 KB Output is correct
49 Correct 151 ms 55564 KB Output is correct
50 Correct 132 ms 55336 KB Output is correct
51 Correct 136 ms 55276 KB Output is correct
52 Correct 123 ms 55340 KB Output is correct
53 Correct 116 ms 55252 KB Output is correct
54 Correct 126 ms 55216 KB Output is correct
55 Correct 106 ms 55084 KB Output is correct
56 Correct 138 ms 55336 KB Output is correct
57 Correct 137 ms 55328 KB Output is correct
58 Correct 91 ms 55716 KB Output is correct
59 Correct 109 ms 55528 KB Output is correct
60 Correct 134 ms 57492 KB Output is correct
61 Correct 129 ms 56728 KB Output is correct
62 Correct 136 ms 56408 KB Output is correct
63 Correct 182 ms 56360 KB Output is correct
64 Correct 133 ms 56356 KB Output is correct
65 Correct 156 ms 56356 KB Output is correct
66 Correct 128 ms 56372 KB Output is correct
67 Correct 152 ms 56244 KB Output is correct
68 Correct 123 ms 56104 KB Output is correct
69 Correct 143 ms 56356 KB Output is correct
70 Correct 167 ms 56356 KB Output is correct
71 Correct 102 ms 56232 KB Output is correct