Untitled
unknown
plain_text
a year ago
2.0 kB
5
Indexable
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<long long> A(n);
for(int i=0;i<n;i++) cin >> A[i];
// Compute prefix sums
vector<long long> prefix(n+1, 0);
for(int i=1;i<=n;i++) prefix[i] = prefix[i-1] + A[i-1];
// Build map from sum to list of indices
// sum -> list of j where prefix[j] = sum
// j ranges from 0 to n
unordered_map<long long, vector<int>> sum_map;
for(int j=0; j<=n; j++) {
sum_map[prefix[j]].push_back(j);
}
bool found = false;
for(int i=0;i<n;i++){
long long S = prefix[n] - A[i];
if(S %2 !=0) continue;
long long target = S /2;
// Check if target exists in prefix[j] where j <i
if(sum_map.find(target) != sum_map.end()){
const vector<int> &lst = sum_map[target];
// Need j <=n, and j <i
// Find if there's any j in lst where j <i
// Using upper_bound
// lst is sorted
// Find first j >=i
// Number of j <i is lower_bound(lst.begin(), lst.end(), i) - lst.begin()
int cnt = lower_bound(lst.begin(), lst.end(), i) - lst.begin();
if(cnt >0){
found = true;
break;
}
}
// Check if target + A[i] exists in prefix[j} where j >=i
long long target_plus = target + A[i];
if(sum_map.find(target_plus) != sum_map.end()){
const vector<int> &lst = sum_map[target_plus];
// Find if there's any j >=i
// Using lower_bound
// Find first j >=i
int pos = lower_bound(lst.begin(), lst.end(), i) - lst.begin();
if(pos < lst.size()){
found = true;
break;
}
}
}
if(found) cout << "Yes\n";
else cout << "No\n";
}
Editor is loading...
Leave a Comment