結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー coco18000coco18000
提出日時 2020-07-17 21:47:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,664 bytes
コンパイル時間 1,459 ms
コンパイル使用メモリ 168,220 KB
実行使用メモリ 5,548 KB
最終ジャッジ日時 2023-08-20 00:30:41
合計ジャッジ時間 3,561 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 22 ms
5,052 KB
testcase_04 AC 26 ms
5,352 KB
testcase_05 AC 22 ms
5,152 KB
testcase_06 AC 20 ms
4,940 KB
testcase_07 AC 25 ms
5,316 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 13 ms
4,380 KB
testcase_10 AC 23 ms
5,464 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 26 ms
5,368 KB
testcase_13 AC 26 ms
5,548 KB
testcase_14 AC 26 ms
5,320 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 1 ms
4,376 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 3 ms
4,384 KB
testcase_24 AC 9 ms
4,380 KB
testcase_25 AC 19 ms
4,844 KB
testcase_26 AC 6 ms
4,380 KB
testcase_27 AC 11 ms
4,380 KB
testcase_28 AC 15 ms
4,392 KB
testcase_29 AC 20 ms
4,872 KB
testcase_30 AC 24 ms
5,056 KB
testcase_31 AC 7 ms
4,376 KB
testcase_32 AC 6 ms
4,376 KB
testcase_33 AC 20 ms
5,288 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

struct BinaryIndexTree {
public:
    BinaryIndexTree(int n)
            : mN(n) {
        mPow = 1;
        while (mPow < n)
            mPow <<= 1;
        node.resize(n + 1);
        REP(i, n + 1) {
            node[i] = 0;
        }
    }

    ~BinaryIndexTree() {
        node.resize(0);
    }

    //! 1-index get
    ll sum(int index) {
        ll sum = 0;
        for (int i = index; i > 0; i -= i & (-i))
            sum += node[i];
        return sum;
    }

    //! 1-index add
    void add(int index, ll value) {
        for (int i = index; i <= mN; i += i & (-i))
            node[i] += value;
    }

    int lowerBound(ll value) {
        if (value <= 0)
            return 0;
        int x = 0;
        for (int k = mPow; k > 0; k >>= 1) {
            if (x + k <= mN && node[x + k] < value) {
                value -= node[x + k];
                x += k;
            }
        }
        return x + 1;
    }

private:
    vector<ll> node;
    ll mN;
    ll mPow;
};

ll A[100005];
ll B[100005];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    REP(i, N) {
        cin >> A[i];
    }
    REP(i, N) {
        ll b;
        cin >> b;
        B[b] = i + 1;
    }

    BinaryIndexTree bit(N + 5);
    ll ans = 0;
    REP(i, N) {
        ans += i - bit.sum(B[A[i]]);
        bit.add(B[A[i]], 1);
    }
    cout << ans << endl;

    return 0;
}
0