can anyone help me with the chgoram problem of august challenge
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
can anyone help me with the chgoram problem of august challenge
Name |
---|
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 vertexj
that share an edge withi
. Try to visualize it as a splitting the tree into two subsets, where subsetA
is fromj
to all other vertexes connected to it except fromi
and subsetB
the remaining vertexes.Also, let
val
be the value of our current vertex. We need to count how many(x, y)
pairs we have, where:x
is inA
y
is inB
x < val < y
L
= how many values are less thanval
inA
R
= how many values are greater thanval
inB
current_answer
=L
*R
If the middle value is not 2, after you sum the answer for every vertex, you need to divide the
total_answer
by two, to remove some repeted pairs.The biggest problem now is: how to do fast queries on how many values are less/greater than
val
in 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.