Help needed to solve JOI 2013/2014 Problem — Historical

Revision en2, by Lance_HAOH, 2017-07-15 16:50:43

Hi. I am having problem trying to solve JOI 2013/2914 — Historical Research. The english problem statement can be found here.

For those who can read Japanese, the editorial can be found here

The input and output data can be found here

My approach is as follows:

We use an additional BST to answer our max product queries and element updates efficiently. Hence, each element of the BST would be <product,element,frequency>.

Let N be the number of elements in our array and Q be the number of queries.

Perform square root decomposition on the queries by breaking them into blocks and sorting them in increasing order of left bound followed by increasing order of right bound. Time-complexity of this operation is O(Q × logQ).

We keep 2 pointers to track the left and right bound of the subarray that we have calculated the element frequency for. Every time we shift the left pointer, we remove the elements from the left side of the window from the BST. Every time we shift the right pointer, we add the new elements in the window to the BST.

Of course, we have to update the product accordingly. Complexity of this operation is . This is because each query can only be in one block. Hence, the left pointer can only move by on each query (i.e. in total) and the right pointer moves by O(N) in every block and we have blocks. Hence, right pointer moves by in total. Each movement incurs an update operation in the BST which is O(logN)

Every time we need to query for

Tags joi 2014, mos_algorithm, sliding window, two-pointers

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English Lance_HAOH 2017-07-15 17:21:30 946 Tiny change: ' \right) $\n' -> ' \right) $. However, $ 1 \le Q,N \le 100000 $\n' (published)
en2 English Lance_HAOH 2017-07-15 16:50:43 1331 Tiny change: 'ht bound. Complexity ' -> 'ht bound. Time-complexity '
en1 English Lance_HAOH 2017-07-15 15:13:52 633 Initial revision (saved to drafts)