Snipers
Difficulty: Hard

The National Security is in charge of the president's security. Tomorrow the president is going to speak to people. Today, everybody is crazy because there is a threat in progress. The agents believe there is a possibility of
a sniper attack. The city consists of just one road with N contiguous buildings.

The experts say the snipers could take position on the roof of the buildings following the next assumptions:

- If there is a sniper at building i-th with height Hi and there is other sniper at building j-th with height Hj,then Hi < Hj and j - i <= D for (1 <= i < j <= N).
- The terrorist are going to set as much snipers as they can, following the previous constraints in order to increase the success.

The National Security wants you to make a program which helps them to know how many buildings could contain a
sniper.	

**Input specification**

The first line contains two numbers N (1<=N<=100000) and D (1<=D<=10000). Next follow a line with N integers
Hi(1<=Hi<=100000), the height of the i-th building. (Hi != Hj, Max(i-D-1, 1) <= j < i). 

**Output specification**

Output just a single line with the number of buildings could contain a sniper. 


**Sample input**
<br>5 1
<br>8 1 9 5 6


**Sample output**
<br>4

Hint:
<br>The maximum number of snipers could be are 2.
They could occupy the next buildings:
2 3
4 5
There are 4 buildings could contain snipers, bulding number 2,3,4 and 5.

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (Python):
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	static int N, D;
	static String cad[];
	static int arr[],inc[];
	static int tree[];
	static int c, d, maxx, val, upper, remove, sol;
		
	static void update(int index, int a, int b) {
		if(a > c || b < c) return;
		if(a == b) {
			tree[index] = d;
			return;
		}
		
		update(2*index, a, (a+b)/2);
		update(2*index+1, (a+b)/2+1, b);
		
		tree[index] = Math.max(tree[2*index], tree[2*index+1]);
	}
	
	static int querie(int index, int a, int b) {
		if(a > d || b < c) return 0;
		if(a >= c && b <= d) {
			return tree[index];
		}		
		return Math.max(querie(2*index, a, (a+b)/2), querie(2*index+1, (a+b)/2+1, b));
	}
	
	public static void main(String[] args) throws IOException {
		InputStreamReader sr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(sr);
		
		cad = br.readLine().split(" ");
		N = Integer.parseInt(cad[0]);
		D = Integer.parseInt(cad[1]);		
			
		arr = new int[N+2];
		inc = new int[N+2];
		
		cad = br.readLine().split(" ");
		for (int i = 0; i < N; i++) {
			arr[i] = Integer.parseInt(cad[i]);
			upper = Math.max(upper, arr[i]);				
		}	

		tree = new int[upper * 5 + 2];		
	
		maxx = 0;		
		for(int i = 0;i<N;i++) {
			remove = i - D - 1;
			if(remove >= 0) {
				c = arr[remove];
				d = 0;
				update(1,1,upper);
			}		
			
			c = 1;
			d = arr[i] - 1;
			if(c <= d) {
				val = querie(1, 1, upper); 
			} else {
				val = 0;
			}
			
			inc[i] =  val + 1;
			maxx = Math.max(maxx, inc[i]);
			
			c = arr[i];
			d = inc[i];
			update(1, 1, upper);
		}
		
		tree = new int[upper * 5 + 2];		
		
		for(int i = N-1;i>=0;i--) {
			remove = i + D + 1;
			if(remove < N) {
				c = arr[remove];
				d = 0;
				update(1,1,upper);
			}		
			
			c = arr[i] + 1;
			d = upper;
			if(c <= d) {
				val = querie(1, 1, upper); 
			} else {
				val = 0;
			}
			
			if(inc[i] + val == maxx) {
				++sol;				
			}
			
			c = arr[i];
			d = val + 1;
			update(1, 1, upper);
		}
				
		System.out.println(sol);
	}
}