题意
Sol
昨天没想到真是有点可惜了。
我们考虑每个点作为最大值的贡献,首先预处理出每个位置\(i\)左边第一个比他大的数\(l\),显然\([l + 1, i]\)内的数的后继要么是\(i\),要么在这一段区间中。那么可以对这段区间\(+1\),然后每次查询\([i - k + 1, i]\)的最大值即可
#include#define Pair pair #define MP(x, y) make_pair(x, y)#define fi first#define se second//#define int long long #define LL long long #define Fin(x) {freopen(#x".in","r",stdin);}#define Fout(x) {freopen(#x".out","w",stdout);}using namespace std;const int MAXN = 4e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;const double eps = 1e-9;template inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}template inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}template inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}template inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}template inline LL mul(A x, B y) {return 1ll * x * y % mod;}template inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}template inline void debug(A a){cout << a << '\n';}template inline LL sqr(A x){return 1ll * x * x;}inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f;}int N, K, a[MAXN], st[MAXN], top, pre[MAXN];int root, ls[MAXN], rs[MAXN], tag[MAXN], mx[MAXN], ll[MAXN], rr[MAXN], tot, times;void ps(int &k, int v, int l, int r) { if(!k) k = ++tot, ll[k] = l, rr[k] = r; mx[k] += v; tag[k] += v;}void pushdown(int k, int l, int r) { if(!tag[k]) return ; int mid = l + r >> 1; ps(ls[k], tag[k], l, mid); ps(rs[k], tag[k], mid + 1, r); tag[k] = 0;}void update(int k) { mx[k] = max(mx[ls[k]], mx[rs[k]]);}void IntAdd(int &k, int l, int r, int ql, int qr, int v) { if(!k) k = ++tot, ll[k] = l, rr[k] = r; if(ql <= l && r <= qr) {ps(k, v, l, r); return ;} pushdown(k, l, r); int mid = l + r >> 1; if(ql <= mid) IntAdd(ls[k], l, mid, ql, qr, v); if(qr > mid) IntAdd(rs[k], mid + 1, r, ql, qr, v); update(k);}int Query(int k, int l, int r, int ql, int qr) { if(!k) return 0; if(ql <= l && r <= qr) return mx[k]; pushdown(k, l, r); int mid = l + r >> 1; if(ql > mid) return Query(rs[k], mid + 1, r, ql, qr); else if(qr <= mid) return Query(ls[k], l, mid, ql, qr); else return max(Query(ls[k], l, mid, ql, qr), Query(rs[k], mid + 1, r, ql, qr));}signed main() { N = read(); K = read(); for(int i = 1; i <= N; i++) a[i] = read(); for(int i = N; i; i--) { while(top && a[st[top]] <= a[i]) pre[st[top--]] = i; st[++top] = i; } while(top) pre[st[top--]] = 0; for(int i = 1; i <= N; i++) { IntAdd(root, 1, N, pre[i] + 1, i, 1); if(i >= K) cout << Query(root, 1, N, i - K + 1, i) << " "; } return 0;}