## Introduction

At the time I am writing this, it seems that all accepted solutions to 786D - Бог рэпа [this problem] use optimized O(n*q) algorithms, including the only accepted solution during the contest by Retired_MiFaFaOvO. The editorial gives some hints on an algorithm with complexity O((n+q)*lg^3(n)), but nobody seems to have successfully implemented something like it. So I thought I'd give it a try today and after a lot of hard work, I got it accepted. My code can be found here: 25820897. Since I enjoyed solving this problem, and since I used a lot of different interesting techniques, I decided to share my approach to this problem in this blog.

## Centroid decomposition

*(just like in the editorial.)*

We will decompose the tree using centroid decomposition. When processing a centroid, we will need to count for each query (that is in this subtree) the number of paths that pass through the centroid and are lexographically smaller than the query-path. Because of the way the centroid decomposition works, we will process each query at most a logarithmic number of times. Each time we process a query-centroid-combination, we will first compare the path of the query-startnode to the centroid (all paths that we need to count now start with this path) with the query-path (capped at the length of the first path). We will later see how to do this quickly. If they differ, we know that all or none of the paths from the query-startnode though the centroid are lexographically smaller than the query-path. If they are equal, things are a little more difficult. We now need to find where the remaining part of the query-path falls in the subtree of all paths rooted in the centroid, excluding the subtree of the query-startnode.

## Trie rooted in centroid with binary search

*(to sort paths starting in centroid and find the position of the remaining part of the query-path)*

To find the position of a path in the subtree of all paths rooted in the centroid, we will first construct a trie from the centroid. Then walking this trie in-order will result in a sorted list of all paths rooted in the centroid. If we store all these paths in an array, we can use binary search to find the position of the remaining part of the query path. For this we again need to compare two paths in the tree. We will see later how to do this quickly.

## Binary Indexed Tree

*(to count only active nodes)*

We only need to count paths in a different subtree than the subtree of the query-startnode. For this, we will use a binary indexed tree. Initally we start with all paths enabled, just before processing queries in a subtree we disable all paths in the subtree, and after processing queries in the subtree we re-enable all paths in the subtree. Then when we know the position of the remaining part of the query-path in list of all paths in the subtree, we can query the binary indexed tree for a prefix sum to obtain the number of paths that we need to count.

## Ladder decomposition

*(to decompose paths in O(lg(n)) strings)*

During the algorithm, we frequently needed to compare the strings for two paths in the tree, and we need to be able to calculate this quickly. The editorial suggests using hashing for this, but we will use another technique. We will build a string S of linear size, such that each path can be decomposed into a logaritmic number of substrings of S. Then two paths can be compared as follows: split each path in a logarithmic number of substrings of S, compare the first parts of both lists (with length equal to the shorter of the two parts), if they differ we know the result, otherwise we continue with the remaining parts. We continue this way until we find a part that differs or we reach the end of one of the paths; in both cases we know the answer. Assuming we can compare two substrings of S in constant time, then comparing two paths using this algorithm takes only logarithmic time.

How should S look like so it has the above property? First build a ladder composition of the tree. The total sum of the sizes of all ladders will be at most 2*n. In S, store for each ladder the upward and the downward strings. Then the length of S will be at most 4*n. And each path in the tree can be decomposed into a logarithmic number of parts of a ladder (upward or downward) which correspond to a substring of S.

## Suffix array and lcp array with sparse table range minimum query

*(to compare strings)*

To compare two substrings in S quickly, we can create a suffix array for S. The relative position of the startpoints in the suffix array of the two suffixes corresponding to the two substrings will then tell us which of the two suffixes comes first, and the minimum over the range in the corresponding lcp array will tell us if the prefixes of the two suffixes (the substrings) are equal or not. To be able to compare substrings in constant time, we should store their position in the suffix array and build a sparse table for rmq queries in the lcp array.

## Suffix tree

*(to create suffix array and lcp array)*

There are other ways to do this, and you can even afford to use a somewhat slower algorithm, but I used my suffix tree library implementation to create the suffix array in linear time.

## Conclusion

Like I said, I really enjoyed solving this problem. Sometimes it seemed like there was so much stuff to do, but when it all came together in the end it was a wonderful feeling. I want to thank PrinceOfPersia for creating this problem.

On a more critical note, I don't think its realistic to expect someone to implement something like this in a two hour contest. And it is very difficult to set the time limit so that these kind of solutions pass, but O(n*q) solutions don't. Because of these reasons, I don't think it was a good problem for a two hour contest.

what the ...

I've taken a look at your code. Please use "enter" key more often.

Even though I'm not going to solve this problem (because of suffix arrays and trees), it's always nice when someone explains their approach with details. Kudos.

I guess everyone has their own coding style he/she is comfortable with. Sometimes when I look at someones code I think 'is it really necessary to put that code on 10 lines when it also fits on 1' :).

That reminds me of a story from an TCO a while ago. I was coding and Jan Kuipers was watching my screen. When somebody asked him how I was doing, he replied 'he has a bug over there' (pointing about half a meter right of the screen).

Just out of curiosity — why you don't want to solve problems about suffix arrays and trees?

I don't like string problems and I'm weak in them.

"I'm weak in them". That's exactly why you should solve problems about suffix arrays and trees :))

My goal is to enjoy CP, not to be the best in the world.

Why not both? ;)

I don't think I'm able to achieve the latter anyway. And spending too much time on something decreases the pleasure (enjoyment).

The lines in your code are so long that I felt like reading Rap God's lyrics.

He probably has dual monitor setup, and his code fully uses setup's power :D

I think hash+heavy_light decopositon+binary_search/ can solve this problem, now I'm typing it...complexity is O((n+q)*log(n)^2) with small constant factor

http://codeforces.com/contest/786/submission/26214476

here is my 647 lines implement, it is much more difficult than I begining think.

265ms...

My eyes went nuts...

btw,I'd like to recommend you another string matching problems:

http://uoj.ac/problem/90

which I have code 1000+lines...

Got it!

Code God.