Problem p02904 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF 1145141919810364364
#define int long long
#define all(vec) vec.begin(), vec.end()
#define P pair<int, int>
#define S second
#define F first

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int n, k, p[100010];
int ans = 1;
set<int> st;
int cnt = 1;
bool bl = false;

signed main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < k; i++) {
    st.insert(p[i]);
    if (i) {
      if (p[i - 1] < p[i])
        cnt++;
      else
        cnt = 1;
    }
  }
  if (cnt == k)
    bl = true;
  for (int i = k; i < n; i++) {
    if (p[i - 1] < p[i])
      cnt++;
    else
      cnt = 1;
    if (*st.begin() != p[i - k] || *st.rbegin() > p[i]) {
      if (!bl || cnt < k)
        ans++;
    }
    st.erase(p[i - k]);
    st.insert(p[i]);
    if (cnt >= k)
      bl = true;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF 1145141919810364364
#define int long long
#define all(vec) vec.begin(), vec.end()
#define P pair<int, int>
#define S second
#define F first

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int n, k, p[200010];
int ans = 1;
set<int> st;
int cnt = 1;
bool bl = false;

signed main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < k; i++) {
    st.insert(p[i]);
    if (i) {
      if (p[i - 1] < p[i])
        cnt++;
      else
        cnt = 1;
    }
  }
  if (cnt == k)
    bl = true;
  for (int i = k; i < n; i++) {
    if (p[i - 1] < p[i])
      cnt++;
    else
      cnt = 1;
    if (*st.begin() != p[i - k] || *st.rbegin() > p[i]) {
      if (!bl || cnt < k)
        ans++;
    }
    st.erase(p[i - k]);
    st.insert(p[i]);
    if (cnt >= k)
      bl = true;
  }
  cout << ans << endl;
}