提交时间:2020-11-16 21:32:20

运行 ID: 1914

#include <bits/stdc++.h> using namespace std; #define int long long char buf[1 << 21], *p1 = buf, *p2 = buf; inline char getc(){ return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++; } inline void read(int &x){ x = 0; char s = getc(); while(s < '0' || s > '9') s = getc(); while('0' <= s && s <= '9'){ x = (x << 3) + (x << 1) + (s ^ 48); s = getc(); } } void print(const int &x){ if(x > 9) print(x / 10); putchar(x % 10 + '0'); } const int N = 1000010; int n, x, a, sum, ans; struct node{ int w, op, pos; }q[N]; int l; inline void pop(){ if(q[l].w == q[l - 1].w && q[l].op != q[l - 1].op) sum -= q[l - 1].pos - q[l - 2].pos; --l; } inline void push(int w, int op, int pos){ while(l && (q[l].w < w || (q[l].w == w && q[l].op == op))) pop(); if(q[l].w == w && q[l].op != op) sum += q[l].pos - q[l - 1].pos; q[++l] = (node){w, op, pos}; } signed main() { read(n); read(x); for(int i = 1;i <= n;i++){ read(a); push(a, 1, i); push(x - a, 2, i); ans += sum; } print(ans); return 0; }