結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー hedwig100hedwig100
提出日時 2020-07-17 21:55:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 1,472 bytes
コンパイル時間 1,466 ms
コンパイル使用メモリ 167,320 KB
実行使用メモリ 4,888 KB
最終ジャッジ日時 2023-08-20 00:54:30
合計ジャッジ時間 3,962 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 49 ms
4,380 KB
testcase_04 AC 56 ms
4,888 KB
testcase_05 AC 48 ms
4,380 KB
testcase_06 AC 45 ms
4,380 KB
testcase_07 AC 55 ms
4,572 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 32 ms
4,380 KB
testcase_10 AC 55 ms
4,876 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 57 ms
4,592 KB
testcase_13 AC 57 ms
4,528 KB
testcase_14 AC 57 ms
4,616 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 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,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 6 ms
4,380 KB
testcase_24 AC 21 ms
4,380 KB
testcase_25 AC 43 ms
4,376 KB
testcase_26 AC 10 ms
4,376 KB
testcase_27 AC 25 ms
4,376 KB
testcase_28 AC 35 ms
4,376 KB
testcase_29 AC 45 ms
4,380 KB
testcase_30 AC 54 ms
4,572 KB
testcase_31 AC 14 ms
4,380 KB
testcase_32 AC 10 ms
4,380 KB
testcase_33 AC 48 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,384 KB
testcase_39 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i ++)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;


template<class T> class BinaryIndexedTree {
    int N;
    vector<T> bit;
    
    public :
    BinaryIndexedTree(int _N): N(_N),bit(N + 1,0){
    }

    void add(int i,T x) {
        while (i <= N) {
            bit[i] += x;
            i += (i & -i);
        }
    }

    T sum(int i) {
        T ret = 0;
        while (i > 0) {
            ret += bit[i];
            i -= (i & -i);
        }
        return ret;
    }

    int lower_bound(T x) {
        if (x <= 0) return 0;
        int i = 0;
        int K = 1;
        while (2*K > N) K <<= 1;
        for (;K > 0; K >>= 1) {
            if (i + K <= N && bit[i + K] < x) {
                x -= bit[i + K];
                i += K;
            }
        }
        return i + 1;
    }
};

int main() {
    int N; cin >> N;
    vector<int> A(N),B(N),C(N + 1);
    rep(i,N) cin >> A[i];
    rep(i,N) cin >> B[i];
    rep(i,N) C[B[i]] = i;
    rep(i,N) A[i] = C[A[i]];
    

    BinaryIndexedTree<int> bit(N);
    ll ans = 0;
    rep(i,N) {
        ans += i - bit.sum(A[i] + 1);
        bit.add(A[i] + 1,1);
    }
    cout << ans << '\n';
    return 0;
}
0