結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー nebukuro09nebukuro09
提出日時 2020-07-17 21:37:43
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 1,808 bytes
コンパイル時間 822 ms
コンパイル使用メモリ 127,776 KB
実行使用メモリ 13,960 KB
最終ジャッジ日時 2023-09-04 08:28:55
合計ジャッジ時間 4,347 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 109 ms
12,448 KB
testcase_04 AC 125 ms
13,960 KB
testcase_05 AC 109 ms
12,596 KB
testcase_06 AC 99 ms
10,504 KB
testcase_07 AC 124 ms
13,108 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 46 ms
9,268 KB
testcase_10 AC 76 ms
13,008 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 122 ms
13,048 KB
testcase_13 AC 122 ms
13,748 KB
testcase_14 AC 122 ms
12,948 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 10 ms
4,384 KB
testcase_24 AC 41 ms
6,264 KB
testcase_25 AC 94 ms
10,380 KB
testcase_26 AC 21 ms
5,164 KB
testcase_27 AC 51 ms
6,576 KB
testcase_28 AC 73 ms
8,012 KB
testcase_29 AC 99 ms
11,912 KB
testcase_30 AC 124 ms
12,696 KB
testcase_31 AC 28 ms
5,724 KB
testcase_32 AC 19 ms
5,640 KB
testcase_33 AC 77 ms
12,656 KB
testcase_34 AC 1 ms
4,380 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,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdlib, std.datetime;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    auto B = readln.split.map!(to!int).array;

    auto X = new int[](N+1);
    foreach (i, a; A) X[a] = i.to!int;

    auto C = N.iota.map!(i => tuple(i, B[i])).array;
    C.sort!((a,b)=>X[a[1]] < X[b[1]]);

    auto st = new SegmentTree!(long, (a, b)=>a+b, 0L)(N+1);
    long ans = 0;

    foreach (i; 0..N) {
        auto val = B[i];
        auto idx = X[val];
        ans += st.query(idx, N-1);
        st.add(idx, 1);
    }

    ans.writeln;
}


class SegmentTree(T, alias op, T e) {
    T[] table;
    int size;
    int offset;

    this(int n) {
        size = 1;
        while (size <= n) size <<= 1;
        size <<= 1;
        table = new T[](size);
        fill(table, e);
        offset = size / 2;
    }

    void assign(int pos, T val) {
        pos += offset;
        table[pos] = val;
        while (pos > 1) {
            pos /= 2;
            table[pos] = op(table[pos*2], table[pos*2+1]);
        }
    }

    void add(int pos, T val) {
        pos += offset;
        table[pos] += val;
        while (pos > 1) {
            pos /= 2;
            table[pos] = op(table[pos*2], table[pos*2+1]);
        }
    }

    T query(int l, int r) {
        return query(l, r, 1, 0, offset-1);
    }

    T query(int l, int r, int i, int a, int b) {
        if (b < l || r < a) {
            return e;
        } else if (l <= a && b <= r) {
            return table[i];
        } else {
            return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
        }
    }
}


0