4.9 a daily problem solution to a problem

Eugene and an array

Involving knowledge points:

  • Double pointer / prefix and

solution:

  • \ (The meaning of problems simply summarized as to give the array a, b let you find all of the array, the array b need to meet two conditions: \)
  • \ (Condition 1, b array is a subarray of the array (required continuous) \)
  • \ (Condition 2, b subarray of the array (required continuous) is not equal to 0 and the sum \)
  • \ (N = 1e5, violence unsolvable \)
  • \ (Subarray is continuous because, assuming [1, x], and as a, [1, y] and also is a, it shows [x + 1, y] is 0, and this piece array \)
  • $ According to this feature, the right fixing point y, we can only take the left point x + 2, x + 3, x + 4 ...... y $
  • \ (Taken because if left point x + 1 or less, [x + 1, Y] array and this piece is 0, the condition is not satisfied \)
  • \ (So the approach is: O (n) to enumerate every right point, save it with a map and a prefix, the maximum recorded at the left point, update the answer \)

std:

#include <bits/stdc++.h>
using namespace std;
#define ll long long 
const int maxn = 200005;
ll a[maxn],pre[maxn];
int main()
{
    int n , l = 0;
    ll ans = 0;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i],pre[i] = pre[i-1] + a[i];
    map<ll , int > mp;
    mp[0] = 0;
    for(int i=1;i<=n;i++){
        if(mp[pre[i]] || pre[i] == 0)
            l = max(l , mp[pre[i]] + 1);
        ans += 1ll*(i - l);
        mp[pre[i]] = i;
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/QFNU-ACM/p/12666177.html