結果

問題 No.694 square1001 and Permutation 3
ユーザー merom686merom686
提出日時 2018-06-08 22:48:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 3,000 ms
コード長 1,270 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 9,036 KB
最終ジャッジ日時 2023-09-13 00:02:56
合計ジャッジ時間 4,258 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 154 ms
9,004 KB
testcase_08 AC 171 ms
8,716 KB
testcase_09 AC 180 ms
8,700 KB
testcase_10 AC 104 ms
8,740 KB
testcase_11 AC 182 ms
9,036 KB
testcase_12 AC 180 ms
8,772 KB
testcase_13 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct BIT {
    BIT(int n) : b(n + 1), n(n) {}
    void add(int i, int v) {
        for (int k = i + 1; k <= n; k += k & -k) b[k] += v;
    }
    int sum(int k) {
        int s = 0;
        for (; k > 0; k -= k & -k) s += b[k];
        return s;
    }
    vector<int> b;
    int n;
};

void coco(vector<int>& v) {
    size_t n = v.size();
    vector<pair<int, int>> t(n);
    for (int i = 0; i < n; i++) {
        t[i] = { v[i], i };
    }
    sort(t.begin(), t.end());
    int k = 0;
    for (int i = 0; i < n; i++) {
        if (i > 0 && t[i].first != t[i - 1].first) k++;
        v[t[i].second] = k;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    coco(a);

    ll s = 0;
    BIT bt(n);
    for (int i = 0; i < n; i++) {
        s += i - bt.sum(a[i] + 1);
        bt.add(a[i], 1);
    }

    for (int i = 0; i < n; i++) {
        cout << s << '\n';
        s -= bt.sum(a[i]);
        s += n - bt.sum(a[i] + 1);
    }

    cout << flush;

    return 0;
}
0