結果

問題 No.649 ここでちょっとQK!
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-11 22:04:01
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 2,252 bytes
コンパイル時間 2,470 ms
コンパイル使用メモリ 79,992 KB
実行使用メモリ 79,620 KB
最終ジャッジ日時 2024-04-26 02:19:40
合計ジャッジ時間 13,404 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
52,636 KB
testcase_01 AC 132 ms
54,296 KB
testcase_02 RE -
testcase_03 AC 1,592 ms
76,664 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 115 ms
52,808 KB
testcase_08 RE -
testcase_09 AC 133 ms
54,264 KB
testcase_10 AC 128 ms
54,288 KB
testcase_11 AC 121 ms
54,236 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 192 ms
56,884 KB
testcase_28 AC 192 ms
56,852 KB
testcase_29 AC 185 ms
56,796 KB
testcase_30 AC 1,167 ms
79,620 KB
testcase_31 AC 1,118 ms
78,880 KB
testcase_32 AC 114 ms
53,864 KB
testcase_33 AC 119 ms
54,300 KB
testcase_34 RE -
testcase_35 AC 121 ms
53,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  static int N;
  static int[] dat;

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int q = sc.nextInt();
    int k = sc.nextInt();
    HashSet<Long> set = new HashSet<Long>();
    ArrayList<Long> list = new ArrayList<Long>();
    long[][] que = new long[q][2];
    for(int i = 0; i < q; i++) {
      int s1 = sc.nextInt();
      if(s1 == 1) {
        long s2 = sc.nextInt();
        que[i][0] = 1;
        que[i][1] = s2;
        set.add(s2);
      } else {
        que[i][0] = 2;
      }
    }
    for(Iterator<Long> i = set.iterator(); i.hasNext();) {
      list.add(i.next());
    }
    Collections.sort(list);
    long[] ret = new long[list.size()];
    HashMap<Long, Integer> map = new HashMap<Long, Integer>();
    for(int i = 0; i < list.size(); i++) {
      long r = list.get(i);
      map.put(r, i);
      ret[i] = r;
    }
    init(list.size());
    for(int i = 0; i < q; i++) {
      if(que[i][0] == 1) {
        update(map.get(que[i][1]), 1);
      } else {
        int l = 0;
        int r = list.size();
        int ans = 0;
        if(query(0, N, 0, 0, N) >= k) {
        while(l < r) {
          int med = (l + r) / 2;
          int sum = query(0, med + 1, 0, 0, N);
          if(sum == k) {
            ans = med;
            r = med;
          } else if(sum > k) {
            ans = med;
            r = med;
          } else {
            l = med + 1;
          }
        }
        update(ans, -1);
        System.out.println(ret[ans]);
        } else {
          System.out.println(-1);
        }
      }
    }
  }

  public static void init(int n_) {
    int n = 1;
    while(n < n_) {
      n *= 2;
    }
    N = n;
    dat = new int[2 * N - 1];
  }

  public static void update(int k, int x) {
    int a = k + N - 1;
    dat[a] += x;
    while(a > 0) {
      a = (a - 1) / 2;
      dat[a] = dat[2 * a + 1] + dat[2 * a + 2]; 
    }
  }

  public static int query(int a, int b, int k, int l, int r) {
    if((r <= a) || (l >= b)) return 0;
    if((a <= l) && (r <= b)) {
      return dat[k];
    } else {
      int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
      int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
      return vl + vr;
    }
  }
}
0