Olympiad in Informatics

BZOJ1176: [Balkan2007] Mokia — Solution

Main Idea

In this problem \(w\) is large, and we need two-dimensional sums. One workable approach is a nested data structure; the first idea is a Fenwick tree of dynamically allocated segment trees, with time complexity \(O(log^2 m)\). But the space is still large: you can see that the segment trees take up a huge amount of space, so consider replacing them with balanced BSTs, which shrinks the space enormously while the time complexity stays the same.

For further optimization, consider hashing the indices.