結果
問題 | No.649 ここでちょっとQK! |
ユーザー | akakimidori |
提出日時 | 2019-05-28 03:23:35 |
言語 | C (gcc 12.3.0) |
結果 |
AC
|
実行時間 | 130 ms / 3,000 ms |
コード長 | 3,560 bytes |
コンパイル時間 | 257 ms |
コンパイル使用メモリ | 33,232 KB |
実行使用メモリ | 11,264 KB |
最終ジャッジ日時 | 2024-09-17 15:37:26 |
合計ジャッジ時間 | 3,762 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 23 ms
6,944 KB |
testcase_04 | AC | 111 ms
11,264 KB |
testcase_05 | AC | 111 ms
11,264 KB |
testcase_06 | AC | 111 ms
11,136 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,940 KB |
testcase_10 | AC | 1 ms
6,940 KB |
testcase_11 | AC | 1 ms
6,940 KB |
testcase_12 | AC | 58 ms
6,944 KB |
testcase_13 | AC | 58 ms
6,944 KB |
testcase_14 | AC | 57 ms
6,944 KB |
testcase_15 | AC | 59 ms
6,944 KB |
testcase_16 | AC | 56 ms
6,940 KB |
testcase_17 | AC | 64 ms
6,944 KB |
testcase_18 | AC | 73 ms
6,940 KB |
testcase_19 | AC | 79 ms
7,040 KB |
testcase_20 | AC | 88 ms
7,424 KB |
testcase_21 | AC | 93 ms
7,680 KB |
testcase_22 | AC | 100 ms
8,192 KB |
testcase_23 | AC | 108 ms
8,448 KB |
testcase_24 | AC | 116 ms
8,832 KB |
testcase_25 | AC | 120 ms
9,216 KB |
testcase_26 | AC | 130 ms
9,600 KB |
testcase_27 | AC | 1 ms
6,944 KB |
testcase_28 | AC | 1 ms
6,940 KB |
testcase_29 | AC | 1 ms
6,940 KB |
testcase_30 | AC | 50 ms
6,944 KB |
testcase_31 | AC | 52 ms
6,940 KB |
testcase_32 | AC | 1 ms
6,940 KB |
testcase_33 | AC | 1 ms
6,940 KB |
testcase_34 | AC | 1 ms
6,940 KB |
testcase_35 | AC | 1 ms
6,940 KB |
ソースコード
#include<stdio.h> #include<stdlib.h> #include<stdint.h> #include<inttypes.h> typedef int32_t i32; typedef int64_t i64; typedef struct WBT_node { i64 val; i32 size; struct WBT_node *left; struct WBT_node *right; } node; i32 get_size (const node *t) { return t == NULL ? 0 : t->size; } node* new_node (const i64 val) { node *t = (node *) calloc (1, sizeof (node)); *t = (node) {val, 1, NULL, NULL}; return t; } void free_node (node *t) { if (t == NULL) return; free_node (t->left); free_node (t->right); free (t); } void update (node *t) { if (t == NULL) return; t->size = 1 + get_size (t->left) + get_size (t->right); } i32 get_bias (node *t, i32 b) { if (t == NULL) return 0; i32 l = get_size (t->left) + 1; i32 r = get_size (t->right) + 1; if (b * l >= r && l <= b * r) return 0; return b * l < r ? -1 : 1; } node* left_rotate (node *u) { node *v = u->right; u->right = v->left; v->left = u; update (u); update (v); return v; } node* right_rotate (node *v) { node *u = v->left; v->left = u->right; u->right = v; update (v); update (u); return u; } node* balance (node *t) { i32 b = get_bias (t, 3); if (b < 0) { if (get_bias (t->right, 2) > 0) t->right = right_rotate (t->right); t = left_rotate (t); } else if (b > 0) { if (get_bias (t->left, 2) < 0) t->left = left_rotate (t->left); t = right_rotate (t); } return t; } node* join (node *l, node *m, node *r) { i32 a = get_size (l) + 1; i32 b = get_size (r) + 1; if (3 * a >= b && a <= 3 * b) { m->left = l; m->right = r; update (m); return m; } if (3 * a < b) { r->left = join (l, m, r->left); update (r); return balance (r); } else { l->right = join (l->right, m, r); update (l); return balance (l); } } node* merge (node *l, node *r) { if (l == NULL) return r; if (r == NULL) return l; if (get_size (l) <= get_size (r)) { r->left = merge (l, r->left); update (r); return balance (r); } else { l->right = merge (l->right, r); update (l); return balance (l); } } void split (node *t, i32 k, node **x, node **y) { if (k == 0) { *x = NULL; *y = t; return; } if (get_size (t->left) + 1 <= k) { split (t->right, k - 1 - get_size (t->left), x, y); *x = join (t->left, t, *x); } else { split (t->left, k, x, y); *y = join (*y, t, t->right); } } node* insert (node *t, i64 val) { if (t == NULL) return new_node (val); if (val < t->val) { t->left = insert (t->left, val); } else { t->right = insert (t->right, val); } update (t); return balance (t); } i64 search (node *t, i32 k) { i32 c = get_size (t->left); if (k <= c) return search (t->left, k); k -= c; if (k == 1) return t->val; return search (t->right, k - 1); } node* erase (node *t, i64 val) { if (t->val == val) { node *r = merge (t->left, t->right); free (t); return r; } if (val < t->val) { t->left = erase (t->left, val); } else { t->right = erase (t->right, val); } update (t); return balance (t); } void run (void) { i32 q, k; scanf ("%" SCNi32 "%" SCNi32, &q, &k); node *set = NULL; while (q--) { i32 op; scanf ("%" SCNi32, &op); if (op == 1) { i64 v; scanf ("%" SCNi64, &v); set = insert (set, v); } else if (get_size (set) < k) { puts ("-1"); } else { i64 v = search (set, k); printf ("%" PRIi64 "\n", v); set = erase (set, v); } } } int main (void) { run(); return 0; }