int tree[ 100001 ]; // Tree array, used to take the sum of the data in the interval [x, y] /* & Special operation, the value (decimal) of t&(-t) is the position where the first 1 appears from right to left in t in binary. Combined with the special properties of tree-like arrays, this value is useful */ int lowbit(int t) { return t&(-t); } /* Suppose that the data at the array number x has been changed, so that the data at the x position has an increment v Modify the tree array as follows, so that the relevant sums containing x-bit data are increased by v According to the nature of the tree array, that is, add v to the data with subscripts x, x+lowbit(x), x+lowbit(x+lowbit(x)).... */ void add(int x,int v) { for(int i=x; i<=100000; i+=lowbit(i)) tree[i]+=v; } /* Take the sum of the data in the interval [0,x] */ int getsum(int x) { int ans=0; for(int i=x;i>0;i-=lowbit(i)) ans += tree[i]; return years; } /* Binary search to further reduce the time complexity */ int binarySearch(int l, int r, int median) { int mid ; while (l<=r) { mid = (l+r)/2; if (getsum(mid)<median) l = mid+1; //注意此处是 >= else if (getsum(mid)>=median) r = mid-1; else return mid; } return l; }