#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(1);
// cin.tie(0);
int n;
cin>>n;
vector<int> w(n+1, 0);
for(int i = 1; i <= n; i++){
cin>>w[i];
if(i){w[i]+=w[i-1];}
// cout<<"D";
}
// cout<<"d\n";
vector<pair<int, int>> s;
map<int, int> m;
// cout<<"d\n";
for(int i = 1; i < n+1; i++){
for(int j = 0; j < n-i+1; j++){
m[w[j+i]-w[j]]++;
}
}
for(auto v : m){
s.push_back(v);
}
// cout<<"d\n";
sort(s.begin(), s.end());
// cout<<s.size()<<" d\n";
int o = 0;
for (int i = 0; i < s.size() - 2; i++) {
int l = i + 1;
int r = s.size() - 1;
while (l < r) {
if (s[i].first + s[l].first + s[r].first == 0) {
// cout<<s[i].first<<" "<<s[l].first<<" "<<s[r].first<<"\n";
o+=s[i].second*s[l].second*s[r].second;
r--;
}
else if (s[i].first + s[l].first + s[r].first < 0){
l++;
}
else{
r--;
}
}
}
cout<<o<<"\n";
}
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 54 55 56 57 58 59 60 61 62 63 64 | #include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(1); // cin.tie(0); int n; cin>>n; vector<int> w(n+1, 0); for(int i = 1; i <= n; i++){ cin>>w[i]; if(i){w[i]+=w[i-1];} // cout<<"D"; } // cout<<"d\n"; vector<pair<int, int>> s; map<int, int> m; // cout<<"d\n"; for(int i = 1; i < n+1; i++){ for(int j = 0; j < n-i+1; j++){ m[w[j+i]-w[j]]++; } } for(auto v : m){ s.push_back(v); } // cout<<"d\n"; sort(s.begin(), s.end()); // cout<<s.size()<<" d\n"; int o = 0; for (int i = 0; i < s.size() - 2; i++) { int l = i + 1; int r = s.size() - 1; while (l < r) { if (s[i].first + s[l].first + s[r].first == 0) { // cout<<s[i].first<<" "<<s[l].first<<" "<<s[r].first<<"\n"; o+=s[i].second*s[l].second*s[r].second; r--; } else if (s[i].first + s[l].first + s[r].first < 0){ l++; } else{ r--; } } } cout<<o<<"\n"; } |
English