답안 #1106480

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1106480 2024-10-30T12:41:22 Z Octagons 트리 (IOI24_tree) C++17
59 / 100
2000 ms 50356 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+5;
long long L, R, w[N];
vector<int> adj[N];
int n, pr[N], x[N], in[N], sz[N], idx;
int srtx[N], rv[N];
bool f;
pair<int, ll> srtox[N];
ll srtx2[N];

struct node {
	pair<int, int> mn;
	ll sm;
} t[(1 << 19)];

void build(int l, int r, int x) {
	if (l == r) {
		t[x].sm = 0;
		t[x].mn = {w[rv[l]], l};
		if (adj[rv[l]].empty())t[x].mn = {int(1e9), int(1e9)};
		return;
	}
	int m = (l + r) >> 1;
	build(l, m, x*2+1);
	build(m+1, r, x*2+2);
	t[x].sm = 0;
	t[x].mn = min(t[x*2+1].mn, t[x*2+2].mn);
}

void upd(int l, int r, int x, int i, ll v) {
	t[x].sm -= v;
	if (l == r)return;
	int m = (l + r) >> 1;
	if (i <= m) {
		upd(l, m, x*2+1, i, v);
	} else {
		upd(m+1, r, x*2+2, i, v);
	}
}

ll getsm(int li, int ri, int x, int l, int r) {
	if (li >= l && ri <= r)return t[x].sm;
	if (li > r || ri < l)return 0;
	int m = (li + ri) >> 1;
	return getsm(li, m, x*2+1, l, r) + getsm(m+1, ri, x*2+2, l, r);
}
pair<int, int> geti(int li, int ri, int x, int l, int r) {
	if (li >= l && ri <= r)return t[x].mn;
	if (li > r || ri < l)return {int(1e9), int(1e9)};
	int m = (li + ri) >> 1;
	return min(geti(li, m, x*2+1, l, r), geti(m+1, ri, x*2+2, l, r));
}

void rem(int li, int ri, int x, int l, int r) {
	if (t[x].mn.first == 1e9)return;
	if (li > r || ri < l)return;
	if (li == ri) {
		t[x].mn = {int(1e9), int(1e9)};
		return;
	}
	int m = (li + ri) >> 1;
	rem(li, m, x*2+1, l, r);
	rem(m+1, ri, x*2+2, l, r);
	t[x].mn = min(t[x*2+1].mn, t[x*2+2].mn);
}
ll bit[int(1e6+1)];
void init(int u) {
	x[u] = (adj[u].empty());
	rv[idx] = u;
	in[u] = idx++;
	sz[u] = 1;
	for (auto &v : adj[u]) {
		init(v);
		x[u] += x[v];
		sz[u] += sz[v];
	}
	if (w[u] == 0) {
		x[u] = 1;
	}
}
ll lf;


ll getb(int i) {
	ll ret = 0;
	while (i) {
		ret += bit[i];
		i -= (i & -i);
	}
	return ret;
}

void updop(int i, ll v) {
	while (i <= 1e6) {
		bit[i] += v;
		i += (i & -i);
	}
}

void updb(int lo, int ro, ll v) {
	if (lo > ro)return;
	updop(lo, v);
	updop(ro+1, -v);
}

int id[N];

bool cmp(int i, int j) {
	return (x[i] < x[j]);
}

void init(vector<int> P, vector<int> W) {
	n = P.size();
	f = true;
	idx = 0;
	lf = 0;
	for (int i = 0; i < n; i++) {
		adj[i].clear();
		w[i] = W[i];
		pr[i] = P[i];
		f &= (w[i] <= 1);
	}
	for (int i = 1; i < n; i++) {
		adj[P[i]].push_back(i);
	}
	init(0);
	for (int i = 0; i < n; i++) {
		srtx[i] = x[i];
		if (adj[i].empty())lf += w[i];
		srtox[i] = {x[i], w[i]};
	}
	sort(srtox, srtox+n);
	sort(srtx, srtx+n);
	ll smr = 0, smr2 = 0;
	for (int i = 0; i < n; i++) {
		id[i] = i;
	}
	sort(id, id+n, cmp);
	for (int i = n-1; i >= 0; i--) {
		smr += srtox[i].second;
		srtox[i].second = smr;
		if (id[i] == 0) {
			smr2 += 0;
		} else {
			smr2 += w[pr[id[i]]];
		}
		srtx2[i] = smr2;
	}
	srtx2[n] = srtox[n].second = 0;
	for (int i = 0; i <= 1e6; i++) {
		bit[i] = 0;
	}
	for (int i = 1; i < n; i++) {
		if (x[pr[i]] > x[i])updb(x[i], x[pr[i]]-1, w[pr[i]] * x[i]);
	}
}
ll ans;

void dfs(int u) {
	if (adj[u].empty()) {
		ans += L * w[u];
		upd(0, n-1, 0, in[u], -L);
		return;
	}
	for (auto &v : adj[u]) {
		dfs(v);
	}
	ll xx = getsm(0, n-1, 0, in[u], in[u] + sz[u] - 1);
	while (xx > R) {
		ll nedi = xx - R;
		pair<int, int> mno = geti(0, n-1, 0, in[u], in[u] + sz[u] - 1);
		ll xo = getsm(0, n-1, 0, mno.second, mno.second + sz[rv[mno.second]] - 1);
		if (xo == L) {
			rem(0, n-1, 0, mno.second, mno.second + sz[rv[mno.second]] - 1);
			continue;
		}
		if (xo - L >= nedi) {
			ans += nedi * w[rv[mno.second]];
			upd(0, n-1, 0, mno.second, nedi);
			rem(0, n-1, 0, mno.second + 1, mno.second + sz[rv[mno.second]] - 1);
			break;
		}
		xx -= (xo - L);
		ans += (xo - L) * w[rv[mno.second]];
		upd(0, n-1, 0, mno.second, xo - L);
		rem(0, n-1, 0, mno.second, mno.second + sz[rv[mno.second]] - 1);
	}
}

long long query(int lo, int ro) {
	L = lo, R = ro;
	if (f) {
		ans = L * lf;
		ll sb = max(1ll, (R / L) - 1ll);
		while ((sb + 1ll) * L <= R)sb++;
		int id = upper_bound(srtx, srtx+n, sb) - srtx;
		ans -= R * srtox[id].second;
		ans += R * srtx2[id];
		ans += L * getb(sb);
		return ans;
	}
	build(0, n-1, 0);
	ans = 0;
	dfs(0);
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 29008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 595 ms 50340 KB Output is correct
2 Correct 79 ms 47300 KB Output is correct
3 Correct 644 ms 43924 KB Output is correct
4 Correct 633 ms 42568 KB Output is correct
5 Correct 636 ms 42340 KB Output is correct
6 Correct 80 ms 41544 KB Output is correct
7 Correct 648 ms 42388 KB Output is correct
8 Correct 625 ms 42468 KB Output is correct
9 Correct 757 ms 41544 KB Output is correct
10 Correct 184 ms 38520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 29264 KB Output is correct
2 Correct 11 ms 29264 KB Output is correct
3 Correct 12 ms 29264 KB Output is correct
4 Correct 10 ms 29264 KB Output is correct
5 Correct 10 ms 29264 KB Output is correct
6 Correct 10 ms 29264 KB Output is correct
7 Correct 12 ms 29168 KB Output is correct
8 Correct 6 ms 29008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 29264 KB Output is correct
2 Correct 11 ms 29264 KB Output is correct
3 Correct 12 ms 29264 KB Output is correct
4 Correct 10 ms 29264 KB Output is correct
5 Correct 10 ms 29264 KB Output is correct
6 Correct 10 ms 29264 KB Output is correct
7 Correct 12 ms 29168 KB Output is correct
8 Correct 6 ms 29008 KB Output is correct
9 Correct 238 ms 36884 KB Output is correct
10 Correct 130 ms 37336 KB Output is correct
11 Correct 235 ms 36720 KB Output is correct
12 Correct 146 ms 36984 KB Output is correct
13 Correct 244 ms 36208 KB Output is correct
14 Correct 309 ms 35952 KB Output is correct
15 Correct 239 ms 34640 KB Output is correct
16 Correct 238 ms 34612 KB Output is correct
17 Correct 323 ms 34640 KB Output is correct
18 Correct 233 ms 34640 KB Output is correct
19 Correct 235 ms 34640 KB Output is correct
20 Correct 229 ms 34384 KB Output is correct
21 Correct 317 ms 34384 KB Output is correct
22 Correct 58 ms 33484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 49608 KB Output is correct
2 Correct 110 ms 43964 KB Output is correct
3 Correct 112 ms 43972 KB Output is correct
4 Correct 108 ms 44084 KB Output is correct
5 Correct 107 ms 43828 KB Output is correct
6 Correct 78 ms 46120 KB Output is correct
7 Correct 115 ms 43140 KB Output is correct
8 Correct 99 ms 40140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 49608 KB Output is correct
2 Correct 110 ms 43964 KB Output is correct
3 Correct 112 ms 43972 KB Output is correct
4 Correct 108 ms 44084 KB Output is correct
5 Correct 107 ms 43828 KB Output is correct
6 Correct 78 ms 46120 KB Output is correct
7 Correct 115 ms 43140 KB Output is correct
8 Correct 99 ms 40140 KB Output is correct
9 Correct 101 ms 44840 KB Output is correct
10 Correct 99 ms 44072 KB Output is correct
11 Correct 108 ms 44084 KB Output is correct
12 Correct 96 ms 44072 KB Output is correct
13 Correct 85 ms 46028 KB Output is correct
14 Correct 102 ms 43048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2068 ms 50356 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 29008 KB Output is correct
2 Correct 595 ms 50340 KB Output is correct
3 Correct 79 ms 47300 KB Output is correct
4 Correct 644 ms 43924 KB Output is correct
5 Correct 633 ms 42568 KB Output is correct
6 Correct 636 ms 42340 KB Output is correct
7 Correct 80 ms 41544 KB Output is correct
8 Correct 648 ms 42388 KB Output is correct
9 Correct 625 ms 42468 KB Output is correct
10 Correct 757 ms 41544 KB Output is correct
11 Correct 184 ms 38520 KB Output is correct
12 Correct 10 ms 29264 KB Output is correct
13 Correct 11 ms 29264 KB Output is correct
14 Correct 12 ms 29264 KB Output is correct
15 Correct 10 ms 29264 KB Output is correct
16 Correct 10 ms 29264 KB Output is correct
17 Correct 10 ms 29264 KB Output is correct
18 Correct 12 ms 29168 KB Output is correct
19 Correct 6 ms 29008 KB Output is correct
20 Correct 238 ms 36884 KB Output is correct
21 Correct 130 ms 37336 KB Output is correct
22 Correct 235 ms 36720 KB Output is correct
23 Correct 146 ms 36984 KB Output is correct
24 Correct 244 ms 36208 KB Output is correct
25 Correct 309 ms 35952 KB Output is correct
26 Correct 239 ms 34640 KB Output is correct
27 Correct 238 ms 34612 KB Output is correct
28 Correct 323 ms 34640 KB Output is correct
29 Correct 233 ms 34640 KB Output is correct
30 Correct 235 ms 34640 KB Output is correct
31 Correct 229 ms 34384 KB Output is correct
32 Correct 317 ms 34384 KB Output is correct
33 Correct 58 ms 33484 KB Output is correct
34 Correct 94 ms 49608 KB Output is correct
35 Correct 110 ms 43964 KB Output is correct
36 Correct 112 ms 43972 KB Output is correct
37 Correct 108 ms 44084 KB Output is correct
38 Correct 107 ms 43828 KB Output is correct
39 Correct 78 ms 46120 KB Output is correct
40 Correct 115 ms 43140 KB Output is correct
41 Correct 99 ms 40140 KB Output is correct
42 Correct 101 ms 44840 KB Output is correct
43 Correct 99 ms 44072 KB Output is correct
44 Correct 108 ms 44084 KB Output is correct
45 Correct 96 ms 44072 KB Output is correct
46 Correct 85 ms 46028 KB Output is correct
47 Correct 102 ms 43048 KB Output is correct
48 Execution timed out 2068 ms 50356 KB Time limit exceeded
49 Halted 0 ms 0 KB -