結果
| 問題 |
No.1604 Swap Sort:ONE
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-05 03:53:11 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,115 bytes |
| コンパイル時間 | 2,382 ms |
| コンパイル使用メモリ | 214,844 KB |
| 最終ジャッジ日時 | 2025-01-23 14:10:31 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 |
ソースコード
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
// 1-indexed
template<typename T>
struct BIT{
int n;
vector<T> bit;
BIT(int n_=0):n(n_),bit(n+1){}
T sum(int i){
T res=0;
for(;i>0;i-=(i&-i))res+=bit[i];
return res;
}
void add(int i,T a){
if(i==0)return;
for(;i<=n;i+=(i&-i)){bit[i]+=a;}
}
int lower_bound(T k){ // k<=sum(res)
if(k<=0)return 0;
int res=0,i=1;
while((i<<1)<=n)i<<=1;
for(;i;i>>=1){
if(res+i<=n&&bit[res+i]<k)k-=bit[res+=i];
}
return res+1;
}
};
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<int> a(n);
for(int i=0;i<n;i++){
cin >> a[i];
}
int res=0;
BIT<int> bit(n);
for(int i=0;i<n;i++){
res+=i-bit.sum(a[i]);
bit.add(a[i],1);
}
cout << res << endl;
}