Editorial for Problem for Cengiz


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.
Analiz ve Çözüm Yolu

Bu problemi O(N3)'te kaba kuvvet(brute force) kullanarak çözebiliriz, yani tüm aralıkların üzerinde gezerek elemanları toplayabiliriz. Çözüm, bir aralık toplamını O(1)'de hesaplamaya yarayan, Sj=kjk=1ak dizisindeki kısmi toplamların önceden hesaplanmasıyla O(N2)'ye optimize edilebilir.

Fakat bu hala zaman kısıtlamasına uymak için yeterli değil. Verilen dizi üzerinde ilerleyip kısmi toplamları hesaplayalım. j 'yinci kısmi toplam Sj için S1,S2,...,Sj1 arasındaki KSk'den büyük kısmi toplamları saymalıyız. Bu segment ağacı veya multiset gibi herhangi bir dengeli ağaç veri yapısı kullanılarak O(log(N) de yapılabilir. Böylece genel karmaşıklık O(Nlog(N)) olur.


Analysis and Guide for Solution

We can solve this problem using a brute force in O(N3), i.e. iterating over all intervals and summing up the elements. The solution can be optimized to O(N2) by precalculation of partial sums on the given array, Sj=kjk=1ak which allows to compute an interval sum in O(1).

However it's still not enough to fit into the time limit. Let's iterate over the given array and compute partial sums. For jth partial sum Sj we need to count the number of partial sums among S1,S2,...,Sj1 that are greater than KSk. This can be done in O(log(N) by using any balanced tree data structure like a segment tree or multiset. Thus the overall complexity is O(Nlog(N)).