//#include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstring> #include<map> #include<unordered_map> #include<deque> usingnamespace std; #define ls rt<<1 #define rs rt<<1|1 #define LL long long #define ll long long #define MAXN 200000 #define mod 1000000007 #define MS 100009
LL n,m,k; deque<LL > mx,mn; LL a[MS];
intmain(){ ios::sync_with_stdio(false); cin >> n >> m; for (int i=1;i<=n;i++){ cin >> a[i]; } while (m--){ cin >> k; LL ans = (n+1)*n/2; int l = 1; while (!mx.empty()) mx.pop_back(); while (!mn.empty()) mn.pop_back(); for (int i=1;i<=n;i++){ while (!mx.empty() && a[ mx.back() ] < a[i]) mx.pop_back(); while (!mn.empty() && a[ mn.back() ] > a[i]) mn.pop_back(); mx.push_back(i); mn.push_back(i); while (a[mx.front()] - a[mn.front()] > k){ l++; while (mx.front() < l) mx.pop_front(); while (mn.front() < l) mn.pop_front(); } ans -= (i-l+1); } cout << ans << "\n";