提交时间:2020-11-17 08:40:11
运行 ID: 1929
#include <bits/stdc++.h> #define int long long using namespace std; int n,x,a[1000010],b[1000010],ans,t,sum; struct node{ int c,pos,val; }s[1000010]; void del(){ if(t==1){ t=0; return ; } if(s[t].val==s[t-1].val){ sum-=s[t-1].pos-s[t-2].pos; } t--; } signed main(){ cin>>n>>x; //memset(minn,0x3f,sizeof(minn)); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); b[i]=x-a[i]; } for(int i=1;i<=n;i++){ while(t && s[t].val<a[i]) del(); while(s[t].val==a[i] && t && s[t].c==1) del(); s[++t]=(node{1,i,a[i]}); if(s[t].val==s[t-1].val) sum+=s[t-1].pos-s[t-2].pos; while(t && s[t].val<b[i]) del(); while(s[t].val==b[i] && t && s[t].c==2) del(); s[++t]=(node{2,i,b[i]}); if(s[t].val==s[t-1].val) sum+=s[t-1].pos-s[t-2].pos; ans+=sum; } cout<<ans; return 0; }