結果

問題 No.649 ここでちょっとQK!
ユーザー te-shte-sh
提出日時 2018-02-13 17:54:20
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 202 ms / 3,000 ms
コード長 2,033 bytes
コンパイル時間 2,298 ms
コンパイル使用メモリ 161,744 KB
実行使用メモリ 27,448 KB
最終ジャッジ日時 2023-09-03 18:46:20
合計ジャッジ時間 6,910 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 103 ms
6,872 KB
testcase_04 AC 160 ms
27,448 KB
testcase_05 AC 157 ms
26,972 KB
testcase_06 AC 94 ms
6,872 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 95 ms
10,780 KB
testcase_13 AC 94 ms
11,044 KB
testcase_14 AC 94 ms
11,016 KB
testcase_15 AC 93 ms
11,312 KB
testcase_16 AC 87 ms
11,540 KB
testcase_17 AC 97 ms
12,032 KB
testcase_18 AC 115 ms
21,280 KB
testcase_19 AC 130 ms
22,196 KB
testcase_20 AC 139 ms
22,788 KB
testcase_21 AC 149 ms
21,312 KB
testcase_22 AC 159 ms
23,380 KB
testcase_23 AC 169 ms
22,380 KB
testcase_24 AC 180 ms
22,632 KB
testcase_25 AC 189 ms
25,188 KB
testcase_26 AC 202 ms
25,932 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 71 ms
10,752 KB
testcase_31 AC 68 ms
10,724 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(31): 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.container; // SList, DList, BinaryHeap
import std.typecons;  // Tuple, Nullable, BigFlags

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
T[] readArray(T)(size_t n){auto a=new T[](n),r=readln.splitter;foreach(ref v;a){v=r.front.to!T;r.popFront;}return a;}
T[] readArrayM(T)(size_t n){auto a=new T[](n);foreach(ref v;a)v=readln.chomp.to!T;return a;}

void main()
{
  auto rbt = redBlackTree!(true, long)();
  int q, k; readV(q, k);
  auto v = new long[](q);
  foreach (i; 0..q) {
    auto rd = readln.splitter;
    auto t = rd.front.to!int; rd.popFront();
    switch (t) {
    case 1:
      v[i] = rd.front.to!long;
      break;
    case 2:
      v[i] = -1;
      break;
    default:
      assert(0);
    }
  }

  auto u = v.dup.sort.uniq.array, n = u.length.to!int;
  int[long] buf;
  foreach (int i, ui; u) buf[ui] = i;

  auto bt = BiTree!int(n);
  foreach (vi; v) {
    if (vi != -1) {
      ++bt[buf[vi]];
    } else {
      if (bt[0..$] < k) {
        writeln(-1);
      } else {
        auto bs = iota(0, n).map!(x => tuple(x, bt[0..x])).assumeSorted!"a[1] < b[1]";
        auto r = bs.lowerBound(tuple(0, k)).back[0];
        writeln(u[r]);
        --bt[r];
      }
    }
  }
}

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) if (op == "+" || op == "-")
  {
    ++i;
    for (; i <= n; i += i & -i) mixin("buf[i] " ~ op ~ "= val;");
  }

  void opIndexUnary(string op)(size_t i) if (op == "++" || op == "--")
  {
    ++i;
    for (; i <= n; i += i & -i) mixin("buf[i]" ~ op ~ ";");
  }

  pure T opSlice(size_t r, size_t l) { return get(l) - get(r); }
  pure T opIndex(size_t i) { return opSlice(i, i+1); }
  pure size_t opDollar() { return n; }

private:

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