結果

問題 No.728 ギブ and テイク
ユーザー nebukuro09nebukuro09
提出日時 2018-08-24 22:53:37
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,401 ms / 3,000 ms
コード長 2,176 bytes
コンパイル時間 1,512 ms
コンパイル使用メモリ 129,532 KB
実行使用メモリ 134,928 KB
最終ジャッジ日時 2023-09-03 20:54:39
合計ジャッジ時間 14,177 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 65 ms
11,384 KB
testcase_14 AC 111 ms
18,060 KB
testcase_15 AC 48 ms
9,360 KB
testcase_16 AC 97 ms
16,116 KB
testcase_17 AC 89 ms
15,928 KB
testcase_18 AC 965 ms
110,016 KB
testcase_19 AC 1,027 ms
110,096 KB
testcase_20 AC 1,199 ms
116,980 KB
testcase_21 AC 1,087 ms
111,988 KB
testcase_22 AC 399 ms
46,492 KB
testcase_23 AC 254 ms
34,904 KB
testcase_24 AC 823 ms
114,996 KB
testcase_25 AC 832 ms
114,736 KB
testcase_26 AC 389 ms
47,996 KB
testcase_27 AC 1,401 ms
127,676 KB
testcase_28 AC 1,025 ms
134,928 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 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, std.bitmanip;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto LR = N.iota.map!(_ => readln.split.map!(to!long).array).array;

    long[] tmp = [-10L^^10];
    int[long] comp;
    foreach (i; 0..N) {
        tmp ~= A[i];
        tmp ~= A[i] - LR[i][0];
        tmp ~= A[i] + LR[i][1];
    }
    tmp = tmp.sort().uniq().array;
    foreach (i; 0..tmp.length) {
        comp[tmp[i]] = i.to!int;
    }
    int M = tmp.length.to!int;

    auto pq = new BinaryHeap!(Array!(Tuple!(long, long, long)), "a[2] > b[2]")();
    auto st = new SegmentTree(M);
    long ans = 0;

    foreach (i; 0..N) {
        while (!pq.empty && pq.front[2] < A[i]) {
            st.add(comp[pq.front[1]], -1);
            pq.removeFront;
        }
        ans += st.sum(comp[A[i] - LR[i][0]], comp[A[i]]);
        st.add(comp[A[i]], 1);
        pq.insert(tuple(A[i] - LR[i][0], A[i], A[i] + LR[i][1]));
    }

    ans.writeln;
}


class SegmentTree {
    long[] table;
    int size;

    this(int n) {
        assert(bsr(n) < 29);
        size = 1 << (bsr(n) + 2);
        table = new long[](size);
    }

    void add(int pos, long num) {
        return add(pos, num, 0, 0, size/2-1);
    }

    void add(int pos, long num, int i, int left, int right) {
        table[i] += num;
        if (left == right)
            return;
        auto mid = (left + right) / 2;
        if (pos <= mid)
            add(pos, num, i*2+1, left, mid);
        else
            add(pos, num, i*2+2, mid+1, right);
    }

    long sum(int pl, int pr) {
        return sum(pl, pr, 0, 0, size/2-1);
    }

    long sum(int pl, int pr, int i, int left, int right) {
        if (pl > right || pr < left)
            return 0;
        else if (pl <= left && right <= pr)
            return table[i];
        else
            return
                sum(pl, pr, i*2+1, left, (left+right)/2) +
                sum(pl, pr, i*2+2, (left+right)/2+1, right);
    }
}
0