Problem p02904 (python)

Fixed submission:
import math
import numpy as np
import decimal
import collections
import itertools
import sys
#Union-Find
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
def unite(x, y):
    p = find(x)
    q = find(y)
    if p == q:
        return None
    if p > q:
        p,q = q,p
    par[p] += par[q]
    par[q] = p
def same(x, y):
    return find(x) == find(y)
def size(x):
    return -par[find(x)]
n = 0
par = [-1 for i in range(n)]

#素数関連
def prime_numbers(x):
    if x < 2:
        return []
    prime_numbers = [i for i in range(x)]
    prime_numbers[1] = 0
    for prime_number in prime_numbers:
        if prime_number > math.sqrt(x):
            break
        if prime_number == 0:
            continue
        for composite_number in range(2 * prime_number, x, prime_number):
            prime_numbers[composite_number] = 0
    return [prime_number for prime_number in prime_numbers if prime_number != 0]
def is_prime(x):
    if x < 2:
        return False
    if x == 2 or x == 3 or x == 5:
        return True
    if x % 2 == 0 or x % 3 == 0 or x % 5 == 0:
        return False
    prime_number = 7
    difference = 4
    while prime_number <= math.sqrt(x):
        if x % prime_number == 0:
            return False
        prime_number += difference
        difference = 6 - difference
    return True
#nCr
mod = 10 ** 9 + 7
def comb(n, r):
    if n < r:return 0
    if n < 0 or r < 0:return 0
    return fa[n] * fi[r] % mod * fi[n - r] % mod
fa = [1] * (n + 1)
fi = [1] * (n + 1)
for i in range(1, n + 1):
    fa[i] = fa[i - 1] * i % mod
    fi[i] = pow(fa[i], mod - 2, mod)
#BIT
BIT = [0] * (n + 1)
def add(i, x):
    while i <= n:
        BIT[i] += x
        i += i & -i
def query(i):
    s = 0
    while i > 0:
        s += BIT[i]
        i -= i & -i
from collections import deque
n, k = map(int, input().split())
p = list(map(int, input().split()))
cnt = 0
li = [1 for i in range(n - k + 1)]
for i in range(1, k):
    if p[i - 1] > p[i]:
        cnt += 1
if cnt == 0:
    li[0] = 0
for i in range(k, n):
    if p[i - 1] > p[i]:
        cnt += 1
    if p[i - k] > p[i - k + 1]:
        cnt -= 1
    if cnt == 0:
        li[i - k + 1] = 0
Q1 = deque([])
Q2 = deque([])
L1 = [0 for i in range(n - k + 2)]
L2 = [0 for i in range(n - k + 2)]
for i in range(n):
    while Q1 and p[Q1[-1]] > p[i]:
        Q1.pop()
    while Q2 and p[Q2[-1]] < p[i]:
        Q2.pop()
    Q1.append(i)
    Q2.append(i)
    if Q1[0] == i - k + 1:
        Q1.popleft()
    if Q2[0] == i - k + 1:
        Q2.popleft()
    if i >= k - 2:
        L1[i - k + 2] = p[Q1[0]]
        L2[i - k + 2] = p[Q2[0]]
ans = 0
for i in range(1, n - k + 1):
    if p[i - 1] > L1[i] or p[i + k - 1] < L2[i]:
        if li[i] > 0:
            ans += 1
print(ans - min(li) + li[0] + 1)

Buggy submission (verdict: Runtime Error):
import math
import numpy as np
import decimal
import collections
import itertools
import sys
#Union-Find
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
def unite(x, y):
    p = find(x)
    q = find(y)
    if p == q:
        return None
    if p > q:
        p,q = q,p
    par[p] += par[q]
    par[q] = p
def same(x, y):
    return find(x) == find(y)
def size(x):
    return -par[find(x)]
n = 0
par = [-1 for i in range(n)]

#素数関連
def prime_numbers(x):
    if x < 2:
        return []
    prime_numbers = [i for i in range(x)]
    prime_numbers[1] = 0
    for prime_number in prime_numbers:
        if prime_number > math.sqrt(x):
            break
        if prime_number == 0:
            continue
        for composite_number in range(2 * prime_number, x, prime_number):
            prime_numbers[composite_number] = 0
    return [prime_number for prime_number in prime_numbers if prime_number != 0]
def is_prime(x):
    if x < 2:
        return False
    if x == 2 or x == 3 or x == 5:
        return True
    if x % 2 == 0 or x % 3 == 0 or x % 5 == 0:
        return False
    prime_number = 7
    difference = 4
    while prime_number <= math.sqrt(x):
        if x % prime_number == 0:
            return False
        prime_number += difference
        difference = 6 - difference
    return True
#nCr
mod = 10 ** 9 + 7
def comb(n, r):
    if n < r:return 0
    if n < 0 or r < 0:return 0
    return fa[n] * fi[r] % mod * fi[n - r] % mod
fa = [1] * (n + 1)
fi = [1] * (n + 1)
for i in range(1, n + 1):
    fa[i] = fa[i - 1] * i % mod
    fi[i] = pow(fa[i], mod - 2, mod)
#BIT
BIT = [0] * (n + 1)
def add(i, x):
    while i <= n:
        BIT[i] += x
        i += i & -i
def query(i):
    s = 0
    while i > 0:
        s += BIT[i]
        i -= i & -i
from collections import deque
n, k = map(int, input().split())
p = list(map(int, input().split()))
cnt = 0
li = [1 for i in range(n - k + 1)]
for i in range(1, k):
    if p[i - 1] > p[i]:
        cnt += 1
if cnt == 0:
    li[0] = 0
for i in range(k, n):
    if p[i - 1] > p[i]:
        cnt += 1
    if p[i - k] > p[i - k + 1]:
        cnt -= 1
    if cnt == 0:
        li[i - k + 1] = 0
Q1 = deque([0 for i in range(n - k)])
Q2 = deque([0 for i in range(n - k)])
L1, L2 = list(Q1), list(Q2)
for i in range(n):
    while Q1 and p[Q1[-1]] > p[i]:
        Q1.pop()
    while Q2 and p[Q2[-1]] < p[i]:
        Q2.pop()
    Q1.append(i)
    Q2.append(i)
    if Q1[0] == i - k - 1:
        Q1.popleft()
    if Q2[0] == i - k - 1:
        Q2.popleft()
    if i >= k - 2:
        L1[i - k] = p[Q1[0]]
        L2[i - k] = p[Q2[0]]
ans = 0
for i in range(1, n - k - 1):
    if p[i - 1] > L1[i] or p[i + k - 1] < L2[i]:
        if li[i + 1] > 0:
            ans += 1
print(ans - min(li) + li[0] + 1)

Buggy submission (verdict: Runtime Error):
import math
import numpy as np
import decimal
import collections
import itertools
import sys
#Union-Find
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
def unite(x, y):
    p = find(x)
    q = find(y)
    if p == q:
        return None
    if p > q:
        p,q = q,p
    par[p] += par[q]
    par[q] = p
def same(x, y):
    return find(x) == find(y)
def size(x):
    return -par[find(x)]
n = 0
par = [-1 for i in range(n)]

#素数関連
def prime_numbers(x):
    if x < 2:
        return []
    prime_numbers = [i for i in range(x)]
    prime_numbers[1] = 0
    for prime_number in prime_numbers:
        if prime_number > math.sqrt(x):
            break
        if prime_number == 0:
            continue
        for composite_number in range(2 * prime_number, x, prime_number):
            prime_numbers[composite_number] = 0
    return [prime_number for prime_number in prime_numbers if prime_number != 0]
def is_prime(x):
    if x < 2:
        return False
    if x == 2 or x == 3 or x == 5:
        return True
    if x % 2 == 0 or x % 3 == 0 or x % 5 == 0:
        return False
    prime_number = 7
    difference = 4
    while prime_number <= math.sqrt(x):
        if x % prime_number == 0:
            return False
        prime_number += difference
        difference = 6 - difference
    return True
#nCr
mod = 10 ** 9 + 7
def comb(n, r):
    if n < r:return 0
    if n < 0 or r < 0:return 0
    return fa[n] * fi[r] % mod * fi[n - r] % mod
fa = [1] * (n + 1)
fi = [1] * (n + 1)
for i in range(1, n + 1):
    fa[i] = fa[i - 1] * i % mod
    fi[i] = pow(fa[i], mod - 2, mod)
#BIT
BIT = [0] * (n + 1)
def add(i, x):
    while i <= n:
        BIT[i] += x
        i += i & -i
def query(i):
    s = 0
    while i > 0:
        s += BIT[i]
        i -= i & -i
from collections import deque
n, k = map(int, input().split())
p = list(map(int, input().split()))
cnt = 0
li = [1 for i in range(n - k + 1)]
for i in range(1, k):
    if p[i - 1] > p[i]:
        cnt += 1
if cnt == 0:
    li[0] = 0
for i in range(k, n):
    if p[i - 1] > p[i]:
        cnt += 1
    if p[i - k] > p[i - k + 1]:
        cnt -= 1
    if cnt == 0:
        li[i - k + 1] = 0
Q1 = deque([0 for i in range(n - k + 1)])
Q2 = deque([0 for i in range(n - k + 1)])
L1, L2 = list(Q1), list(Q2)
for i in range(n):
    while Q1 and p[Q1[-1]] > p[i]:
        Q1.pop()
    while Q2 and p[Q1[-1]] < p[i]:
        Q2.pop()
    Q1.append(i)
    Q2.append(i)
    if Q1[0] == i - k:
        Q1.popleft()
    if Q2[0] == i - k:
        Q2.popleft()
    if i + 1 >= k:
        L1[i - k + 1] = p[Q1[0]]
        L2[i - k + 1] = p[Q2[0]]
ans = 0
for i in range(1, n - k):
    if p[i - 1] > L1[i] or p[i + k] < L2[i]:
        if li[i + 1] > 0:
            ans += 1
print(ans - min(li) + li[0] + 1)