結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー beetbeet
提出日時 2020-07-17 22:19:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,443 bytes
コンパイル時間 2,139 ms
コンパイル使用メモリ 200,748 KB
実行使用メモリ 7,128 KB
最終ジャッジ日時 2023-08-20 01:47:25
合計ジャッジ時間 4,388 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 22 ms
6,488 KB
testcase_04 AC 25 ms
6,952 KB
testcase_05 AC 22 ms
6,460 KB
testcase_06 AC 21 ms
6,164 KB
testcase_07 AC 25 ms
7,128 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 14 ms
5,408 KB
testcase_10 AC 24 ms
6,964 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 25 ms
7,056 KB
testcase_13 AC 25 ms
7,084 KB
testcase_14 AC 25 ms
6,948 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 10 ms
4,384 KB
testcase_25 AC 19 ms
6,008 KB
testcase_26 AC 6 ms
4,380 KB
testcase_27 AC 12 ms
4,896 KB
testcase_28 AC 16 ms
5,364 KB
testcase_29 AC 21 ms
6,200 KB
testcase_30 AC 24 ms
6,748 KB
testcase_31 AC 7 ms
4,380 KB
testcase_32 AC 5 ms
4,384 KB
testcase_33 AC 21 ms
6,460 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';


template<typename T>
struct BIT{
  Int n;
  vector<T> bit;
  // 1-indexed
  BIT(Int n_):n(n_+1),bit(n+1,0){}

  T sum(Int i){
    T s(0);
    for(Int x=i;x>0;x-=(x&-x))
      s+=bit[x];
    return s;
  }

  void add(Int i,T a){
    if(i==0) return;
    for(Int x=i;x<=n;x+=(x&-x))
      bit[x]+=a;
  }

  // [l, r)
  T query(Int l,Int r){
    return sum(r-1)-sum(l-1);
  }

  Int lower_bound(Int w){
    if(w<=0) return 0;
    Int x=0,r=1;
    while(r<n) r<<=1;
    for(Int k=r;k>0;k>>=1){
      if(x+k<=n&&bit[x+k]<w){
        w-=bit[x+k];
        x+=k;
      }
    }
    return x+1;
  }

  // 0-indexed
  T sum0(Int i){return sum(i+1);}
  void add0(Int i,T a){add(i+1,a);}
  T query0(Int l,Int r){return sum(r)-sum(l);}
};

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n;
  cin>>n;
  vector<Int> as(n),bs(n);
  for(Int i=0;i<n;i++) cin>>as[i];
  for(Int i=0;i<n;i++) cin>>bs[i];

  vector<Int> rev(n+1);
  for(Int i=0;i<n;i++) rev[as[i]]=i;

  vector<Int> cs(n);
  for(Int i=0;i<n;i++) cs[i]=rev[bs[i]];

  BIT<Int> bit(n+10);
  Int ans=0;
  for(Int i=0;i<n;i++){
    ans+=i-bit.sum0(cs[i]);
    bit.add0(cs[i],1);
  }
  cout<<ans<<newl;
  return 0;
}
0