提交时间:2020-11-16 15:01:18

运行 ID: 1851

#include <iostream> #include <cstdio> #define int long long #define fi first #define se second using namespace std; const int MAX=1e6+5; struct node{ int value,pos,color; //0 ai 1 x-ai }sta[MAX<<1]; int tp; int a[MAX]; int n,x,sum,ans; signed main(){ scanf("%lld %lld",&n,&x); sta[0].color=-1; for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); while(tp && sta[tp].value<a[i]){ if(tp>1 && sta[tp].value==sta[tp-1].value && sta[tp].color!=sta[tp-1].color)sum-=(sta[tp-1].pos-sta[tp-2].pos); tp--; } sta[++tp]=node{a[i],i,0}; if(tp>1 && sta[tp].value==sta[tp-1].value && sta[tp].color!=sta[tp-1].color)sum+=(sta[tp-1].pos-sta[tp-2].pos); while(tp && sta[tp].value<x-a[i]){ if(tp>1 && sta[tp].value==sta[tp-1].value && sta[tp].color!=sta[tp-1].color)sum-=(sta[tp-1].pos-sta[tp-2].pos); tp--; } sta[++tp]=node{x-a[i],i,1}; if(tp>1 && sta[tp].value==sta[tp-1].value && sta[tp].color!=sta[tp-1].color)sum+=(sta[tp-1].pos-sta[tp-2].pos); ans+=sum; } printf("%lld",ans); return 0; }