Problem p02904 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  std::vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }

  std::set<int> range_value;
  std::unordered_multiset<int> pos;
  for (int i = 0; i < k; i++) {
    range_value.insert(p[i]);
  }
  int result = 1;
  pos.insert(k - 1);
  for (int i = k; i < n; i++) {
    int curr_min = *range_value.begin();
    int curr_max = *(--range_value.end());
    int next_value = p[i];
    range_value.insert(next_value);
    range_value.erase(p[i - k]);
    if (curr_min < *range_value.begin() && curr_max < *(--range_value.end())) {
      // nothing
    } else {
      pos.insert(i);
      result++;
    }
  }

  int increasing_pos_count = 0;
  int max = -1;
  int increasing_length = 0;
  for (int i = 0; i < n; i++) {
    if (max < p[i]) {
      increasing_length++;
      max = p[i];
      if (pos.find(i) != pos.end() && increasing_length >= k) {
        increasing_pos_count++;
      }
    } else {
      increasing_length = 1;
      max = p[i];
    }
  }

  result -= (increasing_pos_count * (increasing_pos_count - 1)) / 2;

  assert(result >= 1);

  cout << result << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  std::vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }

  std::set<int> range_value;
  std::unordered_multiset<int> pos;
  for (int i = 0; i < k; i++) {
    range_value.insert(p[i]);
  }
  int result = 1;
  pos.insert(k - 1);
  for (int i = k; i < n; i++) {
    int curr_min = *range_value.begin();
    int curr_max = *(--range_value.end());
    int next_value = p[i];
    range_value.insert(next_value);
    range_value.erase(p[i - k]);
    if (curr_min < *range_value.begin() && curr_max < *(--range_value.end())) {
      // nothing
    } else {
      pos.insert(i);
      result++;
    }
  }

  int increasing_pos_count = 0;
  int max = -1;
  int increasing_length = 0;
  for (int i = 0; i < n; i++) {
    if (max < p[i]) {
      increasing_length++;
      max = p[i];
      if (pos.find(i) != pos.end() && increasing_length >= k) {
        increasing_pos_count++;
      }
    } else {
      increasing_length = 1;
      max = p[i];
    }
  }

  if (increasing_pos_count >= 2) {
    result -= (increasing_pos_count);
    result++;
  }

  assert(result >= 1);

  cout << result << endl;

  return 0;
}