import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.NoSuchElementException; class BitVector{ long[] small; int[] big; int n; public BitVector(int n) { this.n=n; small=new long[n/64+1]; big=new int[n/64+1]; } void set(int k) { small[k/64]|=1L<<(k%64); } void build() { for(int i=0;i0?Long.bitCount(small[i-1]):0)+(i>0?big[i-1]:0); } } int access(int k) { return (int) ((small[k/64]>>>(k%64))&1); } //[l,r) int rank(int l,int r,long m) { if(l>=r)return 0; int ret=bitCount(r)-bitCount(l); return m==1?ret:(r-l-ret); } //[0,n) int bitCount(int n) { n=Math.min(n, this.n); if(n<=0)return 0; --n; return big[n/64]+Long.bitCount((small[n/64]<<(63-n%64))>>>(63-n%64)); } } class WaveletMatrix{ int w=64; BitVector[] b=new BitVector[w];//b[i]:=&2^{i+1}で安定ソートしたときの第&2^{i} int[] nz=new int[w]; int n; final long base=Long.MAX_VALUE/2; public WaveletMatrix(long[] a) { n=a.length; for(int i=0;i comp=new Comparator() { @Override public int compare(long[] o1, long[] o2) { return Long.compare((o1[0]>>>o1[1])&1, (o2[0]>>>o2[1])&1); } }; for(int bit=w-1;bit>=0;--bit) { for(int i=0;i>>bit)&1)==1) { b[bit].set(i); } } for(int i=0;i=0;--bit) { int v=b[bit].access(pos); ret|=(1L<r-l)throw new AssertionError(); long ret=0; for(int bit=w-1;bit>=0;--bit) { int nl=-1,nr=-1; if(b[bit].rank(l, r, 0)>=k+1) { nl=b[bit].rank(0, l, 0); nr=nl+b[bit].rank(l, r, 0); }else { nl=nz[bit]+b[bit].rank(0, l, 1); nr=nl+b[bit].rank(l, r, 1); k-=b[bit].rank(l, r, 0); ret|=1L<0?right[i-1]:0); for(int i=0;i0?left[i-1]:0); for(int i=1;i Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} }