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(N^3)\)'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, \(S_j = \sum_{k = 1}^{k \le j}{a_k}\) dizisindeki kısmi toplamların önceden hesaplanmasıyla \(O(N^2)\)'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 \(S_j\) için \(S_1, S_2, ..., S_{j-1}\) arasındaki \(K - S_k\)'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(N \cdot log(N))\) olur.


Analysis and Guide for Solution

We can solve this problem using a brute force in \(O(N^3)\), i.e. iterating over all intervals and summing up the elements. The solution can be optimized to \(O(N^2)\) by precalculation of partial sums on the given array, \(S_j = \sum_{k = 1}^{k \le j}{a_k}\) 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 \(j^{th}\) partial sum \(S_j\) we need to count the number of partial sums among \(S_1, S_2, ..., S_{j-1}\) that are greater than \(K - S_k\). 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(N \cdot log(N))\).