結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー soto800soto800
提出日時 2020-07-17 22:14:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 2,629 bytes
コンパイル時間 2,883 ms
コンパイル使用メモリ 169,548 KB
実行使用メモリ 6,416 KB
最終ジャッジ日時 2023-08-20 01:36:58
合計ジャッジ時間 5,640 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 50 ms
5,816 KB
testcase_04 AC 58 ms
6,416 KB
testcase_05 AC 49 ms
5,680 KB
testcase_06 AC 45 ms
5,664 KB
testcase_07 AC 59 ms
6,224 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 32 ms
4,836 KB
testcase_10 AC 55 ms
6,228 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 58 ms
6,212 KB
testcase_13 AC 57 ms
6,164 KB
testcase_14 AC 58 ms
6,284 KB
testcase_15 AC 1 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,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 6 ms
4,380 KB
testcase_24 AC 21 ms
4,376 KB
testcase_25 AC 46 ms
5,416 KB
testcase_26 AC 11 ms
4,380 KB
testcase_27 AC 26 ms
4,384 KB
testcase_28 AC 35 ms
4,952 KB
testcase_29 AC 44 ms
5,412 KB
testcase_30 AC 54 ms
6,016 KB
testcase_31 AC 15 ms
4,380 KB
testcase_32 AC 10 ms
4,376 KB
testcase_33 AC 49 ms
5,748 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 2 ms
4,376 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 lli long long int
#define REP(i,s,n) for(lli i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 1
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

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; }

 /* https://qiita.com/drken/items/1b7e6e459c24a83bb7fd */
template <class Abel> struct BIT {
    vector<Abel> dat;
    Abel UNITY_SUM = 0;                     // to be set

    /* [1, n] */
    BIT(lli n) { init(n); }
    void init(lli n) {
        dat.resize(n + 1);
        for (lli i = 0; i < (lli)dat.size(); ++i) dat[i] = UNITY_SUM;
    }

    /* a is 1-indexed */
    inline void add(lli a, Abel x) {
        for (lli i = a; i < (lli)dat.size(); i += i & -i)
            dat[i] = dat[i] + x;
    }

    /* [1, a], a is 1-indexed */
    inline Abel sum(lli a) {
        Abel res = UNITY_SUM;
        for (lli i = a; i > 0; i -= i & -i)
            res = res + dat[i];
        return res;
    }

    /* [a, b), a and b are 1-indexed */
    inline Abel sum(lli a, lli b) {
        return sum(b - 1) - sum(a - 1);
    }

    /* k-th number (k is 0-indexed) */
    lli get(long long k) {
        ++k;
        lli res = 0;
        lli N = 1; while (N < (lli)dat.size()) N *= 2;
        for (lli i = N / 2; i > 0; i /= 2) {
            if (res + i < (lli)dat.size() && dat[res + i] < k) {
                k = k - dat[res + i];
                res = res + i;
            }
        }
        return res + 1;
    }
};

lli con[100100];

void solve(){

    lli n;
    cin>>n;
    vector<lli> a(n),b(n);
    REP(i,0,n)cin>>a[i];
    REP(i,0,n){
        cin>>b[i];
        con[b[i]] = i+1;
    }

    REP(i,0,n){
        a[i] = con[a[i]];
    }

    lli ans = 0;
    BIT<lli> bits(n+10);

    REP(i,0,n){
        bits.add(a[i],1);
        ans += i+1-bits.sum(a[i]);
    }
    cout<<ans<<endl;
}

int main(){
    // cout << fixed << setprecision(5);

    lli t=1;
    //cin>>t;
    while(t--)solve();

    return 0;
}
0