Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- #ifndef ONLINE_JUDGE
- freopen("split_in.txt", "r", stdin);
- #endif
- int n;
- cin >> n;
- ll arr[n] = {};
- cin >> arr[0];
- ll zeros = 0;
- if (arr[0] == 0)
- zeros = 1;
- for (int i = 1; i < n; ++i)
- {
- ll k;
- cin >> k;
- arr[i] = arr[i - 1] + k;
- if (arr[i] == 0)
- ++zeros;
- }
- if (arr[n - 1] % 3 != 0 || n < 3)
- {
- cout << 0;
- return 0;
- }
- if (arr[n - 1] == 0)
- {
- zeros -= 2;
- cout << zeros * (zeros + 1) / 2;
- return 0;
- }
- ll size = arr[n - 1] / 3;
- ll count1[n] = {}, temp[n + 1] = {};
- int index1 = 0, index2 = 0;
- for (int i = 0; i < n; ++i)
- {
- if (arr[i] == size)
- {
- if (temp[index1] == 0)
- ++count1[index1];
- else
- ++count1[++index1];
- }
- else if (arr[i] == 2 * size)
- {
- if (count1[index2 + 1] == 0)
- {
- if (count1[index2] > 0)
- ++temp[index2];
- }
- else
- ++temp[++index2];
- }
- }
- int count2[index2 + 1] = {};
- for (int i = 0; i < index2 + 1; ++i)
- count2[i] = temp[index2 - i] + temp[index2 - i + 1];
- ll sum = 0;
- for (int i = 0; i < index2 + 1; ++i)
- sum += count1[i] * count2[index2 - i];
- cout << sum;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement