Advertisement
Guest User

mYcOdE

a guest
Jan 23rd, 2012
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <algorithm>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <string>
  7. #include <cstring>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. struct item {
  13.     int key, prior;
  14.     item * l, * r;
  15.     item () {}
  16.     item (int key, int prior) : key (key), prior (prior), l (NULL), r (NULL) {}
  17. };
  18.  
  19. typedef item * pitem;
  20.  
  21. pitem root;
  22.  
  23. void split (pitem t, int key, pitem & l, pitem & r)
  24. {
  25.     if (! t)
  26.         l = NULL, r = NULL;
  27.     else if (key >= t.key)
  28.     {
  29.         split (t.r, key, t.r, r);
  30.         l = t;
  31.     }
  32.     else
  33.     {
  34.         split (t.l, key, l, t.l);
  35.         r = t;
  36.     }
  37. }
  38.  
  39. void insert (pitem & t, pitem it)
  40. {
  41.     if (! t)
  42.     {
  43.         t = it;
  44.     }
  45.     else if (t.prior < it.prior)
  46.     {
  47.         split (t, it.key, it.l, it.r), t = it;
  48.     }
  49.     else
  50.     {
  51.         insert (it.key > t.key ? t.r : t.l, it);
  52.     }
  53. }
  54.  
  55. int sear (pitem t, int x)
  56. {
  57.     if (!t)
  58.         return -1;
  59.     else if (t.key < x) sear (t.r, x);
  60.     else if (t.key > x) sear (t.l, x);
  61.     else return t.prior;
  62. }
  63.  
  64.  
  65. int main() {
  66.  
  67. #ifndef ONLINE_JUDGE
  68.     freopen("input.txt","rt",stdin);
  69.     freopen("output.txt","wt",stdout);
  70. #endif
  71.     srand (time (NULL));
  72.     int z;
  73.     scanf ("%d", &z);
  74.     for (int i = 1; i <= z; i++)
  75.     {
  76.         scanf ("\n");
  77.         char c;
  78.         int a;
  79.         scanf ("%c", &c);
  80.         if (c == 'a')
  81.         {
  82.             scanf (" %d", &a);
  83.             pritem news;
  84.             news.key = a;
  85.             // check it! //
  86.             news.prior = rand () % 100000;
  87.             insert (root, news);
  88.         }
  89.         else if (c == 's')
  90.         {
  91.             cout << "the prior: ";
  92.             scanf (" %d", &a);
  93.             cout << sear (root, a);
  94.             cout << "\n";
  95.         }
  96.     }
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement