結果

問題 No.2956 Substitute with Average
ユーザー Xinyuan Huang
提出日時 2025-02-25 23:10:17
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 468 ms / 3,000 ms
コード長 968 bytes
コンパイル時間 3,205 ms
コンパイル使用メモリ 276,884 KB
実行使用メモリ 51,308 KB
最終ジャッジ日時 2025-02-25 23:10:30
合計ジャッジ時間 11,223 ms
ジャッジサーバーID
(参考情報)
judge7 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;

constexpr int M = 30;

int main() {
    int n; std::cin >> n;
    std::vector<int> a(n + 1);
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i];
    i64 res = n * (n + 1LL) / 2; 
    for (int x = 1; x <= M; ++x) {
        std::vector<int> b(n + 1), s(n + 1);
        for (int i = 1; i <= n; ++i)
            b[i] = a[i] - x;
        std::partial_sum(b.begin(), b.end(), s.begin());
        const int DELTA = n * M;
        auto f = [&](int v) {return DELTA + v;};
        std::vector<int> zero(DELTA * 2 + 1);
        std::vector<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])];
            }
        }
    }
    std::cout << res + 1 << "\n";
    return 0;
}
0