Problem:
Score : 600 points

Problem Statement

You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K.

a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K?

Constraints

All input values are integers.

1 ≤ N ≤ 2 \times 10^5

1 ≤ K ≤ 10^9

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N K
a_1
a_2
:
a_N

Output

Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K.

Sample Input 1

3 6
7
5
7

Sample Output 1

5

All the non-empty contiguous subsequences of a are listed below:

{a_1} = {7}

{a_1, a_2} = {7, 5}

{a_1, a_2, a_3} = {7, 5, 7}

{a_2} = {5}

{a_2, a_3} = {5, 7}

{a_3} = {7}

Their means are 7, 6, 19/3, 5, 6 and 7, respectively, and five among them are 6 or greater. Note that {a_1} and {a_3} are indistinguishable by the values of their elements, but we count them individually.

Sample Input 2

1 2
1

Sample Output 2

0

Sample Input 3

7 26
10
20
30
40
30
20
10

Sample Output 3

13

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <list>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <iterator> 
#include <limits>

#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a); i<(b); i++)
#define ALL(a) (a).begin(),(a).end()
#define RALL(A) (a).rbegin(),(a).rend()
#define PB push_back
#define MP make_pair

#define dump(x) cerr << #x << " = " << (x) << endl;

using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

int N, K;
VI A;

template <typename T>
map<T, int> zaatu (vector<T> arr) {
    map<T, int> ret;
    vector<T> arr_copy;
    copy(arr.begin(), arr.end(), back_inserter(arr_copy));
    sort(arr_copy.begin(), arr_copy.end());
    int index = 0;
    for (auto e : arr_copy) {
        if (e == arr_copy[0]) {
            ret[e] = index;
        } else {
            if (e != arr_copy[index]) {
                index += 1;
                ret[e] = index;
            }
        }
    }
    return ret;
}

template <typename T>
struct BIT {
    int N;
    vector<T> bit;

    BIT (int N) {
        this -> N = N;
        for (int i = 0; i <= N; i++) {
            bit.push_back(0);
        }
    }

    /*
     * a_1 + a_2 + ... + a_n
     * O(log n)
     */
    T sum (int n) {
        T ret = 0;
        while (n > 0) {
            ret += bit[n];
            // nの最後の1のビットに対応する数を引く
            // ex) 0110 -> 0100 (= 0110 - 0010)
            n -= n & -n;
        }
        return ret;
    }

    /*
     * a_n += x
     * O(log n)
     */
    void add (int n, T x) {
        while (n <= N) {
            bit[n] += x;
            // nの最後の1のビットに対応する数を足す
            // ex) 0101 -> 0110 (= 0101 + 0001)
            n += n & -n;
        }
    }
};

int main(int argc, char const* argv[])
{
    cin >> N >> K;
    REP(i, N) {
        int a; cin >> a; A.PB(a);
    }
    VI sums;
    sums.PB(0);
    int now = 0;
    REP(i, N) {
        now += A[i];
        sums.PB(now - (i + 1) * K);
    }
    /*
    cout << "dbg" << endl;
    for (auto e : sums) {
        cout << e << " ";
    }
    cout << endl;
    */

    map<int, int> za = zaatu(sums);
    BIT<int> bit(N + 1);
    LL ans = 0;
    REP(i, N + 1) {
        // cout << i << " " << za[sums[i]] << endl;
        ans += i - bit.sum(za[sums[i]] + 1);
        bit.add(za[sums[i]] + 1, 1);
    }
    cout << ((LL)(N + 1) * N) / 2 - ans << endl;
    return 0;
}