Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct elem
  4. {
  5. int k;
  6. char w[10];
  7. struct elem **next;
  8. int *span;
  9. }**p, *l;
  10.  
  11. int *arr;
  12.  
  13. struct elem *Succ(struct elem *x)
  14. {
  15. return x->next[0];
  16. }
  17.  
  18. void skip(int m, int k, struct elem **p)
  19. {
  20. struct elem *x = l;
  21. int i = m - 1;
  22. while (i >= 0)
  23. {
  24. arr[i] = i;
  25. if(arr[i] == m-1) arr[i] = 0;
  26. else arr[i] = arr[i+1];
  27. while (x->next[i] != NULL && x->next[i]->k < k)
  28. {
  29. arr[i] = arr[i] + x->span[i];
  30. x = x->next[i];
  31. }
  32. p[i] = x;
  33. i--;
  34. }
  35. }
  36.  
  37. void Lookup(int m, int k)
  38. {
  39. p = (struct elem**)malloc(m * sizeof(struct elem*));
  40. struct elem *x;
  41. skip(m, k, p);
  42. x = Succ(p[0]);
  43. printf("%s\n", x->w);
  44. free(p);
  45. }
  46.  
  47. void Insert(int m, int k)
  48. {
  49. p = (struct elem**)malloc(m * sizeof(struct elem*));
  50. skip(m, k, p);
  51. struct elem *x = (struct elem*)malloc(m * sizeof(struct elem));
  52. x->next = (struct elem**)malloc(m * sizeof(struct elem*));
  53. x->span = (int*)malloc(m * sizeof(int));
  54. int i = 0;
  55. while (x < m)
  56. {
  57. x->next[i] = NULL;
  58. i++;
  59. }
  60. x->k = k;
  61. scanf("%s", x->w);
  62. int r = rand()*2;
  63. i = 0;
  64. while (i < m && r%2 == 0)
  65. {
  66. x->next[i] = p[i]->next[i];
  67. p[i]->next[i] = x;
  68. x->span[i] = p[i]->span[i] - arr[0] + arr[i];
  69. p[i]->span[i] = arr[0] - arr[i] + 1;
  70. i++;
  71. r = r/2;
  72. }
  73. while (i < m)
  74. {
  75. x->next[i] = NULL;
  76. p[i]->span[i]++;
  77. i++;
  78. }
  79. free(p);
  80. }
  81.  
  82. void rank(int m, int k)
  83. {
  84. int i = m-1;
  85. int r = 0;
  86. struct elem * x;
  87. x = l;
  88. while(i >= 0)
  89. {
  90. while(x->next[i] != NULL && k > x->next[i]->k)
  91. {
  92. r += x->span[i];
  93. x = x->next[i];
  94. }
  95. i--;
  96. }
  97. printf("%d\n", r);
  98. }
  99.  
  100. void delete(int m, int k)
  101. {
  102. p = (struct elem**)malloc(m * sizeof(struct elem*));
  103. skip(m, k, p);
  104. struct elem *x = Succ(p[0]);
  105. int i = 0;
  106. while (i < m && p[i]->next[i] == x)
  107. {
  108. p[i]->span[i] += x->span[i] - 1;
  109. p[i]->next[i] = x->next[i];
  110. i++;
  111. }
  112. while (i < m)
  113. {
  114. p[i]->span[i]--;
  115. i++;
  116. }
  117. free(p);
  118. free(x->span);
  119. free(x->next);
  120. free(x);
  121. }
  122.  
  123. int check(int m, char*s)
  124. {
  125. int k;
  126. if (s[0] == 'I')
  127. {
  128. scanf("%d", &k);
  129. Insert(m, k);
  130. return 0;
  131. }
  132. if (s[0] == 'L')
  133. {
  134. scanf("%d", &k);
  135. Lookup(m, k);
  136. return 0;
  137. }
  138. if (s[0] == 'D')
  139. {
  140. scanf("%d", &k);
  141. delete(m, k);
  142. return 0;
  143. }
  144. if (s[0] == 'R')
  145. {
  146. scanf("%d", &k);
  147. rank(m, k);
  148. return 0;
  149. }
  150. }
  151.  
  152. int main()
  153. {
  154. int n;
  155. scanf("%d", &n);
  156. int m = log2(n) + 1;
  157. arr = (int*)malloc(m * sizeof(int));
  158. l = (struct elem*)malloc(m * sizeof(struct elem));
  159. l->next = (struct elem**)malloc(m * sizeof(struct elem*));
  160. l->span = (int*)malloc(m * sizeof(int));
  161. int i = 0;
  162. while (i < m)
  163. {
  164. l->next[i] = NULL;
  165. i++;
  166. }
  167. for (int i = 0; i < n; i++)
  168. {
  169. char s[7];
  170. scanf("%s", s);
  171. check(m, s);
  172. }
  173. free(arr);
  174. struct elem *t;
  175. while(l != NULL)
  176. {
  177. t = l->next[0];
  178. free(l->span);
  179. free(l->next);
  180. free(l);
  181. l = t;
  182. }
  183. return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement