Подмассив подсчета имеет неотрицательную суммуC++

Программы на C++. Форум разработчиков
Anonymous
Подмассив подсчета имеет неотрицательную сумму

Сообщение Anonymous »


Is there any optimal solution (better than O(n^2) to count sub-arrays have non-negative sum?

I've try by using prefix sum and set to count the number of the subarray but it meets TLE because time complexity of distance function is linear:
multiset a for(int i=1;i>tmp; sum=sum[i-1]+tmp; } ll ans=0; for(int i=1;i0) { auto it = a.upper_bound(sum); ans+=(distance(a.begin(),it; } if(sum>=0) ++ans; a.insert(sum); }

Источник: https://stackoverflow.com/questions/780 ... gative-sum

Вернуться в «C++»