-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHEFTOWN(EASY)
70 lines (66 loc) · 1.67 KB
/
CHEFTOWN(EASY)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll minimum(ll a, ll b){
if(a>b)return b;
else return a;
}
ll maximum(ll a ,ll b){
if(a>b)return a;
else return b;
}
struct node{
ll max;
ll min;
};
node n={-1,1000000009};
void printnode(node s){
cout<<"MIN="<<s.min<<" MAX="<<s.max<<'\n';
}
void build(ll A[],node seg[] ,ll low, ll high, ll pos){
if(low==high){
seg[pos].min=A[low];
seg[pos].max=A[low];
//cout<<"Node inserted at "<<pos<<'\n';
//printnode(seg[pos]);
return ;
}
ll mid = (low+high)/2;
build(A,seg,low, mid, 2*pos+1);
build(A,seg,mid+1, high, 2*pos+2);
seg[pos].min=minimum(seg[pos*2+1].min,seg[pos*2+2].min);
seg[pos].max=maximum(seg[pos*2+1].max,seg[pos*2+2].max);
//cout<<"Node inserted at "<<pos<<'\n';
// printnode(seg[pos]);
}
node query(node seg[],ll low,ll high,ll qlow, ll qhigh,ll pos ){
if(qlow>high||qhigh<low||low>high)return n;
else if(qlow<=low&&qhigh>=high)return seg[pos];
ll mid=(low+high)/2;
node res;
node r1=query(seg,low,mid,qlow ,qhigh,2*pos+1);
node r2=query(seg,mid+1,high,qlow ,qhigh,2*pos+2);
res.max=maximum(r1.max,r2.max);
res.min=minimum(r1.min,r2.min);
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
ll N,W;
cin>>N>>W;
ll A[N];
ll x=pow(2,1+ceil(log2(N+1)));
node seg[x];
for(int i=0;i<N;i++)cin>>A[i];
build(A,seg,0,N-1,0);
ll Ans=0;
for(int i=0;i<N-W+1;i++){
node ans=query(seg,0,N-1,i,i+W-1,0);
//printnode(ans);
if(ans.max-ans.min+1==W)Ans++;
}
cout<<Ans;
return 0;
}