結果

問題 No.2938 Sigma Sigma Distance Distance Problem
ユーザー InTheBloom
提出日時 2024-10-18 23:01:05
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,242 bytes
コンパイル時間 1,532 ms
コンパイル使用メモリ 128,368 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-18 23:02:31
合計ジャッジ時間 2,466 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <numeric>
#include <map>
#include <atcoder/segtree>

using namespace std;
using ll = long long;

using namespace atcoder;

using S = ll;

S f (S a, S b) {
    return a + b;
}

S op () {
    return 0LL;
}

int main () {
    int N; cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];

    segtree<S, f, op> acc_i(N + 1), acc_A(N + 1), acc_xAx(N + 1), acc_f(N + 1);
    for (int i = 1; i <= N; i++) {
        acc_i.set(i, i);
        acc_A.set(i, A[i - 1]);
        acc_xAx.set(i, i * A[i - 1]);
    }

    vector<int> ord(N);
    iota(ord.begin(), ord.end(), 1);
    sort(ord.begin(), ord.end(), [&] (int x, int y) { return A[x - 1] < A[y - 1]; });

    ll ans = 0;
    for (auto i : ord) {
        ll add = 0;
        add += 1LL * (N - i + 1 - 2 * acc_f.prod(i, N + 1)) * i * A[i - 1];
        add += acc_xAx.prod(i, N + 1);

        add -= i * acc_A.prod(i, N + 1);
        add -= A[i - 1] * acc_i.prod(i, N + 1);

        // 仮定が逆転してしまったものを直す。
        acc_i.set(i, -i);
        acc_A.set(i, -A[i - 1]);
        acc_xAx.set(i, -i * A[i - 1]);
        acc_f.set(i, 1);

        ans += add;
    }
    ans *= 2;

    cout << ans << "\n";
}
0