#define _USE_MATH_DEFINES #include using namespace std; //template #define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; templateinline bool chmax(T& a,T b){if(ainline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} //end typedef pair P; typedef pair T; const int m=101010; int n,k,a[m]; ll res=0; void rec(int lb,int rb){ if(rb-lb<=2){ if(rb-lb==1 and a[lb]*2==k)res++; else if(rb-lb==2){ if(a[lb]+a[lb+1]==k and a[lb]!=a[lb+1])res+=2; if(a[lb]*2==k)res++; if(a[lb+1]*2==k)res++; } return; } int mid=(lb+rb)/2; rec(lb,mid); rec(mid,rb); unordered_map> num; unordered_map> mis,rui,is_one; int mi=inf,cnt=1; for(int i=mid;i v=p.second; sort(ALL(v)); mis[val].push_back(-inf); rui[val].push_back(0); is_one[val].push_back(0); for(T t:v){ mis[val].push_back(t.first); rui[val].push_back(t.second.first); if(t.second.second==1)is_one[val].push_back(t.second.first); else is_one[val].push_back(0); } mis[val].push_back(inf); rep(i,0,rui[val].size()-1){ rui[val][i+1]+=rui[val][i]; is_one[val][i+1]+=is_one[val][i]; } } mi=inf; cnt=1; for(int i=mid-1;i>=lb;i--){ if(mi==a[i])cnt++; if(chmin(mi,a[i]))cnt=1; if(mis[k-a[i]].empty())continue; int cen=upper_bound(ALL(mis[k-a[i]]),mi)-mis[k-a[i]].begin(); if(cnt==1 and mis[k-a[i]][cen]!=inf){ res+=(*rui[k-a[i]].rbegin()-rui[k-a[i]][cen-1])-i*(rui[k-a[i]].size()-cen-1); } cen--; if(mis[k-a[i]][cen]==mi)cen--; if(cen>=1)res+=is_one[k-a[i]][cen]-i*(cen+1); /* for(auto p:num[k-a[i]]){ int opp=p.first,idx=p.second.first,add=p.second.second; if(oppmi and cnt==1)res+=idx-i+1; } */ } } int main(){ cin>>n>>k; rep(i,0,n)cin>>a[i]; rec(0,n); cout<