結果

問題 No.649 ここでちょっとQK!
ユーザー aajisakaaajisaka
提出日時 2019-11-14 20:20:19
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 281 ms / 3,000 ms
コード長 7,044 bytes
コンパイル時間 1,910 ms
コンパイル使用メモリ 178,700 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-09-21 22:31:56
合計ジャッジ時間 6,551 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author aajisaka
*/
#include<bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
constexpr double PI = 3.14159265358979323846;
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());
// Original: https://qiita.com/drken/items/1b7e6e459c24a83bb7fd
// BEGIN
template<class VAL> struct RBST {
VAL SUM_UNITY = 0; // to be set
unsigned int randInt() {
static unsigned int tx = 123456789, ty = 362436069, tz = 521288629, tw = 88675123;
unsigned int tt = (tx ^ (tx << 11));
tx = ty; ty = tz; tz = tw;
return (tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)));
}
struct NODE {
NODE *left, *right;
VAL val; // the value of the node
int size; // the size of the subtree
VAL sum; // the value-sum of the subtree
NODE() : val(SUM_UNITY), size(1), sum(SUM_UNITY) {
left = right = NULL;
}
NODE(VAL v) : val(v), size(1), sum(v) {
left = right = NULL;
}
/* additional update */
inline void update() {
}
/* additional lazy-propagation */
inline void push() {
/* ex: reverse */
/*
if (this->rev) {
swap(this->left, this->right);
if (this->left) this->left->rev ^= true;
if (this->right) this->right->rev ^= true;
this->rev = false;
}
*/
}
};
///////////////////////
// root
///////////////////////
NODE* root;
RBST() : root(NULL) { }
RBST(NODE* node) : root(node) { }
///////////////////////
// basic operations
///////////////////////
/* size */
inline int size(NODE *node) {
return !node ? 0 : node->size;
}
inline int size() {
return this->size(this->root);
}
/* sum */
inline VAL sum(NODE *node) {
return !node ? SUM_UNITY : node->sum;
}
inline VAL sum() {
return this->sum(this->root);
}
/* update, push */
inline NODE* update(NODE *node) {
node->size = size(node->left) + size(node->right) + 1;
node->sum = sum(node->left) + sum(node->right) + node->val;
node->update();
return node;
}
inline void push(NODE *node) {
if (!node) return;
node->push();
}
/* lower_bound */
inline int lowerBound(NODE *node, VAL val) {
push(node);
if (!node) return 0;
if (val <= node->val) return lowerBound(node->left, val);
else return size(node->left) + lowerBound(node->right, val) + 1;
}
inline int lowerBound(VAL val) {
return this->lowerBound(this->root, val);
}
/* upper_bound */
inline int upperBound(NODE *node, VAL val) {
push(node);
if (!node) return 0;
if (val >= node->val) return size(node->left) + upperBound(node->right, val) + 1;
else return upperBound(node->left, val);
}
inline int upperBound(VAL val) {
return this->upperBound(this->root, val);
}
/* count */
inline int count(VAL val) {
return upperBound(val) - lowerBound(val);
}
/* get --- k: 0-index */
inline VAL get(NODE *node, int k) {
push(node);
if (!node) return -1;
if (k == size(node->left)) return node->val;
if (k < size(node->left)) return get(node->left, k);
else return get(node->right, k - size(node->left) - 1);
}
inline VAL get(int k) {
return get(this->root, k);
}
///////////////////////
// merge-split
///////////////////////
NODE* merge(NODE *left, NODE *right) {
push(left);
push(right);
if (!left || !right) {
if (left) return left;
else return right;
}
if (randInt() % (left->size + right->size) < left->size) {
left->right = merge(left->right, right);
return update(left);
}
else {
right->left = merge(left, right->left);
return update(right);
}
}
void merge(RBST add) {
this->root = this->merge(this->root, add.root);
}
pair<NODE*, NODE*> split(NODE* node, int k) { // [0, k), [k, n)
push(node);
if (!node) return make_pair(node, node);
if (k <= size(node->left)) {
pair<NODE*, NODE*> sub = split(node->left, k);
node->left = sub.second;
return make_pair(sub.first, update(node));
}
else {
pair<NODE*, NODE*> sub = split(node->right, k - size(node->left) - 1);
node->right = sub.first;
return make_pair(update(node), sub.second);
}
}
RBST split(int k) {
pair<NODE*, NODE*> sub = split(this->root, k);
this->root = sub.first;
return RBST(sub.second);
}
///////////////////////
// insert-erase
///////////////////////
void insert(const VAL val) {
pair<NODE*, NODE*> sub = this->split(this->root, this->lowerBound(val));
this->root = this->merge(this->merge(sub.first, new NODE(val)), sub.second);
}
void erase(const VAL val) {
if (!this->count(val)) return;
pair<NODE*, NODE*> sub = this->split(this->root, this->lowerBound(val));
this->root = this->merge(sub.first, this->split(sub.second, 1).second);
}
///////////////////////
// debug
///////////////////////
void print(NODE *node) {
if (!node) return;
push(node);
print(node->left);
cout << node->val << " ";
print(node->right);
}
void print() {
cout << "{";
print(this->root);
cout << "}" << endl;
}
};
// END
class D {
public:
void solve(istream& cin, ostream& cout) {
SPEED;
int q, k; cin >> q >> k;
RBST<ll> st;
while(q--) {
int a; cin >> a;
if (a==1) {
ll v; cin >> v;
st.insert(v);
} else {
if (st.size() < k) {
cout << -1 << '\n';
} else {
ll ret = st.get(k-1);
//debug(ret);
cout << ret << '\n';
st.erase(ret);
}
}
}
}
};
signed main() {
D solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0