Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define f first
- #define s second
- #define pb push_back
- #define mp make_pair
- #define ll long long
- #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- using namespace std;
- const int INF = 1e9 + 1;
- const int MOD = 1e9 + 7;
- const int MOD2 = 1e9 + 9;
- const int MAXN = 1e5 + 10;
- int cnt[MAXN];
- void solve(){
- int n;
- ll k;
- cin >> n >> k;
- long long a[n + 1];
- for (int i = 1; i <= n; i++){
- cin >> a[i];
- }
- int right = 0;
- multiset <long long> st;
- long long dif = 0;
- ll ans = 0;
- for (int left = 1; left <= n; left++){
- while(right < n && dif <= k){
- right++;
- st.insert(a[right]);
- dif = *st.rbegin() - *st.begin();
- }
- if (dif > k){
- ans += right - left;
- }
- else{
- ans += right - left + 1;
- }
- st.erase(st.find(a[left]));
- if (st.empty()){
- dif = 0;
- }
- else{
- dif = *st.rbegin() - *st.begin();
- }
- }
- cout << ans;
- }
- int main() {
- IOS;
- int t = 1;
- //cin >> t;
- while(t--){
- solve();
- }
- return 0;
- }
Advertisement