zoukankan      html  css  js  c++  java
  • Eugene and an array CodeForces

    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][−1,2,−3] is good, as all arrays [1][−1], [1,2][−1,2], [1,2,3][−1,2,−3], [2][2], [2,3][2,−3], [3][−3] have nonzero sums of elements. However, array [1,2,1,3][−1,2,−1,−3] isn't good, as his subarray [1,2,1][−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 (1n2×1051≤n≤2×105)  — the length of array aa.

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

    Output

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

    Examples

    Input
    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], [1,2][1,2], [2][2], [2,3][2,−3], [3][−3]. However, the subarray [1,2,3][1,2,−3] isn't good, as its subarray [1,2,3][1,2,−3] has sum of elements equal to 00.

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

    #include <bits/stdc++.h>
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include<cstring>
    #include <algorithm>
    #include <queue>
    #include<map>
    #include<set>
    #include<vector>
    using namespace std;
    typedef long long ll;
    const int inf = 1e9;
    const int mod = 1000000007;
    const int mx = 5e5+10; //check the limits, dummy
    typedef pair<int, int> pa;
    const double PI = acos(-1);
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(int i=(a),_=(b);i>=_;i--)
    #define clr(a) memset(a, -1, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pai
    void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    ll n, m, k,ans=0,t,p,x;
    int a[mx];
    
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);    
        cin >> n;
        vector<ll>pre(n+1,0);
        re(i, 0, n) {
            cin >> x;
            pre[i + 1] = pre[i] + x;
        }
        int start = 0, end = 0;
        set<ll>s= { 0 };
        while (start<n)
        {
            while (end < n && !s.count(pre[end + 1])) {
                ++end;
                s.insert(pre[end]);
            }
            ans += end - start;
            s.erase(pre[start]);
            ++start;
        }
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    URLProtocol服务协议
    ODBC、OLEDB和ADO之间的关系 ,以及性能比较
    如何在VS2015查看C#界面窗体里的控件层次
    SpeechVoiceSpeakFlags枚举类型的详细解释
    SQL中遇到多条相同内容只取一条的最简单实现方法
    flink elasticsearch sink table 忽略部分字段开发
    flink elasticsearch source table 集成elasticsearch-hadoop connector开发
    记一次python 协程给合多线程死锁问题
    kubernetes gitlab runner java maven ci/cd 整体方案示例
    某云elasticsearch节点失效,手动重置primary,迁移分区
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12768082.html
Copyright © 2011-2022 走看看