結果

問題 No.649 ここでちょっとQK!
ユーザー ikdikd
提出日時 2018-03-20 19:48:00
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 292 ms / 3,000 ms
コード長 1,759 bytes
コンパイル時間 938 ms
コンパイル使用メモリ 112,936 KB
実行使用メモリ 37,996 KB
最終ジャッジ日時 2024-06-13 00:07:07
合計ジャッジ時間 6,670 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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