結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 49 ms
4,488 KB
testcase_04 AC 56 ms
4,752 KB
testcase_05 AC 48 ms
4,436 KB
testcase_06 AC 44 ms
4,380 KB
testcase_07 AC 56 ms
4,832 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 32 ms
4,376 KB
testcase_10 AC 55 ms
4,688 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 56 ms
4,576 KB
testcase_13 AC 56 ms
4,748 KB
testcase_14 AC 56 ms
4,576 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 6 ms
4,380 KB
testcase_24 AC 20 ms
4,380 KB
testcase_25 AC 42 ms
4,376 KB
testcase_26 AC 11 ms
4,380 KB
testcase_27 AC 25 ms
4,380 KB
testcase_28 AC 34 ms
4,380 KB
testcase_29 AC 46 ms
4,384 KB
testcase_30 AC 54 ms
4,612 KB
testcase_31 AC 15 ms
4,376 KB
testcase_32 AC 10 ms
4,376 KB
testcase_33 AC 48 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,500 KB
testcase_39 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

struct FenwickTree{//入力は0インデックス
  vector<ll> d;
  FenwickTree(int n) : d(n, 0){}
  bool _addD(int index, ll add){
    if(index - 1 < 0 || index - 1 >= (int)d.size()) return false;
    d.at(index-1) += add;
    return true;
  }
  void add(int index, int n){
    index++;
    while(index != 0){
      bool ret = _addD(index, n);
      if(ret == false) break;
      index += index & (-index);
    }
  }
  ll refer(int index){
    index++;
    ll ret = 0;
    while(index != 0){
      if(index > (int)d.size() || index <= 0) break;
      ret += d.at(index-1);
      index -= index & (-index);
    }
    return ret;
  }
  ll refer(int l, int r){//[l, r]
    return refer(r) - refer(l-1);
  }
};


int main(){
  int n;
  cin >> n;
  vector<int> v(n);
  vector<int> a(n);
  rep(i, n){
    int in;
    cin >> in;
    a.at(i) = in;
  }
  rep(i, n){
    int in;
    cin >> in;
    v.at(in-1) = i;
  }
  rep(i, n){
    a.at(i) = v.at(a.at(i)-1);
    // cerr << a.at(i) << " ";
  }
  // cerr << endl;

  FenwickTree ft(n);
  ll ans = 0;
  rep(i, n){
    ans += i - ft.refer(a.at(i));
    // cerr << i << " " << a.at(i) << endl;
    ft.add(a.at(i), 1);
  }
  cout << ans << endl;
  return 0;
}
0