Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define pragma
- #ifdef pragma
- #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #endif // pragma
- #include<bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define ll long long
- #define all(x) begin(x), end(x)
- #define pb push_back
- #define x first
- #define y second
- //#define int long long
- #define zero(two) memset(two, 0, sizeof(two))
- using namespace std;
- using namespace __gnu_pbds;
- typedef vector<int> vi;
- typedef vector<bool> vb;
- typedef pair<int, int> pii;
- typedef long double ld;
- typedef vector<vi> matrix;
- template<typename T>
- using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
- const ld PI = atan2(0, -1);
- void seriy() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- // cout << fixed << setprecision(10);
- #if _offline
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #else
- freopen("next.in", "r", stdin);
- freopen("next.out", "w", stdout);
- #endif
- }
- const int MAXN = 2e5 + 10;
- const int INF = 2e9 + 7;
- const int MAXLOG = 20;
- const int MOD = 1e9;
- const int BASE = 47;
- const int MAX = (1ll << 32) - 1;
- struct treap {
- int x, y;
- treap *left = nullptr;
- treap *right = nullptr;
- treap(int x1, int y1) {
- x = x1;
- y = y1;
- }
- treap() {
- }
- };
- struct pt {
- int x, y, num;
- };
- treap *root = nullptr;
- int sz = 1;
- void split(treap *t, int k, treap *&a, treap *&b) {
- if(t == nullptr) {
- a = nullptr;
- b = nullptr;
- return;
- }
- if(k > t -> x) {
- split(t -> right, k, t -> right, b);
- a = t;
- }
- else {
- split(t -> left, k, a, t -> left);
- b = t;
- }
- }
- treap *unite(treap *t1, treap *t2) {
- if(t2 == nullptr) {
- return t1;
- }
- if(t1 == nullptr) {
- return t2;
- }
- if(t1 -> y > t2 -> y) {
- t1 -> right = unite(t1 -> right, t2);
- return t1;
- }
- else {
- t2 -> left = unite(t1, t2 -> left);
- return t2;
- }
- }
- void add(int x) {
- // cerr << x << '\n';
- mt19937 rr(chrono::high_resolution_clock::now().time_since_epoch().count());
- treap *cur = new treap(x, rr());
- if(root == nullptr) {
- root = cur;
- return;
- }
- pair<treap*, treap*> kek;
- split(root, x, kek.x, kek.y);
- kek.x = unite(kek.x, cur);
- root = unite(kek.x, kek.y);
- }
- int get_ans(treap *kek) {
- // cerr << kek -> x << '\n';
- if(kek -> left == nullptr) {
- return kek -> x;
- }
- return get_ans(kek -> left);
- }
- int get(int x) {
- pair<treap*, treap*> kek;
- split(root, x, kek.x, kek.y);
- // if(x == 2) {
- // cerr << (kek.x == nullptr) << '\n';
- // }
- // cerr << '\n';
- if(kek.y == nullptr) {
- root = unite(kek.x, kek.y);
- return -1;
- }
- // cerr << x << '\n';
- int ans = get_ans(kek.y);
- root = unite(kek.x, kek.y);
- return ans;
- }
- signed main() {
- seriy();
- int q;
- cin >> q;
- int prev = -1;
- while(q--) {
- char tp;
- cin >> tp;
- if(tp == '+') {
- int n;
- cin >> n;
- if(prev == -1) {
- // cerr << n << '\n';
- add(n);
- }
- else {
- // cerr << (n + prev) % MOD << '\n';
- add((n + prev) % MOD);
- }
- prev = -1;
- }
- else {
- int n;
- cin >> n;
- prev = get(n);
- cout << prev << '\n';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement