結果
問題 | No.1604 Swap Sort:ONE |
ユーザー |
|
提出日時 | 2021-07-16 21:33:08 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 11 ms / 2,000 ms |
コード長 | 905 bytes |
コンパイル時間 | 1,824 ms |
コンパイル使用メモリ | 194,948 KB |
最終ジャッジ日時 | 2025-01-23 01:26:26 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; struct fenwick_tree { typedef int T; T n; vector<T> bit; // 各要素の初期値は 0 fenwick_tree(T num) : bit(num+1, 0) { n = num; } // a_i += w void add(T i, T w) { for (T x = i; x <= n; x += x & -x) { bit[x] += w; } } // [1, i] の和を計算. T sum(T i) { T ret = 0; for (T x = i; x > 0; x -= x & -x) { ret += bit[x]; } return ret; } // [left+1, right] の和を計算. T sum(T left, T right) { return sum(right) - sum(left); } }; int main() { int n; cin >> n; vector<int> p(n); int ans = 0; for (int i = 0; i < n; i++) cin >> p[i]; fenwick_tree f_tree(n); for (int j = 0; j < n; j++) { ans += j - f_tree.sum(p[j]); f_tree.add(p[j], 1); } cout << ans << endl; }