fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define MAXK 1000006
  5.  
  6. static int K;
  7.  
  8. static struct STATE{
  9. int len;
  10. char letter;
  11. STATE *prev[20];
  12. } *state[MAXK];
  13.  
  14. void Init() {}
  15.  
  16. STATE *make(int len, char letter, STATE *prev)
  17. {
  18. STATE *ret=(STATE*)malloc(sizeof(STATE));
  19. ret->len = len; ret->letter = letter;
  20. for (int i=0;i<20;i++) ret->prev[i] = NULL;
  21. ret->prev[0] = prev;
  22. for (int i=0;i<19&&ret->prev[i];i++){
  23. ret->prev[i+1] = ret->prev[i]->prev[i];
  24. }
  25. return ret;
  26. }
  27.  
  28. void TypeLetter(char letter)
  29. {
  30. STATE *tmp;
  31. if (state[K]) tmp = make(state[K]->len+1,letter,state[K]);
  32. else tmp = make(1,letter,NULL);
  33. state[++K] = tmp;
  34. }
  35.  
  36. void UndoCommands(int steps)
  37. {
  38. state[++K] = state[K-steps-1];
  39. }
  40.  
  41. char GetLetter(int pos)
  42. {
  43. STATE *now=state[K];
  44. int bef=now->len-pos-1,i;
  45. for (i=20;i--;) if ((bef>>i)&1){
  46. now = now->prev[i];
  47. }
  48. return now->letter;
  49. }
  50.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty