### 代码示例 ```cpp #include #include #include #include #define ll long long using namespace std;
struct poi { ll sum, pos; } a[200010]; ll n, k, ans, cnt, tree[200010], lisan[200010];
void read(ll &k) { k = 0; int f = 1; char c = getchar(); while (c <'0' || c > '9') if (c == '-') f = -1, c = getchar(); while (c <= '9' && c >= '0') k = k * 10 + c - '0', c = getchar(); k *= f; }
bool cmp(poi a, poi b) { return a.sum int lowbit(int x) { return x & -x; }
void add(int x, int delta) { for (int i = x; i <= cnt; i += lowbit(i)) tree[i] += delta; }
int sum(int x) { int s = 0; for (int i = x; i >= 1; i -= lowbit(i)) s += tree[i]; return s; }
int main() { read(n); read(k); for (int i = 1; i <= n; i++) read(a[i].sum), a[i].sum -= k; for (int i = 1; i <= n; i++) a[i].sum += a[i-1].sum, a[i].pos = i; sort(a, a + 1 + n, cmp); for (int i = 0; i <= n; i++) { if (a[i].sum != a[i-1].sum || i == 0) cnt++; lisan[a[i].pos] = cnt; } for (int i = n; i >= 0; i--) { ans += sum(cnt) - sum(lisan[i] - 1); add(lisan[i], 1); } printf("%lld\n", ans); return 0; } ``` ### 参考链接 - [AtCoder Regular Contest 075 E - Meaningful Mean(树状数组)](http://www.mamicode.com/info-detail-1850626.html)