結果

問題 No.649 ここでちょっとQK!
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-11 23:06:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,424 ms / 3,000 ms
コード長 2,253 bytes
コンパイル時間 2,247 ms
コンパイル使用メモリ 79,964 KB
実行使用メモリ 107,536 KB
最終ジャッジ日時 2024-04-26 06:13:39
合計ジャッジ時間 44,498 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
41,444 KB
testcase_01 AC 131 ms
42,000 KB
testcase_02 AC 129 ms
41,424 KB
testcase_03 AC 1,590 ms
66,276 KB
testcase_04 AC 1,479 ms
99,240 KB
testcase_05 AC 1,484 ms
100,116 KB
testcase_06 AC 1,159 ms
64,860 KB
testcase_07 AC 125 ms
41,488 KB
testcase_08 AC 126 ms
41,416 KB
testcase_09 AC 130 ms
41,660 KB
testcase_10 AC 126 ms
41,520 KB
testcase_11 AC 126 ms
41,324 KB
testcase_12 AC 1,541 ms
77,896 KB
testcase_13 AC 1,580 ms
78,580 KB
testcase_14 AC 1,585 ms
80,584 KB
testcase_15 AC 1,554 ms
77,976 KB
testcase_16 AC 1,478 ms
78,632 KB
testcase_17 AC 1,544 ms
79,368 KB
testcase_18 AC 1,705 ms
81,920 KB
testcase_19 AC 1,767 ms
85,628 KB
testcase_20 AC 1,826 ms
86,392 KB
testcase_21 AC 2,043 ms
94,568 KB
testcase_22 AC 2,209 ms
93,176 KB
testcase_23 AC 2,288 ms
99,988 KB
testcase_24 AC 2,283 ms
101,432 KB
testcase_25 AC 2,407 ms
107,536 KB
testcase_26 AC 2,424 ms
105,308 KB
testcase_27 AC 207 ms
44,228 KB
testcase_28 AC 209 ms
43,924 KB
testcase_29 AC 202 ms
44,148 KB
testcase_30 AC 1,160 ms
72,776 KB
testcase_31 AC 1,117 ms
70,624 KB
testcase_32 AC 128 ms
41,600 KB
testcase_33 AC 128 ms
41,916 KB
testcase_34 AC 126 ms
41,468 KB
testcase_35 AC 126 ms
41,272 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.nextLong();
        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