I try use heavy-light decomposition to solve problem http://www.spoj.pl/problems/COT/. But I can't do it. Please help to solve that problem. Thanks for your helping! :)
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
I try use heavy-light decomposition to solve problem http://www.spoj.pl/problems/COT/. But I can't do it. Please help to solve that problem. Thanks for your helping! :)
Name |
---|
I can solve this problem in O(N * Log(N)^2). We have to use heavy-light decomposition + persistent segment trees. For each chain of decomposition we have to build segment trees t[i] where t[i] is a tree for first i vertices of chain from top to bottom. At first step we can transform all numbers into numbers from 1 to n. Then build a "start" tree a[i] = 0, for i 1..n . To add a vertice with number X to a tree we just increase value at X. i.e. a[X]++; To answer a query for two vertices X and Y we have to find their LCA Z. Then find about O(log(N)) trees on the way from Z to X and from Z to Y. It is easy to find k-th element of a tree merged from these ones.
I think u can solve it in O(n*log^3(n)). Use binary search with heavy light. Save all numbers in sorted order for each stage in segment tree. It is quite easy to write. But i dont know will it be good for TL.
I found someone's solutions which gets accepted. But I didn't understand the code. Here is link.
It's NlogN solution. It's also uses Persistent segment tree. Let's build a tree described above in my solution, but for each vertice. Tree for a fixed vertice x contains all values on the way from root to this vertice. tree T(x, y) — tree for the way from x to y is T(x, z) + T(y, z) — 2 * T(root, z), where z = LCA(x, y)