Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. const int MAXN = 1000010; //1e6
  6. struct node {
  7. int key, prio;
  8. node *ch[2];
  9. } base[MAXN], *top, *root, *null, nil;
  10.  
  11. typedef node* tree;
  12.  
  13. inline tree newNode(int key) {
  14. static int seed = 3312;
  15. top -> key = key; top -> prio = seed = int((seed * 48271LL) & 2147483647);
  16. top -> ch[0] = top -> ch[1] = null;
  17. return top++;
  18. }
  19.  
  20. inline void Rotate(tree &x, int d) {
  21. tree y = x -> ch[!d];
  22. x -> ch[!d] = y -> ch[d];
  23. y -> ch[d] = x;
  24. x = y;
  25. }
  26.  
  27. void Insert(tree &t, int key) {
  28. if(t == null) t = newNode(key);
  29. else
  30. {
  31. int d = t -> key < key;
  32. Insert(t -> ch[d], key);
  33. if(t -> ch[d] -> prio < t -> prio) Rotate(t, !d);
  34. }
  35. }
  36.  
  37. //assuming the key exists in the set
  38. void Delete(tree &t, int key) {
  39. if (t -> key != key) {
  40. Delete(t -> ch[t -> key < key], key);
  41. }
  42. else if(t -> ch[0] == null) t = t -> ch[1];
  43. else if(t -> ch[1] == null) t = t -> ch[0];
  44. else {
  45. int d = t -> ch[0] -> prio < t -> ch[1] -> prio;
  46. Rotate(t, d);
  47. Delete(t -> ch[d], key);
  48. }
  49. }
  50.  
  51. int main()
  52. {
  53. srand(time(0));
  54. const int n = 1000000; //1e6
  55. null = &nil;
  56. top = base;
  57. root = newNode(0); //dummy new root
  58. clock_t t0 = clock();
  59. for(int i = 0; i < n; ++i)
  60. {
  61. Insert(root, rand()); //rand key insert
  62. }
  63. printf("Time: %f\n", ((float)clock() - t0) / CLOCKS_PER_SEC);
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement