Soma

Problem Link

Solution by dudunotes only

Code / Notes

#include <bits/stdc++.h>
using namespace std;

int main() {
    int N, K;
    cin >> N >> K;

    vector<int> A(N);
    for (auto &x : A) cin >> x;

    vector<long long> P(N);
    for (int i = 0; i < N; i++) {
        if (i == 0) P[i] = A[i];
        else P[i] = P[i-1] + A[i];
    }

    map<long long, int> M;
    M[0]++;

    int ans = 0;
    for (int i = 0; i < N; i++) {
        // se o i fosse o r, quanto que teria que ser a psum do l-1
        long long want = P[i]-K;
        ans += M[want];

        M[P[i]]++;
    }

    cout << ans << endl;
}

Last updated 2 weeks, 3 days ago


« Back to problem