problem link: 61E - Enemy is weak
Reading the editorial and seeing other's code I have come to know that the problem is solvable by using binary indexed tree — BIT (aka fenwick tree). But I didn't understand how binary indexed tree is applicable for this problem.
Though I know BIT, I don't understand how to solve this problem using BIT. In contest hour, how should I determine that I should use BIT for this problem?
I have also read some code of others, but I didn't understand any full code. Specially, what is the use of lower_bound here? What it is doing?
Can anybody help me to solve and understand fully the problem, tricks behind this problem, solution — how BIT is working here and how did you understand that this is solvable by BIT ?
Thanks in advance.
suppose we choose a[j] as our middle element then number of ways = number of i such that i < j and a[i] > a[j] * number of k such that k > j and a[k] < a[j] . we can easily find this using BIT or segment tree
My solution has a large amount of comments, so it might help you understand what's going on in mine and others' code: http://codeforces.net/contest/61/submission/8675312
As for the technique, this problem is very similar to inversion counting (though slightly more complicated), which you can read more about by Googling "counting inversions with a bit" (without the quotes).
I don't know how to solve it with a single BIT tree, but with two BIT trees a very simple.
If you know how to solve
a[i] < a[j], i > j
problem, also known as inversion pairs, this code easy to understand.well this might help 24408090 . In this code I have used ordered_set which can be used by including the headers in my code. It works like std::set but also supports order of an element and finding element by order. Nevertheless you must learn BIT and segment tree. this is just for your understanding.