結果
| 問題 | No.742 にゃんにゃんにゃん 猫の挨拶 |
| コンテスト | |
| ユーザー |
kyuna
|
| 提出日時 | 2019-07-26 19:57:04 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 15 ms / 2,500 ms |
| コード長 | 1,307 bytes |
| 記録 | |
| コンパイル時間 | 809 ms |
| コンパイル使用メモリ | 77,520 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-02 06:32:04 |
| 合計ジャッジ時間 | 1,304 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
ソースコード
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
template <typename Abel> struct BIT {
const Abel UNITY_SUM = 0;
vector<Abel> dat;
BIT(int n) : dat(n + 1, UNITY_SUM) { } // [1, n]
inline void add(int i, Abel x) {
while (i < dat.size()) { dat[i] += x; i += i & -i; }
}
inline Abel sum(int i) { // [1, i]
Abel res = UNITY_SUM;
while (i > 0) { res += dat[i]; i -= i & -i; }
return res;
}
inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); } // [a, b)
/* debug */
Abel operator[](int i) { return sum(i, i + 1); }
void debug() {
for (int i = 1; i < dat.size(); ++i) cout << sum(i, i + 1) << ",";
cout << endl;
}
};
template<typename T> long long inversion_number(const vector<T> &a) {
int n = a.size();
vector<T> b = a;
sort(begin(b), end(b)); b.erase(unique(begin(b), end(b)), end(b));
long long res = 0;
BIT<int> bit(n);
for (T ai: a) {
int order = lower_bound(begin(b), end(b), ai) - begin(b) + 1;
res += bit.sum(order + 1, n + 1); bit.add(order, 1);
}
return res;
}
int main() {
int n; cin >> n;
vector<int> a(n);
for (auto &ai: a) cin >> ai;
cout << inversion_number(a) << endl;
return 0;
}
kyuna