結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー betrue12betrue12
提出日時 2020-07-17 21:30:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 1,374 bytes
コンパイル時間 2,073 ms
コンパイル使用メモリ 202,792 KB
実行使用メモリ 5,148 KB
最終ジャッジ日時 2023-08-19 23:48:03
合計ジャッジ時間 4,644 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 50 ms
4,688 KB
testcase_04 AC 57 ms
4,968 KB
testcase_05 AC 50 ms
4,576 KB
testcase_06 AC 46 ms
4,684 KB
testcase_07 AC 58 ms
4,840 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 33 ms
4,380 KB
testcase_10 AC 57 ms
5,148 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 58 ms
4,956 KB
testcase_13 AC 57 ms
5,012 KB
testcase_14 AC 57 ms
4,880 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 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,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 6 ms
4,376 KB
testcase_24 AC 21 ms
4,376 KB
testcase_25 AC 44 ms
4,380 KB
testcase_26 AC 11 ms
4,376 KB
testcase_27 AC 26 ms
4,376 KB
testcase_28 AC 35 ms
4,376 KB
testcase_29 AC 46 ms
4,684 KB
testcase_30 AC 55 ms
4,936 KB
testcase_31 AC 15 ms
4,376 KB
testcase_32 AC 10 ms
4,376 KB
testcase_33 AC 49 ms
4,900 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
struct BIT {
    int n;
    vector<T> dat;

    BIT(int n=0){
        initialize(n);
    }

    void initialize(int nin){
        n = nin;
        dat.assign(n, 0);
    }

    T sum(int i){
        T s = 0;
        while(i >= 0){
            s += dat[i];
            i = (i & (i+1)) - 1;
        }
        return s;
    }

    T sum_between(int i, int j){
        return sum(j) - sum(i-1);
    }

    void plus(int i, T x){
        while(i < n){
            dat[i] += x;
            i |= i+1;
        }
    }

    // a[0]+...+a[ret] >= x
    int lower_bound(T x){
        if(x < 0) return -1;
        int ret = -1;
        int k = 1;
        while(2*k <= n) k <<= 1;
        for( ;k>0; k>>=1){
            if(ret+k < n && dat[ret+k] < x){
                x -= dat[ret+k];
                ret += k;
            }
        }
        return ret + 1;
    }
};


int main(){
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    for(int i=0; i<N; i++) cin >> A[i], A[i]--;
    for(int i=0; i<N; i++) cin >> B[i], B[i]--;
    
    vector<int> R(N);
    for(int i=0; i<N; i++) R[B[i]] = i;
    for(int i=0; i<N; i++) A[i] = R[A[i]];

    BIT<int64_t> bit(N);
    int64_t ans = 0;
    for(int a : A){
        ans += bit.sum_between(a, N-1);
        bit.plus(a, 1);
    }
    cout << ans << endl;
    return 0;
}
0