Submission #1099824

# Submission time Handle Problem Language Result Execution time Memory
1099824 2024-10-12T05:30:46 Z model_code Tree (IOI24_tree) C++17
100 / 100
144 ms 23120 KB
// correct/yiping-full.cpp

#include<bits/stdc++.h>
#include"tree.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

vector<int> fa;
int find(int u){ return fa[u] == u? u: fa[u] = find(fa[u]);}

int n;
ll sumleaf;
vector< array<ll,2> > ans;

ll query(int l,int r)
{
	int k = r / l;
	ll res;
	if(k > n) res = 0;
	else res = l * ans[k][0] + r * ans[k][1];
	return res + l * sumleaf;
}

void init(vector<int> anc, vector<int> wei)
{
	n = (int)anc.size();
	
	vector< vector<int> > g(n);
	for(int i=1; i<n; ++i)
		g[anc[i]].emplace_back(i);
	
	sumleaf = 0;
	for(int i=0; i<n; ++i) if((int)g[i].size() == 0)
	{
		sumleaf += wei[i];
		wei[i] = 0;
	}
	
	ans.resize(n+2);
	fill(ans.begin(), ans.end(), array<ll,2>{0,0});
	
	auto upd = [&] (int k, int w)
	{
		ans[k-1][0] += (ll)k * w;
		ans[k-1][1] += - w;
	};
	
	vector<pii> eff(n);
	for(int i=0; i<n; ++i)
		eff[i] = {wei[i], i};
	sort(eff.begin(), eff.end());
	reverse(eff.begin(), eff.end());
	
	fa.resize(n);
	iota(fa.begin(), fa.end(), 0);
	
	vector<int> num(n, 1), rig(n, eff[0].first);
	
	auto connect = [&] (int u,int v,int cur)
	{
		u = find(u); v = find(v);
		if(u == v) return;
		upd(num[u], rig[u] - cur);
		upd(num[v], rig[v] - cur);
		num[v] += num[u];
		rig[v] = cur;
		fa[u] = v;
	};
	
	vector<int> val(n, 0);
	
	for(auto t: eff)
	{
		if(t.first == 0) break;
		
		int u = t.second;
		val[u] = 1;
		
		if(anc[u] >= 0 && val[anc[u]])
			connect(u, anc[u], t.first);
		for(int v: g[u])
			connect(v, u, t.first);
		
		int v = find(u);
		if(rig[v] != t.first)
			upd(num[v], rig[v] - t.first);
		--num[v];
		rig[v] = t.first;
	}
	
	for(int i=0; i<n; ++i) if(fa[i] == i)
		upd(num[i], rig[i]);
	
	for(int i=n; i>=1; --i)
	{
		ans[i][0] += ans[i+1][0];
		ans[i][1] += ans[i+1][1];
	}
}

vector<ll> tree(vector<int> anc, vector<int> wei, vector<int> L, vector<int> R)
{
	init(anc, wei);
	
	vector<ll> res(L.size());
	for(int i=0; i<(int)L.size(); ++i)
		res[i] = query(L[i], R[i]);
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 20148 KB Output is correct
2 Correct 53 ms 19920 KB Output is correct
3 Correct 56 ms 18132 KB Output is correct
4 Correct 64 ms 19796 KB Output is correct
5 Correct 86 ms 19796 KB Output is correct
6 Correct 54 ms 19912 KB Output is correct
7 Correct 68 ms 19796 KB Output is correct
8 Correct 68 ms 19824 KB Output is correct
9 Correct 70 ms 19024 KB Output is correct
10 Correct 38 ms 16836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 17 ms 6312 KB Output is correct
10 Correct 28 ms 6236 KB Output is correct
11 Correct 16 ms 6232 KB Output is correct
12 Correct 19 ms 6236 KB Output is correct
13 Correct 34 ms 6084 KB Output is correct
14 Correct 17 ms 5980 KB Output is correct
15 Correct 20 ms 6312 KB Output is correct
16 Correct 18 ms 6236 KB Output is correct
17 Correct 18 ms 6252 KB Output is correct
18 Correct 18 ms 6236 KB Output is correct
19 Correct 23 ms 6312 KB Output is correct
20 Correct 21 ms 6104 KB Output is correct
21 Correct 18 ms 6040 KB Output is correct
22 Correct 17 ms 5436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 20800 KB Output is correct
2 Correct 72 ms 20712 KB Output is correct
3 Correct 71 ms 20816 KB Output is correct
4 Correct 70 ms 20696 KB Output is correct
5 Correct 74 ms 20568 KB Output is correct
6 Correct 75 ms 23020 KB Output is correct
7 Correct 75 ms 19792 KB Output is correct
8 Correct 50 ms 17604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 20800 KB Output is correct
2 Correct 72 ms 20712 KB Output is correct
3 Correct 71 ms 20816 KB Output is correct
4 Correct 70 ms 20696 KB Output is correct
5 Correct 74 ms 20568 KB Output is correct
6 Correct 75 ms 23020 KB Output is correct
7 Correct 75 ms 19792 KB Output is correct
8 Correct 50 ms 17604 KB Output is correct
9 Correct 69 ms 18900 KB Output is correct
10 Correct 69 ms 20768 KB Output is correct
11 Correct 70 ms 20752 KB Output is correct
12 Correct 74 ms 20564 KB Output is correct
13 Correct 72 ms 23056 KB Output is correct
14 Correct 74 ms 19836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 19928 KB Output is correct
2 Correct 79 ms 18988 KB Output is correct
3 Correct 86 ms 20688 KB Output is correct
4 Correct 144 ms 20564 KB Output is correct
5 Correct 85 ms 20716 KB Output is correct
6 Correct 91 ms 20700 KB Output is correct
7 Correct 96 ms 20564 KB Output is correct
8 Correct 94 ms 22980 KB Output is correct
9 Correct 126 ms 19912 KB Output is correct
10 Correct 87 ms 19944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 99 ms 20148 KB Output is correct
3 Correct 53 ms 19920 KB Output is correct
4 Correct 56 ms 18132 KB Output is correct
5 Correct 64 ms 19796 KB Output is correct
6 Correct 86 ms 19796 KB Output is correct
7 Correct 54 ms 19912 KB Output is correct
8 Correct 68 ms 19796 KB Output is correct
9 Correct 68 ms 19824 KB Output is correct
10 Correct 70 ms 19024 KB Output is correct
11 Correct 38 ms 16836 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 600 KB Output is correct
20 Correct 17 ms 6312 KB Output is correct
21 Correct 28 ms 6236 KB Output is correct
22 Correct 16 ms 6232 KB Output is correct
23 Correct 19 ms 6236 KB Output is correct
24 Correct 34 ms 6084 KB Output is correct
25 Correct 17 ms 5980 KB Output is correct
26 Correct 20 ms 6312 KB Output is correct
27 Correct 18 ms 6236 KB Output is correct
28 Correct 18 ms 6252 KB Output is correct
29 Correct 18 ms 6236 KB Output is correct
30 Correct 23 ms 6312 KB Output is correct
31 Correct 21 ms 6104 KB Output is correct
32 Correct 18 ms 6040 KB Output is correct
33 Correct 17 ms 5436 KB Output is correct
34 Correct 71 ms 20800 KB Output is correct
35 Correct 72 ms 20712 KB Output is correct
36 Correct 71 ms 20816 KB Output is correct
37 Correct 70 ms 20696 KB Output is correct
38 Correct 74 ms 20568 KB Output is correct
39 Correct 75 ms 23020 KB Output is correct
40 Correct 75 ms 19792 KB Output is correct
41 Correct 50 ms 17604 KB Output is correct
42 Correct 69 ms 18900 KB Output is correct
43 Correct 69 ms 20768 KB Output is correct
44 Correct 70 ms 20752 KB Output is correct
45 Correct 74 ms 20564 KB Output is correct
46 Correct 72 ms 23056 KB Output is correct
47 Correct 74 ms 19836 KB Output is correct
48 Correct 47 ms 19928 KB Output is correct
49 Correct 79 ms 18988 KB Output is correct
50 Correct 86 ms 20688 KB Output is correct
51 Correct 144 ms 20564 KB Output is correct
52 Correct 85 ms 20716 KB Output is correct
53 Correct 91 ms 20700 KB Output is correct
54 Correct 96 ms 20564 KB Output is correct
55 Correct 94 ms 22980 KB Output is correct
56 Correct 126 ms 19912 KB Output is correct
57 Correct 87 ms 19944 KB Output is correct
58 Correct 71 ms 20856 KB Output is correct
59 Correct 79 ms 20816 KB Output is correct
60 Correct 77 ms 18176 KB Output is correct
61 Correct 124 ms 19028 KB Output is correct
62 Correct 102 ms 20756 KB Output is correct
63 Correct 98 ms 20836 KB Output is correct
64 Correct 91 ms 20676 KB Output is correct
65 Correct 85 ms 20564 KB Output is correct
66 Correct 92 ms 20696 KB Output is correct
67 Correct 95 ms 20700 KB Output is correct
68 Correct 90 ms 23120 KB Output is correct
69 Correct 82 ms 19920 KB Output is correct
70 Correct 80 ms 18884 KB Output is correct
71 Correct 66 ms 17744 KB Output is correct