結果

問題 No.59 鉄道の旅
ユーザー izuru_matsuuraizuru_matsuura
提出日時 2016-11-22 23:47:59
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 127 ms / 5,000 ms
コード長 2,031 bytes
コンパイル時間 1,536 ms
コンパイル使用メモリ 143,424 KB
実行使用メモリ 41,332 KB
最終ジャッジ日時 2023-09-02 23:08:02
合計ジャッジ時間 2,623 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
35,644 KB
testcase_01 AC 10 ms
36,144 KB
testcase_02 AC 9 ms
35,676 KB
testcase_03 AC 11 ms
38,112 KB
testcase_04 AC 108 ms
40,284 KB
testcase_05 AC 11 ms
36,956 KB
testcase_06 AC 11 ms
37,752 KB
testcase_07 AC 10 ms
36,448 KB
testcase_08 AC 25 ms
38,256 KB
testcase_09 AC 20 ms
38,844 KB
testcase_10 AC 22 ms
36,660 KB
testcase_11 AC 15 ms
36,224 KB
testcase_12 AC 61 ms
36,148 KB
testcase_13 AC 94 ms
40,572 KB
testcase_14 AC 127 ms
41,332 KB
testcase_15 AC 11 ms
36,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void log(A...)(A arg) {
    stderr.writeln(arg);
}
int size(T)(in T s) {
    return cast(int)s.length;
}

class RangeSumQuery2 {
    // root縺ョindex縺ッ1
    int N;
    long[] V, L;
    this(int N) {
        import core.bitop;
        this.N = 1 << (bsr(N - 1) + 1);
        this.V = new long[2 * this.N];
        this.L = new long[2 * this.N];
    }
    /* [s, t)縺ォ荳€讒倥↓x繧貞刈邂・*/
    void add(long x, int s, int t) { return add(x, s, t, 0, N, 1); }
    void add(long x, int s, int t, int a, int b, int k) {
        if (t <= a || b <= s) return;
        if (s <= a && b <= t) {
            L[k] += x;
            return;
        }
        if (s < b || a < t) {
            V[k] += (min(t, b) - max(s, a)) * x;
            if (b - a == 1) return;
            int m = (a + b) / 2;
            add(x, s, t, a, m, k * 2);
            add(x, s, t, m, b, k * 2 + 1);
        }
    }
    /* [s, t)縺ョ蜥・*/
    long query(int s, int t) { return query(s, t, 0, N, 1); }
    long query(int s, int t, int a, int b, int k) {
        if (t <= a || b <= s) return 0;
        if (s <= a && b <= t) return V[k] + L[k] * (b - a);
        int m = (a + b) / 2;
        return (min(t, b) - max(s, a)) * L[k] +
            query(s, t, a, m, k * 2) +
            query(s, t, m, b, k * 2 + 1);
    }
};

void main() {
    int N, K;
    readf("%d %d\n", &N, &K);
    auto rsq = new RangeSumQuery2(1000001);
    int[int] m;
    foreach (i; 0 .. N) {
        int W; readf("%d\n", &W);
        if (W < 0) {
            W = -W;
            if (m.get(W, 0) == 0) continue;
            rsq.add(-1, 0, W + 1);
            m[W]--;
        } else {
            if (rsq.query(W, W + 1) >= K) continue;
            m[W] = m.get(W, 0) + 1;
            rsq.add(1, 0, W + 1);
        }
    }
    writeln(rsq.query(0, 1));
}

0