結果

問題 No.956 Number of Unbalanced
ユーザー waynetuinforwaynetuinfor
提出日時 2020-01-09 04:57:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,255 bytes
コンパイル時間 2,069 ms
コンパイル使用メモリ 206,200 KB
実行使用メモリ 535,928 KB
最終ジャッジ日時 2024-05-02 13:31:30
合計ジャッジ時間 16,463 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,144 KB
testcase_01 AC 4 ms
6,144 KB
testcase_02 AC 3 ms
6,144 KB
testcase_03 AC 4 ms
6,272 KB
testcase_04 AC 3 ms
6,272 KB
testcase_05 AC 4 ms
6,144 KB
testcase_06 AC 416 ms
14,080 KB
testcase_07 AC 456 ms
13,696 KB
testcase_08 AC 373 ms
13,972 KB
testcase_09 AC 294 ms
13,696 KB
testcase_10 AC 355 ms
13,568 KB
testcase_11 AC 534 ms
20,260 KB
testcase_12 AC 388 ms
13,992 KB
testcase_13 MLE -
testcase_14 AC 677 ms
14,720 KB
testcase_15 AC 571 ms
269,228 KB
testcase_16 AC 460 ms
14,208 KB
testcase_17 MLE -
testcase_18 AC 604 ms
14,720 KB
testcase_19 MLE -
testcase_20 AC 633 ms
14,848 KB
testcase_21 MLE -
testcase_22 AC 590 ms
269,224 KB
testcase_23 AC 472 ms
14,424 KB
testcase_24 MLE -
testcase_25 AC 399 ms
15,160 KB
testcase_26 AC 255 ms
14,480 KB
testcase_27 AC 414 ms
14,984 KB
testcase_28 AC 399 ms
15,168 KB
testcase_29 AC 638 ms
14,720 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];
vector<int> pos[kN];
vector<pair<long long*, long long>> hist;

void RollBack() {
    while (!hist.empty()) {
        *hist.back().first = hist.back().second;
        hist.pop_back();
    }
    hist.clear();
}

void Assign(long long *p, long long v) {
    hist.emplace_back(p, *p);
    *p = v;
}

void Apply(int o, long long f, long long t, int l, int r) {
    Assign(&cf[o], cf[o] + f);
    Assign(&ct[o], ct[o] + t);
    Assign(&st[o], st[o] + f * (l + r - 1) * (r - l) / 2);
    Assign(&st[o], 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], l, m);
    Apply(o * 2 + 2, cf[o], ct[o], m, r);
    Assign(&cf[o], 0);
    Assign(&ct[o], 0);
}

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, 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);
    Assign(&st[o], st[o * 2 + 1] + st[o * 2 + 2]);
}

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);
    }
    RollBack();
    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