Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef long long int int64;
  6.  
  7. typedef struct node{
  8.     int64 key;
  9.     struct node *left, *right;
  10. }node;
  11.  
  12. int64 count1 = 0, count = 0;
  13.  
  14. node *newNode(int64 item){
  15.     node *temp =  (node *) malloc(sizeof(node));
  16.     temp->key = item;
  17.     temp->left = temp->right = NULL;
  18.     return temp;
  19. }
  20.  
  21. node *BST_insert(node* root, int64 key){
  22.  
  23.     if(root == NULL){
  24.         count1++;
  25.       return newNode(key);            
  26.     }
  27.  
  28.     if(key < root->key)
  29.         root->left  = BST_insert(root->left, key);
  30.      else if(key > root->key)
  31.         root->right = BST_insert(root->right, key);
  32.    
  33.     return root;
  34. }
  35.  
  36. node *minValueNode(node* root){
  37.     node *atual = root;
  38.  
  39.     while(atual->left != NULL)
  40.          atual = atual->left;
  41.  
  42.     return atual;
  43. }
  44.  
  45. node *BST_delete(node* root, int64 key){
  46.  
  47.     if (root == NULL) return root;
  48.  
  49.     if (key < root->key)
  50.         root->left = BST_delete(root->left, key);
  51.  
  52.     else if (key > root->key)
  53.         root->right = BST_delete(root->right, key);
  54.  
  55.     else{
  56.         if (root->left == NULL){
  57.             node *temp = root->right;
  58.             free(root);
  59.             return temp;
  60.         }
  61.         else if (root->right == NULL){
  62.             node *temp = root->left;
  63.             free(root);
  64.             return temp;
  65.         }
  66.  
  67.         node* temp = minValueNode(root->right);
  68.         root->key = temp->key;
  69.         root->right = BST_delete(root->right, temp->key);
  70.     }
  71.     return root;
  72. }
  73.  
  74. node* BST_search(node *root, int64 key){
  75.    node *atual = root;
  76.    
  77.    while(atual->key != key){
  78.    
  79.       if(atual != NULL) {
  80.        
  81.          if(atual->key > key){
  82.             atual = atual->left;
  83.          }
  84.          else {                
  85.             atual = atual->right;
  86.          }        
  87.          if(atual == NULL){
  88.             return NULL;
  89.          }
  90.       }        
  91.    }
  92.    return atual;
  93. }
  94.  
  95. int64 pivot(int64 *d, int64 n){
  96.     int64 pivot[3] = { 0 }, aux = 0, x = 0, y = 0;
  97.  
  98.     if(n < 3)
  99.         return d[0];
  100.     else if(n == 0)
  101.         return 2;
  102.     else{
  103.         pivot[0] = d[0], pivot[1] = d[(n-1)/2], pivot[2] = d[n-1];
  104.  
  105.         for(x = 0; x < 2; x++){
  106.             for(y = x + 1; y < 3; y++){
  107.                 if(pivot[x] < pivot[y]){
  108.                     aux = pivot[x];
  109.                     pivot[x] = pivot[y];
  110.                     pivot[y] = aux;
  111.                 }
  112.             }
  113.         }
  114.         return pivot[1];
  115.     }
  116. }
  117.  
  118. int64 height(node* root){
  119.     if(root == NULL)
  120.        return 0;
  121.     else{
  122.  
  123.        int64 l = height(root->left);
  124.        int64 r = height(root->right);
  125.  
  126.         if(l > r)
  127.            return(l+1);
  128.         else return(r+1);
  129.    }
  130. }
  131.  
  132. int64 *congruente(int64 a, int64 c, int64 n, int64 d[], int64 m, int64 seedD){
  133.     int64 linear = 0, x = 0;
  134.  
  135.     d[0] = seedD;
  136.  
  137.     for(x = 1; x < n; x++){
  138.         linear = (a*d[x-1] + c)%m;
  139.         d[x] = linear;
  140.     }
  141.  
  142.     return d;
  143. }
  144.  
  145. int64 *aloca(long long int size){
  146.     int64 *vetor;
  147.  
  148.     vetor = (int64 *) malloc(sizeof(int64)*size);
  149.  
  150.     return vetor;
  151. }
  152.  
  153. void D_insert_tree(node *root, int64 *D, int64 n, int64 pivo){
  154.     int64 *s1, *s2, aux = 0, auxi = 0, x = 0, y = 0, aux3 = 0, aux4 = 0;
  155.  
  156.     s1 = aloca(n);
  157.     s2 = aloca(n);
  158.  
  159.     if(n == 1){
  160.         root = BST_insert(root, D[0]);
  161.         return;
  162.     }
  163.     else if(n == 2){
  164.         root = BST_insert(root, D[0]);
  165.         root = BST_insert(root, D[1]);
  166.         return;
  167.     }
  168.  
  169.     for(x = 0; x < n; x++){
  170.         if(pivo > D[x]){
  171.             s2[aux3] = D[x];
  172.             aux3++;
  173.         }
  174.         else{
  175.             if(pivo < D[x]){
  176.                 s1[aux4] = D[x];
  177.                 aux4++;
  178.             }
  179.         }
  180.     }
  181.  
  182.     aux = pivot(s1, aux4);
  183.     if(aux == 2)
  184.         return;
  185.     else{
  186.         root = BST_insert(root, aux);
  187.         D_insert_tree(root, s1, aux4, aux);
  188.     }
  189.  
  190.     auxi = pivot(s2, aux3);
  191.     if(auxi == 2)
  192.         return;
  193.     else{
  194.         root = BST_insert(root, auxi);
  195.         D_insert_tree(root, s2, aux3, auxi);
  196.     }
  197. }
  198.  
  199. void printPostorder(node* root, int64 l, int64 r){
  200.  
  201.      if (root == NULL)
  202.         return;
  203.  
  204.     if(l <= r && r < root->key){
  205.         printPostorder(root->left, l, r);        
  206.         l++;
  207.         count++;
  208.     }
  209.     else if(l <= r && r > root->key){
  210.         printPostorder(root->right, l, r);        
  211.         l++;
  212.         count++;
  213.     }
  214.     root = BST_delete(root, l);
  215. }
  216.  
  217. int main(){
  218.     int64 congru = 0, m = 0, a = 0, c = 0, seedD = 0, *D, pivo = 0, n = 0, q = 0, x = 0, y = 0, l = 0, r = 0, add = 0, aux1 = 0, aux2 = 0;
  219.     char oper[4];
  220.     node *root = NULL, *del = NULL;
  221.  
  222.     scanf("%lld %lld %lld %lld %lld", &n, &m, &seedD, &a, &c);
  223.  
  224.     scanf("%lld", &q);
  225.  
  226.     D = aloca(n);
  227.  
  228.     D = congruente(a, c, n, D, m, seedD);
  229.  
  230.     pivo = pivot(D, n);
  231.  
  232.     root = BST_insert(root, pivo);
  233.  
  234.     D_insert_tree(root, D, n, pivo);
  235.  
  236.     printf("0: %lld\n",  height(root));
  237.  
  238.     for(x = 0; x < q; x++){
  239.         scanf("%s", oper);
  240.  
  241.         if(strcmp(oper, "DEL") == 0){  
  242.  
  243.             scanf("%lld %lld", &l, &r);
  244.            
  245.             printPostorder(root, l, r);
  246.            /* for(y = l; y <= r; y++){
  247.  
  248.                 del = BST_search(root, y);
  249.  
  250.                 if(del != NULL){
  251.                     root = BST_delete(root, y);
  252.                     count++;
  253.                 }
  254.             }*/
  255.             printf("%lld: %lld\n", x+1, count);
  256.         }
  257.         else{
  258.             scanf("%lld", &add);
  259.  
  260.             //temp = BST_search(root, add);
  261.             root = BST_insert(root, add);
  262.  
  263.             if(count1 != 0)
  264.                 printf("%lld: 1\n", x+1);
  265.             else{          
  266.                 printf("%lld: 0\n", x+1);                
  267.             }
  268.         }
  269.          count = 0;
  270.          count1 = 0;
  271.     }
  272.     return 0;
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement