結果

問題 No.649 ここでちょっとQK!
ユーザー みずくらげみずくらげ
提出日時 2019-07-23 11:54:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 403 ms / 3,000 ms
コード長 4,747 bytes
コンパイル時間 939 ms
コンパイル使用メモリ 74,568 KB
実行使用メモリ 12,996 KB
最終ジャッジ日時 2023-09-07 09:23:51
合計ジャッジ時間 7,697 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 248 ms
4,380 KB
testcase_04 AC 183 ms
12,792 KB
testcase_05 AC 180 ms
12,932 KB
testcase_06 AC 188 ms
12,996 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 206 ms
7,580 KB
testcase_13 AC 171 ms
7,624 KB
testcase_14 AC 166 ms
7,768 KB
testcase_15 AC 179 ms
7,800 KB
testcase_16 AC 173 ms
7,644 KB
testcase_17 AC 194 ms
8,000 KB
testcase_18 AC 215 ms
8,420 KB
testcase_19 AC 231 ms
8,824 KB
testcase_20 AC 251 ms
9,264 KB
testcase_21 AC 269 ms
9,688 KB
testcase_22 AC 295 ms
10,104 KB
testcase_23 AC 316 ms
10,528 KB
testcase_24 AC 336 ms
11,000 KB
testcase_25 AC 361 ms
11,460 KB
testcase_26 AC 403 ms
11,904 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 147 ms
7,644 KB
testcase_31 AC 151 ms
7,640 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 実験(他の人のコード)


#include <iostream>
using namespace std;


typedef long long VAL;

struct RBST {
    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(0), size(1), sum(0) {
            left = right = NULL;
        }
        
		NODE(VAL v) : val(v), size(1), sum(v) {
			left = right = NULL;
		}


    };
    
    
    ///////////////////////
    // 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 ? 0 : 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;
		return node;
	}

    
    /* lower_bound */
    inline int lowerBound(NODE *node, VAL val) {
        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) {
        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) {
        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) {
		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)
		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);
    }
};
    

int main() {
    RBST S;
    int Q, K;
    cin >> Q >> K;
    for (int i = 0; i < Q; ++i) {
        int T;
        cin >> T;
        if (T == 1) {
            long long val;
            cin >> val;
            S.insert(val);
        }
        else {
            if (S.size() < K) {
                cout << -1 << endl;
                continue;
            }
            long long res = S.get(K-1);
            cout << res << endl;
            S.erase(res);
        }
    }
}













0