Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <algorithm>
- #include <cstdio>
- #include <cstdlib>
- #include <string>
- #include <cstring>
- #include <vector>
- using namespace std;
- struct item {
- int key, prior;
- item * l, * r;
- item () {}
- item (int key, int prior) : key (key), prior (prior), l (NULL), r (NULL) {}
- };
- typedef item * pitem;
- pitem root;
- void split (pitem t, int key, pitem & l, pitem & r)
- {
- if (! t)
- l = NULL, r = NULL;
- else if (key >= t.key)
- {
- split (t.r, key, t.r, r);
- l = t;
- }
- else
- {
- split (t.l, key, l, t.l);
- r = t;
- }
- }
- void insert (pitem & t, pitem it)
- {
- if (! t)
- {
- t = it;
- }
- else if (t.prior < it.prior)
- {
- split (t, it.key, it.l, it.r), t = it;
- }
- else
- {
- insert (it.key > t.key ? t.r : t.l, it);
- }
- }
- int sear (pitem t, int x)
- {
- if (!t)
- return -1;
- else if (t.key < x) sear (t.r, x);
- else if (t.key > x) sear (t.l, x);
- else return t.prior;
- }
- int main() {
- #ifndef ONLINE_JUDGE
- freopen("input.txt","rt",stdin);
- freopen("output.txt","wt",stdout);
- #endif
- srand (time (NULL));
- int z;
- scanf ("%d", &z);
- for (int i = 1; i <= z; i++)
- {
- scanf ("\n");
- char c;
- int a;
- scanf ("%c", &c);
- if (c == 'a')
- {
- scanf (" %d", &a);
- pritem news;
- news.key = a;
- // check it! //
- news.prior = rand () % 100000;
- insert (root, news);
- }
- else if (c == 's')
- {
- cout << "the prior: ";
- scanf (" %d", &a);
- cout << sear (root, a);
- cout << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement