Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HLD solution for Promotion Counting(Platinum) #4886

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 112 additions & 4 deletions solutions/gold/usaco-696.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ id: usaco-696
source: USACO Platinum 2017 January
title: Promotion Counting
author: Benjamin Qi, Timothy Gao, William Yuan
contributors: Vaishnav Krishnan
---

[Official Editorial (Java)](http://www.usaco.org/current/data/sol_promote_platinum_jan17.html)
Expand Down Expand Up @@ -306,10 +307,117 @@ int main() {
</CPPSection>
</LanguageSection>

<IncompleteSection>
## Solution 3: HLD

Add Centroid Decomp/HLD Solution
**Time Complexity:** $\mathcal{O}(N\log ^2N)$

Sort the nodes from largest to smallest value, do path updates from each node to the root.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i get what you mean but like, maybe make the wording a bit more specific

like: "Sort the nodes in descending order of proficiency. When adding in each node, we do a path update from the root to this node." (except be specific with what type of path update we do)


<LanguageSection>
<CPPSection>

```cpp
#include <bits/stdc++.h>
using namespace std;

class segTree {
Vaishnav-K787 marked this conversation as resolved.
Show resolved Hide resolved
private:
vector<int> segtree;

Sort nodes from largest to smallest value, do path updates from itself the root.
public:
segTree(int N) {
if (__builtin_popcount(N) > 1) N = 1 << (32 - __builtin_clz(N));
Vaishnav-K787 marked this conversation as resolved.
Show resolved Hide resolved
segtree.resize(N << 1, 0);
}
void update(int i, int val) {
for (i += (int)segtree.size() >> 1; i > 0; i >>= 1) segtree[i] += val;
}
int query(int l, int r) {
int ans = 0;
for (l += (int)segtree.size() >> 1, r += ((int)segtree.size() >> 1) + 1; l < r;
l >>= 1, r >>= 1) {
if (l & 1) ans += segtree[l++];
if (r & 1) ans += segtree[--r];
}
return ans;
}
};

</IncompleteSection>
void dfs_hld(vector<vector<int>> &edges, vector<vector<int>> &heavy_paths,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i personally find this to be kind of ugly, ngl

IMO there's two ways to clean it up

  1. make these global arrays (sans might make u change it tho lol)
  2. lambda dfs

I would say don't change it yet, but be aware that you might be asked to later.

vector<int> &depth, vector<int> &sizes, int start) {
int heavy_child = -1;
++sizes[start];
for (int i : edges[start]) {
depth[i] = depth[start] + 1;
dfs_hld(edges, heavy_paths, depth, sizes, i);
sizes[start] += sizes[i];
if (heavy_child == -1 || sizes[heavy_child] < sizes[i]) heavy_child = i;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just in general put braces around blocks of code even if it's one statement

}
if (heavy_child != -1) {
swap(heavy_paths[start], heavy_paths[heavy_child]);
heavy_paths[start].push_back(heavy_child);
}
}

int main() {
ios_base::sync_with_stdio(false);
Vaishnav-K787 marked this conversation as resolved.
Show resolved Hide resolved
cin.tie(nullptr);
freopen("promote.in", "r", stdin);
freopen("promote.out", "w", stdout);
int N;
cin >> N;
vector<pair<int, int>> cow_proficiency(N, pair<int, int>());
Vaishnav-K787 marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < N; ++i) {
cin >> cow_proficiency[i].first;
cow_proficiency[i].second = i;
}
vector<vector<int>> edges(N, vector<int>()), heavy_paths(N, vector<int>());
Vaishnav-K787 marked this conversation as resolved.
Show resolved Hide resolved

vector<int> sizes(N, 0), heavy_index(N, -1), path_index(N, -1), depth(N, 0),
parents(N, -1), ans(N, 0), cur_ans(N, 0);

for (int i = 1; i < N; ++i) {
cin >> parents[i];
edges[--parents[i]].push_back(i);
}

dfs_hld(edges, heavy_paths, depth, sizes, 0);
vector<segTree> segtrees(N, segTree(0));
for (int i = 0; i < N; ++i) {
reverse(heavy_paths[i].begin(), heavy_paths[i].end());
for (int j = 0; j < (int)heavy_paths[i].size(); ++j) {
heavy_index[heavy_paths[i][j]] = i;
path_index[heavy_paths[i][j]] = j;
}
segtrees[i] = segTree((int)heavy_paths[i].size());
}

sort(cow_proficiency.begin(), cow_proficiency.end(), greater<pair<int, int>>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort(cow_proficiency.begin(), cow_proficiency.end(), greater()) also works btw

Copy link
Author

@Vaishnav-K787 Vaishnav-K787 Nov 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it works locally but throws a compile error on usaco

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most guide code is written to be submitted in C++ 17 I'm pretty sure

cuz like, with C++ there's literally no disadvantage with going with a higher version (I'm pretty sure)

if ur curious why, it's cuz of class template argument deduction

for (auto &p : cow_proficiency) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider using a structured binding

e.g. for (auto &[thing_a, thing_b] : cow_proficiency) or something

if (heavy_index[p.second] != -1) {
ans[p.second] =
segtrees[heavy_index[p.second]].query(0, path_index[p.second]);
} else ans[p.second] = cur_ans[p.second];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

braces

p.second = parents[p.second];
while (p.second >= 0) {
if (heavy_index[p.second] != -1) {
segtrees[heavy_index[p.second]].update(0, 1);
if (path_index[p.second] + 1 <
(int)heavy_paths[heavy_index[p.second]].size()) {
segtrees[heavy_index[p.second]].update(path_index[p.second] + 1,
-1);
}
p.second = parents[heavy_paths[heavy_index[p.second]][0]];
} else {
++cur_ans[p.second];
p.second = parents[p.second];
}
}
}
for (int i = 0; i < N; ++i) cout << ans[i] << '\n';
Vaishnav-K787 marked this conversation as resolved.
Show resolved Hide resolved
return 0;
Vaishnav-K787 marked this conversation as resolved.
Show resolved Hide resolved
}
```

</CPPSection>
</LanguageSection>
Loading