-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathG.cpp
72 lines (67 loc) · 1.32 KB
/
G.cpp
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
68
69
70
71
72
#include "bits/stdc++.h"
using namespace std;
int t[1000010 * 4];
int p[1000010 * 4];
int n;
void pushdown(int c) {
int l = c << 1;
int r = l + 1;
p[l] += p[c];
p[r] += p[c];
t[l] += p[c];
t[r] += p[c];
p[c] = 0;
}
void update(int x, int y, int val, int c = 1, int b = 1, int e = n) {
if(x <= b && e <= y) {
t[c] += val;
p[c] += val;
return ;
}
if (y < b || e < x) return ;
pushdown(c);
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
update(x, y, val, l, b, m);
update(x, y, val, r, m+1, e);
t[c] = max(t[l], t[r]);
}
int query(int x, int y, int c = 1, int b = 1, int e = n) {
if(x <= b && e <= y) {
return t[c];
}
if(y < b || e < x) return 0;
pushdown(c);
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
return max(query(x, y, l, b, m), query(x, y, r, m+1, e));
}
int a[1000010];
int ans[1000010];
int main(int argc, char const *argv[])
{
int k;
scanf("%d %d", &n, &k);
vector <int> v;
v.emplace_back(0);
a[0] = INT_MAX;
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
while(!v.empty() && a[v.back()] < a[i]) {
v.pop_back();
}
update(v.back() + 1, i, 1);
// cout << "add: " << v.back() + 1 << ' ' << i << endl;
v.emplace_back(i);
if(i >= k) {
ans[i] = query(i - k + 1, i);
}
}
for(int i = k; i <= n; i++) {
printf("%d ", ans[i]);
}
printf("\n");
return 0;
}