Problem:
Score : 700 points

Problem Statement

Snuke has a permutation (P_0,P_1,\cdots,P_{N-1}) of (0,1,\cdots,N-1).

Now, he will perform the following operation exactly once:

Choose K consecutive elements in P and sort them in ascending order.

Find the number of permutations that can be produced as P after the operation.

Constraints

2 \leq N \leq 200000

2 \leq K \leq N

0 \leq P_i \leq N-1

P_0,P_1,\cdots,P_{N-1} are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_0 P_1 \cdots P_{N-1}

Output

Print the number of permutations that can be produced as P after the operation.

Sample Input 1

5 3
0 2 1 4 3

Sample Output 1

2

Two permutations can be produced as P after the operation: (0,1,2,4,3) and (0,2,1,3,4).

Sample Input 2

4 4
0 1 2 3

Sample Output 2

1

Sample Input 3

10 4
2 0 1 3 7 5 4 6 8 9

Sample Output 3

6

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#define STOPIT
#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
#define lint long long
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
  std::cerr << " " << head;
  debug_impl(tail...);
}
#ifndef STOPIT
#define debug(...)\
  std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
  debug_impl(__VA_ARGS__);\
  std::cerr << std::noboolalpha;
#else
#define debug 0;
#endif

template < typename Value, typename Cmp = std::less< Value > >
class sliding_minimum {
    std::deque< Value > que;

    void pop_left_until(int target) {
      assert(target <= right);
      while (left < target)
        { shrink(); }
    }
    void push_right_until(int target) {
      assert(target <= n);
      while (right < target)
        { extend(); }
    }

  public:
    int                  n;
    std::vector< Value > v;
    Cmp             cmp;
    int                 left, right;

    sliding_minimum(std::vector< Value > const& v) :
      n    (v.size()),
      v    (v),
      cmp  (Cmp()),
      left (0),
      right(0)
      {}

    auto get_left_index()  const { return left; }
    auto get_right_index() const { return right; }

    auto query_index() const
      { assert(!que.empty()); return que.front(); }
    auto query() const
      { return v.at(query_index()); }

    void shrink() {
      if (que.front() == left++)
        { que.pop_front(); }
      assert(left <= right);
    }
    void extend() {
      while (!que.empty() && !cmp(v.at(que.back()), v.at(right)))
        { que.pop_back(); }
      que.push_back(right++);
      assert(right <= n);
    }
    void slide()
      { shrink(); extend(); }
    void move(int l, int r) {
      push_right_until(r);
      pop_left_until(l);
    }
};

template < typename Container,
  typename Value = typename Container::value_type,
  std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr
>
std::istream& operator>> (std::istream& is, Container& v) {
  for (auto & x : v) is >> x;
  return is;
}

template < typename Container,
  typename Value = typename Container::value_type,
  std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr
>
std::ostream& operator<< (std::ostream& os, Container const& v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int n, k; std::cin >> n >> k;
  std::vector< int > a(n); std::cin >> a;
  if (n == k) {
    std::cout << 1 << std::endl;
    return 0;
  }

  std::vector< int > c(n, 0);
  rep(i, 1, n)
    { c.at(i) = c.at(i - 1) + (a.at(i - 1) > a.at(i)); }
  auto flat = [&] (auto x, auto y)
    { return c.at(x) == c.at(y); };
  debug(c);

  auto slmin = sliding_minimum< int >(a);
  auto slmax = sliding_minimum< int, std::greater< int > >(a);
  slmin.move(0, k + 1);
  slmax.move(0, k + 1);
  auto ret  = (int)!flat(0, k - 1);
  auto flag = false;
  rep(i, 0, n - k) {
    auto j = i + k;
    debug(i, j);
    auto same =
         i == slmin.query_index()
      && j == slmax.query_index();
    auto flat0 = flat(i, j - 1);
    auto flat1 = flat(i + 1, j);
    flag |= flat0;
    flag |= flat1;
    debug(same, flat1);
    if (!same && !flat1)
      { ret++; }
    if (j == n - 1)
      { break; }
    slmin.slide();
    slmax.slide();
  }
  ret += flag;
  std::cout << ret << std::endl;
  return 0;
}