long long kth_smallest(long long k){ long long lo = 0, hi = tree.size()-1; long long mid = -1; while(lo < hi){ mid = (lo + hi)/2; if(k <= tree.get_sum(mid)) hi = mid; else lo = mid + 1; } //cerr << "sum up to " << lo << " is " << tree.get_sum(lo) << endl; if(tree.get_sum(lo) < k) return -1; return lo; }
long long Rank(long long n){ return tree.get_sum(n-1); }