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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>

using namespace std;

int length;
int tab[500];
int newTab[250000];
int newLength = 0;

vector<int> values[2000007];

int found = 0;

int added = 1000004;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> length;

    for (int i = 0; i < length; ++i)
    {
        cin >> tab[i];
    }
    for (int i = 0; i < length; ++i)
    {
        int current = 0;
        for (int j = i; j < length; ++j)
        {
            current += tab[j];
            newTab[newLength] = current;
            ++newLength;
        }
    }
    for (int i = 0; i < newLength; ++i)
    {
        values[newTab[i] + added].push_back(i);
    }
    for (int i = 0; i < newLength; ++i)
    {
        for (int j = i + 2; j < newLength; ++j)
        {
            int searched = -(newTab[i] + newTab[j]);
            if (searched + added < 0 || searched + added >= 2000007 || values[searched + added].empty()) continue;
            auto it = upper_bound(values[searched + added].begin(), values[searched + added].end(), i);
            auto it2 = lower_bound(values[searched + added].begin(), values[searched + added].end(), j - 1);
            if (it2 == values[searched + added].end())
            {
                --it2;
            }
            if (it != values[searched + added].end())
            {
                if (*it2 <= i) continue;
                if (*it >= j) continue;
                if (*it2 >= j) --it2;
                found += distance(it, it2) + 1;
                // cout << i << ' ' << j << "   " << *it << ' ' << *it2 << ' ' << '\n';
            }
        }
    }
    cout << found << '\n';
}