PROBLEM LINK:Author: Sunny Agarwal DIFFICULTY:Medium PREREQUISITES:stock span problem, stack, binary search, value compression, combinatorics PROBLEM:Given an array $[A_1, \ldots, A_N]$. Let $B$ be the list of maximums of all subarrays of $A$ ($B$ has length $\frac{N(N+1)}{2}$). $M$ games will be played, each with a single constraint of the form "$C$ $K$", where $C$ is either $<$, $=$, $>$, and $K$ is an integer. In a single game, players alternate turns, and in a move a player marks off an integer in $B$ satisfying the constraint "$C$ $K$", and the player with no more valid moves is the loser. You have to determine the winner of each game. QUICK EXPLANATION:Each game depends only on the parity of the number of integers satisfying the constraint. Thus, we only need to find the number of values in $B$ satisfying the constraint. Let $L_i$ be the largest index $L_i < i$ such that $A_{L_i} \ge A_i$ (set $L_i = 0$ if no such value exists) Let $R_i$ be the smallest index $R_i > i$ such that $A_{R_i} > A_i$ (set $R_i = N+1$ if no such value exists) All $L_i$s and $R_i$s can be computed in $O(N)$ using a stack. There can be at most $N$ distinct values in $A$, say $[V_1, \ldots, V_M]$ with $M \le N$ (in sorted order). Let $f(j)$ be the number of subarrays in which $V_j$ is the maximum. Then $f(j) = \sum_{A_i = j} (R_i  i)(i  L_i)$. There are three kinds of constraints "$< K$", "$= K$" and "$> K$". we need to know the sum of the $f(j)$ for all $j$ in which $V_j$ satisfies the constraint. We introduce a fourth kind of constraint, "$\le K$". The other three kinds of constraints can be reduced to this. To answer a "$\le K$" constraint, first find the largest index $j$ such that $V_j \le K$ (with binary search). Then the result we want is $f(1) + \cdots + f(j)$. This number can quickly be obtained if we precompute all prefix sums beforehand. EXPLANATION:The solution has multiple parts. We start with the most obvious questions first, so that we know what to precompute, etc. A "game"Let's first discuss how a game occurs. Suppose we have the list $B$, which contains the $\frac{N(N+1)}{2}$ maximums among all subarrays (of course, we can't construct this array in the harder subtasks because of its size). The first thing to notice is that the order of $B$'s elements doesn't matter. Thus, we can assume that $B$ is sorted. Let's first assume that we have a constraint. The key thing to notice is that the game is really simple: the players simply alternate turns marking off numbers in $B$ satisfying the constraint. In fact, the winner is solely dependent on the number of values in $B$ satisfying the constraint, i.e., it doesn't matter whatever strategy both players are using! To be specific:
Therefore, we only need to count those elements of $B$ satisfying the constraint! Because $B$ is sorted and because of the nature of the constraints, this number can be computed using one or two binary searches! To illustrate, let $I(K)$ be the number of elements of $B$ that are $\le K$ (or simply the index of the largest $i$ such that $B_i \le K$, which can be computed with a binary search). Then:
Thus, if we already have the array $B$, then we can easily compute the result of any game in $O\left(\log \left(\frac{N(N+1)}{2}\right)\right) = O(\log N)$ time. Runlength encodingUnfortunately, the previous approach uses up $O(N^2)$ memory because of the array $B$. But we can reduce this significantly by noticing that there are only at most $N$ distinct values in $B$. (Why?) So let $[V_1, \ldots, V_M]$ be the distinct values in $B$, and let $f(i)$ be the number of occurrences of $V_i$ in $B$. We can then compute $I(K)$ by doing a binary search in $V$ instead for the largest $i$ such that $V_i \le K$, and then $I(K)$ is simply $f(1) + f(2) + \cdots + f(i)$. If we precompute all prefix sums of the $f(i)$, then $I(K)$ can still be computed in $O(\log N)$ time, but the memory requirements decrease to just $O(N)$. Computing $V$ and $f(i)$To complete the algorithm, we need to compute the array $[V_1, \ldots, V_M]$ and the values $f(1), \ldots, f(M)$. Note that the sequence $V$ can be computed in $O(N \log N)$ time from the array $A$ (with a simple sort + uniquify operation). Thus, all that remains is to compute the $f(i)$s. First, let's suppose that all elements of $A$ are distinct. Consider the value $A_i$. How many subarrays are there whose maximum is $A_i$? Well, let $L_i$ and $R_i$ are the nearest larger elements to the left and right of $A_i$, respectively. Then the subarray cannot contain either $A_{L_i}$ or $A_{R_i}$. Thus, the subarray should be strictly contained in $[A_{L_i+1}, \ldots, A_{R_i1}]$, but due to the way $L_i$ and $R_i$ are defined, we see that the latter condition is sufficient. Thus, there are $(R_i  i)(i  L_i)$ possible subarrays (there are $(R_i  i)$ and $(i  L_i)$ ways to choose the right and left endpoints, respectively). Let us denote this quantity by $m(i)$. As an example, consider the following image ($A_i$ is the height of the bar over the number $i$):
In this case, suppose $i = 5$. Then $L_i = 1$ and $R_i = 8$. Thus, there are $(R_i  i) = 3$ and $(i  L_i) = 4$ choices for the right and left endpoints, respectively, for a total of $3\times 4 = 12$ subarrays, as shown below:
As a consequence, we have $f(j) = m(i)$ for the (unique) $i$ such that $A_i = V_j$. In case either $L_i$ or $R_i$ doesn't exist, we can use the values $0$ or $N+1$, respectively. This formula, and the expression $m(i) = (R_i  i)(i  L_i)$ is nice; unfortunately it doesn't extend straightforwardly if the $A_i$s are not all distinct. To see why, consider the following example:
The natural extension of $f(j) = m(i)$ would be $f(j) = \sum_{A_i = V_j} m(i)$. However, in the above case, it doesn't quite work. Consider $j = 3$. There are three $i$s with $A_i = 3$, namely $i = 3$, $i = 5$ and $i = 8$. But we have $m(3) = 6$, $m(5) = 6$ and $m(8) = 2$, so according to this formula this gives $f(3) = 6 + 6 + 2 = 14$. However, the correct value of $f(3)$ is $10\,$! (verify) Clearly we need to fix this formula. We can do that by redefining $m(i)$ so that it counts the subarrays whose leftmost maximum is $A_i$. This uses the fact that each subarray has a unique leftmost maximum, so no subarray is doublecounted. To implement this, redefine $L_i$ by weakening it so that $A_{L_i}$ can equal $A_i$ (i.e. $A_{L_i} \ge A_i$). Then $m(i)$ stays the same, i.e. $(R_i  i)(i  L_i)$. In the example above, we get the new values $m(3) = 6$, $m(5) = 2$ and $m(8) = 2$, so $f(3)$ is correctly computed as $6 + 2 + 2 = 10$. But how do we compute the $L_i$s and $R_i$s? Actually, these values can be computed easily in $O(N)$ time by means of a stack: it's actually a standard problem called the stock span problem. (Recognizing this as the stock span problem requires experience, but if you didn't know about this, notice that the problem of computing the $L_i$s and $R_i$s can be done in $O(N \log N)$ time with a segment tree) Now, all that remains is to compute the $f(i)$s given the $m(i)$s. But this can be done easily in linear time also:
That's it! We now have all the parts needed for the whole solution. The preprocessing takes $O(N \log N)$ time, and each query can be answered in $O(\log N)$ time! The following is an example implementation in C++:
As always, if you have any questions, feel free to ask :) Time Complexity:$O((N + M) \log N)$ AUTHOR'S AND TESTER'S SOLUTIONS:
This question is marked "community wiki".
asked 16 Aug '15, 13:07

I think test data was weak. Here is my O(N^2) solution that has 100 points : https://www.codechef.com/viewsolution/7729815 answered 17 Aug '15, 18:01
1
This is just because there was no test data which contain all the elements in either ascending or descending order. This should not have happened.
(18 Aug '15, 00:22)

Stock Span Approach to solve m(i) array was amazing . It just does in O(n) with help of stack. Thanks for editorial.I learned new thing today. I also solved with same algorithm but I didn't apply Stock Span approach.I solved it in O(NlogN) by using BST(Binary Search Tree) . It also worked and accepted. But Stock Span Approach is much much faster. answered 18 Aug '15, 18:25

@author I used same approach, but my code failed to pass last test cases,here is the solution link. https://www.codechef.com/viewsolution/7733279 answered 17 Aug '15, 22:08

I am getting TLE in DCGAME...... My code is here https://www.codechef.com/viewsolution/7848594 please help.Thnx in advance :) answered 17 Aug '15, 23:34

@antim_patel, This is the same problem which I and many of many friends were facing. Since your solution is quite similar to one, except the way you have calculated the frequency using stack, my suggest you to refer to my solutions https://www.codechef.com/viewsolution/7801472 (50 points) and https://www.codechef.com/viewsolution/7804811 (100 points). Since I needed to reduce the constant for nlogn, I changed the quries part a bit. Since inside the queries, binary search takes (logn) time and map further takes (logn) time, the constant factor is increased. Instead of the map, the queries can be answered in O(1) using arrays. Just store the map values into an array outside the loop. Also, I removed the sort function because it was not needed as the map stores the elements in sorted order only. Sort function has a large constant factor as well. Hope it helps. answered 18 Aug '15, 00:32

setter and tester solution Access Denied. Please fix this answered 18 Aug '15, 02:10

https://www.codechef.com/viewsolution/7843568 Help me, my code didnt even pass subtask 1 for some reason answered 18 Aug '15, 10:05

Same solution but because of unnecessary computation got TLE ... :( answered 18 Aug '15, 17:14

Would love some feedback on my code. https://www.codechef.com/viewsolution/7861684 I was able to pass all tasks except #7 (TLE). Using randomly generated test data w/ n, m = 1,000,000 my code spends majority of time in the binary searches. I am using imported bisect_right and bisect_left from imported library. I assume that is the fasted method? Is it just a problem of python being slow? Thanks so much for any feedback. answered 19 Aug '15, 00:51

hi, i solved the problem using divide and conquer with binary search . I was able to get 50 points , is it able to optimize my code at any point to get better time complexity ?? answered 19 Aug '15, 22:24

here is My Solution . It passes some subtask and fails for some subtask too..can anyone answered 19 Aug '15, 23:23

my java implementation of the editorial. Getting RE . Anyone can find out the problem ? answered 23 Aug '15, 12:06

please use fast IO to get last test case/ second last test case accepted. See the link for fast IO http://ideone.com/fOK6CW answered 26 Aug '15, 09:10

Any comments/suggestions on how I can optimize my solution to pass the last subtask ? I did stack span as well as binary search and linear sum as well. But last sub task is throwing TLE ! answered 27 Aug '15, 21:16

bhosri k editorialist bohot harami hote hai koi example nahi kuch nahi editorial k naam par aapni girlfriend ki kahani likh dete h... answered 03 Nov '15, 15:16
