結果
問題 | No.742 にゃんにゃんにゃん 猫の挨拶 |
ユーザー |
![]() |
提出日時 | 2020-06-25 09:48:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 40 ms / 2,500 ms |
コード長 | 1,970 bytes |
コンパイル時間 | 1,923 ms |
コンパイル使用メモリ | 198,016 KB |
最終ジャッジ日時 | 2025-01-11 10:04:08 |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
#include<bits/stdc++.h>using namespace std;#define ALL(x) x.begin(),x.end()#define rep(i,n) for(int i=0;i<(n);i++)#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;#define INF 1000000000#define mod 1000000007using ll=long long;const ll LINF=1001002003004005006ll;int dx[]={1,0,-1,0};int dy[]={0,1,0,-1};// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}template<typename T>struct BinaryIndexedTree{vector<T> data;BinaryIndexedTree(int sz){data.assign(sz+1, 0);}// 区間[0,k]の合計 sum(v[0]~v[k])T sum(int k){T ret=0;for(++k;k>0;k-=(k&-k)) ret+=data[k];return ret;}// v[a]+=xvoid add(int k,T x){for(++k;k<data.size();k+=(k&-k)) data[k]+=x;}// wを超える最小のv[i]のiを返す(0indexedのを返す)(data上のindexを返したいならreturn x+1)int lower_bound(T w){//T?if(w<=0) return 0;//左右どっちに行くか考えるint x=0,r=1;while(r<data.size()) r<<=1;for(int k=r;k>0;k>>=1){if(x+k<=data.size()&&data[x+k]<w){//左部分を引いて右へ,再起っぽいw-=data[x+k];x+=k;}}// return x+1;return x;}};//転倒数を求めるll inversion_number(vector<int> v){int n=v.size();if(n<=1){return 0;}int vmax=*max_element(ALL(v));BinaryIndexedTree<ll> bit(vmax+1);ll ret=0;rep(i,n){ret+=bit.sum(vmax)-bit.sum(v[i]);bit.add(v[i],1);}return ret;}signed main(){cin.tie(0);ios::sync_with_stdio(0);int n;cin>>n;vector<int> a(n);rep(i,n){cin>>a[i];a[i]--;}cout<<inversion_number(a)<<endl;return 0;}