結果

問題 No.618 labo-index
ユーザー te-shte-sh
提出日時 2017-12-19 13:40:54
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 255 ms / 6,000 ms
コード長 1,981 bytes
コンパイル時間 1,681 ms
コンパイル使用メモリ 98,376 KB
実行使用メモリ 13,140 KB
最終ジャッジ日時 2023-09-03 17:43:09
合計ジャッジ時間 7,856 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 120 ms
10,408 KB
testcase_20 AC 136 ms
10,032 KB
testcase_21 AC 124 ms
9,820 KB
testcase_22 AC 128 ms
10,308 KB
testcase_23 AC 127 ms
9,816 KB
testcase_24 AC 130 ms
9,840 KB
testcase_25 AC 133 ms
10,104 KB
testcase_26 AC 128 ms
10,104 KB
testcase_27 AC 128 ms
9,504 KB
testcase_28 AC 119 ms
9,512 KB
testcase_29 AC 126 ms
10,036 KB
testcase_30 AC 122 ms
9,836 KB
testcase_31 AC 129 ms
9,816 KB
testcase_32 AC 135 ms
10,084 KB
testcase_33 AC 129 ms
9,280 KB
testcase_34 AC 204 ms
12,192 KB
testcase_35 AC 255 ms
13,140 KB
testcase_36 AC 85 ms
6,108 KB
testcase_37 AC 145 ms
10,084 KB
testcase_38 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(38): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.typecons;  // Tuple, Nullable, BigFlags

void main()
{
  auto qn = readln.chomp.to!int;

  struct Query { int t; long x; }

  auto q = new Query[](qn);
  foreach (i; 0..qn) {
    auto rd = readln.splitter;
    auto t = rd.front.to!int; rd.popFront();
    auto x = rd.front.to!long;
    q[i] = Query(t, x);
  }

  long[] y;
  auto xs = 0L;
  foreach (qi; q)
    switch (qi.t) {
    case 1:
      y ~= qi.x-xs;
      break;
    case 2:
      break;
    case 3:
      xs += qi.x;
      break;
    default:
      assert(0);
    }

  y = y.sort().uniq.array;
  auto ys = y.assumeSorted;

  int[long] za;
  foreach (int i, yi; y) za[yi] = i;

  auto bt = BiTree!int(y.length), n = 0;
  xs = 0L;
  int[] ord;

  auto calc(long l)
  {
    auto x2 = ys.lowerBound(l-xs).length;
    return bt[x2..$] < l;
  }

  foreach (qi; q) {
    switch (qi.t) {
    case 1:
      auto x2 = za[qi.x-xs];
      bt[x2] += 1;
      ord ~= x2;
      ++n;
      break;
    case 2:
      auto x2 = ord[qi.x-1];
      bt[x2] += -1;
      --n;
      break;
    case 3:
      xs += qi.x;
      break;
    default:
      assert(0);
    }

    if (n == 0) {
      writeln(0);
    } else {
      auto bsearch = iota(0, n+1).map!(l => tuple(l, calc(l))).assumeSorted!"a[1] < b[1]";
      auto r = bsearch.lowerBound(tuple(0, true));
      writeln(r.back[0]);
    }
  }
}

struct BiTree(T)
{
  const size_t n;
  T[] buf;

  this(size_t n)
  {
    this.n = n;
    this.buf = new T[](n + 1);
  }

  void opIndexOpAssign(string op: "+")(T val, size_t i)
  {
    ++i;
    for (; i <= n; i += i & -i)
      buf[i] += val;
  }

  pure T opSlice(size_t r, size_t l) const
  {
    return get(l) - get(r);
  }

  pure size_t opDollar() const { return n; }
  pure T opIndex(size_t i) const { return opSlice(i, i+1); }

private:

  pure T get(size_t i) const
  {
    auto s = T(0);
    for (; i > 0; i -= i & -i)
      s += buf[i];
    return s;
  }
}
0