Advertisement
Matrix_code

data-structure - Skip_list

Aug 24th, 2017 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. // SKIP LIST
  2. // https://www.codechef.com/JAN12/problems/CARDSHUF
  3.  
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<math.h>
  7. #define REP(i,a,b) for(i=a;i<b;i++)
  8. #define rep(i,n) REP(i,0,n)
  9.  
  10.  
  11. #define RAND (rand()/(RAND_MAX+1.0))
  12.  
  13. #define SKIPLIST_MAX_HEIGHT 18
  14. #define SKIPLIST_SCALE 2.0
  15.  
  16. typedef struct struct_skip_list{
  17.   int height;
  18.   int val;
  19.   int *dist;
  20.   struct struct_skip_list **next;
  21. }skipList;
  22.  
  23.  
  24. void skipListInit(skipList *s, int val, int height){
  25.   int i;
  26.   if(height <= 0){
  27.     REP(i,1,SKIPLIST_MAX_HEIGHT) if(RAND*SKIPLIST_SCALE < 1.0) break;
  28.     height = i;
  29.   }
  30.   s->dist = (int*)malloc(height*sizeof(int));
  31.   s->next = (skipList**)malloc(height*sizeof(skipList*));
  32.   rep(i,height) s->dist[i] = -1;
  33.   s->val = val;
  34.   s->height = height;
  35. }
  36.  
  37. void skipListFree(skipList *s){
  38.   free(s->dist); free(s->next);
  39. }
  40.  
  41. int skipListGetSize(skipList *s){
  42.   int i;
  43.   for(i=s->height-1; i>=0; i--) if(s->dist[i] > 0){
  44.     return s->dist[i] + skipListGetSize(s->next[i]);
  45.   }
  46.   return 0;
  47. }
  48.  
  49. void skipListAddElementInLast(skipList *s, skipList *add, int sz){
  50.   int i;
  51.   for(i=s->height-1; i>=0; i--){
  52.     if(s->dist[i] < 0 && i < add->height){
  53.       if(sz <= -1) sz = skipListGetSize(s);
  54.       s->dist[i] = sz+1;
  55.       s->next[i] = add;
  56.       continue;
  57.     }
  58.     if(s->dist[i] >= 0){
  59.       skipListAddElementInLast(s->next[i], add, sz-(s->dist[i]));
  60.       break;
  61.     }
  62.   }
  63. }
  64.  
  65. void skipListAddListInLast(skipList *s, skipList *add, int sz){
  66.   int i;
  67.   for(i=s->height-1; i>=0; i--){
  68.     if(s->dist[i] < 0 && add->dist[i] >= 0){
  69.       if(sz <= -1) sz = skipListGetSize(s);
  70.       s->dist[i] = sz + add->dist[i];
  71.       s->next[i] = add->next[i];
  72.       continue;
  73.     }
  74.     if(s->dist[i] >= 0){
  75.       skipListAddListInLast(s->next[i], add, sz-(s->dist[i]));
  76.       break;
  77.     }
  78.   }
  79. }
  80.  
  81. /* a => a[1..K], b => a[K+1..last] */
  82. void skipListDevide(skipList *a, skipList *b, int K){
  83.   int i;
  84.  
  85.   rep(i,a->height) b->dist[i] = -1;
  86.  
  87.   for(i=a->height-1; i>=0; i--){
  88.     if(a->dist[i] < 0) continue;
  89.     if(a->dist[i] > K){
  90.       b->dist[i] = a->dist[i] - K;
  91.       b->next[i] = a->next[i];
  92.       a->dist[i] = -1;
  93.       continue;
  94.     }
  95.     if(a->dist[i] <= K){
  96.       skipListDevide(a->next[i], b, K-(a->dist[i]));
  97.       break;
  98.     }
  99.   }
  100. }
  101.  
  102. skipList* skipListGetKthElement(skipList *s, int K){
  103.   int i;
  104.  
  105.   if(K==0) return s;
  106.   for(i=s->height-1; i>=0; i--){
  107.     if(s->dist[i] < 0) continue;
  108.     if(K >= s->dist[i]) return skipListGetKthElement(s->next[i], K-(s->dist[i]));
  109.   }
  110.  
  111.   return (skipList*)NULL;
  112. }
  113.  
  114. void skipListPuts(skipList *s){
  115.   int br = 0;
  116.   for(;;){
  117.     if(s->dist[0] < 0) break;
  118.     s = s->next[0];
  119.     /*    if(br) printf(" -> "); else br = 1;*/
  120.     if(br) putchar(' '); else br = 1;
  121.     printf("%d",s->val+1);
  122.   }
  123.   puts("");
  124. }
  125.  
  126.  
  127. skipList S, A, B, C, ls[111111]; /* list: 1 -> 2 -> ... initially */
  128. skipList SS, AA, BB, CC, lsls[111111]; /* list: N -> N-1 -> ... initially */
  129.  
  130. int main(){
  131.   int i,j,k,l;
  132.   int N, M;
  133.   int a, b, c;
  134.   skipList swap;
  135.  
  136.   srand(time(NULL));
  137.  
  138.   skipListInit(&S, -1, SKIPLIST_MAX_HEIGHT);
  139.   skipListInit(&A, -1, SKIPLIST_MAX_HEIGHT);
  140.   skipListInit(&B, -1, SKIPLIST_MAX_HEIGHT);
  141.   skipListInit(&C, -1, SKIPLIST_MAX_HEIGHT);
  142.   skipListInit(&SS, -1, SKIPLIST_MAX_HEIGHT);
  143.   skipListInit(&AA, -1, SKIPLIST_MAX_HEIGHT);
  144.   skipListInit(&BB, -1, SKIPLIST_MAX_HEIGHT);
  145.   skipListInit(&CC, -1, SKIPLIST_MAX_HEIGHT);
  146.  
  147.   scanf("%d%d",&N,&M);
  148.   rep(i,N) skipListInit(ls+i, i, 0);
  149.   rep(i,N) skipListAddElementInLast(&S, ls+i, i);
  150.   rep(i,N) skipListInit(lsls+i, N-1-i, 0);
  151.   rep(i,N) skipListAddElementInLast(&SS, lsls+i, i);
  152.  
  153.   while(M--){
  154.     scanf("%d%d%d",&a,&b,&c);
  155.    
  156.     swap = S; S = A; A = swap;
  157.     skipListDevide(&A, &S, a);
  158.     skipListDevide(&SS, &AA, N-a);
  159.    
  160.     swap = S; S = B; B = swap;
  161.     skipListDevide(&B, &S, b);
  162.     skipListDevide(&SS, &BB, N-a-b);
  163.    
  164.     swap = S; S = A; A = swap;
  165.     skipListAddListInLast(&S, &A, a);
  166.     skipListAddListInLast(&SS, &AA, N-a-b);
  167.    
  168.     swap = S; S = C; C = swap;
  169.     skipListDevide(&C, &S, c);
  170.     skipListDevide(&SS, &CC, N-b-c);
  171.    
  172.     swap = S; S = BB; BB = swap;
  173.     skipListAddListInLast(&S, &BB, b);
  174.     skipListAddListInLast(&SS, &B, N-b-c);
  175.    
  176.     swap = S; S = C; C = swap;
  177.     skipListAddListInLast(&S, &C, c);
  178.     skipListAddListInLast(&SS, &CC, N-c);
  179.   }
  180.  
  181.   skipListPuts(&S);
  182.  
  183.   return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement