結果

問題 No.649 ここでちょっとQK!
ユーザー k_6101k_6101
提出日時 2020-08-06 23:04:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,411 ms / 3,000 ms
コード長 6,429 bytes
コンパイル時間 5,978 ms
コンパイル使用メモリ 84,428 KB
実行使用メモリ 119,276 KB
最終ジャッジ日時 2023-10-22 00:48:15
合計ジャッジ時間 28,300 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
51,004 KB
testcase_01 AC 57 ms
53,056 KB
testcase_02 AC 57 ms
51,020 KB
testcase_03 AC 269 ms
56,792 KB
testcase_04 AC 682 ms
105,212 KB
testcase_05 AC 663 ms
115,364 KB
testcase_06 AC 300 ms
62,688 KB
testcase_07 AC 61 ms
53,752 KB
testcase_08 AC 69 ms
52,616 KB
testcase_09 AC 68 ms
53,036 KB
testcase_10 AC 69 ms
52,720 KB
testcase_11 AC 60 ms
53,748 KB
testcase_12 AC 626 ms
85,888 KB
testcase_13 AC 628 ms
82,260 KB
testcase_14 AC 605 ms
84,180 KB
testcase_15 AC 622 ms
85,036 KB
testcase_16 AC 569 ms
84,296 KB
testcase_17 AC 831 ms
90,628 KB
testcase_18 AC 881 ms
90,820 KB
testcase_19 AC 873 ms
91,708 KB
testcase_20 AC 922 ms
92,916 KB
testcase_21 AC 984 ms
95,100 KB
testcase_22 AC 986 ms
96,792 KB
testcase_23 AC 1,042 ms
99,728 KB
testcase_24 AC 1,411 ms
113,932 KB
testcase_25 AC 1,190 ms
118,304 KB
testcase_26 AC 1,287 ms
119,276 KB
testcase_27 AC 85 ms
54,376 KB
testcase_28 AC 81 ms
53,316 KB
testcase_29 AC 84 ms
52,576 KB
testcase_30 AC 534 ms
79,236 KB
testcase_31 AC 535 ms
78,832 KB
testcase_32 AC 62 ms
52,684 KB
testcase_33 AC 66 ms
53,756 KB
testcase_34 AC 80 ms
53,040 KB
testcase_35 AC 62 ms
53,756 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);
	Set<Long> set = new TreeSet();
	Map<Long, Integer> map = new HashMap();
	Map<Integer, Long> mapR = new HashMap();
	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();
				set.add(v[q]);
			}
		}
		int cnt = 1;
		for(long val : set) {
			map.put(val, cnt);
			mapR.put(cnt, val);
			cnt++;
		}
		int size;
		for (int i = 0; i < Q; i++) {
			if(t[i] == 1)	bit.add(map.get(v[i]), 1);
			else {
				size = bit.sum(200000);
				if(size < K)	out.println(-1);
				else {
					cnt = bit.lower_bound(K);
					out.println(mapR.get(cnt));
					bit.add(cnt, -1);
				}
			}
		}
	}
	// 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