cses

solution to cses exercise problems
git clone git://git.superpozycja.net/cses
Log | Files | Refs | README

commit 6fa9af6d38aa1ecc4961741ab850cc9a3b73f324
parent 8e6988fa09d48111bed9bcbb4f7baa9343950cf7
Author: superpozycja <anna@superpozycja.net>
Date:   Sun,  3 Nov 2024 23:49:20 +0100

solve subarray sums i

Diffstat:
Asorting_and_searching/subarray_sums_i.cpp | 49+++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 49 insertions(+), 0 deletions(-)

diff --git a/sorting_and_searching/subarray_sums_i.cpp b/sorting_and_searching/subarray_sums_i.cpp @@ -0,0 +1,49 @@ +#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; + int a[n]; + for (int i = 0; i < n; i++) { + cin >> a[i]; + } + int l = 0, r = 0; + ll s = a[0]; + int res = 0; + while (l < n && r < n) { + if (s == x) + res++; + + if (s >= x) { + s -= a[l]; + l++; + } else { + r++; + s += r < n ? a[r] : 0; + } + } + cout << res << "\n"; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + solve(); +}