cses

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

subarray_sums_i.cpp (694B)


      1 #include <bits/stdc++.h>
      2 
      3 using namespace std;
      4 
      5 using ui = unsigned int;
      6 using l = long;
      7 using ul = unsigned long;
      8 using ll = long long;
      9 using ull = unsigned long long;
     10 
     11 using vi = vector<int>;
     12 using vui = vector<ui>;
     13 using vl = vector<l>;
     14 using vul = vector<ul>;
     15 using vll = vector<ll>;
     16 using vull = vector<ull>;
     17 
     18 void solve()
     19 {
     20 	int n, x;
     21 	cin >> n >> x;
     22 	int a[n];
     23 	for (int i = 0; i < n; i++) {
     24 		cin >> a[i];
     25 	}
     26 	int l = 0, r = 0;
     27 	ll s = a[0];
     28 	int res = 0;
     29 	while (l < n && r < n) {
     30 		if (s == x)
     31 			res++;
     32 
     33 		if (s >= x) {
     34 			s -= a[l];
     35 			l++;
     36 		} else {
     37 			r++;
     38 			s += r < n ? a[r] : 0;
     39 		}
     40 	}
     41 	cout << res << "\n";
     42 }
     43 
     44 int main()
     45 {
     46 	ios::sync_with_stdio(0);
     47 	cin.tie(0);
     48 	solve();
     49 }