結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー mugen_1337mugen_1337
提出日時 2020-07-18 00:57:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 2,461 bytes
コンパイル時間 2,827 ms
コンパイル使用メモリ 211,152 KB
実行使用メモリ 10,288 KB
最終ジャッジ日時 2023-08-20 07:07:51
合計ジャッジ時間 5,941 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 75 ms
9,084 KB
testcase_04 AC 90 ms
9,876 KB
testcase_05 AC 76 ms
9,088 KB
testcase_06 AC 68 ms
8,576 KB
testcase_07 AC 88 ms
9,824 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 33 ms
6,992 KB
testcase_10 AC 59 ms
10,240 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 89 ms
10,120 KB
testcase_13 AC 91 ms
10,288 KB
testcase_14 AC 93 ms
10,172 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 28 ms
5,428 KB
testcase_25 AC 62 ms
8,240 KB
testcase_26 AC 12 ms
4,504 KB
testcase_27 AC 34 ms
6,140 KB
testcase_28 AC 48 ms
7,280 KB
testcase_29 AC 64 ms
8,520 KB
testcase_30 AC 81 ms
9,612 KB
testcase_31 AC 18 ms
4,868 KB
testcase_32 AC 12 ms
4,384 KB
testcase_33 AC 69 ms
9,108 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 mod 1000000007
using ll=long long;
const int INF=1000000000;
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;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;

template<typename T1,typename T2>
ostream &operator<<(ostream &os,const pair<T1,T2>&p){
    os<<p.first<<" "<<p.second;
    return os;
}

template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==v.size()?"":" ");
    return os;
}

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]+=x
   void 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(){
    int n;cin>>n;
    int a[n],b[n];
    rep(i,n) cin>>a[i];
    rep(i,n) cin>>b[i];

    map<int,int> ai;
    rep(i,n) ai[a[i]]=i;

    vector<int> c(n);
    rep(i,n)c[i]=ai[b[i]];

    cout<<inversion_number(c)<<endl;
    return 0;
}
0