結果

問題 No.649 ここでちょっとQK!
ユーザー ikdikd
提出日時 2018-03-20 19:48:00
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 322 ms / 3,000 ms
コード長 1,759 bytes
コンパイル時間 861 ms
コンパイル使用メモリ 95,884 KB
実行使用メモリ 38,592 KB
最終ジャッジ日時 2023-09-03 19:01:21
合計ジャッジ時間 7,764 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 161 ms
6,604 KB
testcase_04 AC 280 ms
36,644 KB
testcase_05 AC 278 ms
38,592 KB
testcase_06 AC 188 ms
8,172 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 132 ms
15,772 KB
testcase_13 AC 134 ms
15,776 KB
testcase_14 AC 138 ms
16,036 KB
testcase_15 AC 135 ms
17,736 KB
testcase_16 AC 137 ms
16,232 KB
testcase_17 AC 151 ms
17,004 KB
testcase_18 AC 186 ms
33,972 KB
testcase_19 AC 203 ms
33,288 KB
testcase_20 AC 222 ms
33,452 KB
testcase_21 AC 230 ms
33,108 KB
testcase_22 AC 250 ms
34,292 KB
testcase_23 AC 274 ms
34,044 KB
testcase_24 AC 281 ms
33,876 KB
testcase_25 AC 298 ms
35,448 KB
testcase_26 AC 322 ms
34,996 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 2 ms
4,384 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 113 ms
13,200 KB
testcase_31 AC 112 ms
12,932 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.typecons;

  int n, k; rd(n, k);
  alias Query=Tuple!(int, "t", long, "v");
  auto que=new Query[](n);
  foreach(i; 0..n){
    auto args=readln.split.to!(long[]);
    if(args.length==1) que[i].t=2;
    else que[i].t=1, que[i].v=args[$-1];
  }

  int[long] compression(Query[] q){
    long[] vs;
    foreach(e; q)if(e.t==1) vs~=e.v;
    sort(vs);
    int[long] ret;
    foreach(v; vs){
      if(v in ret) continue;
      ret[v]=to!(int)(ret.length);
    }
    return ret;
  }
  long[int] make_inv(int[long] map){
    long[int] ret;
    foreach(v; map.keys){
      ret[map[v]]=v;
    }
    return ret;
  }

  auto map=compression(que), inv=make_inv(map);
  auto tree=new SquareRootDecomposition(map.length.to!(int));
  foreach(q; que){
    if(q.t==1){
      tree.add(map[q.v], 1);
    }else{
      auto v=tree.find(k);
      if(v>=0){
        tree.add(v, -1);
        writeln(inv[v]);
      }else{
        writeln(-1);
      }
    }
  }
}

class SquareRootDecomposition{
  int D=1;
  int[] val, bucsum;
  this(int n){
    while(D*D<n) D++;
    val.length=(D*D);
    bucsum.length=D;
  }
  void add(int i, int x){
    val[i]+=x;
    bucsum[i/D]+=x;
  }
  int find(int x){
    int s=0, ret=-1;
    loop:foreach(int i; 0..D){
      if(s+bucsum[i]>=x){
        foreach(int j; 0..D){
          if(s+val[i*D+j]>=x){
            ret=i*D+j;
            break loop;
          }else{
            s+=val[i*D+j];
          }
        }
      }else{
        s+=bucsum[i];
      }
    }
    return ret;
  }
}

void rd(Query...)(ref Query x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
0