KeyInsertion.blocklist.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include<stdio.h>
  2. #include<string.h>
  3. const int MAXL = 135000;
  4. const int BLOCKSIZE = 800;
  5. const int MAXBLOCK = 2*MAXL/BLOCKSIZE*2+100;
  6. int n,m;
  7. int str[MAXL*2];
  8. //node list operation
  9. int freelist[MAXBLOCK];
  10. int freepos;
  11. inline int newnode(){
  12. return freelist[freepos++];
  13. }
  14. inline void delnode(int t){
  15. freelist[--freepos]=t;
  16. }
  17. //block list operation
  18. int data[MAXBLOCK][BLOCKSIZE];
  19. int count[MAXBLOCK];
  20. int next[MAXBLOCK];
  21. void find(int &p,int &b){
  22. for(b=0; b!=-1 && p>count[b]; b=next[b])
  23. p -= count[b];
  24. }
  25. void maintainlist(int b){
  26. for(; b!=-1; b=next[b])
  27. for(int t=next[b]; t!=-1 && count[b] + count[t] <= BLOCKSIZE; t=next[b]){
  28. memcpy(data[b]+count[b], data[t], count[t]*sizeof(int));
  29. count[b]+=count[t];
  30. next[b]=next[t];
  31. delnode(t);
  32. }
  33. }
  34. void blockfill(int b,int str[],int n,int e){
  35. next[b]=e;
  36. count[b]=n;
  37. memcpy(data[b], str, n*sizeof(int));
  38. }
  39. void splite(int b,int p){
  40. if(b==-1 || count[b]==p)return;
  41. int t = newnode();
  42. blockfill(t, data[b]+p, count[b]-p, next[b]);
  43. count[b]= p;
  44. next[b] = t;
  45. }
  46. void insert(int p,int n,int str[]){
  47. int b,i,t;
  48. find(p,b);
  49. splite(b,p);
  50. for(i=0; i+BLOCKSIZE <= n; i+=BLOCKSIZE){
  51. t=newnode();
  52. blockfill(t, str+i, BLOCKSIZE, next[b]);
  53. next[b]=t;
  54. b=t;
  55. }
  56. if(n-i){
  57. t=newnode();
  58. blockfill(t, str+i, n-i, next[b]);
  59. next[b]=t;
  60. }
  61. maintainlist(b);
  62. }
  63. int erase(int p,int n){
  64. int b,e;
  65. find(p,b);
  66. splite(b,p);
  67. for(e=next[b]; e!=-1 && n > count[e]; e=next[e])n -= count[e];
  68. if(n){splite(e,n);e=next[e];}
  69. for(int t=next[b]; t!=e; t=next[b]){
  70. next[b]=next[t];
  71. delnode(t);
  72. }
  73. maintainlist(b);
  74. }
  75. int getcount(){
  76. int sum=0;
  77. for(int b=0; b!=-1; b=next[b])
  78. sum+=count[b];
  79. return sum;
  80. }
  81. int get(){
  82. for(int b=0,i=0; b!=-1; b=next[b]){
  83. memcpy(str+i, data[b], count[b]*sizeof(int));
  84. i+=count[b];
  85. }
  86. }
  87. //uni-set operation
  88. int nextfree[MAXL];
  89. int root(int x){
  90. if(x>m || x == nextfree[x])return x;
  91. return nextfree[x]=root(nextfree[x]);
  92. }
  93. //main
  94. void init(){
  95. freopen("2131.in","r",stdin);
  96. freopen("2131.out","w",stdout);
  97. scanf("%d%d",&n,&m);
  98. for(int i=m; i>=0 ; --i)nextfree[i]=i;
  99. for(int i=1; i<MAXBLOCK; ++i)freelist[i]=i;
  100. freepos=1;
  101. next[0]=-1;
  102. count[0]=0;
  103. memset(str,0,sizeof(str));
  104. insert(0, m, str);
  105. }
  106. void solve(){
  107. int s,t,x,i;
  108. for(s=1; s<=n; ++s){
  109. scanf("%d",&t);
  110. x = root(t);
  111. if(x <= m){
  112. erase(x-1,1);
  113. ++nextfree[x];
  114. }
  115. str[0]=s;
  116. insert(t-1, 1, str);
  117. }
  118. n = getcount();
  119. get();
  120. while(n && str[n-1]==0)--n;
  121. printf("%d\n",n);
  122. printf("%d",str[0]);
  123. for(i=1; i<n; ++i)printf(" %d",str[i]);
  124. printf("\n");
  125. }
  126. int main(){
  127. init();
  128. solve();
  129. return 0;
  130. }