Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <algorithm>
- #include <cstdio>
- #define fi first
- #define se second
- #define sc scanf
- #define pr printf
- #define pb push_back
- #define rank rank1
- #define sz(c) (int)(c).size()
- #define all(c) (c).begin(), (c).end()
- #define rall(c) (c).rbegin(), (c).rend()
- #define fin(s) freopen( s, "r", stdin );
- #define fout(s) freopen( s, "w", stdout );
- using ll = long long;
- using ld = long double;
- using ull = unsigned long long;
- using namespace std;
- const long long N = 100100;
- const long long MOD = 1e9 + 7;
- const long long INF = 1e15;
- const int block = 25;
- const long double PI = acos(-1.0);
- int TN = 1;
- struct Node {
- Node* left;
- Node* right;
- int sz;
- int x, y;
- Node(int xx) {
- x = xx; y = rand(); left = NULL; right = NULL;
- };
- };
- Node* T = NULL;
- Node* _merge(Node* A, Node* B) {
- if (!A) {
- return B;
- }
- if (!B) {
- return A;
- }
- if (A->y > B->y) {
- A->right = _merge(A->right, B);
- return A;
- }
- else if (A->y <= B->y) {
- B->left = _merge(A, B->left);
- return B;
- }
- }
- pair<Node*, Node*> _split(Node* A, int k) {
- if (!A) {
- return {NULL, NULL};
- }
- if (A->x < k) {
- auto T = _split(A->right, k);
- A->right = T.fi;
- return {A, T.se};
- }
- else {
- auto T = _split(A->left, k);
- A->left = T.se;
- return {T.fi, A};
- }
- }
- Node* _find(Node* A, int k) {
- if (!A) {
- return 0;
- }
- if (A->x == k) {
- return A;
- }
- if (A->x < k) {
- return _find(A->right, k);
- }
- else {
- return _find(A->left, k);
- }
- }
- void _insert(int x) {
- Node* k = new Node(x);
- if (!_find(T, k->x)) {
- auto t = _split(T, k->x);
- t.fi = _merge(t.fi, k);
- T = _merge(t.fi, t.se);
- }
- }
- Node* _delete(Node* A, int x) {
- auto T1 = _split(A, x);
- auto T2 = _split(T1.se, x + 1);
- if (T2.fi) {
- delete T2.fi;
- }
- return _merge(T1.fi, T2.se);
- }
- Node* _next(Node* T, int x) {
- if (!T) {
- return 0;
- }
- if (T->x > x) {
- auto to = _next(T->left, x);
- if (to)
- return to;
- else
- return T;
- }
- else {
- return _next(T->right, x);
- }
- }
- Node* _prev(Node* T, int x) {
- if (!T) {
- return 0;
- }
- if (T->x < x) {
- auto to = _prev(T->right, x);
- if (to)
- return to;
- else
- return T;
- }
- else {
- return _prev(T->left, x);
- }
- }
- string s;
- int x;
- void solve() {
- while (cin >> s) {
- cin >> x;
- if (s == "insert") {
- _insert(x);
- }
- if (s == "delete") {
- T = _delete(T, x);
- }
- if (s == "exists") {
- if (_find(T, x)) {
- cout << "true\n";
- }
- else {
- cout << "false\n";
- }
- }
- if (s == "next") {
- auto ans = _next(T, x);
- if (!ans) {
- cout << "none\n";
- }
- else {
- cout << ans->x << "\n";
- }
- }
- if (s == "prev") {
- auto ans = _prev(T, x);
- if (!ans) {
- cout << "none\n";
- }
- else {
- cout << ans->x << "\n";
- }
- }
- }
- return;
- }
- int main() {
- ios_base::sync_with_stdio(0);
- /// cin.tie(NULL); cout.tie(NULL);
- /// ------------------------------------------------
- /// fin("permutation.in"); fout("permutation.out");
- /// cin >> TN;
- /// ------------------------------------------------
- while (TN--) solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement