tijie
2025-03-09 10:59:41
发布于:四川
#include <bits/stdc++.h>
#include<iostream>
using i64 = long long;
constexpr int M = 30;
int main() {
int n; stdcin >> n;
stdvector<int> a(n + 1);
for (int i = 1; i <= n; ++i)
stdcin >> a[i];
i64 res = n * (n + 1LL) / 2;
for (int x = 1; x <= M; ++x) {
stdvector<int> b(n + 1), s(n + 1);
for (int i = 1; i <= n; ++i)
b[i] = a[i] - x;
stdpartial_sum(b.begin(), b.end(), s.begin());
const int DELTA = n * M;
auto f = [&](int v) {return DELTA + v;};
stdvector<int> zero(DELTA * 2 + 1);
stdvector<int> nzero(DELTA * 2 + 1);
for (int i = 1; i <= n; ++i)
if (b[i] == 0) {
zero[f(s[i - 1])] += 1;
res -= zero[f(s[i])] + nzero[f(s[i])];
}
else {
nzero[f(s[i - 1])] += 1;
res -= zero[f(s[i])];
}
}
stdcout << res + 1 << "\n";
return 0;
}
这里空空如也
有帮助,赞一个