提交时间:2020-11-16 16:40:37

运行 ID: 1872

#include<bits/stdc++.h> #define int long long using namespace std; int n,x; int val; int ans; int sum; struct node{ int w; int col; int pos; }; node sta[1000005]; int top; void Delete(){ if(top==1){ top=0; return ; } if(sta[top].w==sta[top-1].w) sum-=sta[top-1].pos-sta[top-2].pos; top--; } signed main(){ cin>>n>>x; for(int i=1;i<=n;i++){ scanf("%lld",&val); while(top && sta[top].w<val) Delete(); while(sta[top].w==val && sta[top].col==1 && top) Delete(); sta[++top]=(node){val,1,i}; if(sta[top].w==sta[top-1].w) sum+=sta[top-1].pos-sta[top-2].pos; val=x-val; while(top && sta[top].w<val) Delete(); while(sta[top].w==val && sta[top].col==2 && top) Delete(); sta[++top]=(node){val,2,i}; if(sta[top].w==sta[top-1].w) sum+=sta[top-1].pos-sta[top-2].pos; ans+=sum; } cout<<ans<<endl; return 0; }