結果

問題 No.649 ここでちょっとQK!
ユーザー bal4ubal4u
提出日時 2019-06-02 09:56:28
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 78 ms / 3,000 ms
コード長 2,136 bytes
コンパイル時間 209 ms
コンパイル使用メモリ 31,600 KB
実行使用メモリ 7,840 KB
最終ジャッジ日時 2023-10-17 22:57:11
合計ジャッジ時間 3,182 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,780 KB
testcase_01 AC 2 ms
5,780 KB
testcase_02 AC 2 ms
5,780 KB
testcase_03 AC 5 ms
4,348 KB
testcase_04 AC 39 ms
7,840 KB
testcase_05 AC 38 ms
7,048 KB
testcase_06 AC 23 ms
7,048 KB
testcase_07 AC 1 ms
5,780 KB
testcase_08 AC 2 ms
5,780 KB
testcase_09 AC 1 ms
5,780 KB
testcase_10 AC 2 ms
5,780 KB
testcase_11 AC 1 ms
5,780 KB
testcase_12 AC 38 ms
7,048 KB
testcase_13 AC 38 ms
7,048 KB
testcase_14 AC 38 ms
7,048 KB
testcase_15 AC 38 ms
7,048 KB
testcase_16 AC 36 ms
7,048 KB
testcase_17 AC 41 ms
7,048 KB
testcase_18 AC 45 ms
7,048 KB
testcase_19 AC 49 ms
7,312 KB
testcase_20 AC 52 ms
7,312 KB
testcase_21 AC 57 ms
7,312 KB
testcase_22 AC 61 ms
7,312 KB
testcase_23 AC 65 ms
7,576 KB
testcase_24 AC 69 ms
7,576 KB
testcase_25 AC 74 ms
7,576 KB
testcase_26 AC 78 ms
7,840 KB
testcase_27 AC 2 ms
5,784 KB
testcase_28 AC 2 ms
5,784 KB
testcase_29 AC 2 ms
5,784 KB
testcase_30 AC 33 ms
7,048 KB
testcase_31 AC 32 ms
7,048 KB
testcase_32 AC 2 ms
5,780 KB
testcase_33 AC 1 ms
5,780 KB
testcase_34 AC 1 ms
5,780 KB
testcase_35 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:8:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    8 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:17: note: in expansion of macro 'gc'
   17 |         int c = gc();
      |                 ^~
main.c: In function 'out':
main.c:9:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
    9 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:27:17: note: in expansion of macro 'pc'
   27 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: 649 ここでちょっとQK
// 2019.6.2 bal4u

#include <stdio.h>
#include <stdlib.h>

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
long long in()    // 非負整数の入力
{
	long long n = 0;
	int c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

void out(long long n)  // 非負整数の表示(出力)
{
	int i;
	char b[40];

	if (!n) pc('0');
	else {
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

#define MAXVAL 200000
int bit[MAXVAL+5];
int sz = MAXVAL;
int p = 1<<18;
#if 0
void init(int maxVal) {
	sz = maxVal;
	p = 1; while (p < sz) p <<= 1;
}
#endif

void add(int i, int x) {
	i++; while (i <= sz)
		bit[i] += x, i += i & -i;
}

void insert(int val) { add(val, 1); }
void erase(int val) { add(val, -1); }

int nth_element(int n) {
	int a = 0, q = p; n--;
	while (q >>= 1)
		if (a+q <= sz && bit[a+q] <= n)
			a += q, n -= bit[a];
	return a;
}

int cmp(const void *a, const void *b) {
	long long t;
	t = *(long long *)a - *(long long *)b;
	if (t < 0) return -1;
	return t > 0;
}

// 配列 a 、個数 n 、 リターン値はユニーク化した配列の長さ(個数)
int uniq(long long *a, int n) {
	int i = 0, j;
	a[n] = a[n-1] + 1;
	for (j = 1; j < n; j++) {
		while (a[j] == a[i]) j++;
		a[++i] = a[j];
	}
	if (a[i] < a[n]) i++;
	return i;
}

int Q, K;
int q[MAXVAL]; long long v[MAXVAL];
long long u[MAXVAL]; int sz;

int bsch(long long x)
{
	int m, l = 0, r = sz-1;

    while (l < r) {
        m = (l + r) >> 1;
		if (u[m] == x) return m;
        if (u[m] < x) l = m + 1; else r = m;
    }
	return l;
}

int main()
{
	int i, a, ans, f;
	
	Q = (int)in(), K = (int)in();
	sz = 0; for (i = 0; i < Q; i++) {
		q[i] = (gc() & 1) - 1, gc();
		if (!q[i]) v[i] = in(), u[sz++] = v[i];
	}
	qsort(u, sz, sizeof(long long), cmp);
	sz = uniq(u, sz);
	f = 0; for (i = 0; i < Q; i++) {
		if (q[i]) {
			if (f < K) pc('-'), pc('1'), pc('\n');
			else out(u[a = nth_element(K)]), erase(a), f--;
		} else insert(bsch(v[i])), f++;
	}
	return 0;
}
0