Let's process both our queries and our elements in a left-to-right manner, something like
for (int i = 0; i < N; ++i) {
// 1. Add a[i] to all internal data structures
// 2. Calculate answers for all queries q such that r[q] == i
}
Here we have O(N)
iterations of this loop and we want to do both update of the data structure and query the answer for suffix of currently processed part in o(N)
time.
Let's use the array contains[i][j]
which has 1
if suffix starting at the position i
contains number j
and 0
otherwise. Consider also that we have calculated prefix sums for each contains[i]
separately. In this case we could answer each particular suffix query in O(log N)
time using binary search: we should just find the first zero in the corresponding contains[l[i]]
array which is exactly the first position where the partial sum is equal to index, and not to index + 1. Unfortunately, such arrays would take O(N^2)
space and need O(N^2)
time for each update.
So, we have to optimize. Let's build a 2-dimensional range tree with "sum query" and "assignment" range operations. In such tree we can query sum on any sub-rectangle and assign the same value to all the elements of any sub-rectangle in O(log^2 N)
time, which allows us to do the update in O(log^2 N)
time and queries in O(log^3 N)
time, giving the time complexity O(Nlog^2 N + Qlog^3 N)
. The space complexity O((N + Q)log^2 N)
(and the same time for initialization of the arrays) is achieved using lazy initialization.
UP: Let's revise how the query works in range trees with "sum". For 1-dimensional tree (to not make this answer too long), it's something like this:
class Tree
{
int l, r; // begin and end of the interval represented by this vertex
int sum; // already calculated sum
int overriden; // value of override or special constant
Tree *left, *right; // pointers to children
}
// returns sum of the part of this subtree that lies between from and to
int Tree::get(int from, int to)
{
if (from > r || to < l) // no intersection
{
return 0;
}
if (l <= from && to <= r) // whole subtree lies within the interval
{
return sum;
}
if (overriden != NO_OVERRIDE) // should push override to children
{
left->overriden = right->overriden = overriden;
left->sum = right->sum = (r - l) / 2 * overriden;
overriden = NO_OVERRIDE;
}
return left->get(from, to) + right->get(from, to); // split to 2 queries
}
Given that in our particular case all queries to the tree are prefix sum queries, from
is always equal to 0
, so, one of the calls to children always return a trivial answer (0
or already computed sum
). So, instead of doing O(log N)
queries to the 2-dimensional tree in the binary search algorithm, we could implement an ad-hoc procedure for search, very similar to this get
query. It should first get the value of the left child (which takes O(1)
since it's already calculated), then check if the node we're looking for is to the left (this sum is less than number of leafs in the left subtree) and go to the left or to the right based on this information. This approach will further optimize the query to O(log^2 N)
time (since it's one tree operation now), giving the resulting complexity of O((N + Q)log^2 N))
both time and space.
Not sure this solution is fast enough for both Q
and N
up to 10^5
, but it may probably be further optimized.