can anyone help me with the chgoram problem of august challenge
can anyone help me with the chgoram problem of august challenge
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | turmax | 3559 |
| 6 | tourist | 3541 |
| 7 | strapple | 3515 |
| 8 | ksun48 | 3461 |
| 9 | dXqwq | 3436 |
| 10 | Otomachi_Una | 3413 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | adamant | 153 |
| 3 | Um_nik | 147 |
| 4 | Proof_by_QED | 146 |
| 5 | Dominater069 | 145 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | TheScrasse | 134 |
| 10 | chromate00 | 133 |
| Название |
|---|



Here is a link for some quick explanation.
For example, let's suppose that restaurants must be in increasing order: 1, 2, 3.
Assume that you are checking all the vertexes as the middle one. Now you want to know how many can be the first and how many can be the last, of course, they must belong to different subtrees of our current vertex.
For every vertex
i, go through each vertexjthat share an edge withi. Try to visualize it as a splitting the tree into two subsets, where subsetAis fromjto all other vertexes connected to it except fromiand subsetBthe remaining vertexes.Also, let
valbe the value of our current vertex. We need to count how many(x, y)pairs we have, where:xis inAyis inBx < val < yL= how many values are less thanvalinAR= how many values are greater thanvalinBcurrent_answer=L*RIf the middle value is not 2, after you sum the answer for every vertex, you need to divide the
total_answerby two, to remove some repeted pairs.The biggest problem now is: how to do fast queries on how many values are less/greater than
valin a subtree?I have solved this with persistent segment tree, where I do a query in an interval that represent this subtree. Like:
st[current_version]->query(x, y) - st[current_version - size_of_this_subtree]->query(x, y)The problem have a tight time limit if you code with persistent segment tree, you can use other faster methods to do this offline, like the small to large trick.