allocate mem(2^28) ord = [] \\ order of appearance count = [] \\ cumulative count emitted = [] \\ recently emitted n = 0 emit(v) = { n++; \\ print (n " " v); if (#emitted < v+1, emitted = concat(emitted, vector(v+1-#emitted + 1000)); count = concat(count, vector(v+1-#count + 1000)); ); if (count[v+1]==0 && emitted[v+1]==0, \\ new value ord = concat(ord,v); ); emitted[v+1]++; } { \\ initial segment emit(0); print ("1 " #ord); for (segment=2, 10 000, old count = #ord; count += emitted; emitted = vector(#emitted); for (o=1, #ord, emit(ord[o]); emit(count[ord[o]+1]); ); print (segment " " #ord - old count); ); } quit