結果

問題 No.694 square1001 and Permutation 3
ユーザー finefine
提出日時 2018-09-28 13:46:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,374 bytes
コンパイル時間 2,216 ms
コンパイル使用メモリ 179,448 KB
実行使用メモリ 44,316 KB
最終ジャッジ日時 2024-04-20 09:23:41
合計ジャッジ時間 14,340 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

//引数unzipには圧縮前のvectorを入れる
int compress(vector<ll>& unzip, map<ll, int>& zip) {
    sort(unzip.begin(), unzip.end());
    unzip.erase(unique(unzip.begin(), unzip.end()), unzip.end());
    for (int i = 0; i < unzip.size(); i++) {
        zip[unzip[i]] = i;
    }
    return unzip.size();
}

//1-indexであることに注意
template <typename T>
struct BIT {
    int n;
    vector<T> data;

    BIT(int n) : n(n), data(n + 1, 0) {}

    T sum(int i) {
        T s = 0;
        while (i > 0) {
            s += data[i];
            i -= i & -i;
        }
        return s;
    }

    void add(int i, T x) {
        while (i <= n) {
            data[i] += x;
            i += i & -i;
        }
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<ll> unzip(a);
    map<ll, int> zip;
    compress(unzip, zip);

    int ans = 0;
    BIT<int> b(unzip.size());
    for (int i = 0; i < n; i++) {
        ans += i - b.sum(zip[a[i]] + 1);
        b.add((zip[a[i]] + 1), 1);
    }

    cout << ans << endl;
    for (int i = 0; i < n - 1; i++) {
        ans -= b.sum(zip[a[i]]);
        ans += n - b.sum(zip[a[i]] + 1);
        cout << ans << endl;
    }
    return 0;
}
0