Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 869B Kuriyama Mirai's Stones - Two Arrays

Sorted and original

The trick: build two prefix sum arrays.

1.1. pref1pref1 from the original array (for Type 1 queries)

2.2. pref2pref2 from the sorted array (for Type 2 queries)

Sort a copy of the array once at the start. Build prefix sums for both versions. Each query checks its type, then uses the corresponding prefix array. Time: O(nlogn)O(n \log n)

Time complexity: O(nlogn)O(n \log n) for sorting, O(n)O(n) for building both prefix arrays, O(1)O(1) per query. Total: O(nlogn+q)O(n \log n + q).

Space complexity: O(n)O(n) for storing both prefix arrays.