Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <cstdlib>
- using namespace std;
- long long mod = 1e9;
- struct Treap {
- long long key;
- int prior;
- Treap *left, *right;
- Treap(long long key):key(key), left(NULL), right(NULL) { this->prior = rand(); }
- };
- Treap *merge_(Treap *l, Treap *r) {
- if (r == NULL) {
- return l;
- }
- if (l == NULL) {
- return r;
- }
- if (l->prior < r->prior) {
- r->left = merge_(l, r->left);
- return r;
- }
- else {
- l->right = merge_(l->right, r);
- return l;
- }
- }
- void split(Treap *root, long long key, Treap *&l, Treap *&r) {
- if (root == NULL) {
- l = r = NULL;
- return;
- }
- if (root->key < key) {
- Treap *tmp_l = NULL, *tmp_r = NULL;
- split(root->right, key, tmp_l, tmp_r);
- root->right = tmp_l;
- l = root, r = tmp_r;
- return;
- }
- else {
- Treap *tmp_l = NULL, *tmp_r = NULL;
- split(root->left, key, tmp_l, tmp_r);
- root->left = tmp_r;
- l = tmp_l, r = root;
- return;
- }
- }
- void find_with_parent(Treap *root, long long key, Treap *&x, Treap *&parent) {
- x = root, parent = NULL;
- while (x != NULL) {
- if (x->key == key) {
- return;
- }
- else if (x->key < key) {
- x = x->right;
- }
- else if (x->key > key) {
- parent = x, x = x->left;
- }
- }
- }
- Treap *find_(Treap *root, long long key) {
- Treap *x, *parent;
- find_with_parent(root, key, x, parent);
- return x;
- }
- bool exists(Treap *root, long long key) {
- return find_(root, key) != NULL;
- }
- bool insert(Treap *&root, long long key) {
- if (exists(root, key)) {
- return false;
- }
- Treap *l = NULL, *r = NULL;
- split(root, key, l, r);
- Treap *x = new Treap(key);
- root = merge_(merge_(l, x), r);
- return true;
- }
- bool delete_(Treap *&root, long long key) {
- if (!exists(root, key)) {
- return false;
- }
- Treap *x = NULL, *parent = NULL;
- find_with_parent(root, key, x, parent);
- if (parent == NULL) {
- root = merge_(x->left, x->right);
- }
- else if (parent->left == x) {
- parent->left = merge_(x->left, x->right);
- }
- else if (parent->right == x) {
- parent->right = merge_(x->left, x->right);
- }
- return true;
- }
- int main() {
- Treap *root = NULL;
- string s = "";
- long long number;
- int n;
- cin >> n;
- long long another_number = 0;
- for (int i = 0; i < n; i++){
- cin >> s >> number;
- if (s == "+"){
- number = (number + another_number) % mod;
- insert(root, number);
- another_number = 0;
- }
- else{
- Treap *x;
- Treap *parent;
- find_with_parent(root, number, x, parent);
- if (x == NULL){
- if (parent == NULL || parent->key < number){
- another_number = -1;
- }
- else{
- another_number = parent->key;
- }
- }
- else{
- another_number = x->key;
- }
- if (x != NULL && parent != NULL && x->key >= number && parent->key >= number){
- another_number = min(x->key, parent->key);
- }
- cout << another_number << '\n';
- /*
- if (another_number == -1){
- another_number = 0;
- }
- */
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement