結果

問題 No.694 square1001 and Permutation 3
ユーザー parukiparuki
提出日時 2018-06-14 21:44:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,070 ms / 3,000 ms
コード長 2,201 bytes
コンパイル時間 1,724 ms
コンパイル使用メモリ 174,268 KB
実行使用メモリ 13,152 KB
最終ジャッジ日時 2023-09-13 04:16:45
合計ジャッジ時間 9,802 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 786 ms
12,664 KB
testcase_08 AC 1,017 ms
13,020 KB
testcase_09 AC 1,070 ms
13,152 KB
testcase_10 AC 758 ms
12,948 KB
testcase_11 AC 1,059 ms
13,004 KB
testcase_12 AC 1,055 ms
12,868 KB
testcase_13 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
#define vv(a,b,c,d) vector<vector<a> >(b,vector<a>(c,d))
#define vvv(a,b,c,d,e) vector<vector<vector<a> > >(b,vv(a,c,d,e))
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

template<class Val>
struct BinaryIndexedTree {
    int n;
    vector<Val> t;
    BinaryIndexedTree() {}
    BinaryIndexedTree(int _n) :n(_n + 1), t(_n + 1) {}
    void add(int k, Val val) {
        k++;
        while (k < n) {
            t[k] += val;
            k += (k&-k);
        }
    }
    void set(int k, Val val) {
        add(k, -sum(k, k + 1));
        add(k, val);
    }
    Val sum(int k) {
        Val r = 0;
        while (k > 0) {
            r += t[k];
            k -= (k&-k);
        }
        return r;
    }
    Val sum(int l, int r) {
        return sum(r) - sum(l);
    }
};

template<class Val>
vector<Val> compress(vector<Val> &v) {
    vector<Val> a = v;
    sort(all(a));
    a.erase(unique(all(a)), a.end());
    each(b, v)b = (int)(lower_bound(all(a), b) - a.begin());
    return a;
}

long long inversionNumber(vector<int> a) {
    compress(a);
    long long res = 0;
    int n = (int)a.size();
    BinaryIndexedTree<long long> bit(n + 1);
    for (int x : a) {
        res += bit.sum(x + 1, n + 1);
        bit.add(x, 1);
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    int N;
    cin >> N;
    vi A(N), B(N);
    rep(i, N) {
        cin >> A[i];
        B[i] = A[i];
    }
    sort(all(B));
    ll cnt = inversionNumber(A);

    rep(i, N) {
        cout << cnt << endl;
        cnt -= lower_bound(all(B), A[i]) - B.begin();
        cnt += B.end() - lower_bound(all(B), A[i] + 1);
    }
}
0