Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <iomanip>
- #include <queue>
- #include <cmath>
- #include <algorithm>
- #include <tuple>
- #include <iomanip>
- #include <stdio.h>
- #include <map>
- #include <bitset>
- #include <set>
- #include <stack>
- #include <queue>
- #include <unordered_set>
- #include <cassert>
- #include <stdlib.h>
- #include <time.h>
- #include <random>
- //#pragma GCC optimize("Ofast,no-stack-protector")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
- //#pragma GCC optimize("unroll-loops")
- //#pragma GCC optimize("fast-math")
- //#pragma GCC optimize("section-anchors")
- //#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
- //#pragma GCC optimize("vpt")
- //#pragma GCC optimize("rename-registers")
- //#pragma GCC optimize("move-loop-invariants")
- //#pragma GCC optimize("unswitch-loops")
- //#pragma GCC optimize("function-sections")
- //#pragma GCC optimize("data-sections")
- //#pragma GCC optimize("branch-target-load-optimize")
- //#pragma GCC optimize("branch-target-load-optimize2")
- //#pragma GCC optimize("btr-bb-exclusive")
- #define int long long
- #define ll long long
- #define ull unsigned long long
- #define all(a) a.begin(), a.end()
- #define pii pair<int, int>
- #define pb push_back
- #define ld long double
- using namespace std;
- const int INF = 1e17;
- //const int mod = 2600000069;
- //const int p = 179;
- struct node{
- node *l, *r;
- int x, y = rand();
- node() {
- x = -1;
- l = r = nullptr;
- }
- node(int x_) {
- x = x_;
- l = r = nullptr;
- }
- };
- typedef node* pnode;
- vector<node> a(300000);
- int ind = 0;
- pnode merge(pnode a, pnode b) {
- if (!a) return b;
- if (!b) return a;
- if (a->y > b->y) {
- a->r = merge(a->r, b);
- return a;
- } else {
- b->l = merge(a, b->l);
- return b;
- }
- }
- void split(pnode a, int x, pnode& l, pnode& r) {
- if (!a) {
- l = r = nullptr;
- return;
- }
- if (a->x < x) {
- split(a->r, x, l, r);
- a->r = l;
- l = a;
- } else {
- split(a->l, x, l, r);
- a->l = r;
- r = a;
- }
- }
- pnode root = nullptr;
- void add(int x) {
- pair<pnode, pnode> q;
- split(root, x, q.first, q.second);
- pnode t = &a[ind++];
- t->x = x;
- root = merge(q.first, merge(t, q.second));
- }
- int last = 0;
- int left_most(pnode t) {
- if (t->l) {
- return left_most(t->l);
- } else return t->x;
- }
- void next(int x) {
- pair<pnode, pnode> q;
- split(root, x, q.first, q.second);
- if (!q.second) {
- last = -1;
- } else {
- last = left_most(q.second);
- }
- root = merge(q.first, q.second);
- cout << last << "\n";
- }
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int n;
- cin >> n;
- char t;
- int x;
- while (n--) {
- // for (int i = 0; i < ind; i++) {
- // cout << a[i].x << " " << a[i].y << "\n";
- // }
- cin >> t >> x;
- if (t == '+') {
- add(x + last);
- } else {
- next(x);
- }
- }
- }
- /*
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement