結果
| 問題 | No.742 にゃんにゃんにゃん 猫の挨拶 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2018-10-06 00:26:19 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 5 ms / 2,500 ms | 
| コード長 | 768 bytes | 
| コンパイル時間 | 1,627 ms | 
| コンパイル使用メモリ | 168,912 KB | 
| 実行使用メモリ | 6,820 KB | 
| 最終ジャッジ日時 | 2024-10-12 14:00:01 | 
| 合計ジャッジ時間 | 2,099 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//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;
    BIT<int> b(n);
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        int m;
        cin >> m;
        ans += i - b.sum(m);
        b.add(m, 1);
    }
    cout << ans << endl;
    return 0;
}
            
            
            
        