結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー mikianaaamikianaaa
提出日時 2020-10-21 22:06:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 2,395 bytes
コンパイル時間 1,740 ms
コンパイル使用メモリ 176,256 KB
実行使用メモリ 13,788 KB
最終ジャッジ日時 2023-09-28 14:28:06
合計ジャッジ時間 5,247 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 108 ms
12,460 KB
testcase_04 AC 129 ms
13,524 KB
testcase_05 AC 111 ms
12,532 KB
testcase_06 AC 97 ms
11,720 KB
testcase_07 AC 137 ms
13,680 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 47 ms
9,100 KB
testcase_10 AC 83 ms
13,656 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 139 ms
13,660 KB
testcase_13 AC 135 ms
13,788 KB
testcase_14 AC 134 ms
13,676 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 8 ms
4,376 KB
testcase_24 AC 38 ms
6,972 KB
testcase_25 AC 99 ms
11,540 KB
testcase_26 AC 18 ms
5,216 KB
testcase_27 AC 51 ms
7,808 KB
testcase_28 AC 70 ms
9,024 KB
testcase_29 AC 101 ms
11,676 KB
testcase_30 AC 127 ms
13,496 KB
testcase_31 AC 24 ms
5,700 KB
testcase_32 AC 16 ms
4,896 KB
testcase_33 AC 100 ms
12,520 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;

template <class Monoid> struct SegTree {
    using Func = function<Monoid(Monoid, Monoid)>;
    const Func F;
    const Monoid UNITY;
    int SIZE_R;
    vector<Monoid> dat;

    SegTree(int n, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
        init(n);
    }
    void init(int n) {
        SIZE_R = 1;
        while (SIZE_R < n)
            SIZE_R *= 2;
        dat.assign(SIZE_R * 2, UNITY);
    }

    /* set, a is 0-indexed */
    void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
    void build() {
        for (int k = SIZE_R - 1; k > 0; --k)
            dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
    }

    /* update a, a is 0-indexed */
    void update(int a, const Monoid &v) {
        int k = a + SIZE_R;
        dat[k] = v;
        while (k >>= 1)
            dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
    }

    /* get [a, b), a and b are 0-indexed */
    Monoid get(int a, int b) {
        Monoid vleft = UNITY, vright = UNITY;
        for (int left = a + SIZE_R, right = b + SIZE_R; left < right;
             left >>= 1, right >>= 1) {
            if (left & 1)
                vleft = F(vleft, dat[left++]);
            if (right & 1)
                vright = F(dat[--right], vright);
        }
        return F(vleft, vright);
    }
    inline Monoid operator[](int a) { return dat[a + SIZE_R]; }

    /* debug */
    void print() {
        for (int i = 0; i < SIZE_R; ++i) {
            cout << (*this)[i];
            if (i != SIZE_R - 1)
                cout << ",";
        }
        cout << endl;
    }
};

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

    int N;
    cin >> N;
    vector<ll> a(N), b(N), c(N);
    map<ll, ll> ma;
    rep(i, N) {
        cin >> a[i];
        a[i]--;
        ma[a[i]] = i;
    }

    rep(i, N) {
        cin >> b[i];
        b[i]--;
        c[i] = ma[b[i]];
    }

    function<ll(ll, ll)> f = [](ll a, ll b) { return a + b; };
    SegTree<ll> tree(N + 5, f, 0);
    ll ans = 0;
    rep(i, N) {
        int ind = c[i];
        ans += tree.get(ind, N);
        tree.update(ind, 1);
    }

    cout << ans << endl;
}
0