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
#include<bits/stdc++.h>
using namespace std;
#define F first 
#define S second 
int main(){
    int n;
    cin>>n;
    vector<long long>uc(n);
    for(int i=0; i<n; i++){
        cin>>uc[i];
    }
    map<long long,int>buc;
    for(int i=0;i<n;i++){
        long long su=0;
        for(int j=i; j<n; j++){
            su+=uc[j];
            buc[su]++;
        }
    }
    int bsiz=buc.size();
    long long wyn=0;
    for(auto e0=buc.begin();e0!=buc.end();e0++){
        auto j=e0,k=prev(buc.end());
        while(1){
            long long sum= e0->F + j->F + k->F;
            if(sum==0)
            {
                if(j!=k&&j!=e0&&k!=e0)
                    wyn+= e0->S * j->S * k->S;
                else if(k==j&&j==e0)
                    wyn+=e0->S*(e0->S-1)*(e0->S-2)/6;
                else if(e0==j){
                    wyn+=e0->S*(e0->S-1)*k->S/2;
                }
                else if(k==j){
                    wyn+=e0->S*j->S*(j->S-1)/2;
                }
            }
            if(j==k)
                break;
            if(sum>0){
                k=prev(k);
            }
            else if(sum<=0){
                j=next(j);
            }
        }
    }
    cout<<wyn;
}