#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; const long double PI = acos((long double)(-1)); using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; ll n, k; vector a; ll calc(vector &v1, vector &v2) { ll idx = 0; //v2のidx ll mn = INF; //最小値 ll mn_cnt = 0; //minの最小値(1以外はout) ll res = 0; map> mp; //値、個数、総和 for (ll i = 0; i < v1.size(); i++) { if (chmin(mn, v1[i])) mn_cnt = 1; else if (mn == v1[i]) mn_cnt++; if (mn_cnt > 1) continue; while (idx < v2.size() and v2[idx] > mn) { mp[v2[idx]].first++; mp[v2[idx]].second += v1.size() + idx + 1; idx++; } if (mp.find(k - v1[i]) != mp.end()) res += mp[k - v1[i]].second - (v1.size() - i - 1) * mp[k - v1[i]].first; } return res; } ll f(int left, int right) { if (right - left <= 0) return 0; if (right - left == 1) return a[left] * 2 == k; ll mid = (left + right) >> 1; ll res = f(left, mid) + f(mid, right); vector v1, v2; for (int i = left; i < mid; i++) v1.emplace_back(a[i]); for (int i = mid; i < right; i++) v2.emplace_back(a[i]); reverse(v1.begin(), v1.end()); res += calc(v1, v2); //v1側に最小値がある場合 res += calc(v2, v1); //v2側に最小値がある場合 return res; } int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ scanf("%lld %lld", &n, &k); a.resize(n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]); cout << f(0, n) << endl; return 0; }