結果

問題 No.956 Number of Unbalanced
ユーザー waynetuinforwaynetuinfor
提出日時 2020-01-09 05:05:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 373 ms / 2,000 ms
コード長 2,126 bytes
コンパイル時間 2,185 ms
コンパイル使用メモリ 202,640 KB
実行使用メモリ 17,268 KB
最終ジャッジ日時 2024-05-02 13:31:40
合計ジャッジ時間 8,374 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,144 KB
testcase_01 AC 3 ms
6,144 KB
testcase_02 AC 3 ms
6,272 KB
testcase_03 AC 3 ms
6,272 KB
testcase_04 AC 3 ms
6,144 KB
testcase_05 AC 3 ms
6,144 KB
testcase_06 AC 218 ms
14,208 KB
testcase_07 AC 234 ms
13,952 KB
testcase_08 AC 205 ms
14,080 KB
testcase_09 AC 156 ms
13,568 KB
testcase_10 AC 206 ms
13,696 KB
testcase_11 AC 280 ms
14,464 KB
testcase_12 AC 221 ms
14,208 KB
testcase_13 AC 127 ms
13,812 KB
testcase_14 AC 352 ms
14,848 KB
testcase_15 AC 121 ms
7,168 KB
testcase_16 AC 245 ms
14,464 KB
testcase_17 AC 160 ms
7,168 KB
testcase_18 AC 372 ms
14,848 KB
testcase_19 AC 135 ms
11,764 KB
testcase_20 AC 373 ms
14,848 KB
testcase_21 AC 136 ms
17,268 KB
testcase_22 AC 121 ms
7,168 KB
testcase_23 AC 245 ms
14,464 KB
testcase_24 AC 129 ms
12,996 KB
testcase_25 AC 191 ms
13,568 KB
testcase_26 AC 133 ms
13,568 KB
testcase_27 AC 192 ms
13,696 KB
testcase_28 AC 189 ms
13,568 KB
testcase_29 AC 362 ms
14,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int kN = 100'000 + 5;
const int kM = kN + kN;
long long st[kM * 4], cf[kM * 4], ct[kM * 4];
int a[kN], l[kN], r[kN];
bool cl[kM * 4];
vector<int> pos[kN];

void Apply(int o, long long f, long long t, bool c, int l, int r) {
    if (c) {
        cf[o] = ct[o] = st[o] = 0;
        cl[o] = true;
    }
    cf[o] += f;
    ct[o] += t;
    st[o] += f * (l + r - 1) * (r - l) / 2;
    st[o] += t * (r - l);
}

void Push(int o, int l, int r) {
    int m = (l + r) >> 1;
    Apply(o * 2 + 1, cf[o], ct[o], cl[o], l, m);
    Apply(o * 2 + 2, cf[o], ct[o], cl[o], m, r);
    cf[o] = 0;
    ct[o] = 0;
    cl[o] = false;
}

long long Query(int l, int r, int ql, int qr, int o = 0) {
    if (l >= qr || ql >= r) return 0;
    if (l >= ql && r <= qr) return st[o];
    Push(o, l, r);
    int m = (l + r) >> 1;
    return Query(l, m, ql, qr, o * 2 + 1) + Query(m, r, ql, qr, o * 2 + 2);
}

void Modify(int l, int r, int ql, int qr, long long f, long long t, int o = 0) {
    if (l >= qr || ql >= r) return;
    if (l >= ql && r <= qr) return Apply(o, f, t, false, l, r);
    Push(o, l, r);
    int m = (l + r) >> 1;
    Modify(l, m, ql, qr, f, t, o * 2 + 1);
    Modify(m, r, ql, qr, f, t, o * 2 + 2);
    st[o] = st[o * 2 + 1] + st[o * 2 + 2];
}

void Clear(int o) {
    st[o] = cf[o] = ct[o] = 0;
    cl[o] = true;
}

long long Solve(int v, int n) {
    int m = pos[v].size();
    for (int i = 0; i < m; ++i) {
        r[i] = pos[v][i] - 1;
        l[i + 1] = pos[v][i];
    }
    r[m] = n;
    long long res = 0;
    for (int i = 0; i <= m; ++i) {
        int ql = 2 * i - r[i] + kN, qr = 2 * i - l[i] + kN;
        res += Query(0, kM, ql - 1, qr);
        Modify(0, kM, qr + 1, kM, 0, qr - ql + 1);
        Modify(0, kM, ql, qr + 1, 1, 1 - ql);
    }
    Clear(0);
    return res;
}

int main() {
    int n; scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        pos[a[i]].push_back(i);
    }
    long long ans = 0;
    for (int i = 0; i < kN; ++i) {
        if (!pos[i].empty()) ans += Solve(i, n);
    }
    printf("%lld\n", ans);
    return 0;
}
0