結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー misora192misora192
提出日時 2020-07-18 20:55:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 1,739 bytes
コンパイル時間 1,599 ms
コンパイル使用メモリ 174,912 KB
実行使用メモリ 9,656 KB
最終ジャッジ日時 2023-08-20 23:55:36
合計ジャッジ時間 4,927 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 86 ms
8,908 KB
testcase_04 AC 103 ms
9,396 KB
testcase_05 AC 84 ms
8,816 KB
testcase_06 AC 77 ms
8,332 KB
testcase_07 AC 101 ms
9,332 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 37 ms
6,684 KB
testcase_10 AC 69 ms
9,640 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 103 ms
9,584 KB
testcase_13 AC 103 ms
9,588 KB
testcase_14 AC 103 ms
9,656 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 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 8 ms
4,380 KB
testcase_24 AC 32 ms
5,412 KB
testcase_25 AC 73 ms
8,064 KB
testcase_26 AC 15 ms
4,376 KB
testcase_27 AC 40 ms
5,936 KB
testcase_28 AC 58 ms
6,756 KB
testcase_29 AC 81 ms
8,264 KB
testcase_30 AC 100 ms
9,388 KB
testcase_31 AC 22 ms
4,744 KB
testcase_32 AC 14 ms
4,380 KB
testcase_33 AC 80 ms
8,812 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=(0);i<(n);i++)

using namespace std;

typedef long long ll;

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 (a>b) { a=b; return 1; } return 0; }

template< typename Monoid >
struct SegmentTree {
    using F = function< Monoid(Monoid, Monoid) >;

    int sz;
    vector< Monoid > seg;

    const F f;
    const Monoid M1;

    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
        sz = 1;
        while(sz < n) sz <<= 1;
        seg.assign(2 * sz, M1);
    }

    void set(int k, const Monoid &x) {
        seg[k + sz] = x;
    }

    void build() {
        for(int k = sz - 1; k > 0; k--) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    void update(int k, const Monoid &x) {
        k += sz;
        seg[k] = x;
        while(k >>= 1) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    Monoid query(int a, int b) {
        Monoid L = M1, R = M1;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if(a & 1) L = f(L, seg[a++]);
            if(b & 1) R = f(seg[--b], R);
        }
        return f(L, R);
    }

    Monoid operator[](const int &k) const {
        return seg[k + sz];
    }
};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	vector<int> a(n), b(n);
	rep(i, n) cin >> a[i];
	rep(i, n) cin >> b[i];

	map<int, int> mp;
	rep(i, n) mp[a[i]] = i + 1;

	SegmentTree<int> seg(n+1, [](int a, int b){ return a + b; }, 0);
	ll ans = 0;
	rep(i, n){
		int j = mp[b[i]];
		ll val = seg.query(1,j+1);
		ans += i - val;
		seg.update(j, 1);
	}

	cout << ans << endl;	
}
0