Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- #include <random>
- #include <stack>
- #include <queue>
- #include <string>
- #include <tuple>
- #include <set>
- #include <numeric>
- #include <list>
- #include <iomanip>
- #include <cassert>
- #include <array>
- #include <unordered_map>
- #include <bitset>
- #include <ctime>
- #include <chrono>
- using namespace std;
- struct node {
- long long x, y, s;
- node* l, * r;
- };
- vector<node>memory(16*1024*1024/sizeof(node));
- long long c = 0;
- node* new_node() {
- return &memory[c++];
- }
- long long size(node* a) {
- if (a == NULL) {
- return 0;
- }
- return a->s;
- }
- node* merge(node* r, node* l) {
- if (r == NULL or l == NULL) {
- return max(l, r);
- }
- if (r->y > l->y) {
- r->l = merge(l, r->l);
- r->s = size(r->l) + size(r->r);
- return r;
- }
- l->r = merge(l->r, r);
- l->s = size(l->l) + size(l->r);
- return l;
- }
- array<node*, 2> split(node* s, long long x) {
- if (s == NULL) {
- return { NULL, NULL };
- }
- if (s->x < x) {
- auto q = split(s->r, x);
- s->r = q[0];
- s->s = size(s->l) + size(s->r);
- return { s, q[1] };
- }
- auto q = split(s->l, x);
- s->l = q[1];
- s->s = size(s->l) + size(s->r);
- return { q[0], s };
- }
- node* kmin(node* s, long long k) {
- if (s == NULL) { return s; };
- if (k <= size(s->l)) {
- return kmin(s->l, k);
- }
- k -= size(s->l);
- if (k == 1) {
- return s;
- }
- return kmin(s->r, k - 1);
- }
- bool cmp(node* a, node* b) {
- return a->x < b->x;
- }
- node* add(node* s, long long x, long long y) {
- node* q = new_node();
- q->x = x; q->y = y;
- auto qwe = split(s, x);
- return merge(merge(qwe[0], q), qwe[1]);
- }
- void print(node* s) {
- if (s == NULL) return;
- print(s -> l);
- cout << s -> x << ' ';
- print(s -> r);
- }
- int main() {
- srand(time(0));
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- long long n;
- cin >> n;
- node* s = NULL;
- long long c1 = 0;
- for (long long i = 0; i < n; i++) {
- char q; long long j;
- cin >> q >> j;
- if (q == '+') {
- s = add(s, (j+c1 + 1000000000)%(1000000000), (long long)rand());
- print(s);
- cout << '\n';
- c1 = 0;
- }
- else {
- auto q = split(s, j);
- if (q[1] == NULL) {
- cout << "-1\n";
- c1 = -1;
- }
- else {
- auto qweqwe = kmin(q[1], 1);
- cout << qweqwe->x << "\n";
- c1 = qweqwe->x;
- }
- s = merge(q[0], q[1]);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement