-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1426d.cpp
More file actions
53 lines (37 loc) · 991 Bytes
/
Copy path1426d.cpp
File metadata and controls
53 lines (37 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// Idea: subarray sum from l to r will be 0 if psa[r] == psa[l - 1]. Once we
// "fix" a smaller range, any bigger range outside it will also be fixed.
// Iterate, and hold all prefix sums up to that value. If such a prefix sum
// exists, remove all prefix sums since we have fixed all subarrays up to there.
void solve() {
int n;
cin >> n;
vector<int> a(n + 2);
vector<ll> psa(n + 2);
for (int i = 1; i <= n; i++) {
cin >> a[i];
psa[i] = psa[i - 1] + a[i];
}
unordered_set<ll> p; // Holds PSA value
p.emplace(0);
int ans = 0;
for (int i = 1; i <= n; i++) {
if (p.contains(psa[i])) {
ans++;
p.clear();
}
p.emplace(psa[i - 1]);
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}