cpinitiative / usaco-guide

A free collection of curated, high-quality resources to take you from Bronze to Platinum and beyond.
https://usaco.guide
Other
1.6k stars 477 forks source link

New Module Proposal - Advanced Data Structure Design #4640

Open jamperezmondragon opened 1 month ago

jamperezmondragon commented 1 month ago

Hello, I was talking to lunchbox and thought maybe it could be an interesting addition to the guide, he told me to write an issue.

This (and the next 2 proposals) are classes I taught to the Mexican IOI team some months ago (I am the team leader).

This is just a draft, if you like it I can work more on it and make a PR in the next couple weeks.

Advanced data structure design

Inspired by this blog and it's video.

What is an invariant? What is a monovariant?

Example problem to define and showcase this concepts is IMO 2009 C1.

The main idea of this module is to think about data structures as graphs where each node stores an invariant or a monovariant.

It's a good idea to start designing a data structure as a "bag" where you can add nodes, and as you start to recognize more properties you'll need to solve the problem, use degrees of freedom to narrow down the structure of the algorithm, connecting the nodes in some way and adding invariants or monovariants to them.

Definitions:

Example problems

Range Updates and Sums with Square Root Decomposition

The main idea is to do blocking, and maintain several invariants for each block. Say there's $N/B$ blocks, we'll maintain the following variables:

Our invariants are:

We will process updates in such a way that this invariants will always hold. Whenever we process any update, if it's contained within the same block, reconstruct it in $O(B)$ time.

Scoreboard Semanal

Translation of the problem:

There's a set of students. Each week, they solve some amount of problems (could be negative). After each week, the student $i$ will get 1 stress point for each student with more solved problems than them. given $N \le 10^5$ students, and $Q \le 10^5$ weeks, each week having $U_i$ updates to the ranking (which consists of a student that solved a non zero number of problems and how many they solved), determine the final number of stress points each student gets after the $Q$ weeks. It is guaranteed that the sum of $U_i$ is at most $5\times 10^5$ and that the number of solved problems for each student will never leave the range $[-5\times 10^5, 5\times10^5]$.

Solution: (I have yet to write a more detailed and motivated explanation)

T-shirts

We iterate over the t-shirts in decreasing order of quality untying by cost. Each t-shirt with cost $c$ will be bought by any person with a budget larger than or equal to $c$ when that t-shirt is processed. We want to decrease by $c$ all the budgets larger than or equal to $c$, and add 1 to their answers.

Welcome home, Chtholly

Similar idea to the editorial, I think it's pretty educational and we can motivate each step of the solution after seeing the problems above.

Problems to try

bqi343 commented 1 month ago

Looks interesting, though I don't really know it would go considering it's a mix of some topics already in the guide / some new things. Maybe after the others in the Plat range queries section?

Questions:

Thus, the final complexity is $O(Q(N/B + B))$, and since $Q = O(N)$ we can take $B = \sqrt{N}$, which is optimal by AM-GM inequality.