output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int const N = 1000000;
int x[N], pa[N], lc[N];
stack<int> st;
multiset<int> an;
set<pair<int, int> > tr[N];
int P(int v) { return pa[v] < 0 ? v : pa[v] = P(pa[v]); }
int sz(int id) {
assert(!tr[id].empty());
return tr[id].rbegin()->first - tr[id].begin()->first + 1;
}
void J(int a, int b) {
int of = tr[a].rbegin()->first - tr[b].rbegin()->first - 1;
if (tr[a].size() < tr[b].size()) {
swap(tr[a], tr[b]);
of = tr[a].rbegin()->first - tr[b].rbegin()->first + 1;
}
for (auto it = tr[b].begin(); it != tr[b].end(); tr[b].erase(it++)) {
int id = it->second;
lc[id] = it->first + of;
tr[a].insert(make_pair(lc[id], id));
}
pa[b] = a;
}
void ad(int id) {
tr[id].insert(make_pair(0, id));
while (!st.empty() && x[st.top()] < x[id]) {
if (!tr[st.top()].empty()) {
an.erase(an.find(sz(st.top())));
J(id, st.top());
}
st.pop();
}
st.push(id);
an.insert(sz(id));
}
void er(int id) {
int g = P(id);
an.erase(an.find(sz(g)));
tr[g].erase(make_pair(lc[id], id));
if (!tr[g].empty()) an.insert(sz(g));
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < (int)(n); ++i) scanf("%d", x + i);
for (int i = 0; i < (int)(n); ++i) pa[i] = -1;
bool sp = false;
for (int i = 0; i < (int)(n); ++i) {
if (i - k >= 0) er(i - k);
ad(i);
if (i + 1 >= k) {
if (sp)
printf(" ");
else
sp = true;
printf("%d", *an.rbegin());
}
}
printf("\n");
}
| ### Prompt
Please create a solution in cpp to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int const N = 1000000;
int x[N], pa[N], lc[N];
stack<int> st;
multiset<int> an;
set<pair<int, int> > tr[N];
int P(int v) { return pa[v] < 0 ? v : pa[v] = P(pa[v]); }
int sz(int id) {
assert(!tr[id].empty());
return tr[id].rbegin()->first - tr[id].begin()->first + 1;
}
void J(int a, int b) {
int of = tr[a].rbegin()->first - tr[b].rbegin()->first - 1;
if (tr[a].size() < tr[b].size()) {
swap(tr[a], tr[b]);
of = tr[a].rbegin()->first - tr[b].rbegin()->first + 1;
}
for (auto it = tr[b].begin(); it != tr[b].end(); tr[b].erase(it++)) {
int id = it->second;
lc[id] = it->first + of;
tr[a].insert(make_pair(lc[id], id));
}
pa[b] = a;
}
void ad(int id) {
tr[id].insert(make_pair(0, id));
while (!st.empty() && x[st.top()] < x[id]) {
if (!tr[st.top()].empty()) {
an.erase(an.find(sz(st.top())));
J(id, st.top());
}
st.pop();
}
st.push(id);
an.insert(sz(id));
}
void er(int id) {
int g = P(id);
an.erase(an.find(sz(g)));
tr[g].erase(make_pair(lc[id], id));
if (!tr[g].empty()) an.insert(sz(g));
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < (int)(n); ++i) scanf("%d", x + i);
for (int i = 0; i < (int)(n); ++i) pa[i] = -1;
bool sp = false;
for (int i = 0; i < (int)(n); ++i) {
if (i - k >= 0) er(i - k);
ad(i);
if (i + 1 >= k) {
if (sp)
printf(" ");
else
sp = true;
printf("%d", *an.rbegin());
}
}
printf("\n");
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000003;
int n, k, p[N], stk[N], top, head[N], to[N], nxt[N], dfn[N], siz[N], tim;
inline void add(int a, int b) {
static int cnt = 0;
to[++cnt] = b;
nxt[cnt] = head[a];
head[a] = cnt;
}
inline void dfs(int x) {
dfn[x] = ++tim;
siz[x] = 1;
for (register int i = head[x]; i; i = nxt[i])
dfs(to[i]), siz[x] += siz[to[i]];
}
int seg[N << 2], lazy[N << 2];
inline void pushdown(int x) {
if (lazy[x]) {
seg[x << 1] += lazy[x];
seg[x << 1 | 1] += lazy[x];
lazy[x << 1] += lazy[x];
lazy[x << 1 | 1] += lazy[x];
lazy[x] = 0;
}
}
inline void pushup(int x) { seg[x] = max(seg[x << 1], seg[x << 1 | 1]); }
inline void change(int x, int L, int R, int l, int r, int v) {
if (l <= L && R <= r) {
seg[x] += v;
lazy[x] += v;
return;
}
int mid = L + R >> 1;
pushdown(x);
if (l <= mid) change(x << 1, L, mid, l, r, v);
if (mid < r) change(x << 1 | 1, mid + 1, R, l, r, v);
pushup(x);
}
int main() {
scanf("%d%d", &n, &k);
for (register int i = 1; i <= n; i++) scanf("%d", p + i);
for (register int i = 1; i <= n; i++) {
while (top && p[stk[top]] < p[i]) {
add(i, stk[top]);
--top;
}
stk[++top] = i;
}
while (top) add(n + 1, stk[top]), --top;
dfs(n + 1);
for (register int i = 1; i <= k; i++)
change(1, 1, n + 1, dfn[i], dfn[i] + siz[i] - 1, 1);
printf("%d", seg[1]);
for (register int i = k + 1; i <= n; i++) {
change(1, 1, n + 1, dfn[i], dfn[i] + siz[i] - 1, 1);
change(1, 1, n + 1, dfn[i - k], dfn[i - k] + siz[i - k] - 1, -1);
printf(" %d", seg[1]);
}
}
| ### Prompt
In CPP, your task is to solve the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1000003;
int n, k, p[N], stk[N], top, head[N], to[N], nxt[N], dfn[N], siz[N], tim;
inline void add(int a, int b) {
static int cnt = 0;
to[++cnt] = b;
nxt[cnt] = head[a];
head[a] = cnt;
}
inline void dfs(int x) {
dfn[x] = ++tim;
siz[x] = 1;
for (register int i = head[x]; i; i = nxt[i])
dfs(to[i]), siz[x] += siz[to[i]];
}
int seg[N << 2], lazy[N << 2];
inline void pushdown(int x) {
if (lazy[x]) {
seg[x << 1] += lazy[x];
seg[x << 1 | 1] += lazy[x];
lazy[x << 1] += lazy[x];
lazy[x << 1 | 1] += lazy[x];
lazy[x] = 0;
}
}
inline void pushup(int x) { seg[x] = max(seg[x << 1], seg[x << 1 | 1]); }
inline void change(int x, int L, int R, int l, int r, int v) {
if (l <= L && R <= r) {
seg[x] += v;
lazy[x] += v;
return;
}
int mid = L + R >> 1;
pushdown(x);
if (l <= mid) change(x << 1, L, mid, l, r, v);
if (mid < r) change(x << 1 | 1, mid + 1, R, l, r, v);
pushup(x);
}
int main() {
scanf("%d%d", &n, &k);
for (register int i = 1; i <= n; i++) scanf("%d", p + i);
for (register int i = 1; i <= n; i++) {
while (top && p[stk[top]] < p[i]) {
add(i, stk[top]);
--top;
}
stk[++top] = i;
}
while (top) add(n + 1, stk[top]), --top;
dfs(n + 1);
for (register int i = 1; i <= k; i++)
change(1, 1, n + 1, dfn[i], dfn[i] + siz[i] - 1, 1);
printf("%d", seg[1]);
for (register int i = k + 1; i <= n; i++) {
change(1, 1, n + 1, dfn[i], dfn[i] + siz[i] - 1, 1);
change(1, 1, n + 1, dfn[i - k], dfn[i - k] + siz[i - k] - 1, -1);
printf(" %d", seg[1]);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
const int oo = 0x3f3f3f3f;
namespace SEG {
const int SZ = N << 2;
int tag[SZ + 5], mx[SZ + 5];
void push_down(int u) {
if (tag[u]) {
mx[(u << 1)] += tag[u], tag[(u << 1)] += tag[u];
mx[(u << 1 | 1)] += tag[u], tag[(u << 1 | 1)] += tag[u];
}
tag[u] = 0;
}
void add(int u, int l, int r, int x, int y, int v) {
if (x <= l && r <= y) {
mx[u] += v;
tag[u] += v;
} else {
if (x <= ((l + r) >> 1)) add((u << 1), l, ((l + r) >> 1), x, y, v);
if (((l + r) >> 1) < y) add((u << 1 | 1), ((l + r) >> 1) + 1, r, x, y, v);
mx[u] = max(mx[(u << 1)], mx[(u << 1 | 1)]);
}
}
int query(int u, int l, int r, int x, int y) {
if (x > r || y < l) return -oo;
if (x <= l && r <= y) return mx[u];
push_down(u);
return max(query((u << 1), l, ((l + r) >> 1), x, y),
query((u << 1 | 1), ((l + r) >> 1) + 1, r, x, y));
}
} // namespace SEG
int n, k;
int a[N + 5];
int stk[N + 5], bnd[N + 5], top;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
bnd[i] = i;
while (top > 0 && a[stk[top - 1]] < a[i]) {
bnd[i] = bnd[stk[top - 1]];
--top;
}
stk[top++] = i;
}
for (int i = 1; i <= n; ++i) {
SEG::add(1, 1, n, bnd[i], i, +1);
if (i - k + 1 > 0) {
printf("%d ", SEG::query(1, 1, n, i - k + 1, i));
}
}
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
const int oo = 0x3f3f3f3f;
namespace SEG {
const int SZ = N << 2;
int tag[SZ + 5], mx[SZ + 5];
void push_down(int u) {
if (tag[u]) {
mx[(u << 1)] += tag[u], tag[(u << 1)] += tag[u];
mx[(u << 1 | 1)] += tag[u], tag[(u << 1 | 1)] += tag[u];
}
tag[u] = 0;
}
void add(int u, int l, int r, int x, int y, int v) {
if (x <= l && r <= y) {
mx[u] += v;
tag[u] += v;
} else {
if (x <= ((l + r) >> 1)) add((u << 1), l, ((l + r) >> 1), x, y, v);
if (((l + r) >> 1) < y) add((u << 1 | 1), ((l + r) >> 1) + 1, r, x, y, v);
mx[u] = max(mx[(u << 1)], mx[(u << 1 | 1)]);
}
}
int query(int u, int l, int r, int x, int y) {
if (x > r || y < l) return -oo;
if (x <= l && r <= y) return mx[u];
push_down(u);
return max(query((u << 1), l, ((l + r) >> 1), x, y),
query((u << 1 | 1), ((l + r) >> 1) + 1, r, x, y));
}
} // namespace SEG
int n, k;
int a[N + 5];
int stk[N + 5], bnd[N + 5], top;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
bnd[i] = i;
while (top > 0 && a[stk[top - 1]] < a[i]) {
bnd[i] = bnd[stk[top - 1]];
--top;
}
stk[top++] = i;
}
for (int i = 1; i <= n; ++i) {
SEG::add(1, 1, n, bnd[i], i, +1);
if (i - k + 1 > 0) {
printf("%d ", SEG::query(1, 1, n, i - k + 1, i));
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, k, a[N];
vector<int> g[N];
int sta[N], top, dep[N];
int dfn[N], dfn_end[N], clk;
int val[N];
void dfs(int u) {
dfn[u] = ++clk;
val[clk] = dep[u];
for (int v : g[u]) {
dfs(v);
}
dfn_end[u] = clk;
}
int mx[N * 10], tag[N * 10];
const int inf = -0xcfcfcfcf;
void push(int id) {
if (tag[id]) {
tag[(id << 1)] += tag[id], tag[(id << 1 | 1)] += tag[id];
mx[id] += tag[id], tag[id] = 0;
}
}
void upd(int id) {
push((id << 1)), push((id << 1 | 1));
mx[id] = max(mx[(id << 1)], mx[(id << 1 | 1)]);
}
void modify(int id, int l, int r, int ql, int qr, int val) {
int mid = (l + r) >> 1;
push(id);
if (ql <= l && r <= qr) {
tag[id] += val;
return;
} else if (qr <= mid) {
modify((id << 1), l, mid, ql, qr, val);
} else if (ql >= mid + 1) {
modify((id << 1 | 1), mid + 1, r, ql, qr, val);
} else {
modify((id << 1), l, mid, ql, mid, val);
modify((id << 1 | 1), mid + 1, r, mid + 1, qr, val);
}
upd(id);
}
int ans[N];
int main() {
memset(mx, 0xcf, sizeof mx);
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
for (int i = n; i; --i) {
while (top && a[sta[top]] <= a[i]) --top;
dep[i] = dep[sta[top]] + 1;
g[sta[top]].push_back(i);
sta[++top] = i;
}
dfs(0);
int L = n - k + 1, R = n;
for (int i = L; i <= R; ++i) {
modify(1, 1, clk, dfn[i], dfn[i], inf + dep[i]);
}
for (int i = 1; i <= n - k + 1; ++i) {
push(1), ans[i] = mx[1];
modify(1, 1, clk, dfn[R], dfn_end[R], -1);
modify(1, 1, clk, dfn[L - 1], dfn[L - 1], inf + dep[L - 1]);
--R, --L;
}
for (int i = n - k + 1; i; --i) {
printf("%d ", ans[i]);
}
}
| ### Prompt
Generate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, k, a[N];
vector<int> g[N];
int sta[N], top, dep[N];
int dfn[N], dfn_end[N], clk;
int val[N];
void dfs(int u) {
dfn[u] = ++clk;
val[clk] = dep[u];
for (int v : g[u]) {
dfs(v);
}
dfn_end[u] = clk;
}
int mx[N * 10], tag[N * 10];
const int inf = -0xcfcfcfcf;
void push(int id) {
if (tag[id]) {
tag[(id << 1)] += tag[id], tag[(id << 1 | 1)] += tag[id];
mx[id] += tag[id], tag[id] = 0;
}
}
void upd(int id) {
push((id << 1)), push((id << 1 | 1));
mx[id] = max(mx[(id << 1)], mx[(id << 1 | 1)]);
}
void modify(int id, int l, int r, int ql, int qr, int val) {
int mid = (l + r) >> 1;
push(id);
if (ql <= l && r <= qr) {
tag[id] += val;
return;
} else if (qr <= mid) {
modify((id << 1), l, mid, ql, qr, val);
} else if (ql >= mid + 1) {
modify((id << 1 | 1), mid + 1, r, ql, qr, val);
} else {
modify((id << 1), l, mid, ql, mid, val);
modify((id << 1 | 1), mid + 1, r, mid + 1, qr, val);
}
upd(id);
}
int ans[N];
int main() {
memset(mx, 0xcf, sizeof mx);
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
for (int i = n; i; --i) {
while (top && a[sta[top]] <= a[i]) --top;
dep[i] = dep[sta[top]] + 1;
g[sta[top]].push_back(i);
sta[++top] = i;
}
dfs(0);
int L = n - k + 1, R = n;
for (int i = L; i <= R; ++i) {
modify(1, 1, clk, dfn[i], dfn[i], inf + dep[i]);
}
for (int i = 1; i <= n - k + 1; ++i) {
push(1), ans[i] = mx[1];
modify(1, 1, clk, dfn[R], dfn_end[R], -1);
modify(1, 1, clk, dfn[L - 1], dfn[L - 1], inf + dep[L - 1]);
--R, --L;
}
for (int i = n - k + 1; i; --i) {
printf("%d ", ans[i]);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int n, k, arr[N], idx[N], last[N], dfs_cnt = 0;
vector<int> g[N];
stack<int> st;
bool vis[N];
int seg[4 * N], lazy[4 * N];
void DFS(int node) {
idx[node] = last[node] = dfs_cnt++;
vis[node] = true;
for (int i = 0; i < (int)g[node].size(); i++) {
DFS(g[node][i]);
last[node] = last[g[node][i]];
}
}
inline void fix(int s, int e, int idx) {
seg[idx] += lazy[idx];
if (s != e) {
lazy[(idx << 1)] += lazy[idx];
lazy[(idx << 1) + 1] += lazy[idx];
}
lazy[idx] = 0;
}
int l, r, val;
int update(int s, int e, int idx) {
fix(s, e, idx);
if (s > r || e < l) return seg[idx];
if (s >= l && e <= r) {
lazy[idx] += val;
fix(s, e, idx);
return seg[idx];
}
return seg[idx] = max(update(s, (s + e) / 2, idx * 2),
update((s + e) / 2 + 1, e, idx * 2 + 1));
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
while ((int)st.size() > 0 && arr[i] > arr[st.top()]) {
g[i].push_back(st.top());
st.pop();
}
st.push(i);
}
for (int i = n - 1; i >= 0; i--) {
if (!vis[i]) DFS(i);
}
int j = 0;
for (j = 0; j < k; j++) {
l = idx[j];
r = last[j];
val = 1;
update(0, n - 1, 1);
}
j--;
int ss;
while (j < n) {
fix(0, n - 1, 1);
printf("%d ", seg[1]);
ss = j - k + 1;
l = idx[ss];
r = idx[ss];
val = -1000000010;
update(0, n - 1, 1);
j++;
if (j == n) break;
l = idx[j];
r = last[j];
val = 1;
update(0, n - 1, 1);
}
puts("");
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int n, k, arr[N], idx[N], last[N], dfs_cnt = 0;
vector<int> g[N];
stack<int> st;
bool vis[N];
int seg[4 * N], lazy[4 * N];
void DFS(int node) {
idx[node] = last[node] = dfs_cnt++;
vis[node] = true;
for (int i = 0; i < (int)g[node].size(); i++) {
DFS(g[node][i]);
last[node] = last[g[node][i]];
}
}
inline void fix(int s, int e, int idx) {
seg[idx] += lazy[idx];
if (s != e) {
lazy[(idx << 1)] += lazy[idx];
lazy[(idx << 1) + 1] += lazy[idx];
}
lazy[idx] = 0;
}
int l, r, val;
int update(int s, int e, int idx) {
fix(s, e, idx);
if (s > r || e < l) return seg[idx];
if (s >= l && e <= r) {
lazy[idx] += val;
fix(s, e, idx);
return seg[idx];
}
return seg[idx] = max(update(s, (s + e) / 2, idx * 2),
update((s + e) / 2 + 1, e, idx * 2 + 1));
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
while ((int)st.size() > 0 && arr[i] > arr[st.top()]) {
g[i].push_back(st.top());
st.pop();
}
st.push(i);
}
for (int i = n - 1; i >= 0; i--) {
if (!vis[i]) DFS(i);
}
int j = 0;
for (j = 0; j < k; j++) {
l = idx[j];
r = last[j];
val = 1;
update(0, n - 1, 1);
}
j--;
int ss;
while (j < n) {
fix(0, n - 1, 1);
printf("%d ", seg[1]);
ss = j - k + 1;
l = idx[ss];
r = idx[ss];
val = -1000000010;
update(0, n - 1, 1);
j++;
if (j == n) break;
l = idx[j];
r = last[j];
val = 1;
update(0, n - 1, 1);
}
puts("");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e6;
int n, k, a[mxN], l[mxN], p[mxN], ld[mxN], lb[mxN];
int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); }
void join(int x, int y) {
p[x] = y;
lb[y] = lb[x];
ld[y] += ld[x];
}
void upd1(int i, int j) {
i = find(i);
++ld[i];
if (j < n) --ld[j];
while (ld[i] >= 0 && lb[i]) join(lb[i] - 1, i);
}
void upd2(int i) {
if (i < k) return;
int j = find(i - k + 1);
while (lb[j] > i - k) join(lb[j] - 1, j);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
iota(p, p + n, 0);
iota(lb, lb + n, 0);
for (int i = 0; i < n; ++i) {
upd2(i);
l[i] = i - 1;
while (l[i] >= 0 && a[l[i]] < a[i]) l[i] = l[l[i]];
upd1(l[i] + 1, i + 1);
if (i >= k - 1) cout << ld[find(0)] << " ";
}
}
| ### Prompt
Create a solution in CPP for the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e6;
int n, k, a[mxN], l[mxN], p[mxN], ld[mxN], lb[mxN];
int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); }
void join(int x, int y) {
p[x] = y;
lb[y] = lb[x];
ld[y] += ld[x];
}
void upd1(int i, int j) {
i = find(i);
++ld[i];
if (j < n) --ld[j];
while (ld[i] >= 0 && lb[i]) join(lb[i] - 1, i);
}
void upd2(int i) {
if (i < k) return;
int j = find(i - k + 1);
while (lb[j] > i - k) join(lb[j] - 1, j);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
iota(p, p + n, 0);
iota(lb, lb + n, 0);
for (int i = 0; i < n; ++i) {
upd2(i);
l[i] = i - 1;
while (l[i] >= 0 && a[l[i]] < a[i]) l[i] = l[l[i]];
upd1(l[i] + 1, i + 1);
if (i >= k - 1) cout << ld[find(0)] << " ";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
struct segtree {
int mx[maxn << 2], lazy[maxn << 2];
void maintain(int rt) { mx[rt] = max(mx[(rt << 1)], mx[(rt << 1 | 1)]); }
void push(int rt) {
if (!lazy[rt]) return;
lazy[(rt << 1)] += lazy[rt], lazy[(rt << 1 | 1)] += lazy[rt];
mx[(rt << 1)] += lazy[rt], mx[(rt << 1 | 1)] += lazy[rt];
lazy[rt] = 0;
}
void update(int l, int r, int rt, int ql, int qr, int v) {
if (ql <= l && qr >= r) {
lazy[rt] += v;
mx[rt] += v;
return;
;
}
push(rt);
int mid = l + r >> 1;
if (ql <= mid) update(l, mid, (rt << 1), ql, qr, v);
if (qr > mid) update(mid + 1, r, (rt << 1 | 1), ql, qr, v);
maintain(rt);
}
} T;
vector<vector<int> > v;
int in[maxn], out[maxn], tot;
void dfs(int x) {
in[x] = ++tot;
for (auto &u : v[x]) dfs(u);
out[x] = tot;
}
int go[maxn], a[maxn];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
v.resize(n + 2);
for (int i = 1; i <= n; ++i) cin >> a[i], go[i] = i + 1;
for (int i = n; i >= 1; --i) {
while (go[i] <= n && a[i] >= a[go[i]]) {
go[i] = go[go[i]];
}
v[go[i]].push_back(i);
}
dfs(n + 1);
for (int i = 1; i <= n; ++i) {
T.update(1, n + 1, 1, in[i], out[i], 1);
if (i - k >= 1)
T.update(1, n + 1, 1, in[i - k], out[i - k], -1),
T.update(1, n + 1, 1, in[i - k], in[i - k], -1e9);
if (i - k >= 0) cout << T.mx[1] << ' ';
}
cout << '\n';
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
struct segtree {
int mx[maxn << 2], lazy[maxn << 2];
void maintain(int rt) { mx[rt] = max(mx[(rt << 1)], mx[(rt << 1 | 1)]); }
void push(int rt) {
if (!lazy[rt]) return;
lazy[(rt << 1)] += lazy[rt], lazy[(rt << 1 | 1)] += lazy[rt];
mx[(rt << 1)] += lazy[rt], mx[(rt << 1 | 1)] += lazy[rt];
lazy[rt] = 0;
}
void update(int l, int r, int rt, int ql, int qr, int v) {
if (ql <= l && qr >= r) {
lazy[rt] += v;
mx[rt] += v;
return;
;
}
push(rt);
int mid = l + r >> 1;
if (ql <= mid) update(l, mid, (rt << 1), ql, qr, v);
if (qr > mid) update(mid + 1, r, (rt << 1 | 1), ql, qr, v);
maintain(rt);
}
} T;
vector<vector<int> > v;
int in[maxn], out[maxn], tot;
void dfs(int x) {
in[x] = ++tot;
for (auto &u : v[x]) dfs(u);
out[x] = tot;
}
int go[maxn], a[maxn];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
v.resize(n + 2);
for (int i = 1; i <= n; ++i) cin >> a[i], go[i] = i + 1;
for (int i = n; i >= 1; --i) {
while (go[i] <= n && a[i] >= a[go[i]]) {
go[i] = go[go[i]];
}
v[go[i]].push_back(i);
}
dfs(n + 1);
for (int i = 1; i <= n; ++i) {
T.update(1, n + 1, 1, in[i], out[i], 1);
if (i - k >= 1)
T.update(1, n + 1, 1, in[i - k], out[i - k], -1),
T.update(1, n + 1, 1, in[i - k], in[i - k], -1e9);
if (i - k >= 0) cout << T.mx[1] << ' ';
}
cout << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX / 3;
class SegmentTree {
public:
SegmentTree(int _n) : n(_n), N(_n << 1), data(N, 0), chgs(N, 0) {
h = 0;
while (1 << h <= N) {
++h;
}
}
void build(vector<int>& nums) {
assert(nums.size() <= n);
int sz = nums.size();
for (int i = 0; i < sz; ++i) {
data[i + n] = nums[i];
}
for (int i = n - 1; i > 0; --i) {
data[i] = max(data[i << 1], data[i << 1 | 1]);
}
}
void apply(int x, int v) {
data[x] += v;
if (x < n) {
chgs[x] += v;
}
}
void pull(int x) {
for (int p = x >> 1; p > 0; p >>= 1) {
if (chgs[p] == 0) {
data[p] = max(data[p << 1], data[p << 1 | 1]);
}
}
}
void push(int x) {
for (int i = h; i >= 0; --i) {
int p = x >> i;
if (chgs[p] != 0) {
apply(p << 1, chgs[p]);
apply(p << 1 | 1, chgs[p]);
chgs[p] = 0;
}
}
}
void update(int x, int v) {
x += n;
push(x);
data[x] = v;
pull(x);
}
void update(int l, int r, int v) {
l += n;
r += n;
int ll = l, rr = r;
push(ll);
push(rr);
while (l <= r) {
if (l & 0x1) {
apply(l++, v);
}
if ((r & 0x1) == 0) {
apply(r--, v);
}
l >>= 1;
r >>= 1;
}
pull(ll);
pull(rr);
}
int get(int x) {
x += n;
push(x);
return data[x];
}
int get(int l, int r) {
l += n;
r += n;
push(l);
push(r);
int res = -INF;
while (l <= r) {
if (l & 0x1) {
res = max(res, data[l++]);
}
if ((r & 0x1) == 0) {
res = max(res, data[r--]);
}
l >>= 1;
r >>= 1;
}
return res;
}
private:
int n;
int N;
int h;
vector<int> data;
vector<int> chgs;
};
class Solution {
public:
vector<int> calc(vector<int>& nums, int n, int k) {
vector<int> rights(n, n);
vector<int> stk(n + 1);
int sz = 0;
for (int i = n - 1; i >= 0; --i) {
while (sz > 0) {
int j = stk[sz - 1];
if (nums[j] <= nums[i]) {
--sz;
} else {
break;
}
}
rights[i] = sz == 0 ? n : stk[sz - 1];
stk[sz++] = i;
}
vector<vector<int>> graph(n + 1);
for (int i = 0; i < n; ++i) {
graph[rights[i]].push_back(i);
}
int rt = n;
vector<int> in(n + 1, 0);
vector<int> out(n + 1, 0);
int clk = -1;
dfs(rt, -1, clk, graph, in, out);
vector<int> vals(n + 1, -INF);
SegmentTree st(n + 100);
st.build(vals);
vector<int> res;
res.reserve(n - k + 1);
for (int i = 0; i < n; ++i) {
int idx = in[i];
st.update(idx, 0);
st.update(in[i], out[i], 1);
if (i >= k) {
st.update(in[i - k], -INF);
}
if (i + 1 >= k) {
res.push_back(st.get(0, n));
}
}
return res;
}
private:
void dfs(int u, int p, int& clk, vector<vector<int>>& graph, vector<int>& in,
vector<int>& out) {
in[u] = ++clk;
for (auto v : graph[u]) {
if (v != p) {
dfs(v, u, clk, graph, in, out);
}
}
out[u] = clk;
}
};
int main(int argc, char** argv) {
ios::sync_with_stdio(false);
cin.tie(0);
Solution sol;
int n, k;
cin >> n >> k;
vector<int> nums(n);
for (int i = 0; i < n; ++i) {
cin >> nums[i];
}
auto res = sol.calc(nums, n, k);
for (auto r : res) {
cout << r << " ";
}
cout << "\n";
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX / 3;
class SegmentTree {
public:
SegmentTree(int _n) : n(_n), N(_n << 1), data(N, 0), chgs(N, 0) {
h = 0;
while (1 << h <= N) {
++h;
}
}
void build(vector<int>& nums) {
assert(nums.size() <= n);
int sz = nums.size();
for (int i = 0; i < sz; ++i) {
data[i + n] = nums[i];
}
for (int i = n - 1; i > 0; --i) {
data[i] = max(data[i << 1], data[i << 1 | 1]);
}
}
void apply(int x, int v) {
data[x] += v;
if (x < n) {
chgs[x] += v;
}
}
void pull(int x) {
for (int p = x >> 1; p > 0; p >>= 1) {
if (chgs[p] == 0) {
data[p] = max(data[p << 1], data[p << 1 | 1]);
}
}
}
void push(int x) {
for (int i = h; i >= 0; --i) {
int p = x >> i;
if (chgs[p] != 0) {
apply(p << 1, chgs[p]);
apply(p << 1 | 1, chgs[p]);
chgs[p] = 0;
}
}
}
void update(int x, int v) {
x += n;
push(x);
data[x] = v;
pull(x);
}
void update(int l, int r, int v) {
l += n;
r += n;
int ll = l, rr = r;
push(ll);
push(rr);
while (l <= r) {
if (l & 0x1) {
apply(l++, v);
}
if ((r & 0x1) == 0) {
apply(r--, v);
}
l >>= 1;
r >>= 1;
}
pull(ll);
pull(rr);
}
int get(int x) {
x += n;
push(x);
return data[x];
}
int get(int l, int r) {
l += n;
r += n;
push(l);
push(r);
int res = -INF;
while (l <= r) {
if (l & 0x1) {
res = max(res, data[l++]);
}
if ((r & 0x1) == 0) {
res = max(res, data[r--]);
}
l >>= 1;
r >>= 1;
}
return res;
}
private:
int n;
int N;
int h;
vector<int> data;
vector<int> chgs;
};
class Solution {
public:
vector<int> calc(vector<int>& nums, int n, int k) {
vector<int> rights(n, n);
vector<int> stk(n + 1);
int sz = 0;
for (int i = n - 1; i >= 0; --i) {
while (sz > 0) {
int j = stk[sz - 1];
if (nums[j] <= nums[i]) {
--sz;
} else {
break;
}
}
rights[i] = sz == 0 ? n : stk[sz - 1];
stk[sz++] = i;
}
vector<vector<int>> graph(n + 1);
for (int i = 0; i < n; ++i) {
graph[rights[i]].push_back(i);
}
int rt = n;
vector<int> in(n + 1, 0);
vector<int> out(n + 1, 0);
int clk = -1;
dfs(rt, -1, clk, graph, in, out);
vector<int> vals(n + 1, -INF);
SegmentTree st(n + 100);
st.build(vals);
vector<int> res;
res.reserve(n - k + 1);
for (int i = 0; i < n; ++i) {
int idx = in[i];
st.update(idx, 0);
st.update(in[i], out[i], 1);
if (i >= k) {
st.update(in[i - k], -INF);
}
if (i + 1 >= k) {
res.push_back(st.get(0, n));
}
}
return res;
}
private:
void dfs(int u, int p, int& clk, vector<vector<int>>& graph, vector<int>& in,
vector<int>& out) {
in[u] = ++clk;
for (auto v : graph[u]) {
if (v != p) {
dfs(v, u, clk, graph, in, out);
}
}
out[u] = clk;
}
};
int main(int argc, char** argv) {
ios::sync_with_stdio(false);
cin.tie(0);
Solution sol;
int n, k;
cin >> n >> k;
vector<int> nums(n);
for (int i = 0; i < n; ++i) {
cin >> nums[i];
}
auto res = sol.calc(nums, n, k);
for (auto r : res) {
cout << r << " ";
}
cout << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 10;
int tree[4 * maxn], lazy[4 * maxn], seen[maxn];
int N, K, A[maxn], in[maxn], out[maxn];
vector<int> graph[maxn];
int cnt = 0;
void dfs(int u, int p) {
seen[u] = 1;
for (auto &v : graph[u]) {
if (v == p) continue;
dfs(v, u);
}
}
void dfs2(int u, int p) {
in[u] = ++cnt;
for (auto &v : graph[u]) {
if (v == p) continue;
dfs2(v, u);
}
out[u] = cnt;
}
void update(int node, int start, int end, int l, int r, int val) {
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (start != end) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
if (start > end || start > r || end < l) return;
if (start >= l && end <= r) {
tree[node] += val;
if (start != end) {
lazy[node * 2] += val;
lazy[node * 2 + 1] += val;
}
return;
}
int mid = (start + end) / 2;
update(node * 2, start, mid, l, r, val);
update(node * 2 + 1, mid + 1, end, l, r, val);
tree[node] = max(tree[node * 2], tree[node * 2 + 1]);
}
int query(int node, int start, int end, int l, int r) {
if (start > end || start > r || end < l) return 0;
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (start != end) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
if (start >= l && end <= r) return tree[node];
int mid = (start + end) / 2;
int p1 = query(node * 2, start, mid, l, r);
int p2 = query(node * 2 + 1, mid + 1, end, l, r);
return max(p1, p2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
for (int i = 1; i < N + 1; i++) cin >> A[i];
stack<pair<int, int> > st;
for (int i = N; i >= 1; i--) {
while (st.size() && st.top().first <= A[i]) st.pop();
if (st.size()) {
graph[i].push_back(st.top().second);
graph[st.top().second].push_back(i);
}
st.push({A[i], i});
}
for (int i = N; i >= 1; i--)
if (!seen[i]) {
dfs(i, i);
graph[i].push_back(0);
graph[0].push_back(i);
}
dfs2(0, 0);
for (int i = 1; i < K + 1; i++) update(1, 1, cnt, in[i], out[i], 1);
vector<int> res;
res.push_back(query(1, 1, cnt, 1, cnt));
for (int i = K + 1; i < N + 1; i++) {
update(1, 1, cnt, in[i - K], out[i - K], -1);
update(1, 1, cnt, in[i], out[i], 1);
res.push_back(query(1, 1, cnt, 1, cnt));
}
for (auto &x : res) cout << x << ' ';
cout << '\n';
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 10;
int tree[4 * maxn], lazy[4 * maxn], seen[maxn];
int N, K, A[maxn], in[maxn], out[maxn];
vector<int> graph[maxn];
int cnt = 0;
void dfs(int u, int p) {
seen[u] = 1;
for (auto &v : graph[u]) {
if (v == p) continue;
dfs(v, u);
}
}
void dfs2(int u, int p) {
in[u] = ++cnt;
for (auto &v : graph[u]) {
if (v == p) continue;
dfs2(v, u);
}
out[u] = cnt;
}
void update(int node, int start, int end, int l, int r, int val) {
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (start != end) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
if (start > end || start > r || end < l) return;
if (start >= l && end <= r) {
tree[node] += val;
if (start != end) {
lazy[node * 2] += val;
lazy[node * 2 + 1] += val;
}
return;
}
int mid = (start + end) / 2;
update(node * 2, start, mid, l, r, val);
update(node * 2 + 1, mid + 1, end, l, r, val);
tree[node] = max(tree[node * 2], tree[node * 2 + 1]);
}
int query(int node, int start, int end, int l, int r) {
if (start > end || start > r || end < l) return 0;
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (start != end) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
if (start >= l && end <= r) return tree[node];
int mid = (start + end) / 2;
int p1 = query(node * 2, start, mid, l, r);
int p2 = query(node * 2 + 1, mid + 1, end, l, r);
return max(p1, p2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
for (int i = 1; i < N + 1; i++) cin >> A[i];
stack<pair<int, int> > st;
for (int i = N; i >= 1; i--) {
while (st.size() && st.top().first <= A[i]) st.pop();
if (st.size()) {
graph[i].push_back(st.top().second);
graph[st.top().second].push_back(i);
}
st.push({A[i], i});
}
for (int i = N; i >= 1; i--)
if (!seen[i]) {
dfs(i, i);
graph[i].push_back(0);
graph[0].push_back(i);
}
dfs2(0, 0);
for (int i = 1; i < K + 1; i++) update(1, 1, cnt, in[i], out[i], 1);
vector<int> res;
res.push_back(query(1, 1, cnt, 1, cnt));
for (int i = K + 1; i < N + 1; i++) {
update(1, 1, cnt, in[i - K], out[i - K], -1);
update(1, 1, cnt, in[i], out[i], 1);
res.push_back(query(1, 1, cnt, 1, cnt));
}
for (auto &x : res) cout << x << ' ';
cout << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int t[4000050], tag[4000050];
vector<int> v[1005000];
inline void refresh(int now) { t[now] = max(t[now << 1], t[now << 1 | 1]); }
void pushdown(int l, int r, int now) {
if (l == r) {
tag[now] = 0;
return;
}
tag[now << 1] += tag[now];
tag[now << 1 | 1] += tag[now];
int mid = (l + r) >> 1;
t[now << 1] += tag[now];
t[now << 1 | 1] += tag[now];
tag[now] = 0;
}
void add(int l, int r, int now, int LL, int RR, int data) {
if (LL <= l && r <= RR) {
t[now] += data;
tag[now] += data;
return;
}
if (tag[now]) pushdown(l, r, now);
int mid = (l + r) >> 1;
if (LL <= mid) add(l, mid, now << 1, LL, RR, data);
if (mid < RR) add(mid + 1, r, now << 1 | 1, LL, RR, data);
refresh(now);
}
int query(int l, int r, int now, int LL, int RR) {
if (LL <= l && r <= RR) {
return t[now];
}
if (tag[now]) pushdown(l, r, now);
int mid = (l + r) >> 1;
int ans = -1e9;
if (LL <= mid) ans = max(query(l, mid, now << 1, LL, RR), ans);
if (mid < RR) ans = max(ans, query(mid + 1, r, now << 1 | 1, LL, RR));
refresh(now);
return ans;
}
int i, j, k, n, m, st[1005000][2], siz[1005000], id[1005000], it, sb;
void dfs(int x) {
siz[x]++;
for (auto i : v[x]) {
dfs(i);
siz[x] += siz[i];
}
id[x] = ++it;
}
int main() {
scanf("%d%d", &n, &sb);
for (i = 1; i <= n; i++) {
scanf("%d", &k);
while (m && st[m][1] < k) {
v[i].push_back(st[m][0]);
m--;
}
st[++m][0] = i;
st[m][1] = k;
}
n++;
while (m) {
v[n].push_back(st[m][0]);
m--;
}
dfs(n);
for (i = 1; i <= sb; i++) {
add(1, n, 1, id[i] - siz[i] + 1, id[i], 1);
}
for (i = sb; i < n; i++) {
printf("%d ", t[1]);
add(1, n, 1, id[i + 1] - siz[i + 1] + 1, id[i + 1], 1);
add(1, n, 1, id[i - sb + 1] - siz[i - sb + 1] + 1, id[i - sb + 1], -1);
}
}
| ### Prompt
In CPP, your task is to solve the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int t[4000050], tag[4000050];
vector<int> v[1005000];
inline void refresh(int now) { t[now] = max(t[now << 1], t[now << 1 | 1]); }
void pushdown(int l, int r, int now) {
if (l == r) {
tag[now] = 0;
return;
}
tag[now << 1] += tag[now];
tag[now << 1 | 1] += tag[now];
int mid = (l + r) >> 1;
t[now << 1] += tag[now];
t[now << 1 | 1] += tag[now];
tag[now] = 0;
}
void add(int l, int r, int now, int LL, int RR, int data) {
if (LL <= l && r <= RR) {
t[now] += data;
tag[now] += data;
return;
}
if (tag[now]) pushdown(l, r, now);
int mid = (l + r) >> 1;
if (LL <= mid) add(l, mid, now << 1, LL, RR, data);
if (mid < RR) add(mid + 1, r, now << 1 | 1, LL, RR, data);
refresh(now);
}
int query(int l, int r, int now, int LL, int RR) {
if (LL <= l && r <= RR) {
return t[now];
}
if (tag[now]) pushdown(l, r, now);
int mid = (l + r) >> 1;
int ans = -1e9;
if (LL <= mid) ans = max(query(l, mid, now << 1, LL, RR), ans);
if (mid < RR) ans = max(ans, query(mid + 1, r, now << 1 | 1, LL, RR));
refresh(now);
return ans;
}
int i, j, k, n, m, st[1005000][2], siz[1005000], id[1005000], it, sb;
void dfs(int x) {
siz[x]++;
for (auto i : v[x]) {
dfs(i);
siz[x] += siz[i];
}
id[x] = ++it;
}
int main() {
scanf("%d%d", &n, &sb);
for (i = 1; i <= n; i++) {
scanf("%d", &k);
while (m && st[m][1] < k) {
v[i].push_back(st[m][0]);
m--;
}
st[++m][0] = i;
st[m][1] = k;
}
n++;
while (m) {
v[n].push_back(st[m][0]);
m--;
}
dfs(n);
for (i = 1; i <= sb; i++) {
add(1, n, 1, id[i] - siz[i] + 1, id[i], 1);
}
for (i = sb; i < n; i++) {
printf("%d ", t[1]);
add(1, n, 1, id[i + 1] - siz[i + 1] + 1, id[i + 1], 1);
add(1, n, 1, id[i - sb + 1] - siz[i - sb + 1] + 1, id[i - sb + 1], -1);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int n, k, tim;
int a[N], nex[N], in[N], out[N];
vector<int> son[N];
template <typename T>
void read(T &x) {
int f = 0;
char c = getchar();
while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
for (x = 0; c >= '0' && c <= '9'; c = getchar())
x = (x << 3) + (x << 1) + c - '0';
if (f) x = -x;
}
namespace BIT {
int tr[N];
void init() { memset(tr, 0x3f, sizeof tr); }
void upd(int x, int v) {
for (int i = x; i; i -= i & -i) {
tr[i] = min(tr[i], v);
}
}
int query(int x) {
int ans = inf;
for (int i = x; i < N; i += i & -i) {
ans = min(ans, tr[i]);
}
return ans;
}
} // namespace BIT
namespace SEG {
int tr[N << 2], tag[N << 2];
void pushdown(int x) {
tr[x << 1] += tag[x];
tr[x << 1 | 1] += tag[x];
tag[x << 1] += tag[x];
tag[x << 1 | 1] += tag[x];
tag[x] = 0;
}
void update(int x, int l, int r, int ql, int qr, int v) {
if (ql <= l && r <= qr) {
tr[x] += v;
tag[x] += v;
return;
}
pushdown(x);
int mid = (l + r) >> 1;
if (mid >= ql) {
update(x << 1, l, mid, ql, qr, v);
}
if (mid < qr) {
update(x << 1 | 1, mid + 1, r, ql, qr, v);
}
tr[x] = max(tr[x << 1], tr[x << 1 | 1]);
}
} // namespace SEG
void dfs(int x) {
in[x] = out[x] = ++tim;
for (auto i : son[x]) {
dfs(i);
out[x] = max(out[x], out[i]);
}
}
int main() {
read(n);
read(k);
for (int i = 1; i <= n; ++i) read(a[i]);
BIT::init();
BIT::upd(n + 1, n + 1);
for (int i = n; i >= 1; --i) {
nex[i] = BIT::query(a[i]);
if (a[i] > 1) BIT::upd(a[i] - 1, i);
}
for (int i = 1; i <= n; ++i) {
son[nex[i]].push_back(i);
}
dfs(n + 1);
for (int i = 1; i <= k; ++i) {
SEG::update(1, 1, n + 1, in[i], out[i], 1);
}
printf("%d ", SEG::tr[1]);
for (int i = k + 1; i <= n; ++i) {
SEG::update(1, 1, n + 1, in[i - k], in[i - k], -inf);
SEG::update(1, 1, n + 1, in[i], out[i], 1);
printf("%d ", SEG::tr[1]);
}
puts("");
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int n, k, tim;
int a[N], nex[N], in[N], out[N];
vector<int> son[N];
template <typename T>
void read(T &x) {
int f = 0;
char c = getchar();
while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
for (x = 0; c >= '0' && c <= '9'; c = getchar())
x = (x << 3) + (x << 1) + c - '0';
if (f) x = -x;
}
namespace BIT {
int tr[N];
void init() { memset(tr, 0x3f, sizeof tr); }
void upd(int x, int v) {
for (int i = x; i; i -= i & -i) {
tr[i] = min(tr[i], v);
}
}
int query(int x) {
int ans = inf;
for (int i = x; i < N; i += i & -i) {
ans = min(ans, tr[i]);
}
return ans;
}
} // namespace BIT
namespace SEG {
int tr[N << 2], tag[N << 2];
void pushdown(int x) {
tr[x << 1] += tag[x];
tr[x << 1 | 1] += tag[x];
tag[x << 1] += tag[x];
tag[x << 1 | 1] += tag[x];
tag[x] = 0;
}
void update(int x, int l, int r, int ql, int qr, int v) {
if (ql <= l && r <= qr) {
tr[x] += v;
tag[x] += v;
return;
}
pushdown(x);
int mid = (l + r) >> 1;
if (mid >= ql) {
update(x << 1, l, mid, ql, qr, v);
}
if (mid < qr) {
update(x << 1 | 1, mid + 1, r, ql, qr, v);
}
tr[x] = max(tr[x << 1], tr[x << 1 | 1]);
}
} // namespace SEG
void dfs(int x) {
in[x] = out[x] = ++tim;
for (auto i : son[x]) {
dfs(i);
out[x] = max(out[x], out[i]);
}
}
int main() {
read(n);
read(k);
for (int i = 1; i <= n; ++i) read(a[i]);
BIT::init();
BIT::upd(n + 1, n + 1);
for (int i = n; i >= 1; --i) {
nex[i] = BIT::query(a[i]);
if (a[i] > 1) BIT::upd(a[i] - 1, i);
}
for (int i = 1; i <= n; ++i) {
son[nex[i]].push_back(i);
}
dfs(n + 1);
for (int i = 1; i <= k; ++i) {
SEG::update(1, 1, n + 1, in[i], out[i], 1);
}
printf("%d ", SEG::tr[1]);
for (int i = k + 1; i <= n; ++i) {
SEG::update(1, 1, n + 1, in[i - k], in[i - k], -inf);
SEG::update(1, 1, n + 1, in[i], out[i], 1);
printf("%d ", SEG::tr[1]);
}
puts("");
return 0;
}
``` |
#include <bits/stdc++.h>
const int MAXN = 1e6 + 10;
const double eps = 1e-8;
using namespace std;
const int inf = 1e9;
struct edge {
int t;
edge *next;
} e[MAXN << 1], *h[MAXN], *o = e;
void add(int x, int y) {
o->t = y;
o->next = h[x];
h[x] = o++;
}
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
int n, k;
int a[MAXN], key[MAXN];
int st[MAXN], tot, cnt;
int dep[MAXN], p[MAXN], num[MAXN];
void dfs(int x, int pre, int deep) {
dep[x] = deep + 1;
num[x] = 1;
p[x] = ++cnt;
key[x] = x;
for (edge *j = h[x]; j; j = j->next) {
dfs(j->t, x, deep + 1);
num[x] += num[j->t];
}
}
int maxx[MAXN << 2], tag[MAXN << 2];
void push(int x) {
if (tag[x]) {
maxx[x << 1] -= tag[x];
maxx[x << 1 | 1] -= tag[x];
tag[x << 1] += tag[x];
tag[x << 1 | 1] += tag[x];
tag[x] = 0;
}
}
void up(int x) { maxx[x] = max(maxx[x << 1], maxx[x << 1 | 1]); }
void built(int x, int l, int r) {
if (l == r) {
maxx[x] = -inf;
return;
}
int mid = (l + r) >> 1;
built(x << 1, l, mid);
built(x << 1 | 1, mid + 1, r);
up(x);
}
void update(int x, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
tag[x]++;
maxx[x]--;
return;
}
int mid = (l + r) >> 1;
push(x);
if (ql <= mid) update(x << 1, l, mid, ql, qr);
if (qr > mid) update(x << 1 | 1, mid + 1, r, ql, qr);
up(x);
}
void update1(int x, int l, int r, int t, int k) {
if (l == r) {
maxx[x] += k;
return;
}
int mid = (l + r) >> 1;
push(x);
if (t <= mid)
update1(x << 1, l, mid, t, k);
else
update1(x << 1 | 1, mid + 1, r, t, k);
up(x);
}
int ans[MAXN];
int main() {
n = read();
k = read();
key[0] = inf;
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = n; i >= 1; i--) {
while (tot && a[st[tot]] <= a[i]) tot--;
if (!tot)
add(n + 1, i);
else
add(st[tot], i);
st[++tot] = i;
}
dfs(n + 1, 0, 0);
built(1, 1, n + 1);
for (int i = n; i >= n - k + 1; i--)
update1(1, 1, n + 1, p[i], dep[i] - 1 + inf);
ans[n - k + 1] = maxx[1];
for (int i = n - k; i >= 1; i--) {
update(1, 1, n + 1, p[i + k], p[i + k] + num[i + k] - 1);
update1(1, 1, n + 1, p[i + k], -inf);
update1(1, 1, n + 1, p[i], dep[i] + inf - 1);
ans[i] = maxx[1];
}
for (int i = 1; i <= n - k + 1; i++) printf("%d ", ans[i]);
printf("\n");
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
const int MAXN = 1e6 + 10;
const double eps = 1e-8;
using namespace std;
const int inf = 1e9;
struct edge {
int t;
edge *next;
} e[MAXN << 1], *h[MAXN], *o = e;
void add(int x, int y) {
o->t = y;
o->next = h[x];
h[x] = o++;
}
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
int n, k;
int a[MAXN], key[MAXN];
int st[MAXN], tot, cnt;
int dep[MAXN], p[MAXN], num[MAXN];
void dfs(int x, int pre, int deep) {
dep[x] = deep + 1;
num[x] = 1;
p[x] = ++cnt;
key[x] = x;
for (edge *j = h[x]; j; j = j->next) {
dfs(j->t, x, deep + 1);
num[x] += num[j->t];
}
}
int maxx[MAXN << 2], tag[MAXN << 2];
void push(int x) {
if (tag[x]) {
maxx[x << 1] -= tag[x];
maxx[x << 1 | 1] -= tag[x];
tag[x << 1] += tag[x];
tag[x << 1 | 1] += tag[x];
tag[x] = 0;
}
}
void up(int x) { maxx[x] = max(maxx[x << 1], maxx[x << 1 | 1]); }
void built(int x, int l, int r) {
if (l == r) {
maxx[x] = -inf;
return;
}
int mid = (l + r) >> 1;
built(x << 1, l, mid);
built(x << 1 | 1, mid + 1, r);
up(x);
}
void update(int x, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
tag[x]++;
maxx[x]--;
return;
}
int mid = (l + r) >> 1;
push(x);
if (ql <= mid) update(x << 1, l, mid, ql, qr);
if (qr > mid) update(x << 1 | 1, mid + 1, r, ql, qr);
up(x);
}
void update1(int x, int l, int r, int t, int k) {
if (l == r) {
maxx[x] += k;
return;
}
int mid = (l + r) >> 1;
push(x);
if (t <= mid)
update1(x << 1, l, mid, t, k);
else
update1(x << 1 | 1, mid + 1, r, t, k);
up(x);
}
int ans[MAXN];
int main() {
n = read();
k = read();
key[0] = inf;
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = n; i >= 1; i--) {
while (tot && a[st[tot]] <= a[i]) tot--;
if (!tot)
add(n + 1, i);
else
add(st[tot], i);
st[++tot] = i;
}
dfs(n + 1, 0, 0);
built(1, 1, n + 1);
for (int i = n; i >= n - k + 1; i--)
update1(1, 1, n + 1, p[i], dep[i] - 1 + inf);
ans[n - k + 1] = maxx[1];
for (int i = n - k; i >= 1; i--) {
update(1, 1, n + 1, p[i + k], p[i + k] + num[i + k] - 1);
update1(1, 1, n + 1, p[i + k], -inf);
update1(1, 1, n + 1, p[i], dep[i] + inf - 1);
ans[i] = maxx[1];
}
for (int i = 1; i <= n - k + 1; i++) printf("%d ", ans[i]);
printf("\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)(1e6) + 322;
const int inf = 0;
const int mod = 1000000007;
const double eps = 1e-9;
int n, k;
int st[N], a[N], id, timer, L[N], R[N];
vector<int> v[N];
void dfs(int x, int lvl = 0, int p = -1) {
L[x] = timer++;
for (auto to : v[x]) {
if (to == p) continue;
dfs(to, lvl + 1, x);
}
R[x] = timer++;
}
struct Node {
int mx = 0;
int push = 0;
} t[N * 8];
inline void Merge(Node &acc, Node &left, Node &right) {
acc.mx = max(left.mx, right.mx);
}
inline void push(int x, int l, int r) {
if (t[x].push != -inf) {
t[x].mx += t[x].push;
if (l != r) {
t[x + x].push += t[x].push;
t[x + x + 1].push += t[x].push;
}
t[x].push = -inf;
}
}
void update(int val, int tl, int tr, int x = 1, int l = L[0], int r = R[0]) {
push(x, l, r);
if (tl > r || l > tr) return;
if (tl <= l && r <= tr) {
t[x].push += val;
push(x, l, r);
return;
}
int mid = (l + r) >> 1;
update(val, tl, tr, x + x, l, mid);
update(val, tl, tr, x + x + 1, mid + 1, r);
Merge(t[x], t[x + x], t[x + x + 1]);
}
int getmax() {
push(1, L[0], R[0]);
return t[1].mx;
}
signed main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
reverse(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i) {
while (id > 0 && a[st[id]] <= a[i]) id--;
v[i].push_back(st[id]);
v[st[id]].push_back(i);
st[++id] = i;
}
dfs(0);
for (int i = 1; i <= k; ++i) {
update(1, L[i], R[i]);
}
vector<int> ans;
ans.push_back(getmax());
for (int i = 1; i + k <= n; ++i) {
update(-1, L[i], R[i]);
update(1, L[i + k], R[i + k]);
ans.push_back(getmax());
}
reverse(ans.begin(), ans.end());
for (auto it : ans) cout << it << ' ';
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = (int)(1e6) + 322;
const int inf = 0;
const int mod = 1000000007;
const double eps = 1e-9;
int n, k;
int st[N], a[N], id, timer, L[N], R[N];
vector<int> v[N];
void dfs(int x, int lvl = 0, int p = -1) {
L[x] = timer++;
for (auto to : v[x]) {
if (to == p) continue;
dfs(to, lvl + 1, x);
}
R[x] = timer++;
}
struct Node {
int mx = 0;
int push = 0;
} t[N * 8];
inline void Merge(Node &acc, Node &left, Node &right) {
acc.mx = max(left.mx, right.mx);
}
inline void push(int x, int l, int r) {
if (t[x].push != -inf) {
t[x].mx += t[x].push;
if (l != r) {
t[x + x].push += t[x].push;
t[x + x + 1].push += t[x].push;
}
t[x].push = -inf;
}
}
void update(int val, int tl, int tr, int x = 1, int l = L[0], int r = R[0]) {
push(x, l, r);
if (tl > r || l > tr) return;
if (tl <= l && r <= tr) {
t[x].push += val;
push(x, l, r);
return;
}
int mid = (l + r) >> 1;
update(val, tl, tr, x + x, l, mid);
update(val, tl, tr, x + x + 1, mid + 1, r);
Merge(t[x], t[x + x], t[x + x + 1]);
}
int getmax() {
push(1, L[0], R[0]);
return t[1].mx;
}
signed main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
reverse(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i) {
while (id > 0 && a[st[id]] <= a[i]) id--;
v[i].push_back(st[id]);
v[st[id]].push_back(i);
st[++id] = i;
}
dfs(0);
for (int i = 1; i <= k; ++i) {
update(1, L[i], R[i]);
}
vector<int> ans;
ans.push_back(getmax());
for (int i = 1; i + k <= n; ++i) {
update(-1, L[i], R[i]);
update(1, L[i + k], R[i + k]);
ans.push_back(getmax());
}
reverse(ans.begin(), ans.end());
for (auto it : ans) cout << it << ' ';
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int N = 1e6 + 7;
const int INF = 1e9 + 7;
int t[4 * N], lz[4 * N];
int tin[N], tout[N], tim;
int a[N], p[N];
int n, k;
vector<int> g[N];
void dfs(int v) {
tin[v] = ++tim;
for (int to : g[v]) dfs(to);
tout[v] = tim;
}
void push(int v, int l, int r) {
if (l != r) {
lz[v + v] += lz[v];
lz[v + v + 1] += lz[v];
}
t[v] += lz[v];
lz[v] = 0;
}
void upd(int v, int l, int r, int ql, int qr, int val) {
push(v, l, r);
if (ql <= l && r <= qr) {
lz[v] = val;
push(v, l, r);
return;
}
if (r < ql || qr < l) return;
int mid = (l + r) >> 1;
upd(v + v, l, mid, ql, qr, val);
upd(v + v + 1, mid + 1, r, ql, qr, val);
t[v] = max(t[v + v], t[v + v + 1]);
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
stack<int> st;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (!st.empty()) {
p[i] = st.top();
g[p[i]].push_back(i);
}
st.push(i);
}
for (int i = n; i >= 1; i--)
if (!p[i]) dfs(i);
for (int i = 1; i <= k; i++) upd(1, 1, n, tin[i], tout[i], 1);
printf("%d ", t[1]);
for (int i = k + 1; i <= n; i++) {
upd(1, 1, n, tin[i - k], tout[i - k], -1);
upd(1, 1, n, tin[i], tout[i], 1);
printf("%d ", t[1]);
}
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int N = 1e6 + 7;
const int INF = 1e9 + 7;
int t[4 * N], lz[4 * N];
int tin[N], tout[N], tim;
int a[N], p[N];
int n, k;
vector<int> g[N];
void dfs(int v) {
tin[v] = ++tim;
for (int to : g[v]) dfs(to);
tout[v] = tim;
}
void push(int v, int l, int r) {
if (l != r) {
lz[v + v] += lz[v];
lz[v + v + 1] += lz[v];
}
t[v] += lz[v];
lz[v] = 0;
}
void upd(int v, int l, int r, int ql, int qr, int val) {
push(v, l, r);
if (ql <= l && r <= qr) {
lz[v] = val;
push(v, l, r);
return;
}
if (r < ql || qr < l) return;
int mid = (l + r) >> 1;
upd(v + v, l, mid, ql, qr, val);
upd(v + v + 1, mid + 1, r, ql, qr, val);
t[v] = max(t[v + v], t[v + v + 1]);
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
stack<int> st;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (!st.empty()) {
p[i] = st.top();
g[p[i]].push_back(i);
}
st.push(i);
}
for (int i = n; i >= 1; i--)
if (!p[i]) dfs(i);
for (int i = 1; i <= k; i++) upd(1, 1, n, tin[i], tout[i], 1);
printf("%d ", t[1]);
for (int i = k + 1; i <= n; i++) {
upd(1, 1, n, tin[i - k], tout[i - k], -1);
upd(1, 1, n, tin[i], tout[i], 1);
printf("%d ", t[1]);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int max;
int lazyAdd;
};
int n, k;
int sequence[1000000];
int dpNext[1000000];
vector<int> rootIndices;
vector<vector<int>> edges;
int rangeStarts[1000000];
int rangeEnds[1000000];
int nextN = 0;
vector<Node> rt;
void topologicalSort(int node) {
rangeStarts[node] = nextN;
nextN++;
for (int nextNode : edges[node]) {
topologicalSort(nextNode);
}
rangeEnds[node] = nextN;
}
void updateRangeTree(int qL, int qR, int rL, int rR, int i, int add) {
if (qL == rL && qR == rR) {
rt[i].max += add;
rt[i].lazyAdd += add;
return;
}
int rM = (rL + rR) >> 1;
int childL = (i << 1) + 1;
int childR = childL + 1;
if (rt[i].lazyAdd != 0) {
rt[childL].max += rt[i].lazyAdd;
rt[childR].max += rt[i].lazyAdd;
rt[childL].lazyAdd += rt[i].lazyAdd;
rt[childR].lazyAdd += rt[i].lazyAdd;
rt[i].lazyAdd = 0;
}
if (qL < rM) updateRangeTree(qL, min(qR, rM), rL, rM, childL, add);
if (qR > rM) updateRangeTree(max(qL, rM), qR, rM, rR, childR, add);
rt[i].max = max(rt[childL].max, rt[childR].max);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> sequence[i];
}
edges.resize(n);
for (int i = n - 1; i >= 0; i--) {
int nextIndex = i + 1;
dpNext[i] = n;
while (nextIndex != n) {
if (sequence[i] < sequence[nextIndex]) {
dpNext[i] = nextIndex;
edges[nextIndex].push_back(i);
break;
} else {
nextIndex = dpNext[nextIndex];
}
}
if (dpNext[i] == n) {
rootIndices.push_back(i);
}
}
for (int r : rootIndices) {
topologicalSort(r);
}
edges.clear();
rt.assign(1000000 << 2, {0, 0});
for (int i = 0; i < k; i++) {
updateRangeTree(rangeStarts[i], rangeEnds[i], 0, n, 0, 1);
}
cout << rt[0].max << " ";
for (int i = k; i < n; i++) {
updateRangeTree(rangeStarts[i], rangeEnds[i], 0, n, 0, 1);
updateRangeTree(rangeStarts[i - k], rangeEnds[i - k], 0, n, 0, -1);
cout << rt[0].max << " ";
}
cout << '\n';
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct Node {
int max;
int lazyAdd;
};
int n, k;
int sequence[1000000];
int dpNext[1000000];
vector<int> rootIndices;
vector<vector<int>> edges;
int rangeStarts[1000000];
int rangeEnds[1000000];
int nextN = 0;
vector<Node> rt;
void topologicalSort(int node) {
rangeStarts[node] = nextN;
nextN++;
for (int nextNode : edges[node]) {
topologicalSort(nextNode);
}
rangeEnds[node] = nextN;
}
void updateRangeTree(int qL, int qR, int rL, int rR, int i, int add) {
if (qL == rL && qR == rR) {
rt[i].max += add;
rt[i].lazyAdd += add;
return;
}
int rM = (rL + rR) >> 1;
int childL = (i << 1) + 1;
int childR = childL + 1;
if (rt[i].lazyAdd != 0) {
rt[childL].max += rt[i].lazyAdd;
rt[childR].max += rt[i].lazyAdd;
rt[childL].lazyAdd += rt[i].lazyAdd;
rt[childR].lazyAdd += rt[i].lazyAdd;
rt[i].lazyAdd = 0;
}
if (qL < rM) updateRangeTree(qL, min(qR, rM), rL, rM, childL, add);
if (qR > rM) updateRangeTree(max(qL, rM), qR, rM, rR, childR, add);
rt[i].max = max(rt[childL].max, rt[childR].max);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> sequence[i];
}
edges.resize(n);
for (int i = n - 1; i >= 0; i--) {
int nextIndex = i + 1;
dpNext[i] = n;
while (nextIndex != n) {
if (sequence[i] < sequence[nextIndex]) {
dpNext[i] = nextIndex;
edges[nextIndex].push_back(i);
break;
} else {
nextIndex = dpNext[nextIndex];
}
}
if (dpNext[i] == n) {
rootIndices.push_back(i);
}
}
for (int r : rootIndices) {
topologicalSort(r);
}
edges.clear();
rt.assign(1000000 << 2, {0, 0});
for (int i = 0; i < k; i++) {
updateRangeTree(rangeStarts[i], rangeEnds[i], 0, n, 0, 1);
}
cout << rt[0].max << " ";
for (int i = k; i < n; i++) {
updateRangeTree(rangeStarts[i], rangeEnds[i], 0, n, 0, 1);
updateRangeTree(rangeStarts[i - k], rangeEnds[i - k], 0, n, 0, -1);
cout << rt[0].max << " ";
}
cout << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);
template <class T, class S>
inline void add(T& a, S b) {
a += b;
if (a >= mod) a -= mod;
}
template <class T, class S>
inline void sub(T& a, S b) {
a -= b;
if (a < 0) a += mod;
}
template <class T, class S>
inline bool chkmax(T& a, S b) {
return a < b ? a = b, true : false;
}
template <class T, class S>
inline bool chkmin(T& a, S b) {
return a > b ? a = b, true : false;
}
int n, k, to[N], a[N];
vector<int> G[N];
int in[N], ot[N], idx;
vector<int> ans;
struct SegmentTree {
int mx[N << 2], lazy[N << 2];
inline void pull(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); }
inline void push(int rt) {
if (lazy[rt]) {
mx[rt << 1] += lazy[rt];
mx[rt << 1 | 1] += lazy[rt];
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void update(int L, int R, int val, int l, int r, int rt) {
if (R < l || r < L || R < L) return;
if (L <= l && r <= R) {
mx[rt] += val;
lazy[rt] += val;
return;
}
push(rt);
int mid = l + r >> 1;
update(L, R, val, l, mid, rt << 1);
update(L, R, val, mid + 1, r, rt << 1 | 1);
pull(rt);
}
void setVal(int p, int val, int l, int r, int rt) {
if (l == r) {
mx[rt] = val;
return;
}
push(rt);
int mid = l + r >> 1;
if (p <= mid)
setVal(p, val, l, mid, rt << 1);
else
setVal(p, val, mid + 1, r, rt << 1 | 1);
pull(rt);
}
int query(int L, int R, int l, int r, int rt) {
if (R < l || r < L || R < L) return 0;
if (L <= l && r <= R) return mx[rt];
push(rt);
int mid = l + r >> 1;
return max(query(L, R, l, mid, rt << 1),
query(L, R, mid + 1, r, rt << 1 | 1));
}
} Tree;
void dfs(int u) {
in[u] = ++idx;
for (auto& v : G[u]) dfs(v);
ot[u] = idx;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
stack<int> stk;
for (int i = n; i >= 1; i--) {
while (((int)stk.size()) && a[stk.top()] <= a[i]) stk.pop();
to[i] = ((int)stk.size()) ? stk.top() : n + 1;
stk.push(i);
G[to[i]].push_back(i);
}
dfs(n + 1);
for (int i = n; i > n - k; i--) {
Tree.setVal(in[i], Tree.query(in[to[i]], in[to[i]], 1, n + 1, 1) + 1, 1,
n + 1, 1);
}
ans.push_back(Tree.mx[1]);
for (int i = n - k; i >= 1; i--) {
Tree.update(in[i + k], ot[i + k], -1, 1, n + 1, 1);
Tree.setVal(in[i], Tree.query(in[to[i]], in[to[i]], 1, n + 1, 1) + 1, 1,
n + 1, 1);
ans.push_back(Tree.mx[1]);
}
reverse((ans).begin(), (ans).end());
for (int i = 0; i < ((int)ans.size()); i++)
printf("%d%c", ans[i], " \n"[i == ((int)ans.size()) - 1]);
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);
template <class T, class S>
inline void add(T& a, S b) {
a += b;
if (a >= mod) a -= mod;
}
template <class T, class S>
inline void sub(T& a, S b) {
a -= b;
if (a < 0) a += mod;
}
template <class T, class S>
inline bool chkmax(T& a, S b) {
return a < b ? a = b, true : false;
}
template <class T, class S>
inline bool chkmin(T& a, S b) {
return a > b ? a = b, true : false;
}
int n, k, to[N], a[N];
vector<int> G[N];
int in[N], ot[N], idx;
vector<int> ans;
struct SegmentTree {
int mx[N << 2], lazy[N << 2];
inline void pull(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); }
inline void push(int rt) {
if (lazy[rt]) {
mx[rt << 1] += lazy[rt];
mx[rt << 1 | 1] += lazy[rt];
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void update(int L, int R, int val, int l, int r, int rt) {
if (R < l || r < L || R < L) return;
if (L <= l && r <= R) {
mx[rt] += val;
lazy[rt] += val;
return;
}
push(rt);
int mid = l + r >> 1;
update(L, R, val, l, mid, rt << 1);
update(L, R, val, mid + 1, r, rt << 1 | 1);
pull(rt);
}
void setVal(int p, int val, int l, int r, int rt) {
if (l == r) {
mx[rt] = val;
return;
}
push(rt);
int mid = l + r >> 1;
if (p <= mid)
setVal(p, val, l, mid, rt << 1);
else
setVal(p, val, mid + 1, r, rt << 1 | 1);
pull(rt);
}
int query(int L, int R, int l, int r, int rt) {
if (R < l || r < L || R < L) return 0;
if (L <= l && r <= R) return mx[rt];
push(rt);
int mid = l + r >> 1;
return max(query(L, R, l, mid, rt << 1),
query(L, R, mid + 1, r, rt << 1 | 1));
}
} Tree;
void dfs(int u) {
in[u] = ++idx;
for (auto& v : G[u]) dfs(v);
ot[u] = idx;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
stack<int> stk;
for (int i = n; i >= 1; i--) {
while (((int)stk.size()) && a[stk.top()] <= a[i]) stk.pop();
to[i] = ((int)stk.size()) ? stk.top() : n + 1;
stk.push(i);
G[to[i]].push_back(i);
}
dfs(n + 1);
for (int i = n; i > n - k; i--) {
Tree.setVal(in[i], Tree.query(in[to[i]], in[to[i]], 1, n + 1, 1) + 1, 1,
n + 1, 1);
}
ans.push_back(Tree.mx[1]);
for (int i = n - k; i >= 1; i--) {
Tree.update(in[i + k], ot[i + k], -1, 1, n + 1, 1);
Tree.setVal(in[i], Tree.query(in[to[i]], in[to[i]], 1, n + 1, 1) + 1, 1,
n + 1, 1);
ans.push_back(Tree.mx[1]);
}
reverse((ans).begin(), (ans).end());
for (int i = 0; i < ((int)ans.size()); i++)
printf("%d%c", ans[i], " \n"[i == ((int)ans.size()) - 1]);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
const int maxnode = maxn << 2;
int n, kk, ai[maxn + 5], st[maxn + 5];
int in[maxn + 5], out[maxn + 5], DFN = 0;
int ans[maxn + 5], m = 0;
vector<int> G[maxn + 5];
int tr[maxnode + 5], lz[maxnode + 5];
void PushDown(int x) {
tr[(x << 1)] += lz[x], tr[(x << 1 | 1)] += lz[x];
lz[(x << 1)] += lz[x], lz[(x << 1 | 1)] += lz[x];
lz[x] = 0;
}
void Inc(int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr)
++tr[x], ++lz[x];
else {
PushDown(x);
if (ll <= ((l + r) >> 1)) Inc((x << 1), l, ((l + r) >> 1), ll, rr);
if (((l + r) >> 1) < rr) Inc((x << 1 | 1), ((l + r) >> 1) + 1, r, ll, rr);
tr[x] = max(tr[(x << 1)], tr[(x << 1 | 1)]);
}
}
void Reset(int x, int l, int r, int p) {
if (l == r)
lz[x] = 0, tr[x] = -(maxn + 10);
else {
PushDown(x);
if (p <= ((l + r) >> 1))
Reset((x << 1), l, ((l + r) >> 1), p);
else
Reset((x << 1 | 1), ((l + r) >> 1) + 1, r, p);
tr[x] = max(tr[(x << 1)], tr[(x << 1 | 1)]);
}
}
void dfs(int x) {
in[x] = ++DFN;
for (auto y : G[x]) dfs(y);
out[x] = DFN;
}
int main() {
scanf("%d%d", &n, &kk);
for (int i = 1; i <= n; ++i) scanf("%d", ai + i);
{
int top = 0;
for (int i = n; i >= 1; --i) {
while (top && ai[i] >= ai[st[top]]) --top;
if (top == 0)
G[n + 1].push_back(i);
else
G[st[top]].push_back(i);
st[++top] = i;
}
}
dfs(n + 1);
for (int i = 1; i <= n; ++i) {
Inc(1, 1, n + 1, in[i], out[i]);
if (i - kk >= 1) Reset(1, 1, n + 1, in[i - kk]);
if (i >= kk) ans[++m] = tr[1];
}
for (int i = 1; i <= m; ++i) printf("%d%c", ans[i], (i == m) ? '\n' : ' ');
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
const int maxnode = maxn << 2;
int n, kk, ai[maxn + 5], st[maxn + 5];
int in[maxn + 5], out[maxn + 5], DFN = 0;
int ans[maxn + 5], m = 0;
vector<int> G[maxn + 5];
int tr[maxnode + 5], lz[maxnode + 5];
void PushDown(int x) {
tr[(x << 1)] += lz[x], tr[(x << 1 | 1)] += lz[x];
lz[(x << 1)] += lz[x], lz[(x << 1 | 1)] += lz[x];
lz[x] = 0;
}
void Inc(int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr)
++tr[x], ++lz[x];
else {
PushDown(x);
if (ll <= ((l + r) >> 1)) Inc((x << 1), l, ((l + r) >> 1), ll, rr);
if (((l + r) >> 1) < rr) Inc((x << 1 | 1), ((l + r) >> 1) + 1, r, ll, rr);
tr[x] = max(tr[(x << 1)], tr[(x << 1 | 1)]);
}
}
void Reset(int x, int l, int r, int p) {
if (l == r)
lz[x] = 0, tr[x] = -(maxn + 10);
else {
PushDown(x);
if (p <= ((l + r) >> 1))
Reset((x << 1), l, ((l + r) >> 1), p);
else
Reset((x << 1 | 1), ((l + r) >> 1) + 1, r, p);
tr[x] = max(tr[(x << 1)], tr[(x << 1 | 1)]);
}
}
void dfs(int x) {
in[x] = ++DFN;
for (auto y : G[x]) dfs(y);
out[x] = DFN;
}
int main() {
scanf("%d%d", &n, &kk);
for (int i = 1; i <= n; ++i) scanf("%d", ai + i);
{
int top = 0;
for (int i = n; i >= 1; --i) {
while (top && ai[i] >= ai[st[top]]) --top;
if (top == 0)
G[n + 1].push_back(i);
else
G[st[top]].push_back(i);
st[++top] = i;
}
}
dfs(n + 1);
for (int i = 1; i <= n; ++i) {
Inc(1, 1, n + 1, in[i], out[i]);
if (i - kk >= 1) Reset(1, 1, n + 1, in[i - kk]);
if (i >= kk) ans[++m] = tr[1];
}
for (int i = 1; i <= m; ++i) printf("%d%c", ans[i], (i == m) ? '\n' : ' ');
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char buf[1 << 20], *p1, *p2;
inline unsigned int read() {
unsigned int s = 0, f = 1;
char ch = (p1 == p2 ? (p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin),
p1 == p2 ? EOF : *p1++)
: *p1++);
while (ch < '0' || ch > '9') {
if (ch == '-') f = -f;
ch = (p1 == p2 ? (p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin),
p1 == p2 ? EOF : *p1++)
: *p1++);
};
while (ch >= '0' && ch <= '9') {
s = (s << 1) + (s << 3) + ch - '0';
ch = (p1 == p2 ? (p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin),
p1 == p2 ? EOF : *p1++)
: *p1++);
};
return s * f;
}
const unsigned int maxn = 1e6 + 10;
unsigned int sta[maxn], top;
unsigned int nxt[maxn], las[maxn], val[maxn], a[maxn];
unsigned int tree[maxn << 2], lazy[maxn << 2];
void build(unsigned int rt, unsigned int l, unsigned int r) {
if (l == r) return tree[rt] = val[l], void();
build((rt << 1), l, ((l + r) >> 1));
build((rt << 1 | 1), ((l + r) >> 1) + 1, r);
tree[rt] = max(tree[(rt << 1)], tree[(rt << 1 | 1)]);
}
void update(unsigned int rt, unsigned int val) {
lazy[rt] += val;
tree[rt] += val;
return;
}
void push_down(unsigned int rt) {
if (!lazy[rt]) return;
update((rt << 1), lazy[rt]);
update((rt << 1 | 1), lazy[rt]);
lazy[rt] = 0;
}
unsigned int ask(unsigned int rt, unsigned int l, unsigned int r,
unsigned int s, unsigned int t) {
if (s <= l && t >= r) return tree[rt];
push_down(rt);
unsigned int Max = 0;
if (s <= ((l + r) >> 1))
Max = max(Max, ask((rt << 1), l, ((l + r) >> 1), s, t));
if (t > ((l + r) >> 1))
Max = max(Max, ask((rt << 1 | 1), ((l + r) >> 1) + 1, r, s, t));
return Max;
}
void del(unsigned int rt, unsigned int l, unsigned int r, unsigned int pos) {
if (l == r) return tree[rt] = 0, void();
push_down(rt);
if (pos <= ((l + r) >> 1))
del((rt << 1), l, ((l + r) >> 1), pos);
else
del((rt << 1 | 1), ((l + r) >> 1) + 1, r, pos);
tree[rt] = max(tree[(rt << 1)], tree[(rt << 1 | 1)]);
}
void add(unsigned int rt, unsigned int l, unsigned int r, unsigned int s,
unsigned int t, unsigned int val) {
if (s <= l && t >= r) return update(rt, val), void();
push_down(rt);
if (s <= ((l + r) >> 1)) add((rt << 1), l, ((l + r) >> 1), s, t, val);
if (t > ((l + r) >> 1)) add((rt << 1 | 1), ((l + r) >> 1) + 1, r, s, t, val);
tree[rt] = max(tree[(rt << 1)], tree[(rt << 1 | 1)]);
}
signed main() {
unsigned int n = read(), k = read();
for (unsigned int i = 1; i <= n; i++) a[i] = read();
for (unsigned int i = k; i >= 1; i--) {
while (top && a[sta[top]] <= a[i]) top--;
nxt[i] = sta[top];
sta[++top] = i;
}
for (unsigned int i = k; i >= 1; i--) val[i] = val[nxt[i]] + 1;
top = 0;
for (unsigned int i = 1; i <= n; i++) {
while (top && a[sta[top]] < a[i]) top--;
las[i] = sta[top];
sta[++top] = i;
}
build(1, 1, n);
printf("%d ", tree[1]);
del(1, 1, n, 1);
for (unsigned int i = 2; i + k - 1 <= n; i++) {
unsigned int pos = las[i + k - 1] + 1;
add(1, 1, n, max(i, pos), i + k - 1, 1);
printf("%d ", tree[1]);
del(1, 1, n, i);
}
puts("");
}
| ### Prompt
Generate a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char buf[1 << 20], *p1, *p2;
inline unsigned int read() {
unsigned int s = 0, f = 1;
char ch = (p1 == p2 ? (p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin),
p1 == p2 ? EOF : *p1++)
: *p1++);
while (ch < '0' || ch > '9') {
if (ch == '-') f = -f;
ch = (p1 == p2 ? (p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin),
p1 == p2 ? EOF : *p1++)
: *p1++);
};
while (ch >= '0' && ch <= '9') {
s = (s << 1) + (s << 3) + ch - '0';
ch = (p1 == p2 ? (p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin),
p1 == p2 ? EOF : *p1++)
: *p1++);
};
return s * f;
}
const unsigned int maxn = 1e6 + 10;
unsigned int sta[maxn], top;
unsigned int nxt[maxn], las[maxn], val[maxn], a[maxn];
unsigned int tree[maxn << 2], lazy[maxn << 2];
void build(unsigned int rt, unsigned int l, unsigned int r) {
if (l == r) return tree[rt] = val[l], void();
build((rt << 1), l, ((l + r) >> 1));
build((rt << 1 | 1), ((l + r) >> 1) + 1, r);
tree[rt] = max(tree[(rt << 1)], tree[(rt << 1 | 1)]);
}
void update(unsigned int rt, unsigned int val) {
lazy[rt] += val;
tree[rt] += val;
return;
}
void push_down(unsigned int rt) {
if (!lazy[rt]) return;
update((rt << 1), lazy[rt]);
update((rt << 1 | 1), lazy[rt]);
lazy[rt] = 0;
}
unsigned int ask(unsigned int rt, unsigned int l, unsigned int r,
unsigned int s, unsigned int t) {
if (s <= l && t >= r) return tree[rt];
push_down(rt);
unsigned int Max = 0;
if (s <= ((l + r) >> 1))
Max = max(Max, ask((rt << 1), l, ((l + r) >> 1), s, t));
if (t > ((l + r) >> 1))
Max = max(Max, ask((rt << 1 | 1), ((l + r) >> 1) + 1, r, s, t));
return Max;
}
void del(unsigned int rt, unsigned int l, unsigned int r, unsigned int pos) {
if (l == r) return tree[rt] = 0, void();
push_down(rt);
if (pos <= ((l + r) >> 1))
del((rt << 1), l, ((l + r) >> 1), pos);
else
del((rt << 1 | 1), ((l + r) >> 1) + 1, r, pos);
tree[rt] = max(tree[(rt << 1)], tree[(rt << 1 | 1)]);
}
void add(unsigned int rt, unsigned int l, unsigned int r, unsigned int s,
unsigned int t, unsigned int val) {
if (s <= l && t >= r) return update(rt, val), void();
push_down(rt);
if (s <= ((l + r) >> 1)) add((rt << 1), l, ((l + r) >> 1), s, t, val);
if (t > ((l + r) >> 1)) add((rt << 1 | 1), ((l + r) >> 1) + 1, r, s, t, val);
tree[rt] = max(tree[(rt << 1)], tree[(rt << 1 | 1)]);
}
signed main() {
unsigned int n = read(), k = read();
for (unsigned int i = 1; i <= n; i++) a[i] = read();
for (unsigned int i = k; i >= 1; i--) {
while (top && a[sta[top]] <= a[i]) top--;
nxt[i] = sta[top];
sta[++top] = i;
}
for (unsigned int i = k; i >= 1; i--) val[i] = val[nxt[i]] + 1;
top = 0;
for (unsigned int i = 1; i <= n; i++) {
while (top && a[sta[top]] < a[i]) top--;
las[i] = sta[top];
sta[++top] = i;
}
build(1, 1, n);
printf("%d ", tree[1]);
del(1, 1, n, 1);
for (unsigned int i = 2; i + k - 1 <= n; i++) {
unsigned int pos = las[i + k - 1] + 1;
add(1, 1, n, max(i, pos), i + k - 1, 1);
printf("%d ", tree[1]);
del(1, 1, n, i);
}
puts("");
}
``` |
#include <bits/stdc++.h>
const int N = 1000200;
const long long mod = 1000000007;
using namespace std;
int n;
int k;
int a[N];
int r[N];
int d[N];
int p[N];
int dip[N];
multiset<int> s[N];
vector<int> g[N];
multiset<int> res;
int get(int x) { return x == p[x] ? x : p[x] = get(p[x]); }
int best(int x) {
if (s[x].empty()) {
return 1;
}
return dip[x] - *s[x].begin() + 1;
}
void add(int x) {
if (r[x]) {
g[r[x]].push_back(x);
}
s[x].insert(dip[x]);
for (int y : g[x]) {
if (x - y >= k) {
continue;
}
int py = get(y);
res.erase(res.find(best(py)));
if (s[py].size() > s[x].size()) {
swap(s[x], s[py]);
}
for (int z : s[py]) {
s[x].insert(z);
}
s[py].clear();
p[py] = x;
}
res.insert(best(x));
}
int main() {
ios_base::sync_with_stdio(0);
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
vector<int> v;
for (int i = n; i >= 1; i--) {
p[i] = i;
while (!v.empty() && a[i] >= a[v.back()]) {
v.pop_back();
}
if (!v.empty()) {
r[i] = v.back();
dip[i] = dip[r[i]] - 1;
}
v.push_back(i);
}
for (int i = 1; i <= k; i++) {
add(i);
}
printf("%d ", *(--res.end()));
for (int i = 1; i <= n - k; i++) {
int x = get(i);
res.erase(res.find(best(x)));
if (x != i) {
s[x].erase(s[x].find(dip[i]));
res.insert(best(x));
}
add(i + k);
printf("%d ", *(--res.end()));
}
}
| ### Prompt
Create a solution in cpp for the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
const int N = 1000200;
const long long mod = 1000000007;
using namespace std;
int n;
int k;
int a[N];
int r[N];
int d[N];
int p[N];
int dip[N];
multiset<int> s[N];
vector<int> g[N];
multiset<int> res;
int get(int x) { return x == p[x] ? x : p[x] = get(p[x]); }
int best(int x) {
if (s[x].empty()) {
return 1;
}
return dip[x] - *s[x].begin() + 1;
}
void add(int x) {
if (r[x]) {
g[r[x]].push_back(x);
}
s[x].insert(dip[x]);
for (int y : g[x]) {
if (x - y >= k) {
continue;
}
int py = get(y);
res.erase(res.find(best(py)));
if (s[py].size() > s[x].size()) {
swap(s[x], s[py]);
}
for (int z : s[py]) {
s[x].insert(z);
}
s[py].clear();
p[py] = x;
}
res.insert(best(x));
}
int main() {
ios_base::sync_with_stdio(0);
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
vector<int> v;
for (int i = n; i >= 1; i--) {
p[i] = i;
while (!v.empty() && a[i] >= a[v.back()]) {
v.pop_back();
}
if (!v.empty()) {
r[i] = v.back();
dip[i] = dip[r[i]] - 1;
}
v.push_back(i);
}
for (int i = 1; i <= k; i++) {
add(i);
}
printf("%d ", *(--res.end()));
for (int i = 1; i <= n - k; i++) {
int x = get(i);
res.erase(res.find(best(x)));
if (x != i) {
s[x].erase(s[x].find(dip[i]));
res.insert(best(x));
}
add(i + k);
printf("%d ", *(--res.end()));
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int INF = 1e9;
struct SegmentTree {
int st[4 * N], lp[4 * N];
void Propagate(int k) {
int v = lp[k];
if (v == 0) return;
lp[k] = 0;
lp[k << 1] += v;
lp[k << 1 | 1] += v;
st[k << 1] += v;
st[k << 1 | 1] += v;
}
void Assign(int k, int l, int r, int i, int v) {
if (l > i || r < i) return;
if (l == r) {
st[k] = v;
return;
}
Propagate(k);
Assign(k << 1, l, ((l + r) >> 1), i, v);
Assign(k << 1 | 1, ((l + r) >> 1) + 1, r, i, v);
st[k] = max(st[k << 1], st[k << 1 | 1]);
}
void Update(int k, int l, int r, int i, int j, int v) {
if (l > j || r < i) return;
if (i <= l && r <= j) {
st[k] += v;
lp[k] += v;
return;
}
Propagate(k);
Update(k << 1, l, ((l + r) >> 1), i, j, v);
Update(k << 1 | 1, ((l + r) >> 1) + 1, r, i, j, v);
st[k] = max(st[k << 1], st[k << 1 | 1]);
}
int get(int k, int l, int r, int i) {
if (l == r) return st[k];
Propagate(k);
if (i <= ((l + r) >> 1)) return get(k << 1, l, ((l + r) >> 1), i);
return get(k << 1 | 1, ((l + r) >> 1) + 1, r, i);
}
} ST;
int n, m;
int a[N], L[N], R[N], f[N];
void Init() {
stack<int> ST;
for (int i = 1; i <= n; ++i) {
while (ST.size() && a[ST.top()] < a[i]) ST.pop();
L[i] = (ST.size() == 0 ? 1 : ST.top() + 1);
ST.push(i);
}
while (ST.size()) ST.pop();
for (int i = n; i >= 1; --i) {
while (ST.size() && a[ST.top()] <= a[i]) ST.pop();
R[i] = (ST.size() == 0 ? n : ST.top() - 1);
ST.push(i);
}
for (int i = n; i >= n - m + 1; --i) {
int j = R[i];
f[i] = (j >= n ? 1 : f[j + 1] + 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i];
Init();
for (int i = n - m + 1; i <= n; ++i) ST.Assign(1, 1, n, i, f[i]);
vector<int> ans;
for (int i = n - m + 1; i >= 1; --i) {
ans.push_back(ST.st[1]);
int r = i + m - 1;
int l = L[r];
if (l <= r - 1) ST.Update(1, 1, n, l, r - 1, -1);
ST.Assign(1, 1, n, r, -INF);
if (i - 1 >= 1) {
int j = R[i - 1];
f[i - 1] = (j + 1 >= r ? 1 : ST.get(1, 1, n, j + 1) + 1);
ST.Assign(1, 1, n, i - 1, f[i - 1]);
}
}
reverse(ans.begin(), ans.end());
for (int v : ans) cout << v << ' ';
cout << '\n';
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int INF = 1e9;
struct SegmentTree {
int st[4 * N], lp[4 * N];
void Propagate(int k) {
int v = lp[k];
if (v == 0) return;
lp[k] = 0;
lp[k << 1] += v;
lp[k << 1 | 1] += v;
st[k << 1] += v;
st[k << 1 | 1] += v;
}
void Assign(int k, int l, int r, int i, int v) {
if (l > i || r < i) return;
if (l == r) {
st[k] = v;
return;
}
Propagate(k);
Assign(k << 1, l, ((l + r) >> 1), i, v);
Assign(k << 1 | 1, ((l + r) >> 1) + 1, r, i, v);
st[k] = max(st[k << 1], st[k << 1 | 1]);
}
void Update(int k, int l, int r, int i, int j, int v) {
if (l > j || r < i) return;
if (i <= l && r <= j) {
st[k] += v;
lp[k] += v;
return;
}
Propagate(k);
Update(k << 1, l, ((l + r) >> 1), i, j, v);
Update(k << 1 | 1, ((l + r) >> 1) + 1, r, i, j, v);
st[k] = max(st[k << 1], st[k << 1 | 1]);
}
int get(int k, int l, int r, int i) {
if (l == r) return st[k];
Propagate(k);
if (i <= ((l + r) >> 1)) return get(k << 1, l, ((l + r) >> 1), i);
return get(k << 1 | 1, ((l + r) >> 1) + 1, r, i);
}
} ST;
int n, m;
int a[N], L[N], R[N], f[N];
void Init() {
stack<int> ST;
for (int i = 1; i <= n; ++i) {
while (ST.size() && a[ST.top()] < a[i]) ST.pop();
L[i] = (ST.size() == 0 ? 1 : ST.top() + 1);
ST.push(i);
}
while (ST.size()) ST.pop();
for (int i = n; i >= 1; --i) {
while (ST.size() && a[ST.top()] <= a[i]) ST.pop();
R[i] = (ST.size() == 0 ? n : ST.top() - 1);
ST.push(i);
}
for (int i = n; i >= n - m + 1; --i) {
int j = R[i];
f[i] = (j >= n ? 1 : f[j + 1] + 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i];
Init();
for (int i = n - m + 1; i <= n; ++i) ST.Assign(1, 1, n, i, f[i]);
vector<int> ans;
for (int i = n - m + 1; i >= 1; --i) {
ans.push_back(ST.st[1]);
int r = i + m - 1;
int l = L[r];
if (l <= r - 1) ST.Update(1, 1, n, l, r - 1, -1);
ST.Assign(1, 1, n, r, -INF);
if (i - 1 >= 1) {
int j = R[i - 1];
f[i - 1] = (j + 1 >= r ? 1 : ST.get(1, 1, n, j + 1) + 1);
ST.Assign(1, 1, n, i - 1, f[i - 1]);
}
}
reverse(ans.begin(), ans.end());
for (int v : ans) cout << v << ' ';
cout << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
int n, k, cnt;
vector<int> ve[maxn];
int id[maxn], sz[maxn];
int mx[maxn << 2], lz[maxn << 2];
int a[maxn];
void dfs(int u) {
sz[u] = 1;
id[u] = ++cnt;
for (int i = 0; i < ve[u].size(); i++) {
int v = ve[u][i];
dfs(v);
sz[u] += sz[v];
}
}
void update(int rt, int l, int r, int a, int b, int v) {
int m = (l + r) / 2, ls = rt << 1, rs = rt << 1 | 1;
if (lz[rt] && l != r) {
lz[ls] += lz[rt];
lz[rs] += lz[rt];
mx[ls] += lz[rt];
mx[rs] += lz[rt];
lz[rt] = 0;
}
if (l >= a && r <= b) {
lz[rt] += v;
mx[rt] += v;
return;
}
if (a <= m) update(ls, l, m, a, b, v);
if (b > m) update(rs, m + 1, r, a, b, v);
mx[rt] = max(mx[ls], mx[rs]);
}
int main() {
while (~scanf("%d%d", &n, &k)) {
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
ve[i].clear();
sz[i] = id[i] = 0;
mx[i] = lz[i] = 0;
}
stack<int> st;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[i] >= a[st.top()]) st.pop();
if (!st.empty()) ve[st.top()].push_back(i);
st.push(i);
}
cnt = 0;
for (int i = n; i >= 1; i--)
if (!id[i]) dfs(i);
for (int i = 1; i <= k; i++) update(1, 1, n, id[i], id[i] + sz[i] - 1, 1);
printf("%d", mx[1]);
for (int i = k + 1; i <= n; i++) {
update(1, 1, n, id[i - k], id[i - k], -10000000);
update(1, 1, n, id[i], id[i] + sz[i] - 1, 1);
printf(" %d", mx[1]);
}
printf("\n");
}
}
| ### Prompt
Develop a solution in CPP to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
int n, k, cnt;
vector<int> ve[maxn];
int id[maxn], sz[maxn];
int mx[maxn << 2], lz[maxn << 2];
int a[maxn];
void dfs(int u) {
sz[u] = 1;
id[u] = ++cnt;
for (int i = 0; i < ve[u].size(); i++) {
int v = ve[u][i];
dfs(v);
sz[u] += sz[v];
}
}
void update(int rt, int l, int r, int a, int b, int v) {
int m = (l + r) / 2, ls = rt << 1, rs = rt << 1 | 1;
if (lz[rt] && l != r) {
lz[ls] += lz[rt];
lz[rs] += lz[rt];
mx[ls] += lz[rt];
mx[rs] += lz[rt];
lz[rt] = 0;
}
if (l >= a && r <= b) {
lz[rt] += v;
mx[rt] += v;
return;
}
if (a <= m) update(ls, l, m, a, b, v);
if (b > m) update(rs, m + 1, r, a, b, v);
mx[rt] = max(mx[ls], mx[rs]);
}
int main() {
while (~scanf("%d%d", &n, &k)) {
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
ve[i].clear();
sz[i] = id[i] = 0;
mx[i] = lz[i] = 0;
}
stack<int> st;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[i] >= a[st.top()]) st.pop();
if (!st.empty()) ve[st.top()].push_back(i);
st.push(i);
}
cnt = 0;
for (int i = n; i >= 1; i--)
if (!id[i]) dfs(i);
for (int i = 1; i <= k; i++) update(1, 1, n, id[i], id[i] + sz[i] - 1, 1);
printf("%d", mx[1]);
for (int i = k + 1; i <= n; i++) {
update(1, 1, n, id[i - k], id[i - k], -10000000);
update(1, 1, n, id[i], id[i] + sz[i] - 1, 1);
printf(" %d", mx[1]);
}
printf("\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int mod1 = 998244353;
#pragma GCC optimize("Ofast")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
uniform_int_distribution<int> uid(l, r);
return uid(rng);
}
long long int pwr(long long int a, long long int b) {
a %= mod;
int ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
class Segtree_With_lazy {
public:
vector<int> t, lz;
int n, h;
Segtree_With_lazy(int n) : n(n) {
t.resize(2 * n, 0);
lz.resize(n, 0);
h = sizeof(int) * 8 - __builtin_clz(n);
42;
}
void apply(int p, int val) {
t[p] += val;
if (p < n) lz[p] += val;
}
void build(int p) {
while (p > 1) p >>= 1, t[p] = max(t[p << 1], t[p << 1 | 1]) + lz[p];
}
void push(int p) {
for (int s = h; s > 0; s--) {
int i = p >> s;
if (lz[i]) {
apply(i << 1, lz[i]);
apply(i << 1 | 1, lz[i]);
lz[i] = 0;
}
}
}
void inc(int l, int r, int val) {
r++;
l += n, r += n;
int l0 = l, r0 = r;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) apply(l++, val);
if (r & 1) apply(--r, val);
}
build(l0);
build(r0 - 1);
}
int query(int l, int r) {
r++;
l += n, r += n;
push(l);
push(r - 1);
int res = -2e9;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = max(res, t[l++]);
if (r & 1) res = max(res, t[--r]);
}
return res;
}
};
int tt = 0;
vector<int> s, e;
void dfs(int u, vector<vector<int>> &g) {
s[u] = tt++;
for (int v : g[u]) {
dfs(v, g);
}
e[u] = tt - 1;
}
void solve() {
int n, k;
cin >> n >> k;
42;
vector<int> a(n);
s.resize(n + 1), e.resize(n + 1);
for (int i = 0; i <= n - 1; i++) {
cin >> a[i];
}
stack<pair<int, int>> st;
st.push({1e9 + 5, n});
vector<vector<int>> g(n + 1);
for (int i = n - 1; i >= 0; i--) {
while (st.top().first <= a[i]) st.pop();
g[st.top().second].push_back(i);
st.push({a[i], i});
42;
}
dfs(n, g);
Segtree_With_lazy S(n + 1);
for (int i = 0; i <= n - 1; i++) {
S.inc(s[i], e[i], 1);
42;
if (i >= k) {
S.inc(s[i - k], s[i - k], -1e9);
}
if (i >= k - 1) {
cout << S.query(0, n) << " ";
}
}
}
signed main() {
ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
for (int tt = 1; tt <= t; tt++) {
solve();
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int mod1 = 998244353;
#pragma GCC optimize("Ofast")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
uniform_int_distribution<int> uid(l, r);
return uid(rng);
}
long long int pwr(long long int a, long long int b) {
a %= mod;
int ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
class Segtree_With_lazy {
public:
vector<int> t, lz;
int n, h;
Segtree_With_lazy(int n) : n(n) {
t.resize(2 * n, 0);
lz.resize(n, 0);
h = sizeof(int) * 8 - __builtin_clz(n);
42;
}
void apply(int p, int val) {
t[p] += val;
if (p < n) lz[p] += val;
}
void build(int p) {
while (p > 1) p >>= 1, t[p] = max(t[p << 1], t[p << 1 | 1]) + lz[p];
}
void push(int p) {
for (int s = h; s > 0; s--) {
int i = p >> s;
if (lz[i]) {
apply(i << 1, lz[i]);
apply(i << 1 | 1, lz[i]);
lz[i] = 0;
}
}
}
void inc(int l, int r, int val) {
r++;
l += n, r += n;
int l0 = l, r0 = r;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) apply(l++, val);
if (r & 1) apply(--r, val);
}
build(l0);
build(r0 - 1);
}
int query(int l, int r) {
r++;
l += n, r += n;
push(l);
push(r - 1);
int res = -2e9;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = max(res, t[l++]);
if (r & 1) res = max(res, t[--r]);
}
return res;
}
};
int tt = 0;
vector<int> s, e;
void dfs(int u, vector<vector<int>> &g) {
s[u] = tt++;
for (int v : g[u]) {
dfs(v, g);
}
e[u] = tt - 1;
}
void solve() {
int n, k;
cin >> n >> k;
42;
vector<int> a(n);
s.resize(n + 1), e.resize(n + 1);
for (int i = 0; i <= n - 1; i++) {
cin >> a[i];
}
stack<pair<int, int>> st;
st.push({1e9 + 5, n});
vector<vector<int>> g(n + 1);
for (int i = n - 1; i >= 0; i--) {
while (st.top().first <= a[i]) st.pop();
g[st.top().second].push_back(i);
st.push({a[i], i});
42;
}
dfs(n, g);
Segtree_With_lazy S(n + 1);
for (int i = 0; i <= n - 1; i++) {
S.inc(s[i], e[i], 1);
42;
if (i >= k) {
S.inc(s[i - k], s[i - k], -1e9);
}
if (i >= k - 1) {
cout << S.query(0, n) << " ";
}
}
}
signed main() {
ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
for (int tt = 1; tt <= t; tt++) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T, class T2>
inline bool chkmax(T &x, const T2 &y) {
return x < y ? x = y, 1 : 0;
}
template <class T, class T2>
inline bool chkmin(T &x, const T2 &y) {
return x > y ? x = y, 1 : 0;
}
const long long mod = 1e9 + 7;
template <class T>
inline void fix(T &x) {
if (x >= mod | x <= -mod) {
x %= mod;
}
if (x < 0) {
x += mod;
}
}
const long long MAX_N = 1e6 + 10;
struct SegTree {
long long tree[4 * MAX_N], lazy[4 * MAX_N];
SegTree() {
fill_n(tree, 4 * MAX_N, 0);
fill_n(lazy, 4 * MAX_N, 0);
}
void push(long long curr, long long l, long long r) {
tree[curr] += lazy[curr];
if (l != r) {
lazy[curr * 2] += lazy[curr];
lazy[curr * 2 + 1] += lazy[curr];
}
lazy[curr] = 0;
}
void upd(long long curr, long long l, long long r, long long ql, long long qr,
long long val) {
push(curr, l, r);
if (ql <= l && r <= qr) {
lazy[curr] += val;
push(curr, l, r);
return;
} else if (l > qr || r < ql) {
return;
}
long long m = (l + r) / 2;
upd(curr * 2, l, m, ql, qr, val);
upd(curr * 2 + 1, m + 1, r, ql, qr, val);
tree[curr] = max(tree[curr * 2], tree[curr * 2 + 1]);
}
long long ans(long long curr, long long l, long long r) {
push(curr, l, r);
return tree[curr];
}
};
vector<long long> g[MAX_N];
long long in[MAX_N], out[MAX_N], tme = 0;
long long n, k;
long long arr[MAX_N];
void dfs(long long x) {
in[x] = ++tme;
for (auto it : g[x]) {
dfs(it);
}
out[x] = tme;
}
SegTree str;
void add(long long x) { str.upd(1, 0, tme + 1, in[x], out[x], 1); }
void remove(long long x) { str.upd(1, 0, tme + 1, in[x], in[x], -mod * mod); }
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
for (long long i = 0; i < n; i++) {
cin >> arr[i];
}
stack<long long> st;
st.push(n);
arr[n] = mod;
for (long long i = n - 1; i >= 0; i--) {
while (arr[i] >= arr[st.top()]) {
st.pop();
}
g[st.top()].push_back(i);
st.push(i);
}
dfs(n);
for (long long i = 0; i < k; i++) {
add(i);
}
for (long long i = 0; i + k < n; i++) {
cout << str.ans(1, 0, tme + 1) << " ";
remove(i);
add(i + k);
}
cout << str.ans(1, 0, tme) << "\n";
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T, class T2>
inline bool chkmax(T &x, const T2 &y) {
return x < y ? x = y, 1 : 0;
}
template <class T, class T2>
inline bool chkmin(T &x, const T2 &y) {
return x > y ? x = y, 1 : 0;
}
const long long mod = 1e9 + 7;
template <class T>
inline void fix(T &x) {
if (x >= mod | x <= -mod) {
x %= mod;
}
if (x < 0) {
x += mod;
}
}
const long long MAX_N = 1e6 + 10;
struct SegTree {
long long tree[4 * MAX_N], lazy[4 * MAX_N];
SegTree() {
fill_n(tree, 4 * MAX_N, 0);
fill_n(lazy, 4 * MAX_N, 0);
}
void push(long long curr, long long l, long long r) {
tree[curr] += lazy[curr];
if (l != r) {
lazy[curr * 2] += lazy[curr];
lazy[curr * 2 + 1] += lazy[curr];
}
lazy[curr] = 0;
}
void upd(long long curr, long long l, long long r, long long ql, long long qr,
long long val) {
push(curr, l, r);
if (ql <= l && r <= qr) {
lazy[curr] += val;
push(curr, l, r);
return;
} else if (l > qr || r < ql) {
return;
}
long long m = (l + r) / 2;
upd(curr * 2, l, m, ql, qr, val);
upd(curr * 2 + 1, m + 1, r, ql, qr, val);
tree[curr] = max(tree[curr * 2], tree[curr * 2 + 1]);
}
long long ans(long long curr, long long l, long long r) {
push(curr, l, r);
return tree[curr];
}
};
vector<long long> g[MAX_N];
long long in[MAX_N], out[MAX_N], tme = 0;
long long n, k;
long long arr[MAX_N];
void dfs(long long x) {
in[x] = ++tme;
for (auto it : g[x]) {
dfs(it);
}
out[x] = tme;
}
SegTree str;
void add(long long x) { str.upd(1, 0, tme + 1, in[x], out[x], 1); }
void remove(long long x) { str.upd(1, 0, tme + 1, in[x], in[x], -mod * mod); }
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
for (long long i = 0; i < n; i++) {
cin >> arr[i];
}
stack<long long> st;
st.push(n);
arr[n] = mod;
for (long long i = n - 1; i >= 0; i--) {
while (arr[i] >= arr[st.top()]) {
st.pop();
}
g[st.top()].push_back(i);
st.push(i);
}
dfs(n);
for (long long i = 0; i < k; i++) {
add(i);
}
for (long long i = 0; i + k < n; i++) {
cout << str.ans(1, 0, tme + 1) << " ";
remove(i);
add(i + k);
}
cout << str.ans(1, 0, tme) << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int head[1000010], n, k, a[1000010], tree[4000010], add[4000010],
st[1000010][2], t = 0, o = 0, size[1000010], iid[1000010], x = 0;
struct edge {
int to, link;
} e[1000010];
void add_edge(int u, int v) { e[++o].to = v, e[o].link = head[u], head[u] = o; }
void dfs(int u) {
iid[u] = ++x;
size[u] = 1;
for (int i = head[u]; i; i = e[i].link) {
dfs(e[i].to);
size[u] += size[e[i].to];
}
}
void pushdown(int t) {
tree[t << 1] += add[t];
add[t << 1] += add[t];
tree[t << 1 | 1] += add[t];
add[t << 1 | 1] += add[t];
add[t] = 0;
}
void pushup(int t) { tree[t] = max(tree[t << 1], tree[t << 1 | 1]); }
void add_tree(int ll, int rr, int l, int r, int t, int c) {
if (ll <= l && r <= rr) {
tree[t] += c, add[t] += c;
return;
}
pushdown(t);
int mid = (l + r) >> 1;
if (mid >= ll) add_tree(ll, rr, l, mid, t << 1, c);
if (mid < rr) add_tree(ll, rr, mid + 1, r, t << 1 | 1, c);
pushup(t);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[++n] = INT_MAX;
st[++t][0] = a[n], st[t][1] = n;
for (int i = n - 1; i >= 1; i--) {
while (st[t][0] <= a[i] && t) t--;
add_edge(st[t][1], i);
st[++t][0] = a[i], st[t][1] = i;
}
dfs(n);
for (int i = 1; i <= k; i++)
add_tree(iid[i], iid[i] + size[i] - 1, 1, n, 1, 1);
printf("%d ", tree[1]);
for (int i = k + 1; i < n; i++) {
add_tree(iid[i], iid[i] + size[i] - 1, 1, n, 1, 1);
add_tree(iid[i - k], iid[i - k] + size[i - k] - 1, 1, n, 1, -1);
printf("%d ", tree[1]);
}
printf("\n");
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int head[1000010], n, k, a[1000010], tree[4000010], add[4000010],
st[1000010][2], t = 0, o = 0, size[1000010], iid[1000010], x = 0;
struct edge {
int to, link;
} e[1000010];
void add_edge(int u, int v) { e[++o].to = v, e[o].link = head[u], head[u] = o; }
void dfs(int u) {
iid[u] = ++x;
size[u] = 1;
for (int i = head[u]; i; i = e[i].link) {
dfs(e[i].to);
size[u] += size[e[i].to];
}
}
void pushdown(int t) {
tree[t << 1] += add[t];
add[t << 1] += add[t];
tree[t << 1 | 1] += add[t];
add[t << 1 | 1] += add[t];
add[t] = 0;
}
void pushup(int t) { tree[t] = max(tree[t << 1], tree[t << 1 | 1]); }
void add_tree(int ll, int rr, int l, int r, int t, int c) {
if (ll <= l && r <= rr) {
tree[t] += c, add[t] += c;
return;
}
pushdown(t);
int mid = (l + r) >> 1;
if (mid >= ll) add_tree(ll, rr, l, mid, t << 1, c);
if (mid < rr) add_tree(ll, rr, mid + 1, r, t << 1 | 1, c);
pushup(t);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[++n] = INT_MAX;
st[++t][0] = a[n], st[t][1] = n;
for (int i = n - 1; i >= 1; i--) {
while (st[t][0] <= a[i] && t) t--;
add_edge(st[t][1], i);
st[++t][0] = a[i], st[t][1] = i;
}
dfs(n);
for (int i = 1; i <= k; i++)
add_tree(iid[i], iid[i] + size[i] - 1, 1, n, 1, 1);
printf("%d ", tree[1]);
for (int i = k + 1; i < n; i++) {
add_tree(iid[i], iid[i] + size[i] - 1, 1, n, 1, 1);
add_tree(iid[i - k], iid[i - k] + size[i - k] - 1, 1, n, 1, -1);
printf("%d ", tree[1]);
}
printf("\n");
}
``` |
#include <bits/stdc++.h>
using namespace std;
int s[1000005 << 2], tag[1000005 << 2];
inline void upd(int rt, int v) { s[rt] += v, tag[rt] += v; }
inline void PushUp(int rt) { s[rt] = max(s[rt << 1], s[rt << 1 | 1]); }
inline void PushDown(int rt) {
if (tag[rt]) upd(rt << 1, tag[rt]), upd(rt << 1 | 1, tag[rt]), tag[rt] = 0;
}
void Update(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) return upd(rt, 1);
PushDown(rt);
int m = (l + r) >> 1;
if (L <= m) Update(L, R, l, m, rt << 1);
if (m < R) Update(L, R, m + 1, r, rt << 1 | 1);
PushUp(rt);
}
int query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) return s[rt];
PushDown(rt);
int m = (l + r) >> 1, ret = 0;
if (L <= m) ret = query(L, R, l, m, rt << 1);
if (m < R) ret = max(ret, query(L, R, m + 1, r, rt << 1 | 1));
return ret;
}
int sta[1000005], top, pre[1000005], a[1000005], n, k;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i; i--) {
while (top && a[sta[top]] <= a[i]) pre[sta[top]] = i, top--;
sta[++top] = i;
}
for (int i = 1; i <= n; i++) {
Update(pre[i] + 1, i, 1, n, 1);
if (i >= k) printf("%d ", query(i - k + 1, i, 1, n, 1));
}
puts("");
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int s[1000005 << 2], tag[1000005 << 2];
inline void upd(int rt, int v) { s[rt] += v, tag[rt] += v; }
inline void PushUp(int rt) { s[rt] = max(s[rt << 1], s[rt << 1 | 1]); }
inline void PushDown(int rt) {
if (tag[rt]) upd(rt << 1, tag[rt]), upd(rt << 1 | 1, tag[rt]), tag[rt] = 0;
}
void Update(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) return upd(rt, 1);
PushDown(rt);
int m = (l + r) >> 1;
if (L <= m) Update(L, R, l, m, rt << 1);
if (m < R) Update(L, R, m + 1, r, rt << 1 | 1);
PushUp(rt);
}
int query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) return s[rt];
PushDown(rt);
int m = (l + r) >> 1, ret = 0;
if (L <= m) ret = query(L, R, l, m, rt << 1);
if (m < R) ret = max(ret, query(L, R, m + 1, r, rt << 1 | 1));
return ret;
}
int sta[1000005], top, pre[1000005], a[1000005], n, k;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i; i--) {
while (top && a[sta[top]] <= a[i]) pre[sta[top]] = i, top--;
sta[++top] = i;
}
for (int i = 1; i <= n; i++) {
Update(pre[i] + 1, i, 1, n, 1);
if (i >= k) printf("%d ", query(i - k + 1, i, 1, n, 1));
}
puts("");
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, e, zx, xc, za, i, j, ii, jj, k, f[1000009], pi, p[1000009][3],
lf[1000009], rg[1000009], tim, seg[2500009], seg2[2500009], z, x, l, r,
pas[1000009], pasi;
bool bo[1000009];
deque<pair<int, int> > de;
vector<int> v[1000009];
void dfs(int q, int w) {
bo[q] = 1;
tim++;
lf[q] = rg[q] = tim;
for (vector<int>::iterator it = v[q].begin(); it != v[q].end(); it++) {
if ((*it) == w) continue;
dfs((*it), q);
if (rg[q] < rg[(*it)]) rg[q] = rg[(*it)];
}
}
void pushdown() {
if (p[pi][0] != p[pi][1]) {
seg2[p[pi][2] * 2] += seg2[p[pi][2]];
seg2[p[pi][2] * 2 + 1] += seg2[p[pi][2]];
}
seg[p[pi][2]] += seg2[p[pi][2]];
seg2[p[pi][2]] = 0;
}
void upd() {
pushdown();
if (p[pi][0] > r || p[pi][1] < l) {
pi--;
return;
}
if (p[pi][0] >= l && p[pi][1] <= r) {
seg2[p[pi][2]] += z;
pushdown();
pi--;
return;
}
pi++;
p[pi][0] = p[pi - 1][0];
p[pi][1] = (p[pi - 1][0] + p[pi - 1][1]) / 2;
p[pi][2] = p[pi - 1][2] * 2;
upd();
pi++;
p[pi][0] = (p[pi - 1][0] + p[pi - 1][1]) / 2 + 1;
p[pi][1] = p[pi - 1][1];
p[pi][2] = p[pi - 1][2] * 2 + 1;
upd();
seg[p[pi][2]] = max(seg[p[pi][2] * 2], seg[p[pi][2] * 2 + 1]);
pi--;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> a >> k;
for (i = 1; i <= a; i++) cin >> f[i];
for (i = a; i >= 1; i--) {
zx = 0;
while (de.size() != 0) {
if (de.front().first <= f[i]) {
de.pop_front();
} else {
zx = de.front().second;
break;
}
}
de.push_front(make_pair(f[i], i));
if (zx != 0) {
v[zx].push_back(i);
}
}
for (i = a; i >= 1; i--) {
if (bo[i] == 1) continue;
dfs(i, 0);
}
za = 1;
while (za < a) za *= 2;
for (i = a; i > a - k; i--) {
pi = 1;
p[pi][0] = 1;
p[pi][1] = za;
p[pi][2] = 1;
l = lf[i];
r = rg[i];
z = 1;
upd();
}
pi = 1;
p[pi][0] = 1;
p[pi][1] = za;
p[pi][2] = 1;
pushdown();
pasi++;
pas[pasi] = seg[1];
for (i = a; i > k; i--) {
pi = 1;
p[pi][0] = 1;
p[pi][1] = za;
p[pi][2] = 1;
l = lf[i];
r = rg[i];
z = -1;
upd();
pi = 1;
p[pi][0] = 1;
p[pi][1] = za;
p[pi][2] = 1;
l = lf[i - k];
r = rg[i - k];
z = 1;
upd();
pi = 1;
p[pi][0] = 1;
p[pi][1] = za;
p[pi][2] = 1;
pushdown();
pasi++;
pas[pasi] = seg[1];
}
for (i = pasi; i >= 1; i--) cout << pas[i] << " ";
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, e, zx, xc, za, i, j, ii, jj, k, f[1000009], pi, p[1000009][3],
lf[1000009], rg[1000009], tim, seg[2500009], seg2[2500009], z, x, l, r,
pas[1000009], pasi;
bool bo[1000009];
deque<pair<int, int> > de;
vector<int> v[1000009];
void dfs(int q, int w) {
bo[q] = 1;
tim++;
lf[q] = rg[q] = tim;
for (vector<int>::iterator it = v[q].begin(); it != v[q].end(); it++) {
if ((*it) == w) continue;
dfs((*it), q);
if (rg[q] < rg[(*it)]) rg[q] = rg[(*it)];
}
}
void pushdown() {
if (p[pi][0] != p[pi][1]) {
seg2[p[pi][2] * 2] += seg2[p[pi][2]];
seg2[p[pi][2] * 2 + 1] += seg2[p[pi][2]];
}
seg[p[pi][2]] += seg2[p[pi][2]];
seg2[p[pi][2]] = 0;
}
void upd() {
pushdown();
if (p[pi][0] > r || p[pi][1] < l) {
pi--;
return;
}
if (p[pi][0] >= l && p[pi][1] <= r) {
seg2[p[pi][2]] += z;
pushdown();
pi--;
return;
}
pi++;
p[pi][0] = p[pi - 1][0];
p[pi][1] = (p[pi - 1][0] + p[pi - 1][1]) / 2;
p[pi][2] = p[pi - 1][2] * 2;
upd();
pi++;
p[pi][0] = (p[pi - 1][0] + p[pi - 1][1]) / 2 + 1;
p[pi][1] = p[pi - 1][1];
p[pi][2] = p[pi - 1][2] * 2 + 1;
upd();
seg[p[pi][2]] = max(seg[p[pi][2] * 2], seg[p[pi][2] * 2 + 1]);
pi--;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> a >> k;
for (i = 1; i <= a; i++) cin >> f[i];
for (i = a; i >= 1; i--) {
zx = 0;
while (de.size() != 0) {
if (de.front().first <= f[i]) {
de.pop_front();
} else {
zx = de.front().second;
break;
}
}
de.push_front(make_pair(f[i], i));
if (zx != 0) {
v[zx].push_back(i);
}
}
for (i = a; i >= 1; i--) {
if (bo[i] == 1) continue;
dfs(i, 0);
}
za = 1;
while (za < a) za *= 2;
for (i = a; i > a - k; i--) {
pi = 1;
p[pi][0] = 1;
p[pi][1] = za;
p[pi][2] = 1;
l = lf[i];
r = rg[i];
z = 1;
upd();
}
pi = 1;
p[pi][0] = 1;
p[pi][1] = za;
p[pi][2] = 1;
pushdown();
pasi++;
pas[pasi] = seg[1];
for (i = a; i > k; i--) {
pi = 1;
p[pi][0] = 1;
p[pi][1] = za;
p[pi][2] = 1;
l = lf[i];
r = rg[i];
z = -1;
upd();
pi = 1;
p[pi][0] = 1;
p[pi][1] = za;
p[pi][2] = 1;
l = lf[i - k];
r = rg[i - k];
z = 1;
upd();
pi = 1;
p[pi][0] = 1;
p[pi][1] = za;
p[pi][2] = 1;
pushdown();
pasi++;
pas[pasi] = seg[1];
}
for (i = pasi; i >= 1; i--) cout << pas[i] << " ";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int f = 1, x = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline long long readll() {
long long f = 1, x = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int MAXN = 1e6 + 6;
int n, k, a[MAXN], pre[MAXN], sta[MAXN], t;
struct SegmentTree {
int mx[MAXN << 2], tag[MAXN << 2];
void push_down(int p) {
if (tag[p]) {
mx[p << 1] += tag[p];
mx[p << 1 | 1] += tag[p];
tag[p << 1] += tag[p];
tag[p << 1 | 1] += tag[p];
tag[p] = 0;
}
}
void push_up(int p) { mx[p] = max(mx[p << 1], mx[p << 1 | 1]); }
void modify(int p, int l, int r, int ql, int qr) {
if (ql <= l && qr >= r) {
mx[p]++;
tag[p]++;
return;
}
push_down(p);
int mid = (l + r) >> 1;
if (ql <= mid) modify(p << 1, l, mid, ql, qr);
if (qr > mid) modify(p << 1 | 1, mid + 1, r, ql, qr);
push_up(p);
}
int query(int p, int l, int r, int ql, int qr) {
if (ql <= l && qr >= r) return mx[p];
push_down(p);
int mid = (l + r) >> 1, res = 0;
if (ql <= mid) res = query(p << 1, l, mid, ql, qr);
if (qr > mid) res = max(res, query(p << 1 | 1, mid + 1, r, ql, qr));
push_up(p);
return res;
}
SegmentTree() {}
} T;
int main() {
n = read();
k = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
while (t && a[sta[t]] < a[i]) --t;
pre[i] = sta[t];
sta[++t] = i;
}
for (int i = 1; i <= n; ++i) {
T.modify(1, 1, n, pre[i] + 1, i);
if (i >= k) {
printf("%d ", T.query(1, 1, n, i - k + 1, i));
}
}
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int f = 1, x = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline long long readll() {
long long f = 1, x = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int MAXN = 1e6 + 6;
int n, k, a[MAXN], pre[MAXN], sta[MAXN], t;
struct SegmentTree {
int mx[MAXN << 2], tag[MAXN << 2];
void push_down(int p) {
if (tag[p]) {
mx[p << 1] += tag[p];
mx[p << 1 | 1] += tag[p];
tag[p << 1] += tag[p];
tag[p << 1 | 1] += tag[p];
tag[p] = 0;
}
}
void push_up(int p) { mx[p] = max(mx[p << 1], mx[p << 1 | 1]); }
void modify(int p, int l, int r, int ql, int qr) {
if (ql <= l && qr >= r) {
mx[p]++;
tag[p]++;
return;
}
push_down(p);
int mid = (l + r) >> 1;
if (ql <= mid) modify(p << 1, l, mid, ql, qr);
if (qr > mid) modify(p << 1 | 1, mid + 1, r, ql, qr);
push_up(p);
}
int query(int p, int l, int r, int ql, int qr) {
if (ql <= l && qr >= r) return mx[p];
push_down(p);
int mid = (l + r) >> 1, res = 0;
if (ql <= mid) res = query(p << 1, l, mid, ql, qr);
if (qr > mid) res = max(res, query(p << 1 | 1, mid + 1, r, ql, qr));
push_up(p);
return res;
}
SegmentTree() {}
} T;
int main() {
n = read();
k = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
while (t && a[sta[t]] < a[i]) --t;
pre[i] = sta[t];
sta[++t] = i;
}
for (int i = 1; i <= n; ++i) {
T.modify(1, 1, n, pre[i] + 1, i);
if (i >= k) {
printf("%d ", T.query(1, 1, n, i - k + 1, i));
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 1e18;
template <typename T>
void read(T &x) {
x = 0;
int s = 1, c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') s = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
x *= s;
}
template <typename T>
void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
write(x);
puts("");
}
const int maxn = 1e6 + 5;
int n, k, a[maxn], dfn[maxn], Time, size[maxn];
vector<int> adj[maxn];
class SegmentTree {
public:
struct Node {
int l, r, maxx, tag;
} t[maxn << 2];
void Push_up(int p) { t[p].maxx = max(t[p << 1].maxx, t[p << 1 | 1].maxx); }
void Push_down(int p) {
if (t[p].tag) {
t[p << 1].maxx += t[p].tag, t[p << 1 | 1].maxx += t[p].tag;
t[p << 1].tag += t[p].tag, t[p << 1 | 1].tag += t[p].tag;
t[p].tag = 0;
}
}
void Build(int l, int r, int p) {
t[p].l = l, t[p].r = r;
if (l == r) return;
int mid = (l + r) >> 1;
Build(l, mid, p << 1);
Build(mid + 1, r, p << 1 | 1);
}
void Modify(int l, int r, int p, int k) {
if (l <= t[p].l && t[p].r <= r) {
t[p].maxx += k, t[p].tag += k;
return;
}
Push_down(p);
int mid = (t[p].l + t[p].r) >> 1;
if (l <= mid) Modify(l, r, p << 1, k);
if (mid < r) Modify(l, r, p << 1 | 1, k);
Push_up(p);
}
} T;
void dfs(int fa) {
dfn[fa] = ++Time;
size[fa] = 1;
for (auto i : adj[fa]) {
dfs(i);
size[fa] += size[i];
}
}
void Pre() {
stack<int> st;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
int fa = st.empty() ? n + 1 : st.top();
adj[fa].push_back(i);
st.push(i);
}
dfs(n + 1);
}
void Solve() {
T.Build(1, n + 1, 1);
for (int i = 1; i <= k - 1; i++) T.Modify(dfn[i], dfn[i] + size[i] - 1, 1, 1);
for (int i = 1; i + k - 1 <= n; i++) {
T.Modify(dfn[i + k - 1], dfn[i + k - 1] + size[i + k - 1] - 1, 1, 1);
printf("%d ", T.t[1].maxx);
T.Modify(dfn[i], dfn[i] + size[i] - 1, 1, -1);
}
}
int main() {
read(n), read(k);
for (int i = 1; i <= n; i++) read(a[i]);
Pre();
Solve();
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 1e18;
template <typename T>
void read(T &x) {
x = 0;
int s = 1, c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') s = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
x *= s;
}
template <typename T>
void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
write(x);
puts("");
}
const int maxn = 1e6 + 5;
int n, k, a[maxn], dfn[maxn], Time, size[maxn];
vector<int> adj[maxn];
class SegmentTree {
public:
struct Node {
int l, r, maxx, tag;
} t[maxn << 2];
void Push_up(int p) { t[p].maxx = max(t[p << 1].maxx, t[p << 1 | 1].maxx); }
void Push_down(int p) {
if (t[p].tag) {
t[p << 1].maxx += t[p].tag, t[p << 1 | 1].maxx += t[p].tag;
t[p << 1].tag += t[p].tag, t[p << 1 | 1].tag += t[p].tag;
t[p].tag = 0;
}
}
void Build(int l, int r, int p) {
t[p].l = l, t[p].r = r;
if (l == r) return;
int mid = (l + r) >> 1;
Build(l, mid, p << 1);
Build(mid + 1, r, p << 1 | 1);
}
void Modify(int l, int r, int p, int k) {
if (l <= t[p].l && t[p].r <= r) {
t[p].maxx += k, t[p].tag += k;
return;
}
Push_down(p);
int mid = (t[p].l + t[p].r) >> 1;
if (l <= mid) Modify(l, r, p << 1, k);
if (mid < r) Modify(l, r, p << 1 | 1, k);
Push_up(p);
}
} T;
void dfs(int fa) {
dfn[fa] = ++Time;
size[fa] = 1;
for (auto i : adj[fa]) {
dfs(i);
size[fa] += size[i];
}
}
void Pre() {
stack<int> st;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
int fa = st.empty() ? n + 1 : st.top();
adj[fa].push_back(i);
st.push(i);
}
dfs(n + 1);
}
void Solve() {
T.Build(1, n + 1, 1);
for (int i = 1; i <= k - 1; i++) T.Modify(dfn[i], dfn[i] + size[i] - 1, 1, 1);
for (int i = 1; i + k - 1 <= n; i++) {
T.Modify(dfn[i + k - 1], dfn[i + k - 1] + size[i + k - 1] - 1, 1, 1);
printf("%d ", T.t[1].maxx);
T.Modify(dfn[i], dfn[i] + size[i] - 1, 1, -1);
}
}
int main() {
read(n), read(k);
for (int i = 1; i <= n; i++) read(a[i]);
Pre();
Solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000 + 5;
const int inf = 0x3f3f3f3f;
int n, k, dfs_clock, A[MAXN], dfn[MAXN], sz[MAXN];
vector<int> G[MAXN];
stack<pair<int, int> > s;
inline void add(int u, int v) { G[u].push_back(v); }
void dfs(int u) {
sz[u] = 1, dfn[u] = ++dfs_clock;
for (int i = 0; i < (int)G[u].size(); ++i) dfs(G[u][i]), sz[u] += sz[G[u][i]];
}
int maxv[MAXN << 2], addv[MAXN << 2];
inline void up(int o) { maxv[o] = max(maxv[(o << 1)], maxv[((o << 1) | 1)]); }
inline void Add(int o, int k) { maxv[o] += k, addv[o] += k; }
inline void down(int o) {
if (addv[o]) {
Add((o << 1), addv[o]);
Add(((o << 1) | 1), addv[o]);
addv[o] = 0;
}
}
inline void modify(int o, int l, int r, int ql, int qr, int k) {
if (qr < l || ql > r) return;
if (ql <= l && r <= qr) return Add(o, k);
down(o);
modify((o << 1), l, ((l + r) >> 1), ql, qr, k);
modify(((o << 1) | 1), ((l + r) >> 1) + 1, r, ql, qr, k);
up(o);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", A + i);
A[++n] = inf;
for (int i = 1; i <= n; ++i) {
while (!s.empty() && s.top().first < A[i]) add(i, s.top().second), s.pop();
s.push(make_pair(A[i], i));
}
dfs(n);
for (int i = 1; i <= k; ++i) modify(1, 1, n, dfn[i], dfn[i] + sz[i] - 1, 1);
printf("%d ", maxv[1]);
for (int i = k + 1; i < n; ++i) {
modify(1, 1, n, dfn[i], dfn[i] + sz[i] - 1, 1);
modify(1, 1, n, dfn[i - k], dfn[i - k] + sz[i - k] - 1, -1);
printf("%d ", maxv[1]);
}
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000 + 5;
const int inf = 0x3f3f3f3f;
int n, k, dfs_clock, A[MAXN], dfn[MAXN], sz[MAXN];
vector<int> G[MAXN];
stack<pair<int, int> > s;
inline void add(int u, int v) { G[u].push_back(v); }
void dfs(int u) {
sz[u] = 1, dfn[u] = ++dfs_clock;
for (int i = 0; i < (int)G[u].size(); ++i) dfs(G[u][i]), sz[u] += sz[G[u][i]];
}
int maxv[MAXN << 2], addv[MAXN << 2];
inline void up(int o) { maxv[o] = max(maxv[(o << 1)], maxv[((o << 1) | 1)]); }
inline void Add(int o, int k) { maxv[o] += k, addv[o] += k; }
inline void down(int o) {
if (addv[o]) {
Add((o << 1), addv[o]);
Add(((o << 1) | 1), addv[o]);
addv[o] = 0;
}
}
inline void modify(int o, int l, int r, int ql, int qr, int k) {
if (qr < l || ql > r) return;
if (ql <= l && r <= qr) return Add(o, k);
down(o);
modify((o << 1), l, ((l + r) >> 1), ql, qr, k);
modify(((o << 1) | 1), ((l + r) >> 1) + 1, r, ql, qr, k);
up(o);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", A + i);
A[++n] = inf;
for (int i = 1; i <= n; ++i) {
while (!s.empty() && s.top().first < A[i]) add(i, s.top().second), s.pop();
s.push(make_pair(A[i], i));
}
dfs(n);
for (int i = 1; i <= k; ++i) modify(1, 1, n, dfn[i], dfn[i] + sz[i] - 1, 1);
printf("%d ", maxv[1]);
for (int i = k + 1; i < n; ++i) {
modify(1, 1, n, dfn[i], dfn[i] + sz[i] - 1, 1);
modify(1, 1, n, dfn[i - k], dfn[i - k] + sz[i - k] - 1, -1);
printf("%d ", maxv[1]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int tr[1000005 * 4], mark[1000005 * 4], dfn[1000005], deep[1000005], n, k,
a[1000005], nx[1000005], sz, R[1000005], ans[1000005], cnt;
vector<int> G[1000005];
void dfs(int x) {
dfn[x] = ++sz;
for (auto u : G[x])
if (!dfn[u]) {
deep[u] = deep[x] + 1;
dfs(u);
}
R[x] = sz;
}
void insert(int p, int l, int r, int x, int u) {
if (x > r || l > x) return;
if (l == r) {
tr[p] += u;
return;
}
if (mark[p]) {
mark[p * 2] += mark[p];
mark[p * 2 + 1] += mark[p];
tr[p * 2] -= mark[p];
tr[p * 2 + 1] -= mark[p];
mark[p] = 0;
}
int mid = (l + r) / 2;
insert(p * 2, l, mid, x, u);
insert(p * 2 + 1, mid + 1, r, x, u);
tr[p] = max(tr[p * 2], tr[p * 2 + 1]);
}
void del(int p, int l, int r, int x, int y) {
if (x > r || l > y) return;
if (x <= l && r <= y) {
tr[p]--;
mark[p]++;
return;
}
if (mark[p]) {
mark[p * 2] += mark[p];
mark[p * 2 + 1] += mark[p];
tr[p * 2] -= mark[p];
tr[p * 2 + 1] -= mark[p];
mark[p] = 0;
}
int mid = (l + r) / 2;
del(p * 2, l, mid, x, y);
del(p * 2 + 1, mid + 1, r, x, y);
tr[p] = max(tr[p * 2], tr[p * 2 + 1]);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[n + 1] = n + 1;
nx[n] = n + 1;
for (int i = n - 1; i >= 1; i--) {
nx[i] = i + 1;
while (a[nx[i]] <= a[i]) nx[i] = nx[nx[i]];
G[nx[i]].push_back(i);
}
for (int i = n; i >= 1; i--)
if (!dfn[i]) {
deep[i] = 1;
dfs(i);
}
for (int i = n; i >= n - k + 1; i--) insert(1, 1, n, dfn[i], deep[i]);
ans[++cnt] = tr[1];
for (int i = n - k; i >= 1; i--) {
insert(1, 1, n, dfn[i], deep[i]);
del(1, 1, n, dfn[i + k], R[i + k]);
ans[++cnt] = tr[1];
}
for (int i = cnt; i >= 1; i--) printf("%d ", ans[i]);
}
| ### Prompt
Please create a solution in cpp to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int tr[1000005 * 4], mark[1000005 * 4], dfn[1000005], deep[1000005], n, k,
a[1000005], nx[1000005], sz, R[1000005], ans[1000005], cnt;
vector<int> G[1000005];
void dfs(int x) {
dfn[x] = ++sz;
for (auto u : G[x])
if (!dfn[u]) {
deep[u] = deep[x] + 1;
dfs(u);
}
R[x] = sz;
}
void insert(int p, int l, int r, int x, int u) {
if (x > r || l > x) return;
if (l == r) {
tr[p] += u;
return;
}
if (mark[p]) {
mark[p * 2] += mark[p];
mark[p * 2 + 1] += mark[p];
tr[p * 2] -= mark[p];
tr[p * 2 + 1] -= mark[p];
mark[p] = 0;
}
int mid = (l + r) / 2;
insert(p * 2, l, mid, x, u);
insert(p * 2 + 1, mid + 1, r, x, u);
tr[p] = max(tr[p * 2], tr[p * 2 + 1]);
}
void del(int p, int l, int r, int x, int y) {
if (x > r || l > y) return;
if (x <= l && r <= y) {
tr[p]--;
mark[p]++;
return;
}
if (mark[p]) {
mark[p * 2] += mark[p];
mark[p * 2 + 1] += mark[p];
tr[p * 2] -= mark[p];
tr[p * 2 + 1] -= mark[p];
mark[p] = 0;
}
int mid = (l + r) / 2;
del(p * 2, l, mid, x, y);
del(p * 2 + 1, mid + 1, r, x, y);
tr[p] = max(tr[p * 2], tr[p * 2 + 1]);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[n + 1] = n + 1;
nx[n] = n + 1;
for (int i = n - 1; i >= 1; i--) {
nx[i] = i + 1;
while (a[nx[i]] <= a[i]) nx[i] = nx[nx[i]];
G[nx[i]].push_back(i);
}
for (int i = n; i >= 1; i--)
if (!dfn[i]) {
deep[i] = 1;
dfs(i);
}
for (int i = n; i >= n - k + 1; i--) insert(1, 1, n, dfn[i], deep[i]);
ans[++cnt] = tr[1];
for (int i = n - k; i >= 1; i--) {
insert(1, 1, n, dfn[i], deep[i]);
del(1, 1, n, dfn[i + k], R[i + k]);
ans[++cnt] = tr[1];
}
for (int i = cnt; i >= 1; i--) printf("%d ", ans[i]);
}
``` |
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1000006;
int n, k;
int a[N];
int t[N * 4];
int laz[N * 4];
void shi(int pos) {
t[pos * 2] += laz[pos];
laz[pos * 2] += laz[pos];
t[pos * 2 + 1] += laz[pos];
laz[pos * 2 + 1] += laz[pos];
laz[pos] = 0;
}
void ubds(int tl, int tr, int x, int y, int pos) {
if (tl == tr) {
t[pos] = y;
return;
}
shi(pos);
int m = (tl + tr) / 2;
if (x <= m)
ubds(tl, m, x, y, pos * 2);
else
ubds(m + 1, tr, x, y, pos * 2 + 1);
t[pos] = min(t[pos * 2], t[pos * 2 + 1]);
}
int qrys(int tl, int tr, int l, int r, int pos) {
if (l > r) return n + 1;
if (tl == l && tr == r) return t[pos];
shi(pos);
int m = (tl + tr) / 2;
return min(qrys(tl, m, l, min(m, r), pos * 2),
qrys(m + 1, tr, max(m + 1, l), r, pos * 2 + 1));
}
void ubd0(int tl, int tr, int x, int y, int pos) {
if (tl == tr) {
t[pos] = y;
return;
}
shi(pos);
int m = (tl + tr) / 2;
if (x <= m)
ubd0(tl, m, x, y, pos * 2);
else
ubd0(m + 1, tr, x, y, pos * 2 + 1);
t[pos] = max(t[pos * 2], t[pos * 2 + 1]);
}
int qry0(int tl, int tr, int x, int pos) {
if (tl == tr) {
return t[pos];
}
shi(pos);
int m = (tl + tr) / 2;
if (x <= m)
return qry0(tl, m, x, pos * 2);
else
return qry0(m + 1, tr, x, pos * 2 + 1);
}
void ubd(int tl, int tr, int l, int r, int y, int pos) {
if (l > r) return;
if (tl == l && tr == r) {
t[pos] += y;
laz[pos] += y;
return;
}
shi(pos);
int m = (tl + tr) / 2;
ubd(tl, m, l, min(m, r), y, pos * 2);
ubd(m + 1, tr, max(m + 1, l), r, y, pos * 2 + 1);
t[pos] = max(t[pos * 2], t[pos * 2 + 1]);
}
int p[N];
vector<int> g[N];
int tin[N], tout[N], ti;
void dfs0(int x) {
tin[x] = ++ti;
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
dfs0(h);
}
tout[x] = ti;
}
void solv() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) ubds(1, n, i, n + 1, 1);
for (int i = n; i >= 1; --i) {
p[i] = qrys(1, n, a[i] + 1, n, 1);
ubds(1, n, a[i], i, 1);
}
for (int i = 1; i <= n; ++i) g[p[i]].push_back(i);
dfs0(n + 1);
for (int i = 1; i <= n + 1; ++i) ubd0(1, n + 1, i, 0, 1);
for (int i = n; i >= n - k + 1; --i) {
ubd0(1, n + 1, tin[i], qry0(1, n + 1, tin[p[i]], 1) + 1, 1);
}
vector<int> ans;
ans.push_back(t[1]);
for (int i = n - k; i >= 1; --i) {
ubd(1, n + 1, tin[i + k], tout[i + k], -1, 1);
if (p[i] <= i + k - 1)
ubd0(1, n + 1, tin[i], qry0(1, n + 1, tin[p[i]], 1) + 1, 1);
else
ubd0(1, n + 1, tin[i], 1, 1);
ans.push_back(t[1]);
}
reverse((ans).begin(), (ans).end());
for (int i = 0; i < ((int)(ans).size()); ++i) printf("%d ", ans[i]);
printf("\n");
}
int main() {
solv();
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1000006;
int n, k;
int a[N];
int t[N * 4];
int laz[N * 4];
void shi(int pos) {
t[pos * 2] += laz[pos];
laz[pos * 2] += laz[pos];
t[pos * 2 + 1] += laz[pos];
laz[pos * 2 + 1] += laz[pos];
laz[pos] = 0;
}
void ubds(int tl, int tr, int x, int y, int pos) {
if (tl == tr) {
t[pos] = y;
return;
}
shi(pos);
int m = (tl + tr) / 2;
if (x <= m)
ubds(tl, m, x, y, pos * 2);
else
ubds(m + 1, tr, x, y, pos * 2 + 1);
t[pos] = min(t[pos * 2], t[pos * 2 + 1]);
}
int qrys(int tl, int tr, int l, int r, int pos) {
if (l > r) return n + 1;
if (tl == l && tr == r) return t[pos];
shi(pos);
int m = (tl + tr) / 2;
return min(qrys(tl, m, l, min(m, r), pos * 2),
qrys(m + 1, tr, max(m + 1, l), r, pos * 2 + 1));
}
void ubd0(int tl, int tr, int x, int y, int pos) {
if (tl == tr) {
t[pos] = y;
return;
}
shi(pos);
int m = (tl + tr) / 2;
if (x <= m)
ubd0(tl, m, x, y, pos * 2);
else
ubd0(m + 1, tr, x, y, pos * 2 + 1);
t[pos] = max(t[pos * 2], t[pos * 2 + 1]);
}
int qry0(int tl, int tr, int x, int pos) {
if (tl == tr) {
return t[pos];
}
shi(pos);
int m = (tl + tr) / 2;
if (x <= m)
return qry0(tl, m, x, pos * 2);
else
return qry0(m + 1, tr, x, pos * 2 + 1);
}
void ubd(int tl, int tr, int l, int r, int y, int pos) {
if (l > r) return;
if (tl == l && tr == r) {
t[pos] += y;
laz[pos] += y;
return;
}
shi(pos);
int m = (tl + tr) / 2;
ubd(tl, m, l, min(m, r), y, pos * 2);
ubd(m + 1, tr, max(m + 1, l), r, y, pos * 2 + 1);
t[pos] = max(t[pos * 2], t[pos * 2 + 1]);
}
int p[N];
vector<int> g[N];
int tin[N], tout[N], ti;
void dfs0(int x) {
tin[x] = ++ti;
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
dfs0(h);
}
tout[x] = ti;
}
void solv() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) ubds(1, n, i, n + 1, 1);
for (int i = n; i >= 1; --i) {
p[i] = qrys(1, n, a[i] + 1, n, 1);
ubds(1, n, a[i], i, 1);
}
for (int i = 1; i <= n; ++i) g[p[i]].push_back(i);
dfs0(n + 1);
for (int i = 1; i <= n + 1; ++i) ubd0(1, n + 1, i, 0, 1);
for (int i = n; i >= n - k + 1; --i) {
ubd0(1, n + 1, tin[i], qry0(1, n + 1, tin[p[i]], 1) + 1, 1);
}
vector<int> ans;
ans.push_back(t[1]);
for (int i = n - k; i >= 1; --i) {
ubd(1, n + 1, tin[i + k], tout[i + k], -1, 1);
if (p[i] <= i + k - 1)
ubd0(1, n + 1, tin[i], qry0(1, n + 1, tin[p[i]], 1) + 1, 1);
else
ubd0(1, n + 1, tin[i], 1, 1);
ans.push_back(t[1]);
}
reverse((ans).begin(), (ans).end());
for (int i = 0; i < ((int)(ans).size()); ++i) printf("%d ", ans[i]);
printf("\n");
}
int main() {
solv();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, m, a[N], dp[N], fi[N], l, ne[N], zz[N], rk[N], size[N], tot;
struct Tree {
int l, r, num, flag;
} T1[N * 4], T2[N * 4];
void jb(int x, int y) {
ne[++tot] = fi[x];
fi[x] = tot;
zz[tot] = y;
}
void build1(int x, int l, int r) {
T1[x].l = l;
T1[x].r = r;
T1[x].num = 1e9;
if (l == r) return;
int mid = (l + r) / 2;
build1(x * 2, l, mid);
build1(x * 2 + 1, mid + 1, r);
}
void build2(int x, int l, int r) {
T2[x].l = l;
T2[x].r = r;
if (l == r) return;
int mid = (l + r) / 2;
build2(x * 2, l, mid);
build2(x * 2 + 1, mid + 1, r);
}
void insert1(int x, int y, int z) {
T1[x].num = z;
if (T1[x].l == T1[x].r) return;
int mid = (T1[x].l + T1[x].r) / 2;
if (y <= mid)
insert1(x * 2, y, z);
else
insert1(x * 2 + 1, y, z);
}
int find1(int x, int l, int r) {
if (T1[x].l > r || l > T1[x].r) return 1e9;
if (T1[x].l >= l && T1[x].r <= r) return T1[x].num;
return min(find1(x * 2, l, r), find1(x * 2 + 1, l, r));
}
void down(int x) {
T2[x * 2].num += T2[x].flag;
T2[x * 2 + 1].num += T2[x].flag;
T2[x * 2].flag += T2[x].flag;
T2[x * 2 + 1].flag += T2[x].flag;
T2[x].flag = 0;
}
void insert2(int x, int l, int r) {
if (T2[x].l > r || l > T2[x].r) return;
if (T2[x].l >= l && T2[x].r <= r) {
T2[x].num++;
T2[x].flag++;
return;
}
down(x);
insert2(x * 2, l, r);
insert2(x * 2 + 1, l, r);
T2[x].num = max(T2[x * 2].num, T2[x * 2 + 1].num);
}
void del(int x, int y) {
if (T2[x].l == T2[x].r) {
T2[x].num = -1e9;
return;
}
down(x);
int mid = (T1[x].l + T1[x].r) / 2;
if (y <= mid)
del(x * 2, y);
else
del(x * 2 + 1, y);
T2[x].num = max(T2[x * 2].num, T2[x * 2 + 1].num);
}
void dfs(int x, int y) {
size[x] = 1;
rk[x] = ++l;
for (int i = fi[x]; i; i = ne[i])
if (zz[i] != y) dfs(zz[i], x), size[x] += size[zz[i]];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
build1(1, 1, n + 1);
insert1(1, n + 1, n + 1);
for (int i = n; i; i--) {
dp[i] = find1(1, a[i] + 1, n + 1);
insert1(1, a[i], i);
jb(dp[i], i);
}
dfs(n + 1, 0);
build2(1, 1, n + 1);
for (int i = 1; i <= m; i++) insert2(1, rk[i], size[i] + rk[i] - 1);
printf("%d ", T2[1].num);
for (int i = m + 1; i <= n; i++) {
del(1, rk[i - m]);
insert2(1, rk[i], size[i] + rk[i] - 1);
printf("%d ", T2[1].num);
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, m, a[N], dp[N], fi[N], l, ne[N], zz[N], rk[N], size[N], tot;
struct Tree {
int l, r, num, flag;
} T1[N * 4], T2[N * 4];
void jb(int x, int y) {
ne[++tot] = fi[x];
fi[x] = tot;
zz[tot] = y;
}
void build1(int x, int l, int r) {
T1[x].l = l;
T1[x].r = r;
T1[x].num = 1e9;
if (l == r) return;
int mid = (l + r) / 2;
build1(x * 2, l, mid);
build1(x * 2 + 1, mid + 1, r);
}
void build2(int x, int l, int r) {
T2[x].l = l;
T2[x].r = r;
if (l == r) return;
int mid = (l + r) / 2;
build2(x * 2, l, mid);
build2(x * 2 + 1, mid + 1, r);
}
void insert1(int x, int y, int z) {
T1[x].num = z;
if (T1[x].l == T1[x].r) return;
int mid = (T1[x].l + T1[x].r) / 2;
if (y <= mid)
insert1(x * 2, y, z);
else
insert1(x * 2 + 1, y, z);
}
int find1(int x, int l, int r) {
if (T1[x].l > r || l > T1[x].r) return 1e9;
if (T1[x].l >= l && T1[x].r <= r) return T1[x].num;
return min(find1(x * 2, l, r), find1(x * 2 + 1, l, r));
}
void down(int x) {
T2[x * 2].num += T2[x].flag;
T2[x * 2 + 1].num += T2[x].flag;
T2[x * 2].flag += T2[x].flag;
T2[x * 2 + 1].flag += T2[x].flag;
T2[x].flag = 0;
}
void insert2(int x, int l, int r) {
if (T2[x].l > r || l > T2[x].r) return;
if (T2[x].l >= l && T2[x].r <= r) {
T2[x].num++;
T2[x].flag++;
return;
}
down(x);
insert2(x * 2, l, r);
insert2(x * 2 + 1, l, r);
T2[x].num = max(T2[x * 2].num, T2[x * 2 + 1].num);
}
void del(int x, int y) {
if (T2[x].l == T2[x].r) {
T2[x].num = -1e9;
return;
}
down(x);
int mid = (T1[x].l + T1[x].r) / 2;
if (y <= mid)
del(x * 2, y);
else
del(x * 2 + 1, y);
T2[x].num = max(T2[x * 2].num, T2[x * 2 + 1].num);
}
void dfs(int x, int y) {
size[x] = 1;
rk[x] = ++l;
for (int i = fi[x]; i; i = ne[i])
if (zz[i] != y) dfs(zz[i], x), size[x] += size[zz[i]];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
build1(1, 1, n + 1);
insert1(1, n + 1, n + 1);
for (int i = n; i; i--) {
dp[i] = find1(1, a[i] + 1, n + 1);
insert1(1, a[i], i);
jb(dp[i], i);
}
dfs(n + 1, 0);
build2(1, 1, n + 1);
for (int i = 1; i <= m; i++) insert2(1, rk[i], size[i] + rk[i] - 1);
printf("%d ", T2[1].num);
for (int i = m + 1; i <= n; i++) {
del(1, rk[i - m]);
insert2(1, rk[i], size[i] + rk[i] - 1);
printf("%d ", T2[1].num);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MXN = 1e6 + 5;
const int OFF = 1 << 20;
int timer;
int arr[MXN];
int sol[MXN];
int tin[MXN];
int tout[MXN];
int tree[2 * OFF];
int prop[2 * OFF];
vector<int> adj[MXN];
void update(int l, int r, int dx, int lo = 0, int hi = OFF, int x = 0) {
if (l <= lo && hi <= r) {
tree[x] += dx;
prop[x] += dx;
return;
}
int mid = (lo + hi) / 2;
if (l < mid) update(l, r, dx, lo, mid, 2 * x + 1);
if (mid < r) update(l, r, dx, mid, hi, 2 * x + 2);
tree[x] = max(tree[2 * x + 1], tree[2 * x + 2]) + prop[x];
}
void dfs(int x) {
tin[x] = timer++;
for (int y : adj[x]) {
dfs(y);
}
tout[x] = timer;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
stack<int> stk;
for (int i = n - 1; i >= 0; --i) {
while (stk.size() && arr[stk.top()] <= arr[i]) {
stk.pop();
}
if (stk.size()) {
adj[stk.top()].push_back(i);
}
stk.push(i);
}
for (int i = n - 1; i >= 0; --i) {
if (!tin[i]) {
dfs(i);
}
}
update(0, OFF, -MXN);
for (int i = n - 1; i >= 0; --i) {
if (n - i > k) {
int j = i + k;
update(tin[j], tout[j], -1);
update(tin[j], tin[j] + 1, -MXN);
}
update(tin[i], tout[i], 1);
update(tin[i], tin[i] + 1, MXN);
sol[i] = tree[0];
}
for (int i = 0; i <= n - k; ++i) {
cout << sol[i] << ' ';
}
}
| ### Prompt
Please formulate a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MXN = 1e6 + 5;
const int OFF = 1 << 20;
int timer;
int arr[MXN];
int sol[MXN];
int tin[MXN];
int tout[MXN];
int tree[2 * OFF];
int prop[2 * OFF];
vector<int> adj[MXN];
void update(int l, int r, int dx, int lo = 0, int hi = OFF, int x = 0) {
if (l <= lo && hi <= r) {
tree[x] += dx;
prop[x] += dx;
return;
}
int mid = (lo + hi) / 2;
if (l < mid) update(l, r, dx, lo, mid, 2 * x + 1);
if (mid < r) update(l, r, dx, mid, hi, 2 * x + 2);
tree[x] = max(tree[2 * x + 1], tree[2 * x + 2]) + prop[x];
}
void dfs(int x) {
tin[x] = timer++;
for (int y : adj[x]) {
dfs(y);
}
tout[x] = timer;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
stack<int> stk;
for (int i = n - 1; i >= 0; --i) {
while (stk.size() && arr[stk.top()] <= arr[i]) {
stk.pop();
}
if (stk.size()) {
adj[stk.top()].push_back(i);
}
stk.push(i);
}
for (int i = n - 1; i >= 0; --i) {
if (!tin[i]) {
dfs(i);
}
}
update(0, OFF, -MXN);
for (int i = n - 1; i >= 0; --i) {
if (n - i > k) {
int j = i + k;
update(tin[j], tout[j], -1);
update(tin[j], tin[j] + 1, -MXN);
}
update(tin[i], tout[i], 1);
update(tin[i], tin[i] + 1, MXN);
sol[i] = tree[0];
}
for (int i = 0; i <= n - k; ++i) {
cout << sol[i] << ' ';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int n, k, a[N], st[N], tp = 0, tag[N << 2], mx[N << 2];
void mdf(int x, int v) { tag[x] += v, mx[x] += v; }
void pushdown(int x) {
if (tag[x]) mdf((x << 1), tag[x]), mdf((x << 1 | 1), tag[x]), tag[x] = 0;
}
void modify(int L, int R, int v, int x = 1, int l = 1, int r = n) {
if (L <= l && r <= R) return mdf(x, v);
pushdown(x);
int mid = (l + r) >> 1;
if (L <= mid) modify(L, R, v, (x << 1), l, mid);
if (R > mid) modify(L, R, v, (x << 1 | 1), mid + 1, r);
mx[x] = max(mx[(x << 1)], mx[(x << 1 | 1)]);
}
int query(int L, int R, int x = 1, int l = 1, int r = n) {
if (L <= l && r <= R) return mx[x];
pushdown(x);
int mid = (l + r) >> 1, ret = 0;
if (L <= mid) ret = max(ret, query(L, R, (x << 1), l, mid));
if (R > mid) ret = max(ret, query(L, R, (x << 1 | 1), mid + 1, r));
return ret;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
a[i] = gi();
while (tp && a[st[tp]] < a[i]) --tp;
modify(st[tp] + 1, i, 1);
st[++tp] = i;
if (i >= k) cout << query(i - k + 1, i) << ' ';
}
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int n, k, a[N], st[N], tp = 0, tag[N << 2], mx[N << 2];
void mdf(int x, int v) { tag[x] += v, mx[x] += v; }
void pushdown(int x) {
if (tag[x]) mdf((x << 1), tag[x]), mdf((x << 1 | 1), tag[x]), tag[x] = 0;
}
void modify(int L, int R, int v, int x = 1, int l = 1, int r = n) {
if (L <= l && r <= R) return mdf(x, v);
pushdown(x);
int mid = (l + r) >> 1;
if (L <= mid) modify(L, R, v, (x << 1), l, mid);
if (R > mid) modify(L, R, v, (x << 1 | 1), mid + 1, r);
mx[x] = max(mx[(x << 1)], mx[(x << 1 | 1)]);
}
int query(int L, int R, int x = 1, int l = 1, int r = n) {
if (L <= l && r <= R) return mx[x];
pushdown(x);
int mid = (l + r) >> 1, ret = 0;
if (L <= mid) ret = max(ret, query(L, R, (x << 1), l, mid));
if (R > mid) ret = max(ret, query(L, R, (x << 1 | 1), mid + 1, r));
return ret;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
a[i] = gi();
while (tp && a[st[tp]] < a[i]) --tp;
modify(st[tp] + 1, i, 1);
st[++tp] = i;
if (i >= k) cout << query(i - k + 1, i) << ' ';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
inline int read() {
int s = 0, w = 1;
register char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
return s * w;
}
int n, K, a[N], pos[N];
vector<pair<int, int> > A;
set<int> B;
int sum[N << 2], flag[N << 2];
inline void pu(int x) { sum[x] = max(sum[(x << 1)], sum[(x << 1 | 1)]); }
inline void pd(int x) {
sum[(x << 1)] += flag[x];
sum[(x << 1 | 1)] += flag[x];
flag[(x << 1)] += flag[x];
flag[(x << 1 | 1)] += flag[x];
flag[x] = 0;
}
void UpDate(int u, int v, int l, int r, int x, int k) {
if (l >= u && r <= v) {
sum[x] += k;
flag[x] += k;
return;
}
if (flag[x]) pd(x);
int mid = (l + r) / 2;
if (u <= mid) UpDate(u, v, l, mid, (x << 1), k);
if (v > mid) UpDate(u, v, mid + 1, r, (x << 1 | 1), k);
pu(x);
}
int Ask(int u, int v, int l, int r, int x) {
if (l >= u && r <= v) return sum[x];
if (flag[x]) pd(x);
int mid = (l + r) / 2, tt = 0;
if (u <= mid) tt = max(tt, Ask(u, v, l, mid, (x << 1)));
if (v > mid) tt = max(tt, Ask(u, v, mid + 1, r, (x << 1 | 1)));
return tt;
}
signed main() {
n = read(), K = read();
for (register int i = 1; i <= n; i++)
a[i] = read(), A.emplace_back(make_pair(a[i], -i));
sort(A.begin(), A.end());
reverse(A.begin(), A.end());
B.insert(0), B.insert(n + 1);
for (register int i = 0; i < n; i++) {
int id = -A[i].second;
int tt = *(--B.lower_bound(id));
B.insert(id), pos[id] = tt + 1;
}
for (register int i = 1; i <= n; i++) {
UpDate(pos[i], i, 1, n, 1, 1);
if (i >= K) printf("%d ", Ask(i - K + 1, i, 1, n, 1));
}
puts("");
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
inline int read() {
int s = 0, w = 1;
register char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
return s * w;
}
int n, K, a[N], pos[N];
vector<pair<int, int> > A;
set<int> B;
int sum[N << 2], flag[N << 2];
inline void pu(int x) { sum[x] = max(sum[(x << 1)], sum[(x << 1 | 1)]); }
inline void pd(int x) {
sum[(x << 1)] += flag[x];
sum[(x << 1 | 1)] += flag[x];
flag[(x << 1)] += flag[x];
flag[(x << 1 | 1)] += flag[x];
flag[x] = 0;
}
void UpDate(int u, int v, int l, int r, int x, int k) {
if (l >= u && r <= v) {
sum[x] += k;
flag[x] += k;
return;
}
if (flag[x]) pd(x);
int mid = (l + r) / 2;
if (u <= mid) UpDate(u, v, l, mid, (x << 1), k);
if (v > mid) UpDate(u, v, mid + 1, r, (x << 1 | 1), k);
pu(x);
}
int Ask(int u, int v, int l, int r, int x) {
if (l >= u && r <= v) return sum[x];
if (flag[x]) pd(x);
int mid = (l + r) / 2, tt = 0;
if (u <= mid) tt = max(tt, Ask(u, v, l, mid, (x << 1)));
if (v > mid) tt = max(tt, Ask(u, v, mid + 1, r, (x << 1 | 1)));
return tt;
}
signed main() {
n = read(), K = read();
for (register int i = 1; i <= n; i++)
a[i] = read(), A.emplace_back(make_pair(a[i], -i));
sort(A.begin(), A.end());
reverse(A.begin(), A.end());
B.insert(0), B.insert(n + 1);
for (register int i = 0; i < n; i++) {
int id = -A[i].second;
int tt = *(--B.lower_bound(id));
B.insert(id), pos[id] = tt + 1;
}
for (register int i = 1; i <= n; i++) {
UpDate(pos[i], i, 1, n, 1, 1);
if (i >= K) printf("%d ", Ask(i - K + 1, i, 1, n, 1));
}
puts("");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = (1e9 + 7);
const long long INF = (1e9 + 123456789);
const long long INFL = (INF * INF);
inline long long addm(long long a, long long b, long long m = MOD) {
return ((a + b) % m);
}
inline long long subm(long long a, long long b, long long m = MOD) {
return (((a - b) % m + m) % m);
}
inline long long mulm(long long a, long long b, long long m = MOD) {
return ((a * b) % m);
}
long long powm(long long a, long long b, long long m = MOD) {
long long ret = (!b) ? 1 : powm(a, b / 2, m);
return (!b) ? 1 : mulm(mulm(ret, ret, m), b % 2 ? a : 1, m);
}
long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long inv(long long x, long long m = MOD) { return powm(x, m - 2, m); }
const int N = 1000500;
int a[N], lft[N], nxt[N];
vector<int> nodes, g[N];
struct SegmentTree {
int f, t;
SegmentTree *lc, *rc;
bool has_child, has_lazy = false;
int upd = 0;
int vmax = 0;
SegmentTree(int l, int r) {
if (l == r) {
f = t = l;
lc = rc = NULL;
has_child = false;
} else {
f = l;
t = r;
int mid = (l + r) / 2;
lc = new SegmentTree(l, mid);
rc = new SegmentTree(mid + 1, r);
has_child = true;
}
}
void change(int v) {
vmax += v;
upd += v;
has_lazy = true;
}
void push() {
if (!has_lazy) return;
lc->change(upd);
rc->change(upd);
upd = 0;
has_lazy = false;
}
void update(int l, int r, int x) {
if (r < f || l > t) return;
if (l <= f && t <= r) {
change(x);
return;
}
push();
lc->update(l, r, x);
rc->update(l, r, x);
vmax = max(lc->vmax, rc->vmax);
}
int query(int l, int r) {
if (r < f || l > t) return 0;
if (l <= f && t <= r) return vmax;
return vmax = max(lc->query(l, r), rc->query(l, r));
}
};
void solve() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
stack<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (!st.empty())
nxt[i] = st.top();
else
nxt[i] = n;
st.push(i);
}
for (int i = 0; i < n; i++) g[nxt[i]].push_back(i);
function<void(int)> post = [&](int v) {
int l = (int)(nodes).size();
for (int u : g[v]) post(u);
lft[(int)(nodes).size()] = l;
nodes.push_back(v);
};
post(n);
SegmentTree root(0, n);
for (int i = 0; i < k; i++) root.update(lft[i], i, 1);
vector<int> ans;
ans.push_back(root.query(0, n));
for (int i = k; i < n; i++) {
root.update(lft[i - k], i - k, -1);
root.update(lft[i], i, 1);
ans.push_back(root.query(0, n));
}
for (int i = 0; i < (int)(ans).size(); i++) {
if (i) cout << " ";
cout << ans[i];
}
cout << "\n";
}
int main(int argc, char *argv[]) {
cin.sync_with_stdio(0);
cin.tie(NULL);
solve();
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = (1e9 + 7);
const long long INF = (1e9 + 123456789);
const long long INFL = (INF * INF);
inline long long addm(long long a, long long b, long long m = MOD) {
return ((a + b) % m);
}
inline long long subm(long long a, long long b, long long m = MOD) {
return (((a - b) % m + m) % m);
}
inline long long mulm(long long a, long long b, long long m = MOD) {
return ((a * b) % m);
}
long long powm(long long a, long long b, long long m = MOD) {
long long ret = (!b) ? 1 : powm(a, b / 2, m);
return (!b) ? 1 : mulm(mulm(ret, ret, m), b % 2 ? a : 1, m);
}
long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long inv(long long x, long long m = MOD) { return powm(x, m - 2, m); }
const int N = 1000500;
int a[N], lft[N], nxt[N];
vector<int> nodes, g[N];
struct SegmentTree {
int f, t;
SegmentTree *lc, *rc;
bool has_child, has_lazy = false;
int upd = 0;
int vmax = 0;
SegmentTree(int l, int r) {
if (l == r) {
f = t = l;
lc = rc = NULL;
has_child = false;
} else {
f = l;
t = r;
int mid = (l + r) / 2;
lc = new SegmentTree(l, mid);
rc = new SegmentTree(mid + 1, r);
has_child = true;
}
}
void change(int v) {
vmax += v;
upd += v;
has_lazy = true;
}
void push() {
if (!has_lazy) return;
lc->change(upd);
rc->change(upd);
upd = 0;
has_lazy = false;
}
void update(int l, int r, int x) {
if (r < f || l > t) return;
if (l <= f && t <= r) {
change(x);
return;
}
push();
lc->update(l, r, x);
rc->update(l, r, x);
vmax = max(lc->vmax, rc->vmax);
}
int query(int l, int r) {
if (r < f || l > t) return 0;
if (l <= f && t <= r) return vmax;
return vmax = max(lc->query(l, r), rc->query(l, r));
}
};
void solve() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
stack<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (!st.empty())
nxt[i] = st.top();
else
nxt[i] = n;
st.push(i);
}
for (int i = 0; i < n; i++) g[nxt[i]].push_back(i);
function<void(int)> post = [&](int v) {
int l = (int)(nodes).size();
for (int u : g[v]) post(u);
lft[(int)(nodes).size()] = l;
nodes.push_back(v);
};
post(n);
SegmentTree root(0, n);
for (int i = 0; i < k; i++) root.update(lft[i], i, 1);
vector<int> ans;
ans.push_back(root.query(0, n));
for (int i = k; i < n; i++) {
root.update(lft[i - k], i - k, -1);
root.update(lft[i], i, 1);
ans.push_back(root.query(0, n));
}
for (int i = 0; i < (int)(ans).size(); i++) {
if (i) cout << " ";
cout << ans[i];
}
cout << "\n";
}
int main(int argc, char *argv[]) {
cin.sync_with_stdio(0);
cin.tie(NULL);
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
int a[MAXN], st[MAXN], en[MAXN];
vector<int> adj[MAXN];
int t = 0, tree[4 * MAXN], lazy[4 * MAXN];
void prop(int l, int r, int k) {
if (l != r) {
lazy[k * 2] += lazy[k];
lazy[k * 2 + 1] += lazy[k];
}
lazy[k] = 0;
}
int qry(int l, int r, int k, int a, int b) {
tree[k] += lazy[k];
prop(l, r, k);
if (r < a || b < l) return 0;
if (a <= l && r <= b) return tree[k];
int m = (l + r) / 2;
return max(qry(l, m, k * 2, a, b), qry(m + 1, r, k * 2 + 1, a, b));
}
void upd(int l, int r, int k, int a, int b, int v) {
tree[k] += lazy[k];
prop(l, r, k);
if (r < a || b < l) return;
if (a <= l && r <= b) {
tree[k] += v;
lazy[k] += v;
prop(l, r, k);
return;
}
int m = (l + r) / 2;
upd(l, m, k * 2, a, b, v);
upd(m + 1, r, k * 2 + 1, a, b, v);
tree[k] = max(tree[k * 2], tree[k * 2 + 1]);
}
void add(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
void dfs(int u, int p) {
st[u] = t++;
for (int v : adj[u])
if (v != p) dfs(v, u);
en[u] = t;
}
int ind[MAXN];
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 0; i < (n); i++) scanf("%d", &(a[i]));
deque<int> pq;
vector<int> root;
for (int i = n - 1; i >= 0; i--) {
while (!pq.empty() && a[i] >= a[pq.back()]) pq.pop_back();
if (!pq.empty())
add(i, pq.back());
else
root.push_back(i);
pq.push_back(i);
}
for (int i = 0; i < n; i++) sort(adj[i].begin(), adj[i].end());
reverse(root.begin(), root.end());
for (int u : root) dfs(u, -1);
for (int i = 0; i < n; i++) {
upd(0, n - 1, 1, st[i], en[i] - 1, 1);
if (i >= k - 1) {
printf("%d ", qry(0, n - 1, 1, 0, n - 1));
upd(0, n - 1, 1, st[i - k + 1], en[i - k + 1] - 1, -1);
}
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
int a[MAXN], st[MAXN], en[MAXN];
vector<int> adj[MAXN];
int t = 0, tree[4 * MAXN], lazy[4 * MAXN];
void prop(int l, int r, int k) {
if (l != r) {
lazy[k * 2] += lazy[k];
lazy[k * 2 + 1] += lazy[k];
}
lazy[k] = 0;
}
int qry(int l, int r, int k, int a, int b) {
tree[k] += lazy[k];
prop(l, r, k);
if (r < a || b < l) return 0;
if (a <= l && r <= b) return tree[k];
int m = (l + r) / 2;
return max(qry(l, m, k * 2, a, b), qry(m + 1, r, k * 2 + 1, a, b));
}
void upd(int l, int r, int k, int a, int b, int v) {
tree[k] += lazy[k];
prop(l, r, k);
if (r < a || b < l) return;
if (a <= l && r <= b) {
tree[k] += v;
lazy[k] += v;
prop(l, r, k);
return;
}
int m = (l + r) / 2;
upd(l, m, k * 2, a, b, v);
upd(m + 1, r, k * 2 + 1, a, b, v);
tree[k] = max(tree[k * 2], tree[k * 2 + 1]);
}
void add(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
void dfs(int u, int p) {
st[u] = t++;
for (int v : adj[u])
if (v != p) dfs(v, u);
en[u] = t;
}
int ind[MAXN];
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 0; i < (n); i++) scanf("%d", &(a[i]));
deque<int> pq;
vector<int> root;
for (int i = n - 1; i >= 0; i--) {
while (!pq.empty() && a[i] >= a[pq.back()]) pq.pop_back();
if (!pq.empty())
add(i, pq.back());
else
root.push_back(i);
pq.push_back(i);
}
for (int i = 0; i < n; i++) sort(adj[i].begin(), adj[i].end());
reverse(root.begin(), root.end());
for (int u : root) dfs(u, -1);
for (int i = 0; i < n; i++) {
upd(0, n - 1, 1, st[i], en[i] - 1, 1);
if (i >= k - 1) {
printf("%d ", qry(0, n - 1, 1, 0, n - 1));
upd(0, n - 1, 1, st[i - k + 1], en[i - k + 1] - 1, -1);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int better(int a, int b, const vector<int>& depth,
const vector<vector<int>>& par) {
if (depth[a] >= depth[b]) return 2 * depth.size();
int sb = b;
for (int i = 19; i >= 0; --i)
if (par[b][i] != -1 && depth[par[b][i]] >= depth[a]) b = par[b][i];
for (int i = 19; i >= 0; --i)
if (par[a][i] != -1 && par[b][i] != -1 && par[a][i] != par[b][i]) {
a = par[a][i];
b = par[b][i];
sb = par[sb][i];
}
return par[sb][0];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (auto& x : a) cin >> x;
vector<int> f(n, -1);
vector<int> depth(n, 0);
vector<int> stk;
for (int i = n - 1; i >= 0; --i) {
while (stk.size() > 0 && a[stk.back()] <= a[i]) stk.pop_back();
if (stk.size() > 0) {
f[i] = stk.back();
depth[i] = depth[f[i]] + 1;
}
stk.push_back(i);
}
vector<vector<int>> par(n, vector<int>(20, -1));
for (int i = 0; i < n; ++i) par[i][0] = f[i];
for (int s = 1; s < 20; ++s)
for (int i = 0; i < n; ++i)
if (par[i][s - 1] != -1) {
par[i][s] = par[par[i][s - 1]][s - 1];
}
deque<int> q;
vector<int> res;
for (int i = n - 1; i >= 0; --i) {
while (q.size() > 1 && better(q[1], q[0], depth, par) >= i + k) {
q.pop_front();
}
while (q.size() > 0) {
int x = better(i, q.back(), depth, par);
if (x >= i + k) {
q.pop_back();
continue;
}
if (q.size() == 1) break;
int y = better(q.back(), q[q.size() - 2], depth, par);
if (x >= y) {
q.pop_back();
continue;
}
break;
}
q.push_back(i);
if (i + k <= n) {
int x = q.front();
int cnt = 0;
for (int j = 19; j >= 0; --j) {
if (par[x][j] != -1 && par[x][j] < i + k) {
cnt += 1 << j;
x = par[x][j];
}
}
res.push_back(cnt);
}
}
for (int i = res.size() - 1; i >= 0; --i) cout << res[i] + 1 << " ";
cout << endl;
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int better(int a, int b, const vector<int>& depth,
const vector<vector<int>>& par) {
if (depth[a] >= depth[b]) return 2 * depth.size();
int sb = b;
for (int i = 19; i >= 0; --i)
if (par[b][i] != -1 && depth[par[b][i]] >= depth[a]) b = par[b][i];
for (int i = 19; i >= 0; --i)
if (par[a][i] != -1 && par[b][i] != -1 && par[a][i] != par[b][i]) {
a = par[a][i];
b = par[b][i];
sb = par[sb][i];
}
return par[sb][0];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (auto& x : a) cin >> x;
vector<int> f(n, -1);
vector<int> depth(n, 0);
vector<int> stk;
for (int i = n - 1; i >= 0; --i) {
while (stk.size() > 0 && a[stk.back()] <= a[i]) stk.pop_back();
if (stk.size() > 0) {
f[i] = stk.back();
depth[i] = depth[f[i]] + 1;
}
stk.push_back(i);
}
vector<vector<int>> par(n, vector<int>(20, -1));
for (int i = 0; i < n; ++i) par[i][0] = f[i];
for (int s = 1; s < 20; ++s)
for (int i = 0; i < n; ++i)
if (par[i][s - 1] != -1) {
par[i][s] = par[par[i][s - 1]][s - 1];
}
deque<int> q;
vector<int> res;
for (int i = n - 1; i >= 0; --i) {
while (q.size() > 1 && better(q[1], q[0], depth, par) >= i + k) {
q.pop_front();
}
while (q.size() > 0) {
int x = better(i, q.back(), depth, par);
if (x >= i + k) {
q.pop_back();
continue;
}
if (q.size() == 1) break;
int y = better(q.back(), q[q.size() - 2], depth, par);
if (x >= y) {
q.pop_back();
continue;
}
break;
}
q.push_back(i);
if (i + k <= n) {
int x = q.front();
int cnt = 0;
for (int j = 19; j >= 0; --j) {
if (par[x][j] != -1 && par[x][j] < i + k) {
cnt += 1 << j;
x = par[x][j];
}
}
res.push_back(cnt);
}
}
for (int i = res.size() - 1; i >= 0; --i) cout << res[i] + 1 << " ";
cout << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, m1, len, top, a[1000005], size[1000005], dfn[1000005], nex[1000005],
wen[1000005], hea[1000005], sta[1000005];
struct segment_tree {
int a[4000005], lazy[4000005];
void pushdown(int k) {
int t = lazy[k];
a[k << 1] += t;
lazy[k << 1] += t;
a[(k << 1) | 1] += t;
lazy[(k << 1) | 1] += t;
lazy[k] = 0;
}
void update(int l, int r, int k, int x, int y, int z) {
if (l >= x && r <= y) {
a[k] += z;
lazy[k] += z;
return;
}
pushdown(k);
int mid = (l + r) >> 1;
if (x <= mid) update(l, mid, k << 1, x, y, z);
if (y > mid) update(mid + 1, r, (k << 1) | 1, x, y, z);
a[k] = max(a[k << 1], a[(k << 1) | 1]);
}
int find(int l, int r, int k, int x, int y) {
if (l >= x && r <= y) return a[k];
pushdown(k);
int mid = (l + r) >> 1, ans = 0;
if (x <= mid) ans = find(l, mid, k << 1, x, y);
if (y > mid) ans = max(ans, find(mid + 1, r, (k << 1) | 1, x, y));
return ans;
}
} st;
void add(int x, int y) {
++len;
nex[len] = hea[x];
wen[len] = y;
hea[x] = len;
}
void dfs(int x) {
dfn[x] = ++m1;
size[x] = 1;
for (int i = hea[x]; i; i = nex[i]) {
dfs(wen[i]);
size[x] += size[wen[i]];
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[n + 1] = n + 1;
++n;
for (int i = 1; i <= n; i++) {
while (top && a[sta[top]] < a[i]) add(i, sta[top--]);
sta[++top] = i;
}
dfs(n);
for (int i = 1; i <= m; i++)
st.update(1, n, 1, dfn[i], dfn[i] + size[i] - 1, 1);
printf("%d", st.a[1]);
for (int i = m + 1; i < n; i++) {
st.update(1, n, 1, dfn[i], dfn[i] + size[i] - 1, 1);
st.update(1, n, 1, dfn[i - m], dfn[i - m] + size[i - m] - 1, -1);
printf(" %d", st.a[1]);
}
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, m1, len, top, a[1000005], size[1000005], dfn[1000005], nex[1000005],
wen[1000005], hea[1000005], sta[1000005];
struct segment_tree {
int a[4000005], lazy[4000005];
void pushdown(int k) {
int t = lazy[k];
a[k << 1] += t;
lazy[k << 1] += t;
a[(k << 1) | 1] += t;
lazy[(k << 1) | 1] += t;
lazy[k] = 0;
}
void update(int l, int r, int k, int x, int y, int z) {
if (l >= x && r <= y) {
a[k] += z;
lazy[k] += z;
return;
}
pushdown(k);
int mid = (l + r) >> 1;
if (x <= mid) update(l, mid, k << 1, x, y, z);
if (y > mid) update(mid + 1, r, (k << 1) | 1, x, y, z);
a[k] = max(a[k << 1], a[(k << 1) | 1]);
}
int find(int l, int r, int k, int x, int y) {
if (l >= x && r <= y) return a[k];
pushdown(k);
int mid = (l + r) >> 1, ans = 0;
if (x <= mid) ans = find(l, mid, k << 1, x, y);
if (y > mid) ans = max(ans, find(mid + 1, r, (k << 1) | 1, x, y));
return ans;
}
} st;
void add(int x, int y) {
++len;
nex[len] = hea[x];
wen[len] = y;
hea[x] = len;
}
void dfs(int x) {
dfn[x] = ++m1;
size[x] = 1;
for (int i = hea[x]; i; i = nex[i]) {
dfs(wen[i]);
size[x] += size[wen[i]];
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[n + 1] = n + 1;
++n;
for (int i = 1; i <= n; i++) {
while (top && a[sta[top]] < a[i]) add(i, sta[top--]);
sta[++top] = i;
}
dfs(n);
for (int i = 1; i <= m; i++)
st.update(1, n, 1, dfn[i], dfn[i] + size[i] - 1, 1);
printf("%d", st.a[1]);
for (int i = m + 1; i < n; i++) {
st.update(1, n, 1, dfn[i], dfn[i] + size[i] - 1, 1);
st.update(1, n, 1, dfn[i - m], dfn[i - m] + size[i - m] - 1, -1);
printf(" %d", st.a[1]);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000004;
int t[4 * N];
int d[4 * N];
void upd(int v, int x) {
t[v] += x;
d[v] += x;
}
void push(int v) {
if (d[v]) {
upd(2 * v + 1, d[v]);
upd(2 * v + 2, d[v]);
d[v] = 0;
}
}
void upd(int v, int l, int r, int x, int tl = 0, int tr = N) {
if (r <= tl || tr <= l) {
return;
} else if (l <= tl && tr <= r) {
upd(v, x);
} else {
push(v);
int tm = (tl + tr) / 2;
upd(2 * v + 1, l, r, x, tl, tm);
upd(2 * v + 2, l, r, x, tm, tr);
t[v] = max(t[2 * v + 1], t[2 * v + 2]);
}
}
int get(int v, int l, int r, int tl = 0, int tr = N) {
if (r <= tl || tr <= l) {
return 0;
} else if (l <= tl && tr <= r) {
return t[v];
} else {
push(v);
int tm = (tl + tr) / 2;
return max(get(2 * v + 1, l, r, tl, tm), get(2 * v + 2, l, r, tm, tr));
}
}
vector<int> g[N];
int tin[N];
int tout[N];
int timer;
void dfs(int v, int p = -1) {
tin[v] = timer++;
for (int to : g[v]) {
if (to != p) {
dfs(to, v);
}
}
tout[v] = timer;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> R(n);
for (int i = n - 1; i >= 0; i--) {
R[i] = i + 1;
while (R[i] < n && a[i] >= a[R[i]]) {
R[i] = R[R[i]];
}
g[R[i]].push_back(i);
}
dfs(n);
int j = 0;
for (int i = 0; i + k <= n; i++) {
while (j < i + k) {
upd(0, tin[j], tout[j], 1);
++j;
}
cout << get(0, 0, N) << ' ';
upd(0, tin[i], tin[i] + 1, -N);
}
cout << '\n';
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1000004;
int t[4 * N];
int d[4 * N];
void upd(int v, int x) {
t[v] += x;
d[v] += x;
}
void push(int v) {
if (d[v]) {
upd(2 * v + 1, d[v]);
upd(2 * v + 2, d[v]);
d[v] = 0;
}
}
void upd(int v, int l, int r, int x, int tl = 0, int tr = N) {
if (r <= tl || tr <= l) {
return;
} else if (l <= tl && tr <= r) {
upd(v, x);
} else {
push(v);
int tm = (tl + tr) / 2;
upd(2 * v + 1, l, r, x, tl, tm);
upd(2 * v + 2, l, r, x, tm, tr);
t[v] = max(t[2 * v + 1], t[2 * v + 2]);
}
}
int get(int v, int l, int r, int tl = 0, int tr = N) {
if (r <= tl || tr <= l) {
return 0;
} else if (l <= tl && tr <= r) {
return t[v];
} else {
push(v);
int tm = (tl + tr) / 2;
return max(get(2 * v + 1, l, r, tl, tm), get(2 * v + 2, l, r, tm, tr));
}
}
vector<int> g[N];
int tin[N];
int tout[N];
int timer;
void dfs(int v, int p = -1) {
tin[v] = timer++;
for (int to : g[v]) {
if (to != p) {
dfs(to, v);
}
}
tout[v] = timer;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> R(n);
for (int i = n - 1; i >= 0; i--) {
R[i] = i + 1;
while (R[i] < n && a[i] >= a[R[i]]) {
R[i] = R[R[i]];
}
g[R[i]].push_back(i);
}
dfs(n);
int j = 0;
for (int i = 0; i + k <= n; i++) {
while (j < i + k) {
upd(0, tin[j], tout[j], 1);
++j;
}
cout << get(0, 0, N) << ' ';
upd(0, tin[i], tin[i] + 1, -N);
}
cout << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int n, k, p[maxn], stk[maxn], top, head[maxn], tid[maxn], siz[maxn], tot, cnt;
struct abc {
int to, nxt;
} e[maxn * 2];
void add(int x, int y) {
e[++cnt].nxt = head[x];
e[cnt].to = y;
head[x] = cnt;
}
void dfs(int x) {
tid[x] = ++tot;
siz[x] = 1;
for (int i = head[x]; i; i = e[i].nxt) {
dfs(e[i].to);
siz[x] += siz[e[i].to];
}
}
int tree[4 * maxn], tag[4 * maxn];
void down(int o) {
tree[o << 1] += tag[o];
tree[o << 1 | 1] += tag[o];
tag[o << 1] += tag[o];
tag[o << 1 | 1] += tag[o];
tag[o] = 0;
}
void update(int l, int r, int o, int x, int y, int val) {
if (x <= l && r <= y) {
tree[o] += val;
tag[o] += val;
return;
}
int mid = (l + r) >> 1;
down(o);
if (x <= mid) update(l, mid, o << 1, x, y, val);
if (y > mid) update(mid + 1, r, o << 1 | 1, x, y, val);
tree[o] = max(tree[o << 1], tree[o << 1 | 1]);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &p[i]);
for (int i = 1; i <= n; ++i) {
while (top && p[stk[top]] < p[i]) {
add(i, stk[top]);
--top;
}
stk[++top] = i;
}
while (top) {
add(n + 1, stk[top]);
--top;
}
dfs(n + 1);
for (int i = 1; i <= k; ++i)
update(1, n + 1, 1, tid[i], tid[i] + siz[i] - 1, 1);
printf("%d", tree[1]);
for (int i = k + 1; i <= n; ++i) {
update(1, n + 1, 1, tid[i], tid[i] + siz[i] - 1, 1);
update(1, n + 1, 1, tid[i - k], tid[i - k] + siz[i - k] - 1, -1);
printf(" %d", tree[1]);
}
}
| ### Prompt
Please formulate a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int n, k, p[maxn], stk[maxn], top, head[maxn], tid[maxn], siz[maxn], tot, cnt;
struct abc {
int to, nxt;
} e[maxn * 2];
void add(int x, int y) {
e[++cnt].nxt = head[x];
e[cnt].to = y;
head[x] = cnt;
}
void dfs(int x) {
tid[x] = ++tot;
siz[x] = 1;
for (int i = head[x]; i; i = e[i].nxt) {
dfs(e[i].to);
siz[x] += siz[e[i].to];
}
}
int tree[4 * maxn], tag[4 * maxn];
void down(int o) {
tree[o << 1] += tag[o];
tree[o << 1 | 1] += tag[o];
tag[o << 1] += tag[o];
tag[o << 1 | 1] += tag[o];
tag[o] = 0;
}
void update(int l, int r, int o, int x, int y, int val) {
if (x <= l && r <= y) {
tree[o] += val;
tag[o] += val;
return;
}
int mid = (l + r) >> 1;
down(o);
if (x <= mid) update(l, mid, o << 1, x, y, val);
if (y > mid) update(mid + 1, r, o << 1 | 1, x, y, val);
tree[o] = max(tree[o << 1], tree[o << 1 | 1]);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &p[i]);
for (int i = 1; i <= n; ++i) {
while (top && p[stk[top]] < p[i]) {
add(i, stk[top]);
--top;
}
stk[++top] = i;
}
while (top) {
add(n + 1, stk[top]);
--top;
}
dfs(n + 1);
for (int i = 1; i <= k; ++i)
update(1, n + 1, 1, tid[i], tid[i] + siz[i] - 1, 1);
printf("%d", tree[1]);
for (int i = k + 1; i <= n; ++i) {
update(1, n + 1, 1, tid[i], tid[i] + siz[i] - 1, 1);
update(1, n + 1, 1, tid[i - k], tid[i - k] + siz[i - k] - 1, -1);
printf(" %d", tree[1]);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
const double PI = acos(-1.0);
const double EPS = 1e-6;
const int MOD = (int)1e9 + 7;
const int maxn = (int)1e6 + 5;
const int LOGN = 20;
int tree[4 * maxn], lazy[4 * maxn];
void propagate(int node, int b, int e) {
if (lazy[node] == 0) return;
tree[node] += lazy[node];
if (b != e) {
lazy[2 * node] += lazy[node];
lazy[2 * node + 1] += lazy[node];
}
lazy[node] = 0;
}
int query(int node, int b, int e, int l, int r) {
propagate(node, b, e);
if (l > e || r < b) return 0LL;
if (b >= l && e <= r) return tree[node];
int Left = node * 2;
int Right = node * 2 + 1;
int mid = (b + e) / 2;
return max(query(Left, b, mid, l, r), query(Right, mid + 1, e, l, r));
}
void update(int node, int b, int e, int l, int r, int add) {
propagate(node, b, e);
if (l > e || r < b) return;
if (b >= l && e <= r) {
lazy[node] = add;
propagate(node, b, e);
return;
}
int Left = node * 2;
int Right = node * 2 + 1;
int mid = (b + e) / 2;
update(Left, b, mid, l, r, add);
update(Right, mid + 1, e, l, r, add);
tree[node] = max(tree[Left], tree[Right]);
}
int a[maxn], ans[maxn];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n, k;
cin >> n >> k;
vector<int> v;
v.push_back(0);
a[0] = INT_MAX;
for (int i = 1; i <= (n); i++) {
cin >> a[i];
while (!v.empty() && a[i] > a[v.back()]) v.pop_back();
update(1, 1, n, v.back() + 1, i, +1);
v.push_back(i);
if (i >= k) ans[i] = query(1, 1, n, i - k + 1, i);
}
for (int i = (k); i <= (n); i++) cout << ans[i] << " ";
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
const double PI = acos(-1.0);
const double EPS = 1e-6;
const int MOD = (int)1e9 + 7;
const int maxn = (int)1e6 + 5;
const int LOGN = 20;
int tree[4 * maxn], lazy[4 * maxn];
void propagate(int node, int b, int e) {
if (lazy[node] == 0) return;
tree[node] += lazy[node];
if (b != e) {
lazy[2 * node] += lazy[node];
lazy[2 * node + 1] += lazy[node];
}
lazy[node] = 0;
}
int query(int node, int b, int e, int l, int r) {
propagate(node, b, e);
if (l > e || r < b) return 0LL;
if (b >= l && e <= r) return tree[node];
int Left = node * 2;
int Right = node * 2 + 1;
int mid = (b + e) / 2;
return max(query(Left, b, mid, l, r), query(Right, mid + 1, e, l, r));
}
void update(int node, int b, int e, int l, int r, int add) {
propagate(node, b, e);
if (l > e || r < b) return;
if (b >= l && e <= r) {
lazy[node] = add;
propagate(node, b, e);
return;
}
int Left = node * 2;
int Right = node * 2 + 1;
int mid = (b + e) / 2;
update(Left, b, mid, l, r, add);
update(Right, mid + 1, e, l, r, add);
tree[node] = max(tree[Left], tree[Right]);
}
int a[maxn], ans[maxn];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n, k;
cin >> n >> k;
vector<int> v;
v.push_back(0);
a[0] = INT_MAX;
for (int i = 1; i <= (n); i++) {
cin >> a[i];
while (!v.empty() && a[i] > a[v.back()]) v.pop_back();
update(1, 1, n, v.back() + 1, i, +1);
v.push_back(i);
if (i >= k) ans[i] = query(1, 1, n, i - k + 1, i);
}
for (int i = (k); i <= (n); i++) cout << ans[i] << " ";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int n, k;
int maxv[4 * maxn];
int addv[4 * maxn];
int a[maxn];
int L[maxn];
void pushup(int o) { maxv[o] = max(maxv[o * 2], maxv[o * 2 + 1]); }
void pushdown(int o, int L, int R) {
if (addv[o]) {
addv[o * 2] += addv[o];
addv[o * 2 + 1] += addv[o];
maxv[o * 2] += addv[o];
maxv[o * 2 + 1] += addv[o];
addv[o] = 0;
}
}
void update(int o, int L, int R, int ql, int qr, int v) {
if (ql <= L && R <= qr) {
addv[o] += v;
maxv[o] += v;
return;
}
pushdown(o, L, R);
int M = (L + R) >> 1;
if (ql <= M) update(o * 2, L, M, ql, qr, v);
if (qr > M) update(o * 2 + 1, M + 1, R, ql, qr, v);
pushup(o);
}
int query(int o, int L, int R, int ql, int qr) {
if (ql <= L && R <= qr) {
return maxv[o];
}
pushdown(o, L, R);
int ret = 0;
int M = (L + R) >> 1;
if (ql <= M) ret = max(ret, query(o * 2, L, M, ql, qr));
if (qr > M) ret = max(ret, query(o * 2 + 1, M + 1, R, ql, qr));
pushup(o);
return ret;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[0] = 1e7;
L[1] = 0;
for (int i = 2; i <= n; i++) {
int j = i - 1;
while (a[i] > a[j]) j = L[j];
L[i] = j;
}
for (int i = 1; i < k; i++) update(1, 1, n, L[i] + 1, i, 1);
for (int i = 1, j = k; j <= n; i++, j++) {
update(1, 1, n, L[j] + 1, j, 1);
cout << query(1, 1, n, i, j) << " ";
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int n, k;
int maxv[4 * maxn];
int addv[4 * maxn];
int a[maxn];
int L[maxn];
void pushup(int o) { maxv[o] = max(maxv[o * 2], maxv[o * 2 + 1]); }
void pushdown(int o, int L, int R) {
if (addv[o]) {
addv[o * 2] += addv[o];
addv[o * 2 + 1] += addv[o];
maxv[o * 2] += addv[o];
maxv[o * 2 + 1] += addv[o];
addv[o] = 0;
}
}
void update(int o, int L, int R, int ql, int qr, int v) {
if (ql <= L && R <= qr) {
addv[o] += v;
maxv[o] += v;
return;
}
pushdown(o, L, R);
int M = (L + R) >> 1;
if (ql <= M) update(o * 2, L, M, ql, qr, v);
if (qr > M) update(o * 2 + 1, M + 1, R, ql, qr, v);
pushup(o);
}
int query(int o, int L, int R, int ql, int qr) {
if (ql <= L && R <= qr) {
return maxv[o];
}
pushdown(o, L, R);
int ret = 0;
int M = (L + R) >> 1;
if (ql <= M) ret = max(ret, query(o * 2, L, M, ql, qr));
if (qr > M) ret = max(ret, query(o * 2 + 1, M + 1, R, ql, qr));
pushup(o);
return ret;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[0] = 1e7;
L[1] = 0;
for (int i = 2; i <= n; i++) {
int j = i - 1;
while (a[i] > a[j]) j = L[j];
L[i] = j;
}
for (int i = 1; i < k; i++) update(1, 1, n, L[i] + 1, i, 1);
for (int i = 1, j = k; j <= n; i++, j++) {
update(1, 1, n, L[j] + 1, j, 1);
cout << query(1, 1, n, i, j) << " ";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int num[1000001];
int L[1000001];
int sum_tree[1000001 * 3];
int max_tree[1000001 * 3];
void update(int a, int b, int i = 1, int start = 1, int end = n + 1) {
if (start == a && end == b) {
max_tree[i] += 1;
sum_tree[i] += 1;
return;
}
int mid = (start + end) / 2;
if (a < mid) update(a, min(b, mid), i * 2, start, mid);
if (b > mid) update(max(a, mid), b, i * 2 + 1, mid, end);
max_tree[i] = max(max_tree[i * 2], max_tree[i * 2 + 1]) + sum_tree[i];
}
int query(int a, int b, int i = 1, int start = 1, int end = n + 1) {
if (start == a && end == b) return max_tree[i];
int mid = (start + end) / 2;
int ans = 0;
if (a < mid) ans = max(ans, query(a, min(b, mid), i * 2, start, mid));
if (b > mid) ans = max(ans, query(max(a, mid), b, i * 2 + 1, mid, end));
return ans + sum_tree[i];
}
int main() {
cin >> n >> k;
num[0] = 1 << 30;
for (int i = 1; i <= n; ++i) {
cin >> num[i];
}
for (int i = 1; i <= n; ++i) {
int idx = i - 1;
while (num[i] > num[idx]) {
idx = L[idx];
}
L[i] = idx;
}
for (int i = 1; i <= n; ++i) {
update(L[i] + 1, i + 1);
if (i >= k) {
cout << query(i - k + 1, i + 1) << ' ';
}
}
cout << '\n';
}
| ### Prompt
Generate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k;
int num[1000001];
int L[1000001];
int sum_tree[1000001 * 3];
int max_tree[1000001 * 3];
void update(int a, int b, int i = 1, int start = 1, int end = n + 1) {
if (start == a && end == b) {
max_tree[i] += 1;
sum_tree[i] += 1;
return;
}
int mid = (start + end) / 2;
if (a < mid) update(a, min(b, mid), i * 2, start, mid);
if (b > mid) update(max(a, mid), b, i * 2 + 1, mid, end);
max_tree[i] = max(max_tree[i * 2], max_tree[i * 2 + 1]) + sum_tree[i];
}
int query(int a, int b, int i = 1, int start = 1, int end = n + 1) {
if (start == a && end == b) return max_tree[i];
int mid = (start + end) / 2;
int ans = 0;
if (a < mid) ans = max(ans, query(a, min(b, mid), i * 2, start, mid));
if (b > mid) ans = max(ans, query(max(a, mid), b, i * 2 + 1, mid, end));
return ans + sum_tree[i];
}
int main() {
cin >> n >> k;
num[0] = 1 << 30;
for (int i = 1; i <= n; ++i) {
cin >> num[i];
}
for (int i = 1; i <= n; ++i) {
int idx = i - 1;
while (num[i] > num[idx]) {
idx = L[idx];
}
L[i] = idx;
}
for (int i = 1; i <= n; ++i) {
update(L[i] + 1, i + 1);
if (i >= k) {
cout << query(i - k + 1, i + 1) << ' ';
}
}
cout << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> a;
int T = 0;
vector<vector<int> > g;
vector<int> tin, tout;
void dfs(int v) {
tin[v] = T++;
for (int i = 0; i < g[v].size(); i++) {
dfs(g[v][i]);
}
tout[v] = T;
}
vector<int> tmax, tadd;
void push(int v) {
tadd[2 * v + 1] += tadd[v];
tadd[2 * v + 2] += tadd[v];
tadd[v] = 0;
}
int getmax(int v) { return tmax[v] + tadd[v]; }
void add(int v, int l, int r, int lf, int rg, int val) {
if (l == lf && r == rg) {
tadd[v] += val;
return;
}
int m = (l + r) / 2;
push(v);
if (lf < m) add(2 * v + 1, l, m, lf, min(m, rg), val);
if (rg > m) add(2 * v + 2, m, r, max(lf, m), rg, val);
tmax[v] = max(getmax(2 * v + 1), getmax(2 * v + 2));
}
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(0);
cin >> n >> k;
a.assign(n, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
g.resize(n + 1);
tin.resize(n + 1, 0);
tout.resize(n + 1, 0);
vector<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.back()] <= a[i]) {
st.pop_back();
}
int next = st.empty() ? n : st.back();
g[next].push_back(i);
st.push_back(i);
}
dfs(n);
tmax.assign(4 * (n + 1), 0);
tadd.assign(4 * (n + 1), 0);
for (int i = 0; i < k - 1; i++) {
add(0, 0, n + 1, tin[i], tout[i], +1);
}
for (int i = 0; i + k <= n; i++) {
add(0, 0, n + 1, tin[i + k - 1], tout[i + k - 1], +1);
cout << getmax(0) << " ";
add(0, 0, n + 1, tin[i], tout[i], -1);
}
cout << "\n";
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> a;
int T = 0;
vector<vector<int> > g;
vector<int> tin, tout;
void dfs(int v) {
tin[v] = T++;
for (int i = 0; i < g[v].size(); i++) {
dfs(g[v][i]);
}
tout[v] = T;
}
vector<int> tmax, tadd;
void push(int v) {
tadd[2 * v + 1] += tadd[v];
tadd[2 * v + 2] += tadd[v];
tadd[v] = 0;
}
int getmax(int v) { return tmax[v] + tadd[v]; }
void add(int v, int l, int r, int lf, int rg, int val) {
if (l == lf && r == rg) {
tadd[v] += val;
return;
}
int m = (l + r) / 2;
push(v);
if (lf < m) add(2 * v + 1, l, m, lf, min(m, rg), val);
if (rg > m) add(2 * v + 2, m, r, max(lf, m), rg, val);
tmax[v] = max(getmax(2 * v + 1), getmax(2 * v + 2));
}
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(0);
cin >> n >> k;
a.assign(n, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
g.resize(n + 1);
tin.resize(n + 1, 0);
tout.resize(n + 1, 0);
vector<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.back()] <= a[i]) {
st.pop_back();
}
int next = st.empty() ? n : st.back();
g[next].push_back(i);
st.push_back(i);
}
dfs(n);
tmax.assign(4 * (n + 1), 0);
tadd.assign(4 * (n + 1), 0);
for (int i = 0; i < k - 1; i++) {
add(0, 0, n + 1, tin[i], tout[i], +1);
}
for (int i = 0; i + k <= n; i++) {
add(0, 0, n + 1, tin[i + k - 1], tout[i + k - 1], +1);
cout << getmax(0) << " ";
add(0, 0, n + 1, tin[i], tout[i], -1);
}
cout << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, sz, timer;
int p[1000005];
vector<int> v[1000005];
int tin[1000005], tout[1000005];
int t[4000005], tt[4000005];
int depth[1000005];
int a[1000005];
pair<int, int> q[1000005];
bool used[1000005];
void dfs(int x, int y = 0) {
used[x] = true;
tin[x] = ++timer;
depth[x] = depth[y] + 1;
for (int i = 0; i < v[x].size(); ++i) {
int to = v[x][i];
dfs(to, x);
}
tout[x] = timer;
return;
}
void update(int x, int y) {
x += sz - 1;
t[x] = y;
x >>= 1;
while (x) {
t[x] = max(t[x + x], t[x + x + 1]);
x >>= 1;
}
return;
}
int get(int l, int r, int ll, int rr, int x) {
if (l > r || ll > r || l > rr || ll > rr) return 0;
if (l >= ll && r <= rr) return t[x];
int mid = (l + r) >> 1;
return max(get(l, mid, ll, rr, x + x), get(mid + 1, r, ll, rr, x + x + 1));
}
void update2(int x, int y) {
x += sz - 1;
tt[x] = y;
x >>= 1;
while (x) {
tt[x] = max(tt[x + x], tt[x + x + 1]);
x >>= 1;
}
return;
}
int get_pred(int x) {
if (x == p[x]) return x;
return p[x] = get_pred(p[x]);
}
void update_val(int x) {
int val = get(1, sz, tin[x], tout[x], 1) - depth[x] + 1;
update2(x, val);
return;
}
void add_vertex(int x) {
for (int i = 0; i < v[x].size(); ++i) {
int to = v[x][i];
update2(to, 0);
to = get_pred(to);
p[to] = x;
}
update(tin[x], depth[x]);
update_val(x);
return;
}
void del_vertex(int x) {
update(tin[x], 0);
x = get_pred(x);
update_val(x);
return;
}
int main(int argc, const char* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
q[0] = {1e9, n + 1};
for (int i = n; i > 0; --i) {
while (sz > 0 && q[sz].first <= a[i]) --sz;
v[q[sz].second].push_back(i);
q[++sz] = {a[i], i};
}
for (int i = n + 1; i > 0; --i)
if (!used[i]) dfs(i);
for (int i = 1; i <= n + 1; ++i) p[i] = i;
sz = 1;
while (sz < n + 1) sz += sz;
for (int i = 1; i <= m; ++i) {
add_vertex(i);
}
cout << tt[1] << " ";
for (int i = m + 1; i <= n; ++i) {
add_vertex(i);
del_vertex(i - m);
cout << tt[1] << " ";
}
cout << '\n';
}
| ### Prompt
Create a solution in Cpp for the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, sz, timer;
int p[1000005];
vector<int> v[1000005];
int tin[1000005], tout[1000005];
int t[4000005], tt[4000005];
int depth[1000005];
int a[1000005];
pair<int, int> q[1000005];
bool used[1000005];
void dfs(int x, int y = 0) {
used[x] = true;
tin[x] = ++timer;
depth[x] = depth[y] + 1;
for (int i = 0; i < v[x].size(); ++i) {
int to = v[x][i];
dfs(to, x);
}
tout[x] = timer;
return;
}
void update(int x, int y) {
x += sz - 1;
t[x] = y;
x >>= 1;
while (x) {
t[x] = max(t[x + x], t[x + x + 1]);
x >>= 1;
}
return;
}
int get(int l, int r, int ll, int rr, int x) {
if (l > r || ll > r || l > rr || ll > rr) return 0;
if (l >= ll && r <= rr) return t[x];
int mid = (l + r) >> 1;
return max(get(l, mid, ll, rr, x + x), get(mid + 1, r, ll, rr, x + x + 1));
}
void update2(int x, int y) {
x += sz - 1;
tt[x] = y;
x >>= 1;
while (x) {
tt[x] = max(tt[x + x], tt[x + x + 1]);
x >>= 1;
}
return;
}
int get_pred(int x) {
if (x == p[x]) return x;
return p[x] = get_pred(p[x]);
}
void update_val(int x) {
int val = get(1, sz, tin[x], tout[x], 1) - depth[x] + 1;
update2(x, val);
return;
}
void add_vertex(int x) {
for (int i = 0; i < v[x].size(); ++i) {
int to = v[x][i];
update2(to, 0);
to = get_pred(to);
p[to] = x;
}
update(tin[x], depth[x]);
update_val(x);
return;
}
void del_vertex(int x) {
update(tin[x], 0);
x = get_pred(x);
update_val(x);
return;
}
int main(int argc, const char* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
q[0] = {1e9, n + 1};
for (int i = n; i > 0; --i) {
while (sz > 0 && q[sz].first <= a[i]) --sz;
v[q[sz].second].push_back(i);
q[++sz] = {a[i], i};
}
for (int i = n + 1; i > 0; --i)
if (!used[i]) dfs(i);
for (int i = 1; i <= n + 1; ++i) p[i] = i;
sz = 1;
while (sz < n + 1) sz += sz;
for (int i = 1; i <= m; ++i) {
add_vertex(i);
}
cout << tt[1] << " ";
for (int i = m + 1; i <= n; ++i) {
add_vertex(i);
del_vertex(i - m);
cout << tt[1] << " ";
}
cout << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[1000005];
stack<int> cur;
int nxt[1000005];
vector<int> adj[1000005];
int in_t[1000005], out_t[1000005];
int timer = 0;
void dfs(int x, int p) {
in_t[x] = timer++;
for (auto i : adj[x]) {
if (i == p) continue;
dfs(i, x);
}
out_t[x] = timer - 1;
}
int segtree[4 * 1000005];
int lazy[4 * 1000005];
void push(int x, int l, int r) {
if (lazy[x] == 0) return;
segtree[x] += lazy[x];
if (l != r) {
lazy[2 * x + 1] += lazy[x];
lazy[2 * x + 2] += lazy[x];
}
lazy[x] = 0;
}
void update(int x, int l, int r, int a, int b, int v) {
push(x, l, r);
if (r < a || b < l) return;
if (a <= l && r <= b) {
lazy[x] = v;
push(x, l, r);
return;
}
int m = (l + r) / 2;
update(2 * x + 1, l, m, a, b, v);
update(2 * x + 2, m + 1, r, a, b, v);
segtree[x] = max(segtree[2 * x + 1], segtree[2 * x + 2]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n - 1; i >= 0; i--) {
while (cur.size() > 0 && a[cur.top()] <= a[i]) {
cur.pop();
}
nxt[i] = (cur.size() > 0 ? cur.top() : n);
cur.push(i);
}
for (int i = 0; i < n; i++) {
adj[i].push_back(nxt[i]);
adj[nxt[i]].push_back(i);
}
dfs(n, -1);
for (int i = 0; i < k; i++) {
update(0, 0, n, in_t[i], out_t[i], 1);
}
cout << segtree[0] << " ";
for (int i = 0; i < n - k; i++) {
update(0, 0, n, in_t[i], out_t[i], -1);
update(0, 0, n, in_t[i + k], out_t[i + k], 1);
cout << segtree[0] << " ";
}
cout << endl;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[1000005];
stack<int> cur;
int nxt[1000005];
vector<int> adj[1000005];
int in_t[1000005], out_t[1000005];
int timer = 0;
void dfs(int x, int p) {
in_t[x] = timer++;
for (auto i : adj[x]) {
if (i == p) continue;
dfs(i, x);
}
out_t[x] = timer - 1;
}
int segtree[4 * 1000005];
int lazy[4 * 1000005];
void push(int x, int l, int r) {
if (lazy[x] == 0) return;
segtree[x] += lazy[x];
if (l != r) {
lazy[2 * x + 1] += lazy[x];
lazy[2 * x + 2] += lazy[x];
}
lazy[x] = 0;
}
void update(int x, int l, int r, int a, int b, int v) {
push(x, l, r);
if (r < a || b < l) return;
if (a <= l && r <= b) {
lazy[x] = v;
push(x, l, r);
return;
}
int m = (l + r) / 2;
update(2 * x + 1, l, m, a, b, v);
update(2 * x + 2, m + 1, r, a, b, v);
segtree[x] = max(segtree[2 * x + 1], segtree[2 * x + 2]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n - 1; i >= 0; i--) {
while (cur.size() > 0 && a[cur.top()] <= a[i]) {
cur.pop();
}
nxt[i] = (cur.size() > 0 ? cur.top() : n);
cur.push(i);
}
for (int i = 0; i < n; i++) {
adj[i].push_back(nxt[i]);
adj[nxt[i]].push_back(i);
}
dfs(n, -1);
for (int i = 0; i < k; i++) {
update(0, 0, n, in_t[i], out_t[i], 1);
}
cout << segtree[0] << " ";
for (int i = 0; i < n - k; i++) {
update(0, 0, n, in_t[i], out_t[i], -1);
update(0, 0, n, in_t[i + k], out_t[i + k], 1);
cout << segtree[0] << " ";
}
cout << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
unsigned int read() {
unsigned int rt = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') {
rt = rt * 10 + c - '0';
c = getchar();
}
return rt;
}
struct Edge {
unsigned int to, next;
} ed[1000005 * 2];
unsigned int head[1000005], cnt;
void add(int a, int b) {
ed[++cnt].to = b;
ed[cnt].next = head[a];
head[a] = cnt;
}
unsigned int sta[1000005], a[1000005], size[1000005];
unsigned int n, k, top;
void init() {
a[++n] = -1;
sta[++top] = 1;
size[1] = 1;
for (int i = 2; i <= n; i++) {
size[i] = 1;
while (a[sta[top]] < a[i] && top) {
add(i, sta[top]);
size[i] += size[sta[top--]];
}
sta[++top] = i;
}
}
unsigned int beg[1000005], last[1000005], dfs;
stack<int> q;
void fuckldy() {
q.push(n);
while (!q.empty()) {
int x = q.top();
q.pop();
beg[x] = ++dfs;
for (int i = head[x]; i; i = ed[i].next) q.push(ed[i].to);
}
for (int i = 1; i <= n; i++) last[i] = beg[i] + size[i] - 1;
}
struct node {
int flag, maxx;
} nd[1000005 * 4];
void pushdown(int x) {
nd[x << 1].maxx += nd[x].flag;
nd[x << 1 | 1].maxx += nd[x].flag;
nd[x << 1].flag += nd[x].flag;
nd[x << 1 | 1].flag += nd[x].flag;
nd[x].flag = 0;
}
void change(int l, int r, int x, int L, int R, int val) {
if (l >= L && r <= R) {
nd[x].flag += val;
nd[x].maxx += val;
return;
}
pushdown(x);
int mid = l + r >> 1;
if (L <= mid) change(l, mid, x << 1, L, R, val);
if (R > mid) change(mid + 1, r, x << 1 | 1, L, R, val);
nd[x].maxx = max(nd[x << 1].maxx, nd[x << 1 | 1].maxx);
}
int main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
init();
fuckldy();
for (int i = 1; i <= k; i++) change(1, n, 1, beg[i], last[i], 1);
printf("%u ", nd[1].maxx);
for (int i = k + 1, j = 1; i < n; i++, j++) {
change(1, n, 1, beg[i], last[i], 1);
change(1, n, 1, beg[j], last[j], -1);
printf("%u ", nd[1].maxx);
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
unsigned int read() {
unsigned int rt = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') {
rt = rt * 10 + c - '0';
c = getchar();
}
return rt;
}
struct Edge {
unsigned int to, next;
} ed[1000005 * 2];
unsigned int head[1000005], cnt;
void add(int a, int b) {
ed[++cnt].to = b;
ed[cnt].next = head[a];
head[a] = cnt;
}
unsigned int sta[1000005], a[1000005], size[1000005];
unsigned int n, k, top;
void init() {
a[++n] = -1;
sta[++top] = 1;
size[1] = 1;
for (int i = 2; i <= n; i++) {
size[i] = 1;
while (a[sta[top]] < a[i] && top) {
add(i, sta[top]);
size[i] += size[sta[top--]];
}
sta[++top] = i;
}
}
unsigned int beg[1000005], last[1000005], dfs;
stack<int> q;
void fuckldy() {
q.push(n);
while (!q.empty()) {
int x = q.top();
q.pop();
beg[x] = ++dfs;
for (int i = head[x]; i; i = ed[i].next) q.push(ed[i].to);
}
for (int i = 1; i <= n; i++) last[i] = beg[i] + size[i] - 1;
}
struct node {
int flag, maxx;
} nd[1000005 * 4];
void pushdown(int x) {
nd[x << 1].maxx += nd[x].flag;
nd[x << 1 | 1].maxx += nd[x].flag;
nd[x << 1].flag += nd[x].flag;
nd[x << 1 | 1].flag += nd[x].flag;
nd[x].flag = 0;
}
void change(int l, int r, int x, int L, int R, int val) {
if (l >= L && r <= R) {
nd[x].flag += val;
nd[x].maxx += val;
return;
}
pushdown(x);
int mid = l + r >> 1;
if (L <= mid) change(l, mid, x << 1, L, R, val);
if (R > mid) change(mid + 1, r, x << 1 | 1, L, R, val);
nd[x].maxx = max(nd[x << 1].maxx, nd[x << 1 | 1].maxx);
}
int main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
init();
fuckldy();
for (int i = 1; i <= k; i++) change(1, n, 1, beg[i], last[i], 1);
printf("%u ", nd[1].maxx);
for (int i = k + 1, j = 1; i < n; i++, j++) {
change(1, n, 1, beg[i], last[i], 1);
change(1, n, 1, beg[j], last[j], -1);
printf("%u ", nd[1].maxx);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int mx[maxn * 4], tag[maxn * 4], cnt, sz[maxn], a[maxn], id[maxn];
vector<int> G[maxn];
stack<int> s;
void dfs(int u) {
sz[u] = 1;
id[u] = ++cnt;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dfs(v);
sz[u] += sz[v];
}
}
void pushdown(int o, int ls, int rs) {
if (tag[o] != 0) {
tag[ls] += tag[o], tag[rs] += tag[o];
mx[ls] += tag[o], mx[rs] += tag[o];
tag[o] = 0;
}
}
void up(int o, int l, int r, int ql, int qr, int v) {
int m = (l + r) / 2, ls = o * 2, rs = o * 2 + 1;
if (l != r) pushdown(o, ls, rs);
if (l >= ql && r <= qr) {
mx[o] += v;
tag[o] += v;
return;
}
if (ql <= m) up(ls, l, m, ql, qr, v);
if (qr > m) up(rs, m + 1, r, ql, qr, v);
mx[o] = max(mx[ls], mx[rs]);
}
int main() {
int n, k, ans = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i; i--) {
while (!s.empty() && a[i] >= a[s.top()]) s.pop();
if (!s.empty()) G[s.top()].push_back(i);
s.push(i);
}
for (int i = n; i; i--)
if (!id[i]) dfs(i);
for (int i = 1; i <= k; i++) up(1, 1, n, id[i], id[i] + sz[i] - 1, 1);
printf("%d", mx[1]);
for (int i = k + 1; i <= n; i++) {
up(1, 1, n, id[i - k], id[i - k], -10000000);
up(1, 1, n, id[i], id[i] + sz[i] - 1, 1);
printf(" %d", mx[1]);
}
}
| ### Prompt
Generate a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int mx[maxn * 4], tag[maxn * 4], cnt, sz[maxn], a[maxn], id[maxn];
vector<int> G[maxn];
stack<int> s;
void dfs(int u) {
sz[u] = 1;
id[u] = ++cnt;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dfs(v);
sz[u] += sz[v];
}
}
void pushdown(int o, int ls, int rs) {
if (tag[o] != 0) {
tag[ls] += tag[o], tag[rs] += tag[o];
mx[ls] += tag[o], mx[rs] += tag[o];
tag[o] = 0;
}
}
void up(int o, int l, int r, int ql, int qr, int v) {
int m = (l + r) / 2, ls = o * 2, rs = o * 2 + 1;
if (l != r) pushdown(o, ls, rs);
if (l >= ql && r <= qr) {
mx[o] += v;
tag[o] += v;
return;
}
if (ql <= m) up(ls, l, m, ql, qr, v);
if (qr > m) up(rs, m + 1, r, ql, qr, v);
mx[o] = max(mx[ls], mx[rs]);
}
int main() {
int n, k, ans = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i; i--) {
while (!s.empty() && a[i] >= a[s.top()]) s.pop();
if (!s.empty()) G[s.top()].push_back(i);
s.push(i);
}
for (int i = n; i; i--)
if (!id[i]) dfs(i);
for (int i = 1; i <= k; i++) up(1, 1, n, id[i], id[i] + sz[i] - 1, 1);
printf("%d", mx[1]);
for (int i = k + 1; i <= n; i++) {
up(1, 1, n, id[i - k], id[i - k], -10000000);
up(1, 1, n, id[i], id[i] + sz[i] - 1, 1);
printf(" %d", mx[1]);
}
}
``` |
#include <bits/stdc++.h>
const int MAXN = 1e6 + 10;
const double eps = 1e-8;
using namespace std;
const int inf = 1e9;
struct edge {
int t;
edge *next;
} e[MAXN << 1], *h[MAXN], *o = e;
void add(int x, int y) {
o->t = y;
o->next = h[x];
h[x] = o++;
}
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
int n, k;
int a[MAXN], key[MAXN];
int st[MAXN], tot, cnt;
int fa[MAXN][21], dep[MAXN], p[MAXN], num[MAXN];
void dfs(int x, int pre, int deep) {
fa[x][0] = pre;
dep[x] = deep + 1;
num[x] = 1;
p[x] = ++cnt;
key[x] = x;
for (int i = 1; i <= 20; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
for (edge *j = h[x]; j; j = j->next) {
dfs(j->t, x, deep + 1);
num[x] += num[j->t];
}
}
int St(int x, int y) {
int p = x;
for (int i = 20; i >= 0; i--)
if (key[fa[x][i]] <= y) x = fa[x][i];
return dep[p] - dep[fa[x][0]];
}
int maxx[MAXN << 2], tag[MAXN << 2];
void push(int x) {
if (tag[x]) {
maxx[x << 1] -= tag[x];
maxx[x << 1 | 1] -= tag[x];
tag[x << 1] += tag[x];
tag[x << 1 | 1] += tag[x];
tag[x] = 0;
}
}
void up(int x) { maxx[x] = max(maxx[x << 1], maxx[x << 1 | 1]); }
void built(int x, int l, int r) {
if (l == r) {
maxx[x] = -inf;
return;
}
int mid = (l + r) >> 1;
built(x << 1, l, mid);
built(x << 1 | 1, mid + 1, r);
up(x);
}
void update(int x, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
tag[x]++;
maxx[x]--;
return;
}
int mid = (l + r) >> 1;
push(x);
if (ql <= mid) update(x << 1, l, mid, ql, qr);
if (qr > mid) update(x << 1 | 1, mid + 1, r, ql, qr);
up(x);
}
void update1(int x, int l, int r, int t, int k) {
if (l == r) {
maxx[x] += k;
return;
}
int mid = (l + r) >> 1;
push(x);
if (t <= mid)
update1(x << 1, l, mid, t, k);
else
update1(x << 1 | 1, mid + 1, r, t, k);
up(x);
}
int ans[MAXN];
int main() {
n = read();
k = read();
key[0] = inf;
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = n; i >= 1; i--) {
while (tot && a[st[tot]] <= a[i]) tot--;
if (!tot)
add(n + 1, i);
else
add(st[tot], i);
st[++tot] = i;
}
dfs(n + 1, 0, 0);
built(1, 1, n + 1);
for (int i = n; i >= n - k + 1; i--)
update1(1, 1, n + 1, p[i], dep[i] - 1 + inf);
ans[n - k + 1] = maxx[1];
for (int i = n - k; i >= 1; i--) {
update(1, 1, n + 1, p[i + k], p[i + k] + num[i + k] - 1);
update1(1, 1, n + 1, p[i + k], -inf);
update1(1, 1, n + 1, p[i], dep[i] + inf - 1);
ans[i] = maxx[1];
}
for (int i = 1; i <= n - k + 1; i++) printf("%d ", ans[i]);
printf("\n");
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
const int MAXN = 1e6 + 10;
const double eps = 1e-8;
using namespace std;
const int inf = 1e9;
struct edge {
int t;
edge *next;
} e[MAXN << 1], *h[MAXN], *o = e;
void add(int x, int y) {
o->t = y;
o->next = h[x];
h[x] = o++;
}
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
int n, k;
int a[MAXN], key[MAXN];
int st[MAXN], tot, cnt;
int fa[MAXN][21], dep[MAXN], p[MAXN], num[MAXN];
void dfs(int x, int pre, int deep) {
fa[x][0] = pre;
dep[x] = deep + 1;
num[x] = 1;
p[x] = ++cnt;
key[x] = x;
for (int i = 1; i <= 20; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
for (edge *j = h[x]; j; j = j->next) {
dfs(j->t, x, deep + 1);
num[x] += num[j->t];
}
}
int St(int x, int y) {
int p = x;
for (int i = 20; i >= 0; i--)
if (key[fa[x][i]] <= y) x = fa[x][i];
return dep[p] - dep[fa[x][0]];
}
int maxx[MAXN << 2], tag[MAXN << 2];
void push(int x) {
if (tag[x]) {
maxx[x << 1] -= tag[x];
maxx[x << 1 | 1] -= tag[x];
tag[x << 1] += tag[x];
tag[x << 1 | 1] += tag[x];
tag[x] = 0;
}
}
void up(int x) { maxx[x] = max(maxx[x << 1], maxx[x << 1 | 1]); }
void built(int x, int l, int r) {
if (l == r) {
maxx[x] = -inf;
return;
}
int mid = (l + r) >> 1;
built(x << 1, l, mid);
built(x << 1 | 1, mid + 1, r);
up(x);
}
void update(int x, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
tag[x]++;
maxx[x]--;
return;
}
int mid = (l + r) >> 1;
push(x);
if (ql <= mid) update(x << 1, l, mid, ql, qr);
if (qr > mid) update(x << 1 | 1, mid + 1, r, ql, qr);
up(x);
}
void update1(int x, int l, int r, int t, int k) {
if (l == r) {
maxx[x] += k;
return;
}
int mid = (l + r) >> 1;
push(x);
if (t <= mid)
update1(x << 1, l, mid, t, k);
else
update1(x << 1 | 1, mid + 1, r, t, k);
up(x);
}
int ans[MAXN];
int main() {
n = read();
k = read();
key[0] = inf;
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = n; i >= 1; i--) {
while (tot && a[st[tot]] <= a[i]) tot--;
if (!tot)
add(n + 1, i);
else
add(st[tot], i);
st[++tot] = i;
}
dfs(n + 1, 0, 0);
built(1, 1, n + 1);
for (int i = n; i >= n - k + 1; i--)
update1(1, 1, n + 1, p[i], dep[i] - 1 + inf);
ans[n - k + 1] = maxx[1];
for (int i = n - k; i >= 1; i--) {
update(1, 1, n + 1, p[i + k], p[i + k] + num[i + k] - 1);
update1(1, 1, n + 1, p[i + k], -inf);
update1(1, 1, n + 1, p[i], dep[i] + inf - 1);
ans[i] = maxx[1];
}
for (int i = 1; i <= n - k + 1; i++) printf("%d ", ans[i]);
printf("\n");
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N = 1e6 + 10;
const int inf = 1 << 30;
const long long md = 998244353;
int n, k, a[N], lg[N], mx[N][20], head[N], pos, dfn[N], rnk[N], dfscnt,
anc[N][20], siz[N];
struct edge {
int nxt, to;
} e[N << 1];
void add(int u, int v) {
e[++pos] = (edge){head[u], v};
head[u] = pos;
}
int Query(int l, int r) {
int k = lg[r - l + 1];
return max(mx[l][k], mx[r - (1 << k) + 1][k]);
}
int seg[N << 2], tag[N << 2];
void pushup(int rt) { seg[rt] = max(seg[rt << 1], seg[rt << 1 | 1]); }
void pushdown(int rt) {
if (tag[rt]) {
seg[rt << 1] += tag[rt];
seg[rt << 1 | 1] += tag[rt];
tag[rt << 1] += tag[rt];
tag[rt << 1 | 1] += tag[rt];
tag[rt] = 0;
}
}
void motify(int rt, int l, int r, int ql, int qr, int d) {
if (ql <= l && r <= qr) {
tag[rt] += d;
seg[rt] += d;
return;
}
int mid = l + r >> 1;
pushdown(rt);
if (mid >= ql) motify(rt << 1, l, mid, ql, qr, d);
if (mid + 1 <= qr) motify(rt << 1 | 1, mid + 1, r, ql, qr, d);
pushup(rt);
}
void dfs(int u, int fa) {
int i;
dfn[u] = ++dfscnt;
rnk[dfscnt] = u;
siz[u] = 1;
for (i = head[u]; i; i = e[i].nxt) {
if (e[i].to == fa) continue;
dfs(e[i].to, u);
siz[u] += siz[e[i].to];
anc[e[i].to][0] = u;
}
}
void ins(int u) { motify(1, 1, n, dfn[u], dfn[u] + siz[u] - 1, 1); }
void del(int u) { motify(1, 1, n, dfn[u], dfn[u], -inf); }
int main() {
scanf("%d%d", &n, &k);
int i, j;
for (i = 1; i <= n; ++i) scanf("%d", &a[i]), mx[i][0] = a[i];
for (j = 1; j <= 19; ++j)
for (i = 1; i + (1 << j) - 1 <= n; ++i)
mx[i][j] = max(mx[i][j - 1], mx[i + (1 << j - 1)][j - 1]);
for (i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
for (i = 1; i <= n; ++i) {
int l = i, r = n;
if (Query(i, n) == a[i]) continue;
while (l < r - 1) {
int mid = l + r >> 1;
if (Query(i, mid) > a[i])
r = mid;
else
l = mid + 1;
}
if (Query(i, l) > a[i])
add(i, l), add(l, i);
else
add(i, r), add(r, i);
}
for (i = n; i >= 1; --i)
if (!dfn[i]) dfs(i, 0);
for (j = 1; j <= 19; ++j)
for (i = 1; i <= n; ++i) anc[i][j] = anc[anc[i][j - 1]][j - 1];
for (i = 1; i <= k; ++i) ins(i);
printf("%d ", seg[1]);
for (i = 2; i + k - 1 <= n; ++i) {
ins(i + k - 1);
del(i - 1);
printf("%d ", seg[1]);
}
}
| ### Prompt
Please formulate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N = 1e6 + 10;
const int inf = 1 << 30;
const long long md = 998244353;
int n, k, a[N], lg[N], mx[N][20], head[N], pos, dfn[N], rnk[N], dfscnt,
anc[N][20], siz[N];
struct edge {
int nxt, to;
} e[N << 1];
void add(int u, int v) {
e[++pos] = (edge){head[u], v};
head[u] = pos;
}
int Query(int l, int r) {
int k = lg[r - l + 1];
return max(mx[l][k], mx[r - (1 << k) + 1][k]);
}
int seg[N << 2], tag[N << 2];
void pushup(int rt) { seg[rt] = max(seg[rt << 1], seg[rt << 1 | 1]); }
void pushdown(int rt) {
if (tag[rt]) {
seg[rt << 1] += tag[rt];
seg[rt << 1 | 1] += tag[rt];
tag[rt << 1] += tag[rt];
tag[rt << 1 | 1] += tag[rt];
tag[rt] = 0;
}
}
void motify(int rt, int l, int r, int ql, int qr, int d) {
if (ql <= l && r <= qr) {
tag[rt] += d;
seg[rt] += d;
return;
}
int mid = l + r >> 1;
pushdown(rt);
if (mid >= ql) motify(rt << 1, l, mid, ql, qr, d);
if (mid + 1 <= qr) motify(rt << 1 | 1, mid + 1, r, ql, qr, d);
pushup(rt);
}
void dfs(int u, int fa) {
int i;
dfn[u] = ++dfscnt;
rnk[dfscnt] = u;
siz[u] = 1;
for (i = head[u]; i; i = e[i].nxt) {
if (e[i].to == fa) continue;
dfs(e[i].to, u);
siz[u] += siz[e[i].to];
anc[e[i].to][0] = u;
}
}
void ins(int u) { motify(1, 1, n, dfn[u], dfn[u] + siz[u] - 1, 1); }
void del(int u) { motify(1, 1, n, dfn[u], dfn[u], -inf); }
int main() {
scanf("%d%d", &n, &k);
int i, j;
for (i = 1; i <= n; ++i) scanf("%d", &a[i]), mx[i][0] = a[i];
for (j = 1; j <= 19; ++j)
for (i = 1; i + (1 << j) - 1 <= n; ++i)
mx[i][j] = max(mx[i][j - 1], mx[i + (1 << j - 1)][j - 1]);
for (i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
for (i = 1; i <= n; ++i) {
int l = i, r = n;
if (Query(i, n) == a[i]) continue;
while (l < r - 1) {
int mid = l + r >> 1;
if (Query(i, mid) > a[i])
r = mid;
else
l = mid + 1;
}
if (Query(i, l) > a[i])
add(i, l), add(l, i);
else
add(i, r), add(r, i);
}
for (i = n; i >= 1; --i)
if (!dfn[i]) dfs(i, 0);
for (j = 1; j <= 19; ++j)
for (i = 1; i <= n; ++i) anc[i][j] = anc[anc[i][j - 1]][j - 1];
for (i = 1; i <= k; ++i) ins(i);
printf("%d ", seg[1]);
for (i = 2; i + k - 1 <= n; ++i) {
ins(i + k - 1);
del(i - 1);
printf("%d ", seg[1]);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, s, c, a[1000010], d[1000010], n1[1000010], n2[1000010], f1[1000010],
f2[1000010], t[1000010];
bool v[1000010];
int main() {
scanf("%d%d", &n, &s);
if (s == 1) {
for (int i = (1); i <= (n); ++i) printf("1 ");
return 0;
}
for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]);
for (int i = n; i; --i) {
while (c && d[c] <= a[i]) --c;
d[++c] = a[i];
a[i] = c;
t[i] = i;
}
for (int i = (1); i <= (n); ++i) {
int x = i - 1;
for (; x && (a[x] > a[i] || a[t[x]] - a[x] <= a[t[i]] - a[i]); x = f2[x]) {
int y = x;
for (; y && a[t[i]] >= a[y]; y = f1[y]) v[y] = 1;
if (y) {
n1[y] = t[i];
f1[t[i]] = y;
t[i] = t[x];
}
}
if (x)
n2[x] = i, f2[i] = x;
else
c = i;
if (i >= s) {
printf("%d ", a[t[c]] - a[c] + 1);
if (!v[i - s + 1]) {
v[i - s + 1] = 1;
t[c] = n1[i - s + 1];
f1[n1[i - s + 1]] = 0;
if (n2[c] && a[t[c]] - a[c] <= a[t[n2[c]]] - a[n2[c]]) {
for (int j = i - s + 1; j; j = n1[j]) v[j] = 1;
f2[n2[c]] = 0;
c = n2[c];
}
}
}
}
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, s, c, a[1000010], d[1000010], n1[1000010], n2[1000010], f1[1000010],
f2[1000010], t[1000010];
bool v[1000010];
int main() {
scanf("%d%d", &n, &s);
if (s == 1) {
for (int i = (1); i <= (n); ++i) printf("1 ");
return 0;
}
for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]);
for (int i = n; i; --i) {
while (c && d[c] <= a[i]) --c;
d[++c] = a[i];
a[i] = c;
t[i] = i;
}
for (int i = (1); i <= (n); ++i) {
int x = i - 1;
for (; x && (a[x] > a[i] || a[t[x]] - a[x] <= a[t[i]] - a[i]); x = f2[x]) {
int y = x;
for (; y && a[t[i]] >= a[y]; y = f1[y]) v[y] = 1;
if (y) {
n1[y] = t[i];
f1[t[i]] = y;
t[i] = t[x];
}
}
if (x)
n2[x] = i, f2[i] = x;
else
c = i;
if (i >= s) {
printf("%d ", a[t[c]] - a[c] + 1);
if (!v[i - s + 1]) {
v[i - s + 1] = 1;
t[c] = n1[i - s + 1];
f1[n1[i - s + 1]] = 0;
if (n2[c] && a[t[c]] - a[c] <= a[t[n2[c]]] - a[n2[c]]) {
for (int j = i - s + 1; j; j = n1[j]) v[j] = 1;
f2[n2[c]] = 0;
c = n2[c];
}
}
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
struct seg {
int tr[MAXN << 2], lazy[MAXN << 2];
inline void push_up(int rt) { tr[rt] = max(tr[rt << 1], tr[rt << 1 | 1]); }
inline void push_down(int rt) {
if (lazy[rt]) {
tr[rt << 1] += lazy[rt];
tr[rt << 1 | 1] += lazy[rt];
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void build(int l, int r, int rt) {
tr[rt] = lazy[rt] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(l, mid, rt << 1);
build(mid + 1, r, rt << 1 | 1);
}
void update(int L, int R, int val, int l, int r, int rt) {
if (L <= l && r <= R) {
tr[rt] += val;
lazy[rt] += val;
return;
}
int mid = (l + r) >> 1;
push_down(rt);
if (L <= mid) update(L, R, val, l, mid, rt << 1);
if (mid < R) update(L, R, val, mid + 1, r, rt << 1 | 1);
push_up(rt);
}
} se;
stack<int> st;
int a[MAXN], in[MAXN], out[MAXN], tim = 0;
vector<int> E[MAXN];
void dfs(int now) {
in[now] = ++tim;
for (int v : E[now]) {
dfs(v);
}
out[now] = tim;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
n++;
a[n] = 1e9;
for (int i = 1; i <= n; i++) {
while (!st.empty() && a[st.top()] < a[i]) {
E[i].push_back(st.top());
st.pop();
}
st.push(i);
}
dfs(n);
se.build(1, n, 1);
for (int i = 1; i < n; i++) {
se.update(in[i], out[i], 1, 1, n, 1);
if (i > k) se.update(in[i - k], out[i - k], -1, 1, n, 1);
if (i >= k) printf("%d ", se.tr[1]);
}
puts("");
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
struct seg {
int tr[MAXN << 2], lazy[MAXN << 2];
inline void push_up(int rt) { tr[rt] = max(tr[rt << 1], tr[rt << 1 | 1]); }
inline void push_down(int rt) {
if (lazy[rt]) {
tr[rt << 1] += lazy[rt];
tr[rt << 1 | 1] += lazy[rt];
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void build(int l, int r, int rt) {
tr[rt] = lazy[rt] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(l, mid, rt << 1);
build(mid + 1, r, rt << 1 | 1);
}
void update(int L, int R, int val, int l, int r, int rt) {
if (L <= l && r <= R) {
tr[rt] += val;
lazy[rt] += val;
return;
}
int mid = (l + r) >> 1;
push_down(rt);
if (L <= mid) update(L, R, val, l, mid, rt << 1);
if (mid < R) update(L, R, val, mid + 1, r, rt << 1 | 1);
push_up(rt);
}
} se;
stack<int> st;
int a[MAXN], in[MAXN], out[MAXN], tim = 0;
vector<int> E[MAXN];
void dfs(int now) {
in[now] = ++tim;
for (int v : E[now]) {
dfs(v);
}
out[now] = tim;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
n++;
a[n] = 1e9;
for (int i = 1; i <= n; i++) {
while (!st.empty() && a[st.top()] < a[i]) {
E[i].push_back(st.top());
st.pop();
}
st.push(i);
}
dfs(n);
se.build(1, n, 1);
for (int i = 1; i < n; i++) {
se.update(in[i], out[i], 1, 1, n, 1);
if (i > k) se.update(in[i - k], out[i - k], -1, 1, n, 1);
if (i >= k) printf("%d ", se.tr[1]);
}
puts("");
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
using namespace std;
vector<vector<long long> > g;
vector<long long> tin, tout;
long long timer = 0;
void dfs(long long v) {
tin[v] = timer++;
for (auto i : g[v]) {
dfs(i);
}
tout[v] = timer - 1;
}
struct Node {
long long mx, add;
};
vector<Node> tree;
void push(long long v) {
if (tree[v].add != 0) {
tree[v * 2 + 1].add += tree[v].add;
tree[v * 2 + 1].mx += tree[v].add;
tree[v * 2 + 2].add += tree[v].add;
tree[v * 2 + 2].mx += tree[v].add;
tree[v].add = 0;
}
}
void addVal(long long v, long long tl, long long tr, long long l, long long r,
long long val) {
if (l > r) return;
if (tl == l && tr == r) {
tree[v].add += val;
tree[v].mx += val;
return;
}
long long tm = (tl + tr) / 2;
push(v);
addVal(v * 2 + 1, tl, tm, l, min(tm, r), val);
addVal(v * 2 + 2, tm + 1, tr, max(tm + 1, l), r, val);
tree[v].mx = max(tree[v * 2 + 1].mx, tree[v * 2 + 2].mx);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, k;
cin >> n >> k;
g.resize(n + 1);
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
tin.resize(n + 2), tout.resize(n + 2);
stack<long long> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.top()] <= a[i]) {
st.pop();
}
long long nxt = st.empty() ? n : st.top();
g[nxt].push_back(i);
st.push(i);
}
dfs(n);
tree.resize(4 * (n + 1), Node({0, 0}));
for (long long i = 0; i < k - 1; i++) {
addVal(0, 0, n + 1, tin[i], tout[i], 1);
if (0) {
cout << i << " " << tin[i] << " " << tout[i] << " " << 1 << "\n";
}
}
for (long long l = 0; l + k <= n; l++) {
addVal(0, 0, n + 1, tin[l + k - 1], tout[l + k - 1], +1);
cout << tree[0].mx << " ";
addVal(0, 0, n + 1, tin[l], tout[l], -1);
if (0) {
cout << l + k - 1 << " " << tin[l + k - 1] << " " << tout[l + k - 1]
<< " " << 1 << "\n";
;
cout << l << " " << tin[l] << " " << tout[l] << " " << -1 << "\n";
}
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
using namespace std;
vector<vector<long long> > g;
vector<long long> tin, tout;
long long timer = 0;
void dfs(long long v) {
tin[v] = timer++;
for (auto i : g[v]) {
dfs(i);
}
tout[v] = timer - 1;
}
struct Node {
long long mx, add;
};
vector<Node> tree;
void push(long long v) {
if (tree[v].add != 0) {
tree[v * 2 + 1].add += tree[v].add;
tree[v * 2 + 1].mx += tree[v].add;
tree[v * 2 + 2].add += tree[v].add;
tree[v * 2 + 2].mx += tree[v].add;
tree[v].add = 0;
}
}
void addVal(long long v, long long tl, long long tr, long long l, long long r,
long long val) {
if (l > r) return;
if (tl == l && tr == r) {
tree[v].add += val;
tree[v].mx += val;
return;
}
long long tm = (tl + tr) / 2;
push(v);
addVal(v * 2 + 1, tl, tm, l, min(tm, r), val);
addVal(v * 2 + 2, tm + 1, tr, max(tm + 1, l), r, val);
tree[v].mx = max(tree[v * 2 + 1].mx, tree[v * 2 + 2].mx);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, k;
cin >> n >> k;
g.resize(n + 1);
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
tin.resize(n + 2), tout.resize(n + 2);
stack<long long> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.top()] <= a[i]) {
st.pop();
}
long long nxt = st.empty() ? n : st.top();
g[nxt].push_back(i);
st.push(i);
}
dfs(n);
tree.resize(4 * (n + 1), Node({0, 0}));
for (long long i = 0; i < k - 1; i++) {
addVal(0, 0, n + 1, tin[i], tout[i], 1);
if (0) {
cout << i << " " << tin[i] << " " << tout[i] << " " << 1 << "\n";
}
}
for (long long l = 0; l + k <= n; l++) {
addVal(0, 0, n + 1, tin[l + k - 1], tout[l + k - 1], +1);
cout << tree[0].mx << " ";
addVal(0, 0, n + 1, tin[l], tout[l], -1);
if (0) {
cout << l + k - 1 << " " << tin[l + k - 1] << " " << tout[l + k - 1]
<< " " << 1 << "\n";
;
cout << l << " " << tin[l] << " " << tout[l] << " " << -1 << "\n";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int arr[1000005], st[1000005], et[1000005];
int tree[1000005 * 4], lazy[1000005 * 4];
vector<int> vv[1000005], ans;
stack<int> SS;
int tt;
void dfs(int u, int par) {
st[u] = ++tt;
for (int i = 0; i < vv[u].size(); i++) {
int v = vv[u][i];
if (v != par) dfs(v, u);
}
et[u] = tt;
}
void Down(int node, int left, int right) {
if (lazy[node]) {
tree[left] += lazy[node];
tree[right] += lazy[node];
lazy[left] += lazy[node];
lazy[right] += lazy[node];
lazy[node] = 0;
}
}
void update(int node, int b, int e, int l, int r, int v) {
if (b > r || e < l) return;
if (b >= l && e <= r) {
tree[node] += v;
lazy[node] += v;
return;
}
int left = 2 * node, right = left + 1, mid = (b + e) / 2;
Down(node, left, right);
update(left, b, mid, l, r, v);
update(right, mid + 1, e, l, r, v);
tree[node] = max(tree[left], tree[right]);
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
arr[0] = n + 5;
SS.push(0);
for (int i = n; i >= 1; i--) {
while (arr[SS.top()] <= arr[i]) SS.pop();
if (SS.top() != 0) vv[SS.top()].push_back(i);
SS.push(i);
}
for (int i = n; i >= 1; i--) {
if (st[i] == 0) dfs(i, 0);
}
for (int i = k; i >= 1; i--) {
update(1, 1, n, st[i], et[i], 1);
}
ans.push_back(tree[1]);
for (int j = 1, i = k + 1; i <= n; i++, j++) {
update(1, 1, n, st[j], st[j], -100000000);
update(1, 1, n, st[i], et[i], 1);
ans.push_back(tree[1]);
}
for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]);
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int arr[1000005], st[1000005], et[1000005];
int tree[1000005 * 4], lazy[1000005 * 4];
vector<int> vv[1000005], ans;
stack<int> SS;
int tt;
void dfs(int u, int par) {
st[u] = ++tt;
for (int i = 0; i < vv[u].size(); i++) {
int v = vv[u][i];
if (v != par) dfs(v, u);
}
et[u] = tt;
}
void Down(int node, int left, int right) {
if (lazy[node]) {
tree[left] += lazy[node];
tree[right] += lazy[node];
lazy[left] += lazy[node];
lazy[right] += lazy[node];
lazy[node] = 0;
}
}
void update(int node, int b, int e, int l, int r, int v) {
if (b > r || e < l) return;
if (b >= l && e <= r) {
tree[node] += v;
lazy[node] += v;
return;
}
int left = 2 * node, right = left + 1, mid = (b + e) / 2;
Down(node, left, right);
update(left, b, mid, l, r, v);
update(right, mid + 1, e, l, r, v);
tree[node] = max(tree[left], tree[right]);
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
arr[0] = n + 5;
SS.push(0);
for (int i = n; i >= 1; i--) {
while (arr[SS.top()] <= arr[i]) SS.pop();
if (SS.top() != 0) vv[SS.top()].push_back(i);
SS.push(i);
}
for (int i = n; i >= 1; i--) {
if (st[i] == 0) dfs(i, 0);
}
for (int i = k; i >= 1; i--) {
update(1, 1, n, st[i], et[i], 1);
}
ans.push_back(tree[1]);
for (int j = 1, i = k + 1; i <= n; i++, j++) {
update(1, 1, n, st[j], st[j], -100000000);
update(1, 1, n, st[i], et[i], 1);
ans.push_back(tree[1]);
}
for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]);
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[1000005];
struct edge {
int from;
int to;
int next;
} E[1000005 << 1];
int head[1000005];
int sz = 1;
void add_edge(int u, int v) {
sz++;
E[sz].from = u;
E[sz].to = v;
E[sz].next = head[u];
head[u] = sz;
}
int cnt = 0;
int lb[1000005], rb[1000005];
void dfs(int x, int fa) {
lb[x] = ++cnt;
for (int i = head[x]; i; i = E[i].next) {
int y = E[i].to;
if (y != fa) {
dfs(y, x);
}
}
rb[x] = cnt;
}
struct node {
int l;
int r;
int v;
int mark;
} tree[1000005 << 2];
void push_up(int pos) {
tree[pos].v = max(tree[pos << 1].v, tree[pos << 1 | 1].v);
}
void build(int l, int r, int pos) {
tree[pos].l = l;
tree[pos].r = r;
if (l == r) {
return;
}
int mid = (l + r) >> 1;
build(l, mid, pos << 1);
build(mid + 1, r, pos << 1 | 1);
push_up(pos);
}
void push_down(int pos) {
if (tree[pos].mark) {
tree[pos << 1].v += tree[pos].mark;
tree[pos << 1].mark += tree[pos].mark;
tree[pos << 1 | 1].v += tree[pos].mark;
tree[pos << 1 | 1].mark += tree[pos].mark;
tree[pos].mark = 0;
}
}
void update(int L, int R, int v, int pos) {
if (L <= tree[pos].l && R >= tree[pos].r) {
tree[pos].v += v;
tree[pos].mark += v;
return;
}
push_down(pos);
int mid = (tree[pos].l + tree[pos].r) >> 1;
if (L <= mid) update(L, R, v, pos << 1);
if (R > mid) update(L, R, v, pos << 1 | 1);
push_up(pos);
}
int query(int L, int R, int pos) {
if (L <= tree[pos].l && R >= tree[pos].r) {
return tree[pos].v;
}
push_down(pos);
int mid = (tree[pos].l + tree[pos].r) >> 1;
int ans = 0;
if (L <= mid) ans = max(ans, query(L, R, pos << 1));
if (R > mid) ans = max(ans, query(L, R, pos << 1 | 1));
return ans;
}
int nex[1000005];
void init() {
stack<int> s;
for (int i = n; i >= 1; i--) {
while (!s.empty() && a[s.top()] <= a[i]) s.pop();
if (!s.empty()) {
int p = s.top();
add_edge(p, i);
add_edge(i, p);
} else {
add_edge(n + 1, i);
add_edge(i, n + 1);
}
s.push(i);
}
dfs(n + 1, 0);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
init();
build(1, n + 1, 1);
for (int i = 1; i <= k; i++) {
update(lb[i], rb[i], 1, 1);
}
for (int i = 1; i + k - 1 <= n; i++) {
int r = i + k - 1;
printf("%d ", query(1, n + 1, 1));
update(lb[i], rb[i], -1, 1);
update(lb[r + 1], rb[r + 1], 1, 1);
}
}
| ### Prompt
Please formulate a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[1000005];
struct edge {
int from;
int to;
int next;
} E[1000005 << 1];
int head[1000005];
int sz = 1;
void add_edge(int u, int v) {
sz++;
E[sz].from = u;
E[sz].to = v;
E[sz].next = head[u];
head[u] = sz;
}
int cnt = 0;
int lb[1000005], rb[1000005];
void dfs(int x, int fa) {
lb[x] = ++cnt;
for (int i = head[x]; i; i = E[i].next) {
int y = E[i].to;
if (y != fa) {
dfs(y, x);
}
}
rb[x] = cnt;
}
struct node {
int l;
int r;
int v;
int mark;
} tree[1000005 << 2];
void push_up(int pos) {
tree[pos].v = max(tree[pos << 1].v, tree[pos << 1 | 1].v);
}
void build(int l, int r, int pos) {
tree[pos].l = l;
tree[pos].r = r;
if (l == r) {
return;
}
int mid = (l + r) >> 1;
build(l, mid, pos << 1);
build(mid + 1, r, pos << 1 | 1);
push_up(pos);
}
void push_down(int pos) {
if (tree[pos].mark) {
tree[pos << 1].v += tree[pos].mark;
tree[pos << 1].mark += tree[pos].mark;
tree[pos << 1 | 1].v += tree[pos].mark;
tree[pos << 1 | 1].mark += tree[pos].mark;
tree[pos].mark = 0;
}
}
void update(int L, int R, int v, int pos) {
if (L <= tree[pos].l && R >= tree[pos].r) {
tree[pos].v += v;
tree[pos].mark += v;
return;
}
push_down(pos);
int mid = (tree[pos].l + tree[pos].r) >> 1;
if (L <= mid) update(L, R, v, pos << 1);
if (R > mid) update(L, R, v, pos << 1 | 1);
push_up(pos);
}
int query(int L, int R, int pos) {
if (L <= tree[pos].l && R >= tree[pos].r) {
return tree[pos].v;
}
push_down(pos);
int mid = (tree[pos].l + tree[pos].r) >> 1;
int ans = 0;
if (L <= mid) ans = max(ans, query(L, R, pos << 1));
if (R > mid) ans = max(ans, query(L, R, pos << 1 | 1));
return ans;
}
int nex[1000005];
void init() {
stack<int> s;
for (int i = n; i >= 1; i--) {
while (!s.empty() && a[s.top()] <= a[i]) s.pop();
if (!s.empty()) {
int p = s.top();
add_edge(p, i);
add_edge(i, p);
} else {
add_edge(n + 1, i);
add_edge(i, n + 1);
}
s.push(i);
}
dfs(n + 1, 0);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
init();
build(1, n + 1, 1);
for (int i = 1; i <= k; i++) {
update(lb[i], rb[i], 1, 1);
}
for (int i = 1; i + k - 1 <= n; i++) {
int r = i + k - 1;
printf("%d ", query(1, n + 1, 1));
update(lb[i], rb[i], -1, 1);
update(lb[r + 1], rb[r + 1], 1, 1);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = int(1e6 + 10);
int a[N];
set<pair<int, int> > mst;
int in[N], out[N];
int n, k;
int T = 1;
vector<int> adj[N];
void dfs(int u) {
in[u] = T;
for (int v : adj[u]) {
T++;
dfs(v);
}
out[u] = T;
}
int segtree[N << 3];
int lazzy[N << 3];
void push(int node) {
segtree[node] += lazzy[node];
int lf = node << 1;
int rg = lf + 1;
lazzy[lf] += lazzy[node];
lazzy[rg] += lazzy[node];
lazzy[node] = 0;
}
void update(int node, int i, int j, int l, int r, int val) {
if (lazzy[node]) {
push(node);
}
if (i > r || j < l) {
return;
}
if (l <= i && j <= r) {
lazzy[node] += val;
push(node);
} else {
int mid = (i + j) >> 1;
int lf = node << 1;
int rg = lf + 1;
update(lf, i, mid, l, r, val);
update(rg, mid + 1, j, l, r, val);
segtree[node] = max(segtree[lf], segtree[rg]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
while (mst.size() && (*mst.begin()).first < a[i]) {
adj[i].push_back((*mst.begin()).second);
mst.erase(mst.begin());
}
mst.insert({a[i], i});
}
while (mst.size()) {
adj[n + 1].push_back((*mst.begin()).second);
mst.erase(mst.begin());
}
dfs(n + 1);
for (int i = 1; i <= k - 1; i++) {
update(1, 1, n + 1, in[i], out[i], 1);
}
for (int i = k; i <= n; i++) {
update(1, 1, n + 1, in[i], out[i], 1);
cout << segtree[1] << " ";
update(1, 1, n + 1, in[i - k + 1], out[i - k + 1], -1);
}
cout << endl;
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = int(1e6 + 10);
int a[N];
set<pair<int, int> > mst;
int in[N], out[N];
int n, k;
int T = 1;
vector<int> adj[N];
void dfs(int u) {
in[u] = T;
for (int v : adj[u]) {
T++;
dfs(v);
}
out[u] = T;
}
int segtree[N << 3];
int lazzy[N << 3];
void push(int node) {
segtree[node] += lazzy[node];
int lf = node << 1;
int rg = lf + 1;
lazzy[lf] += lazzy[node];
lazzy[rg] += lazzy[node];
lazzy[node] = 0;
}
void update(int node, int i, int j, int l, int r, int val) {
if (lazzy[node]) {
push(node);
}
if (i > r || j < l) {
return;
}
if (l <= i && j <= r) {
lazzy[node] += val;
push(node);
} else {
int mid = (i + j) >> 1;
int lf = node << 1;
int rg = lf + 1;
update(lf, i, mid, l, r, val);
update(rg, mid + 1, j, l, r, val);
segtree[node] = max(segtree[lf], segtree[rg]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
while (mst.size() && (*mst.begin()).first < a[i]) {
adj[i].push_back((*mst.begin()).second);
mst.erase(mst.begin());
}
mst.insert({a[i], i});
}
while (mst.size()) {
adj[n + 1].push_back((*mst.begin()).second);
mst.erase(mst.begin());
}
dfs(n + 1);
for (int i = 1; i <= k - 1; i++) {
update(1, 1, n + 1, in[i], out[i], 1);
}
for (int i = k; i <= n; i++) {
update(1, 1, n + 1, in[i], out[i], 1);
cout << segtree[1] << " ";
update(1, 1, n + 1, in[i - k + 1], out[i - k + 1], -1);
}
cout << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> a;
inline bool read() {
if (!(cin >> n >> k)) return false;
a.assign(n, 0);
for (int i = int(0); i < int(n); i++) cin >> a[i];
return true;
}
int T = 0;
vector<vector<int> > g;
vector<int> tin, tout;
void dfs(int v) {
tin[v] = T++;
for (int to : g[v]) dfs(to);
tout[v] = T;
}
vector<int> Tmax, Tadd;
inline void push(int v) {
Tadd[2 * v + 1] += Tadd[v];
Tadd[2 * v + 2] += Tadd[v];
Tadd[v] = 0;
}
inline int getmax(int v) { return Tmax[v] + Tadd[v]; }
void addVal(int v, int l, int r, int lf, int rg, int val) {
if (l == lf && r == rg) {
Tadd[v] += val;
return;
}
int mid = (l + r) >> 1;
push(v);
if (lf < mid) addVal(2 * v + 1, l, mid, lf, min(mid, rg), val);
if (rg > mid) addVal(2 * v + 2, mid, r, max(lf, mid), rg, val);
Tmax[v] = max(getmax(2 * v + 1), getmax(2 * v + 2));
}
inline void solve() {
g.resize(n + 1);
tin.resize(n + 1, 0);
tout.resize(n + 1, 0);
vector<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.back()] <= a[i]) st.pop_back();
int nxt = st.empty() ? n : st.back();
g[nxt].push_back(i);
st.push_back(i);
}
dfs(n);
Tmax.assign(4 * (n + 1), 0);
Tadd.assign(4 * (n + 1), 0);
for (int i = int(0); i < int(k - 1); i++)
addVal(0, 0, n + 1, tin[i], tout[i], +1);
for (int l = 0; l + k <= n; l++) {
addVal(0, 0, n + 1, tin[l + k - 1], tout[l + k - 1], +1);
cout << getmax(0) << " ";
addVal(0, 0, n + 1, tin[l], tout[l], -1);
}
cout << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
if (read()) {
solve();
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> a;
inline bool read() {
if (!(cin >> n >> k)) return false;
a.assign(n, 0);
for (int i = int(0); i < int(n); i++) cin >> a[i];
return true;
}
int T = 0;
vector<vector<int> > g;
vector<int> tin, tout;
void dfs(int v) {
tin[v] = T++;
for (int to : g[v]) dfs(to);
tout[v] = T;
}
vector<int> Tmax, Tadd;
inline void push(int v) {
Tadd[2 * v + 1] += Tadd[v];
Tadd[2 * v + 2] += Tadd[v];
Tadd[v] = 0;
}
inline int getmax(int v) { return Tmax[v] + Tadd[v]; }
void addVal(int v, int l, int r, int lf, int rg, int val) {
if (l == lf && r == rg) {
Tadd[v] += val;
return;
}
int mid = (l + r) >> 1;
push(v);
if (lf < mid) addVal(2 * v + 1, l, mid, lf, min(mid, rg), val);
if (rg > mid) addVal(2 * v + 2, mid, r, max(lf, mid), rg, val);
Tmax[v] = max(getmax(2 * v + 1), getmax(2 * v + 2));
}
inline void solve() {
g.resize(n + 1);
tin.resize(n + 1, 0);
tout.resize(n + 1, 0);
vector<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.back()] <= a[i]) st.pop_back();
int nxt = st.empty() ? n : st.back();
g[nxt].push_back(i);
st.push_back(i);
}
dfs(n);
Tmax.assign(4 * (n + 1), 0);
Tadd.assign(4 * (n + 1), 0);
for (int i = int(0); i < int(k - 1); i++)
addVal(0, 0, n + 1, tin[i], tout[i], +1);
for (int l = 0; l + k <= n; l++) {
addVal(0, 0, n + 1, tin[l + k - 1], tout[l + k - 1], +1);
cout << getmax(0) << " ";
addVal(0, 0, n + 1, tin[l], tout[l], -1);
}
cout << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
if (read()) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
const int MAXSEG = 1 << 21;
const int INFTY = 1 << 29;
int N, K;
int A[MAXN];
vector<int> adj[MAXN];
int deg[MAXN];
int R[MAXN][2], ind = 0;
int seg[MAXSEG];
int lazy[MAXSEG];
void dfs(int cur) {
R[cur][0] = ++ind;
for (auto nxt : adj[cur]) dfs(nxt);
R[cur][1] = ind;
}
void down(int node) {
if (lazy[node] != 0) {
seg[2 * node] += lazy[node];
seg[2 * node + 1] += lazy[node];
lazy[2 * node] += lazy[node];
lazy[2 * node + 1] += lazy[node];
lazy[node] = 0;
}
}
void range_update(int node, int left, int right, int ql, int qr) {
if (left > right || ql > qr || right < ql || qr < left) return;
if (ql <= left && right <= qr) {
seg[node]++;
lazy[node]++;
return;
}
down(node);
int mid = (left + right) / 2;
range_update(2 * node, left, mid, ql, qr);
range_update(2 * node + 1, mid + 1, right, ql, qr);
seg[node] = max(seg[2 * node], seg[2 * node + 1]);
}
void point_update(int node, int left, int right, int idx) {
if (left == idx && idx == right) {
seg[node] = -INFTY;
return;
}
down(node);
int mid = (left + right) / 2;
if (idx <= mid)
point_update(2 * node, left, mid, idx);
else
point_update(2 * node + 1, mid + 1, right, idx);
seg[node] = max(seg[2 * node], seg[2 * node + 1]);
}
int main() {
scanf("%d%d", &N, &K);
for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
vector<int> stk;
for (int i = N; i >= 1; i--) {
while (!stk.empty() && A[i] >= A[stk.back()]) stk.pop_back();
if (!stk.empty()) {
adj[stk.back()].push_back(i);
deg[i]++;
}
stk.push_back(i);
}
for (int i = 1; i <= N; i++)
if (deg[i] == 0) dfs(i);
for (int i = 1; i <= K; i++) range_update(1, 1, N, R[i][0], R[i][1]);
printf("%d", seg[1]);
for (int i = 2; i + K - 1 <= N; i++) {
point_update(1, 1, N, R[i - 1][0]);
range_update(1, 1, N, R[i + K - 1][0], R[i + K - 1][1]);
printf(" %d", seg[1]);
}
printf("\n");
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
const int MAXSEG = 1 << 21;
const int INFTY = 1 << 29;
int N, K;
int A[MAXN];
vector<int> adj[MAXN];
int deg[MAXN];
int R[MAXN][2], ind = 0;
int seg[MAXSEG];
int lazy[MAXSEG];
void dfs(int cur) {
R[cur][0] = ++ind;
for (auto nxt : adj[cur]) dfs(nxt);
R[cur][1] = ind;
}
void down(int node) {
if (lazy[node] != 0) {
seg[2 * node] += lazy[node];
seg[2 * node + 1] += lazy[node];
lazy[2 * node] += lazy[node];
lazy[2 * node + 1] += lazy[node];
lazy[node] = 0;
}
}
void range_update(int node, int left, int right, int ql, int qr) {
if (left > right || ql > qr || right < ql || qr < left) return;
if (ql <= left && right <= qr) {
seg[node]++;
lazy[node]++;
return;
}
down(node);
int mid = (left + right) / 2;
range_update(2 * node, left, mid, ql, qr);
range_update(2 * node + 1, mid + 1, right, ql, qr);
seg[node] = max(seg[2 * node], seg[2 * node + 1]);
}
void point_update(int node, int left, int right, int idx) {
if (left == idx && idx == right) {
seg[node] = -INFTY;
return;
}
down(node);
int mid = (left + right) / 2;
if (idx <= mid)
point_update(2 * node, left, mid, idx);
else
point_update(2 * node + 1, mid + 1, right, idx);
seg[node] = max(seg[2 * node], seg[2 * node + 1]);
}
int main() {
scanf("%d%d", &N, &K);
for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
vector<int> stk;
for (int i = N; i >= 1; i--) {
while (!stk.empty() && A[i] >= A[stk.back()]) stk.pop_back();
if (!stk.empty()) {
adj[stk.back()].push_back(i);
deg[i]++;
}
stk.push_back(i);
}
for (int i = 1; i <= N; i++)
if (deg[i] == 0) dfs(i);
for (int i = 1; i <= K; i++) range_update(1, 1, N, R[i][0], R[i][1]);
printf("%d", seg[1]);
for (int i = 2; i + K - 1 <= N; i++) {
point_update(1, 1, N, R[i - 1][0]);
range_update(1, 1, N, R[i + K - 1][0], R[i + K - 1][1]);
printf(" %d", seg[1]);
}
printf("\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T1, typename T2>
inline void chkmin(T1& x, const T2& y) {
if (y < x) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1& x, const T2& y) {
if (x < y) x = y;
}
mt19937 rnd(time(0));
int n, k;
const int MAXN = 2e6 + 228;
const int lgg = 20;
int nxt[MAXN];
int root;
int a[MAXN];
vector<int> g[MAXN];
int tin[MAXN];
int tout[MAXN];
int tl = 0;
int t[4 * MAXN];
int add[4 * MAXN];
void push(int v, int l, int r) {
if (l != r - 1) {
add[2 * v + 1] += add[v];
add[2 * v + 2] += add[v];
}
t[v] += add[v];
add[v] = 0;
}
void upd(int v, int l, int r, int ql, int qr, int x) {
push(v, l, r);
if (l >= qr || ql >= r) return;
if (l >= ql && r <= qr) {
add[v] += x;
push(v, l, r);
return;
}
upd(2 * v + 1, l, (l + r) / 2, ql, qr, x);
upd(2 * v + 2, (l + r) / 2, r, ql, qr, x);
t[v] = max(t[2 * v + 1], t[2 * v + 2]);
}
void dfs(int v, int p) {
tin[v] = tl++;
for (int i : g[v]) {
if (i != p) dfs(i, v);
}
tout[v] = tl++;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout.precision(20), cout.setf(ios::fixed);
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<pair<int, int>> st;
root = n;
fill(nxt, nxt + n, n);
for (int i = 0; i < n; ++i) {
while (st.size() && st.back().first < a[i]) {
nxt[st.back().second] = i;
st.pop_back();
}
st.push_back({a[i], i});
}
for (int i = 0; i < n; ++i) {
g[nxt[i]].push_back(i);
}
dfs(root, root);
for (int i = 0; i < k; ++i) {
upd(0, 0, MAXN, tin[i], tout[i], 1);
}
for (int i = 0; i + k - 1 < n; ++i) {
cout << t[0] << ' ';
if (i + k < n) {
upd(0, 0, MAXN, tin[i + k], tout[i + k], 1);
}
upd(0, 0, MAXN, tin[i], tout[i], -2);
}
}
| ### Prompt
Develop a solution in CPP to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T1, typename T2>
inline void chkmin(T1& x, const T2& y) {
if (y < x) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1& x, const T2& y) {
if (x < y) x = y;
}
mt19937 rnd(time(0));
int n, k;
const int MAXN = 2e6 + 228;
const int lgg = 20;
int nxt[MAXN];
int root;
int a[MAXN];
vector<int> g[MAXN];
int tin[MAXN];
int tout[MAXN];
int tl = 0;
int t[4 * MAXN];
int add[4 * MAXN];
void push(int v, int l, int r) {
if (l != r - 1) {
add[2 * v + 1] += add[v];
add[2 * v + 2] += add[v];
}
t[v] += add[v];
add[v] = 0;
}
void upd(int v, int l, int r, int ql, int qr, int x) {
push(v, l, r);
if (l >= qr || ql >= r) return;
if (l >= ql && r <= qr) {
add[v] += x;
push(v, l, r);
return;
}
upd(2 * v + 1, l, (l + r) / 2, ql, qr, x);
upd(2 * v + 2, (l + r) / 2, r, ql, qr, x);
t[v] = max(t[2 * v + 1], t[2 * v + 2]);
}
void dfs(int v, int p) {
tin[v] = tl++;
for (int i : g[v]) {
if (i != p) dfs(i, v);
}
tout[v] = tl++;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout.precision(20), cout.setf(ios::fixed);
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<pair<int, int>> st;
root = n;
fill(nxt, nxt + n, n);
for (int i = 0; i < n; ++i) {
while (st.size() && st.back().first < a[i]) {
nxt[st.back().second] = i;
st.pop_back();
}
st.push_back({a[i], i});
}
for (int i = 0; i < n; ++i) {
g[nxt[i]].push_back(i);
}
dfs(root, root);
for (int i = 0; i < k; ++i) {
upd(0, 0, MAXN, tin[i], tout[i], 1);
}
for (int i = 0; i + k - 1 < n; ++i) {
cout << t[0] << ' ';
if (i + k < n) {
upd(0, 0, MAXN, tin[i + k], tout[i + k], 1);
}
upd(0, 0, MAXN, tin[i], tout[i], -2);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000006;
const int inf = 1000000009;
int n, k, a[N], go[N], in[N], out[N], tin = 0;
vector<int> g[N];
template <class T>
struct lazy_seg {
public:
vector<T> st, lazy;
lazy_seg(int n) { st.resize(4 * n + 1, 0), lazy.resize(4 * n + 1, 0); }
lazy_seg() = default;
void push(int l, int r, int curr) {
if (lazy[curr] != 0) {
st[curr] += lazy[curr];
if (l != r)
lazy[2 * curr] += lazy[curr], lazy[2 * curr + 1] += lazy[curr];
lazy[curr] = 0;
}
}
void pull(int l, int r, int curr) {
push(l, r, curr);
int mid = l + (r - l) / 2;
if (l != r) push(l, mid, 2 * curr), push(mid + 1, r, 2 * curr + 1);
st[curr] = max(st[2 * curr], st[2 * curr + 1]);
}
void upd(int x, int y, T v, int l, int r, int curr) {
int mid = (l + r) / 2;
push(l, r, curr);
if (l == x && r == y) {
lazy[curr] += v;
push(l, r, curr);
return;
}
if (y <= mid)
upd(x, y, v, l, mid, 2 * curr);
else if (x > mid)
upd(x, y, v, mid + 1, r, 2 * curr + 1);
else
upd(x, mid, v, l, mid, 2 * curr),
upd(mid + 1, y, v, mid + 1, r, 2 * curr + 1);
pull(l, r, curr);
}
T qry(int x, int y, int l, int r, int curr) {
int mid = (l + r) / 2;
push(l, r, curr);
if (l == x && r == y) return st[curr];
if (y <= mid)
return qry(x, y, l, mid, 2 * curr);
else if (x > mid)
return qry(x, y, mid + 1, r, 2 * curr + 1);
return max(qry(x, mid, l, mid, 2 * curr),
qry(mid + 1, y, mid + 1, r, 2 * curr + 1));
}
void build(int l, int r, int curr) {
int mid = (l + r) / 2;
if (l == r) {
st[curr] = 0;
return;
}
build(l, mid, 2 * curr);
build(mid + 1, r, 2 * curr + 1);
st[curr] = (st[2 * curr] + st[2 * curr + 1]);
}
};
bool vis[N];
void dfs(int x) {
vis[x] = true;
in[x] = ++tin;
for (auto w : g[x]) {
if (!vis[w]) dfs(w);
}
out[x] = tin;
}
int32_t main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
stack<int> cur;
lazy_seg<int> seg(n);
for (int i = n; i >= 1; i--) {
while ((int)(cur).size() && a[i] >= a[cur.top()]) cur.pop();
if ((int)(cur).size()) {
g[i].push_back(cur.top());
g[cur.top()].push_back(i);
}
cur.push(i);
}
for (int i = n; i >= 1; i--) {
if (!vis[i]) {
dfs(i);
}
}
for (int i = 1; i <= n; i++) {
if (i > k) {
seg.upd(in[i - k], out[i - k], -1, 1, n, 1);
}
seg.upd(in[i], out[i], 1, 1, n, 1);
if (i >= k) {
printf("%d ", seg.qry(1, n, 1, n, 1));
}
}
printf("\n");
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1000006;
const int inf = 1000000009;
int n, k, a[N], go[N], in[N], out[N], tin = 0;
vector<int> g[N];
template <class T>
struct lazy_seg {
public:
vector<T> st, lazy;
lazy_seg(int n) { st.resize(4 * n + 1, 0), lazy.resize(4 * n + 1, 0); }
lazy_seg() = default;
void push(int l, int r, int curr) {
if (lazy[curr] != 0) {
st[curr] += lazy[curr];
if (l != r)
lazy[2 * curr] += lazy[curr], lazy[2 * curr + 1] += lazy[curr];
lazy[curr] = 0;
}
}
void pull(int l, int r, int curr) {
push(l, r, curr);
int mid = l + (r - l) / 2;
if (l != r) push(l, mid, 2 * curr), push(mid + 1, r, 2 * curr + 1);
st[curr] = max(st[2 * curr], st[2 * curr + 1]);
}
void upd(int x, int y, T v, int l, int r, int curr) {
int mid = (l + r) / 2;
push(l, r, curr);
if (l == x && r == y) {
lazy[curr] += v;
push(l, r, curr);
return;
}
if (y <= mid)
upd(x, y, v, l, mid, 2 * curr);
else if (x > mid)
upd(x, y, v, mid + 1, r, 2 * curr + 1);
else
upd(x, mid, v, l, mid, 2 * curr),
upd(mid + 1, y, v, mid + 1, r, 2 * curr + 1);
pull(l, r, curr);
}
T qry(int x, int y, int l, int r, int curr) {
int mid = (l + r) / 2;
push(l, r, curr);
if (l == x && r == y) return st[curr];
if (y <= mid)
return qry(x, y, l, mid, 2 * curr);
else if (x > mid)
return qry(x, y, mid + 1, r, 2 * curr + 1);
return max(qry(x, mid, l, mid, 2 * curr),
qry(mid + 1, y, mid + 1, r, 2 * curr + 1));
}
void build(int l, int r, int curr) {
int mid = (l + r) / 2;
if (l == r) {
st[curr] = 0;
return;
}
build(l, mid, 2 * curr);
build(mid + 1, r, 2 * curr + 1);
st[curr] = (st[2 * curr] + st[2 * curr + 1]);
}
};
bool vis[N];
void dfs(int x) {
vis[x] = true;
in[x] = ++tin;
for (auto w : g[x]) {
if (!vis[w]) dfs(w);
}
out[x] = tin;
}
int32_t main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
stack<int> cur;
lazy_seg<int> seg(n);
for (int i = n; i >= 1; i--) {
while ((int)(cur).size() && a[i] >= a[cur.top()]) cur.pop();
if ((int)(cur).size()) {
g[i].push_back(cur.top());
g[cur.top()].push_back(i);
}
cur.push(i);
}
for (int i = n; i >= 1; i--) {
if (!vis[i]) {
dfs(i);
}
}
for (int i = 1; i <= n; i++) {
if (i > k) {
seg.upd(in[i - k], out[i - k], -1, 1, n, 1);
}
seg.upd(in[i], out[i], 1, 1, n, 1);
if (i >= k) {
printf("%d ", seg.qry(1, n, 1, n, 1));
}
}
printf("\n");
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a) {
in >> a.first >> a.second;
return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> a) {
out << a.first << " " << a.second;
return out;
}
template <typename T, typename T1>
T maxs(T &a, T1 b) {
if (b > a) a = b;
return a;
}
template <typename T, typename T1>
T mins(T &a, T1 b) {
if (b < a) a = b;
return a;
}
const long long N = 1000005;
struct node {
long long a = 0;
node(long long val = 0) { a = val; }
void merge(node &n1, node &n2) { this->a = max(n1.a, n2.a); }
};
struct update {
long long val = 0;
update(long long t = 0) { val = t; }
void combine(update &par, long long tl, long long tr) { val += par.val; }
void apply(node &node) {
node.a += val;
val = 0;
}
};
template <typename node, typename update>
struct segtree {
node t[4 * N];
bool lazy[4 * N];
update zaker[4 * N];
long long tl[4 * N];
long long tr[4 * N];
node nul;
inline void pushdown(long long v) {
if (lazy[v]) {
apply(zaker[v], v);
lazy[v] = 0;
zaker[v].apply(t[v]);
}
}
inline void apply(update &u, long long v) {
if (tl[v] != tr[v]) {
lazy[2 * v] = lazy[2 * v + 1] = 1;
zaker[2 * v].combine(u, tl[2 * v], tr[2 * v]);
zaker[2 * v + 1].combine(u, tl[2 * v + 1], tr[2 * v + 1]);
}
}
void build(long long v, long long start, long long end) {
tl[v] = start;
tr[v] = end;
if (start == end) {
t[v].a = 0;
} else {
long long m = (start + end) / 2;
build(2 * v, start, m);
build(2 * v + 1, m + 1, end);
t[v].merge(t[2 * v], t[2 * v + 1]);
}
}
void zeno(long long v, long long l, long long r, update val) {
pushdown(v);
if (tr[v] < l || tl[v] > r) return;
if (l <= tl[v] && tr[v] <= r) {
t[v].a += val.val;
apply(val, v);
return;
}
zeno(2 * v, l, r, val);
zeno(2 * v + 1, l, r, val);
t[v].merge(t[2 * v], t[2 * v + 1]);
}
node query(long long v, long long l, long long r) {
if (tr[v] < l || tl[v] > r) return nul;
pushdown(v);
if (l <= tl[v] && tr[v] <= r) {
return t[v];
}
node a = query(2 * v, l, r);
node b = query(2 * v + 1, l, r);
node ans;
ans.merge(a, b);
return ans;
}
public:
node query(long long l, long long r) { return query(1, l, r); }
void upd(long long l, long long r, update val) { return zeno(1, l, r, val); }
};
segtree<node, update> seg;
vector<long long> g[N];
long long eu[N], tin[N], tout[N];
long long t = 0;
void dfs(long long u, long long p) {
tin[u] = ++t;
eu[t] = u;
for (auto i : g[u]) {
if (i != p) dfs(i, u);
}
tout[u] = t;
}
long long solve() {
long long n, k;
cin >> n >> k;
vector<long long> a(n + 1);
for (long long i = 1; i < n + 1; i++) {
cin >> a[i];
}
vector<long long> nxt(n + 1);
stack<pair<long long, long long> > st;
for (long long i = 1; i <= n; i++) {
while (!st.empty() && st.top().first < a[i]) {
nxt[st.top().second] = i;
st.pop();
}
st.push({a[i], i});
}
while (!st.empty()) {
nxt[st.top().second] = n + 1;
st.pop();
}
for (long long i = 1; i <= n; i++) {
g[nxt[i]].push_back(i);
}
dfs(n + 1, 0);
seg.build(1, 1, n + 1);
update upd1, upd2;
upd1.val = 1, upd2.val = -1;
for (long long i = 1; i <= k; i++) seg.upd(tin[i], tout[i], upd1);
cout << seg.query(1, n + 1).a << " ";
for (long long i = 2; i + k - 1 <= n; i++) {
seg.upd(tin[i - 1], tout[i - 1], upd2);
seg.upd(tin[i + k - 1], tout[i + k - 1], upd1);
cout << seg.query(1, n + 1).a << " ";
}
return 0;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a) {
in >> a.first >> a.second;
return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> a) {
out << a.first << " " << a.second;
return out;
}
template <typename T, typename T1>
T maxs(T &a, T1 b) {
if (b > a) a = b;
return a;
}
template <typename T, typename T1>
T mins(T &a, T1 b) {
if (b < a) a = b;
return a;
}
const long long N = 1000005;
struct node {
long long a = 0;
node(long long val = 0) { a = val; }
void merge(node &n1, node &n2) { this->a = max(n1.a, n2.a); }
};
struct update {
long long val = 0;
update(long long t = 0) { val = t; }
void combine(update &par, long long tl, long long tr) { val += par.val; }
void apply(node &node) {
node.a += val;
val = 0;
}
};
template <typename node, typename update>
struct segtree {
node t[4 * N];
bool lazy[4 * N];
update zaker[4 * N];
long long tl[4 * N];
long long tr[4 * N];
node nul;
inline void pushdown(long long v) {
if (lazy[v]) {
apply(zaker[v], v);
lazy[v] = 0;
zaker[v].apply(t[v]);
}
}
inline void apply(update &u, long long v) {
if (tl[v] != tr[v]) {
lazy[2 * v] = lazy[2 * v + 1] = 1;
zaker[2 * v].combine(u, tl[2 * v], tr[2 * v]);
zaker[2 * v + 1].combine(u, tl[2 * v + 1], tr[2 * v + 1]);
}
}
void build(long long v, long long start, long long end) {
tl[v] = start;
tr[v] = end;
if (start == end) {
t[v].a = 0;
} else {
long long m = (start + end) / 2;
build(2 * v, start, m);
build(2 * v + 1, m + 1, end);
t[v].merge(t[2 * v], t[2 * v + 1]);
}
}
void zeno(long long v, long long l, long long r, update val) {
pushdown(v);
if (tr[v] < l || tl[v] > r) return;
if (l <= tl[v] && tr[v] <= r) {
t[v].a += val.val;
apply(val, v);
return;
}
zeno(2 * v, l, r, val);
zeno(2 * v + 1, l, r, val);
t[v].merge(t[2 * v], t[2 * v + 1]);
}
node query(long long v, long long l, long long r) {
if (tr[v] < l || tl[v] > r) return nul;
pushdown(v);
if (l <= tl[v] && tr[v] <= r) {
return t[v];
}
node a = query(2 * v, l, r);
node b = query(2 * v + 1, l, r);
node ans;
ans.merge(a, b);
return ans;
}
public:
node query(long long l, long long r) { return query(1, l, r); }
void upd(long long l, long long r, update val) { return zeno(1, l, r, val); }
};
segtree<node, update> seg;
vector<long long> g[N];
long long eu[N], tin[N], tout[N];
long long t = 0;
void dfs(long long u, long long p) {
tin[u] = ++t;
eu[t] = u;
for (auto i : g[u]) {
if (i != p) dfs(i, u);
}
tout[u] = t;
}
long long solve() {
long long n, k;
cin >> n >> k;
vector<long long> a(n + 1);
for (long long i = 1; i < n + 1; i++) {
cin >> a[i];
}
vector<long long> nxt(n + 1);
stack<pair<long long, long long> > st;
for (long long i = 1; i <= n; i++) {
while (!st.empty() && st.top().first < a[i]) {
nxt[st.top().second] = i;
st.pop();
}
st.push({a[i], i});
}
while (!st.empty()) {
nxt[st.top().second] = n + 1;
st.pop();
}
for (long long i = 1; i <= n; i++) {
g[nxt[i]].push_back(i);
}
dfs(n + 1, 0);
seg.build(1, 1, n + 1);
update upd1, upd2;
upd1.val = 1, upd2.val = -1;
for (long long i = 1; i <= k; i++) seg.upd(tin[i], tout[i], upd1);
cout << seg.query(1, n + 1).a << " ";
for (long long i = 2; i + k - 1 <= n; i++) {
seg.upd(tin[i - 1], tout[i - 1], upd2);
seg.upd(tin[i + k - 1], tout[i + k - 1], upd1);
cout << seg.query(1, n + 1).a << " ";
}
return 0;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
string to_string(const vector<T>& vc, int w) {
if (vc.empty()) return "";
if (w + 1 == vc.size()) return to_string(vc[w]);
return to_string(vc[w]) + "," + to_string(vc, w + 1);
}
template <typename T>
string to_string(const vector<T>& vc) {
return "{" + to_string(vc, 0) + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
class DebugStream {
} LOG;
template <typename T>
DebugStream& operator<<(DebugStream& s, const T&) {
return s;
}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
bs = 29;
int T[maxN << 1], d[maxN], n, arr[maxN], ky[maxN], k;
void apply(int w, int x) {
T[w] += x;
if (w < n) d[w] += x;
}
void pushDown(int w) {
for (int u = lg; u > 0; u--) {
int c = w >> u;
if (d[c]) {
apply(c << 1, d[c]);
apply(c << 1 | 1, d[c]);
d[c] = 0;
}
}
}
void pullUp(int w) {
while (w > 1) {
w >>= 1;
T[w] = max(T[w << 1], T[w << 1 | 1]) + d[w];
}
}
void upd(int l, int r) {
l += n, r += n;
int l0 = l, r0 = r;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) apply(l++, 1);
if (r & 1) apply(--r, 1);
}
pullUp(l0);
pullUp(r0 - 1);
}
int qry(int l, int r) {
l += n, r += n;
pushDown(l);
pushDown(r - 1);
int ret = 0;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) ret = max(ret, T[l++]);
if (r & 1) ret = max(ret, T[--r]);
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> arr[i];
stack<int> stc;
stc.push(-1);
for (int i = 0; i < n; i++) {
while (stc.top() >= 0 && arr[stc.top()] < arr[i]) stc.pop();
ky[i] = stc.top() + 1;
stc.push(i);
}
for (int i = 0; i + 1 < k; i++) upd(ky[i], i + 1);
for (int i = k - 1; i < n; i++) {
upd(ky[i], i + 1);
cout << qry(i - k + 1, i + 1) << ' ';
}
cout << '\n';
}
| ### Prompt
Please formulate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
string to_string(const vector<T>& vc, int w) {
if (vc.empty()) return "";
if (w + 1 == vc.size()) return to_string(vc[w]);
return to_string(vc[w]) + "," + to_string(vc, w + 1);
}
template <typename T>
string to_string(const vector<T>& vc) {
return "{" + to_string(vc, 0) + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
class DebugStream {
} LOG;
template <typename T>
DebugStream& operator<<(DebugStream& s, const T&) {
return s;
}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
bs = 29;
int T[maxN << 1], d[maxN], n, arr[maxN], ky[maxN], k;
void apply(int w, int x) {
T[w] += x;
if (w < n) d[w] += x;
}
void pushDown(int w) {
for (int u = lg; u > 0; u--) {
int c = w >> u;
if (d[c]) {
apply(c << 1, d[c]);
apply(c << 1 | 1, d[c]);
d[c] = 0;
}
}
}
void pullUp(int w) {
while (w > 1) {
w >>= 1;
T[w] = max(T[w << 1], T[w << 1 | 1]) + d[w];
}
}
void upd(int l, int r) {
l += n, r += n;
int l0 = l, r0 = r;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) apply(l++, 1);
if (r & 1) apply(--r, 1);
}
pullUp(l0);
pullUp(r0 - 1);
}
int qry(int l, int r) {
l += n, r += n;
pushDown(l);
pushDown(r - 1);
int ret = 0;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) ret = max(ret, T[l++]);
if (r & 1) ret = max(ret, T[--r]);
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> arr[i];
stack<int> stc;
stc.push(-1);
for (int i = 0; i < n; i++) {
while (stc.top() >= 0 && arr[stc.top()] < arr[i]) stc.pop();
ky[i] = stc.top() + 1;
stc.push(i);
}
for (int i = 0; i + 1 < k; i++) upd(ky[i], i + 1);
for (int i = k - 1; i < n; i++) {
upd(ky[i], i + 1);
cout << qry(i - k + 1, i + 1) << ' ';
}
cout << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps = 1e-9;
const int mod = 1e9 + 7;
int n, k, a[1000005], nxt[1000005];
vector<int> g[1000005];
int td[1000005], tf[1000005], v, lev[1000005];
bool di[1000005];
int lca[1000005][25];
void dfs(int nod) {
td[nod] = tf[nod] = ++v;
for (int i = 1; i <= 20; i++) lca[nod][i] = lca[lca[nod][i - 1]][i - 1];
for (int newn : g[nod]) {
lev[newn] = lev[nod] + 1;
lca[newn][0] = nod;
dfs(newn);
tf[nod] = tf[newn];
}
}
int t[1000005 * 4];
void update(int ini, int fin, int lv, int pos, int v) {
if (ini == fin) {
t[lv] = v;
return;
}
int piv = (ini + fin) >> 1;
if (pos <= piv)
update(ini, piv, lv * 2, pos, v);
else
update(piv + 1, fin, lv * 2 + 1, pos, v);
t[lv] = max(t[lv * 2], t[lv * 2 + 1]);
}
int query(int ini, int fin, int lv, int a, int b) {
if (a > fin || b < ini) return 0;
if (a <= ini && b >= fin) return t[lv];
int piv = (ini + fin) >> 1;
return max(query(ini, piv, lv * 2, a, b),
query(piv + 1, fin, lv * 2 + 1, a, b));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
a[n + 1] = 1 << 30;
for (int i = n; i >= 1; i--) {
nxt[i] = i + 1;
while (a[nxt[i]] <= a[i]) nxt[i] = nxt[nxt[i]];
if (nxt[i] != n + 1) {
g[nxt[i]].push_back(i);
di[i] = true;
}
}
for (int i = 1; i <= n; i++)
if (!di[i]) {
lev[i] = 1;
dfs(i);
}
multiset<int> m;
vector<int> s(n + 1);
auto agrega = [&](int nod, int l, int r) {
int val = 0;
update(1, n, 1, td[nod], lev[nod]);
for (int newn : g[nod]) {
if (newn > r || newn < l) continue;
m.erase(m.find(s[newn]));
val = max(val, s[newn]);
}
s[nod] = val + 1;
m.insert(s[nod]);
};
auto find_root = [&](int nod, int l, int r) {
int root = nod;
for (int i = 20; i >= 0; i--)
if (lca[root][i] && lca[root][i] <= r) root = lca[root][i];
return root;
};
auto elimina = [&](int nod, int l, int r) {
update(1, n, 1, td[nod], 0);
int root = find_root(nod, l, r);
m.erase(m.find(s[root]));
if (nod == root) return;
s[root] = query(1, n, 1, td[root], tf[root]) - lev[root] + 1;
m.insert(s[root]);
};
for (int i = 1; i <= n; i++) {
if (i > k) elimina(i - k, i - k, i - 1);
agrega(i, max(1, i - k + 1), i);
if (i >= k) cout << *m.rbegin() << " \n"[i == n];
}
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps = 1e-9;
const int mod = 1e9 + 7;
int n, k, a[1000005], nxt[1000005];
vector<int> g[1000005];
int td[1000005], tf[1000005], v, lev[1000005];
bool di[1000005];
int lca[1000005][25];
void dfs(int nod) {
td[nod] = tf[nod] = ++v;
for (int i = 1; i <= 20; i++) lca[nod][i] = lca[lca[nod][i - 1]][i - 1];
for (int newn : g[nod]) {
lev[newn] = lev[nod] + 1;
lca[newn][0] = nod;
dfs(newn);
tf[nod] = tf[newn];
}
}
int t[1000005 * 4];
void update(int ini, int fin, int lv, int pos, int v) {
if (ini == fin) {
t[lv] = v;
return;
}
int piv = (ini + fin) >> 1;
if (pos <= piv)
update(ini, piv, lv * 2, pos, v);
else
update(piv + 1, fin, lv * 2 + 1, pos, v);
t[lv] = max(t[lv * 2], t[lv * 2 + 1]);
}
int query(int ini, int fin, int lv, int a, int b) {
if (a > fin || b < ini) return 0;
if (a <= ini && b >= fin) return t[lv];
int piv = (ini + fin) >> 1;
return max(query(ini, piv, lv * 2, a, b),
query(piv + 1, fin, lv * 2 + 1, a, b));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
a[n + 1] = 1 << 30;
for (int i = n; i >= 1; i--) {
nxt[i] = i + 1;
while (a[nxt[i]] <= a[i]) nxt[i] = nxt[nxt[i]];
if (nxt[i] != n + 1) {
g[nxt[i]].push_back(i);
di[i] = true;
}
}
for (int i = 1; i <= n; i++)
if (!di[i]) {
lev[i] = 1;
dfs(i);
}
multiset<int> m;
vector<int> s(n + 1);
auto agrega = [&](int nod, int l, int r) {
int val = 0;
update(1, n, 1, td[nod], lev[nod]);
for (int newn : g[nod]) {
if (newn > r || newn < l) continue;
m.erase(m.find(s[newn]));
val = max(val, s[newn]);
}
s[nod] = val + 1;
m.insert(s[nod]);
};
auto find_root = [&](int nod, int l, int r) {
int root = nod;
for (int i = 20; i >= 0; i--)
if (lca[root][i] && lca[root][i] <= r) root = lca[root][i];
return root;
};
auto elimina = [&](int nod, int l, int r) {
update(1, n, 1, td[nod], 0);
int root = find_root(nod, l, r);
m.erase(m.find(s[root]));
if (nod == root) return;
s[root] = query(1, n, 1, td[root], tf[root]) - lev[root] + 1;
m.insert(s[root]);
};
for (int i = 1; i <= n; i++) {
if (i > k) elimina(i - k, i - k, i - 1);
agrega(i, max(1, i - k + 1), i);
if (i >= k) cout << *m.rbegin() << " \n"[i == n];
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000050;
const int M = 2 * N;
int lid[N], rid[N], tid;
vector<int> E[N];
void DFS(int u) {
lid[u] = ++tid;
for (int v : E[u]) DFS(v);
rid[u] = tid;
}
int ls[M], rs[M], tsz, root, mx[M], lzy[M];
void Set(int &c, int ss, int se, int qs, int qe, int f) {
if (qs > qe || qs > se || ss > qe) return;
if (!c) c = ++tsz;
if (qs <= ss && qe >= se) {
mx[c] += f;
lzy[c] += f;
return;
}
int mid = ss + se >> 1;
Set(ls[c], ss, mid, qs, qe, f);
Set(rs[c], mid + 1, se, qs, qe, f);
mx[c] = max(mx[ls[c]], mx[rs[c]]) + lzy[c];
}
int a[N], S[N], c;
int main() {
int n, k, i;
scanf("%i %i", &n, &k);
for (i = 1; i <= n; i++) scanf("%i", &a[i]);
for (i = n; i >= 1; i--) {
while (c && a[S[c]] <= a[i]) c--;
E[S[c]].push_back(i);
S[++c] = i;
}
DFS(0);
for (i = 1; i < k; i++) Set(root, 1, tid, lid[i], rid[i], 1);
for (i = k; i <= n; i++) {
Set(root, 1, tid, lid[i], rid[i], 1);
if (i > k) Set(root, 1, tid, lid[i - k], rid[i - k], -1);
printf("%i ", mx[root]);
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1000050;
const int M = 2 * N;
int lid[N], rid[N], tid;
vector<int> E[N];
void DFS(int u) {
lid[u] = ++tid;
for (int v : E[u]) DFS(v);
rid[u] = tid;
}
int ls[M], rs[M], tsz, root, mx[M], lzy[M];
void Set(int &c, int ss, int se, int qs, int qe, int f) {
if (qs > qe || qs > se || ss > qe) return;
if (!c) c = ++tsz;
if (qs <= ss && qe >= se) {
mx[c] += f;
lzy[c] += f;
return;
}
int mid = ss + se >> 1;
Set(ls[c], ss, mid, qs, qe, f);
Set(rs[c], mid + 1, se, qs, qe, f);
mx[c] = max(mx[ls[c]], mx[rs[c]]) + lzy[c];
}
int a[N], S[N], c;
int main() {
int n, k, i;
scanf("%i %i", &n, &k);
for (i = 1; i <= n; i++) scanf("%i", &a[i]);
for (i = n; i >= 1; i--) {
while (c && a[S[c]] <= a[i]) c--;
E[S[c]].push_back(i);
S[++c] = i;
}
DFS(0);
for (i = 1; i < k; i++) Set(root, 1, tid, lid[i], rid[i], 1);
for (i = k; i <= n; i++) {
Set(root, 1, tid, lid[i], rid[i], 1);
if (i > k) Set(root, 1, tid, lid[i - k], rid[i - k], -1);
printf("%i ", mx[root]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int s[1000100];
int e[1000100];
int a[1000100];
int cnt = 0, n;
vector<int> pre[1000100];
struct IT {
int d[4000100];
int lz[4000100];
void upd(int num, int l, int r, int x, int y, int val) {
if (l > r || l > y || r < x) return;
if (l >= x && r <= y) {
lz[num] += val;
d[num] += val;
return;
}
upd(num * 2, l, (l + r) / 2, x, y, val);
upd(num * 2 + 1, (l + r) / 2 + 1, r, x, y, val);
d[num] = max(d[num * 2], d[num * 2 + 1]) + lz[num];
}
int get(int num, int l, int r, int x, int y) {
if (l > r || l > y || r < x) return 0;
if (l >= x && r <= y) {
return d[num];
}
return max(get(num * 2, l, (l + r) / 2, x, y),
get(num * 2 + 1, (l + r) / 2 + 1, r, x, y)) +
lz[num];
}
} it;
void init_dfs(int x) {
s[x] = ++cnt;
for (int i = 0; i < pre[x].size(); i++) {
init_dfs(pre[x][i]);
}
e[x] = cnt;
}
void add(int x) { it.upd(1, 1, n, s[x], e[x], 1); }
void rmv(int x) { it.upd(1, 1, n, s[x], e[x], -1); }
stack<int> st;
int main() {
int k;
ios_base::sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = n; i >= 1; i--) {
while (st.size() && a[i] >= a[st.top()]) st.pop();
if (st.size()) {
pre[st.top()].push_back(i);
} else {
pre[n + 1].push_back(i);
}
st.push(i);
}
n++;
init_dfs(n);
for (int i = 1; i <= k; i++) {
add(i);
}
cout << it.get(1, 1, n, 1, n) << " ";
for (int i = k + 1; i < n; i++) {
add(i);
rmv(i - k);
cout << it.get(1, 1, n, 1, n) << " ";
}
}
| ### Prompt
Please create a solution in cpp to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int s[1000100];
int e[1000100];
int a[1000100];
int cnt = 0, n;
vector<int> pre[1000100];
struct IT {
int d[4000100];
int lz[4000100];
void upd(int num, int l, int r, int x, int y, int val) {
if (l > r || l > y || r < x) return;
if (l >= x && r <= y) {
lz[num] += val;
d[num] += val;
return;
}
upd(num * 2, l, (l + r) / 2, x, y, val);
upd(num * 2 + 1, (l + r) / 2 + 1, r, x, y, val);
d[num] = max(d[num * 2], d[num * 2 + 1]) + lz[num];
}
int get(int num, int l, int r, int x, int y) {
if (l > r || l > y || r < x) return 0;
if (l >= x && r <= y) {
return d[num];
}
return max(get(num * 2, l, (l + r) / 2, x, y),
get(num * 2 + 1, (l + r) / 2 + 1, r, x, y)) +
lz[num];
}
} it;
void init_dfs(int x) {
s[x] = ++cnt;
for (int i = 0; i < pre[x].size(); i++) {
init_dfs(pre[x][i]);
}
e[x] = cnt;
}
void add(int x) { it.upd(1, 1, n, s[x], e[x], 1); }
void rmv(int x) { it.upd(1, 1, n, s[x], e[x], -1); }
stack<int> st;
int main() {
int k;
ios_base::sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = n; i >= 1; i--) {
while (st.size() && a[i] >= a[st.top()]) st.pop();
if (st.size()) {
pre[st.top()].push_back(i);
} else {
pre[n + 1].push_back(i);
}
st.push(i);
}
n++;
init_dfs(n);
for (int i = 1; i <= k; i++) {
add(i);
}
cout << it.get(1, 1, n, 1, n) << " ";
for (int i = k + 1; i < n; i++) {
add(i);
rmv(i - k);
cout << it.get(1, 1, n, 1, n) << " ";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, INF = 2147483647;
struct Edge {
int to, nex;
} edge[N];
int idx;
int h[N];
void add_edge(int u, int v) {
edge[++idx] = {v, h[u]};
h[u] = idx;
}
struct Segment_Tree {
int l, r;
int sum;
int add;
} tree[N << 2];
int dep[N], siz[N];
int id[N], timestamp;
int big[N], stk[N], top;
int a[N], vis[N];
int n, k;
void dfs(int p, int fa) {
id[p] = ++timestamp;
siz[p] = 1;
if (~fa) dep[p] = dep[fa] + 1;
for (int i = h[p]; ~i; i = edge[i].nex) {
int to = edge[i].to;
dfs(to, p);
siz[p] += siz[to];
}
}
void push_up(int p) {
tree[p].sum = max(tree[p << 1].sum, tree[p << 1 | 1].sum);
}
void spread(int p) {
if (tree[p].add) {
tree[p << 1].sum += tree[p].add;
tree[p << 1 | 1].sum += tree[p].add;
tree[p << 1].add += tree[p].add;
tree[p << 1 | 1].add += tree[p].add;
tree[p].add = 0;
}
}
void build(int p, int l, int r) {
tree[p].l = l, tree[p].r = r;
if (l == r) return;
int mid = l + r >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
push_up(p);
}
void update(int p, int l, int r, int v) {
if (l <= tree[p].l && r >= tree[p].r) {
tree[p].sum += v;
tree[p].add += v;
return;
}
spread(p);
int mid = tree[p].l + tree[p].r >> 1;
if (l <= mid) update(p << 1, l, r, v);
if (r > mid) update(p << 1 | 1, l, r, v);
push_up(p);
}
int main() {
scanf("%d%d", &n, &k);
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i; i--) {
while (top && a[i] >= a[stk[top]]) top--;
big[i] = stk[top];
add_edge(stk[top], i);
stk[++top] = i;
}
dfs(0, -1);
build(1, 1, n + 1);
for (int i = 1; i <= k - 1; i++) update(1, id[i], id[i] + siz[i] - 1, 1);
for (int l = 1, r = k; r <= n; l++, r++) {
update(1, id[r], id[r] + siz[r] - 1, 1);
printf("%d ", tree[1].sum);
update(1, id[l], id[l] + siz[l] - 1, -1);
}
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, INF = 2147483647;
struct Edge {
int to, nex;
} edge[N];
int idx;
int h[N];
void add_edge(int u, int v) {
edge[++idx] = {v, h[u]};
h[u] = idx;
}
struct Segment_Tree {
int l, r;
int sum;
int add;
} tree[N << 2];
int dep[N], siz[N];
int id[N], timestamp;
int big[N], stk[N], top;
int a[N], vis[N];
int n, k;
void dfs(int p, int fa) {
id[p] = ++timestamp;
siz[p] = 1;
if (~fa) dep[p] = dep[fa] + 1;
for (int i = h[p]; ~i; i = edge[i].nex) {
int to = edge[i].to;
dfs(to, p);
siz[p] += siz[to];
}
}
void push_up(int p) {
tree[p].sum = max(tree[p << 1].sum, tree[p << 1 | 1].sum);
}
void spread(int p) {
if (tree[p].add) {
tree[p << 1].sum += tree[p].add;
tree[p << 1 | 1].sum += tree[p].add;
tree[p << 1].add += tree[p].add;
tree[p << 1 | 1].add += tree[p].add;
tree[p].add = 0;
}
}
void build(int p, int l, int r) {
tree[p].l = l, tree[p].r = r;
if (l == r) return;
int mid = l + r >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
push_up(p);
}
void update(int p, int l, int r, int v) {
if (l <= tree[p].l && r >= tree[p].r) {
tree[p].sum += v;
tree[p].add += v;
return;
}
spread(p);
int mid = tree[p].l + tree[p].r >> 1;
if (l <= mid) update(p << 1, l, r, v);
if (r > mid) update(p << 1 | 1, l, r, v);
push_up(p);
}
int main() {
scanf("%d%d", &n, &k);
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i; i--) {
while (top && a[i] >= a[stk[top]]) top--;
big[i] = stk[top];
add_edge(stk[top], i);
stk[++top] = i;
}
dfs(0, -1);
build(1, 1, n + 1);
for (int i = 1; i <= k - 1; i++) update(1, id[i], id[i] + siz[i] - 1, 1);
for (int l = 1, r = k; r <= n; l++, r++) {
update(1, id[r], id[r] + siz[r] - 1, 1);
printf("%d ", tree[1].sum);
update(1, id[l], id[l] + siz[l] - 1, -1);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[1200000], stk[1200000], dfn[1200000], head[1200000], tot, clk,
fst[1200000], lst[1200000], seg[1200000 << 2], fa[1200000], mx[1200000],
dep[1200000];
struct edge {
int v, nxt;
} e[1200000];
multiset<int> se;
int gfa(int x) { return x == fa[x] ? x : fa[x] = gfa(fa[x]); }
void add(int x, int y) {
e[++tot].v = y;
e[tot].nxt = head[x];
head[x] = tot;
}
void dfs(int u) {
dfn[u] = ++clk;
fst[u] = lst[u] = clk;
for (int i = head[u], v; i; i = e[i].nxt) {
dep[v = e[i].v] = dep[u] + 1;
dfs(v);
lst[u] = clk;
}
}
void mdf(int x, int l, int r, int t, int k) {
if (l == r) {
seg[x] = k;
return;
}
int mid = (l + r) >> 1;
if (t <= mid)
mdf((x << 1), l, mid, t, k);
else
mdf((x << 1 | 1), mid + 1, r, t, k);
seg[x] = max(seg[(x << 1)], seg[(x << 1 | 1)]);
}
int qry(int x, int l, int r, int L, int R) {
if (l >= L && r <= R) return seg[x];
int mid = (l + r) >> 1, tmp = 0;
if (L <= mid) tmp = max(tmp, qry((x << 1), l, mid, L, R));
if (R > mid) tmp = max(tmp, qry((x << 1 | 1), mid + 1, r, L, R));
return tmp;
}
void del(int x) { se.erase(se.find(mx[x])); }
void renew(int x) {
se.insert(mx[x] = qry(1, 1, n + 1, fst[x], lst[x]) - dep[x] + 1);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
int tp = 0;
for (int i = 1; i <= n; ++i) {
for (; tp && a[i] > a[stk[tp]]; --tp) add(i, stk[tp]);
stk[++tp] = i;
}
for (; tp; --tp) add(0, stk[tp]);
dfs(0);
for (int i = 1; i <= n; ++i) fa[i] = i;
for (int i = 1, j = 1; i <= n - m + 1; ++i) {
if (i - 1) {
mdf(1, 1, n + 1, dfn[i - 1], 0);
del(gfa(i - 1));
renew(gfa(i - 1));
}
for (; j < i + m; ++j) {
mdf(1, 1, n + 1, dfn[j], dep[j]);
for (int k = head[j]; k; k = e[k].nxt) {
del(e[k].v);
fa[e[k].v] = j;
}
renew(j);
}
printf("%d ", *(--se.end()));
}
puts("");
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, a[1200000], stk[1200000], dfn[1200000], head[1200000], tot, clk,
fst[1200000], lst[1200000], seg[1200000 << 2], fa[1200000], mx[1200000],
dep[1200000];
struct edge {
int v, nxt;
} e[1200000];
multiset<int> se;
int gfa(int x) { return x == fa[x] ? x : fa[x] = gfa(fa[x]); }
void add(int x, int y) {
e[++tot].v = y;
e[tot].nxt = head[x];
head[x] = tot;
}
void dfs(int u) {
dfn[u] = ++clk;
fst[u] = lst[u] = clk;
for (int i = head[u], v; i; i = e[i].nxt) {
dep[v = e[i].v] = dep[u] + 1;
dfs(v);
lst[u] = clk;
}
}
void mdf(int x, int l, int r, int t, int k) {
if (l == r) {
seg[x] = k;
return;
}
int mid = (l + r) >> 1;
if (t <= mid)
mdf((x << 1), l, mid, t, k);
else
mdf((x << 1 | 1), mid + 1, r, t, k);
seg[x] = max(seg[(x << 1)], seg[(x << 1 | 1)]);
}
int qry(int x, int l, int r, int L, int R) {
if (l >= L && r <= R) return seg[x];
int mid = (l + r) >> 1, tmp = 0;
if (L <= mid) tmp = max(tmp, qry((x << 1), l, mid, L, R));
if (R > mid) tmp = max(tmp, qry((x << 1 | 1), mid + 1, r, L, R));
return tmp;
}
void del(int x) { se.erase(se.find(mx[x])); }
void renew(int x) {
se.insert(mx[x] = qry(1, 1, n + 1, fst[x], lst[x]) - dep[x] + 1);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
int tp = 0;
for (int i = 1; i <= n; ++i) {
for (; tp && a[i] > a[stk[tp]]; --tp) add(i, stk[tp]);
stk[++tp] = i;
}
for (; tp; --tp) add(0, stk[tp]);
dfs(0);
for (int i = 1; i <= n; ++i) fa[i] = i;
for (int i = 1, j = 1; i <= n - m + 1; ++i) {
if (i - 1) {
mdf(1, 1, n + 1, dfn[i - 1], 0);
del(gfa(i - 1));
renew(gfa(i - 1));
}
for (; j < i + m; ++j) {
mdf(1, 1, n + 1, dfn[j], dep[j]);
for (int k = head[j]; k; k = e[k].nxt) {
del(e[k].v);
fa[e[k].v] = j;
}
renew(j);
}
printf("%d ", *(--se.end()));
}
puts("");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
const int MAXLog = 20;
int n, k;
int a[MAXN];
int nxt[MAXN];
int sparse[MAXLog + 5][MAXN];
void init() {
stack<int> st;
for (int i = n - 1; i >= 0; i--) {
while (st.empty() == false && a[st.top()] <= a[i]) st.pop();
nxt[i] = ((st.empty() == true) ? n : st.top());
st.push(i);
}
for (int i = 0; i < n; i++) sparse[0][i] = nxt[i];
for (int i = 1; i <= MAXLog; i++) {
for (int j = 0; j < n; j++) {
if (sparse[i - 1][j] == n)
sparse[i][j] = n;
else
sparse[i][j] = sparse[i - 1][sparse[i - 1][j]];
}
}
}
int getNext(int ind, int steps) {
int ans = ind;
for (int i = MAXLog; i >= 0; i--) {
if (((steps >> i) & 1) == 1) {
ans = sparse[i][ans];
if (ans == n) return ans;
}
}
return ans;
}
int getFitting(int ind, int lim) {
int ans = 0;
for (int i = MAXLog; i >= 0; i--) {
if (sparse[i][ind] <= lim) {
ans += (1 << i);
ind = sparse[i][ind];
}
}
return ans;
}
int getAns(deque<int> &opt, int lim) {
int answer = 0, iter = 0;
;
auto it = opt.end();
it--;
while (true) {
answer = max(answer, getFitting(*it, lim));
iter++;
if (iter == 15) break;
if (it == opt.begin())
break;
else
it--;
}
return answer + 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
vector<int> output;
double average = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
init();
int answer = 0;
deque<int> opt;
for (int i = n - 1; i >= n - k; i--) {
while (opt.empty() == false &&
getFitting(i, n - 1) >= getFitting(opt.front(), n - 1))
opt.pop_front();
opt.push_front(i);
}
output.push_back(getAns(opt, n - 1));
for (int i = n - 2; i >= k - 1; i--) {
while (opt.empty() == false &&
getFitting(i - k + 1, i) >= getFitting(opt.front(), i))
opt.pop_front();
opt.push_front(i - k + 1);
while (opt.size() >= 2) {
auto it1 = opt.end();
it1--;
auto it2 = it1;
it2--;
if (getFitting(*it2, i) >= getFitting(*it1, i))
opt.pop_back();
else
break;
}
average += opt.size();
output.push_back(getAns(opt, i));
}
reverse(output.begin(), output.end());
for (int x : output) cout << x << " ";
cout << '\n';
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
const int MAXLog = 20;
int n, k;
int a[MAXN];
int nxt[MAXN];
int sparse[MAXLog + 5][MAXN];
void init() {
stack<int> st;
for (int i = n - 1; i >= 0; i--) {
while (st.empty() == false && a[st.top()] <= a[i]) st.pop();
nxt[i] = ((st.empty() == true) ? n : st.top());
st.push(i);
}
for (int i = 0; i < n; i++) sparse[0][i] = nxt[i];
for (int i = 1; i <= MAXLog; i++) {
for (int j = 0; j < n; j++) {
if (sparse[i - 1][j] == n)
sparse[i][j] = n;
else
sparse[i][j] = sparse[i - 1][sparse[i - 1][j]];
}
}
}
int getNext(int ind, int steps) {
int ans = ind;
for (int i = MAXLog; i >= 0; i--) {
if (((steps >> i) & 1) == 1) {
ans = sparse[i][ans];
if (ans == n) return ans;
}
}
return ans;
}
int getFitting(int ind, int lim) {
int ans = 0;
for (int i = MAXLog; i >= 0; i--) {
if (sparse[i][ind] <= lim) {
ans += (1 << i);
ind = sparse[i][ind];
}
}
return ans;
}
int getAns(deque<int> &opt, int lim) {
int answer = 0, iter = 0;
;
auto it = opt.end();
it--;
while (true) {
answer = max(answer, getFitting(*it, lim));
iter++;
if (iter == 15) break;
if (it == opt.begin())
break;
else
it--;
}
return answer + 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
vector<int> output;
double average = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
init();
int answer = 0;
deque<int> opt;
for (int i = n - 1; i >= n - k; i--) {
while (opt.empty() == false &&
getFitting(i, n - 1) >= getFitting(opt.front(), n - 1))
opt.pop_front();
opt.push_front(i);
}
output.push_back(getAns(opt, n - 1));
for (int i = n - 2; i >= k - 1; i--) {
while (opt.empty() == false &&
getFitting(i - k + 1, i) >= getFitting(opt.front(), i))
opt.pop_front();
opt.push_front(i - k + 1);
while (opt.size() >= 2) {
auto it1 = opt.end();
it1--;
auto it2 = it1;
it2--;
if (getFitting(*it2, i) >= getFitting(*it1, i))
opt.pop_back();
else
break;
}
average += opt.size();
output.push_back(getAns(opt, i));
}
reverse(output.begin(), output.end());
for (int x : output) cout << x << " ";
cout << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = (1e9 + 7);
const long long INF = (1e9 + 123456789);
const long long INFL = (INF * INF);
inline long long addm(long long a, long long b, long long m = MOD) {
return ((a + b) % m);
}
inline long long subm(long long a, long long b, long long m = MOD) {
return (((a - b) % m + m) % m);
}
inline long long mulm(long long a, long long b, long long m = MOD) {
return ((a * b) % m);
}
long long powm(long long a, long long b, long long m = MOD) {
long long ret = (!b) ? 1 : powm(a, b / 2, m);
return (!b) ? 1 : mulm(mulm(ret, ret, m), b % 2 ? a : 1, m);
}
long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long inv(long long x, long long m = MOD) { return powm(x, m - 2, m); }
const int N = 1500500;
int a[N], lft[N], nxt[N];
vector<int> nodes, g[N];
struct SegmentTree {
int f, t;
SegmentTree *lc, *rc;
bool has_child, has_lazy = false;
int upd = 0;
int vmax = 0;
SegmentTree(int l, int r) {
if (l == r) {
f = t = l;
lc = rc = NULL;
has_child = false;
} else {
f = l;
t = r;
int mid = (l + r) / 2;
lc = new SegmentTree(l, mid);
rc = new SegmentTree(mid + 1, r);
has_child = true;
}
}
void change(int v) {
vmax += v;
upd += v;
has_lazy = true;
}
void push() {
if (!has_lazy) return;
lc->change(upd);
rc->change(upd);
upd = 0;
has_lazy = false;
}
void update(int l, int r, int x) {
if (r < f || l > t) return;
if (l <= f && t <= r) {
change(x);
return;
}
push();
lc->update(l, r, x);
rc->update(l, r, x);
vmax = max(lc->vmax, rc->vmax);
}
int query(int l, int r) {
if (r < f || l > t) return 0;
if (l <= f && t <= r) return vmax;
return vmax = max(lc->query(l, r), rc->query(l, r));
}
};
void solve() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
stack<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (!st.empty())
nxt[i] = st.top();
else
nxt[i] = n;
st.push(i);
}
for (int i = 0; i < n; i++) g[nxt[i]].push_back(i);
function<void(int)> post = [&](int v) {
int l = (int)(nodes).size();
for (int u : g[v]) post(u);
lft[(int)(nodes).size()] = l;
nodes.push_back(v);
};
post(n);
SegmentTree root(0, n);
for (int i = 0; i < k; i++) root.update(lft[i], i, 1);
vector<int> ans;
ans.push_back(root.query(0, n));
for (int i = k; i < n; i++) {
root.update(lft[i - k], i - k, -1);
root.update(lft[i], i, 1);
ans.push_back(root.query(0, n));
}
for (int i = 0; i < (int)(ans).size(); i++) {
if (i) cout << " ";
cout << ans[i];
}
cout << "\n";
}
int main(int argc, char *argv[]) {
cin.sync_with_stdio(0);
cin.tie(NULL);
solve();
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = (1e9 + 7);
const long long INF = (1e9 + 123456789);
const long long INFL = (INF * INF);
inline long long addm(long long a, long long b, long long m = MOD) {
return ((a + b) % m);
}
inline long long subm(long long a, long long b, long long m = MOD) {
return (((a - b) % m + m) % m);
}
inline long long mulm(long long a, long long b, long long m = MOD) {
return ((a * b) % m);
}
long long powm(long long a, long long b, long long m = MOD) {
long long ret = (!b) ? 1 : powm(a, b / 2, m);
return (!b) ? 1 : mulm(mulm(ret, ret, m), b % 2 ? a : 1, m);
}
long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long inv(long long x, long long m = MOD) { return powm(x, m - 2, m); }
const int N = 1500500;
int a[N], lft[N], nxt[N];
vector<int> nodes, g[N];
struct SegmentTree {
int f, t;
SegmentTree *lc, *rc;
bool has_child, has_lazy = false;
int upd = 0;
int vmax = 0;
SegmentTree(int l, int r) {
if (l == r) {
f = t = l;
lc = rc = NULL;
has_child = false;
} else {
f = l;
t = r;
int mid = (l + r) / 2;
lc = new SegmentTree(l, mid);
rc = new SegmentTree(mid + 1, r);
has_child = true;
}
}
void change(int v) {
vmax += v;
upd += v;
has_lazy = true;
}
void push() {
if (!has_lazy) return;
lc->change(upd);
rc->change(upd);
upd = 0;
has_lazy = false;
}
void update(int l, int r, int x) {
if (r < f || l > t) return;
if (l <= f && t <= r) {
change(x);
return;
}
push();
lc->update(l, r, x);
rc->update(l, r, x);
vmax = max(lc->vmax, rc->vmax);
}
int query(int l, int r) {
if (r < f || l > t) return 0;
if (l <= f && t <= r) return vmax;
return vmax = max(lc->query(l, r), rc->query(l, r));
}
};
void solve() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
stack<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (!st.empty())
nxt[i] = st.top();
else
nxt[i] = n;
st.push(i);
}
for (int i = 0; i < n; i++) g[nxt[i]].push_back(i);
function<void(int)> post = [&](int v) {
int l = (int)(nodes).size();
for (int u : g[v]) post(u);
lft[(int)(nodes).size()] = l;
nodes.push_back(v);
};
post(n);
SegmentTree root(0, n);
for (int i = 0; i < k; i++) root.update(lft[i], i, 1);
vector<int> ans;
ans.push_back(root.query(0, n));
for (int i = k; i < n; i++) {
root.update(lft[i - k], i - k, -1);
root.update(lft[i], i, 1);
ans.push_back(root.query(0, n));
}
for (int i = 0; i < (int)(ans).size(); i++) {
if (i) cout << " ";
cout << ans[i];
}
cout << "\n";
}
int main(int argc, char *argv[]) {
cin.sync_with_stdio(0);
cin.tie(NULL);
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = (1e9 + 7);
const long long INF = (1e9 + 123456789);
const long long INFL = (INF * INF);
inline long long addm(long long a, long long b, long long m = MOD) {
return ((a + b) % m);
}
inline long long subm(long long a, long long b, long long m = MOD) {
return (((a - b) % m + m) % m);
}
inline long long mulm(long long a, long long b, long long m = MOD) {
return ((a * b) % m);
}
long long powm(long long a, long long b, long long m = MOD) {
long long ret = (!b) ? 1 : powm(a, b / 2, m);
return (!b) ? 1 : mulm(mulm(ret, ret, m), b % 2 ? a : 1, m);
}
long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long inv(long long x, long long m = MOD) { return powm(x, m - 2, m); }
const int N = 1000500;
int a[N], lft[N], nxt[N];
vector<int> nodes, g[N];
struct SegmentTree {
int f, t;
SegmentTree *lc, *rc;
bool has_child, has_lazy = false;
int upd = 0;
int vmax = 0;
SegmentTree(int l, int r) {
if (l == r) {
f = t = l;
lc = rc = NULL;
has_child = false;
} else {
f = l;
t = r;
int mid = (l + r) / 2;
lc = new SegmentTree(l, mid);
rc = new SegmentTree(mid + 1, r);
has_child = true;
}
}
void change(int v) {
vmax += v;
upd += v;
has_lazy = true;
}
void push() {
if (!has_lazy) return;
lc->change(upd);
rc->change(upd);
upd = 0;
has_lazy = false;
}
void update(int l, int r, int x) {
if (r < f || l > t) return;
if (l <= f && t <= r) {
change(x);
return;
}
push();
lc->update(l, r, x);
rc->update(l, r, x);
vmax = max(lc->vmax, rc->vmax);
}
int query(int l, int r) {
if (r < f || l > t) return 0;
if (l <= f && t <= r) return vmax;
push();
return vmax = max(lc->query(l, r), rc->query(l, r));
}
};
void solve() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
stack<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (!st.empty())
nxt[i] = st.top();
else
nxt[i] = n;
st.push(i);
}
for (int i = 0; i < n; i++) g[nxt[i]].push_back(i);
function<void(int)> post = [&](int v) {
int l = (int)(nodes).size();
for (int u : g[v]) post(u);
lft[(int)(nodes).size()] = l;
nodes.push_back(v);
};
post(n);
SegmentTree root(0, n);
for (int i = 0; i < k; i++) root.update(lft[i], i, 1);
vector<int> ans;
ans.push_back(root.query(0, n));
for (int i = k; i < n; i++) {
root.update(lft[i - k], i - k, -1);
root.update(lft[i], i, 1);
ans.push_back(root.query(0, n));
}
for (int i = 0; i < (int)(ans).size(); i++) {
if (i) cout << " ";
cout << ans[i];
}
cout << "\n";
}
int main(int argc, char *argv[]) {
cin.sync_with_stdio(0);
cin.tie(NULL);
solve();
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = (1e9 + 7);
const long long INF = (1e9 + 123456789);
const long long INFL = (INF * INF);
inline long long addm(long long a, long long b, long long m = MOD) {
return ((a + b) % m);
}
inline long long subm(long long a, long long b, long long m = MOD) {
return (((a - b) % m + m) % m);
}
inline long long mulm(long long a, long long b, long long m = MOD) {
return ((a * b) % m);
}
long long powm(long long a, long long b, long long m = MOD) {
long long ret = (!b) ? 1 : powm(a, b / 2, m);
return (!b) ? 1 : mulm(mulm(ret, ret, m), b % 2 ? a : 1, m);
}
long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long inv(long long x, long long m = MOD) { return powm(x, m - 2, m); }
const int N = 1000500;
int a[N], lft[N], nxt[N];
vector<int> nodes, g[N];
struct SegmentTree {
int f, t;
SegmentTree *lc, *rc;
bool has_child, has_lazy = false;
int upd = 0;
int vmax = 0;
SegmentTree(int l, int r) {
if (l == r) {
f = t = l;
lc = rc = NULL;
has_child = false;
} else {
f = l;
t = r;
int mid = (l + r) / 2;
lc = new SegmentTree(l, mid);
rc = new SegmentTree(mid + 1, r);
has_child = true;
}
}
void change(int v) {
vmax += v;
upd += v;
has_lazy = true;
}
void push() {
if (!has_lazy) return;
lc->change(upd);
rc->change(upd);
upd = 0;
has_lazy = false;
}
void update(int l, int r, int x) {
if (r < f || l > t) return;
if (l <= f && t <= r) {
change(x);
return;
}
push();
lc->update(l, r, x);
rc->update(l, r, x);
vmax = max(lc->vmax, rc->vmax);
}
int query(int l, int r) {
if (r < f || l > t) return 0;
if (l <= f && t <= r) return vmax;
push();
return vmax = max(lc->query(l, r), rc->query(l, r));
}
};
void solve() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
stack<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (!st.empty())
nxt[i] = st.top();
else
nxt[i] = n;
st.push(i);
}
for (int i = 0; i < n; i++) g[nxt[i]].push_back(i);
function<void(int)> post = [&](int v) {
int l = (int)(nodes).size();
for (int u : g[v]) post(u);
lft[(int)(nodes).size()] = l;
nodes.push_back(v);
};
post(n);
SegmentTree root(0, n);
for (int i = 0; i < k; i++) root.update(lft[i], i, 1);
vector<int> ans;
ans.push_back(root.query(0, n));
for (int i = k; i < n; i++) {
root.update(lft[i - k], i - k, -1);
root.update(lft[i], i, 1);
ans.push_back(root.query(0, n));
}
for (int i = 0; i < (int)(ans).size(); i++) {
if (i) cout << " ";
cout << ans[i];
}
cout << "\n";
}
int main(int argc, char *argv[]) {
cin.sync_with_stdio(0);
cin.tie(NULL);
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
const int bits = 20;
const long double pi = 3.14159265358979323846;
vector<int> adj[N];
int st[N], en[N], ptr;
void dfs(int node) {
st[node] = ++ptr;
for (int nxt : adj[node]) {
dfs(nxt);
}
en[node] = ptr;
}
int tree[4 * N], lazy[4 * N];
void pushdown(int si, int ss, int se) {
tree[si] += lazy[si];
if (ss != se) {
lazy[2 * si + 1] += lazy[si];
lazy[2 * si + 2] += lazy[si];
}
lazy[si] = 0;
}
int query(int qs, int qe, int ss, int se, int si) {
pushdown(si, ss, se);
if (qs > se || qe < ss) {
return -inf;
}
if (ss >= qs && se <= qe) {
return tree[si];
}
int mid = (se + ss) / 2;
return min(query(qs, qe, ss, mid, 2 * si + 1),
query(qs, qe, mid + 1, se, 2 * si + 2));
}
void upd(int qs, int qe, int ss, int se, int si, int v) {
pushdown(si, ss, se);
if (qs > se || qe < ss || qs > qe) {
return;
}
if (ss >= qs && se <= qe) {
lazy[si] = v;
pushdown(si, ss, se);
return;
}
int mid = (se + ss) / 2;
upd(qs, qe, ss, mid, 2 * si + 1, v);
upd(qs, qe, mid + 1, se, 2 * si + 2, v);
tree[si] = max(tree[2 * si + 1], tree[2 * si + 2]);
}
void solve() {
int n, k;
cin >> n >> k;
vector<int> arr(n + 1);
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
stack<int> stk;
for (int i = n; i >= 1; i--) {
while (stk.size() && arr[stk.top()] <= arr[i]) stk.pop();
if (stk.size()) {
adj[stk.top()].push_back(i);
} else {
adj[n + 1].push_back(i);
}
stk.push(i);
}
dfs(n + 1);
for (int i = 1; i <= k; i++) {
upd(st[i], en[i], 0, ptr, 0, 1);
}
cout << tree[0] << " ";
for (int i = k + 1; i <= n; i++) {
upd(st[i], en[i], 0, ptr, 0, 1);
upd(st[i - k], en[i - k], 0, ptr, 0, -1);
cout << tree[0] << " ";
}
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
std::cout << std::fixed << std::setprecision(20);
;
int t = 1;
while (t--) {
solve();
}
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
const int bits = 20;
const long double pi = 3.14159265358979323846;
vector<int> adj[N];
int st[N], en[N], ptr;
void dfs(int node) {
st[node] = ++ptr;
for (int nxt : adj[node]) {
dfs(nxt);
}
en[node] = ptr;
}
int tree[4 * N], lazy[4 * N];
void pushdown(int si, int ss, int se) {
tree[si] += lazy[si];
if (ss != se) {
lazy[2 * si + 1] += lazy[si];
lazy[2 * si + 2] += lazy[si];
}
lazy[si] = 0;
}
int query(int qs, int qe, int ss, int se, int si) {
pushdown(si, ss, se);
if (qs > se || qe < ss) {
return -inf;
}
if (ss >= qs && se <= qe) {
return tree[si];
}
int mid = (se + ss) / 2;
return min(query(qs, qe, ss, mid, 2 * si + 1),
query(qs, qe, mid + 1, se, 2 * si + 2));
}
void upd(int qs, int qe, int ss, int se, int si, int v) {
pushdown(si, ss, se);
if (qs > se || qe < ss || qs > qe) {
return;
}
if (ss >= qs && se <= qe) {
lazy[si] = v;
pushdown(si, ss, se);
return;
}
int mid = (se + ss) / 2;
upd(qs, qe, ss, mid, 2 * si + 1, v);
upd(qs, qe, mid + 1, se, 2 * si + 2, v);
tree[si] = max(tree[2 * si + 1], tree[2 * si + 2]);
}
void solve() {
int n, k;
cin >> n >> k;
vector<int> arr(n + 1);
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
stack<int> stk;
for (int i = n; i >= 1; i--) {
while (stk.size() && arr[stk.top()] <= arr[i]) stk.pop();
if (stk.size()) {
adj[stk.top()].push_back(i);
} else {
adj[n + 1].push_back(i);
}
stk.push(i);
}
dfs(n + 1);
for (int i = 1; i <= k; i++) {
upd(st[i], en[i], 0, ptr, 0, 1);
}
cout << tree[0] << " ";
for (int i = k + 1; i <= n; i++) {
upd(st[i], en[i], 0, ptr, 0, 1);
upd(st[i - k], en[i - k], 0, ptr, 0, -1);
cout << tree[0] << " ";
}
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
std::cout << std::fixed << std::setprecision(20);
;
int t = 1;
while (t--) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k, a[1000005];
stack<int> s;
int par[1000005];
vector<int> adj[1000006];
int st[1000005], en[1000005];
int in = 1;
int tree[4 * 1000005];
int lazy[4 * 1000005];
void dfs(int i) {
st[i] = in++;
for (auto j : adj[i]) dfs(j);
en[i] = in;
}
void lazify(int s, int e, int i) {
if (lazy[i]) {
tree[i] += lazy[i];
if (s != e) {
lazy[2 * i] += lazy[i];
lazy[2 * i + 1] += lazy[i];
}
lazy[i] = 0;
}
}
void upd(int l, int r, int s, int e, int i, int x) {
lazify(s, e, i);
if (s > r || e < l || l > r) return;
if (s >= l && e <= r) {
lazy[i] += x;
lazify(s, e, i);
return;
}
int mid = (s + e) >> 1;
upd(l, r, s, mid, 2 * i, x);
upd(l, r, mid + 1, e, 2 * i + 1, x);
tree[i] = max(tree[2 * i], tree[2 * i + 1]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
s.push(n);
par[n] = n + 1;
adj[n + 1].push_back(n);
for (int i = n - 1; i >= 1; i--) {
while (s.size() && a[s.top()] <= a[i]) s.pop();
if (s.size())
par[i] = s.top(), adj[s.top()].push_back(i);
else
par[i] = n + 1, adj[n + 1].push_back(i);
s.push(i);
}
dfs(n + 1);
for (int i = 1; i <= k; i++) upd(st[i], en[i] - 1, 1, in - 1, 1, 1);
lazify(1, in - 1, 1);
cout << tree[1] << " ";
for (int i = k + 1; i <= n; i++) {
upd(st[i], en[i] - 1, 1, in - 1, 1, 1);
upd(st[i - k], st[i - k], 1, in - 1, 1, -1000000000);
lazify(1, in - 1, 1);
cout << tree[1] << " ";
}
cout << '\n';
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k, a[1000005];
stack<int> s;
int par[1000005];
vector<int> adj[1000006];
int st[1000005], en[1000005];
int in = 1;
int tree[4 * 1000005];
int lazy[4 * 1000005];
void dfs(int i) {
st[i] = in++;
for (auto j : adj[i]) dfs(j);
en[i] = in;
}
void lazify(int s, int e, int i) {
if (lazy[i]) {
tree[i] += lazy[i];
if (s != e) {
lazy[2 * i] += lazy[i];
lazy[2 * i + 1] += lazy[i];
}
lazy[i] = 0;
}
}
void upd(int l, int r, int s, int e, int i, int x) {
lazify(s, e, i);
if (s > r || e < l || l > r) return;
if (s >= l && e <= r) {
lazy[i] += x;
lazify(s, e, i);
return;
}
int mid = (s + e) >> 1;
upd(l, r, s, mid, 2 * i, x);
upd(l, r, mid + 1, e, 2 * i + 1, x);
tree[i] = max(tree[2 * i], tree[2 * i + 1]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
s.push(n);
par[n] = n + 1;
adj[n + 1].push_back(n);
for (int i = n - 1; i >= 1; i--) {
while (s.size() && a[s.top()] <= a[i]) s.pop();
if (s.size())
par[i] = s.top(), adj[s.top()].push_back(i);
else
par[i] = n + 1, adj[n + 1].push_back(i);
s.push(i);
}
dfs(n + 1);
for (int i = 1; i <= k; i++) upd(st[i], en[i] - 1, 1, in - 1, 1, 1);
lazify(1, in - 1, 1);
cout << tree[1] << " ";
for (int i = k + 1; i <= n; i++) {
upd(st[i], en[i] - 1, 1, in - 1, 1, 1);
upd(st[i - k], st[i - k], 1, in - 1, 1, -1000000000);
lazify(1, in - 1, 1);
cout << tree[1] << " ";
}
cout << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int dxx[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
dyy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long mod = 1000000007;
const int base = 311;
const int N = 1e6 + 5;
struct segtree {
vector<int> st, lz;
void kc(int n) {
st.assign(4 * n + 5, 0);
lz.assign(4 * n + 5, 0);
}
void down(int i) {
int &t = lz[i];
lz[i << 1] += t;
lz[i << 1 | 1] += t;
st[i << 1] += t;
st[i << 1 | 1] += t;
t = 0;
}
void upd(int i, int l, int r, int u, int v, int val) {
if (u > r || v < l) return;
if (u <= l && r <= v) {
st[i] += val;
lz[i] += val;
return;
}
int mid = (l + r) >> 1;
down(i);
upd(i << 1, l, mid, u, v, val);
upd(i << 1 | 1, mid + 1, r, u, v, val);
st[i] = max(st[i << 1], st[i << 1 | 1]);
}
int get(int i, int l, int r, int u, int v, int val) {
if (u > r || v < l) return 0;
if (u <= l && r <= v) return st[i];
down(i);
int mid = (l + r) >> 1;
return max(get(i << 1, l, mid, u, v, val),
get(i << 1 | 1, mid + 1, r, u, v, val));
}
};
int n, k, a[N];
vector<int> dsk[N];
int tin[N], tout[N], cnt = 0;
void dfs(int u) {
tin[u] = ++cnt;
for (int v : dsk[u]) dfs(v);
tout[u] = cnt;
}
void gogo() {
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
stack<int> stk;
stk.push(n + 1);
a[n + 1] = 1e9;
for (int i = n; i >= 1; --i) {
while (((int)(stk).size()) && a[stk.top()] <= a[i]) stk.pop();
dsk[stk.top()].push_back(i);
stk.push(i);
}
dfs(n + 1);
segtree st;
st.kc(n + 5);
for (int i = 1, j = 0; i + k - 1 <= n; ++i) {
while (j < i + k - 1) {
j++;
st.upd(1, 1, n + 1, tin[j], tout[j], 1);
}
cout << st.st[1] << ' ';
st.upd(1, 1, n + 1, tin[i], tout[i], -1);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("sol"
".inp",
"r")) {
freopen(
"sol"
".inp",
"r", stdin);
freopen(
"sol"
".out",
"w", stdout);
}
gogo();
}
| ### Prompt
Please formulate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int dxx[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
dyy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long mod = 1000000007;
const int base = 311;
const int N = 1e6 + 5;
struct segtree {
vector<int> st, lz;
void kc(int n) {
st.assign(4 * n + 5, 0);
lz.assign(4 * n + 5, 0);
}
void down(int i) {
int &t = lz[i];
lz[i << 1] += t;
lz[i << 1 | 1] += t;
st[i << 1] += t;
st[i << 1 | 1] += t;
t = 0;
}
void upd(int i, int l, int r, int u, int v, int val) {
if (u > r || v < l) return;
if (u <= l && r <= v) {
st[i] += val;
lz[i] += val;
return;
}
int mid = (l + r) >> 1;
down(i);
upd(i << 1, l, mid, u, v, val);
upd(i << 1 | 1, mid + 1, r, u, v, val);
st[i] = max(st[i << 1], st[i << 1 | 1]);
}
int get(int i, int l, int r, int u, int v, int val) {
if (u > r || v < l) return 0;
if (u <= l && r <= v) return st[i];
down(i);
int mid = (l + r) >> 1;
return max(get(i << 1, l, mid, u, v, val),
get(i << 1 | 1, mid + 1, r, u, v, val));
}
};
int n, k, a[N];
vector<int> dsk[N];
int tin[N], tout[N], cnt = 0;
void dfs(int u) {
tin[u] = ++cnt;
for (int v : dsk[u]) dfs(v);
tout[u] = cnt;
}
void gogo() {
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
stack<int> stk;
stk.push(n + 1);
a[n + 1] = 1e9;
for (int i = n; i >= 1; --i) {
while (((int)(stk).size()) && a[stk.top()] <= a[i]) stk.pop();
dsk[stk.top()].push_back(i);
stk.push(i);
}
dfs(n + 1);
segtree st;
st.kc(n + 5);
for (int i = 1, j = 0; i + k - 1 <= n; ++i) {
while (j < i + k - 1) {
j++;
st.upd(1, 1, n + 1, tin[j], tout[j], 1);
}
cout << st.st[1] << ' ';
st.upd(1, 1, n + 1, tin[i], tout[i], -1);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("sol"
".inp",
"r")) {
freopen(
"sol"
".inp",
"r", stdin);
freopen(
"sol"
".out",
"w", stdout);
}
gogo();
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e6 + 527;
int n, k;
int cnt, T;
int x, y, z;
int head[sz];
int a[sz], ans[sz];
int dfn[sz], lev[sz];
stack<int> s;
struct Edge {
int v, nxt;
} edge[sz];
struct node {
int tag, mx;
} tr[sz << 2];
void add(int u, int v) {
edge[++cnt] = (Edge){v, head[u]};
head[u] = cnt;
}
void update(int o) {
tr[o << 1].tag += tr[o].tag;
tr[o << 1 | 1].tag += tr[o].tag;
tr[o << 1].mx += tr[o].tag;
tr[o << 1 | 1].mx += tr[o].tag;
tr[o].tag = 0;
}
void modify(int o, int l, int r) {
if (x <= l && r <= y) return (void)(tr[o].mx += z, tr[o].tag += z);
if (tr[o].tag) update(o);
int mid = (l + r) >> 1;
if (x <= mid) modify(o << 1, l, mid);
if (y > mid) modify(o << 1 | 1, mid + 1, r);
tr[o].mx = max(tr[o << 1].mx, tr[o << 1 | 1].mx);
}
void dfs(int x) {
dfn[x] = ++T;
for (int i = head[x]; i; i = edge[i].nxt) dfs(edge[i].v);
lev[x] = T;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
while (!s.empty() && a[s.top()] < a[i]) {
add(i, s.top());
s.pop();
}
s.push(i);
}
n++;
while (!s.empty()) {
add(n, s.top());
s.pop();
}
dfs(n);
for (int i = 1; i <= k; i++) {
x = dfn[i], y = lev[i], z = 1;
modify(1, 1, n);
}
ans[1] = tr[1].mx;
for (int i = k + 1; i < n; i++) {
x = dfn[i], y = lev[i], z = 1;
modify(1, 1, n);
x = dfn[i - k], y = lev[i - k], z = -1;
modify(1, 1, n);
ans[i - k + 1] = tr[1].mx;
}
for (int i = 1; i <= n - k; i++) printf("%d ", ans[i]);
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e6 + 527;
int n, k;
int cnt, T;
int x, y, z;
int head[sz];
int a[sz], ans[sz];
int dfn[sz], lev[sz];
stack<int> s;
struct Edge {
int v, nxt;
} edge[sz];
struct node {
int tag, mx;
} tr[sz << 2];
void add(int u, int v) {
edge[++cnt] = (Edge){v, head[u]};
head[u] = cnt;
}
void update(int o) {
tr[o << 1].tag += tr[o].tag;
tr[o << 1 | 1].tag += tr[o].tag;
tr[o << 1].mx += tr[o].tag;
tr[o << 1 | 1].mx += tr[o].tag;
tr[o].tag = 0;
}
void modify(int o, int l, int r) {
if (x <= l && r <= y) return (void)(tr[o].mx += z, tr[o].tag += z);
if (tr[o].tag) update(o);
int mid = (l + r) >> 1;
if (x <= mid) modify(o << 1, l, mid);
if (y > mid) modify(o << 1 | 1, mid + 1, r);
tr[o].mx = max(tr[o << 1].mx, tr[o << 1 | 1].mx);
}
void dfs(int x) {
dfn[x] = ++T;
for (int i = head[x]; i; i = edge[i].nxt) dfs(edge[i].v);
lev[x] = T;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
while (!s.empty() && a[s.top()] < a[i]) {
add(i, s.top());
s.pop();
}
s.push(i);
}
n++;
while (!s.empty()) {
add(n, s.top());
s.pop();
}
dfs(n);
for (int i = 1; i <= k; i++) {
x = dfn[i], y = lev[i], z = 1;
modify(1, 1, n);
}
ans[1] = tr[1].mx;
for (int i = k + 1; i < n; i++) {
x = dfn[i], y = lev[i], z = 1;
modify(1, 1, n);
x = dfn[i - k], y = lev[i - k], z = -1;
modify(1, 1, n);
ans[i - k + 1] = tr[1].mx;
}
for (int i = 1; i <= n - k; i++) printf("%d ", ans[i]);
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[1000010], pre[1000010];
int mx[1000010 << 2], flag[1000010 << 2];
void push_up(int u) { mx[u] = max(mx[(u << 1)], mx[((u << 1) | 1)]); }
void push_down(int u) {
if (!flag[u]) return;
mx[(u << 1)] += flag[u];
mx[((u << 1) | 1)] += flag[u];
flag[(u << 1)] += flag[u];
flag[((u << 1) | 1)] += flag[u];
flag[u] = 0;
}
void update(int u, int L, int R, int st, int ed) {
if (st >= R || ed <= L) return;
if (st <= L && ed >= R) {
mx[u]++;
flag[u]++;
return;
}
push_down(u);
update((u << 1), L, (L + R >> 1), st, ed);
update(((u << 1) | 1), (L + R >> 1), R, st, ed);
push_up(u);
}
int calc(int u, int L, int R, int st, int ed) {
if (st >= R || ed <= L) return 0;
if (st <= L && ed >= R) return mx[u];
push_down(u);
return max(calc((u << 1), L, (L + R >> 1), st, ed),
calc(((u << 1) | 1), (L + R >> 1), R, st, ed));
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
int st = i - 1;
while (st && a[st] < a[i]) st = pre[st];
pre[i] = st;
update(1, 1, n + 1, pre[i] + 1, i + 1);
if (i >= k) cout << calc(1, 1, n + 1, i - k + 1, i + 1) << ' ';
}
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[1000010], pre[1000010];
int mx[1000010 << 2], flag[1000010 << 2];
void push_up(int u) { mx[u] = max(mx[(u << 1)], mx[((u << 1) | 1)]); }
void push_down(int u) {
if (!flag[u]) return;
mx[(u << 1)] += flag[u];
mx[((u << 1) | 1)] += flag[u];
flag[(u << 1)] += flag[u];
flag[((u << 1) | 1)] += flag[u];
flag[u] = 0;
}
void update(int u, int L, int R, int st, int ed) {
if (st >= R || ed <= L) return;
if (st <= L && ed >= R) {
mx[u]++;
flag[u]++;
return;
}
push_down(u);
update((u << 1), L, (L + R >> 1), st, ed);
update(((u << 1) | 1), (L + R >> 1), R, st, ed);
push_up(u);
}
int calc(int u, int L, int R, int st, int ed) {
if (st >= R || ed <= L) return 0;
if (st <= L && ed >= R) return mx[u];
push_down(u);
return max(calc((u << 1), L, (L + R >> 1), st, ed),
calc(((u << 1) | 1), (L + R >> 1), R, st, ed));
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
int st = i - 1;
while (st && a[st] < a[i]) st = pre[st];
pre[i] = st;
update(1, 1, n + 1, pre[i] + 1, i + 1);
if (i >= k) cout << calc(1, 1, n + 1, i - k + 1, i + 1) << ' ';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int n, k, sm, a[N], L[N], fa[N], v[N];
int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); }
int main() {
scanf("%d%d", &n, &k);
int r = 1;
for (int i = (1); i <= (n); i++) scanf("%d", &a[i]);
for (int i = (1); i <= (n); i++) {
L[i] = i - 1;
while (L[i] && a[L[i]] < a[i]) L[i] = L[L[i]];
}
for (int i = (1); i <= (n); i++) {
fa[i] = i;
int l = get(L[i] + 1);
if (l <= r) sm++;
v[l]++;
v[i + 1]--;
if (l) fa[l] = l - 1;
if (i >= k) printf("%d ", sm), sm += v[++r];
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int n, k, sm, a[N], L[N], fa[N], v[N];
int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); }
int main() {
scanf("%d%d", &n, &k);
int r = 1;
for (int i = (1); i <= (n); i++) scanf("%d", &a[i]);
for (int i = (1); i <= (n); i++) {
L[i] = i - 1;
while (L[i] && a[L[i]] < a[i]) L[i] = L[L[i]];
}
for (int i = (1); i <= (n); i++) {
fa[i] = i;
int l = get(L[i] + 1);
if (l <= r) sm++;
v[l]++;
v[i + 1]--;
if (l) fa[l] = l - 1;
if (i >= k) printf("%d ", sm), sm += v[++r];
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 10;
const int SZ = 1e6 + 10;
const int mod = 1e9 + 7;
const double PI = acos(-1);
const double eps = 1e-7;
long long read() {
long long n = 0;
char a = getchar();
bool flag = 0;
while (a > '9' || a < '0') {
if (a == '-') flag = 1;
a = getchar();
}
while (a <= '9' && a >= '0') {
n = n * 10 + a - '0', a = getchar();
}
if (flag) n = -n;
return n;
}
int n, k, a[SZ];
vector<int> g[SZ];
void build_graph() {
stack<int> S;
for (int i = n; i >= 1; i--) {
while (S.size() && a[S.top()] <= a[i]) S.pop();
if (S.size()) g[S.top()].push_back(i);
S.push(i);
}
}
struct seg {
int l, r;
int add, maxn;
} tree[SZ * 4];
void update(int p) {
tree[p].maxn = max(tree[p << 1].maxn, tree[p << 1 | 1].maxn);
}
void build(int p, int l, int r) {
tree[p].l = l;
tree[p].r = r;
if (l == r) return;
int mid = l + r >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
}
void pushadd(int p, int d) {
tree[p].add += d;
tree[p].maxn += d;
}
void pushdown(int p) {
if (tree[p].add) {
pushadd(p << 1, tree[p].add);
pushadd(p << 1 | 1, tree[p].add);
tree[p].add = 0;
}
}
void change(int p, int l, int r, int d) {
if (l <= tree[p].l && tree[p].r <= r) {
pushadd(p, d);
return;
}
pushdown(p);
int mid = tree[p].l + tree[p].r >> 1;
if (l <= mid) change(p << 1, l, r, d);
if (mid < r) change(p << 1 | 1, l, r, d);
update(p);
}
int dfn[SZ], sz[SZ], dfs_clock;
void dfs(int u) {
dfn[u] = ++dfs_clock;
sz[u] = 1;
for (int v : g[u]) {
dfs(v);
sz[u] += sz[v];
}
}
int main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
build_graph();
for (int i = n; i >= 1; i--)
if (!dfn[i]) dfs(i);
build(1, 1, n);
for (int i = 1; i <= k; i++) change(1, dfn[i], dfn[i] + sz[i] - 1, 1);
for (int i = k; i <= n; i++) {
printf("%d ", tree[1].maxn);
if (i < n) change(1, dfn[i + 1], dfn[i + 1] + sz[i + 1] - 1, 1);
change(1, dfn[i - k + 1], dfn[i - k + 1] + sz[i - k + 1] - 1, -1);
}
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 10;
const int SZ = 1e6 + 10;
const int mod = 1e9 + 7;
const double PI = acos(-1);
const double eps = 1e-7;
long long read() {
long long n = 0;
char a = getchar();
bool flag = 0;
while (a > '9' || a < '0') {
if (a == '-') flag = 1;
a = getchar();
}
while (a <= '9' && a >= '0') {
n = n * 10 + a - '0', a = getchar();
}
if (flag) n = -n;
return n;
}
int n, k, a[SZ];
vector<int> g[SZ];
void build_graph() {
stack<int> S;
for (int i = n; i >= 1; i--) {
while (S.size() && a[S.top()] <= a[i]) S.pop();
if (S.size()) g[S.top()].push_back(i);
S.push(i);
}
}
struct seg {
int l, r;
int add, maxn;
} tree[SZ * 4];
void update(int p) {
tree[p].maxn = max(tree[p << 1].maxn, tree[p << 1 | 1].maxn);
}
void build(int p, int l, int r) {
tree[p].l = l;
tree[p].r = r;
if (l == r) return;
int mid = l + r >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
}
void pushadd(int p, int d) {
tree[p].add += d;
tree[p].maxn += d;
}
void pushdown(int p) {
if (tree[p].add) {
pushadd(p << 1, tree[p].add);
pushadd(p << 1 | 1, tree[p].add);
tree[p].add = 0;
}
}
void change(int p, int l, int r, int d) {
if (l <= tree[p].l && tree[p].r <= r) {
pushadd(p, d);
return;
}
pushdown(p);
int mid = tree[p].l + tree[p].r >> 1;
if (l <= mid) change(p << 1, l, r, d);
if (mid < r) change(p << 1 | 1, l, r, d);
update(p);
}
int dfn[SZ], sz[SZ], dfs_clock;
void dfs(int u) {
dfn[u] = ++dfs_clock;
sz[u] = 1;
for (int v : g[u]) {
dfs(v);
sz[u] += sz[v];
}
}
int main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
build_graph();
for (int i = n; i >= 1; i--)
if (!dfn[i]) dfs(i);
build(1, 1, n);
for (int i = 1; i <= k; i++) change(1, dfn[i], dfn[i] + sz[i] - 1, 1);
for (int i = k; i <= n; i++) {
printf("%d ", tree[1].maxn);
if (i < n) change(1, dfn[i + 1], dfn[i + 1] + sz[i + 1] - 1, 1);
change(1, dfn[i - k + 1], dfn[i - k + 1] + sz[i - k + 1] - 1, -1);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
template <typename T>
ostream &operator+(ostream &out, const vector<T> &vec) {
for (const auto &x : vec) {
out << x << " ";
}
out << "\n";
return out;
}
template <typename T>
ostream &operator*(ostream &out, const vector<T> &vec) {
for (const auto &x : vec) {
out + x;
}
return out;
}
template <typename T>
istream &operator>>(istream &in, vector<T> &vec) {
for (auto &x : vec) {
in >> x;
}
return in;
}
struct Tree {
int n;
vector<vector<int>> adj;
Tree(int n) : n(n) { adj.resize(n); }
void addedge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
};
struct EulerTour : Tree {
private:
int t;
void dfs(int u, int p) {
tin[u] = t++;
eulertour.push_back(u);
for (const auto &v : adj[u]) {
if (v != p) dfs(v, u);
}
tout[u] = t++;
eulertour.push_back(u);
}
public:
vector<int> tin, tout, eulertour;
void ComputeEulerTour(int root = 0) {
t = 0;
tin.resize(n), tout.resize(n), eulertour.reserve(n << 1);
dfs(root, root);
}
bool isanc(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }
EulerTour(int n) : Tree(n) {}
};
template <typename T, typename U>
struct lazy_segment_tree {
int n;
int H;
T base;
vector<T> segtree;
vector<U> lazytree;
vector<bool> isempty;
function<T(const T &, const T &)> join;
function<T(int, const U &, const T &)> assign;
function<U(const U &, const U &)> lazyassign;
lazy_segment_tree(vector<T> &seq, function<T(const T &, const T &)> join,
function<T(int, const U &, const T &)> assign,
function<U(const U &, const U &)> lazyassign, T base)
: join(join), assign(assign), lazyassign(lazyassign), base(base) {
n = seq.size();
H = 32 - __builtin_clz(n);
segtree.resize(2 * n);
lazytree.resize(n);
isempty.resize(n, 1);
for (int i = 0; i < n; i++) {
segtree[n + i] = seq[i];
}
for (int i = n - 1; i >= 1; i--) {
segtree[i] = join(segtree[(i << 1)], segtree[(i << 1) | 1]);
}
}
lazy_segment_tree(int n, function<T(const T &, const T &)> join,
function<T(int, const U &, const T &)> assign,
function<U(const U &, const U &)> lazyassign, T base)
: join(join), assign(assign), lazyassign(lazyassign), base(base), n(n) {
H = 32 - __builtin_clz(n);
segtree.resize(2 * n);
lazytree.resize(n);
isempty.resize(n, 1);
}
void calc(int pos, int h) {
segtree[pos] = join(segtree[(pos << 1)], segtree[(pos << 1) | 1]);
if (!isempty[pos]) {
segtree[pos] = assign(h, lazytree[pos], segtree[pos]);
}
}
void apply(int pos, U value, int h) {
segtree[pos] = assign(h, value, segtree[pos]);
if (pos < n) {
if (!isempty[pos]) {
lazytree[pos] = lazyassign(value, lazytree[pos]);
} else {
lazytree[pos] = value;
}
isempty[pos] = 0;
}
}
void updatenode(int pos) {
int h = 0;
pos += n;
while (pos > 1) {
h++;
pos >>= 1;
calc(pos, h);
}
}
void push(int pos) {
int h = H;
for (pos += n; h; --h) {
int x = (pos >> h);
if (!isempty[x]) {
apply((x << 1), lazytree[x], h - 1);
apply((x << 1) | 1, lazytree[x], h - 1);
isempty[x] = 1;
}
}
}
void updaterange(int l, int r, U value) {
push(l);
push(r);
int k = 0;
int l0 = l, r0 = r;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1, k++) {
if (l & 1) {
apply(l++, value, k);
}
if (r & 1) {
apply(--r, value, k);
}
}
updatenode(l0);
updatenode(r0);
}
T query(int l, int r) {
push(l);
push(r);
T ansl = base;
T ansr = base;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
ansl = join(ansl, segtree[l++]);
}
if (r & 1) {
ansr = join(segtree[--r], ansr);
}
}
return join(ansl, ansr);
}
};
const int INF = 1e9;
void solve() {
int n, k;
cin >> n >> k;
vector<int> seq(n);
cin >> seq;
EulerTour tree(n + 1);
stack<int> values;
for (int i = n - 1; i >= 0; --i) {
while (!values.empty() && seq[values.top()] <= seq[i]) {
values.pop();
}
if (values.empty()) {
tree.addedge(i, n);
} else {
tree.addedge(i, values.top());
}
values.push(i);
}
tree.ComputeEulerTour(n);
lazy_segment_tree<ll, ll> segtree(
2 * n + 2, [&](ll x, ll y) { return max(x, y); },
[&](int h, ll l, ll x) { return l + x; },
[&](ll x, ll y) { return x + y; }, -INF);
for (int i = 0; i < k; i++) {
segtree.updaterange(tree.tin[i], tree.tout[i], 1);
}
cout << segtree.query(0, 2 * n + 1) << " ";
for (int i = k; i < n; i++) {
segtree.updaterange(tree.tin[i - k], tree.tout[i - k], -INF);
segtree.updaterange(tree.tin[i], tree.tout[i], 1);
cout << segtree.query(0, 2 * n + 1) << " ";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
template <typename T>
ostream &operator+(ostream &out, const vector<T> &vec) {
for (const auto &x : vec) {
out << x << " ";
}
out << "\n";
return out;
}
template <typename T>
ostream &operator*(ostream &out, const vector<T> &vec) {
for (const auto &x : vec) {
out + x;
}
return out;
}
template <typename T>
istream &operator>>(istream &in, vector<T> &vec) {
for (auto &x : vec) {
in >> x;
}
return in;
}
struct Tree {
int n;
vector<vector<int>> adj;
Tree(int n) : n(n) { adj.resize(n); }
void addedge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
};
struct EulerTour : Tree {
private:
int t;
void dfs(int u, int p) {
tin[u] = t++;
eulertour.push_back(u);
for (const auto &v : adj[u]) {
if (v != p) dfs(v, u);
}
tout[u] = t++;
eulertour.push_back(u);
}
public:
vector<int> tin, tout, eulertour;
void ComputeEulerTour(int root = 0) {
t = 0;
tin.resize(n), tout.resize(n), eulertour.reserve(n << 1);
dfs(root, root);
}
bool isanc(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }
EulerTour(int n) : Tree(n) {}
};
template <typename T, typename U>
struct lazy_segment_tree {
int n;
int H;
T base;
vector<T> segtree;
vector<U> lazytree;
vector<bool> isempty;
function<T(const T &, const T &)> join;
function<T(int, const U &, const T &)> assign;
function<U(const U &, const U &)> lazyassign;
lazy_segment_tree(vector<T> &seq, function<T(const T &, const T &)> join,
function<T(int, const U &, const T &)> assign,
function<U(const U &, const U &)> lazyassign, T base)
: join(join), assign(assign), lazyassign(lazyassign), base(base) {
n = seq.size();
H = 32 - __builtin_clz(n);
segtree.resize(2 * n);
lazytree.resize(n);
isempty.resize(n, 1);
for (int i = 0; i < n; i++) {
segtree[n + i] = seq[i];
}
for (int i = n - 1; i >= 1; i--) {
segtree[i] = join(segtree[(i << 1)], segtree[(i << 1) | 1]);
}
}
lazy_segment_tree(int n, function<T(const T &, const T &)> join,
function<T(int, const U &, const T &)> assign,
function<U(const U &, const U &)> lazyassign, T base)
: join(join), assign(assign), lazyassign(lazyassign), base(base), n(n) {
H = 32 - __builtin_clz(n);
segtree.resize(2 * n);
lazytree.resize(n);
isempty.resize(n, 1);
}
void calc(int pos, int h) {
segtree[pos] = join(segtree[(pos << 1)], segtree[(pos << 1) | 1]);
if (!isempty[pos]) {
segtree[pos] = assign(h, lazytree[pos], segtree[pos]);
}
}
void apply(int pos, U value, int h) {
segtree[pos] = assign(h, value, segtree[pos]);
if (pos < n) {
if (!isempty[pos]) {
lazytree[pos] = lazyassign(value, lazytree[pos]);
} else {
lazytree[pos] = value;
}
isempty[pos] = 0;
}
}
void updatenode(int pos) {
int h = 0;
pos += n;
while (pos > 1) {
h++;
pos >>= 1;
calc(pos, h);
}
}
void push(int pos) {
int h = H;
for (pos += n; h; --h) {
int x = (pos >> h);
if (!isempty[x]) {
apply((x << 1), lazytree[x], h - 1);
apply((x << 1) | 1, lazytree[x], h - 1);
isempty[x] = 1;
}
}
}
void updaterange(int l, int r, U value) {
push(l);
push(r);
int k = 0;
int l0 = l, r0 = r;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1, k++) {
if (l & 1) {
apply(l++, value, k);
}
if (r & 1) {
apply(--r, value, k);
}
}
updatenode(l0);
updatenode(r0);
}
T query(int l, int r) {
push(l);
push(r);
T ansl = base;
T ansr = base;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
ansl = join(ansl, segtree[l++]);
}
if (r & 1) {
ansr = join(segtree[--r], ansr);
}
}
return join(ansl, ansr);
}
};
const int INF = 1e9;
void solve() {
int n, k;
cin >> n >> k;
vector<int> seq(n);
cin >> seq;
EulerTour tree(n + 1);
stack<int> values;
for (int i = n - 1; i >= 0; --i) {
while (!values.empty() && seq[values.top()] <= seq[i]) {
values.pop();
}
if (values.empty()) {
tree.addedge(i, n);
} else {
tree.addedge(i, values.top());
}
values.push(i);
}
tree.ComputeEulerTour(n);
lazy_segment_tree<ll, ll> segtree(
2 * n + 2, [&](ll x, ll y) { return max(x, y); },
[&](int h, ll l, ll x) { return l + x; },
[&](ll x, ll y) { return x + y; }, -INF);
for (int i = 0; i < k; i++) {
segtree.updaterange(tree.tin[i], tree.tout[i], 1);
}
cout << segtree.query(0, 2 * n + 1) << " ";
for (int i = k; i < n; i++) {
segtree.updaterange(tree.tin[i - k], tree.tout[i - k], -INF);
segtree.updaterange(tree.tin[i], tree.tout[i], 1);
cout << segtree.query(0, 2 * n + 1) << " ";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e6 + 5;
int N, K, A[INF], Id[INF], Size[INF], F[4 * INF], mrk[4 * INF], l = 1, cnt, ans;
stack<pair<int, int> > S;
vector<int> V[INF];
void dfs(int n) {
Id[n] = ++cnt;
Size[n] = 1;
for (int i = 0, zj; i < V[n].size(); i++) {
zj = V[n][i];
dfs(zj);
Size[n] += Size[zj];
}
}
void pu(int O) {
F[O * 2] += mrk[O];
F[O * 2 + 1] += mrk[O];
mrk[O * 2] += mrk[O];
mrk[O * 2 + 1] += mrk[O];
mrk[O] = 0;
}
void add(int O, int l, int r, int s, int t) {
if (l == s && r == t) {
F[O]++;
mrk[O]++;
return;
}
if (mrk[O]) pu(O);
int mid = (l + r) / 2;
if (t <= mid)
add(O * 2, l, mid, s, t);
else if (s > mid)
add(O * 2 + 1, mid + 1, r, s, t);
else {
add(O * 2, l, mid, s, mid);
add(O * 2 + 1, mid + 1, r, mid + 1, t);
}
F[O] = max(F[O * 2], F[O * 2 + 1]);
}
void del(int O, int l, int r, int s, int t) {
if (l == s && r == t) {
F[O]--;
mrk[O]--;
return;
}
if (mrk[O]) pu(O);
int mid = (l + r) / 2;
if (t <= mid)
del(O * 2, l, mid, s, t);
else if (s > mid)
del(O * 2 + 1, mid + 1, r, s, t);
else {
del(O * 2, l, mid, s, mid);
del(O * 2 + 1, mid + 1, r, mid + 1, t);
}
F[O] = max(F[O * 2], F[O * 2 + 1]);
}
int qu(int O, int l, int r, int s, int t) {
if (l == s && r == t) return F[O];
if (mrk[O]) pu(O);
int mid = (l + r) / 2;
if (t <= mid)
return qu(O * 2, l, mid, s, t);
else if (s > mid)
return qu(O * 2 + 1, mid + 1, r, s, t);
else
return max(qu(O * 2, l, mid, s, mid),
qu(O * 2 + 1, mid + 1, r, mid + 1, t));
}
int main() {
scanf("%d %d", &N, &K);
for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
for (int i = 1; i <= N; i++) {
while (!S.empty() && S.top().first < A[i]) {
V[i].push_back(S.top().second);
S.pop();
}
S.push(make_pair(A[i], i));
}
while (!S.empty()) {
V[N + 1].push_back(S.top().second);
S.pop();
}
dfs(N + 1);
for (int i = 1; i <= K; i++) add(1, 1, N + 1, Id[i], Id[i] + Size[i] - 1);
ans = qu(1, 1, N + 1, 1, N + 1);
printf("%d ", ans);
for (int i = K + 1; i <= N; i++) {
add(1, 1, N + 1, Id[i], Id[i] + Size[i] - 1);
if (i - l + 1 > K) {
del(1, 1, N + 1, Id[l], Id[l] + Size[l] - 1);
l++;
}
ans = qu(1, 1, N + 1, 1, N + 1);
printf("%d ", ans);
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e6 + 5;
int N, K, A[INF], Id[INF], Size[INF], F[4 * INF], mrk[4 * INF], l = 1, cnt, ans;
stack<pair<int, int> > S;
vector<int> V[INF];
void dfs(int n) {
Id[n] = ++cnt;
Size[n] = 1;
for (int i = 0, zj; i < V[n].size(); i++) {
zj = V[n][i];
dfs(zj);
Size[n] += Size[zj];
}
}
void pu(int O) {
F[O * 2] += mrk[O];
F[O * 2 + 1] += mrk[O];
mrk[O * 2] += mrk[O];
mrk[O * 2 + 1] += mrk[O];
mrk[O] = 0;
}
void add(int O, int l, int r, int s, int t) {
if (l == s && r == t) {
F[O]++;
mrk[O]++;
return;
}
if (mrk[O]) pu(O);
int mid = (l + r) / 2;
if (t <= mid)
add(O * 2, l, mid, s, t);
else if (s > mid)
add(O * 2 + 1, mid + 1, r, s, t);
else {
add(O * 2, l, mid, s, mid);
add(O * 2 + 1, mid + 1, r, mid + 1, t);
}
F[O] = max(F[O * 2], F[O * 2 + 1]);
}
void del(int O, int l, int r, int s, int t) {
if (l == s && r == t) {
F[O]--;
mrk[O]--;
return;
}
if (mrk[O]) pu(O);
int mid = (l + r) / 2;
if (t <= mid)
del(O * 2, l, mid, s, t);
else if (s > mid)
del(O * 2 + 1, mid + 1, r, s, t);
else {
del(O * 2, l, mid, s, mid);
del(O * 2 + 1, mid + 1, r, mid + 1, t);
}
F[O] = max(F[O * 2], F[O * 2 + 1]);
}
int qu(int O, int l, int r, int s, int t) {
if (l == s && r == t) return F[O];
if (mrk[O]) pu(O);
int mid = (l + r) / 2;
if (t <= mid)
return qu(O * 2, l, mid, s, t);
else if (s > mid)
return qu(O * 2 + 1, mid + 1, r, s, t);
else
return max(qu(O * 2, l, mid, s, mid),
qu(O * 2 + 1, mid + 1, r, mid + 1, t));
}
int main() {
scanf("%d %d", &N, &K);
for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
for (int i = 1; i <= N; i++) {
while (!S.empty() && S.top().first < A[i]) {
V[i].push_back(S.top().second);
S.pop();
}
S.push(make_pair(A[i], i));
}
while (!S.empty()) {
V[N + 1].push_back(S.top().second);
S.pop();
}
dfs(N + 1);
for (int i = 1; i <= K; i++) add(1, 1, N + 1, Id[i], Id[i] + Size[i] - 1);
ans = qu(1, 1, N + 1, 1, N + 1);
printf("%d ", ans);
for (int i = K + 1; i <= N; i++) {
add(1, 1, N + 1, Id[i], Id[i] + Size[i] - 1);
if (i - l + 1 > K) {
del(1, 1, N + 1, Id[l], Id[l] + Size[l] - 1);
l++;
}
ans = qu(1, 1, N + 1, 1, N + 1);
printf("%d ", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 200000000000000000LL;
const long double EPS = 1e-9;
const int inf = 0x3f3f3f3f;
struct SegmentTree {
int Tree[4 * 1000007], Lazy[4 * 1000007];
SegmentTree() {}
void pushdown(int at) {
if (Lazy[at]) {
Lazy[(at << 1)] += Lazy[at];
Lazy[((at << 1) | 1)] += Lazy[at];
Tree[(at << 1)] += Lazy[at];
Tree[((at << 1) | 1)] += Lazy[at];
Lazy[at] = 0;
}
}
void build(int at, int l, int r) {
Lazy[at] = 0;
if (l == r) {
Tree[at] = 0;
return;
}
int mid = (l + r) / 2;
build((at << 1), l, mid);
build(((at << 1) | 1), mid + 1, r);
Tree[at] = max(Tree[(at << 1)], Tree[((at << 1) | 1)]);
Lazy[at] = 0;
}
void update(int at, int l, int r, int x, int y, int val) {
if (x > r || y < l) return;
if (x <= l && r <= y) {
Tree[at] += val;
Lazy[at] += val;
return;
}
if (l != r) pushdown(at);
int mid = (l + r) / 2;
update((at << 1), l, mid, x, y, val);
update(((at << 1) | 1), mid + 1, r, x, y, val);
Tree[at] = max(Tree[(at << 1)], Tree[((at << 1) | 1)]);
}
int query(int at, int l, int r, int x, int y) {
if (x > r || y < l) return -inf;
if (x <= l && r <= y) return Tree[at];
if (l != r) pushdown(at);
int mid = (l + r) / 2;
return max(query((at << 1), l, mid, x, y),
query(((at << 1) | 1), mid + 1, r, x, y));
}
} seg;
const int N = 1e6 + 7;
vector<int> g[N];
int a[N];
int in[N], out[N], counter = 0;
int n, k;
void dfs(int u) {
in[u] = ++counter;
for (int v : g[u]) dfs(v);
out[u] = counter;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
a[n + 1] = inf;
stack<int> st;
st.push(n + 1);
for (int i = n; i > 0; --i) {
while (!st.empty() and a[st.top()] <= a[i]) {
st.pop();
}
int pos = st.top();
g[pos].push_back(i);
st.push(i);
}
counter = 0;
dfs(n + 1);
seg.build(1, 1, counter);
for (int i = 1; i <= n; ++i) {
seg.update(1, 1, counter, in[i], out[i], 1);
int ep = i - k;
if (ep > 0) seg.update(1, 1, counter, in[ep], out[ep], -1);
if (i >= k) {
int res = seg.query(1, 1, counter, 1, counter);
cout << res << " ";
}
}
cout << "\n";
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = 200000000000000000LL;
const long double EPS = 1e-9;
const int inf = 0x3f3f3f3f;
struct SegmentTree {
int Tree[4 * 1000007], Lazy[4 * 1000007];
SegmentTree() {}
void pushdown(int at) {
if (Lazy[at]) {
Lazy[(at << 1)] += Lazy[at];
Lazy[((at << 1) | 1)] += Lazy[at];
Tree[(at << 1)] += Lazy[at];
Tree[((at << 1) | 1)] += Lazy[at];
Lazy[at] = 0;
}
}
void build(int at, int l, int r) {
Lazy[at] = 0;
if (l == r) {
Tree[at] = 0;
return;
}
int mid = (l + r) / 2;
build((at << 1), l, mid);
build(((at << 1) | 1), mid + 1, r);
Tree[at] = max(Tree[(at << 1)], Tree[((at << 1) | 1)]);
Lazy[at] = 0;
}
void update(int at, int l, int r, int x, int y, int val) {
if (x > r || y < l) return;
if (x <= l && r <= y) {
Tree[at] += val;
Lazy[at] += val;
return;
}
if (l != r) pushdown(at);
int mid = (l + r) / 2;
update((at << 1), l, mid, x, y, val);
update(((at << 1) | 1), mid + 1, r, x, y, val);
Tree[at] = max(Tree[(at << 1)], Tree[((at << 1) | 1)]);
}
int query(int at, int l, int r, int x, int y) {
if (x > r || y < l) return -inf;
if (x <= l && r <= y) return Tree[at];
if (l != r) pushdown(at);
int mid = (l + r) / 2;
return max(query((at << 1), l, mid, x, y),
query(((at << 1) | 1), mid + 1, r, x, y));
}
} seg;
const int N = 1e6 + 7;
vector<int> g[N];
int a[N];
int in[N], out[N], counter = 0;
int n, k;
void dfs(int u) {
in[u] = ++counter;
for (int v : g[u]) dfs(v);
out[u] = counter;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
a[n + 1] = inf;
stack<int> st;
st.push(n + 1);
for (int i = n; i > 0; --i) {
while (!st.empty() and a[st.top()] <= a[i]) {
st.pop();
}
int pos = st.top();
g[pos].push_back(i);
st.push(i);
}
counter = 0;
dfs(n + 1);
seg.build(1, 1, counter);
for (int i = 1; i <= n; ++i) {
seg.update(1, 1, counter, in[i], out[i], 1);
int ep = i - k;
if (ep > 0) seg.update(1, 1, counter, in[ep], out[ep], -1);
if (i >= k) {
int res = seg.query(1, 1, counter, 1, counter);
cout << res << " ";
}
}
cout << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e6;
int n, k, a[mxN], l[mxN];
struct dsu {
int p[mxN], ld[mxN], lb[mxN];
void init() {
iota(p, p + n, 0);
iota(lb, lb + n, 0);
}
int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); }
void join(int x, int y) {
x = find(x), y = find(y);
p[x] = y;
lb[y] = lb[x];
ld[y] += ld[x];
}
void upd(int i, int j) {
i = find(i);
++ld[i];
if (j < n) --ld[j];
while (ld[i] >= 0 && lb[i]) join(lb[i] - 1, i);
}
void upd2(int i) {
if (i < k) return;
int j = find(i - k + 1);
while (lb[j] > i - k) join(lb[j] - 1, j);
}
int qry() { return ld[find(0)]; }
} d;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
d.init();
for (int i = 0; i < n; ++i) {
d.upd2(i);
l[i] = i - 1;
while (l[i] >= 0 && a[l[i]] < a[i]) {
l[i] = l[l[i]];
}
d.upd(l[i] + 1, i + 1);
if (i >= k - 1) cout << d.qry() << " ";
}
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e6;
int n, k, a[mxN], l[mxN];
struct dsu {
int p[mxN], ld[mxN], lb[mxN];
void init() {
iota(p, p + n, 0);
iota(lb, lb + n, 0);
}
int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); }
void join(int x, int y) {
x = find(x), y = find(y);
p[x] = y;
lb[y] = lb[x];
ld[y] += ld[x];
}
void upd(int i, int j) {
i = find(i);
++ld[i];
if (j < n) --ld[j];
while (ld[i] >= 0 && lb[i]) join(lb[i] - 1, i);
}
void upd2(int i) {
if (i < k) return;
int j = find(i - k + 1);
while (lb[j] > i - k) join(lb[j] - 1, j);
}
int qry() { return ld[find(0)]; }
} d;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
d.init();
for (int i = 0; i < n; ++i) {
d.upd2(i);
l[i] = i - 1;
while (l[i] >= 0 && a[l[i]] < a[i]) {
l[i] = l[l[i]];
}
d.upd(l[i] + 1, i + 1);
if (i >= k - 1) cout << d.qry() << " ";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int const1 = 998244353;
vector<vector<int> > g;
vector<int> tin, tout;
int timer = 0;
void dfs(int v) {
tin[v] = timer++;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
dfs(to);
}
tout[v] = timer++;
}
vector<int> tree, upd;
void push(int v, int l, int r) {
if (l == r) {
return;
}
tree[v * 2] += upd[v];
upd[v * 2] += upd[v];
tree[v * 2 + 1] += upd[v];
upd[v * 2 + 1] += upd[v];
upd[v] = 0;
}
void update(int v, int l, int r, int al, int ar, int val) {
if (l >= al && r <= ar) {
upd[v] += val;
tree[v] += val;
} else if (l <= ar && r >= al) {
push(v, l, r);
update(v * 2, l, (r + l) / 2, al, ar, val);
update(v * 2 + 1, (r + l) / 2 + 1, r, al, ar, val);
tree[v] = max(tree[v * 2 + 1], tree[v * 2]);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k;
cin >> n >> k;
tin.resize(n + 1);
tout.resize(n + 1);
g.resize(n + 1);
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
}
stack<int> q;
for (int i = n - 1; i >= 0; i--) {
while (q.size() > 0 && a[q.top()] <= a[i]) {
q.pop();
}
if (q.size() == 0) {
g[0].push_back(i + 1);
} else {
g[q.top() + 1].push_back(i + 1);
}
q.push(i);
}
dfs(0);
tree.resize(4 * timer);
upd.resize(4 * timer);
for (int i = 0; i < n; i++) {
update(1, 0, timer, tin[i + 1], tout[i + 1], 1);
if (i >= k) {
update(1, 0, timer, tin[i + 1 - k], tout[i + 1 - k], -1);
}
if (i >= k - 1) {
cout << tree[1] << " ";
}
}
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int const1 = 998244353;
vector<vector<int> > g;
vector<int> tin, tout;
int timer = 0;
void dfs(int v) {
tin[v] = timer++;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
dfs(to);
}
tout[v] = timer++;
}
vector<int> tree, upd;
void push(int v, int l, int r) {
if (l == r) {
return;
}
tree[v * 2] += upd[v];
upd[v * 2] += upd[v];
tree[v * 2 + 1] += upd[v];
upd[v * 2 + 1] += upd[v];
upd[v] = 0;
}
void update(int v, int l, int r, int al, int ar, int val) {
if (l >= al && r <= ar) {
upd[v] += val;
tree[v] += val;
} else if (l <= ar && r >= al) {
push(v, l, r);
update(v * 2, l, (r + l) / 2, al, ar, val);
update(v * 2 + 1, (r + l) / 2 + 1, r, al, ar, val);
tree[v] = max(tree[v * 2 + 1], tree[v * 2]);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k;
cin >> n >> k;
tin.resize(n + 1);
tout.resize(n + 1);
g.resize(n + 1);
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
}
stack<int> q;
for (int i = n - 1; i >= 0; i--) {
while (q.size() > 0 && a[q.top()] <= a[i]) {
q.pop();
}
if (q.size() == 0) {
g[0].push_back(i + 1);
} else {
g[q.top() + 1].push_back(i + 1);
}
q.push(i);
}
dfs(0);
tree.resize(4 * timer);
upd.resize(4 * timer);
for (int i = 0; i < n; i++) {
update(1, 0, timer, tin[i + 1], tout[i + 1], 1);
if (i >= k) {
update(1, 0, timer, tin[i + 1 - k], tout[i + 1 - k], -1);
}
if (i >= k - 1) {
cout << tree[1] << " ";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class TH>
void _dbg(const char *sdbg, TH h) {
cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << "=" << h << ",";
_dbg(sdbg + 1, a...);
}
mt19937 r(chrono::steady_clock::now().time_since_epoch().count());
long long rand_range(long long a, long long b) {
assert(a <= b);
return a + r() % (b - a + 1);
}
const long long K = 1000;
const long long KK = K * K;
const long long MIL = KK * K;
const long long INF = MIL * MIL;
const long long MOD = MIL + 7;
const long long N = 1e6 + 10;
vector<long long> G[N];
bool vis[N];
long long tim = 0;
pair<long long, long long> czas[N];
void dfs(long long x) {
czas[x].first = tim++;
vis[x] = true;
for (long long i : G[x])
if (!vis[i]) dfs(i);
czas[x].second = tim - 1;
}
struct Node {
Node() : Node(0){};
Node(long long v) {
val[0] = v;
val[1] = 0;
}
Node(Node a, Node b) {
val[0] = max(a.val[0], b.val[0]);
val[1] = 0;
}
void take(Node node, long long len) {
val[0] += node.val[1];
val[1] += node.val[1];
}
void reset() { val[1] = 0; }
void add(long long v, long long len) {
val[0] += v;
val[1] += v;
}
long long val[2];
};
template <typename Nod, typename Vobj>
struct RangeRangeTree {
long long base;
vector<Nod> drz;
vector<bool> brud;
RangeRangeTree(Vobj &o) {
base = 1;
while (base < (long long)((o).size())) base *= 2;
drz.resize(2 * base);
brud.resize(2 * base);
for (long long i = ((0)); i <= (((long long)((o).size())) - 1); i++)
drz[base + i] = Nod(o[i]);
for (long long i = (base - 1); i >= (1); i--)
drz[i] = Nod(drz[2 * i], drz[2 * i + 1]);
}
void czysc(long long x, long long len) {
drz[2 * x].take(drz[x], len);
drz[2 * x + 1].take(drz[x], len);
drz[x].reset();
brud[2 * x] = brud[2 * x + 1] = 1;
brud[x] = 0;
}
void update(long long l, long long r, long long v, long long p, long long k,
long long x) {
if (r < p || l > k) return;
if (l <= p && k <= r) {
drz[x].add(v, k - p + 1);
brud[x] = 1;
return;
}
if (brud[x]) czysc(x, (k - p + 1) / 2);
long long sr = (p + k) / 2;
update(l, r, v, p, sr, 2 * x);
update(l, r, v, sr + 1, k, 2 * x + 1);
drz[x] = Nod(drz[2 * x], drz[2 * x + 1]);
}
void update(long long l, long long r, long long v) {
update(l, r, v, 0, base - 1, 1);
}
Nod query(long long l, long long r, long long p, long long k, long long x) {
if (r < p || l > k) return Node();
if (l <= p && k <= r) {
Nod pom = drz[x];
return drz[x];
}
if (brud[x]) czysc(x, (k - p + 1) / 2);
long long sr = (p + k) / 2;
return Nod(query(l, r, p, sr, 2 * x), query(l, r, sr + 1, k, 2 * x + 1));
}
Nod query(long long l, long long r) { return query(l, r, 0, base - 1, 1); }
};
void solve() {
long long n, k;
cin >> n >> k;
vector<long long> a(n + 1);
for (long long i = ((0)); i <= ((n)-1); i++) cin >> a[i];
a[n] = MIL;
vector<pair<long long, long long> > stos;
stos.emplace_back(MIL, n);
for (long long i = (n - 1); i >= (0); i--) {
while (stos.back().first <= a[i]) stos.pop_back();
G[i].emplace_back(stos.back().second);
G[stos.back().second].emplace_back(i);
stos.emplace_back(a[i], i);
}
dfs(n);
vector<long long> vpom(n + 1, 0);
struct RangeRangeTree<Node, vector<long long> > drz(vpom);
for (long long i = ((0)); i <= ((k - 1) - 1); i++)
drz.update(czas[i].first, czas[i].second, 1);
for (long long i = ((k - 1)); i <= ((n)-1); i++) {
drz.update(czas[i].first, czas[i].second, 1);
cout << drz.drz[1].val[0] << " ";
drz.update(czas[i - k + 1].first, czas[i - k + 1].second, -1);
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cout << setprecision(9) << fixed;
cerr << setprecision(6) << fixed;
long long test = 1;
while (test--) {
solve();
}
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class TH>
void _dbg(const char *sdbg, TH h) {
cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << "=" << h << ",";
_dbg(sdbg + 1, a...);
}
mt19937 r(chrono::steady_clock::now().time_since_epoch().count());
long long rand_range(long long a, long long b) {
assert(a <= b);
return a + r() % (b - a + 1);
}
const long long K = 1000;
const long long KK = K * K;
const long long MIL = KK * K;
const long long INF = MIL * MIL;
const long long MOD = MIL + 7;
const long long N = 1e6 + 10;
vector<long long> G[N];
bool vis[N];
long long tim = 0;
pair<long long, long long> czas[N];
void dfs(long long x) {
czas[x].first = tim++;
vis[x] = true;
for (long long i : G[x])
if (!vis[i]) dfs(i);
czas[x].second = tim - 1;
}
struct Node {
Node() : Node(0){};
Node(long long v) {
val[0] = v;
val[1] = 0;
}
Node(Node a, Node b) {
val[0] = max(a.val[0], b.val[0]);
val[1] = 0;
}
void take(Node node, long long len) {
val[0] += node.val[1];
val[1] += node.val[1];
}
void reset() { val[1] = 0; }
void add(long long v, long long len) {
val[0] += v;
val[1] += v;
}
long long val[2];
};
template <typename Nod, typename Vobj>
struct RangeRangeTree {
long long base;
vector<Nod> drz;
vector<bool> brud;
RangeRangeTree(Vobj &o) {
base = 1;
while (base < (long long)((o).size())) base *= 2;
drz.resize(2 * base);
brud.resize(2 * base);
for (long long i = ((0)); i <= (((long long)((o).size())) - 1); i++)
drz[base + i] = Nod(o[i]);
for (long long i = (base - 1); i >= (1); i--)
drz[i] = Nod(drz[2 * i], drz[2 * i + 1]);
}
void czysc(long long x, long long len) {
drz[2 * x].take(drz[x], len);
drz[2 * x + 1].take(drz[x], len);
drz[x].reset();
brud[2 * x] = brud[2 * x + 1] = 1;
brud[x] = 0;
}
void update(long long l, long long r, long long v, long long p, long long k,
long long x) {
if (r < p || l > k) return;
if (l <= p && k <= r) {
drz[x].add(v, k - p + 1);
brud[x] = 1;
return;
}
if (brud[x]) czysc(x, (k - p + 1) / 2);
long long sr = (p + k) / 2;
update(l, r, v, p, sr, 2 * x);
update(l, r, v, sr + 1, k, 2 * x + 1);
drz[x] = Nod(drz[2 * x], drz[2 * x + 1]);
}
void update(long long l, long long r, long long v) {
update(l, r, v, 0, base - 1, 1);
}
Nod query(long long l, long long r, long long p, long long k, long long x) {
if (r < p || l > k) return Node();
if (l <= p && k <= r) {
Nod pom = drz[x];
return drz[x];
}
if (brud[x]) czysc(x, (k - p + 1) / 2);
long long sr = (p + k) / 2;
return Nod(query(l, r, p, sr, 2 * x), query(l, r, sr + 1, k, 2 * x + 1));
}
Nod query(long long l, long long r) { return query(l, r, 0, base - 1, 1); }
};
void solve() {
long long n, k;
cin >> n >> k;
vector<long long> a(n + 1);
for (long long i = ((0)); i <= ((n)-1); i++) cin >> a[i];
a[n] = MIL;
vector<pair<long long, long long> > stos;
stos.emplace_back(MIL, n);
for (long long i = (n - 1); i >= (0); i--) {
while (stos.back().first <= a[i]) stos.pop_back();
G[i].emplace_back(stos.back().second);
G[stos.back().second].emplace_back(i);
stos.emplace_back(a[i], i);
}
dfs(n);
vector<long long> vpom(n + 1, 0);
struct RangeRangeTree<Node, vector<long long> > drz(vpom);
for (long long i = ((0)); i <= ((k - 1) - 1); i++)
drz.update(czas[i].first, czas[i].second, 1);
for (long long i = ((k - 1)); i <= ((n)-1); i++) {
drz.update(czas[i].first, czas[i].second, 1);
cout << drz.drz[1].val[0] << " ";
drz.update(czas[i - k + 1].first, czas[i - k + 1].second, -1);
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cout << setprecision(9) << fixed;
cerr << setprecision(6) << fixed;
long long test = 1;
while (test--) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void chmin(A &a, B b) {
if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A &a, B b) {
if (a < b) a = b;
}
template <class W, long long lg = 1>
struct WeightedTree {
struct Edge {
long long to;
W cost;
Edge(long long to, W cost) : to(to), cost(cost) {}
};
long long V;
long long root;
vector<vector<Edge>> G;
vector<vector<long long>> par;
vector<long long> dep, sz, head;
vector<long long> tin, tout;
vector<W> dist;
WeightedTree(long long V = 0, long long root = 0)
: V(V),
root(root),
G(V),
par(lg, vector<long long>(V)),
sz(V),
dep(V),
head(V),
dist(V),
tin(V),
tout(V) {}
void addEdge(long long a, long long b, W c = W(1)) {
G[a].push_back(Edge(b, c));
G[b].push_back(Edge(a, c));
}
void dfs(long long v, long long p, long long d, W c) {
par[0][v] = p;
dep[v] = d;
sz[v] = 1;
dist[v] = c;
for (auto &e : G[v]) {
if (e.to == p) continue;
dfs(e.to, v, d + 1, c + e.cost);
sz[v] += sz[e.to];
if (G[v][0].to == p || sz[e.to] > sz[G[v][0].to]) swap(G[v][0], e);
}
}
void dfs_hld(long long v, long long &tt) {
tin[v] = tt++;
for (auto &e : G[v]) {
if (e.to == par[0][v]) continue;
head[e.to] = (e.to == G[v][0].to) ? head[v] : e.to;
dfs_hld(e.to, tt);
}
tout[v] = tt;
}
void init() {
dfs(root, -1, 0, W(0));
long long tt = 0;
dfs_hld(root, tt);
for (long long i = 0; i + 1 < lg; i++) {
for (long long j = 0; j < V; j++) {
if (par[i][j] == -1)
par[i + 1][j] = -1;
else
par[i + 1][j] = par[i][par[i][j]];
}
}
}
long long getLCA(long long u, long long v) {
if (dep[u] < dep[v]) swap(u, v);
for (long long i = 0; i < (lg); i++)
if ((dep[u] - dep[v]) >> i & 1) u = par[i][u];
if (u == v) return u;
for (long long i = lg - 1; i >= 0; i--)
if (par[i][u] != par[i][v]) u = par[i][u], v = par[i][v];
return par[0][v];
}
long long getLength(long long a, long long b = 0) {
long long l = getLCA(a, b);
return dep[a] + dep[b] - 2 * dep[l];
}
W getDistance(long long a, long long b = 0) {
long long l = getLCA(a, b);
return dist[a] + dist[b] - 2 * dist[l];
}
long long getParent(long long v, long long k = 0) { return par[k][v]; }
long long getDepth(long long v) { return dep[v]; }
long long getIn(long long v) { return tin[v]; }
long long getOut(long long v) { return tout[v]; }
};
WeightedTree<long long> T;
const long long SEG = 1 << 20;
long long dat[SEG * 2], put[SEG * 2];
void add(long long a, long long b, long long x, long long k = 0,
long long l = 0, long long r = SEG) {
if (r <= a || b <= l) return;
if (a <= l && r <= b) {
put[k] += x;
return;
}
add(a, b, x, k * 2 + 1, l, (l + r) / 2);
add(a, b, x, k * 2 + 2, (l + r) / 2, r);
dat[k] =
max(dat[k * 2 + 1] + put[k * 2 + 1], dat[k * 2 + 2] + put[k * 2 + 2]);
}
long long query(long long a, long long b, long long k = 0, long long l = 0,
long long r = SEG) {
if (r <= a || b <= l) return 0;
if (a <= l && r <= b) return dat[k] + put[k];
return max(query(a, b, k * 2 + 1, l, (l + r) / 2),
query(a, b, k * 2 + 2, (l + r) / 2, r)) +
put[k];
}
signed main() {
long long N, K;
scanf("%lld%lld", &N, &K);
vector<long long> A(N);
for (long long i = 0; i < (N); i++) scanf("%lld", &A[i]);
T = WeightedTree<long long>(N + 1, N);
vector<long long> stk;
for (long long i = N - 1; i >= 0; i--) {
while (stk.size() && A[stk.back()] <= A[i]) stk.pop_back();
if (stk.size()) {
T.addEdge(i, stk.back());
} else {
T.addEdge(N, i);
}
stk.push_back(i);
}
long long sum = 0;
for (long long i = 0; i < (N + 1); i++) sum += T.G[i].size();
T.init();
for (long long i = 0; i < (K - 1); i++) {
add(T.getIn(i), T.getOut(i), 1);
}
for (long long i = 0; i + K <= N; i++) {
add(T.getIn(i + K - 1), T.getOut(i + K - 1), 1);
if (i) printf(" ");
printf("%lld", query(T.getIn(N), T.getOut(N)));
add(T.getIn(i), T.getOut(i), -1);
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void chmin(A &a, B b) {
if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A &a, B b) {
if (a < b) a = b;
}
template <class W, long long lg = 1>
struct WeightedTree {
struct Edge {
long long to;
W cost;
Edge(long long to, W cost) : to(to), cost(cost) {}
};
long long V;
long long root;
vector<vector<Edge>> G;
vector<vector<long long>> par;
vector<long long> dep, sz, head;
vector<long long> tin, tout;
vector<W> dist;
WeightedTree(long long V = 0, long long root = 0)
: V(V),
root(root),
G(V),
par(lg, vector<long long>(V)),
sz(V),
dep(V),
head(V),
dist(V),
tin(V),
tout(V) {}
void addEdge(long long a, long long b, W c = W(1)) {
G[a].push_back(Edge(b, c));
G[b].push_back(Edge(a, c));
}
void dfs(long long v, long long p, long long d, W c) {
par[0][v] = p;
dep[v] = d;
sz[v] = 1;
dist[v] = c;
for (auto &e : G[v]) {
if (e.to == p) continue;
dfs(e.to, v, d + 1, c + e.cost);
sz[v] += sz[e.to];
if (G[v][0].to == p || sz[e.to] > sz[G[v][0].to]) swap(G[v][0], e);
}
}
void dfs_hld(long long v, long long &tt) {
tin[v] = tt++;
for (auto &e : G[v]) {
if (e.to == par[0][v]) continue;
head[e.to] = (e.to == G[v][0].to) ? head[v] : e.to;
dfs_hld(e.to, tt);
}
tout[v] = tt;
}
void init() {
dfs(root, -1, 0, W(0));
long long tt = 0;
dfs_hld(root, tt);
for (long long i = 0; i + 1 < lg; i++) {
for (long long j = 0; j < V; j++) {
if (par[i][j] == -1)
par[i + 1][j] = -1;
else
par[i + 1][j] = par[i][par[i][j]];
}
}
}
long long getLCA(long long u, long long v) {
if (dep[u] < dep[v]) swap(u, v);
for (long long i = 0; i < (lg); i++)
if ((dep[u] - dep[v]) >> i & 1) u = par[i][u];
if (u == v) return u;
for (long long i = lg - 1; i >= 0; i--)
if (par[i][u] != par[i][v]) u = par[i][u], v = par[i][v];
return par[0][v];
}
long long getLength(long long a, long long b = 0) {
long long l = getLCA(a, b);
return dep[a] + dep[b] - 2 * dep[l];
}
W getDistance(long long a, long long b = 0) {
long long l = getLCA(a, b);
return dist[a] + dist[b] - 2 * dist[l];
}
long long getParent(long long v, long long k = 0) { return par[k][v]; }
long long getDepth(long long v) { return dep[v]; }
long long getIn(long long v) { return tin[v]; }
long long getOut(long long v) { return tout[v]; }
};
WeightedTree<long long> T;
const long long SEG = 1 << 20;
long long dat[SEG * 2], put[SEG * 2];
void add(long long a, long long b, long long x, long long k = 0,
long long l = 0, long long r = SEG) {
if (r <= a || b <= l) return;
if (a <= l && r <= b) {
put[k] += x;
return;
}
add(a, b, x, k * 2 + 1, l, (l + r) / 2);
add(a, b, x, k * 2 + 2, (l + r) / 2, r);
dat[k] =
max(dat[k * 2 + 1] + put[k * 2 + 1], dat[k * 2 + 2] + put[k * 2 + 2]);
}
long long query(long long a, long long b, long long k = 0, long long l = 0,
long long r = SEG) {
if (r <= a || b <= l) return 0;
if (a <= l && r <= b) return dat[k] + put[k];
return max(query(a, b, k * 2 + 1, l, (l + r) / 2),
query(a, b, k * 2 + 2, (l + r) / 2, r)) +
put[k];
}
signed main() {
long long N, K;
scanf("%lld%lld", &N, &K);
vector<long long> A(N);
for (long long i = 0; i < (N); i++) scanf("%lld", &A[i]);
T = WeightedTree<long long>(N + 1, N);
vector<long long> stk;
for (long long i = N - 1; i >= 0; i--) {
while (stk.size() && A[stk.back()] <= A[i]) stk.pop_back();
if (stk.size()) {
T.addEdge(i, stk.back());
} else {
T.addEdge(N, i);
}
stk.push_back(i);
}
long long sum = 0;
for (long long i = 0; i < (N + 1); i++) sum += T.G[i].size();
T.init();
for (long long i = 0; i < (K - 1); i++) {
add(T.getIn(i), T.getOut(i), 1);
}
for (long long i = 0; i + K <= N; i++) {
add(T.getIn(i + K - 1), T.getOut(i + K - 1), 1);
if (i) printf(" ");
printf("%lld", query(T.getIn(N), T.getOut(N)));
add(T.getIn(i), T.getOut(i), -1);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int INF = 9223372036854775807;
int MOD(int a, int b) {
if (a > b)
return a - b;
else
return b - a;
}
int max3(int a, int b, int c) { return max(c, max(a, b)); }
int min3(int a, int b, int c) { return min(a, min(b, c)); }
long long int power(long long int x, long long int y) {
long long int res = 1;
while (y > 0) {
if (y & 1) res = (res * x) % 1000000007;
y = y >> 1;
x = (x * x) % 1000000007;
}
return res % 1000000007;
}
long long int logg(long long int a) {
long long int x = 0;
while (a > 1) {
x++;
a /= 2;
}
return x;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int abso(long long int x) {
if (x < 0) {
return -x;
}
return x;
}
long long int ceiling(long long int a, long long int b) {
if (a % b == 0) {
return a / b;
} else {
return a / b + 1;
}
}
long long int modinv(long long int x) { return power(x, 1000000007 - 2); }
vector<int> cnt;
vector<int> r;
int n, k;
vector<int> a;
int start;
int fin;
vector<int> ans;
vector<int> tree;
vector<int> lazy;
void build(int node, int start, int end) {
if (start == end) {
tree[node] = ans[start];
} else {
int mid = (start + end) / 2;
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}
}
void del(int ind) {
if (fin <= n + 1 && r[fin] == ind) {
cnt[fin] = INF;
r[fin] = -1;
fin--;
}
}
int add(int ind) {
int temp_index =
lower_bound(cnt.begin() + start, cnt.begin() + fin, a[ind]) - cnt.begin();
temp_index--;
start = temp_index;
cnt[start] = a[ind];
r[start] = ind;
if (start + 1 >= cnt.size() || cnt[start + 1] == INF) {
return r[start] - k + 1;
} else {
return r[start + 1] + 1;
}
}
void updateRange(int node, int start, int end, int l, int r, int val) {
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (start != end) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
if (start > end or start > r or end < l) return;
if (start >= l and end <= r) {
tree[node] += val;
if (start != end) {
lazy[node * 2] += val;
lazy[node * 2 + 1] += val;
}
return;
}
int mid = (start + end) / 2;
updateRange(node * 2, start, mid, l, r, val);
updateRange(node * 2 + 1, mid + 1, end, l, r, val);
tree[node] = max(tree[node * 2], tree[node * 2 + 1]);
}
int queryRange(int node, int start, int end, int l, int r) {
if (start > end or start > r or end < l) return 0;
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (start != end) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
if (start >= l and end <= r) return tree[node];
int mid = (start + end) / 2;
int p1 = queryRange(node * 2, start, mid, l, r);
int p2 = queryRange(node * 2 + 1, mid + 1, end, l, r);
return (max(p1, p2));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> n >> k;
start = n + 2;
fin = n + 2;
a.resize(n);
tree.resize(4 * n + 50);
lazy.resize(4 * n + 50);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cnt.resize(n + 2, -1);
ans.resize(n, 0);
r.resize(n + 2, -1);
build(1, 0, n - 1);
for (int i = 0; i < k; i++) {
int ind = add(i);
updateRange(1, 0, n - 1, max(0, ind), i, 1);
}
cout << queryRange(1, 0, n - 1, 0, k - 1) << ' ';
for (int i = 1; i + k - 1 < n; i++) {
del(i - 1);
int cur_index = add(i + k - 1);
updateRange(1, 0, n - 1, max(0, cur_index), min(n - 1, i + k - 1), 1);
cout << queryRange(1, 0, n - 1, i, i + k - 1) << ' ';
}
}
| ### Prompt
Create a solution in cpp for the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int INF = 9223372036854775807;
int MOD(int a, int b) {
if (a > b)
return a - b;
else
return b - a;
}
int max3(int a, int b, int c) { return max(c, max(a, b)); }
int min3(int a, int b, int c) { return min(a, min(b, c)); }
long long int power(long long int x, long long int y) {
long long int res = 1;
while (y > 0) {
if (y & 1) res = (res * x) % 1000000007;
y = y >> 1;
x = (x * x) % 1000000007;
}
return res % 1000000007;
}
long long int logg(long long int a) {
long long int x = 0;
while (a > 1) {
x++;
a /= 2;
}
return x;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int abso(long long int x) {
if (x < 0) {
return -x;
}
return x;
}
long long int ceiling(long long int a, long long int b) {
if (a % b == 0) {
return a / b;
} else {
return a / b + 1;
}
}
long long int modinv(long long int x) { return power(x, 1000000007 - 2); }
vector<int> cnt;
vector<int> r;
int n, k;
vector<int> a;
int start;
int fin;
vector<int> ans;
vector<int> tree;
vector<int> lazy;
void build(int node, int start, int end) {
if (start == end) {
tree[node] = ans[start];
} else {
int mid = (start + end) / 2;
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}
}
void del(int ind) {
if (fin <= n + 1 && r[fin] == ind) {
cnt[fin] = INF;
r[fin] = -1;
fin--;
}
}
int add(int ind) {
int temp_index =
lower_bound(cnt.begin() + start, cnt.begin() + fin, a[ind]) - cnt.begin();
temp_index--;
start = temp_index;
cnt[start] = a[ind];
r[start] = ind;
if (start + 1 >= cnt.size() || cnt[start + 1] == INF) {
return r[start] - k + 1;
} else {
return r[start + 1] + 1;
}
}
void updateRange(int node, int start, int end, int l, int r, int val) {
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (start != end) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
if (start > end or start > r or end < l) return;
if (start >= l and end <= r) {
tree[node] += val;
if (start != end) {
lazy[node * 2] += val;
lazy[node * 2 + 1] += val;
}
return;
}
int mid = (start + end) / 2;
updateRange(node * 2, start, mid, l, r, val);
updateRange(node * 2 + 1, mid + 1, end, l, r, val);
tree[node] = max(tree[node * 2], tree[node * 2 + 1]);
}
int queryRange(int node, int start, int end, int l, int r) {
if (start > end or start > r or end < l) return 0;
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (start != end) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
if (start >= l and end <= r) return tree[node];
int mid = (start + end) / 2;
int p1 = queryRange(node * 2, start, mid, l, r);
int p2 = queryRange(node * 2 + 1, mid + 1, end, l, r);
return (max(p1, p2));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> n >> k;
start = n + 2;
fin = n + 2;
a.resize(n);
tree.resize(4 * n + 50);
lazy.resize(4 * n + 50);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cnt.resize(n + 2, -1);
ans.resize(n, 0);
r.resize(n + 2, -1);
build(1, 0, n - 1);
for (int i = 0; i < k; i++) {
int ind = add(i);
updateRange(1, 0, n - 1, max(0, ind), i, 1);
}
cout << queryRange(1, 0, n - 1, 0, k - 1) << ' ';
for (int i = 1; i + k - 1 < n; i++) {
del(i - 1);
int cur_index = add(i + k - 1);
updateRange(1, 0, n - 1, max(0, cur_index), min(n - 1, i + k - 1), 1);
cout << queryRange(1, 0, n - 1, i, i + k - 1) << ' ';
}
}
``` |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16000000")
using namespace std;
const int Maxn = 1000005;
const int Maxm = 4194304;
int n, k;
vector<int> neigh[Maxn];
int cur, lef[Maxn], rig[Maxn];
int st[Maxm], flag[Maxm];
void Traverse(int v) {
lef[v] = ++cur;
for (int i = 0; i < neigh[v].size(); i++) Traverse(neigh[v][i]);
rig[v] = cur;
}
void downOn(int v, int f) {
st[v] += f;
flag[v] += f;
}
void Down(int v) {
if (flag[v]) {
downOn(2 * v, flag[v]);
downOn(2 * v + 1, flag[v]);
flag[v] = 0;
}
}
void Union(int v) { st[v] = max(st[2 * v], st[2 * v + 1]); }
void Update(int v, int l, int r, int a, int b, int val) {
if (l == a && r == b)
downOn(v, val);
else {
int m = l + r >> 1;
Down(v);
if (a <= m) Update(2 * v, l, m, a, min(m, b), val);
if (m + 1 <= b) Update(2 * v + 1, m + 1, r, max(m + 1, a), b, val);
Union(v);
}
}
int main() {
scanf("%d %d", &n, &k);
vector<pair<int, int> > S;
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
while (!S.empty() && S.back().first < a) {
neigh[i].push_back(S.back().second);
S.pop_back();
}
S.push_back(pair<int, int>(a, i));
}
for (int i = n; i > 0; i--)
if (!lef[i]) Traverse(i);
for (int i = 1; i < k; i++) Update(1, 1, n, lef[i], rig[i], 1);
for (int i = k; i <= n; i++) {
Update(1, 1, n, lef[i], rig[i], 1);
printf("%d%c", st[1], i + 1 <= n ? ' ' : '\n');
Update(1, 1, n, lef[i - (k - 1)], rig[i - (k - 1)], -1);
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16000000")
using namespace std;
const int Maxn = 1000005;
const int Maxm = 4194304;
int n, k;
vector<int> neigh[Maxn];
int cur, lef[Maxn], rig[Maxn];
int st[Maxm], flag[Maxm];
void Traverse(int v) {
lef[v] = ++cur;
for (int i = 0; i < neigh[v].size(); i++) Traverse(neigh[v][i]);
rig[v] = cur;
}
void downOn(int v, int f) {
st[v] += f;
flag[v] += f;
}
void Down(int v) {
if (flag[v]) {
downOn(2 * v, flag[v]);
downOn(2 * v + 1, flag[v]);
flag[v] = 0;
}
}
void Union(int v) { st[v] = max(st[2 * v], st[2 * v + 1]); }
void Update(int v, int l, int r, int a, int b, int val) {
if (l == a && r == b)
downOn(v, val);
else {
int m = l + r >> 1;
Down(v);
if (a <= m) Update(2 * v, l, m, a, min(m, b), val);
if (m + 1 <= b) Update(2 * v + 1, m + 1, r, max(m + 1, a), b, val);
Union(v);
}
}
int main() {
scanf("%d %d", &n, &k);
vector<pair<int, int> > S;
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
while (!S.empty() && S.back().first < a) {
neigh[i].push_back(S.back().second);
S.pop_back();
}
S.push_back(pair<int, int>(a, i));
}
for (int i = n; i > 0; i--)
if (!lef[i]) Traverse(i);
for (int i = 1; i < k; i++) Update(1, 1, n, lef[i], rig[i], 1);
for (int i = k; i <= n; i++) {
Update(1, 1, n, lef[i], rig[i], 1);
printf("%d%c", st[1], i + 1 <= n ? ' ' : '\n');
Update(1, 1, n, lef[i - (k - 1)], rig[i - (k - 1)], -1);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000 + 5;
const int inf = 0x3f3f3f3f;
int n, k, dfs_clock, A[MAXN], dfn[MAXN], sz[MAXN];
vector<int> G[MAXN];
stack<pair<int, int> > s;
inline void add(int u, int v) { G[u].push_back(v); }
void dfs(int u) {
sz[u] = 1, dfn[u] = ++dfs_clock;
for (int i = 0; i < (int)G[u].size(); ++i) dfs(G[u][i]), sz[u] += sz[G[u][i]];
}
int maxv[MAXN << 2], addv[MAXN << 2];
inline void up(int o) { maxv[o] = max(maxv[(o << 1)], maxv[((o << 1) | 1)]); }
inline void Add(int o, int k) { maxv[o] += k, addv[o] += k; }
inline void down(int o) {
if (addv[o]) {
Add((o << 1), addv[o]);
Add(((o << 1) | 1), addv[o]);
addv[o] = 0;
}
}
inline void modify(int o, int l, int r, int ql, int qr, int k) {
if (qr < l || ql > r) return;
if (ql <= l && r <= qr) return Add(o, k);
down(o);
modify((o << 1), l, ((l + r) >> 1), ql, qr, k);
modify(((o << 1) | 1), ((l + r) >> 1) + 1, r, ql, qr, k);
up(o);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", A + i);
A[++n] = inf;
for (int i = 1; i <= n; ++i) {
while (!s.empty() && s.top().first < A[i]) add(i, s.top().second), s.pop();
s.push(make_pair(A[i], i));
}
dfs(n);
for (int i = 1; i <= k; ++i) modify(1, 1, n, dfn[i], dfn[i] + sz[i] - 1, 1);
printf("%d ", maxv[1]);
for (int i = k + 1; i < n; ++i) {
modify(1, 1, n, dfn[i], dfn[i] + sz[i] - 1, 1);
modify(1, 1, n, dfn[i - k], dfn[i - k] + sz[i - k] - 1, -1);
printf("%d ", maxv[1]);
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000 + 5;
const int inf = 0x3f3f3f3f;
int n, k, dfs_clock, A[MAXN], dfn[MAXN], sz[MAXN];
vector<int> G[MAXN];
stack<pair<int, int> > s;
inline void add(int u, int v) { G[u].push_back(v); }
void dfs(int u) {
sz[u] = 1, dfn[u] = ++dfs_clock;
for (int i = 0; i < (int)G[u].size(); ++i) dfs(G[u][i]), sz[u] += sz[G[u][i]];
}
int maxv[MAXN << 2], addv[MAXN << 2];
inline void up(int o) { maxv[o] = max(maxv[(o << 1)], maxv[((o << 1) | 1)]); }
inline void Add(int o, int k) { maxv[o] += k, addv[o] += k; }
inline void down(int o) {
if (addv[o]) {
Add((o << 1), addv[o]);
Add(((o << 1) | 1), addv[o]);
addv[o] = 0;
}
}
inline void modify(int o, int l, int r, int ql, int qr, int k) {
if (qr < l || ql > r) return;
if (ql <= l && r <= qr) return Add(o, k);
down(o);
modify((o << 1), l, ((l + r) >> 1), ql, qr, k);
modify(((o << 1) | 1), ((l + r) >> 1) + 1, r, ql, qr, k);
up(o);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", A + i);
A[++n] = inf;
for (int i = 1; i <= n; ++i) {
while (!s.empty() && s.top().first < A[i]) add(i, s.top().second), s.pop();
s.push(make_pair(A[i], i));
}
dfs(n);
for (int i = 1; i <= k; ++i) modify(1, 1, n, dfn[i], dfn[i] + sz[i] - 1, 1);
printf("%d ", maxv[1]);
for (int i = k + 1; i < n; ++i) {
modify(1, 1, n, dfn[i], dfn[i] + sz[i] - 1, 1);
modify(1, 1, n, dfn[i - k], dfn[i - k] + sz[i - k] - 1, -1);
printf("%d ", maxv[1]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k, i, a[2000005], tin[2000005], tout[2000005], timer;
int t[2000005 * 4], flag[2000005 * 4];
vector<int> g[2000005];
vector<pair<int, int> > st;
void push(int v, int l, int r) {
if (!flag[v]) return;
if (l != r) {
t[v * 2] += flag[v];
t[v * 2 + 1] += flag[v];
flag[v * 2] += flag[v];
flag[v * 2 + 1] += flag[v];
}
flag[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int x) {
int m = (tl + tr) / 2;
push(v, tl, tr);
if (l > r) return;
if (tl == l && tr == r) {
t[v] = max(-1000000000, t[v] + x);
flag[v] += x;
return;
}
upd(v * 2, tl, m, l, min(r, m), x);
upd(v * 2 + 1, m + 1, tr, max(l, m + 1), r, x);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
void dfs(int v) {
int i, to;
tin[v] = timer++;
for (i = 0; i < g[v].size(); i++) {
to = g[v][i];
dfs(to);
}
tout[v] = timer++;
}
int main() {
ios_base ::sync_with_stdio();
cin.tie(0);
scanf("%d%d", &n, &k);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
while (st.size() > 0 && st.back().first < a[i]) {
g[i].push_back(st.back().second);
st.pop_back();
}
st.push_back(make_pair(a[i], i));
}
while (st.size() != 0) {
dfs(st.back().second);
st.pop_back();
}
for (i = 0; i < n; i++) {
if (i - k >= 0) upd(1, 0, 2 * n, tin[i - k], tout[i - k], -n);
upd(1, 0, 2 * n, tin[i], tout[i], 1);
if (i >= k - 1) printf("%d ", t[1]);
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k, i, a[2000005], tin[2000005], tout[2000005], timer;
int t[2000005 * 4], flag[2000005 * 4];
vector<int> g[2000005];
vector<pair<int, int> > st;
void push(int v, int l, int r) {
if (!flag[v]) return;
if (l != r) {
t[v * 2] += flag[v];
t[v * 2 + 1] += flag[v];
flag[v * 2] += flag[v];
flag[v * 2 + 1] += flag[v];
}
flag[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int x) {
int m = (tl + tr) / 2;
push(v, tl, tr);
if (l > r) return;
if (tl == l && tr == r) {
t[v] = max(-1000000000, t[v] + x);
flag[v] += x;
return;
}
upd(v * 2, tl, m, l, min(r, m), x);
upd(v * 2 + 1, m + 1, tr, max(l, m + 1), r, x);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
void dfs(int v) {
int i, to;
tin[v] = timer++;
for (i = 0; i < g[v].size(); i++) {
to = g[v][i];
dfs(to);
}
tout[v] = timer++;
}
int main() {
ios_base ::sync_with_stdio();
cin.tie(0);
scanf("%d%d", &n, &k);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
while (st.size() > 0 && st.back().first < a[i]) {
g[i].push_back(st.back().second);
st.pop_back();
}
st.push_back(make_pair(a[i], i));
}
while (st.size() != 0) {
dfs(st.back().second);
st.pop_back();
}
for (i = 0; i < n; i++) {
if (i - k >= 0) upd(1, 0, 2 * n, tin[i - k], tout[i - k], -n);
upd(1, 0, 2 * n, tin[i], tout[i], 1);
if (i >= k - 1) printf("%d ", t[1]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k, a[1000006], nxt[1000006], tin[1000006], tout[1000006];
vector<int> s, g[1000006], ord;
int t[4 * 1000006], d[4 * 1000006];
void dfs(int v) {
ord.push_back(v);
tin[v] = ord.size() - 1;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
dfs(to);
}
tout[v] = ord.size() - 1;
}
void push(int v, int l, int r, bool f = 1) {
if (d[v] == 0) return;
if (l != r) {
int m = (l + r) / 2;
d[v * 2] += d[v];
d[v * 2 + 1] += d[v];
if (f) {
push(v * 2, l, m, 0);
push(v * 2 + 1, m + 1, r, 0);
}
}
t[v] += d[v];
d[v] = 0;
}
void add(int v, int l, int r, int i, int j, int val) {
if (i > j) return;
push(v, l, r);
if (l == i && r == j) {
d[v] += val;
push(v, l, r);
return;
}
int m = (l + r) / 2;
add(v * 2, l, m, i, min(m, j), val);
add(v * 2 + 1, m + 1, r, max(m + 1, i), j, val);
push(v * 2, l, m);
push(v * 2 + 1, m + 1, r);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d", a + i);
a[n + 1] = 1000000007ll;
s.push_back(n + 1);
for (int i = n; i >= 1; i--) {
while (a[s.back()] <= a[i]) s.pop_back();
nxt[i] = s.back();
s.push_back(i);
g[nxt[i]].push_back(i);
}
dfs(n + 1);
for (int i = 1; i <= k; i++) add(1, 0, ord.size() - 1, tin[i], tout[i], 1);
printf("%d ", t[1]);
for (int i = k + 1; i <= n; i++) {
add(1, 0, ord.size() - 1, tin[i], tout[i], 1);
add(1, 0, ord.size() - 1, tin[i - k], tout[i - k], -1);
printf("%d ", t[1]);
}
cout << endl;
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k, a[1000006], nxt[1000006], tin[1000006], tout[1000006];
vector<int> s, g[1000006], ord;
int t[4 * 1000006], d[4 * 1000006];
void dfs(int v) {
ord.push_back(v);
tin[v] = ord.size() - 1;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
dfs(to);
}
tout[v] = ord.size() - 1;
}
void push(int v, int l, int r, bool f = 1) {
if (d[v] == 0) return;
if (l != r) {
int m = (l + r) / 2;
d[v * 2] += d[v];
d[v * 2 + 1] += d[v];
if (f) {
push(v * 2, l, m, 0);
push(v * 2 + 1, m + 1, r, 0);
}
}
t[v] += d[v];
d[v] = 0;
}
void add(int v, int l, int r, int i, int j, int val) {
if (i > j) return;
push(v, l, r);
if (l == i && r == j) {
d[v] += val;
push(v, l, r);
return;
}
int m = (l + r) / 2;
add(v * 2, l, m, i, min(m, j), val);
add(v * 2 + 1, m + 1, r, max(m + 1, i), j, val);
push(v * 2, l, m);
push(v * 2 + 1, m + 1, r);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d", a + i);
a[n + 1] = 1000000007ll;
s.push_back(n + 1);
for (int i = n; i >= 1; i--) {
while (a[s.back()] <= a[i]) s.pop_back();
nxt[i] = s.back();
s.push_back(i);
g[nxt[i]].push_back(i);
}
dfs(n + 1);
for (int i = 1; i <= k; i++) add(1, 0, ord.size() - 1, tin[i], tout[i], 1);
printf("%d ", t[1]);
for (int i = k + 1; i <= n; i++) {
add(1, 0, ord.size() - 1, tin[i], tout[i], 1);
add(1, 0, ord.size() - 1, tin[i - k], tout[i - k], -1);
printf("%d ", t[1]);
}
cout << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, m1, len, top, a[1000005], size[1000005], dfn[1000005], nex[1000005],
wen[1000005], hea[1000005], sta[1000005];
struct segment_tree {
int a[4000005], lazy[4000005];
void pushdown(int k) {
int t = lazy[k];
a[k << 1] += t;
lazy[k << 1] += t;
a[(k << 1) | 1] += t;
lazy[(k << 1) | 1] += t;
lazy[k] = 0;
}
void update(int l, int r, int k, int x, int y, int z) {
if (l >= x && r <= y) {
a[k] += z;
lazy[k] += z;
return;
}
pushdown(k);
int mid = (l + r) >> 1;
if (x <= mid) update(l, mid, k << 1, x, y, z);
if (y > mid) update(mid + 1, r, (k << 1) | 1, x, y, z);
a[k] = max(a[k << 1], a[(k << 1) | 1]);
}
int find(int l, int r, int k, int x, int y) {
if (l >= x && r <= y) return a[k];
pushdown(k);
int mid = (l + r) >> 1, ans = 0;
if (x <= mid) ans = find(l, mid, k << 1, x, y);
if (y > mid) ans = max(ans, find(mid + 1, r, (k << 1) | 1, x, y));
return ans;
}
} st;
void add(int x, int y) {
++len;
nex[len] = hea[x];
wen[len] = y;
hea[x] = len;
}
void dfs(int x) {
dfn[x] = ++m1;
size[x] = 1;
for (int i = hea[x]; i; i = nex[i]) {
dfs(wen[i]);
size[x] += size[wen[i]];
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[n + 1] = n + 1;
++n;
for (int i = 1; i <= n; i++) {
while (top && a[sta[top]] < a[i]) add(i, sta[top--]);
sta[++top] = i;
}
dfs(n);
for (int i = 1; i <= m; i++)
st.update(1, n, 1, dfn[i], dfn[i] + size[i] - 1, 1);
printf("%d", st.a[1]);
for (int i = m + 1; i < n; i++) {
st.update(1, n, 1, dfn[i], dfn[i] + size[i] - 1, 1);
st.update(1, n, 1, dfn[i - m], dfn[i - m] + size[i - m] - 1, -1);
printf(" %d", st.a[1]);
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, m1, len, top, a[1000005], size[1000005], dfn[1000005], nex[1000005],
wen[1000005], hea[1000005], sta[1000005];
struct segment_tree {
int a[4000005], lazy[4000005];
void pushdown(int k) {
int t = lazy[k];
a[k << 1] += t;
lazy[k << 1] += t;
a[(k << 1) | 1] += t;
lazy[(k << 1) | 1] += t;
lazy[k] = 0;
}
void update(int l, int r, int k, int x, int y, int z) {
if (l >= x && r <= y) {
a[k] += z;
lazy[k] += z;
return;
}
pushdown(k);
int mid = (l + r) >> 1;
if (x <= mid) update(l, mid, k << 1, x, y, z);
if (y > mid) update(mid + 1, r, (k << 1) | 1, x, y, z);
a[k] = max(a[k << 1], a[(k << 1) | 1]);
}
int find(int l, int r, int k, int x, int y) {
if (l >= x && r <= y) return a[k];
pushdown(k);
int mid = (l + r) >> 1, ans = 0;
if (x <= mid) ans = find(l, mid, k << 1, x, y);
if (y > mid) ans = max(ans, find(mid + 1, r, (k << 1) | 1, x, y));
return ans;
}
} st;
void add(int x, int y) {
++len;
nex[len] = hea[x];
wen[len] = y;
hea[x] = len;
}
void dfs(int x) {
dfn[x] = ++m1;
size[x] = 1;
for (int i = hea[x]; i; i = nex[i]) {
dfs(wen[i]);
size[x] += size[wen[i]];
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[n + 1] = n + 1;
++n;
for (int i = 1; i <= n; i++) {
while (top && a[sta[top]] < a[i]) add(i, sta[top--]);
sta[++top] = i;
}
dfs(n);
for (int i = 1; i <= m; i++)
st.update(1, n, 1, dfn[i], dfn[i] + size[i] - 1, 1);
printf("%d", st.a[1]);
for (int i = m + 1; i < n; i++) {
st.update(1, n, 1, dfn[i], dfn[i] + size[i] - 1, 1);
st.update(1, n, 1, dfn[i - m], dfn[i - m] + size[i - m] - 1, -1);
printf(" %d", st.a[1]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
template <typename T>
inline void read(T &x) {
x = 0;
bool sign = false;
char ch = getchar();
while (ch < '0' || '9' < ch) {
sign |= ch == '-';
ch = getchar();
}
while ('0' <= ch && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x = sign ? -x : x;
}
template <typename T>
inline void print(T x) {
static char outp[128];
int tot = 0;
if (!x) {
putchar('0');
return;
} else if (x < 0) {
x = -x;
putchar('-');
}
while (x) {
outp[tot++] = x % 10, x /= 10;
}
while (tot) {
putchar(outp[--tot] + 48);
}
}
const int MAXN = 1e6 + 10;
struct Segtree {
struct Node {
int add, val;
int stdl, stdr;
Node() : add(0), val(0) {}
} tree[((1 << 21) + 16)];
inline void buildtree(int root, int l, int r) {
tree[root].stdl = l;
tree[root].stdr = r;
if (l == r) return;
buildtree((root << 1), l, ((tree[root].stdl + tree[root].stdr) >> 1));
buildtree((root << 1 | 1), ((tree[root].stdl + tree[root].stdr) >> 1) + 1,
r);
}
inline void buildtree(int r) { buildtree(1, 0, r + 1); }
inline void pushdown(int root) {
tree[(root << 1)].add += tree[root].add;
tree[(root << 1)].val += tree[root].add;
tree[(root << 1 | 1)].add += tree[root].add;
tree[(root << 1 | 1)].val += tree[root].add;
tree[root].add = 0;
}
inline void update(int root) {
tree[root].val = max(tree[(root << 1)].val, tree[(root << 1 | 1)].val);
}
inline void add(int root, int l, int r, int p) {
if (l <= tree[root].stdl && tree[root].stdr <= r) {
tree[root].add += p;
tree[root].val += p;
return;
}
pushdown(root);
if (l <= ((tree[root].stdl + tree[root].stdr) >> 1))
add((root << 1), l, r, p);
if (r >= ((tree[root].stdl + tree[root].stdr) >> 1) + 1)
add((root << 1 | 1), l, r, p);
update(root);
}
inline void add(int l, int r, int p) { add(1, l, r, p); }
} Tree;
int a[MAXN], n, k;
vector<int> edge[MAXN];
int bg[MAXN], ed[MAXN];
inline void dfs(int u) {
static int T = 0;
bg[u] = T++;
for (auto v : edge[u]) {
dfs(v);
}
ed[u] = T - 1;
}
int main() {
read(n), read(k);
for (register int i = 0; i <= n - 1; ++i) {
read(a[i]);
}
stack<int> sta;
for (register int i = n - 1; i >= 0; --i) {
while (!sta.empty() && a[sta.top()] <= a[i]) sta.pop();
edge[sta.empty() ? n : sta.top()].push_back(i), sta.push(i);
}
dfs(n);
Tree.buildtree(n);
for (register int i = 0; i <= k - 2; ++i) {
Tree.add(bg[i], ed[i], 1);
}
for (register int i = 0; i <= n - k; ++i) {
Tree.add(bg[i + k - 1], ed[i + k - 1], 1);
print(Tree.tree[1].val), putchar(' ');
Tree.add(bg[i], ed[i], -1);
}
putchar('\n');
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
template <typename T>
inline void read(T &x) {
x = 0;
bool sign = false;
char ch = getchar();
while (ch < '0' || '9' < ch) {
sign |= ch == '-';
ch = getchar();
}
while ('0' <= ch && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x = sign ? -x : x;
}
template <typename T>
inline void print(T x) {
static char outp[128];
int tot = 0;
if (!x) {
putchar('0');
return;
} else if (x < 0) {
x = -x;
putchar('-');
}
while (x) {
outp[tot++] = x % 10, x /= 10;
}
while (tot) {
putchar(outp[--tot] + 48);
}
}
const int MAXN = 1e6 + 10;
struct Segtree {
struct Node {
int add, val;
int stdl, stdr;
Node() : add(0), val(0) {}
} tree[((1 << 21) + 16)];
inline void buildtree(int root, int l, int r) {
tree[root].stdl = l;
tree[root].stdr = r;
if (l == r) return;
buildtree((root << 1), l, ((tree[root].stdl + tree[root].stdr) >> 1));
buildtree((root << 1 | 1), ((tree[root].stdl + tree[root].stdr) >> 1) + 1,
r);
}
inline void buildtree(int r) { buildtree(1, 0, r + 1); }
inline void pushdown(int root) {
tree[(root << 1)].add += tree[root].add;
tree[(root << 1)].val += tree[root].add;
tree[(root << 1 | 1)].add += tree[root].add;
tree[(root << 1 | 1)].val += tree[root].add;
tree[root].add = 0;
}
inline void update(int root) {
tree[root].val = max(tree[(root << 1)].val, tree[(root << 1 | 1)].val);
}
inline void add(int root, int l, int r, int p) {
if (l <= tree[root].stdl && tree[root].stdr <= r) {
tree[root].add += p;
tree[root].val += p;
return;
}
pushdown(root);
if (l <= ((tree[root].stdl + tree[root].stdr) >> 1))
add((root << 1), l, r, p);
if (r >= ((tree[root].stdl + tree[root].stdr) >> 1) + 1)
add((root << 1 | 1), l, r, p);
update(root);
}
inline void add(int l, int r, int p) { add(1, l, r, p); }
} Tree;
int a[MAXN], n, k;
vector<int> edge[MAXN];
int bg[MAXN], ed[MAXN];
inline void dfs(int u) {
static int T = 0;
bg[u] = T++;
for (auto v : edge[u]) {
dfs(v);
}
ed[u] = T - 1;
}
int main() {
read(n), read(k);
for (register int i = 0; i <= n - 1; ++i) {
read(a[i]);
}
stack<int> sta;
for (register int i = n - 1; i >= 0; --i) {
while (!sta.empty() && a[sta.top()] <= a[i]) sta.pop();
edge[sta.empty() ? n : sta.top()].push_back(i), sta.push(i);
}
dfs(n);
Tree.buildtree(n);
for (register int i = 0; i <= k - 2; ++i) {
Tree.add(bg[i], ed[i], 1);
}
for (register int i = 0; i <= n - k; ++i) {
Tree.add(bg[i + k - 1], ed[i + k - 1], 1);
print(Tree.tree[1].val), putchar(' ');
Tree.add(bg[i], ed[i], -1);
}
putchar('\n');
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> v[1000005];
int ans[1000005];
struct node {
node *l, *r;
int a, b;
long long Max;
long long tag;
node(int _a, int _b) : l(NULL), r(NULL), a(_a), b(_b), Max(0), tag(0) {}
} * root;
long long Max(node *n) { return n->Max + n->tag; }
void push(node *n) {
n->l->tag += n->tag;
n->r->tag += n->tag;
n->tag = 0;
}
void pull(node *n) { n->Max = max(Max(n->l), Max(n->r)); }
void build(node *n) {
if (n->a == n->b) return;
int mid = (n->a + n->b) / 2;
n->l = new node(n->a, mid);
n->r = new node(mid + 1, n->b);
build(n->l);
build(n->r);
}
void add(node *n, int l, int r, int k) {
if (n->a >= l && n->b <= r) {
n->tag += k;
return;
}
if (n->b < l || n->a > r) return;
push(n);
add(n->l, l, r, k);
add(n->r, l, r, k);
pull(n);
}
int in[1000005];
int out[1000005];
int t;
int a[1000005];
void dfs(int first) {
in[first] = ++t;
for (auto it : v[first]) {
dfs(it);
}
out[first] = t;
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[n + 1] = 1e9;
vector<pair<int, int> > mystk;
mystk.push_back(make_pair(1e9 + 1, 0));
for (int i = 1; i <= n + 1; i++) {
while (mystk.back().first < a[i]) {
v[i].push_back(mystk.back().second);
mystk.pop_back();
}
mystk.push_back(make_pair(a[i], i));
}
dfs(n + 1);
root = new node(1, n + 1);
build(root);
for (int i = 1; i <= k; i++) {
add(root, in[i], out[i], 1);
}
for (int i = k + 1; i <= n + 1; i++) {
printf("%lld ", Max(root));
add(root, in[i - k], in[i - k], -1e9);
add(root, in[i], out[i], 1);
}
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> v[1000005];
int ans[1000005];
struct node {
node *l, *r;
int a, b;
long long Max;
long long tag;
node(int _a, int _b) : l(NULL), r(NULL), a(_a), b(_b), Max(0), tag(0) {}
} * root;
long long Max(node *n) { return n->Max + n->tag; }
void push(node *n) {
n->l->tag += n->tag;
n->r->tag += n->tag;
n->tag = 0;
}
void pull(node *n) { n->Max = max(Max(n->l), Max(n->r)); }
void build(node *n) {
if (n->a == n->b) return;
int mid = (n->a + n->b) / 2;
n->l = new node(n->a, mid);
n->r = new node(mid + 1, n->b);
build(n->l);
build(n->r);
}
void add(node *n, int l, int r, int k) {
if (n->a >= l && n->b <= r) {
n->tag += k;
return;
}
if (n->b < l || n->a > r) return;
push(n);
add(n->l, l, r, k);
add(n->r, l, r, k);
pull(n);
}
int in[1000005];
int out[1000005];
int t;
int a[1000005];
void dfs(int first) {
in[first] = ++t;
for (auto it : v[first]) {
dfs(it);
}
out[first] = t;
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[n + 1] = 1e9;
vector<pair<int, int> > mystk;
mystk.push_back(make_pair(1e9 + 1, 0));
for (int i = 1; i <= n + 1; i++) {
while (mystk.back().first < a[i]) {
v[i].push_back(mystk.back().second);
mystk.pop_back();
}
mystk.push_back(make_pair(a[i], i));
}
dfs(n + 1);
root = new node(1, n + 1);
build(root);
for (int i = 1; i <= k; i++) {
add(root, in[i], out[i], 1);
}
for (int i = k + 1; i <= n + 1; i++) {
printf("%lld ", Max(root));
add(root, in[i - k], in[i - k], -1e9);
add(root, in[i], out[i], 1);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[1000005], st[4 * 1000005], lazy[4 * 1000005] = {};
void update(int start, int end, int l, int r, int dif, int node) {
if (l > r) return;
if (lazy[node] != 0) {
st[node] += lazy[node];
if (start != end) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (start > r or end < l)
return;
else if (start >= l and end <= r) {
st[node] += dif;
if (start != end) {
lazy[2 * node + 1] += dif;
lazy[2 * node + 2] += dif;
}
return;
}
int mid = (start + end) / 2;
update(start, mid, l, r, dif, 2 * node + 1);
update(mid + 1, end, l, r, dif, 2 * node + 2);
st[node] = max(st[2 * node + 1], st[2 * node + 2]);
}
int queryMin(int start, int end, int l, int r, int node) {
if (lazy[node] != 0) {
st[node] += lazy[node];
if (start != end) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (start > r or end < l)
return 1;
else if (start >= l and end <= r)
return st[node];
else {
int mid = (start + end) / 2, left = 2 * node + 1, right = left + 1;
return max(queryMin(start, mid, l, r, left),
queryMin(mid + 1, end, l, r, right));
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
fill(st, st + 4 * 1000005, 1);
vector<int> root;
for (int i = 0; i < n; i++) {
while (root.size() && a[root.back()] < a[i]) {
root.pop_back();
}
int l;
if (root.size()) {
l = root.back() + 1;
} else {
l = 0;
}
root.push_back(i);
update(0, n - 1, l, i - 1, 1, 0);
if (i >= k - 1) {
cout << queryMin(0, n - 1, i - k + 1, i, 0) << " ";
}
}
cout << endl;
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[1000005], st[4 * 1000005], lazy[4 * 1000005] = {};
void update(int start, int end, int l, int r, int dif, int node) {
if (l > r) return;
if (lazy[node] != 0) {
st[node] += lazy[node];
if (start != end) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (start > r or end < l)
return;
else if (start >= l and end <= r) {
st[node] += dif;
if (start != end) {
lazy[2 * node + 1] += dif;
lazy[2 * node + 2] += dif;
}
return;
}
int mid = (start + end) / 2;
update(start, mid, l, r, dif, 2 * node + 1);
update(mid + 1, end, l, r, dif, 2 * node + 2);
st[node] = max(st[2 * node + 1], st[2 * node + 2]);
}
int queryMin(int start, int end, int l, int r, int node) {
if (lazy[node] != 0) {
st[node] += lazy[node];
if (start != end) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (start > r or end < l)
return 1;
else if (start >= l and end <= r)
return st[node];
else {
int mid = (start + end) / 2, left = 2 * node + 1, right = left + 1;
return max(queryMin(start, mid, l, r, left),
queryMin(mid + 1, end, l, r, right));
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
fill(st, st + 4 * 1000005, 1);
vector<int> root;
for (int i = 0; i < n; i++) {
while (root.size() && a[root.back()] < a[i]) {
root.pop_back();
}
int l;
if (root.size()) {
l = root.back() + 1;
} else {
l = 0;
}
root.push_back(i);
update(0, n - 1, l, i - 1, 1, 0);
if (i >= k - 1) {
cout << queryMin(0, n - 1, i - k + 1, i, 0) << " ";
}
}
cout << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int dp[2097292 >> 1];
int n;
int arr[2097292 >> 1];
int rt[2097292 >> 1];
vector<int> g[2097292 >> 1];
int k = 1;
int dfsord[2097292 >> 1];
int subsz[2097292 >> 1];
struct segt {
int lz;
int value;
};
segt tree[2097292];
void dfs(int v) {
subsz[v] = 1;
dfsord[v] = k++;
for (auto nextv : g[v]) {
dfs(nextv);
subsz[v] += subsz[nextv];
}
}
void pushdown(int l, int r, int index) {
if (tree[index].lz) {
if (l != r) {
tree[index * 2].value += tree[index].lz;
tree[index * 2 + 1].value += tree[index].lz;
tree[index * 2].lz += tree[index].lz;
tree[index * 2 + 1].lz += tree[index].lz;
}
tree[index].lz = 0;
}
}
void update(int start, int end, int l, int r, int index, int val) {
if (start > end || l > r) return;
pushdown(l, r, index);
if (start > r || l > end) return;
if (start <= l && r <= end) {
tree[index].value += val;
tree[index].lz += val;
return;
}
int mid = l + (r - l) / 2;
update(start, end, l, mid, index * 2, val);
update(start, end, mid + 1, r, index * 2 + 1, val);
tree[index].value = max(tree[index * 2].value, tree[index * 2 + 1].value);
}
void rmv(int pt, int l, int r, int index) {
if (l > r) return;
pushdown(l, r, index);
if (l == r && r == pt) {
tree[index].value = -4194304;
return;
}
int mid = l + (r - l) / 2;
if (pt <= mid)
rmv(pt, l, mid, index * 2);
else {
rmv(pt, mid + 1, r, index * 2 + 1);
}
tree[index].value = max(tree[index * 2].value, tree[index * 2 + 1].value);
}
int query() { return tree[1].value; }
int main() {
int k;
scanf("%d%d", &n, &k);
stack<int> st;
int i;
for (i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
for (i = n; i >= 1; i--) {
if (st.empty()) {
rt[i] = -1;
dp[i] = 1;
} else {
while (!st.empty() && arr[st.top()] <= arr[i]) {
st.pop();
}
if (st.empty()) {
rt[i] = -1;
dp[i] = 1;
} else {
rt[i] = st.top();
dp[i] = dp[st.top()] + 1;
}
}
st.push(i);
}
for (i = 1; i <= n; i++) {
if (rt[i] != -1) {
g[rt[i]].push_back(i);
}
}
for (i = n; i >= 1; i--) {
if (!dfsord[i]) {
dfs(i);
}
}
vector<int> ans;
for (i = n; i >= 1; i--) {
update(dfsord[i], dfsord[i], 1, n, 1, dp[i]);
if (i + k - 1 <= n) {
ans.push_back(query());
update(dfsord[i + k - 1], dfsord[i + k - 1] + subsz[i + k - 1] - 1, 1, n,
1, -1);
rmv(dfsord[i + k - 1], 1, n, 1);
}
}
for (i = (int)ans.size() - 1; i >= 0; i--) {
printf("%d ", ans[i]);
}
printf("\n");
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int dp[2097292 >> 1];
int n;
int arr[2097292 >> 1];
int rt[2097292 >> 1];
vector<int> g[2097292 >> 1];
int k = 1;
int dfsord[2097292 >> 1];
int subsz[2097292 >> 1];
struct segt {
int lz;
int value;
};
segt tree[2097292];
void dfs(int v) {
subsz[v] = 1;
dfsord[v] = k++;
for (auto nextv : g[v]) {
dfs(nextv);
subsz[v] += subsz[nextv];
}
}
void pushdown(int l, int r, int index) {
if (tree[index].lz) {
if (l != r) {
tree[index * 2].value += tree[index].lz;
tree[index * 2 + 1].value += tree[index].lz;
tree[index * 2].lz += tree[index].lz;
tree[index * 2 + 1].lz += tree[index].lz;
}
tree[index].lz = 0;
}
}
void update(int start, int end, int l, int r, int index, int val) {
if (start > end || l > r) return;
pushdown(l, r, index);
if (start > r || l > end) return;
if (start <= l && r <= end) {
tree[index].value += val;
tree[index].lz += val;
return;
}
int mid = l + (r - l) / 2;
update(start, end, l, mid, index * 2, val);
update(start, end, mid + 1, r, index * 2 + 1, val);
tree[index].value = max(tree[index * 2].value, tree[index * 2 + 1].value);
}
void rmv(int pt, int l, int r, int index) {
if (l > r) return;
pushdown(l, r, index);
if (l == r && r == pt) {
tree[index].value = -4194304;
return;
}
int mid = l + (r - l) / 2;
if (pt <= mid)
rmv(pt, l, mid, index * 2);
else {
rmv(pt, mid + 1, r, index * 2 + 1);
}
tree[index].value = max(tree[index * 2].value, tree[index * 2 + 1].value);
}
int query() { return tree[1].value; }
int main() {
int k;
scanf("%d%d", &n, &k);
stack<int> st;
int i;
for (i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
for (i = n; i >= 1; i--) {
if (st.empty()) {
rt[i] = -1;
dp[i] = 1;
} else {
while (!st.empty() && arr[st.top()] <= arr[i]) {
st.pop();
}
if (st.empty()) {
rt[i] = -1;
dp[i] = 1;
} else {
rt[i] = st.top();
dp[i] = dp[st.top()] + 1;
}
}
st.push(i);
}
for (i = 1; i <= n; i++) {
if (rt[i] != -1) {
g[rt[i]].push_back(i);
}
}
for (i = n; i >= 1; i--) {
if (!dfsord[i]) {
dfs(i);
}
}
vector<int> ans;
for (i = n; i >= 1; i--) {
update(dfsord[i], dfsord[i], 1, n, 1, dp[i]);
if (i + k - 1 <= n) {
ans.push_back(query());
update(dfsord[i + k - 1], dfsord[i + k - 1] + subsz[i + k - 1] - 1, 1, n,
1, -1);
rmv(dfsord[i + k - 1], 1, n, 1);
}
}
for (i = (int)ans.size() - 1; i >= 0; i--) {
printf("%d ", ans[i]);
}
printf("\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
int a[N], c[N];
struct o_O {
int max, lazy;
} f[N * 4];
void pushdown(int x) {
if (f[x].max == -1) return;
f[x * 2].max += f[x].lazy;
f[x * 2 + 1].max += f[x].lazy;
f[x * 2].lazy += f[x].lazy;
f[x * 2 + 1].lazy += f[x].lazy;
f[x].lazy = 0;
}
int query(int x, int l, int r, int left, int right) {
if (l > right || r < left) return 0;
if (left <= l && right >= r) return f[x].max;
pushdown(x);
return max(query(x * 2, l, (l + r) / 2, left, right),
query(x * 2 + 1, (l + r) / 2 + 1, r, left, right));
}
void update(int x, int l, int r, int left, int right) {
if (l > right || r < left) return;
if (left <= l && right >= r) {
if (f[x].max != -1) {
f[x].max += 1;
f[x].lazy += 1;
}
return;
}
pushdown(x);
update(x * 2, l, (l + r) / 2, left, right);
update(x * 2 + 1, (l + r) / 2 + 1, r, left, right);
f[x].max = max(f[x * 2].max, f[x * 2 + 1].max);
}
void update0(int x, int l, int r, int index) {
if (l > index || r < index) return;
if (l == r) {
f[x].max = -1;
return;
}
pushdown(x);
update0(x * 2, l, (l + r) / 2, index);
update0(x * 2 + 1, (l + r) / 2 + 1, r, index);
f[x].max = max(f[x * 2].max, f[x * 2 + 1].max);
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int l = 0, r = 0;
c[0] = 0;
for (int i = 1; i <= n; i++) {
while (r > l && a[i] > a[c[r]]) r--;
update(1, 1, n, c[r] + 1, i);
c[++r] = i;
if (i >= k) {
printf("%d ", query(1, 1, n, i - k + 1, i));
update0(1, 1, n, i - k + 1);
}
}
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
int a[N], c[N];
struct o_O {
int max, lazy;
} f[N * 4];
void pushdown(int x) {
if (f[x].max == -1) return;
f[x * 2].max += f[x].lazy;
f[x * 2 + 1].max += f[x].lazy;
f[x * 2].lazy += f[x].lazy;
f[x * 2 + 1].lazy += f[x].lazy;
f[x].lazy = 0;
}
int query(int x, int l, int r, int left, int right) {
if (l > right || r < left) return 0;
if (left <= l && right >= r) return f[x].max;
pushdown(x);
return max(query(x * 2, l, (l + r) / 2, left, right),
query(x * 2 + 1, (l + r) / 2 + 1, r, left, right));
}
void update(int x, int l, int r, int left, int right) {
if (l > right || r < left) return;
if (left <= l && right >= r) {
if (f[x].max != -1) {
f[x].max += 1;
f[x].lazy += 1;
}
return;
}
pushdown(x);
update(x * 2, l, (l + r) / 2, left, right);
update(x * 2 + 1, (l + r) / 2 + 1, r, left, right);
f[x].max = max(f[x * 2].max, f[x * 2 + 1].max);
}
void update0(int x, int l, int r, int index) {
if (l > index || r < index) return;
if (l == r) {
f[x].max = -1;
return;
}
pushdown(x);
update0(x * 2, l, (l + r) / 2, index);
update0(x * 2 + 1, (l + r) / 2 + 1, r, index);
f[x].max = max(f[x * 2].max, f[x * 2 + 1].max);
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int l = 0, r = 0;
c[0] = 0;
for (int i = 1; i <= n; i++) {
while (r > l && a[i] > a[c[r]]) r--;
update(1, 1, n, c[r] + 1, i);
c[++r] = i;
if (i >= k) {
printf("%d ", query(1, 1, n, i - k + 1, i));
update0(1, 1, n, i - k + 1);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
const long long MAXN = 2e6 + 55;
using namespace std;
long long readInt() {
bool minus1 = false;
long long result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-')
minus1 = true;
else
result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = result * 10 + (ch - '0');
}
if (minus1)
return -result;
else
return result;
}
int timer = 0;
int a[MAXN];
int tin[MAXN];
int tout[MAXN];
vector<int> g[MAXN];
int used[MAXN];
void dfs(int v) {
timer++;
used[v] = 1;
tin[v] = timer;
for (int i : g[v]) {
if (!used[i]) dfs(i);
}
tout[v] = timer;
}
long long t[MAXN * 4], to[MAXN * 4];
void Push(long long v) {
if (to[v]) {
t[v * 2] += to[v];
t[v * 2 + 1] += to[v];
to[v * 2] += to[v];
to[v * 2 + 1] += to[v];
to[v] = 0;
}
}
void Upd(long long v, long long tl, long long tr, long long l, long long r,
long long x) {
Push(v);
if (l > r) return;
if (l == tl && r == tr) {
t[v] += x;
to[v] += x;
} else {
long long tm = (tl + tr) / 2;
Upd(v * 2, tl, tm, l, min(r, tm), x);
Upd(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, x);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
}
long long Get_Max(long long v, long long tl, long long tr, long long l,
long long r) {
Push(v);
if (l > r) return 0;
if (l == tl && r == tr) {
return t[v];
} else {
long long tm = (tl + tr) / 2;
return max(Get_Max(v * 2, tl, tm, l, min(r, tm)),
Get_Max(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
}
int main() {
int n = readInt(), k = readInt();
for (int i = 1; i <= n; i++) {
a[i] = readInt();
}
vector<int> st;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.back()] <= a[i]) {
st.pop_back();
}
int nxt = st.empty() ? (n + 1) : st.back();
g[nxt].push_back(i);
st.push_back(i);
}
dfs(n + 1);
for (int i = 1; i < k; i++) {
Upd(1, 1, n + 1, tin[i], tout[i], 1);
}
for (int i = 0; i < n - k + 1; i++) {
Upd(1, 1, n + 1, tin[i + k], tout[i + k], 1);
cout << t[1] << ' ';
Upd(1, 1, n + 1, tin[i + 1], tout[i + 1], -1);
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
const long long MAXN = 2e6 + 55;
using namespace std;
long long readInt() {
bool minus1 = false;
long long result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-')
minus1 = true;
else
result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = result * 10 + (ch - '0');
}
if (minus1)
return -result;
else
return result;
}
int timer = 0;
int a[MAXN];
int tin[MAXN];
int tout[MAXN];
vector<int> g[MAXN];
int used[MAXN];
void dfs(int v) {
timer++;
used[v] = 1;
tin[v] = timer;
for (int i : g[v]) {
if (!used[i]) dfs(i);
}
tout[v] = timer;
}
long long t[MAXN * 4], to[MAXN * 4];
void Push(long long v) {
if (to[v]) {
t[v * 2] += to[v];
t[v * 2 + 1] += to[v];
to[v * 2] += to[v];
to[v * 2 + 1] += to[v];
to[v] = 0;
}
}
void Upd(long long v, long long tl, long long tr, long long l, long long r,
long long x) {
Push(v);
if (l > r) return;
if (l == tl && r == tr) {
t[v] += x;
to[v] += x;
} else {
long long tm = (tl + tr) / 2;
Upd(v * 2, tl, tm, l, min(r, tm), x);
Upd(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, x);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
}
long long Get_Max(long long v, long long tl, long long tr, long long l,
long long r) {
Push(v);
if (l > r) return 0;
if (l == tl && r == tr) {
return t[v];
} else {
long long tm = (tl + tr) / 2;
return max(Get_Max(v * 2, tl, tm, l, min(r, tm)),
Get_Max(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
}
int main() {
int n = readInt(), k = readInt();
for (int i = 1; i <= n; i++) {
a[i] = readInt();
}
vector<int> st;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.back()] <= a[i]) {
st.pop_back();
}
int nxt = st.empty() ? (n + 1) : st.back();
g[nxt].push_back(i);
st.push_back(i);
}
dfs(n + 1);
for (int i = 1; i < k; i++) {
Upd(1, 1, n + 1, tin[i], tout[i], 1);
}
for (int i = 0; i < n - k + 1; i++) {
Upd(1, 1, n + 1, tin[i + k], tout[i + k], 1);
cout << t[1] << ' ';
Upd(1, 1, n + 1, tin[i + 1], tout[i + 1], -1);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
const int M = 1e6;
const long long mod = 998244353;
const long long MOD = 998244353;
const long double eps = 0.000000001;
const int P = 1336;
const int inf = 1e9 + 7;
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<int> no, t, add;
int tin[N], tout[N], tim = -1;
int a[N];
vector<int> g[N];
int n, k;
void push(int v) {
t[v * 2] += add[v];
t[v * 2 + 1] += add[v];
add[v * 2] += add[v];
add[v * 2 + 1] += add[v];
add[v] = 0;
}
void upd(int l, int r, int v, int cl, int cr, int zn) {
if (cr < l || r < cl) return;
if (l <= cl && cr <= r) {
t[v] += zn;
add[v] += zn;
return;
}
if (cl != cr) push(v);
int mid = (cl + cr) / 2;
upd(l, r, v * 2, cl, mid, zn);
upd(l, r, v * 2 + 1, mid + 1, cr, zn);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
int maxx(int l, int r, int v, int cl, int cr) {
if (cr < l || r < cl) return -inf;
if (l <= cl && cr <= r) return t[v];
if (cl != cr) push(v);
int mid = (cl + cr) / 2;
return max(maxx(l, r, v * 2, cl, mid), maxx(l, r, v * 2 + 1, mid + 1, cr));
}
void update(int v, int zn) {
no[v] = zn;
v /= 2;
while (v) {
no[v] = min(no[v * 2], no[v * 2 + 1]);
v /= 2;
}
}
int minn(int l, int r, int v, int cl, int cr) {
if (cr < l || r < cl) return n;
if (l <= cl && cr <= r) return no[v];
int mid = (cl + cr) / 2;
return min(minn(l, r, v * 2, cl, mid), minn(l, r, v * 2 + 1, mid + 1, cr));
}
void DFS(int v) {
tim++;
tin[v] = tim;
for (auto to : g[v]) {
DFS(to);
}
tim++;
tout[v] = tim;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(time(0));
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long pr = 1;
while (pr < n) pr *= 2;
no.resize(2 * pr);
for (int i = 0; i < pr + pr; i++) {
no[i] = n;
}
for (int i = n - 1; i >= 0; i--) {
int x = minn(a[i], n - 1, 1, 0, pr - 1);
g[x].push_back(i);
update(pr + a[i] - 1, i);
}
DFS(n);
pr *= 2;
t.resize(2 * pr);
add.resize(2 * pr);
for (int i = 0; i < k; i++) {
upd(tin[i], tout[i] - 1, 1, 0, pr - 1, 1);
}
cout << t[1] << " ";
for (int i = k; i < n; i++) {
upd(tin[i - k], tout[i - k] - 1, 1, 0, pr - 1, -1);
upd(tin[i], tout[i] - 1, 1, 0, pr - 1, 1);
cout << t[1] << " ";
}
}
| ### Prompt
Develop a solution in CPP to the problem described below:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
const int M = 1e6;
const long long mod = 998244353;
const long long MOD = 998244353;
const long double eps = 0.000000001;
const int P = 1336;
const int inf = 1e9 + 7;
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<int> no, t, add;
int tin[N], tout[N], tim = -1;
int a[N];
vector<int> g[N];
int n, k;
void push(int v) {
t[v * 2] += add[v];
t[v * 2 + 1] += add[v];
add[v * 2] += add[v];
add[v * 2 + 1] += add[v];
add[v] = 0;
}
void upd(int l, int r, int v, int cl, int cr, int zn) {
if (cr < l || r < cl) return;
if (l <= cl && cr <= r) {
t[v] += zn;
add[v] += zn;
return;
}
if (cl != cr) push(v);
int mid = (cl + cr) / 2;
upd(l, r, v * 2, cl, mid, zn);
upd(l, r, v * 2 + 1, mid + 1, cr, zn);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
int maxx(int l, int r, int v, int cl, int cr) {
if (cr < l || r < cl) return -inf;
if (l <= cl && cr <= r) return t[v];
if (cl != cr) push(v);
int mid = (cl + cr) / 2;
return max(maxx(l, r, v * 2, cl, mid), maxx(l, r, v * 2 + 1, mid + 1, cr));
}
void update(int v, int zn) {
no[v] = zn;
v /= 2;
while (v) {
no[v] = min(no[v * 2], no[v * 2 + 1]);
v /= 2;
}
}
int minn(int l, int r, int v, int cl, int cr) {
if (cr < l || r < cl) return n;
if (l <= cl && cr <= r) return no[v];
int mid = (cl + cr) / 2;
return min(minn(l, r, v * 2, cl, mid), minn(l, r, v * 2 + 1, mid + 1, cr));
}
void DFS(int v) {
tim++;
tin[v] = tim;
for (auto to : g[v]) {
DFS(to);
}
tim++;
tout[v] = tim;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(time(0));
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long pr = 1;
while (pr < n) pr *= 2;
no.resize(2 * pr);
for (int i = 0; i < pr + pr; i++) {
no[i] = n;
}
for (int i = n - 1; i >= 0; i--) {
int x = minn(a[i], n - 1, 1, 0, pr - 1);
g[x].push_back(i);
update(pr + a[i] - 1, i);
}
DFS(n);
pr *= 2;
t.resize(2 * pr);
add.resize(2 * pr);
for (int i = 0; i < k; i++) {
upd(tin[i], tout[i] - 1, 1, 0, pr - 1, 1);
}
cout << t[1] << " ";
for (int i = k; i < n; i++) {
upd(tin[i - k], tout[i - k] - 1, 1, 0, pr - 1, -1);
upd(tin[i], tout[i] - 1, 1, 0, pr - 1, 1);
cout << t[1] << " ";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6, LG = 22;
int n, k;
int arr[N], nxt[N];
vector<int> adj[N];
int st[N], en[N], eul[N], dfstime;
int btree[4 * N], lazy[4 * N];
void dfs(int x) {
st[x] = dfstime;
eul[dfstime++] = x;
for (int i : adj[x]) dfs(i);
en[x] = dfstime - 1;
}
void prob(int node, int l, int r) {
btree[node] += lazy[node];
if (l != r) {
lazy[node << 1] += lazy[node];
lazy[(node << 1) | 1] += lazy[node];
}
lazy[node] = 0;
}
void update(int node, int l, int r, int ql, int qr, int val) {
if (lazy[node]) prob(node, l, r);
if (r < ql || qr < l) return;
if (ql <= l && r <= qr) {
lazy[node] += val;
prob(node, l, r);
return;
}
int mid = (l + r) >> 1;
update(node << 1, l, mid, ql, qr, val);
update((node << 1) | 1, mid + 1, r, ql, qr, val);
btree[node] = max(btree[node << 1], btree[(node << 1) | 1]);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
stack<int> stk;
for (int i = n - 1; i >= 0; i--) {
while (stk.size() && arr[stk.top()] <= arr[i]) stk.pop();
if (stk.size())
nxt[i] = stk.top();
else
nxt[i] = n;
adj[nxt[i]].push_back(i);
stk.push(i);
}
dfs(n);
for (int i = 0; i < k; i++) {
update(1, 0, n, st[i], en[i], 1);
}
printf("%d", btree[1]);
for (int i = k; i < n; i++) {
update(1, 0, n, st[i - k], en[i - k], -1);
update(1, 0, n, st[i], en[i], 1);
prob(1, 0, n);
printf(" %d", btree[1]);
}
puts("");
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6, LG = 22;
int n, k;
int arr[N], nxt[N];
vector<int> adj[N];
int st[N], en[N], eul[N], dfstime;
int btree[4 * N], lazy[4 * N];
void dfs(int x) {
st[x] = dfstime;
eul[dfstime++] = x;
for (int i : adj[x]) dfs(i);
en[x] = dfstime - 1;
}
void prob(int node, int l, int r) {
btree[node] += lazy[node];
if (l != r) {
lazy[node << 1] += lazy[node];
lazy[(node << 1) | 1] += lazy[node];
}
lazy[node] = 0;
}
void update(int node, int l, int r, int ql, int qr, int val) {
if (lazy[node]) prob(node, l, r);
if (r < ql || qr < l) return;
if (ql <= l && r <= qr) {
lazy[node] += val;
prob(node, l, r);
return;
}
int mid = (l + r) >> 1;
update(node << 1, l, mid, ql, qr, val);
update((node << 1) | 1, mid + 1, r, ql, qr, val);
btree[node] = max(btree[node << 1], btree[(node << 1) | 1]);
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
stack<int> stk;
for (int i = n - 1; i >= 0; i--) {
while (stk.size() && arr[stk.top()] <= arr[i]) stk.pop();
if (stk.size())
nxt[i] = stk.top();
else
nxt[i] = n;
adj[nxt[i]].push_back(i);
stk.push(i);
}
dfs(n);
for (int i = 0; i < k; i++) {
update(1, 0, n, st[i], en[i], 1);
}
printf("%d", btree[1]);
for (int i = k; i < n; i++) {
update(1, 0, n, st[i - k], en[i - k], -1);
update(1, 0, n, st[i], en[i], 1);
prob(1, 0, n);
printf(" %d", btree[1]);
}
puts("");
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
const int N = 1e6 + 10;
int nxt[N];
pair<int, int> seg[N * 4];
int mp[N];
void upd(int nod, int l, int r, int id, int val) {
if (r - l == 1) {
seg[nod] = {mp[l], val};
return;
}
int mid = (r + l) / 2;
if (mid > id)
upd(nod * 2, l, mid, id, val);
else
upd(nod * 2 + 1, mid, r, id, val);
seg[nod].second = max(seg[nod * 2].second, seg[nod * 2 + 1].second);
if (seg[nod].second == seg[nod * 2].second)
seg[nod] = seg[nod * 2];
else
seg[nod] = seg[nod * 2 + 1];
return;
}
pair<int, int> get(int nod, int l, int r, int L, int R) {
if (l >= R || L >= r) return {0, 0};
if (l >= L && r <= R) return seg[nod];
int mid = (r + l) / 2;
pair<int, int> a = get(nod * 2, l, mid, L, R);
pair<int, int> b = get(nod * 2 + 1, mid, r, L, R);
if (a.second > b.second) return a;
return b;
}
vector<int> g[N];
int st[N], fn[N], hi[N], ts = 1;
void dfs(int v, int p, int h) {
st[v] = ts;
hi[v] = h;
for (auto u : g[v]) {
if (u == p) continue;
ts++;
dfs(u, v, h + 1);
}
fn[v] = ts;
}
int a[N];
set<pair<int, int> > s;
int par[N];
int ans[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
a[n + 1] = n + 1;
vector<int> agh;
agh.push_back(n + 1);
for (int i = n; i; i--) {
while (a[agh.back()] <= a[i]) agh.pop_back();
nxt[i] = agh.back();
g[nxt[i]].push_back(i);
agh.push_back(i);
}
dfs(n + 1, n + 1, 1);
for (int i = 1; i <= n + 1; i++) mp[st[i]] = i;
for (int i = 1; i <= k; i++) {
upd(1, 1, N, st[i], hi[i]);
pair<int, int> t = get(1, 1, N, st[i], fn[i] + 1);
for (auto u : g[i]) {
s.erase({ans[u], u});
ans[u] = -1;
}
par[t.first] = i;
ans[i] = t.second - hi[i];
s.insert({ans[i], i});
}
for (int i = k + 1; i <= n + 1; i++) {
cout << s.rbegin()->first + 1 << " ";
int p = par[i - k];
s.erase({ans[p], p});
upd(1, 1, N, st[i - k], 0);
if (p > i - k && ans[p] != -1) {
pair<int, int> z = get(1, 1, N, st[p], fn[p] + 1);
par[z.first] = p;
ans[p] = z.second - hi[p];
s.insert({ans[p], p});
}
upd(1, 1, N, st[i], hi[i]);
pair<int, int> z = get(1, 1, N, st[i], fn[i] + 1);
for (auto u : g[i]) {
s.erase({ans[u], u});
ans[u] = -1;
}
par[z.first] = i;
ans[i] = z.second - hi[i];
s.insert({ans[i], i});
}
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 β€ p_1 < p_2 < ... < p_l β€ |c|, and for each i β [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i].
You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^6) β the length of array a and the length of subsegments.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β array a.
Output
Print n - k + 1 integers β the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β to subsegment a[2..k + 1], and so on.
Examples
Input
6 4
1 5 2 5 3 6
Output
2 2 3
Input
7 6
4 5 2 5 3 6 6
Output
3 3
Note
In the first example:
* [1, 5, 2, 5] β the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]).
* [5, 2, 5, 3] β the sequence is 2, 3 ([c_2, c_3] = [2, 5]).
* [2, 5, 3, 6] β the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]).
In the second example:
* [4, 5, 2, 5, 3, 6] β the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]).
* [5, 2, 5, 3, 6, 6] β the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]).
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
const int N = 1e6 + 10;
int nxt[N];
pair<int, int> seg[N * 4];
int mp[N];
void upd(int nod, int l, int r, int id, int val) {
if (r - l == 1) {
seg[nod] = {mp[l], val};
return;
}
int mid = (r + l) / 2;
if (mid > id)
upd(nod * 2, l, mid, id, val);
else
upd(nod * 2 + 1, mid, r, id, val);
seg[nod].second = max(seg[nod * 2].second, seg[nod * 2 + 1].second);
if (seg[nod].second == seg[nod * 2].second)
seg[nod] = seg[nod * 2];
else
seg[nod] = seg[nod * 2 + 1];
return;
}
pair<int, int> get(int nod, int l, int r, int L, int R) {
if (l >= R || L >= r) return {0, 0};
if (l >= L && r <= R) return seg[nod];
int mid = (r + l) / 2;
pair<int, int> a = get(nod * 2, l, mid, L, R);
pair<int, int> b = get(nod * 2 + 1, mid, r, L, R);
if (a.second > b.second) return a;
return b;
}
vector<int> g[N];
int st[N], fn[N], hi[N], ts = 1;
void dfs(int v, int p, int h) {
st[v] = ts;
hi[v] = h;
for (auto u : g[v]) {
if (u == p) continue;
ts++;
dfs(u, v, h + 1);
}
fn[v] = ts;
}
int a[N];
set<pair<int, int> > s;
int par[N];
int ans[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
a[n + 1] = n + 1;
vector<int> agh;
agh.push_back(n + 1);
for (int i = n; i; i--) {
while (a[agh.back()] <= a[i]) agh.pop_back();
nxt[i] = agh.back();
g[nxt[i]].push_back(i);
agh.push_back(i);
}
dfs(n + 1, n + 1, 1);
for (int i = 1; i <= n + 1; i++) mp[st[i]] = i;
for (int i = 1; i <= k; i++) {
upd(1, 1, N, st[i], hi[i]);
pair<int, int> t = get(1, 1, N, st[i], fn[i] + 1);
for (auto u : g[i]) {
s.erase({ans[u], u});
ans[u] = -1;
}
par[t.first] = i;
ans[i] = t.second - hi[i];
s.insert({ans[i], i});
}
for (int i = k + 1; i <= n + 1; i++) {
cout << s.rbegin()->first + 1 << " ";
int p = par[i - k];
s.erase({ans[p], p});
upd(1, 1, N, st[i - k], 0);
if (p > i - k && ans[p] != -1) {
pair<int, int> z = get(1, 1, N, st[p], fn[p] + 1);
par[z.first] = p;
ans[p] = z.second - hi[p];
s.insert({ans[p], p});
}
upd(1, 1, N, st[i], hi[i]);
pair<int, int> z = get(1, 1, N, st[i], fn[i] + 1);
for (auto u : g[i]) {
s.erase({ans[u], u});
ans[u] = -1;
}
par[z.first] = i;
ans[i] = z.second - hi[i];
s.insert({ans[i], i});
}
}
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.