結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
コンテスト
ユーザー hedwig100
提出日時 2020-05-09 11:31:30
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 7 ms / 2,500 ms
コード長 904 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,209 ms
コンパイル使用メモリ 182,348 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2026-03-26 15:39:18
合計ジャッジ時間 1,795 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i ++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PL;
typedef pair<int,int> P;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

template<class T> class BinaryIndexedTree {
    int N;
    vector<T> bit;
    
    public :
    BinaryIndexedTree(int _N): N(_N){
        bit = vector<T>(N + 1,0);
    }

    void add(int i,int x) {
        while (i <= N) {
            bit[i] += x;
            i += (i & -i);
        }
    }

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

int main() {
    int N; cin >> N;
    vector<int> M(N);
    rep(i,N) cin >> M[i];
    
    int ans = 0;
    BinaryIndexedTree<int> bit(N);
    rep(i,N) {
        ans += i - bit.sum(M[i]);
        bit.add(M[i],1);
    }
    cout << ans << endl;
}
0