Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 869B Kuriyama Mirai's Stones - Walkthrough

Two array variant

You have one array and two query types. For type 11 queries, you use the original array. For type 22 queries, you use the sorted (ascending) array. Build prefix sums for both: pref1pref1 on the original, pref2pref2 on the sorted copy.

Each query gives you ll and rr. The answer is pref[r]pref[l1]pref[r] - pref[l-1] using the appropriate prefix array. Sort once at the start in O(nlogn)O(n \log n), then answer each query in O(1)O(1).