题意:http://acm.hdu.edu.cn/showproblem.php?pid=5400
思路:预处理出每个点向左和向右的最远边界,从左向右枚举中间点,把区间答案加到总答案里面。由与可能与前面的区间重叠,需要减去重复的答案,由于左边界非降,所以重叠的区间长度很容易得到。
#pragma comment(linker, "/STACK:10240000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; //#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);} const double PI = acos(-1.0);
const int INF = 1e9 + ;
const double EPS = 1e-12; /* -------------------------------------------------------------------------------- */ const int maxn = 1e5 + ; int a[maxn], R[maxn], L[maxn]; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int n, d1, d2;
while (cin >> n >> d1 >> d2) {
for (int i = ; i <= n; i ++) {
scanf("%d", a + i);
}
for (int i = n; i >= ; i --) {
a[i] = a[i] - a[i - ];
}
a[n + ] = INF * ;
R[n + ] = INF;
for (int i = ; i <= n; i ++) {
L[i] = max(, a[i] == d1? L[i - ] : i);
}
for (int i = n; i; i --) {
R[i] = min(n, a[i + ] == d2? R[i + ] : i);
}
int lastl = , lastr =;
ll ans = ;
for (int i = ; i <= n; i ++) {
ll c = R[i] - L[i] + ;
ans += c * (c + ) / ;
ll cc = min(lastr, R[i]) - L[i] + ;
if (cc > ) ans -= cc * (cc + ) / ;
lastl = L[i];
umax(lastr, R[i]);
}
cout << ans << endl;
}
return ;
}