zoukankan      html  css  js  c++  java
  • [CF1333C]Eugene and an array

    【原题】

    Eugene likes working with arrays. And today he needs your help in solving one challenging task.

    An array cc is a subarray of an array bb if cc can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

    Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array −1,2,−3 is good, as all arrays −1, −1,2, −1,2,−3, 2, 2,−3, −3 have nonzero sums of elements. However, array −1,2,−1,−3 isn't good, as his subarray −1,2,−1 has sum of elements equal to 00.

    Help Eugene to calculate the number of nonempty good subarrays of a given array aa.

     

    Input

    The first line of the input contains a single integer nn (1≤n≤2×1051≤n≤2×105) — the length of array aa.

    The second line of the input contains nn integers a1,a2,…,an (−109≤ai≤109−109≤ai≤109) — the elements of aa.

     

    Output

    Output a single integer — the number of good subarrays of a.

     

    Examples

    input

    Copy

    3
    1 2 -3

    output

    5

    input

    3
    41 -41 41

    output

    3

    Note

    In the first sample, the following subarrays are good: 1, 1,2, 2, 2,−3, −3. However, the subarray 1,2,−3 isn't good, as its subarray 1,2,−3 has sum of elements equal to 0.

    In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray 41,−41,41 isn't good, as its subarray 41,−41 has sum of elements equal to 0.

    【思路】

    前缀和, map记录前缀和是否出现过及其最后出现位置, num记录最后不可行位置。

     1 #include <algorithm>
     2 #include <cmath>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <list>
     6 #include <map>
     7 #include <iostream>
     8 #include <queue>
     9 #include <set>
    10 #include <stack>
    11 #include <string>
    12 #include <vector>
    13 #include <iomanip>
    14 #define LL long long
    15 #define inf 0x3f3f3f3f
    16 #define INF 0x3f3f3f3f3f3f
    17 #define PI 3.1415926535898
    18 #define F first
    19 #define S second
    20 #define lson  rt << 1
    21 #define rson  rt << 1 | 1
    22 using namespace std;
    23 
    24 const int maxn = 2e5 + 7;
    25 const int maxm = 1e4 + 7;
    26 int n, m;
    27 LL a[maxn], sum[maxn];
    28 map<LL, int> mp;
    29 
    30 LL read()
    31 {
    32     LL x = 0, f = 1;char ch = getchar();
    33     while (ch < '0' || ch>'9') { if (ch == '-')f = -1;ch = getchar(); }
    34     while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0';ch = getchar(); }
    35     return x * f;
    36 }
    37 
    38 int main()
    39 {
    40     ios::sync_with_stdio(false);
    41     cin.tie(0);
    42     n = read();
    43     for (int i = 1; i <= n; i++)
    44     {
    45         a[i] = read();
    46         sum[i] = sum[i - 1] + a[i];
    47     }
    48     LL cnt = 0;
    49     int num = 0;
    50     mp[0] = 0;
    51     for (int i = 1; i <= n; i++)
    52     {
    53         if (mp.count(sum[i])) num = max(mp[sum[i]] + 1, num);
    54         mp[sum[i]] = i;
    55         cnt += i - num;
    56     }
    57     cout << cnt << endl;
    58 }

     

  • 相关阅读:
    一周好文(15)
    一周好文(13)
    宏定义中的 "#" 和 "##"
    一周好文(14)
    CCRenderTexture崩溃问题分析
    C++反射机制的实现(转)
    一周好文(16)
    opengl on mac
    uthash: a hash table for C strcutures
    CC3LineNode线条变色的问题
  • 原文地址:https://www.cnblogs.com/hfcdyp/p/13357657.html
Copyright © 2011-2022 走看看