intquery(){ int l = 0, r = n + 1, mid, sum = 0, ans = 0; // (l, r) 左开右开, Log[i] 表示小于 i 的 2 的最大次幂 while (l + 1 < r) { mid = l + Log[r - l]; if (sum + Bit[mid] < mid) r = mid; else sum += Bit[mid], ans = l = mid; } return ans; }
structFenwick { ll B1[maxn], B2[maxn], B3[maxn]; int n; voidinit(int _n){ n = _n; } voidadd(int x, ll v){ for (int i = x; i <= n; i += lowbit(i)) B1[i] += v, B2[i] += v * x, B3[i] += v * x * x; }
ll get_sum(int x){ ll s = 0; for (int i = x; i; i -= lowbit(i)) s += B1[i] * (x + 1) * (x + 2) - B2[i] * (2 * x + 3) + B3[i]; return s / 2; }
voidupdate(int l, int r, int v){ add(l, v); add(r + 1, -v); }