Buggy python submission (verdict: Time Limit Exceeded):
def main():

    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    m=262143+n
    segtree=[-1]*m
    
    for i in range(262143,m):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        if 2*i+1>=m:
            segtree[i]=-1
        elif 2*i+2>=m:
            segtree[i]=segtree[2*i+1]
        else:
            segtree[i]=min(segtree[2*i+1],segtree[2*i+2])
    
    for i in range(n-k+2):
        qq=[[i,i+k-2,0,0]]
        s=set()
        while qq:
            ii,jj,now,l=qq.pop()
            kubun=2**l
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append([ii,jj,2*now+1,l+1])
                qq.append([ii,jj,2*now+2,l+1])
        min_list.append(min(s))
       
    for i in range(262142,-1,-1):
        if 2*i+1>=m:
            segtree[i]=n+1
        elif 2*i+2>=m:
            segtree[i]=segtree[2*i+1]
        else:
            segtree[i]=max(segtree[2*i+1],segtree[2*i+2])
    
    for i in range(n-k+2):
        qq=[[i,i+k-2,0,0]]
        s=set()
        while qq:
            ii,jj,now,l=qq.pop()
            kubun=2**l
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append([ii,jj,2*now+1,l+1])
                qq.append([ii,jj,2*now+2,l+1])
        max_list.append(max(s))
        
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    m=262143+n
    segtree=[-1]*m
    
    for i in range(262143,m):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=-1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=min(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        min_list.append(min(s))
       
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=n+1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=max(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        max_list.append(max(s))
        
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():

    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    m=262143+n
    segtree=[-1]*m
    
    for i in range(262143,m):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        if 2*i+1>=m:
            segtree[i]=-1
        elif 2*i+2>=m:
            segtree[i]=segtree[2*i+1]
        else:
            segtree[i]=min(segtree[2*i+1],segtree[2*i+2])
    
    for i in range(n-k+2):
        qq=[[i,i+k-2,0,0]]
        s=set()
        while qq:
            ii,jj,now,l=qq.pop()
            kubun=2**l
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append([ii,jj,2*now+1,l+1])
                qq.append([ii,jj,2*now+2,l+1])
        min_list.append(min(s))
       
    for i in range(262142,-1,-1):
        if 2*i+1>=m:
            segtree[i]=n+1
        elif 2*i+2>=m:
            segtree[i]=segtree[2*i+1]
        else:
            segtree[i]=max(segtree[2*i+1],segtree[2*i+2])
    
    for i in range(n-k+2):
        qq=[[i,i+k-2,0,0]]
        s=set()
        while qq:
            ii,jj,now,l=qq.pop()
            kubun=2**l
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append([ii,jj,2*now+1,l+1])
                qq.append([ii,jj,2*now+2,l+1])
        max_list.append(max(s))
        
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()

Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    m=262143+n
    segtree=[-1]*m
    
    for i in range(262143,m):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=-1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=min(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        min_list.append(min(s))
       
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=n+1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=max(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        max_list.append(max(s))
        
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():

    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    m=262143+n
    segtree=[-1]*m
    
    for i in range(262143,m):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        if 2*i+1>=m:
            segtree[i]=-1
        elif 2*i+2>=m:
            segtree[i]=segtree[2*i+1]
        else:
            segtree[i]=min(segtree[2*i+1],segtree[2*i+2])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,0)]
        s=set()
        while qq:
            ii,jj,now,l=qq.pop()
            kubun=2**l
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,l+1))
                qq.append((ii,jj,2*now+2,l+1))
        min_list.append(min(s))
       
    for i in range(262142,-1,-1):
        if 2*i+1>=m:
            segtree[i]=n+1
        elif 2*i+2>=m:
            segtree[i]=segtree[2*i+1]
        else:
            segtree[i]=max(segtree[2*i+1],segtree[2*i+2])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,0)]
        s=set()
        while qq:
            ii,jj,now,l=qq.pop()
            kubun=2**l
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,l+1))
                qq.append((ii,jj,2*now+2,l+1))
        max_list.append(max(s))
        
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    m=262143+n
    segtree=[-1]*m
    
    for i in range(262143,m):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=-1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=min(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        min_list.append(min(s))
       
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=n+1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=max(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        max_list.append(max(s))
        
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():

    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    m=262143+n
    segtree=[-1]*m
    
    for i in range(262143,m):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        if 2*i+1>=m:
            segtree[i]=-1
        elif 2*i+2>=m:
            segtree[i]=segtree[2*i+1]
        else:
            segtree[i]=min(segtree[2*i+1],segtree[2*i+2])
    
    def search(i,j,now):
        for k in range(20):
            if 2**k-1<=now<2**(k+1)-1:
                break
        kubun=2**k
        now2=now-kubun+1
        width=262144//kubun
        left=width*now2
        right=width*(now2+1)-1
        if i<=left and right<=j:
            return segtree[now]
        elif j<left or right<i:
            return n+1
        else:
            return min(search(i,j,2*now+1),search(i,j,2*now+2))
    
    for i in range(n-k+2):
        min_list.append(search(i,i+k-2,0))
       
    for i in range(262142,-1,-1):
        if 2*i+1>=m:
            segtree[i]=n+1
        elif 2*i+2>=m:
            segtree[i]=segtree[2*i+1]
        else:
            segtree[i]=max(segtree[2*i+1],segtree[2*i+2])
    
    def search(i,j,now):
        for k in range(20):
            if 2**k-1<=now<2**(k+1)-1:
                break
        kubun=2**(k)
        now2=now-kubun+1
        width=262144//kubun
        left=width*now2
        right=width*(now2+1)-1
        if i<=left and right<=j:
            return segtree[now]
        elif j<left or right<i:
            return -1
        else:
            return max(search(i,j,2*now+1),search(i,j,2*now+2))
    
    for i in range(n-k+2):
        max_list.append(search(i,i+k-2,0))
        
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    m=262143+n
    segtree=[-1]*m
    
    for i in range(262143,m):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=-1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=min(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        min_list.append(min(s))
       
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=n+1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=max(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        max_list.append(max(s))
        
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():

    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    m=262143+n
    segtree=[-1]*m
    
    for i in range(262143,m):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        if 2*i+1>=m:
            segtree[i]=-1
        elif 2*i+2>=m:
            segtree[i]=segtree[2*i+1]
        else:
            segtree[i]=min(segtree[2*i+1],segtree[2*i+2])
    
    def search(i,j,now):
        for k in range(20):
            if 2**k-1<=now<2**(k+1)-1:
                break
        kubun=2**k
        now2=now-kubun+1
        width=262144//kubun
        left=width*now2
        right=width*(now2+1)-1
        if i<=left and right<=j:
            return segtree[now]
        elif j<left or right<i:
            return n+1
        else:
            return min(search(i,j,2*now+1),search(i,j,2*now+2))
    
    for i in range(n-k+2):
        min_list.append(search(i,i+k-2,0))
       
    for i in range(262142,-1,-1):
        if 2*i+1>=m:
            segtree[i]=n+1
        elif 2*i+2>=m:
            segtree[i]=segtree[2*i+1]
        else:
            segtree[i]=max(segtree[2*i+1],segtree[2*i+2])
    
    def search(i,j,now):
        for k in range(20):
            if 2**k-1<=now<2**(k+1)-1:
                break
        kubun=2**(k)
        now2=now-kubun+1
        width=262144//kubun
        left=width*now2
        right=width*(now2+1)-1
        if i<=left and right<=j:
            return segtree[now]
        elif j<left or right<i:
            return -1
        else:
            return max(search(i,j,2*now+1),search(i,j,2*now+2))
    
    for i in range(n-k+2):
        max_list.append(search(i,i+k-2,0))
        
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    m=262143+n
    segtree=[-1]*m
    
    for i in range(262143,m):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=-1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=min(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        min_list.append(min(s))
       
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=n+1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=max(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        max_list.append(max(s))
        
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():

    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    
    segtree=[n+1]*524288
    
    for i in range(262143,262143+n):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        segtree[i]=min(segtree[2*i+1],segtree[2*i+2])
    
    def search(i,j,now):
        for k in range(20):
            if 2**k-1<=now<2**(k+1)-1:
                break
        kubun=2**(k)
        now2=now-kubun+1
        width=262144//kubun
        left=width*now2
        right=width*(now2+1)-1
        if i<=left and right<=j:
            return segtree[now]
        elif j<left or right<i:
            return n+1
        else:
            return min(search(i,j,2*now+1),search(i,j,2*now+2))
    
    for i in range(n-k+2):
        min_list.append(search(i,i+k-2,0))
    
    segtree=[-1]*524288
    
    for i in range(262143,262143+n):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        segtree[i]=max(segtree[2*i+1],segtree[2*i+2])
    
    def search(i,j,now):
        for k in range(20):
            if 2**k-1<=now<2**(k+1)-1:
                break
        kubun=2**(k)
        now2=now-kubun+1
        width=262144//kubun
        left=width*now2
        right=width*(now2+1)-1
        if i<=left and right<=j:
            return segtree[now]
        elif j<left or right<i:
            return -1
        else:
            return max(search(i,j,2*now+1),search(i,j,2*now+2))
    
    for i in range(n-k+2):
        max_list.append(search(i,i+k-2,0))
        
    cnt=0
    
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()


Fixed submission:
def main():
    import sys
    input=sys.stdin.readline
    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    min_list=[]
    max_list=[]
    m=262143+n
    segtree=[-1]*m
    
    for i in range(262143,m):
        segtree[i]=x[i-262143]
    	
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=-1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=min(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        min_list.append(min(s))
       
    for i in range(262142,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=n+1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i]=max(segtree[j],segtree[j+1])
    
    for i in range(n-k+2):
        qq=[(i,i+k-2,0,1)]
        s=set()
        while qq:
            ii,jj,now,kubun=qq.pop()
            now2=now-kubun+1
            width=262144//kubun
            left=width*now2
            right=width*(now2+1)-1
            if ii<=left and right<=jj:
                s.add(segtree[now])
            elif jj<left or right<ii:
                continue
            else:
                qq.append((ii,jj,2*now+1,2*kubun))
                qq.append((ii,jj,2*now+2,2*kubun))
        max_list.append(max(s))
        
    cnt=0
    flag=[True]*(n-k+1)
    first=False
    for i in range(n-1):
        if x[i]<x[i+1]:
            cnt+=1
        else:
            cnt=0
        if cnt>=k-1:
            if first==False:
                first=True
            else:
                flag[i-k+2]=False
    
    for i in range(1,n-k+1):
        if x[i-1]<min_list[i] and max_list[i]<x[i+k-1]:
            flag[i]=False
            
    print(sum(flag))

if __name__ == '__main__':
	main()