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 LuaJIT (2.0.4) (judge verdict: Accepted)

local n, k = io.read("*n","*n")
local t = {}
for i = 1, n do
  t[i] = 1 + io.read("*n")
end

local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, set, silent)
  if set then
    self.stage[self.stagenum][idx] = idx
  else
    self.stage[self.stagenum][idx] = self.emptyvalue
  end
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local mma, mmi = math.max, math.min
local stmin = SegTree.new(n, mmi, n + 1)
local stmax = SegTree.new(n, mma, 0)
for i = 1, k do
  stmin:setValue(t[i], true, true)
  stmax:setValue(t[i], true, true)
end
stmin:updateAll()
stmax:updateAll()

local sortedCnt = 0
local cnt = 1
local curlen = 1
for i = 1, k - 1 do
  if t[i] < t[i + 1] then
    curlen = curlen + 1
  else
    curlen = 1
  end
end
if k <= curlen then
  sortedCnt = sortedCnt + 1
end

for i = k + 1, n do
  local sorted = false
  if t[i - 1] < t[i] then
    curlen = curlen + 1
  else
    curlen = 1
  end
  if k <= curlen then
    sorted = true
  end

  local prv, nxt = t[i - k], t[i]
  local curmin, curmax = stmin.stage[1][1], stmax.stage[1][1]
  if curmin == prv and curmax < nxt then
  else
    if sorted then
      sortedCnt = sortedCnt + 1
    end
    cnt = cnt + 1
  end
  stmin:setValue(t[i - k], false)
  stmin:setValue(t[i], true)
  stmax:setValue(t[i - k], false)
  stmax:setValue(t[i], true)
end
if 1 < sortedCnt then
  cnt = cnt - sortedCnt + 1
end
print(cnt)