結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー recososorecososo
提出日時 2020-07-17 21:43:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 2,598 bytes
コンパイル時間 1,659 ms
コンパイル使用メモリ 173,188 KB
実行使用メモリ 5,460 KB
最終ジャッジ日時 2023-08-20 00:21:59
合計ジャッジ時間 4,025 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 28 ms
5,376 KB
testcase_04 AC 32 ms
5,356 KB
testcase_05 AC 28 ms
5,200 KB
testcase_06 AC 25 ms
4,816 KB
testcase_07 AC 32 ms
5,416 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 14 ms
4,384 KB
testcase_10 AC 23 ms
5,404 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 31 ms
5,356 KB
testcase_13 AC 32 ms
5,460 KB
testcase_14 AC 32 ms
5,424 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 4 ms
4,384 KB
testcase_24 AC 12 ms
4,376 KB
testcase_25 AC 24 ms
4,932 KB
testcase_26 AC 7 ms
4,380 KB
testcase_27 AC 15 ms
4,380 KB
testcase_28 AC 19 ms
4,416 KB
testcase_29 AC 25 ms
4,828 KB
testcase_30 AC 30 ms
5,084 KB
testcase_31 AC 9 ms
4,380 KB
testcase_32 AC 6 ms
4,376 KB
testcase_33 AC 21 ms
5,088 KB
testcase_34 AC 2 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 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
template< typename T >
struct BIT {
  int N; int max_2beki;

  vector< T > data;
  // 初期化 1-indexedでデータを管理する 0で初期化
  BIT(int size){
      N = ++size;
      data.assign(N, 0);
      max_2beki = 1;
      while(max_2beki * 2 <= N) max_2beki *= 2;
  }

  // [0,k](閉区間)の総和 閉区間に注意!
  T sum(int k) {
    if(k < 0) return 0; // k<0のとき0を返す
    T ret = 0;
    for(++k; k > 0; k -= k & -k) ret += data[k];
    return (ret);
  }

  // [l,r](閉区間)の総和
  inline T sum(int l,int r){
    return sum(r) - sum(l-1);
  }

  // 一点取得 更新はできないことに注意
  inline T operator[](int k){
    return sum(k) - sum(k-1);
  }

  // data[k] += x;
  void add(int k, T x) {
    for(++k; k < N; k += k & -k) data[k] += x;
  }

  // imos法 [l,r]にxを加算
  void imos(int l,int r,T x){
    add(l , x); add(r + 1 , -x);
  }

  // lower_bound sum(i)がval以上となる最小のi
  int lower_bound(T w){
    if(w <= 0) return 0;
    int x = 0;
    for(int k = max_2beki; k > 0; k /= 2){
      if(x+k <= N - 1 && data[x + k] < w){
        w -= data[x + k];
        x += k;
      }
    }
    return x;
  }

  // upper_bound sum(i)がvalより大きくなる最小のi
  int upper_bound(T w){
    if(w < 0) return 0;
    int x = 0;
    for(int k = max_2beki; k > 0; k /= 2){
      if(x+k <= N - 1 && data[x + k] <= w){
        w -= data[x + k];
        x += k;
      }
    }
    return x;
  }

};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;cin >> n;
    vector<int> a(n),b(n),c(n);
    REP(i,n){
        cin >> a[i];
        a[i]--;
    }
    REP(i,n){
        cin >> b[i];
        b[i]--;
        c[b[i]]=i;
    }
    vector<pair<int,int>> p(n);
    REP(i,n){
        p[i]={c[a[i]],i};
    }
    sort(ALL(p));
    BIT<int> bit(n);
    ll ans=0;
    REP(i,n){
        ans+=bit.sum(p[i].second,n-1);
        bit.add(p[i].second,1);
    }
    cout << ans << endl;
}
0