#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair P; int n, k; int a[100010]; ll ans; const int INF=1e9+7; const ll INFL=1e18; void solve(int l, int r){ if(r-l<=2000){ for(int i=l; i<=r; i++){ int mn=INF, cnt=0; for(int j=i; j<=r; j++){ if(mn>a[j]){ mn=a[j]; cnt=1; }else if(mn==a[j]){ cnt++; } if(a[i]+a[j]==k && cnt==1){ ans+=(j-i+1); } } } return; } vector vc; for(int i=l; i<=r; i++) vc.push_back(a[i]); sort(vc.begin(), vc.end()); vc.erase(unique(vc.begin(), vc.end()), vc.end()); int m=(l+r)/2; int mn=INF, cnt=0; vector> mp(vc.size()); for(int i=m; i>=l; i--){ if(mn>a[i]){ mn=a[i]; } mp[(int)(lower_bound(vc.begin(), vc.end(), a[i])-vc.begin())].push_back({mn, m+1-i}); } for(auto &p:mp){ sort(p.begin(), p.end()); for(int i=1; ia[i]){ mn=a[i], cnt=1; }else if(mn==a[i]){ cnt++; } if(cnt==1){ int u=lower_bound(vc.begin(), vc.end(), k-a[i])-vc.begin(); if(u==vc.size() || vc[u]!=k-a[i]) continue; auto v=mp[u]; if(v.empty()) continue; int t=upper_bound(v.begin(), v.end(), P(mn, INFL))-v.begin(); ans+=((ll)v.size()-t)*(i-m); if(t>0) ans+=v.back().second-v[t-1].second; else ans+=v.back().second; } } for(auto &p:mp) p.clear(); mn=INF, cnt=0; for(int i=m+1; i<=r; i++){ if(mn>a[i]){ mn=a[i]; } mp[(int)(lower_bound(vc.begin(), vc.end(), a[i])-vc.begin())].push_back({mn, i-m}); } for(auto &p:mp){ sort(p.begin(), p.end()); for(int i=1; i=l; i--){ if(mn>a[i]){ mn=a[i], cnt=1; }else if(mn==a[i]){ cnt++; } if(cnt==1){ int u=lower_bound(vc.begin(), vc.end(), k-a[i])-vc.begin(); if(u==vc.size() || vc[u]!=k-a[i]) continue; auto v=mp[u]; if(v.empty()) continue; int t=upper_bound(v.begin(), v.end(), P(mn, INFL))-v.begin(); ans+=((ll)v.size()-t)*(m+1-i); if(t>0) ans+=v.back().second-v[t-1].second; else ans+=v.back().second; } } solve(l, m); solve(m+1, r); } int main() { cin>>n>>k; for(int i=0; i>a[i]; } solve(0, n-1); cout<