#include // clang-format off using Int = long long; #define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++) #define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io; #ifndef dump #define dump(...) #endif // clang-format on /** * author: knshnb * created: Sat Jun 13 17:03:51 JST 2020 **/ signed main() { Int n, K; std::cin >> n >> K; std::vector a(n); REP(i, n) std::cin >> a[i]; auto daq = [&](auto self, Int L, Int R) -> Int { if (R - L == 1) return 2 * a[L] == K; Int ret = self(self, L, (L + R) / 2) + self(self, (L + R) / 2, R); REP(t, 2) { Int M = (L + R + t) / 2; Int j = M; Int mi = 1e18; std::unordered_map> mp; // {cnt, sum of index} for (Int i = M - 1; i >= L; i--) { if (a[i] == mi) break; mi = std::min(mi, a[i]); while (j < R && a[j] > mi) { mp[a[j]].first++; mp[a[j]].second += j - M + 1; j++; } ret += mp[K - a[i]].first * (M - i) + mp[K - a[i]].second; } std::reverse(a.begin() + L, a.begin() + R); } return ret; }; std::cout << daq(daq, 0, n) << std::endl; }