結果

問題 No.618 labo-index
ユーザー nebukuro09nebukuro09
提出日時 2018-01-13 10:51:21
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 912 ms / 6,000 ms
コード長 2,464 bytes
コンパイル時間 2,089 ms
コンパイル使用メモリ 113,736 KB
実行使用メモリ 13,216 KB
最終ジャッジ日時 2023-09-03 18:09:56
合計ジャッジ時間 21,150 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 7 ms
4,380 KB
testcase_05 AC 8 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 8 ms
4,380 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 12 ms
4,380 KB
testcase_19 AC 847 ms
9,724 KB
testcase_20 AC 873 ms
10,312 KB
testcase_21 AC 874 ms
11,060 KB
testcase_22 AC 859 ms
10,732 KB
testcase_23 AC 878 ms
10,584 KB
testcase_24 AC 887 ms
9,824 KB
testcase_25 AC 877 ms
9,256 KB
testcase_26 AC 880 ms
10,084 KB
testcase_27 AC 858 ms
10,348 KB
testcase_28 AC 874 ms
10,284 KB
testcase_29 AC 830 ms
9,820 KB
testcase_30 AC 855 ms
9,320 KB
testcase_31 AC 859 ms
9,300 KB
testcase_32 AC 864 ms
10,424 KB
testcase_33 AC 869 ms
11,592 KB
testcase_34 AC 887 ms
11,900 KB
testcase_35 AC 912 ms
13,216 KB
testcase_36 AC 446 ms
6,744 KB
testcase_37 AC 682 ms
11,620 KB
testcase_38 AC 1 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;

void main() {
    auto Q = readln.chomp.to!int;
    
    auto queries = new Tuple!(int, long)[](Q);
    long[] nums;
    int[long] comp;
    long offset;
    
    foreach (i; 0..Q) {
        auto s = readln.split;
        auto t = s[0].to!int;
        auto x = s[1].to!long;
        queries[i] = tuple(t, x);
        if (t == 1) {
            nums ~= x-offset;
        } else if (t == 3) {
            offset += x;
        }
    }

    nums.sort();
    foreach (i, e ; nums.enumerate) {
        comp[e] = i.to!int;
    }
    
    auto st = new SegmentTree(nums.length.to!int);
    
    offset = 0;
    long[] L;

    foreach (q; queries) {
        auto t = q[0];
        auto x = q[1];
        if (t == 1) {
            L ~= x - offset;
            st.add(comp[x-offset], 1);
        } else if (t == 2) {
            st.add(comp[L[x.to!int-1]], -1);
        } else {
            offset += x;
        }

        long hi = 10L^^5+1;
        long lo = 0;

        while (hi - lo > 1) {
            auto mid = (hi + lo) / 2;
            auto lb = nums.assumeSorted.upperBound(mid-offset-1);
            auto cnt = lb.empty ? 0 : st.sum(comp[lb.front], nums.length.to!int-1);
            if (cnt >= mid) lo = mid;
            else hi = mid;
        }

        max(0, lo).writeln;
    }
}


class SegmentTree {
    int[] table;
    int size;

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

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

    void add(int pos, int 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);
    }

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

    int 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