commit 41b03d22ddc1807e9e1b865b3b68df5e17b71c2f parent 6fa9af6d38aa1ecc4961741ab850cc9a3b73f324 Author: superpozycja <anna@superpozycja.net> Date: Sun, 3 Nov 2024 23:50:30 +0100 solve subarray sums ii Diffstat:
A | sorting_and_searching/subarray_sums_ii.cpp | | | 43 | +++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 43 insertions(+), 0 deletions(-)
diff --git a/sorting_and_searching/subarray_sums_ii.cpp b/sorting_and_searching/subarray_sums_ii.cpp @@ -0,0 +1,43 @@ +#include <bits/stdc++.h> + +using namespace std; + +using ui = unsigned int; +using l = long; +using ul = unsigned long; +using ll = long long; +using ull = unsigned long long; + +using vi = vector<int>; +using vui = vector<ui>; +using vl = vector<l>; +using vul = vector<ul>; +using vll = vector<ll>; +using vull = vector<ull>; + +void solve() +{ + int n, x; + cin >> n >> x; + + map<ll, int> a; + a[0] = 1; + ll lst = 0; + ll res = 0; + for (int i = 0; i < n; i++) { + int z; + cin >> z; + lst += z; + res += a[lst - x]; + a[lst]++; + } + + cout << res << "\n"; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + solve(); +}