結果

問題 No.649 ここでちょっとQK!
ユーザー k_6101k_6101
提出日時 2020-08-07 01:01:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 680 ms / 3,000 ms
コード長 7,179 bytes
コンパイル時間 2,736 ms
コンパイル使用メモリ 83,036 KB
実行使用メモリ 73,792 KB
最終ジャッジ日時 2023-10-22 03:52:32
合計ジャッジ時間 16,849 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
53,676 KB
testcase_01 AC 55 ms
53,696 KB
testcase_02 AC 55 ms
52,608 KB
testcase_03 AC 352 ms
65,404 KB
testcase_04 AC 473 ms
73,792 KB
testcase_05 AC 436 ms
73,772 KB
testcase_06 AC 264 ms
61,932 KB
testcase_07 AC 55 ms
52,604 KB
testcase_08 AC 56 ms
52,600 KB
testcase_09 AC 58 ms
52,620 KB
testcase_10 AC 56 ms
53,680 KB
testcase_11 AC 58 ms
50,556 KB
testcase_12 AC 392 ms
66,068 KB
testcase_13 AC 400 ms
66,128 KB
testcase_14 AC 369 ms
66,336 KB
testcase_15 AC 375 ms
66,404 KB
testcase_16 AC 380 ms
66,224 KB
testcase_17 AC 415 ms
66,580 KB
testcase_18 AC 442 ms
64,120 KB
testcase_19 AC 499 ms
68,912 KB
testcase_20 AC 514 ms
68,320 KB
testcase_21 AC 565 ms
70,256 KB
testcase_22 AC 573 ms
70,576 KB
testcase_23 AC 587 ms
70,436 KB
testcase_24 AC 617 ms
71,032 KB
testcase_25 AC 657 ms
71,596 KB
testcase_26 AC 680 ms
73,528 KB
testcase_27 AC 60 ms
52,920 KB
testcase_28 AC 61 ms
52,976 KB
testcase_29 AC 60 ms
53,720 KB
testcase_30 AC 335 ms
66,208 KB
testcase_31 AC 327 ms
66,272 KB
testcase_32 AC 59 ms
52,616 KB
testcase_33 AC 57 ms
53,680 KB
testcase_34 AC 56 ms
53,684 KB
testcase_35 AC 56 ms
53,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.rmi.dgc.Lease;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;

import static java.util.Comparator.*;

public class Main {
	public static void main(String[] args) {
		Main main = new Main();
		main.solve();
		main.out.close();
	}
	// ======================================================================
	BIT bit = new BIT(200010);
	Compress comp = new Compress();
	public void solve() {
		int Q = ni();
		int K = ni();
		int[] t = new int[Q];
		long[] v = new long[Q];
		for (int q = 0; q < Q; q++) {
			t[q] = ni();
			if(t[q] == 1) {
				v[q] = nl();
			}
		}
		// 座標圧縮
		comp.compress(v);
		int size, cnt;
		for (int i = 0; i < Q; i++) {
			if(t[i] == 1)	bit.add(comp.getNo(i)+1, 1);
			else {
				size = bit.sum(200000);
				if(size < K)	out.println(-1);
				else {
					cnt = bit.lower_bound(K);
					out.println(comp.getValue(cnt-1));
					bit.add(cnt, -1);
				}
			}
		}
	}
	// 座標圧縮
	class Compress {
		Set<Long> set = new TreeSet();
		long[] sortedValue_;	// 元の値(インデックスは圧縮後のソートされた位置)
		int[] originalNo_;		// 圧縮後のソートされた位置(インデックスは元の位置)

		// 圧縮する
		public void compress(long[] arg) {
			// 並べ替えて、重複削除
			for (int i = 0; i < arg.length; i++) {
				set.add(arg[i]);
			}
			sortedValue_ = new long[set.size()];
			int cnt = 0;
			for(long val : set)	{
				sortedValue_[cnt++] = val;
			}
			originalNo_ = new int[arg.length];
			for (int i = 0; i < arg.length; i++) {
				originalNo_[i] = Arrays.binarySearch(sortedValue_, arg[i]);
			}
		}
		// 圧縮後の位置を返す
		public int getNo(int index) {
			return originalNo_[index];
		}
		// 元の値を返す
		public long getValue(int index) {
			return sortedValue_[index];
		}
	}

	// Binary Indexed Tree (Fenwick Tree)
	// 数列の初めの要素からi番目までの区間和を求めるために使います
	//
	// 存在する数の位置に 1 を足していけば、X が何番目に小さいかは先頭から X までの合計となる
	// これを2分探索すると Y 番目に小さい数を取得できる
	class BIT {
		int size_;
		int[] data_;
		
		// 初期化でサイズを指定する
		public BIT(int n) {
			size_ = n;						// データサイズ
			data_ = new int[size_+1];		// 1~なので +1する
		}
		// 指定したインデックス(1~)に指定した数を足す  O(logN)
		public void add(int index, int x) {
			if(index == 0) {
				out.println("インデックス 0 は範囲外です");
				System.exit(-1);
			}
			// LSB(Least Significant Bit) = (i & -i)
			// LSBとは二進数で表現した時に初めて1が出てくるのは右から数えて何番目かというもの
			// LSBを足すことで加算する先を決める事ができる
			for (int i = index; i <= size_; i += (i & -i)) {
				data_[i] += x;
			}
		}
		// 1から指定したインデックス(1~)までの合計を計算する  O(logN)
		public int sum(int r) {			// data_[1] +   + data_[r]
			int ret = 0;		
			// LSBを引くことで加算するデータを決める事ができる
			for (int i = r; i > 0; i -= (i & -i)) {
				ret += data_[i];
			}
			return ret;
		}
		// インデックス l から r までの合計を計算する
		public int sum(int l, int r) {	// v[l] +   + v[r]
			return sum(r-1) - sum(l-1);
		}
		// 二分探索
		// 1から指定したインデックス(1~)までの合計が
		// 引数で渡される値以上となる最小インデックスを求める → なければ 0
		// ☆ 前提条件 data_[*] >= 0
		public int lower_bound(long val) {
			if(val <= 0)	return 0;
			int ans = 0, r = 1;
			// r = size_ 以下の最大の2のべき乗
			while(r < size_)	r = r << 1;
			for (int i = r; i > 0; i /= 2) {
				if(ans + i <= size_ && data_[ans + i] < val) {
					val -= data_[ans + i];
					ans += i;
				}
			}
			return ans + 1;
		}
	}

	// ------------------------------------------
	// ライブラリ
	// ------------------------------------------
	// Print
	private PrintWriter out = new PrintWriter(System.out);
	
	// Scanner
	private FastScanner scan = new FastScanner();
	int ni(){ return scan.nextInt();}
	int[] ni(int n){int[] a = new int[n]; for(int i = 0; i < n; i++){a[i] = ni();} return a;}
	int[][] ni(int y, int x){int[][] a = new int[y][x];
	for(int i = 0; i < y; i++){for(int j = 0; j < x; j++){a[i][j] = ni();}} return a;}
	long nl(){return scan.nextLong();}
	long[] nl(int n){long[] a = new long[n]; for(int i = 0; i < n; i++){a[i] = nl();} return a;}
	long[][] nl(int y, int x){long[][] a = new long[y][x];
	for(int i = 0; i < y; i++){for(int j = 0; j < x; j++){a[i][j] = nl();}} return a;}
	String ns(){return scan.next();}
	String[] ns(int n){String[] a = new String[n]; for(int i = 0; i < n; i++){a[i] = ns();} return a;}
	String[][] ns(int y, int x){String[][] a = new String[y][x];
	for(int i = 0; i < y; i++){for(int j = 0; j < x; j++){a[i][j] = ns();}} return a;}
}
class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;
	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		}else{
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}
	private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
	private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
	public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
	public String next() {
		if (!hasNext()) throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while(isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	public long nextLong() {
		if (!hasNext()) throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while(true){
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			}else if(b == -1 || !isPrintableChar(b)){
				return minus ? -n : n;
			}else{
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
		return (int) nl;
	}
	public double nextDouble() { return Double.parseDouble(next());}
}
0