output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
int s[15];
char c[1005][1005];
int a[1005][1005];
int vis[1005][1005];
int cnt[15];
const int dir[2][4] = {{0, 0, 1, -1}, {1, -1, 0, 0}};
struct point {
int x, y, t;
};
vector<point> v[1005];
queue<point> q[1005];
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void write(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int bfs(int x) {
int cnt = 0;
for (int i = 0; i < v[x].size(); i++)
q[x].push(point{v[x][i].x, v[x][i].y, 0});
v[x].clear();
while (!q[x].empty()) {
point u = q[x].front();
q[x].pop();
if (u.t == s[x]) {
v[x].push_back(u);
continue;
}
for (int i = 0; i < 4; i++) {
int dx = u.x + dir[0][i], dy = u.y + dir[1][i];
if (dx >= 1 && dx <= n && dy >= 1 && dy <= m && a[dx][dy] != -1 &&
!vis[dx][dy] && u.t + 1 <= s[x]) {
cnt++;
q[x].push(point{dx, dy, u.t + 1});
vis[dx][dy] = x;
}
}
}
if (cnt >= 1)
return 1;
else
return 0;
}
int main() {
n = read(), m = read(), p = read();
for (int i = 1; i <= p; i++) s[i] = read();
for (int i = 1; i <= n; i++) scanf("%s", (c[i] + 1));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (c[i][j] == '#')
a[i][j] = -1;
else if (c[i][j] >= '1' && c[i][j] <= '9')
vis[i][j] = c[i][j] - '0', v[vis[i][j]].push_back(point{i, j, 0});
}
while (1) {
int sum = 0;
for (int i = 1; i <= p; i++) sum += bfs(i);
if (sum == 0) break;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cnt[vis[i][j]]++;
for (int i = 1; i <= p; i++) cout << cnt[i] << ' ';
cout << endl;
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
int s[15];
char c[1005][1005];
int a[1005][1005];
int vis[1005][1005];
int cnt[15];
const int dir[2][4] = {{0, 0, 1, -1}, {1, -1, 0, 0}};
struct point {
int x, y, t;
};
vector<point> v[1005];
queue<point> q[1005];
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void write(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int bfs(int x) {
int cnt = 0;
for (int i = 0; i < v[x].size(); i++)
q[x].push(point{v[x][i].x, v[x][i].y, 0});
v[x].clear();
while (!q[x].empty()) {
point u = q[x].front();
q[x].pop();
if (u.t == s[x]) {
v[x].push_back(u);
continue;
}
for (int i = 0; i < 4; i++) {
int dx = u.x + dir[0][i], dy = u.y + dir[1][i];
if (dx >= 1 && dx <= n && dy >= 1 && dy <= m && a[dx][dy] != -1 &&
!vis[dx][dy] && u.t + 1 <= s[x]) {
cnt++;
q[x].push(point{dx, dy, u.t + 1});
vis[dx][dy] = x;
}
}
}
if (cnt >= 1)
return 1;
else
return 0;
}
int main() {
n = read(), m = read(), p = read();
for (int i = 1; i <= p; i++) s[i] = read();
for (int i = 1; i <= n; i++) scanf("%s", (c[i] + 1));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (c[i][j] == '#')
a[i][j] = -1;
else if (c[i][j] >= '1' && c[i][j] <= '9')
vis[i][j] = c[i][j] - '0', v[vis[i][j]].push_back(point{i, j, 0});
}
while (1) {
int sum = 0;
for (int i = 1; i <= p; i++) sum += bfs(i);
if (sum == 0) break;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cnt[vis[i][j]]++;
for (int i = 1; i <= p; i++) cout << cnt[i] << ' ';
cout << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
int n, m, p, nm;
vector<char> g;
vector<bool> vis;
vector<int> s, cnt;
vector<queue<int>> ex;
queue<int> q;
bool isEmpty(int v) {
if (v < 0) return false;
int i = v / m, j = v % m;
if (i < 0 || i >= n) return false;
if (j < 0 || j >= m) return false;
return (g[v] == '.');
}
int dist(int u, int v) {
int x1 = u / m, y1 = u % m;
int x2 = v / m, y2 = v % m;
return abs(x1 - x2) + abs(y1 - y2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> p;
nm = n * m;
g.resize(nm);
s.resize(p);
cnt.resize(p);
ex.resize(p);
for (int& e : s) cin >> e;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int v = i * m + j;
char c;
cin >> c;
g[v] = c;
if (c != '.' && c != '#') {
int pl = c - '1';
cnt[pl]++;
ex[pl].push(v);
}
}
}
bool found = true;
while (found) {
found = false;
for (int cur = 0; cur < p; cur++) {
int dis = 0;
while (dis != s[cur] && !ex[cur].empty()) {
queue<int> tmp;
while (!ex[cur].empty()) {
int u = ex[cur].front();
ex[cur].pop();
for (int mul : {-m, -1, m, 1}) {
int to = u + mul;
if (to / m != u / m && to % m != u % m) continue;
if (!isEmpty(to)) continue;
g[to] = cur + '1';
cnt[cur]++;
found = true;
tmp.push(to);
}
}
ex[cur] = tmp;
dis++;
}
}
}
for (int i : cnt) cout << i << " ";
cout << endl;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
int n, m, p, nm;
vector<char> g;
vector<bool> vis;
vector<int> s, cnt;
vector<queue<int>> ex;
queue<int> q;
bool isEmpty(int v) {
if (v < 0) return false;
int i = v / m, j = v % m;
if (i < 0 || i >= n) return false;
if (j < 0 || j >= m) return false;
return (g[v] == '.');
}
int dist(int u, int v) {
int x1 = u / m, y1 = u % m;
int x2 = v / m, y2 = v % m;
return abs(x1 - x2) + abs(y1 - y2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> p;
nm = n * m;
g.resize(nm);
s.resize(p);
cnt.resize(p);
ex.resize(p);
for (int& e : s) cin >> e;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int v = i * m + j;
char c;
cin >> c;
g[v] = c;
if (c != '.' && c != '#') {
int pl = c - '1';
cnt[pl]++;
ex[pl].push(v);
}
}
}
bool found = true;
while (found) {
found = false;
for (int cur = 0; cur < p; cur++) {
int dis = 0;
while (dis != s[cur] && !ex[cur].empty()) {
queue<int> tmp;
while (!ex[cur].empty()) {
int u = ex[cur].front();
ex[cur].pop();
for (int mul : {-m, -1, m, 1}) {
int to = u + mul;
if (to / m != u / m && to % m != u % m) continue;
if (!isEmpty(to)) continue;
g[to] = cur + '1';
cnt[cur]++;
found = true;
tmp.push(to);
}
}
ex[cur] = tmp;
dis++;
}
}
}
for (int i : cnt) cout << i << " ";
cout << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
int direcX[] = {0, 0, 1, -1};
int direcY[] = {1, -1, 0, 0};
struct node {
int x, y;
int player;
int dis;
node(int _x, int _y, int _player, int _dis)
: x(_x), y(_y), player(_player), dis(_dis) {}
node() {}
bool operator<(const node& n1) const {
if (player != n1.player) return player > n1.player;
return dis > n1.dis;
}
};
priority_queue<node> q;
char grid[1001][1001];
int ans[1001][1001];
int speed[10];
int cnt[10];
bool isValid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m && ans[x][y] == -1);
}
int main() {
scanf("%d", &n), scanf("%d", &m), scanf("%d", &p);
memset(ans, -1, sizeof(ans));
for (int i = 1; i <= p; ++i) scanf("%d", &speed[i]);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
cin >> grid[i][j];
if (grid[i][j] >= '1' && grid[i][j] <= '9') {
ans[i][j] = grid[i][j] - '0';
q.push(node(i, j, ans[i][j] - 1, 0));
}
if (grid[i][j] == '#') ans[i][j] = 0;
}
while (!q.empty()) {
node cur = q.top();
q.pop();
if (cur.dis + 1 > speed[(cur.player % p) + 1]) {
q.push(node(cur.x, cur.y, cur.player + p, 0));
continue;
}
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
int nxtX = cur.x + direcX[i];
int nxtY = cur.y + direcY[i];
if (!isValid(nxtX, nxtY)) continue;
ans[nxtX][nxtY] = ((cur.player) % p) + 1;
q.push(node(nxtX, nxtY, cur.player, cur.dis + 1));
}
}
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (ans[i][j] >= 1 && ans[i][j] <= p) cnt[ans[i][j]]++;
for (int i = 1; i <= p; ++i) printf("%d ", cnt[i]);
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
int direcX[] = {0, 0, 1, -1};
int direcY[] = {1, -1, 0, 0};
struct node {
int x, y;
int player;
int dis;
node(int _x, int _y, int _player, int _dis)
: x(_x), y(_y), player(_player), dis(_dis) {}
node() {}
bool operator<(const node& n1) const {
if (player != n1.player) return player > n1.player;
return dis > n1.dis;
}
};
priority_queue<node> q;
char grid[1001][1001];
int ans[1001][1001];
int speed[10];
int cnt[10];
bool isValid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m && ans[x][y] == -1);
}
int main() {
scanf("%d", &n), scanf("%d", &m), scanf("%d", &p);
memset(ans, -1, sizeof(ans));
for (int i = 1; i <= p; ++i) scanf("%d", &speed[i]);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
cin >> grid[i][j];
if (grid[i][j] >= '1' && grid[i][j] <= '9') {
ans[i][j] = grid[i][j] - '0';
q.push(node(i, j, ans[i][j] - 1, 0));
}
if (grid[i][j] == '#') ans[i][j] = 0;
}
while (!q.empty()) {
node cur = q.top();
q.pop();
if (cur.dis + 1 > speed[(cur.player % p) + 1]) {
q.push(node(cur.x, cur.y, cur.player + p, 0));
continue;
}
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
int nxtX = cur.x + direcX[i];
int nxtY = cur.y + direcY[i];
if (!isValid(nxtX, nxtY)) continue;
ans[nxtX][nxtY] = ((cur.player) % p) + 1;
q.push(node(nxtX, nxtY, cur.player, cur.dis + 1));
}
}
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (ans[i][j] >= 1 && ans[i][j] <= p) cnt[ans[i][j]]++;
for (int i = 1; i <= p; ++i) printf("%d ", cnt[i]);
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
char arr[1005][1005];
queue<pair<int, int>> cells[10];
int free_cells, current_player;
int ans[10], s[10];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool valid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '.');
}
void bfs() {
queue<pair<pair<int, int>, int>> tempqueue;
while (!cells[current_player].empty()) {
int x = cells[current_player].front().first;
int y = cells[current_player].front().second;
cells[current_player].pop();
tempqueue.push({{x, y}, 0});
}
while (!tempqueue.empty()) {
int x = tempqueue.front().first.first;
int y = tempqueue.front().first.second;
int turn = tempqueue.front().second;
tempqueue.pop();
for (int i = 0; i < 4; ++i) {
if (!valid(x + dx[i], y + dy[i])) continue;
arr[x + dx[i]][y + dy[i]] = char(current_player + '0');
free_cells--;
if (turn == s[current_player] - 1) {
cells[current_player].push({x + dx[i], y + dy[i]});
} else {
tempqueue.push({{x + dx[i], y + dy[i]}, turn + 1});
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= p; ++i) {
scanf("%d", &s[i]);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
scanf(" %c", &arr[i][j]);
if (arr[i][j] >= '1' && arr[i][j] <= '9') {
int x = arr[i][j] - '0';
cells[x].push({i, j});
}
if (arr[i][j] == '.') free_cells++;
}
}
current_player = 1;
int prev = free_cells;
int ctr = 0;
while (1) {
bfs();
if (prev == free_cells)
ctr++;
else
ctr = 0;
if (ctr >= p) break;
prev = free_cells;
current_player = current_player + 1;
if (current_player > p) current_player = 1;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (arr[i][j] >= '1' && arr[i][j] <= '9') {
ans[arr[i][j] - '0']++;
}
}
}
for (int i = 1; i <= p; ++i) printf("%d ", ans[i]);
cout << '\n';
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
char arr[1005][1005];
queue<pair<int, int>> cells[10];
int free_cells, current_player;
int ans[10], s[10];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool valid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '.');
}
void bfs() {
queue<pair<pair<int, int>, int>> tempqueue;
while (!cells[current_player].empty()) {
int x = cells[current_player].front().first;
int y = cells[current_player].front().second;
cells[current_player].pop();
tempqueue.push({{x, y}, 0});
}
while (!tempqueue.empty()) {
int x = tempqueue.front().first.first;
int y = tempqueue.front().first.second;
int turn = tempqueue.front().second;
tempqueue.pop();
for (int i = 0; i < 4; ++i) {
if (!valid(x + dx[i], y + dy[i])) continue;
arr[x + dx[i]][y + dy[i]] = char(current_player + '0');
free_cells--;
if (turn == s[current_player] - 1) {
cells[current_player].push({x + dx[i], y + dy[i]});
} else {
tempqueue.push({{x + dx[i], y + dy[i]}, turn + 1});
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= p; ++i) {
scanf("%d", &s[i]);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
scanf(" %c", &arr[i][j]);
if (arr[i][j] >= '1' && arr[i][j] <= '9') {
int x = arr[i][j] - '0';
cells[x].push({i, j});
}
if (arr[i][j] == '.') free_cells++;
}
}
current_player = 1;
int prev = free_cells;
int ctr = 0;
while (1) {
bfs();
if (prev == free_cells)
ctr++;
else
ctr = 0;
if (ctr >= p) break;
prev = free_cells;
current_player = current_player + 1;
if (current_player > p) current_player = 1;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (arr[i][j] >= '1' && arr[i][j] <= '9') {
ans[arr[i][j] - '0']++;
}
}
}
for (int i = 1; i <= p; ++i) printf("%d ", ans[i]);
cout << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1005;
const int INF = (1 << 30);
struct el {
int x, y, dist;
el() { this->x = this->y = this->dist = 0; }
el(int x, int y, int dist) {
this->x = x;
this->y = y;
this->dist = dist;
}
inline bool operator<(const el &other) const {
return this->dist > other.dist;
}
};
int n, m, p;
int speed[10], ans[10];
char s[NMAX][NMAX];
int a[NMAX][NMAX];
vector<pair<int, int> > pos[10];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
inline bool Verify(int i, int j) {
return 1 <= i && i <= n && 1 <= j && j <= m;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> p;
for (int i = 1; i <= p; ++i) cin >> speed[i];
for (int i = 1; i <= n; ++i) cin >> (s[i] + 1);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if ('0' <= s[i][j] && s[i][j] <= '9') {
a[i][j] = (s[i][j] - '0');
pos[a[i][j]].push_back(make_pair(i, j));
} else if (s[i][j] == '#') {
a[i][j] = -1;
}
}
}
bool good = true;
while (good) {
good = false;
for (int ind = 1; ind <= p; ++ind) {
deque<el> q;
for (int i = 0; i < pos[ind].size(); ++i)
q.push_back(el(pos[ind][i].first, pos[ind][i].second, speed[ind]));
pos[ind].clear();
int i, j, x, y, dist, k;
while (!q.empty()) {
i = q.front().x;
j = q.front().y;
dist = q.front().dist;
q.pop_front();
if (dist == 0) {
pos[ind].push_back(make_pair(i, j));
continue;
}
for (k = 0; k < 4; ++k) {
x = i + dx[k];
y = j + dy[k];
if (Verify(x, y) && a[x][y] == 0) {
good = true;
a[x][y] = ind;
q.push_back(el(x, y, dist - 1));
}
}
}
}
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (1 <= a[i][j] && a[i][j] <= 9) ++ans[a[i][j]];
for (int i = 1; i <= p; ++i) cout << ans[i] << " ";
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1005;
const int INF = (1 << 30);
struct el {
int x, y, dist;
el() { this->x = this->y = this->dist = 0; }
el(int x, int y, int dist) {
this->x = x;
this->y = y;
this->dist = dist;
}
inline bool operator<(const el &other) const {
return this->dist > other.dist;
}
};
int n, m, p;
int speed[10], ans[10];
char s[NMAX][NMAX];
int a[NMAX][NMAX];
vector<pair<int, int> > pos[10];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
inline bool Verify(int i, int j) {
return 1 <= i && i <= n && 1 <= j && j <= m;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> p;
for (int i = 1; i <= p; ++i) cin >> speed[i];
for (int i = 1; i <= n; ++i) cin >> (s[i] + 1);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if ('0' <= s[i][j] && s[i][j] <= '9') {
a[i][j] = (s[i][j] - '0');
pos[a[i][j]].push_back(make_pair(i, j));
} else if (s[i][j] == '#') {
a[i][j] = -1;
}
}
}
bool good = true;
while (good) {
good = false;
for (int ind = 1; ind <= p; ++ind) {
deque<el> q;
for (int i = 0; i < pos[ind].size(); ++i)
q.push_back(el(pos[ind][i].first, pos[ind][i].second, speed[ind]));
pos[ind].clear();
int i, j, x, y, dist, k;
while (!q.empty()) {
i = q.front().x;
j = q.front().y;
dist = q.front().dist;
q.pop_front();
if (dist == 0) {
pos[ind].push_back(make_pair(i, j));
continue;
}
for (k = 0; k < 4; ++k) {
x = i + dx[k];
y = j + dy[k];
if (Verify(x, y) && a[x][y] == 0) {
good = true;
a[x][y] = ind;
q.push_back(el(x, y, dist - 1));
}
}
}
}
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (1 <= a[i][j] && a[i][j] <= 9) ++ans[a[i][j]];
for (int i = 1; i <= p; ++i) cout << ans[i] << " ";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
long long n, m, p, cs;
long long speed[9];
set<pair<long long, long long>> s[9];
char c[1001][1001];
void expand(long long pl, long long sp) {
queue<tuple<long long, long long, long long>> q;
for (auto u : s[pl]) {
q.push(make_tuple(u.first, u.second, 0));
}
s[pl].clear();
while (q.size()) {
auto [x, y, d] = q.front();
q.pop();
c[x][y] = char(pl + '1');
if (d >= sp) {
s[pl].insert({x, y});
continue;
}
if (c[x - 1][y] == '.') {
q.push(make_tuple(x - 1, y, d + 1));
cs = 1;
}
if (c[x + 1][y] == '.') {
q.push(make_tuple(x + 1, y, d + 1));
cs = 1;
}
if (c[x][y + 1] == '.') {
q.push(make_tuple(x, y + 1, d + 1));
cs = 1;
}
if (c[x][y - 1] == '.') {
q.push(make_tuple(x, y - 1, d + 1));
cs = 1;
}
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
memset(c, '#', sizeof(c));
cin >> n >> m >> p;
for (long long i = 0; i < p; i++) {
cin >> speed[i];
}
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) {
cin >> c[i][j];
for (char k = '1'; k <= '9'; k++) {
if (c[i][j] == k) {
s[k - '1'].insert({i, j});
}
}
}
}
while (1) {
cs = 0;
for (long long i = 0; i < p; i++) {
expand(i, speed[i]);
}
if (cs == 0) break;
}
vector<long long> ans(9);
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) {
for (char k = '1'; k <= '9'; k++) {
if (c[i][j] == k) {
ans[k - '1']++;
}
}
}
}
for (long long i = 0; i < p; i++) cout << ans[i] << ' ';
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
long long n, m, p, cs;
long long speed[9];
set<pair<long long, long long>> s[9];
char c[1001][1001];
void expand(long long pl, long long sp) {
queue<tuple<long long, long long, long long>> q;
for (auto u : s[pl]) {
q.push(make_tuple(u.first, u.second, 0));
}
s[pl].clear();
while (q.size()) {
auto [x, y, d] = q.front();
q.pop();
c[x][y] = char(pl + '1');
if (d >= sp) {
s[pl].insert({x, y});
continue;
}
if (c[x - 1][y] == '.') {
q.push(make_tuple(x - 1, y, d + 1));
cs = 1;
}
if (c[x + 1][y] == '.') {
q.push(make_tuple(x + 1, y, d + 1));
cs = 1;
}
if (c[x][y + 1] == '.') {
q.push(make_tuple(x, y + 1, d + 1));
cs = 1;
}
if (c[x][y - 1] == '.') {
q.push(make_tuple(x, y - 1, d + 1));
cs = 1;
}
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
memset(c, '#', sizeof(c));
cin >> n >> m >> p;
for (long long i = 0; i < p; i++) {
cin >> speed[i];
}
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) {
cin >> c[i][j];
for (char k = '1'; k <= '9'; k++) {
if (c[i][j] == k) {
s[k - '1'].insert({i, j});
}
}
}
}
while (1) {
cs = 0;
for (long long i = 0; i < p; i++) {
expand(i, speed[i]);
}
if (cs == 0) break;
}
vector<long long> ans(9);
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) {
for (char k = '1'; k <= '9'; k++) {
if (c[i][j] == k) {
ans[k - '1']++;
}
}
}
}
for (long long i = 0; i < p; i++) cout << ans[i] << ' ';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int arr[1001][1001], num[1001][1001], s[10], ans[10];
queue<pair<int, int> > qu[10];
int main() {
int n, m, p;
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &p);
for (int i = 1; i <= p; i++) scanf("%d", &s[i]);
for (int i = 1; i <= n; i++) {
string str;
cin >> str;
int len = str.length();
for (int j = 0; j < len; j++) {
if (str[j] == '.')
num[i][j + 1] = 0;
else if (str[j] == '#')
num[i][j + 1] = -1;
else {
num[i][j + 1] = str[j] - '0';
qu[num[i][j + 1]].push(make_pair(i, j + 1));
}
}
}
bool flag = 1;
while (flag) {
flag = 0;
for (int i = 1; i <= p; i++) {
for (int j = 1; j <= s[i]; j++) {
int len = qu[i].size();
while (!qu[i].empty() && len > 0) {
len--;
int x = qu[i].front().first, y = qu[i].front().second;
qu[i].pop();
if (x - 1 > 0 && num[x - 1][y] == 0) {
num[x - 1][y] = i;
qu[i].push(make_pair(x - 1, y));
flag = 1;
}
if (x + 1 <= n && num[x + 1][y] == 0) {
num[x + 1][y] = i;
qu[i].push(make_pair(x + 1, y));
flag = 1;
}
if (y - 1 > 0 && num[x][y - 1] == 0) {
num[x][y - 1] = i;
qu[i].push(make_pair(x, y - 1));
flag = 1;
}
if (y + 1 <= m && num[x][y + 1] == 0) {
num[x][y + 1] = i;
qu[i].push(make_pair(x, y + 1));
flag = 1;
}
}
if (qu[i].empty()) break;
}
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (num[i][j] != -1) ans[num[i][j]]++;
for (int i = 1; i <= p; i++) cout << ans[i] << " ";
cout << endl;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int arr[1001][1001], num[1001][1001], s[10], ans[10];
queue<pair<int, int> > qu[10];
int main() {
int n, m, p;
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &p);
for (int i = 1; i <= p; i++) scanf("%d", &s[i]);
for (int i = 1; i <= n; i++) {
string str;
cin >> str;
int len = str.length();
for (int j = 0; j < len; j++) {
if (str[j] == '.')
num[i][j + 1] = 0;
else if (str[j] == '#')
num[i][j + 1] = -1;
else {
num[i][j + 1] = str[j] - '0';
qu[num[i][j + 1]].push(make_pair(i, j + 1));
}
}
}
bool flag = 1;
while (flag) {
flag = 0;
for (int i = 1; i <= p; i++) {
for (int j = 1; j <= s[i]; j++) {
int len = qu[i].size();
while (!qu[i].empty() && len > 0) {
len--;
int x = qu[i].front().first, y = qu[i].front().second;
qu[i].pop();
if (x - 1 > 0 && num[x - 1][y] == 0) {
num[x - 1][y] = i;
qu[i].push(make_pair(x - 1, y));
flag = 1;
}
if (x + 1 <= n && num[x + 1][y] == 0) {
num[x + 1][y] = i;
qu[i].push(make_pair(x + 1, y));
flag = 1;
}
if (y - 1 > 0 && num[x][y - 1] == 0) {
num[x][y - 1] = i;
qu[i].push(make_pair(x, y - 1));
flag = 1;
}
if (y + 1 <= m && num[x][y + 1] == 0) {
num[x][y + 1] = i;
qu[i].push(make_pair(x, y + 1));
flag = 1;
}
}
if (qu[i].empty()) break;
}
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (num[i][j] != -1) ans[num[i][j]]++;
for (int i = 1; i <= p; i++) cout << ans[i] << " ";
cout << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct cells {
int x, y, turns;
};
vector<cells> start[15];
int n, m, p;
int a[15];
string s[1005];
int dx[5] = {1, 0, -1, 0};
int dy[5] = {0, -1, 0, 1};
int ans[15] = {0};
queue<cells> q[15];
bool check = true;
inline bool isval(int x, int y) {
if (x >= 0 and x < n and y >= 0 and y < m and s[x][y] == '.') {
return true;
}
return false;
}
inline void bfs(int x, int y, int player, int steps) {
if (steps == 0) {
return;
}
for (int i = 0; i < 4; i++) {
int x1 = x + dx[i];
int y1 = y + dy[i];
if (isval(x1, y1)) {
if (!check) {
check = true;
}
s[x1][y1] = char(player + '0');
q[player].push((cells){x1, y1, steps - 1});
}
}
}
int main() {
cin >> n >> m >> p;
for (int i = 1; i <= p; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] - '0' >= 1 and s[i][j] - '0' <= p) {
start[s[i][j] - '0'].push_back((cells){i, j, a[s[i][j] - '0']});
}
}
}
while (check) {
check = false;
for (int pl = 1; pl <= p; pl++) {
for (int i = 0; i < start[pl].size(); i++) {
q[pl].push(start[pl][i]);
}
start[pl].clear();
while (!q[pl].empty()) {
int x = q[pl].front().x;
int y = q[pl].front().y;
int steps = q[pl].front().turns;
if (steps == 0) {
start[pl].push_back((cells){x, y, a[pl]});
}
q[pl].pop();
bfs(x, y, pl, steps);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if ((s[i][j] - '0') >= 1 and (s[i][j] - '0') <= p) {
ans[s[i][j] - '0']++;
}
}
}
for (int i = 1; i <= p; i++) {
cout << ans[i] << " ";
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct cells {
int x, y, turns;
};
vector<cells> start[15];
int n, m, p;
int a[15];
string s[1005];
int dx[5] = {1, 0, -1, 0};
int dy[5] = {0, -1, 0, 1};
int ans[15] = {0};
queue<cells> q[15];
bool check = true;
inline bool isval(int x, int y) {
if (x >= 0 and x < n and y >= 0 and y < m and s[x][y] == '.') {
return true;
}
return false;
}
inline void bfs(int x, int y, int player, int steps) {
if (steps == 0) {
return;
}
for (int i = 0; i < 4; i++) {
int x1 = x + dx[i];
int y1 = y + dy[i];
if (isval(x1, y1)) {
if (!check) {
check = true;
}
s[x1][y1] = char(player + '0');
q[player].push((cells){x1, y1, steps - 1});
}
}
}
int main() {
cin >> n >> m >> p;
for (int i = 1; i <= p; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] - '0' >= 1 and s[i][j] - '0' <= p) {
start[s[i][j] - '0'].push_back((cells){i, j, a[s[i][j] - '0']});
}
}
}
while (check) {
check = false;
for (int pl = 1; pl <= p; pl++) {
for (int i = 0; i < start[pl].size(); i++) {
q[pl].push(start[pl][i]);
}
start[pl].clear();
while (!q[pl].empty()) {
int x = q[pl].front().x;
int y = q[pl].front().y;
int steps = q[pl].front().turns;
if (steps == 0) {
start[pl].push_back((cells){x, y, a[pl]});
}
q[pl].pop();
bfs(x, y, pl, steps);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if ((s[i][j] - '0') >= 1 and (s[i][j] - '0') <= p) {
ans[s[i][j] - '0']++;
}
}
}
for (int i = 1; i <= p; i++) {
cout << ans[i] << " ";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char g[1000][1000];
int n, m, p, s[10];
bool activo[10];
int suma[10];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool esValido(int a, int b) { return (0 <= a && a < n && 0 <= b && b < m); }
struct par {
int x, y;
};
int main() {
scanf("%d %d %d", &n, &m, &p);
for (int i = 1; i <= p; i++) {
scanf("%d", &s[i]);
activo[i] = true;
}
for (int i = 0; i < n; i++) scanf(" %s", &g[i][0]);
queue<par> cola[p + 1];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if ('1' <= g[i][j] && g[i][j] <= '9') cola[g[i][j] - '0'].push({i, j});
while (true) {
int exp = 0;
for (int i = 1; i <= p; i++) {
if (activo[i]) {
int res = 0;
for (int z = 0; z < s[i]; z++) {
int cant = 0;
int tamCola = cola[i].size();
while (tamCola--) {
auto k = cola[i].front();
cola[i].pop();
for (int j = 0; j < 4; j++) {
int x = k.x + dx[j];
int y = k.y + dy[j];
if (esValido(x, y) && g[x][y] == '.') {
exp++;
cant++;
res++;
g[x][y] = i + '0';
cola[i].push({x, y});
}
}
}
if (cant == 0) {
break;
}
}
if (res == 0) {
activo[i] = false;
}
}
}
if (exp == 0) break;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if ('1' <= g[i][j] && g[i][j] <= '9') suma[g[i][j] - '0']++;
for (int i = 1; i <= p; i++) printf("%d ", suma[i]);
printf("\n");
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char g[1000][1000];
int n, m, p, s[10];
bool activo[10];
int suma[10];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool esValido(int a, int b) { return (0 <= a && a < n && 0 <= b && b < m); }
struct par {
int x, y;
};
int main() {
scanf("%d %d %d", &n, &m, &p);
for (int i = 1; i <= p; i++) {
scanf("%d", &s[i]);
activo[i] = true;
}
for (int i = 0; i < n; i++) scanf(" %s", &g[i][0]);
queue<par> cola[p + 1];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if ('1' <= g[i][j] && g[i][j] <= '9') cola[g[i][j] - '0'].push({i, j});
while (true) {
int exp = 0;
for (int i = 1; i <= p; i++) {
if (activo[i]) {
int res = 0;
for (int z = 0; z < s[i]; z++) {
int cant = 0;
int tamCola = cola[i].size();
while (tamCola--) {
auto k = cola[i].front();
cola[i].pop();
for (int j = 0; j < 4; j++) {
int x = k.x + dx[j];
int y = k.y + dy[j];
if (esValido(x, y) && g[x][y] == '.') {
exp++;
cant++;
res++;
g[x][y] = i + '0';
cola[i].push({x, y});
}
}
}
if (cant == 0) {
break;
}
}
if (res == 0) {
activo[i] = false;
}
}
}
if (exp == 0) break;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if ('1' <= g[i][j] && g[i][j] <= '9') suma[g[i][j] - '0']++;
for (int i = 1; i <= p; i++) printf("%d ", suma[i]);
printf("\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const char duom[] = "i.txt";
int speed[10];
bool blocked[1005][1005];
deque<pair<int, int>> Q[10];
int ans[10];
void bfs(int node) {
int times = Q[node].size();
for (int i = 0; i < times; i++) {
pair<int, int> pos = Q[node].front();
Q[node].pop_front();
if (!blocked[pos.first - 1][pos.second]) {
Q[node].push_back(make_pair(pos.first - 1, pos.second));
blocked[pos.first - 1][pos.second] = true;
ans[node]++;
}
if (!blocked[pos.first + 1][pos.second]) {
Q[node].push_back(make_pair(pos.first + 1, pos.second));
blocked[pos.first + 1][pos.second] = true;
ans[node]++;
}
if (!blocked[pos.first][pos.second - 1]) {
Q[node].push_back(make_pair(pos.first, pos.second - 1));
blocked[pos.first][pos.second - 1] = true;
ans[node]++;
}
if (!blocked[pos.first][pos.second + 1]) {
Q[node].push_back(make_pair(pos.first, pos.second + 1));
blocked[pos.first][pos.second + 1] = true;
ans[node]++;
}
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m, p;
cin >> n >> m >> p;
int cnt = n * m;
for (int i = 0; i < p; i++) cin >> speed[i];
char c;
for (int i = 0; i <= n + 1; i++) {
blocked[i][0] = true;
blocked[i][m + 1] = true;
}
for (int i = 0; i <= m + 1; i++) {
blocked[0][i] = true;
blocked[n + 1][i] = true;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c;
cin >> c;
if (c == '#') {
cnt--;
blocked[i][j] = true;
continue;
}
if (c != '.') {
cnt--;
Q[c - '1'].push_front(make_pair(i, j));
ans[c - '1']++;
blocked[i][j] = true;
}
}
}
for (int t = 0; t < cnt; t++) {
for (int i = 0; i < p; i++) {
for (int j = 0; j < speed[i] && Q[i].size(); j++) {
bfs(i);
}
}
}
for (int i = 0; i < p; i++) cout << ans[i] << " ";
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const char duom[] = "i.txt";
int speed[10];
bool blocked[1005][1005];
deque<pair<int, int>> Q[10];
int ans[10];
void bfs(int node) {
int times = Q[node].size();
for (int i = 0; i < times; i++) {
pair<int, int> pos = Q[node].front();
Q[node].pop_front();
if (!blocked[pos.first - 1][pos.second]) {
Q[node].push_back(make_pair(pos.first - 1, pos.second));
blocked[pos.first - 1][pos.second] = true;
ans[node]++;
}
if (!blocked[pos.first + 1][pos.second]) {
Q[node].push_back(make_pair(pos.first + 1, pos.second));
blocked[pos.first + 1][pos.second] = true;
ans[node]++;
}
if (!blocked[pos.first][pos.second - 1]) {
Q[node].push_back(make_pair(pos.first, pos.second - 1));
blocked[pos.first][pos.second - 1] = true;
ans[node]++;
}
if (!blocked[pos.first][pos.second + 1]) {
Q[node].push_back(make_pair(pos.first, pos.second + 1));
blocked[pos.first][pos.second + 1] = true;
ans[node]++;
}
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m, p;
cin >> n >> m >> p;
int cnt = n * m;
for (int i = 0; i < p; i++) cin >> speed[i];
char c;
for (int i = 0; i <= n + 1; i++) {
blocked[i][0] = true;
blocked[i][m + 1] = true;
}
for (int i = 0; i <= m + 1; i++) {
blocked[0][i] = true;
blocked[n + 1][i] = true;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c;
cin >> c;
if (c == '#') {
cnt--;
blocked[i][j] = true;
continue;
}
if (c != '.') {
cnt--;
Q[c - '1'].push_front(make_pair(i, j));
ans[c - '1']++;
blocked[i][j] = true;
}
}
}
for (int t = 0; t < cnt; t++) {
for (int i = 0; i < p; i++) {
for (int j = 0; j < speed[i] && Q[i].size(); j++) {
bfs(i);
}
}
}
for (int i = 0; i < p; i++) cout << ans[i] << " ";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1010;
struct node {
int x, y, step;
node(int a = 0, int b = 0, int c = 0) : x(a), y(b), step(c) {}
};
int m, n, p;
int dir[][2] = {1, 0, -1, 0, 0, 1, 0, -1};
queue<node> q, q1[10];
char mp[N][N];
int vis[N][N], ans[N], speed[N];
bool check(int x, int y) {
if (x > 0 && x <= n && y > 0 && y <= m && !vis[x][y]) return true;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> p;
for (int i = 1; i <= p; i++) cin >> speed[i];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> mp[i][j];
if (mp[i][j] != '.') {
vis[i][j] = 1;
if (mp[i][j] > '0' && mp[i][j] <= '9') {
q1[mp[i][j] - '0'].push(node(i, j, speed[mp[i][j] - '0']));
ans[mp[i][j] - '0']++;
}
}
}
}
while (true) {
int flag = 0;
for (int i = 1; i <= p; i++) {
while (q1[i].size()) {
flag = 1;
q.push(q1[i].front());
q1[i].pop();
}
while (q.size()) {
node now = q.front();
q.pop();
if (now.step == 0) {
q1[i].push(node(now.x, now.y, speed[i]));
continue;
}
for (int j = 0; j < 4; j++) {
int dx = now.x + dir[j][0];
int dy = now.y + dir[j][1];
if (check(dx, dy)) {
vis[dx][dy] = 1;
ans[i]++;
q.push(node(dx, dy, now.step - 1));
}
}
}
}
if (!flag) break;
}
for (int i = 1; i <= p; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1010;
struct node {
int x, y, step;
node(int a = 0, int b = 0, int c = 0) : x(a), y(b), step(c) {}
};
int m, n, p;
int dir[][2] = {1, 0, -1, 0, 0, 1, 0, -1};
queue<node> q, q1[10];
char mp[N][N];
int vis[N][N], ans[N], speed[N];
bool check(int x, int y) {
if (x > 0 && x <= n && y > 0 && y <= m && !vis[x][y]) return true;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> p;
for (int i = 1; i <= p; i++) cin >> speed[i];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> mp[i][j];
if (mp[i][j] != '.') {
vis[i][j] = 1;
if (mp[i][j] > '0' && mp[i][j] <= '9') {
q1[mp[i][j] - '0'].push(node(i, j, speed[mp[i][j] - '0']));
ans[mp[i][j] - '0']++;
}
}
}
}
while (true) {
int flag = 0;
for (int i = 1; i <= p; i++) {
while (q1[i].size()) {
flag = 1;
q.push(q1[i].front());
q1[i].pop();
}
while (q.size()) {
node now = q.front();
q.pop();
if (now.step == 0) {
q1[i].push(node(now.x, now.y, speed[i]));
continue;
}
for (int j = 0; j < 4; j++) {
int dx = now.x + dir[j][0];
int dy = now.y + dir[j][1];
if (check(dx, dy)) {
vis[dx][dy] = 1;
ans[i]++;
q.push(node(dx, dy, now.step - 1));
}
}
}
}
if (!flag) break;
}
for (int i = 1; i <= p; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
char matrix[1000][1000];
int expansion[10];
int controlled[10];
int max_layer[10];
vector<pair<int, int> > starters[10];
int main() {
cin >> n >> m >> p;
queue<pair<pair<int, int>, int> > q;
for (int i = 0; i < p; i++) {
cin >> expansion[i];
controlled[i] = 0;
max_layer[i] = expansion[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> matrix[i][j];
if (matrix[i][j] != '.' && matrix[i][j] != '#') {
int player = matrix[i][j] - '1';
starters[player].push_back(make_pair(i, j));
controlled[player]++;
}
}
}
bool moved = true;
while (moved) {
moved = false;
for (int i = 0; i < p; i++) {
for (int j = 0; j < starters[i].size(); j++)
q.push(make_pair(starters[i][j], 0));
starters[i].clear();
while (!q.empty()) {
int x = q.front().first.first;
int y = q.front().first.second;
int layer = q.front().second;
q.pop();
pair<int, int> locs[4] = {make_pair(x + 1, y), make_pair(x - 1, y),
make_pair(x, y + 1), make_pair(x, y - 1)};
for (pair<int, int> loc : locs) {
if (loc.first < 0 || loc.first > n - 1 || loc.second < 0 ||
loc.second > m - 1)
continue;
if (matrix[loc.first][loc.second] != '.') continue;
moved = true;
controlled[i]++;
matrix[loc.first][loc.second] = (i + '1');
if (layer >= expansion[i] - 1) {
starters[i].push_back(make_pair(loc.first, loc.second));
} else {
q.push(make_pair(make_pair(loc.first, loc.second), layer + 1));
}
}
}
}
}
for (int i = 0; i < p; i++) cout << controlled[i] << " ";
cout << endl;
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
char matrix[1000][1000];
int expansion[10];
int controlled[10];
int max_layer[10];
vector<pair<int, int> > starters[10];
int main() {
cin >> n >> m >> p;
queue<pair<pair<int, int>, int> > q;
for (int i = 0; i < p; i++) {
cin >> expansion[i];
controlled[i] = 0;
max_layer[i] = expansion[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> matrix[i][j];
if (matrix[i][j] != '.' && matrix[i][j] != '#') {
int player = matrix[i][j] - '1';
starters[player].push_back(make_pair(i, j));
controlled[player]++;
}
}
}
bool moved = true;
while (moved) {
moved = false;
for (int i = 0; i < p; i++) {
for (int j = 0; j < starters[i].size(); j++)
q.push(make_pair(starters[i][j], 0));
starters[i].clear();
while (!q.empty()) {
int x = q.front().first.first;
int y = q.front().first.second;
int layer = q.front().second;
q.pop();
pair<int, int> locs[4] = {make_pair(x + 1, y), make_pair(x - 1, y),
make_pair(x, y + 1), make_pair(x, y - 1)};
for (pair<int, int> loc : locs) {
if (loc.first < 0 || loc.first > n - 1 || loc.second < 0 ||
loc.second > m - 1)
continue;
if (matrix[loc.first][loc.second] != '.') continue;
moved = true;
controlled[i]++;
matrix[loc.first][loc.second] = (i + '1');
if (layer >= expansion[i] - 1) {
starters[i].push_back(make_pair(loc.first, loc.second));
} else {
q.push(make_pair(make_pair(loc.first, loc.second), layer + 1));
}
}
}
}
}
for (int i = 0; i < p; i++) cout << controlled[i] << " ";
cout << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, p, last[10];
struct node {
int x, y;
};
int s[10];
char a[1005][1005];
int vis[1005][1005], ans[10], can[10];
queue<node> que[10];
int d[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void bfs() {
for (int i = 1; i <= p; i++) {
while (!que[i].empty()) que[i].pop();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] != '.' && a[i][j] != '#') {
que[a[i][j] - '0'].push({i, j});
vis[i][j] = a[i][j] - '0';
last[a[i][j] - '0']++;
}
}
}
bool flag = true;
while (flag) {
flag = false;
for (int idx = 1; idx <= p; idx++) {
for (int ss = 1; ss <= s[idx] && can[idx] == 0; ss++) {
int num = last[idx];
last[idx] = 0;
for (int t = 1; t <= num; t++) {
node now = que[idx].front();
que[idx].pop();
ans[idx]++;
for (int i = 0; i < 4; i++) {
int xx = now.x + d[i][0], yy = now.y + d[i][1];
if (xx >= 0 && xx < n && yy >= 0 && yy < m && vis[xx][yy] == 0 &&
a[xx][yy] == '.') {
que[idx].push({xx, yy});
vis[xx][yy] = idx;
last[idx]++;
}
}
}
if (que[idx].empty()) can[idx] = idx;
}
}
for (int i = 1; i <= p; i++)
if (!que[i].empty()) flag = true;
}
for (int i = 1; i <= p; i++) printf("%d ", ans[i]);
printf("\n");
}
int main() {
while (scanf("%d %d %d", &n, &m, &p) != EOF) {
memset(a, 0, sizeof(a));
memset(s, 0, sizeof(s));
memset(ans, 0, sizeof(ans));
memset(vis, 0, sizeof(vis));
memset(last, 0, sizeof(last));
memset(can, 0, sizeof(can));
for (int i = 1; i <= p; i++) scanf("%d", &s[i]);
getchar();
for (int i = 0; i < n; i++) scanf("%s", a[i]);
bfs();
}
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, p, last[10];
struct node {
int x, y;
};
int s[10];
char a[1005][1005];
int vis[1005][1005], ans[10], can[10];
queue<node> que[10];
int d[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void bfs() {
for (int i = 1; i <= p; i++) {
while (!que[i].empty()) que[i].pop();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] != '.' && a[i][j] != '#') {
que[a[i][j] - '0'].push({i, j});
vis[i][j] = a[i][j] - '0';
last[a[i][j] - '0']++;
}
}
}
bool flag = true;
while (flag) {
flag = false;
for (int idx = 1; idx <= p; idx++) {
for (int ss = 1; ss <= s[idx] && can[idx] == 0; ss++) {
int num = last[idx];
last[idx] = 0;
for (int t = 1; t <= num; t++) {
node now = que[idx].front();
que[idx].pop();
ans[idx]++;
for (int i = 0; i < 4; i++) {
int xx = now.x + d[i][0], yy = now.y + d[i][1];
if (xx >= 0 && xx < n && yy >= 0 && yy < m && vis[xx][yy] == 0 &&
a[xx][yy] == '.') {
que[idx].push({xx, yy});
vis[xx][yy] = idx;
last[idx]++;
}
}
}
if (que[idx].empty()) can[idx] = idx;
}
}
for (int i = 1; i <= p; i++)
if (!que[i].empty()) flag = true;
}
for (int i = 1; i <= p; i++) printf("%d ", ans[i]);
printf("\n");
}
int main() {
while (scanf("%d %d %d", &n, &m, &p) != EOF) {
memset(a, 0, sizeof(a));
memset(s, 0, sizeof(s));
memset(ans, 0, sizeof(ans));
memset(vis, 0, sizeof(vis));
memset(last, 0, sizeof(last));
memset(can, 0, sizeof(can));
for (int i = 1; i <= p; i++) scanf("%d", &s[i]);
getchar();
for (int i = 0; i < n; i++) scanf("%s", a[i]);
bfs();
}
return 0;
}
``` |
#include <bits/stdc++.h>
char board[1010][1010];
int stepb[1010][1010];
int speed[10];
int delta[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
int nextVisit[10][1010 * 1010];
int nnVisit[10][1010 * 1010];
int nextIdx[10];
int nnIdx[10];
int N, M, P;
int check(int x, int y) { return x >= 0 && y >= 0 && x < N && y < M; }
int main() {
scanf("%d %d %d", &N, &M, &P);
for (int i = 0; i < P; i++) scanf("%d", &speed[i]);
for (int i = 0; i < N; i++) scanf("%s", board[i]);
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cnt += (board[i][j] == '.');
if (board[i][j] >= '1' && board[i][j] <= '9') {
board[i][j]--;
int p = board[i][j] - '0';
nextVisit[p][nextIdx[p]++] = i * 1010 + j;
stepb[i][j] = 0;
} else
stepb[i][j] = -1;
}
}
while (cnt) {
int prevcnt = cnt;
for (int p = 0; p < P; p++) {
for (int ii = 0; ii < nnIdx[p]; ii++) {
int xy = nnVisit[p][ii];
int i = xy / 1010;
int j = xy % 1010;
stepb[i][j] = 0;
nextVisit[p][nextIdx[p]++] = i * 1010 + j;
board[i][j] = '0' + p;
}
nnIdx[p] = 0;
}
for (int p = 0; p < P; p++) {
int rdptr = 0;
int wrptr = nextIdx[p];
while (rdptr < wrptr) {
int current = nextVisit[p][rdptr++];
int x = current / 1010;
int y = current % 1010;
int step = stepb[x][y];
if (step == speed[p]) continue;
for (int k = 0; k < 4; k++) {
int dx = x + delta[k][0];
int dy = y + delta[k][1];
if (!check(dx, dy)) continue;
if (board[dx][dy] != '.') continue;
cnt--;
nnVisit[p][nnIdx[p]++] = dx * 1010 + dy;
board[dx][dy] = 'a' + p;
stepb[dx][dy] = step + 1;
nextVisit[p][wrptr++] = dx * 1010 + dy;
}
}
}
for (int i = 0; i < P; i++) nextIdx[i] = 0;
if (prevcnt == cnt) break;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (board[i][j] >= 'a') {
board[i][j] -= 'a';
board[i][j] += '0';
}
}
}
for (int i = 0; i < P; i++) speed[i] = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) speed[board[i][j] - '0']++;
}
for (int i = 0; i < P; i++) printf("%d ", speed[i]);
printf("\n");
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
char board[1010][1010];
int stepb[1010][1010];
int speed[10];
int delta[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
int nextVisit[10][1010 * 1010];
int nnVisit[10][1010 * 1010];
int nextIdx[10];
int nnIdx[10];
int N, M, P;
int check(int x, int y) { return x >= 0 && y >= 0 && x < N && y < M; }
int main() {
scanf("%d %d %d", &N, &M, &P);
for (int i = 0; i < P; i++) scanf("%d", &speed[i]);
for (int i = 0; i < N; i++) scanf("%s", board[i]);
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cnt += (board[i][j] == '.');
if (board[i][j] >= '1' && board[i][j] <= '9') {
board[i][j]--;
int p = board[i][j] - '0';
nextVisit[p][nextIdx[p]++] = i * 1010 + j;
stepb[i][j] = 0;
} else
stepb[i][j] = -1;
}
}
while (cnt) {
int prevcnt = cnt;
for (int p = 0; p < P; p++) {
for (int ii = 0; ii < nnIdx[p]; ii++) {
int xy = nnVisit[p][ii];
int i = xy / 1010;
int j = xy % 1010;
stepb[i][j] = 0;
nextVisit[p][nextIdx[p]++] = i * 1010 + j;
board[i][j] = '0' + p;
}
nnIdx[p] = 0;
}
for (int p = 0; p < P; p++) {
int rdptr = 0;
int wrptr = nextIdx[p];
while (rdptr < wrptr) {
int current = nextVisit[p][rdptr++];
int x = current / 1010;
int y = current % 1010;
int step = stepb[x][y];
if (step == speed[p]) continue;
for (int k = 0; k < 4; k++) {
int dx = x + delta[k][0];
int dy = y + delta[k][1];
if (!check(dx, dy)) continue;
if (board[dx][dy] != '.') continue;
cnt--;
nnVisit[p][nnIdx[p]++] = dx * 1010 + dy;
board[dx][dy] = 'a' + p;
stepb[dx][dy] = step + 1;
nextVisit[p][wrptr++] = dx * 1010 + dy;
}
}
}
for (int i = 0; i < P; i++) nextIdx[i] = 0;
if (prevcnt == cnt) break;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (board[i][j] >= 'a') {
board[i][j] -= 'a';
board[i][j] += '0';
}
}
}
for (int i = 0; i < P; i++) speed[i] = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) speed[board[i][j] - '0']++;
}
for (int i = 0; i < P; i++) printf("%d ", speed[i]);
printf("\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double EPS = (double)1e-9;
const double PI = (double)acos(-1.0);
int irand(int lo, int hi) {
return (((double)rand()) / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;
}
string toString(long long x) {
stringstream ss;
ss << x;
return ss.str();
}
long long toNumber(string S) {
long long ret;
sscanf(S.c_str(), "%lld", &ret);
return ret;
}
const int INF = (int)2e9;
const long long MOD = (long long)1e9 + 7;
const int mx[] = {-1, 0, 1, 0};
const int my[] = {0, -1, 0, 1};
char s[1005][1005];
queue<pair<int, int> > bfs[15];
int main() {
for (int i = 0; i < 13; i++)
while (!bfs[i].empty()) bfs[i].pop();
int n, m, p;
scanf("%d %d %d", &n, &m, &p);
int speed[15];
for (int i = 1; i <= p; i += 1) scanf("%d", speed + i);
for (int i = 1; i <= n; i += 1) scanf("%s", s[i] + 1);
for (int i = 0; i <= n + 1; i += 1) s[i][0] = s[i][m + 1] = '#';
for (int j = 0; j <= m + 1; j += 1) s[0][j] = s[n + 1][j] = '#';
for (int i = 1; i <= n; i += 1)
for (int j = 1; j <= m; j += 1)
for (int k = 1; k <= p; k += 1) {
if (s[i][j] == '0' + k) bfs[k].push(make_pair(i, j));
}
int res[15];
for (int i = 0; i < 13; i++) res[i] = 0;
for (int flag = 1; flag;) {
flag = 0;
for (int i = 1; i <= p; i += 1)
for (int _speed = 0; _speed < speed[i]; _speed++) {
int sz = (int)bfs[i].size();
if (sz == 0) break;
for (int step = 0; step < sz; step++) {
pair<int, int> pos = bfs[i].front();
bfs[i].pop();
res[i]++;
for (int j = 0; j < 4; j++) {
pair<int, int> pot =
make_pair(pos.first + mx[j], pos.second + my[j]);
if (s[pot.first][pot.second] != '.') continue;
s[pot.first][pot.second] = '0' + i;
bfs[i].push(pot);
}
}
flag = 1;
}
}
for (int i = 1; i <= p; i += 1) printf("%d%c", res[i], " \n"[i == p]);
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double EPS = (double)1e-9;
const double PI = (double)acos(-1.0);
int irand(int lo, int hi) {
return (((double)rand()) / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;
}
string toString(long long x) {
stringstream ss;
ss << x;
return ss.str();
}
long long toNumber(string S) {
long long ret;
sscanf(S.c_str(), "%lld", &ret);
return ret;
}
const int INF = (int)2e9;
const long long MOD = (long long)1e9 + 7;
const int mx[] = {-1, 0, 1, 0};
const int my[] = {0, -1, 0, 1};
char s[1005][1005];
queue<pair<int, int> > bfs[15];
int main() {
for (int i = 0; i < 13; i++)
while (!bfs[i].empty()) bfs[i].pop();
int n, m, p;
scanf("%d %d %d", &n, &m, &p);
int speed[15];
for (int i = 1; i <= p; i += 1) scanf("%d", speed + i);
for (int i = 1; i <= n; i += 1) scanf("%s", s[i] + 1);
for (int i = 0; i <= n + 1; i += 1) s[i][0] = s[i][m + 1] = '#';
for (int j = 0; j <= m + 1; j += 1) s[0][j] = s[n + 1][j] = '#';
for (int i = 1; i <= n; i += 1)
for (int j = 1; j <= m; j += 1)
for (int k = 1; k <= p; k += 1) {
if (s[i][j] == '0' + k) bfs[k].push(make_pair(i, j));
}
int res[15];
for (int i = 0; i < 13; i++) res[i] = 0;
for (int flag = 1; flag;) {
flag = 0;
for (int i = 1; i <= p; i += 1)
for (int _speed = 0; _speed < speed[i]; _speed++) {
int sz = (int)bfs[i].size();
if (sz == 0) break;
for (int step = 0; step < sz; step++) {
pair<int, int> pos = bfs[i].front();
bfs[i].pop();
res[i]++;
for (int j = 0; j < 4; j++) {
pair<int, int> pot =
make_pair(pos.first + mx[j], pos.second + my[j]);
if (s[pot.first][pot.second] != '.') continue;
s[pot.first][pot.second] = '0' + i;
bfs[i].push(pot);
}
}
flag = 1;
}
}
for (int i = 1; i <= p; i += 1) printf("%d%c", res[i], " \n"[i == p]);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
char G[maxn][maxn];
bool vis[maxn][maxn];
int speed[10];
int ans[10];
int n, m, p;
void Init() {
memset(G, 0, sizeof(G));
memset(vis, 0, sizeof(vis));
}
struct point {
int x, y;
point() {}
point(int x, int y) : x(x), y(y) {}
};
bool check(point p) {
if (p.x < 1 || p.x > n || p.y < 1 || p.y > m || vis[p.x][p.y] ||
G[p.x][p.y] == '#')
return 0;
return 1;
}
queue<point> q[10];
bool ifContinue() {
for (int i = 1; i <= p; ++i) {
if (!q[i].empty()) return 1;
}
return 0;
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
cin >> n >> m >> p;
Init();
for (int i = 1; i <= p; ++i) {
cin >> speed[i];
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> G[i][j];
if (G[i][j] != '.' && G[i][j] != '#') {
int no = G[i][j] - '0';
vis[i][j] = 1;
ans[no]++;
q[no].push(point(i, j));
}
}
}
while (ifContinue()) {
for (int i = 1; i <= p; ++i) {
if (q[i].empty()) continue;
for (int j = 0; j < speed[i]; ++j) {
if (q[i].empty()) break;
int len = q[i].size();
for (int k = 0; k < len; ++k) {
point f = q[i].front();
q[i].pop();
for (int l = 0; l < 4; ++l) {
point t = point(f.x + dx[l], f.y + dy[l]);
if (check(t)) {
ans[i]++;
vis[t.x][t.y] = 1;
q[i].push(t);
}
}
}
}
}
}
for (int i = 1; i <= p; ++i) cout << ans[i] << " ";
cout << endl;
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
char G[maxn][maxn];
bool vis[maxn][maxn];
int speed[10];
int ans[10];
int n, m, p;
void Init() {
memset(G, 0, sizeof(G));
memset(vis, 0, sizeof(vis));
}
struct point {
int x, y;
point() {}
point(int x, int y) : x(x), y(y) {}
};
bool check(point p) {
if (p.x < 1 || p.x > n || p.y < 1 || p.y > m || vis[p.x][p.y] ||
G[p.x][p.y] == '#')
return 0;
return 1;
}
queue<point> q[10];
bool ifContinue() {
for (int i = 1; i <= p; ++i) {
if (!q[i].empty()) return 1;
}
return 0;
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
cin >> n >> m >> p;
Init();
for (int i = 1; i <= p; ++i) {
cin >> speed[i];
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> G[i][j];
if (G[i][j] != '.' && G[i][j] != '#') {
int no = G[i][j] - '0';
vis[i][j] = 1;
ans[no]++;
q[no].push(point(i, j));
}
}
}
while (ifContinue()) {
for (int i = 1; i <= p; ++i) {
if (q[i].empty()) continue;
for (int j = 0; j < speed[i]; ++j) {
if (q[i].empty()) break;
int len = q[i].size();
for (int k = 0; k < len; ++k) {
point f = q[i].front();
q[i].pop();
for (int l = 0; l < 4; ++l) {
point t = point(f.x + dx[l], f.y + dy[l]);
if (check(t)) {
ans[i]++;
vis[t.x][t.y] = 1;
q[i].push(t);
}
}
}
}
}
}
for (int i = 1; i <= p; ++i) cout << ans[i] << " ";
cout << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, p, vis;
int e[1001][1001], s[1001], ans[10];
int nxt[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
char ch;
inline bool check(int x, int y) {
return (x < 1 || x > n || y < 1 || y > m || e[x][y] != 0) ? 0 : 1;
}
struct node {
int x, y;
node() {}
node(int nx, int ny) { x = nx, y = ny; }
};
queue<node> q[10];
void nbfs(int t) {
int nvis, cnt;
for (int i = 1; i <= s[t]; i++) {
cnt = q[t].size();
nvis = vis;
while (cnt--) {
node nt = q[t].front();
q[t].pop();
for (int k = 0; k < 4; k++) {
int nx = nt.x + nxt[k][0], ny = nt.y + nxt[k][1];
if (!check(nx, ny)) continue;
vis++, e[nx][ny] = 1, ans[t]++;
q[t].push(node(nx, ny));
}
}
if (vis == nvis) break;
}
}
void bfs() {
if (vis == n * m) return;
int nvis = vis;
for (int i = 1; i <= p; i++)
if (ans[i]) nbfs(i);
if (nvis == vis) return;
bfs();
}
int main() {
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= p; i++) scanf("%d", &s[i]), s[i] = min(max(m, n), s[i]);
for (int i = 1; i <= n; i++) {
scanf("\n");
for (int j = 1; j <= m; j++) {
scanf("%c", &ch);
if (isdigit(ch)) {
e[i][j] = 1, ans[ch - '0']++, vis++;
q[ch - '0'].push(node(i, j));
}
if (ch == '#') e[i][j] = -1, vis++;
}
}
bfs();
for (int i = 1; i <= p; i++) printf("%d ", ans[i]);
;
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, p, vis;
int e[1001][1001], s[1001], ans[10];
int nxt[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
char ch;
inline bool check(int x, int y) {
return (x < 1 || x > n || y < 1 || y > m || e[x][y] != 0) ? 0 : 1;
}
struct node {
int x, y;
node() {}
node(int nx, int ny) { x = nx, y = ny; }
};
queue<node> q[10];
void nbfs(int t) {
int nvis, cnt;
for (int i = 1; i <= s[t]; i++) {
cnt = q[t].size();
nvis = vis;
while (cnt--) {
node nt = q[t].front();
q[t].pop();
for (int k = 0; k < 4; k++) {
int nx = nt.x + nxt[k][0], ny = nt.y + nxt[k][1];
if (!check(nx, ny)) continue;
vis++, e[nx][ny] = 1, ans[t]++;
q[t].push(node(nx, ny));
}
}
if (vis == nvis) break;
}
}
void bfs() {
if (vis == n * m) return;
int nvis = vis;
for (int i = 1; i <= p; i++)
if (ans[i]) nbfs(i);
if (nvis == vis) return;
bfs();
}
int main() {
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= p; i++) scanf("%d", &s[i]), s[i] = min(max(m, n), s[i]);
for (int i = 1; i <= n; i++) {
scanf("\n");
for (int j = 1; j <= m; j++) {
scanf("%c", &ch);
if (isdigit(ch)) {
e[i][j] = 1, ans[ch - '0']++, vis++;
q[ch - '0'].push(node(i, j));
}
if (ch == '#') e[i][j] = -1, vis++;
}
}
bfs();
for (int i = 1; i <= p; i++) printf("%d ", ans[i]);
;
return 0;
}
``` |
#include <bits/stdc++.h>
inline int sbt(int x) { return __builtin_popcount(x); }
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
char a[1013][1013];
bool vis[1013][1013];
int res[13];
int dx[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
int n, m, p;
cin >> n >> m >> p;
int s[13];
for (int i = 1; i <= p; i++) {
cin >> s[i];
}
queue<pair<int, pair<int, int>>> q[13];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] != '#' && a[i][j] != '.') {
q[a[i][j] - '0'].push(make_pair(a[i][j] - '0', make_pair(i, j)));
}
}
}
int curr = 1;
int ok = 1;
while (ok) {
ok = 0;
for (int i = 1; i <= p; i++) {
int steps = s[i];
int kuch_ho_rha_hai = 1;
while (steps && kuch_ho_rha_hai) {
steps--;
kuch_ho_rha_hai = 0;
queue<pair<int, pair<int, int>>> tmp;
while (!q[i].empty()) {
tmp.push(q[i].front());
q[i].pop();
}
while (!tmp.empty()) {
auto p = tmp.front();
tmp.pop();
for (int ii = 0; ii <= 3; ii++) {
int dxx = dx[ii][0] + p.second.first;
int dyy = dx[ii][1] + p.second.second;
int steps = s[i];
if (dxx >= 1 && dyy >= 1 && dxx <= n && dyy <= m &&
a[dxx][dyy] == '.') {
a[dxx][dyy] = char(i + 48);
q[i].push(make_pair(i, make_pair(dxx, dyy)));
kuch_ho_rha_hai++;
ok++;
}
}
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] != '#' && a[i][j] != '.') res[a[i][j] - '0']++;
}
}
for (int i = 1; i <= p; i++) {
cout << res[i] << ' ';
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
inline int sbt(int x) { return __builtin_popcount(x); }
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
char a[1013][1013];
bool vis[1013][1013];
int res[13];
int dx[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
int n, m, p;
cin >> n >> m >> p;
int s[13];
for (int i = 1; i <= p; i++) {
cin >> s[i];
}
queue<pair<int, pair<int, int>>> q[13];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] != '#' && a[i][j] != '.') {
q[a[i][j] - '0'].push(make_pair(a[i][j] - '0', make_pair(i, j)));
}
}
}
int curr = 1;
int ok = 1;
while (ok) {
ok = 0;
for (int i = 1; i <= p; i++) {
int steps = s[i];
int kuch_ho_rha_hai = 1;
while (steps && kuch_ho_rha_hai) {
steps--;
kuch_ho_rha_hai = 0;
queue<pair<int, pair<int, int>>> tmp;
while (!q[i].empty()) {
tmp.push(q[i].front());
q[i].pop();
}
while (!tmp.empty()) {
auto p = tmp.front();
tmp.pop();
for (int ii = 0; ii <= 3; ii++) {
int dxx = dx[ii][0] + p.second.first;
int dyy = dx[ii][1] + p.second.second;
int steps = s[i];
if (dxx >= 1 && dyy >= 1 && dxx <= n && dyy <= m &&
a[dxx][dyy] == '.') {
a[dxx][dyy] = char(i + 48);
q[i].push(make_pair(i, make_pair(dxx, dyy)));
kuch_ho_rha_hai++;
ok++;
}
}
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] != '#' && a[i][j] != '.') res[a[i][j] - '0']++;
}
}
for (int i = 1; i <= p; i++) {
cout << res[i] << ' ';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, p;
int s[10];
char g[N][N];
struct Node {
int x, y;
int rest;
int round;
};
queue<Node> q[10];
int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int ans[10];
int main() {
scanf("%d %d %d", &n, &m, &p);
for (int i = 1; i <= p; i++) scanf("%d", &s[i]);
for (int i = 1; i <= n; i++) scanf("%s", g[i] + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (g[i][j] != '.' && g[i][j] != '#')
q[g[i][j] - '0'].push((Node){i, j, 0, 0});
int r = 0;
while (1) {
r++;
bool flag = 0;
for (int i = 1; i <= p; i++) {
if (q[i].empty()) continue;
while (!q[i].empty()) {
Node tmp = q[i].front();
if (tmp.rest == 0 && tmp.round == r) break;
q[i].pop();
int x = tmp.x, y = tmp.y,
step = tmp.rest == 0 ? s[i] - 1 : tmp.rest - 1, round = r;
for (int k = 0; k < 4; k++) {
int xx = x + d[k][0], yy = y + d[k][1];
if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
if (g[xx][yy] != '.') continue;
g[xx][yy] = '0' + i;
q[i].push((Node){xx, yy, step, round});
flag = 1;
}
}
}
if (flag == 0) break;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (g[i][j] >= '1' && g[i][j] <= '9') ans[g[i][j] - '0']++;
for (int i = 1; i <= p; i++) printf("%d ", ans[i]);
}
| ### Prompt
Develop a solution in CPP to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, p;
int s[10];
char g[N][N];
struct Node {
int x, y;
int rest;
int round;
};
queue<Node> q[10];
int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int ans[10];
int main() {
scanf("%d %d %d", &n, &m, &p);
for (int i = 1; i <= p; i++) scanf("%d", &s[i]);
for (int i = 1; i <= n; i++) scanf("%s", g[i] + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (g[i][j] != '.' && g[i][j] != '#')
q[g[i][j] - '0'].push((Node){i, j, 0, 0});
int r = 0;
while (1) {
r++;
bool flag = 0;
for (int i = 1; i <= p; i++) {
if (q[i].empty()) continue;
while (!q[i].empty()) {
Node tmp = q[i].front();
if (tmp.rest == 0 && tmp.round == r) break;
q[i].pop();
int x = tmp.x, y = tmp.y,
step = tmp.rest == 0 ? s[i] - 1 : tmp.rest - 1, round = r;
for (int k = 0; k < 4; k++) {
int xx = x + d[k][0], yy = y + d[k][1];
if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
if (g[xx][yy] != '.') continue;
g[xx][yy] = '0' + i;
q[i].push((Node){xx, yy, step, round});
flag = 1;
}
}
}
if (flag == 0) break;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (g[i][j] >= '1' && g[i][j] <= '9') ans[g[i][j] - '0']++;
for (int i = 1; i <= p; i++) printf("%d ", ans[i]);
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
char g[1000][1000];
long long p;
long long s[10], ans[10];
pair<long long, long long> d[4] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
long long cnt = 0;
long long rnd = 0;
queue<pair<long long, pair<long long, long long>>> q[10];
void bfs(long long pl) {
while (!q[pl].empty()) {
pair<long long, long long> x = q[pl].front().second;
long long dist = q[pl].front().first;
if (dist == (rnd + 1) * s[pl]) {
break;
}
q[pl].pop();
for (auto del : d) {
if (x.first + del.first >= n || x.first + del.first < 0 ||
x.second + del.second >= m || x.second + del.second < 0)
continue;
if (g[x.first + del.first][x.second + del.second] == '.') {
g[x.first + del.first][x.second + del.second] = pl + '0';
cnt--;
q[pl].push({dist + 1, {x.first + del.first, x.second + del.second}});
}
}
}
}
void display() {
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
cout << g[i][j] << ' ';
}
cout << endl;
}
cout << endl;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m >> p;
for (long long i = 0; i < p; ++i) {
cin >> s[i + 1];
}
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
cin >> g[i][j];
if (g[i][j] != '.' && g[i][j] != '#') {
q[g[i][j] - '0'].push({0, {i, j}});
}
if (g[i][j] == '.') cnt++;
}
}
long long i = 1;
long long streak = 0;
while (cnt > 0) {
long long init = cnt;
bfs(i);
if (cnt == init)
streak++;
else
streak = 0;
i = i % p + 1;
if (i == 1) rnd++;
if (streak == p) break;
}
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
if (g[i][j] != '.' && g[i][j] != '#') {
ans[g[i][j] - '0']++;
}
}
}
for (long long i = 1; i <= p; ++i) cout << ans[i] << ' ';
}
| ### Prompt
In CPP, your task is to solve the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, m;
char g[1000][1000];
long long p;
long long s[10], ans[10];
pair<long long, long long> d[4] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
long long cnt = 0;
long long rnd = 0;
queue<pair<long long, pair<long long, long long>>> q[10];
void bfs(long long pl) {
while (!q[pl].empty()) {
pair<long long, long long> x = q[pl].front().second;
long long dist = q[pl].front().first;
if (dist == (rnd + 1) * s[pl]) {
break;
}
q[pl].pop();
for (auto del : d) {
if (x.first + del.first >= n || x.first + del.first < 0 ||
x.second + del.second >= m || x.second + del.second < 0)
continue;
if (g[x.first + del.first][x.second + del.second] == '.') {
g[x.first + del.first][x.second + del.second] = pl + '0';
cnt--;
q[pl].push({dist + 1, {x.first + del.first, x.second + del.second}});
}
}
}
}
void display() {
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
cout << g[i][j] << ' ';
}
cout << endl;
}
cout << endl;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m >> p;
for (long long i = 0; i < p; ++i) {
cin >> s[i + 1];
}
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
cin >> g[i][j];
if (g[i][j] != '.' && g[i][j] != '#') {
q[g[i][j] - '0'].push({0, {i, j}});
}
if (g[i][j] == '.') cnt++;
}
}
long long i = 1;
long long streak = 0;
while (cnt > 0) {
long long init = cnt;
bfs(i);
if (cnt == init)
streak++;
else
streak = 0;
i = i % p + 1;
if (i == 1) rnd++;
if (streak == p) break;
}
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
if (g[i][j] != '.' && g[i][j] != '#') {
ans[g[i][j] - '0']++;
}
}
}
for (long long i = 1; i <= p; ++i) cout << ans[i] << ' ';
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline void chkmax(int &x, int y) { x < y ? (x = y) : 0; }
inline void chkmin(int &x, int y) { x > y ? (x = y) : 0; }
int n, m, p;
struct node {
int x, y;
};
const int N = 1005;
queue<node> q[10], ex[10];
char sq[N][N];
int vis[N][N], s[15], cnt[10];
const int dx[] = {1, 0, 0, -1}, dy[] = {0, -1, 1, 0};
void file() {}
inline int read() {
int x = 0, f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 1) + (x << 3) + ch - '0';
return x * f;
}
void bfs(int u) {
while (ex[u].size()) ex[u].pop();
while (!q[u].empty()) {
int nowx = q[u].front().x;
int nowy = q[u].front().y;
q[u].pop();
for (int i = 0; i < 4; i++) {
int nx = nowx + dx[i], ny = nowy + dy[i];
if (nx > 0 && nx <= n && ny > 0 && ny <= m && !vis[nx][ny] &&
sq[nx][ny] != '#') {
vis[nx][ny] = u;
ex[u].push((node){nx, ny});
}
}
}
q[u] = ex[u];
}
int main() {
n = read();
m = read();
p = read();
for (int i = 1; i <= p; i++) s[i] = read();
for (int i = 1; i <= n; i++) {
cin >> sq[i] + 1;
for (int j = 1; j <= m; j++)
if (sq[i][j] >= '0' && sq[i][j] <= '9') {
q[sq[i][j] - '0'].push((node){i, j});
vis[i][j] = sq[i][j] - '0';
}
}
bool flag = true;
while (flag) {
flag = false;
for (int i = 1; i <= p; i++)
for (int j = 1; j <= s[i]; j++)
if (q[i].size())
bfs(i), flag = true;
else
break;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cnt[vis[i][j]]++;
for (int i = 1; i <= p; i++) printf("%d ", cnt[i]);
puts("");
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline void chkmax(int &x, int y) { x < y ? (x = y) : 0; }
inline void chkmin(int &x, int y) { x > y ? (x = y) : 0; }
int n, m, p;
struct node {
int x, y;
};
const int N = 1005;
queue<node> q[10], ex[10];
char sq[N][N];
int vis[N][N], s[15], cnt[10];
const int dx[] = {1, 0, 0, -1}, dy[] = {0, -1, 1, 0};
void file() {}
inline int read() {
int x = 0, f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 1) + (x << 3) + ch - '0';
return x * f;
}
void bfs(int u) {
while (ex[u].size()) ex[u].pop();
while (!q[u].empty()) {
int nowx = q[u].front().x;
int nowy = q[u].front().y;
q[u].pop();
for (int i = 0; i < 4; i++) {
int nx = nowx + dx[i], ny = nowy + dy[i];
if (nx > 0 && nx <= n && ny > 0 && ny <= m && !vis[nx][ny] &&
sq[nx][ny] != '#') {
vis[nx][ny] = u;
ex[u].push((node){nx, ny});
}
}
}
q[u] = ex[u];
}
int main() {
n = read();
m = read();
p = read();
for (int i = 1; i <= p; i++) s[i] = read();
for (int i = 1; i <= n; i++) {
cin >> sq[i] + 1;
for (int j = 1; j <= m; j++)
if (sq[i][j] >= '0' && sq[i][j] <= '9') {
q[sq[i][j] - '0'].push((node){i, j});
vis[i][j] = sq[i][j] - '0';
}
}
bool flag = true;
while (flag) {
flag = false;
for (int i = 1; i <= p; i++)
for (int j = 1; j <= s[i]; j++)
if (q[i].size())
bfs(i), flag = true;
else
break;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cnt[vis[i][j]]++;
for (int i = 1; i <= p; i++) printf("%d ", cnt[i]);
puts("");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m, p;
cin >> n >> m >> p;
int movement[10];
for (int i = 1; i <= p; ++i) {
cin >> movement[i];
}
static char arr[1001][1001];
static int freq[10];
queue<pair<int, int>> q[10];
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
char c;
cin >> c;
if (isdigit(c)) {
int p = c - '0';
q[p].push({i, j});
++freq[p];
arr[i][j] = p;
} else {
arr[i][j] = c;
}
}
cin.ignore();
}
bool is_modified;
do {
is_modified = false;
for (int i = 1; i <= p; ++i) {
for (int mv = movement[i]; !q[i].empty() && mv; --mv) {
int cnt = q[i].size();
while (cnt--) {
auto cur = q[i].front();
q[i].pop();
for (int d = 0; d < 4; ++d) {
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
int x = cur.first + dx[d];
int y = cur.second + dy[d];
if (x < 1 || x > n) {
continue;
}
if (y < 1 || y > m) {
continue;
}
if (arr[x][y] != '.') {
continue;
}
is_modified = true;
arr[x][y] = i;
q[i].push({x, y});
++freq[i];
}
}
}
}
} while (is_modified);
for (int i = 1; i <= p; ++i) {
cout << freq[i] << ' ';
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m, p;
cin >> n >> m >> p;
int movement[10];
for (int i = 1; i <= p; ++i) {
cin >> movement[i];
}
static char arr[1001][1001];
static int freq[10];
queue<pair<int, int>> q[10];
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
char c;
cin >> c;
if (isdigit(c)) {
int p = c - '0';
q[p].push({i, j});
++freq[p];
arr[i][j] = p;
} else {
arr[i][j] = c;
}
}
cin.ignore();
}
bool is_modified;
do {
is_modified = false;
for (int i = 1; i <= p; ++i) {
for (int mv = movement[i]; !q[i].empty() && mv; --mv) {
int cnt = q[i].size();
while (cnt--) {
auto cur = q[i].front();
q[i].pop();
for (int d = 0; d < 4; ++d) {
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
int x = cur.first + dx[d];
int y = cur.second + dy[d];
if (x < 1 || x > n) {
continue;
}
if (y < 1 || y > m) {
continue;
}
if (arr[x][y] != '.') {
continue;
}
is_modified = true;
arr[x][y] = i;
q[i].push({x, y});
++freq[i];
}
}
}
}
} while (is_modified);
for (int i = 1; i <= p; ++i) {
cout << freq[i] << ' ';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
queue<pair<int, int> > que[9];
int n, m, p;
int s[N];
string c[N];
int dis[9][N][N];
bool vis[N][N];
bool bk[N][N];
int bel[N][N];
int cnt[9];
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, 1, -1};
bool good(int i, int j) {
return i >= 0 && i < n && j >= 0 && j < m && !vis[i][j] && !bk[i][j];
}
int main() {
memset(dis, 0x3f, sizeof(dis));
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> p;
for (int i = 0; i < p; i++) cin >> s[i];
for (int i = 0; i < n; i++) cin >> c[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (c[i][j] >= '1' && c[i][j] <= '9') {
int p = c[i][j] - '1';
que[p].push({i, j});
dis[p][i][j] = 0;
bk[i][j] = true;
} else if (c[i][j] == '#')
vis[i][j] = true;
}
}
for (int round = 1; round < N * N; round++) {
for (int i = 0; i < p; i++) {
int mxdis = int(min((1LL * round * s[i]), (long long)1e9));
while (!que[i].empty()) {
auto p = que[i].front();
if (dis[i][p.first][p.second] > mxdis) break;
que[i].pop();
if (vis[p.first][p.second]) continue;
vis[p.first][p.second] = true;
cnt[i]++;
for (int d = 0; d < 4; d++) {
int nx = p.first + dx[d], ny = p.second + dy[d];
if (good(nx, ny) && dis[i][nx][ny] > dis[i][p.first][p.second] + 1) {
dis[i][nx][ny] = dis[i][p.first][p.second] + 1;
que[i].push({nx, ny});
}
}
}
}
}
for (int i = 0; i < p; i++) cout << cnt[i] << " ";
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
queue<pair<int, int> > que[9];
int n, m, p;
int s[N];
string c[N];
int dis[9][N][N];
bool vis[N][N];
bool bk[N][N];
int bel[N][N];
int cnt[9];
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, 1, -1};
bool good(int i, int j) {
return i >= 0 && i < n && j >= 0 && j < m && !vis[i][j] && !bk[i][j];
}
int main() {
memset(dis, 0x3f, sizeof(dis));
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> p;
for (int i = 0; i < p; i++) cin >> s[i];
for (int i = 0; i < n; i++) cin >> c[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (c[i][j] >= '1' && c[i][j] <= '9') {
int p = c[i][j] - '1';
que[p].push({i, j});
dis[p][i][j] = 0;
bk[i][j] = true;
} else if (c[i][j] == '#')
vis[i][j] = true;
}
}
for (int round = 1; round < N * N; round++) {
for (int i = 0; i < p; i++) {
int mxdis = int(min((1LL * round * s[i]), (long long)1e9));
while (!que[i].empty()) {
auto p = que[i].front();
if (dis[i][p.first][p.second] > mxdis) break;
que[i].pop();
if (vis[p.first][p.second]) continue;
vis[p.first][p.second] = true;
cnt[i]++;
for (int d = 0; d < 4; d++) {
int nx = p.first + dx[d], ny = p.second + dy[d];
if (good(nx, ny) && dis[i][nx][ny] > dis[i][p.first][p.second] + 1) {
dis[i][nx][ny] = dis[i][p.first][p.second] + 1;
que[i].push({nx, ny});
}
}
}
}
}
for (int i = 0; i < p; i++) cout << cnt[i] << " ";
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct player {
long long int x, y, num;
player(long long int xp, long long int yp, long long int nump) {
x = xp, y = yp, num = nump;
}
};
const long long int M = 17;
const long long int N = 1007;
long long int second[M], ans[M];
long long int vis[N][N];
queue<player> q[17];
long long int dx[] = {1, 0, -1, 0};
long long int dy[] = {0, 1, 0, -1};
bool isok(long long int x, long long int y, long long int n, long long int m) {
if (vis[x][y]) return false;
if (x < 1 || x > n || y < 1 || y > m) return false;
return true;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, m, push, i, j, u, v;
char x;
cin >> n >> m >> push;
for (i = 1; i <= push; i++) cin >> second[i];
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) {
cin >> x;
if (x != '.') vis[i][j] = 1;
if (x == '#' || x == '.') continue;
long long int num = (long long int)x - '0';
player np(i, j, 0);
q[num].push(np);
ans[num]++;
}
long long int cnum = 1;
while (true) {
bool verif = 0;
for (i = 1; i <= push; i++) {
while (!q[i].empty() && q[i].front().num < cnum * second[i]) {
player tp = q[i].front();
q[i].pop();
for (j = 0; j < 4; j++) {
player np(tp.x + dx[j], tp.y + dy[j], 1 + tp.num);
if (isok(np.x, np.y, n, m)) {
ans[i]++;
q[i].push(np);
vis[np.x][np.y] = 1;
verif = 1;
}
}
}
}
if (!verif) break;
cnum++;
}
for (i = 1; i <= push; i++) cout << ans[i] << " ";
cout << '\n';
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct player {
long long int x, y, num;
player(long long int xp, long long int yp, long long int nump) {
x = xp, y = yp, num = nump;
}
};
const long long int M = 17;
const long long int N = 1007;
long long int second[M], ans[M];
long long int vis[N][N];
queue<player> q[17];
long long int dx[] = {1, 0, -1, 0};
long long int dy[] = {0, 1, 0, -1};
bool isok(long long int x, long long int y, long long int n, long long int m) {
if (vis[x][y]) return false;
if (x < 1 || x > n || y < 1 || y > m) return false;
return true;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, m, push, i, j, u, v;
char x;
cin >> n >> m >> push;
for (i = 1; i <= push; i++) cin >> second[i];
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) {
cin >> x;
if (x != '.') vis[i][j] = 1;
if (x == '#' || x == '.') continue;
long long int num = (long long int)x - '0';
player np(i, j, 0);
q[num].push(np);
ans[num]++;
}
long long int cnum = 1;
while (true) {
bool verif = 0;
for (i = 1; i <= push; i++) {
while (!q[i].empty() && q[i].front().num < cnum * second[i]) {
player tp = q[i].front();
q[i].pop();
for (j = 0; j < 4; j++) {
player np(tp.x + dx[j], tp.y + dy[j], 1 + tp.num);
if (isok(np.x, np.y, n, m)) {
ans[i]++;
q[i].push(np);
vis[np.x][np.y] = 1;
verif = 1;
}
}
}
}
if (!verif) break;
cnum++;
}
for (i = 1; i <= push; i++) cout << ans[i] << " ";
cout << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const long long INF = 1e18;
const int MOD = 1e9 + 7;
const long long dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const long long Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
long long in() {
long long x;
cin >> x;
return x;
}
void print() { cout << "\n"; }
template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail) {
cout << head;
if (sizeof...(tail) != 0) cout << " ";
print(forward<Tail>(tail)...);
}
template <class T>
void print(vector<T> &vec) {
for (auto &a : vec) {
cout << a;
if (&a != &vec.back()) cout << " ";
}
cout << "\n";
}
template <class T>
void print(set<T> &set) {
for (auto &a : set) {
cout << a << " ";
}
cout << "\n";
}
template <class T>
void print(vector<vector<T>> &df) {
for (auto &vec : df) {
print(vec);
}
}
long long power(long long x, long long n) {
long long ret = 1;
while (n > 0) {
if (n & 1) ret *= x;
x *= x;
n >>= 1;
}
return ret;
}
long long comb(long long n, long long k) {
vector<vector<long long>> v(n + 1, vector<long long>(n + 1, 0));
for (long long i = 0; i < (long long)((v).size()); i++) {
v[i][0] = 1;
v[i][i] = 1;
}
for (long long k = 1; k < (long long)((v).size()); k++) {
for (long long j = 1; j < k; j++) {
v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]);
}
}
return v[n][k];
}
void add(long long &a, long long b) {
a += b;
if (a >= MOD) a -= MOD;
}
template <class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
long long ceildiv(long long a, long long b) {
if (b < 0) a = -a, b = -b;
if (a >= 0)
return (a + b - 1) / b;
else
return a / b;
}
long long floordiv(long long a, long long b) {
if (b < 0) a = -a, b = -b;
if (a >= 0)
return a / b;
else
return (a - b + 1) / b;
}
long long floorsqrt(long long x) {
assert(x >= 0);
long long ok = 0;
long long ng = 1;
while (ng * ng <= x) ng <<= 1;
while (ng - ok > 1) {
long long mid = (ng + ok) >> 1;
if (mid * mid <= x)
ok = mid;
else
ng = mid;
}
return ok;
}
template <class T>
bool contain(const std::string &s, const T &v) {
return s.find(v) != std::string::npos;
}
__attribute__((constructor)) void faster_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
long long n, m, p;
vector<string> fld;
vector<long long> s;
vector<vector<long long>> vis;
vector<queue<pair<pair<long long, long long>, long long>>> q(9);
void bfs() {
bool changed = true;
while (changed) {
changed = false;
for (long long ni = 0; ni < (long long)(9); ni++) {
while ((!q[ni].empty()) and (q[ni].front().second < s[ni])) {
auto current_pos = q[ni].front();
long long x = current_pos.first.first;
long long y = current_pos.first.second;
long long step = current_pos.second;
q[ni].pop();
for (long long dir = 0; dir < 4; ++dir) {
long long nx = x + dx[dir];
long long ny = y + dy[dir];
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if (fld[nx][ny] == '#') continue;
if (vis[nx][ny] == -1) {
q[ni].push(make_pair(make_pair(nx, ny), step + 1));
vis[nx][ny] = ni;
changed = true;
}
}
}
long long sz = (long long)((q[ni]).size());
vector<pair<long long, long long>> tmp;
for (long long si = 0; si < (long long)(sz); si++) {
auto current_pos = q[ni].front();
long long x = current_pos.first.first;
long long y = current_pos.first.second;
long long step = current_pos.second;
q[ni].pop();
tmp.push_back(make_pair(x, y));
}
for (auto i : tmp) {
q[ni].push(make_pair(i, 0LL));
}
}
}
}
signed main() {
cin >> n >> m >> p;
s.assign(p, 0);
vis.assign(n, vector<long long>(m, -1));
for (long long pi = 0; pi < (long long)(p); pi++) {
cin >> s[pi];
}
for (long long ni = 0; ni < (long long)(n); ni++) {
string ss;
cin >> ss;
fld.push_back(ss);
}
for (long long ni = 0; ni < (long long)(n); ni++) {
for (long long mi = 0; mi < (long long)(m); mi++) {
if (fld[ni][mi] != '.' and fld[ni][mi] != '#') {
q[fld[ni][mi] - '1'].push(make_pair(make_pair(ni, mi), 0LL));
vis[ni][mi] = fld[ni][mi] - '1';
}
}
}
bfs();
vector<long long> ans(p);
for (long long ni = 0; ni < (long long)(n); ni++) {
for (long long mi = 0; mi < (long long)(m); mi++) {
if (vis[ni][mi] != -1) ans[vis[ni][mi]]++;
}
}
print(ans);
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const long long INF = 1e18;
const int MOD = 1e9 + 7;
const long long dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const long long Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
long long in() {
long long x;
cin >> x;
return x;
}
void print() { cout << "\n"; }
template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail) {
cout << head;
if (sizeof...(tail) != 0) cout << " ";
print(forward<Tail>(tail)...);
}
template <class T>
void print(vector<T> &vec) {
for (auto &a : vec) {
cout << a;
if (&a != &vec.back()) cout << " ";
}
cout << "\n";
}
template <class T>
void print(set<T> &set) {
for (auto &a : set) {
cout << a << " ";
}
cout << "\n";
}
template <class T>
void print(vector<vector<T>> &df) {
for (auto &vec : df) {
print(vec);
}
}
long long power(long long x, long long n) {
long long ret = 1;
while (n > 0) {
if (n & 1) ret *= x;
x *= x;
n >>= 1;
}
return ret;
}
long long comb(long long n, long long k) {
vector<vector<long long>> v(n + 1, vector<long long>(n + 1, 0));
for (long long i = 0; i < (long long)((v).size()); i++) {
v[i][0] = 1;
v[i][i] = 1;
}
for (long long k = 1; k < (long long)((v).size()); k++) {
for (long long j = 1; j < k; j++) {
v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]);
}
}
return v[n][k];
}
void add(long long &a, long long b) {
a += b;
if (a >= MOD) a -= MOD;
}
template <class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
long long ceildiv(long long a, long long b) {
if (b < 0) a = -a, b = -b;
if (a >= 0)
return (a + b - 1) / b;
else
return a / b;
}
long long floordiv(long long a, long long b) {
if (b < 0) a = -a, b = -b;
if (a >= 0)
return a / b;
else
return (a - b + 1) / b;
}
long long floorsqrt(long long x) {
assert(x >= 0);
long long ok = 0;
long long ng = 1;
while (ng * ng <= x) ng <<= 1;
while (ng - ok > 1) {
long long mid = (ng + ok) >> 1;
if (mid * mid <= x)
ok = mid;
else
ng = mid;
}
return ok;
}
template <class T>
bool contain(const std::string &s, const T &v) {
return s.find(v) != std::string::npos;
}
__attribute__((constructor)) void faster_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
long long n, m, p;
vector<string> fld;
vector<long long> s;
vector<vector<long long>> vis;
vector<queue<pair<pair<long long, long long>, long long>>> q(9);
void bfs() {
bool changed = true;
while (changed) {
changed = false;
for (long long ni = 0; ni < (long long)(9); ni++) {
while ((!q[ni].empty()) and (q[ni].front().second < s[ni])) {
auto current_pos = q[ni].front();
long long x = current_pos.first.first;
long long y = current_pos.first.second;
long long step = current_pos.second;
q[ni].pop();
for (long long dir = 0; dir < 4; ++dir) {
long long nx = x + dx[dir];
long long ny = y + dy[dir];
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if (fld[nx][ny] == '#') continue;
if (vis[nx][ny] == -1) {
q[ni].push(make_pair(make_pair(nx, ny), step + 1));
vis[nx][ny] = ni;
changed = true;
}
}
}
long long sz = (long long)((q[ni]).size());
vector<pair<long long, long long>> tmp;
for (long long si = 0; si < (long long)(sz); si++) {
auto current_pos = q[ni].front();
long long x = current_pos.first.first;
long long y = current_pos.first.second;
long long step = current_pos.second;
q[ni].pop();
tmp.push_back(make_pair(x, y));
}
for (auto i : tmp) {
q[ni].push(make_pair(i, 0LL));
}
}
}
}
signed main() {
cin >> n >> m >> p;
s.assign(p, 0);
vis.assign(n, vector<long long>(m, -1));
for (long long pi = 0; pi < (long long)(p); pi++) {
cin >> s[pi];
}
for (long long ni = 0; ni < (long long)(n); ni++) {
string ss;
cin >> ss;
fld.push_back(ss);
}
for (long long ni = 0; ni < (long long)(n); ni++) {
for (long long mi = 0; mi < (long long)(m); mi++) {
if (fld[ni][mi] != '.' and fld[ni][mi] != '#') {
q[fld[ni][mi] - '1'].push(make_pair(make_pair(ni, mi), 0LL));
vis[ni][mi] = fld[ni][mi] - '1';
}
}
}
bfs();
vector<long long> ans(p);
for (long long ni = 0; ni < (long long)(n); ni++) {
for (long long mi = 0; mi < (long long)(m); mi++) {
if (vis[ni][mi] != -1) ans[vis[ni][mi]]++;
}
}
print(ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
char A[1009][1009];
int S[10], ans[10];
int main() {
int N, M, P;
scanf("%d%d%d", &N, &M, &P);
for (int i = 1; i <= P; i++) scanf("%d", &S[i]);
queue<pii> que[10];
memset(A, '#', sizeof(A));
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
scanf(" %c", &A[i][j]);
if ('1' <= A[i][j] && A[i][j] <= '9') que[A[i][j] - '0'].push({i, j});
}
}
while (1) {
for (int i = 1; i <= P; i++) {
int e = 0;
while (que[i].size()) {
int sz = que[i].size();
while (sz--) {
int x, y;
tie(x, y) = que[i].front();
que[i].pop();
for (int k = 0; k < 4; k++) {
int X = x + dx[k], Y = y + dy[k];
if (A[X][Y] == '.') {
A[X][Y] = '0' + i;
que[i].push({X, Y});
}
}
}
++e;
if (e == S[i]) break;
}
}
bool fl = 1;
for (int i = 1; i <= P; i++)
if (que[i].size()) fl = 0;
if (fl) break;
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
if ('1' <= A[i][j] && A[i][j] <= '9') ++ans[A[i][j] - '0'];
}
}
for (int i = 1; i <= P; i++) printf("%d ", ans[i]);
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
char A[1009][1009];
int S[10], ans[10];
int main() {
int N, M, P;
scanf("%d%d%d", &N, &M, &P);
for (int i = 1; i <= P; i++) scanf("%d", &S[i]);
queue<pii> que[10];
memset(A, '#', sizeof(A));
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
scanf(" %c", &A[i][j]);
if ('1' <= A[i][j] && A[i][j] <= '9') que[A[i][j] - '0'].push({i, j});
}
}
while (1) {
for (int i = 1; i <= P; i++) {
int e = 0;
while (que[i].size()) {
int sz = que[i].size();
while (sz--) {
int x, y;
tie(x, y) = que[i].front();
que[i].pop();
for (int k = 0; k < 4; k++) {
int X = x + dx[k], Y = y + dy[k];
if (A[X][Y] == '.') {
A[X][Y] = '0' + i;
que[i].push({X, Y});
}
}
}
++e;
if (e == S[i]) break;
}
}
bool fl = 1;
for (int i = 1; i <= P; i++)
if (que[i].size()) fl = 0;
if (fl) break;
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
if ('1' <= A[i][j] && A[i][j] <= '9') ++ans[A[i][j] - '0'];
}
}
for (int i = 1; i <= P; i++) printf("%d ", ans[i]);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
short n, m, p, kx[4] = {1, -1, 0, 0}, ky[4] = {0, 0, -1, 1};
int s[10], d[10];
short e[10];
char a[1002][1002];
queue<pair<short, short> > q[2][10];
int main() {
scanf("%hd%hd%hd", &n, &m, &p);
for (short i = 1; i <= p; ++i) scanf("%d", s + i);
for (short i = 1; i <= n; ++i)
for (short j = 1; j <= m; ++j) {
scanf(" %c", &a[i][j]);
if (a[i][j] >= '1' && a[i][j] <= '9')
q[0][a[i][j] - '0'].push(pair<short, short>(i, j));
}
for (short i = 0; i <= m + 1; ++i) a[0][i] = a[n + 1][i] = '#';
for (short i = 0; i <= n + 1; ++i) a[i][0] = a[i][m + 1] = '#';
bool k, kk;
short x, y, u, v, uu, vv;
do {
k = false;
for (short z = 1; z <= p; ++z) {
kk = true;
for (int g = 1; g <= s[z] && kk; ++g) {
kk = false;
x = e[z], y = x ^ 1;
while (!q[x][z].empty()) {
u = q[x][z].front().first, v = q[x][z].front().second;
q[x][z].pop();
++d[z];
for (short j = 0; j < 4; ++j) {
uu = u + kx[j], vv = v + ky[j];
if (a[uu][vv] == '.') {
q[y][z].push(pair<short, short>(uu, vv));
a[uu][vv] = z + '0';
kk = k = true;
}
}
}
e[z] ^= 1;
}
}
} while (k);
for (short i = 1; i <= p; ++i) cout << d[i] << ' ';
}
| ### Prompt
In cpp, your task is to solve the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
short n, m, p, kx[4] = {1, -1, 0, 0}, ky[4] = {0, 0, -1, 1};
int s[10], d[10];
short e[10];
char a[1002][1002];
queue<pair<short, short> > q[2][10];
int main() {
scanf("%hd%hd%hd", &n, &m, &p);
for (short i = 1; i <= p; ++i) scanf("%d", s + i);
for (short i = 1; i <= n; ++i)
for (short j = 1; j <= m; ++j) {
scanf(" %c", &a[i][j]);
if (a[i][j] >= '1' && a[i][j] <= '9')
q[0][a[i][j] - '0'].push(pair<short, short>(i, j));
}
for (short i = 0; i <= m + 1; ++i) a[0][i] = a[n + 1][i] = '#';
for (short i = 0; i <= n + 1; ++i) a[i][0] = a[i][m + 1] = '#';
bool k, kk;
short x, y, u, v, uu, vv;
do {
k = false;
for (short z = 1; z <= p; ++z) {
kk = true;
for (int g = 1; g <= s[z] && kk; ++g) {
kk = false;
x = e[z], y = x ^ 1;
while (!q[x][z].empty()) {
u = q[x][z].front().first, v = q[x][z].front().second;
q[x][z].pop();
++d[z];
for (short j = 0; j < 4; ++j) {
uu = u + kx[j], vv = v + ky[j];
if (a[uu][vv] == '.') {
q[y][z].push(pair<short, short>(uu, vv));
a[uu][vv] = z + '0';
kk = k = true;
}
}
}
e[z] ^= 1;
}
}
} while (k);
for (short i = 1; i <= p; ++i) cout << d[i] << ' ';
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
const int mv[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int n, m, p;
long long s[maxn];
bool mp[maxn][maxn];
int ans[maxn];
struct node {
int x, y;
long long num;
int color;
int ft;
node() {}
node(int _x, int _y, int _color) {
x = _x;
y = _y;
color = _color;
}
void insert(int _x, int _y, int _color, long long _num, int _o) {
x = _x;
y = _y;
ft = _o;
num = _num - 1;
color = _color;
if (_num == 1) {
num = s[_color];
++ft;
}
}
bool friend operator<(const node &a, const node &b) {
if (a.ft == b.ft) {
if (a.color == b.color) {
return a.num < b.num;
} else {
return a.color > b.color;
}
} else {
return a.ft > b.ft;
}
}
};
vector<node> ve;
void solve() {
priority_queue<node> sk;
for (auto s : ve) sk.push(s);
while (!sk.empty()) {
node top = sk.top();
sk.pop();
for (int i = 0; i < 4; i++) {
int x = top.x + mv[i][0];
int y = top.y + mv[i][1];
if (x >= n || y >= m || x < 0 || y < 0 || mp[x][y]) continue;
mp[x][y] = true;
++ans[top.color];
node f;
f.insert(x, y, top.color, top.num, top.ft);
sk.push(f);
}
}
for (int i = 1; i <= p; i++) cout << ans[i] << " ";
cout << endl;
}
int main() {
while (~scanf("%d%d%d", &n, &m, &p)) {
memset(ans, 0, sizeof(ans));
ve.clear();
for (int i = 1; i <= p; i++) scanf("%lld", s + i);
getchar();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int c = getchar();
if (c == '.')
mp[i][j] = false;
else {
mp[i][j] = true;
if (c == '#') continue;
ans[c - '0']++;
node f = node(i, j, c - '0');
f.ft = 0;
f.num = s[c - '0'];
ve.push_back(f);
}
}
getchar();
}
solve();
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
const int mv[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int n, m, p;
long long s[maxn];
bool mp[maxn][maxn];
int ans[maxn];
struct node {
int x, y;
long long num;
int color;
int ft;
node() {}
node(int _x, int _y, int _color) {
x = _x;
y = _y;
color = _color;
}
void insert(int _x, int _y, int _color, long long _num, int _o) {
x = _x;
y = _y;
ft = _o;
num = _num - 1;
color = _color;
if (_num == 1) {
num = s[_color];
++ft;
}
}
bool friend operator<(const node &a, const node &b) {
if (a.ft == b.ft) {
if (a.color == b.color) {
return a.num < b.num;
} else {
return a.color > b.color;
}
} else {
return a.ft > b.ft;
}
}
};
vector<node> ve;
void solve() {
priority_queue<node> sk;
for (auto s : ve) sk.push(s);
while (!sk.empty()) {
node top = sk.top();
sk.pop();
for (int i = 0; i < 4; i++) {
int x = top.x + mv[i][0];
int y = top.y + mv[i][1];
if (x >= n || y >= m || x < 0 || y < 0 || mp[x][y]) continue;
mp[x][y] = true;
++ans[top.color];
node f;
f.insert(x, y, top.color, top.num, top.ft);
sk.push(f);
}
}
for (int i = 1; i <= p; i++) cout << ans[i] << " ";
cout << endl;
}
int main() {
while (~scanf("%d%d%d", &n, &m, &p)) {
memset(ans, 0, sizeof(ans));
ve.clear();
for (int i = 1; i <= p; i++) scanf("%lld", s + i);
getchar();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int c = getchar();
if (c == '.')
mp[i][j] = false;
else {
mp[i][j] = true;
if (c == '#') continue;
ans[c - '0']++;
node f = node(i, j, c - '0');
f.ft = 0;
f.num = s[c - '0'];
ve.push_back(f);
}
}
getchar();
}
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n, m, k;
bool ch(int x, int y) { return (x < n && x >= 0 && y < m && y >= 0); }
int32_t main() {
cin >> n >> m >> k;
vector<queue<pair<int, int>>> q(k), qq;
vector<vector<vector<int>>> d(k, vector<vector<int>>(n, vector<int>(m, 1e9)));
vector<int> s(k);
for (int i = 0; i < k; i++) cin >> s[i];
vector<vector<char>> a(n, vector<char>(m));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (a[i][j] != '.' && a[i][j] != '#') {
q[a[i][j] - '1'].push({i, j});
}
}
qq = q;
for (int i = 0; i < k; i++) {
while (!q[i].empty()) {
int x = q[i].front().first, y = q[i].front().second;
if (d[i][x][y] == 1e9) d[i][x][y] = 0;
q[i].pop();
for (int j = 0; j < 4; j++) {
if (ch(x + dx[j], y + dy[j]) && a[x + dx[j]][y + dy[j]] == '.' &&
d[i][x + dx[j]][y + dy[j]] == 1e9) {
d[i][x + dx[j]][y + dy[j]] = d[i][x][y] + 1;
q[i].push({x + dx[j], y + dy[j]});
}
}
}
}
q = qq;
int kek = 0;
vector<int> ans(k);
while (1) {
bool fl = 0;
for (int i = 0; i < k; i++) {
while (1) {
if (q[i].empty() ||
(d[i][q[i].front().first][q[i].front().second]) / s[i] != kek) {
break;
}
int x = q[i].front().first, y = q[i].front().second;
if (a[q[i].front().first][q[i].front().second] == char(i + '1')) {
for (int j = 0; j < 4; j++)
if (ch(x + dx[j], y + dy[j]) && a[x + dx[j]][y + dy[j]] == '.') {
q[i].push({x + dx[j], y + dy[j]});
a[x + dx[j]][y + dy[j]] = char(i + '1');
}
ans[i]++;
}
fl = 1;
q[i].pop();
}
}
if (!fl) break;
kek++;
}
for (auto i : ans) cout << i << " ";
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n, m, k;
bool ch(int x, int y) { return (x < n && x >= 0 && y < m && y >= 0); }
int32_t main() {
cin >> n >> m >> k;
vector<queue<pair<int, int>>> q(k), qq;
vector<vector<vector<int>>> d(k, vector<vector<int>>(n, vector<int>(m, 1e9)));
vector<int> s(k);
for (int i = 0; i < k; i++) cin >> s[i];
vector<vector<char>> a(n, vector<char>(m));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (a[i][j] != '.' && a[i][j] != '#') {
q[a[i][j] - '1'].push({i, j});
}
}
qq = q;
for (int i = 0; i < k; i++) {
while (!q[i].empty()) {
int x = q[i].front().first, y = q[i].front().second;
if (d[i][x][y] == 1e9) d[i][x][y] = 0;
q[i].pop();
for (int j = 0; j < 4; j++) {
if (ch(x + dx[j], y + dy[j]) && a[x + dx[j]][y + dy[j]] == '.' &&
d[i][x + dx[j]][y + dy[j]] == 1e9) {
d[i][x + dx[j]][y + dy[j]] = d[i][x][y] + 1;
q[i].push({x + dx[j], y + dy[j]});
}
}
}
}
q = qq;
int kek = 0;
vector<int> ans(k);
while (1) {
bool fl = 0;
for (int i = 0; i < k; i++) {
while (1) {
if (q[i].empty() ||
(d[i][q[i].front().first][q[i].front().second]) / s[i] != kek) {
break;
}
int x = q[i].front().first, y = q[i].front().second;
if (a[q[i].front().first][q[i].front().second] == char(i + '1')) {
for (int j = 0; j < 4; j++)
if (ch(x + dx[j], y + dy[j]) && a[x + dx[j]][y + dy[j]] == '.') {
q[i].push({x + dx[j], y + dy[j]});
a[x + dx[j]][y + dy[j]] = char(i + '1');
}
ans[i]++;
}
fl = 1;
q[i].pop();
}
}
if (!fl) break;
kek++;
}
for (auto i : ans) cout << i << " ";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int llINF = 0x3f3f3f3f3f3f3f;
const int MAXN = 100100;
const long long int mod = 1e9 + 7;
const int LMAXN = 20;
int mx[4] = {1, -1, 0, 0};
int my[4] = {0, 0, -1, 1};
int n, m, p;
int freq[11], s[11];
set<tuple<int, int, int> > fila[11], tem[11];
int mapa[1111][1111];
bool valido(int x, int y) {
if (x < 0 || y < 0) return false;
if (x >= n || y >= m) return false;
return true;
}
bool end() {
int ok = 1;
for (int i = 1; i <= p; i++)
if (!tem[i].empty()) {
ok = 0;
}
return ok;
}
void bfs() {
do {
for (int cara = 1; cara <= p; cara++) {
for (auto x : tem[cara]) fila[cara].insert(x);
tem[cara].clear();
tuple<int, int, int> aux;
while (!fila[cara].empty()) {
aux = *fila[cara].begin();
fila[cara].erase(fila[cara].begin());
int x, y;
x = get<1>(aux);
y = get<2>(aux);
if (get<0>(aux) == s[cara]) {
tem[cara].insert({0, x, y});
continue;
}
for (int i = 0; i < 4; i++) {
if (valido(x + mx[i], y + my[i]) && mapa[x + mx[i]][y + my[i]] == 0) {
freq[cara]++;
fila[cara].insert({get<0>(aux) + 1, x + mx[i], y + my[i]});
mapa[x + mx[i]][y + my[i]] = cara;
}
}
}
}
} while (!end());
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> p;
for (int i = 1; i <= p; i++) cin >> s[i];
string aux;
for (int i = 0; i < n; i++) {
cin >> aux;
for (int j = 0; j < m; j++) {
if (aux[j] >= '1' and aux[j] <= '9') {
freq[aux[j] - '0']++;
tem[aux[j] - '0'].insert({0, i, j});
mapa[i][j] = aux[j] - '0';
} else if (aux[j] == '#')
mapa[i][j] = -1;
else
mapa[i][j] = 0;
}
}
bfs();
for (int i = 1; i <= p; i++) cout << freq[i] << ' ';
cout << endl;
}
| ### Prompt
Create a solution in cpp for the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int llINF = 0x3f3f3f3f3f3f3f;
const int MAXN = 100100;
const long long int mod = 1e9 + 7;
const int LMAXN = 20;
int mx[4] = {1, -1, 0, 0};
int my[4] = {0, 0, -1, 1};
int n, m, p;
int freq[11], s[11];
set<tuple<int, int, int> > fila[11], tem[11];
int mapa[1111][1111];
bool valido(int x, int y) {
if (x < 0 || y < 0) return false;
if (x >= n || y >= m) return false;
return true;
}
bool end() {
int ok = 1;
for (int i = 1; i <= p; i++)
if (!tem[i].empty()) {
ok = 0;
}
return ok;
}
void bfs() {
do {
for (int cara = 1; cara <= p; cara++) {
for (auto x : tem[cara]) fila[cara].insert(x);
tem[cara].clear();
tuple<int, int, int> aux;
while (!fila[cara].empty()) {
aux = *fila[cara].begin();
fila[cara].erase(fila[cara].begin());
int x, y;
x = get<1>(aux);
y = get<2>(aux);
if (get<0>(aux) == s[cara]) {
tem[cara].insert({0, x, y});
continue;
}
for (int i = 0; i < 4; i++) {
if (valido(x + mx[i], y + my[i]) && mapa[x + mx[i]][y + my[i]] == 0) {
freq[cara]++;
fila[cara].insert({get<0>(aux) + 1, x + mx[i], y + my[i]});
mapa[x + mx[i]][y + my[i]] = cara;
}
}
}
}
} while (!end());
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> p;
for (int i = 1; i <= p; i++) cin >> s[i];
string aux;
for (int i = 0; i < n; i++) {
cin >> aux;
for (int j = 0; j < m; j++) {
if (aux[j] >= '1' and aux[j] <= '9') {
freq[aux[j] - '0']++;
tem[aux[j] - '0'].insert({0, i, j});
mapa[i][j] = aux[j] - '0';
} else if (aux[j] == '#')
mapa[i][j] = -1;
else
mapa[i][j] = 0;
}
}
bfs();
for (int i = 1; i <= p; i++) cout << freq[i] << ' ';
cout << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int n, m, q, s[N];
char G[N][N];
vector<pair<int, int>> todo[20];
priority_queue<pair<int, pair<int, int>>> Q;
bool flag = 1;
int dis[N][N], done[N][N];
int ans[20];
inline void update(int u) {
int x, y;
for (auto i : todo[u]) {
x = i.first;
y = i.second;
bool ok = 0;
for (int j = 0; j < 4; j++)
if (G[x + dx[j]][y + dy[j]] == '.') ok = 1;
if (ok) {
dis[x][y] = s[u];
Q.push(make_pair(s[u], i));
}
}
static int cur = 0;
cur++;
todo[u].clear();
while (Q.size()) {
x = Q.top().second.first;
y = Q.top().second.second;
Q.pop();
if (done[x][y] == cur) continue;
done[x][y] = cur;
G[x][y] = '0' + u;
if (dis[x][y] != s[u]) todo[u].push_back(make_pair(x, y)), flag = 1;
for (int j = 0; j < 4; j++) {
int nx = x + dx[j];
int ny = y + dy[j];
if (done[nx][ny] != cur && G[nx][ny] == '.' &&
dis[nx][ny] < dis[x][y] - 1) {
dis[nx][ny] = dis[x][y] - 1;
Q.push(make_pair(dis[nx][ny], make_pair(nx, ny)));
}
}
}
}
int main() {
memset(dis, -1, sizeof dis);
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= q; i++) scanf("%d", &s[i]);
for (int i = 1; i <= n; i++) {
scanf("%s", G[i] + 1);
for (int j = 1; j <= m; j++)
if (isdigit(G[i][j])) todo[G[i][j] - '0'].push_back(make_pair(i, j));
}
while (flag) {
flag = 0;
for (int i = 1; i <= q; i++) update(i);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (isdigit(G[i][j])) ans[G[i][j] - '0']++;
for (int i = 1; i <= q; i++) printf("%d ", ans[i]);
puts("");
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int n, m, q, s[N];
char G[N][N];
vector<pair<int, int>> todo[20];
priority_queue<pair<int, pair<int, int>>> Q;
bool flag = 1;
int dis[N][N], done[N][N];
int ans[20];
inline void update(int u) {
int x, y;
for (auto i : todo[u]) {
x = i.first;
y = i.second;
bool ok = 0;
for (int j = 0; j < 4; j++)
if (G[x + dx[j]][y + dy[j]] == '.') ok = 1;
if (ok) {
dis[x][y] = s[u];
Q.push(make_pair(s[u], i));
}
}
static int cur = 0;
cur++;
todo[u].clear();
while (Q.size()) {
x = Q.top().second.first;
y = Q.top().second.second;
Q.pop();
if (done[x][y] == cur) continue;
done[x][y] = cur;
G[x][y] = '0' + u;
if (dis[x][y] != s[u]) todo[u].push_back(make_pair(x, y)), flag = 1;
for (int j = 0; j < 4; j++) {
int nx = x + dx[j];
int ny = y + dy[j];
if (done[nx][ny] != cur && G[nx][ny] == '.' &&
dis[nx][ny] < dis[x][y] - 1) {
dis[nx][ny] = dis[x][y] - 1;
Q.push(make_pair(dis[nx][ny], make_pair(nx, ny)));
}
}
}
}
int main() {
memset(dis, -1, sizeof dis);
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= q; i++) scanf("%d", &s[i]);
for (int i = 1; i <= n; i++) {
scanf("%s", G[i] + 1);
for (int j = 1; j <= m; j++)
if (isdigit(G[i][j])) todo[G[i][j] - '0'].push_back(make_pair(i, j));
}
while (flag) {
flag = 0;
for (int i = 1; i <= q; i++) update(i);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (isdigit(G[i][j])) ans[G[i][j] - '0']++;
for (int i = 1; i <= q; i++) printf("%d ", ans[i]);
puts("");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
register int r = 0, c = 0, m = 0;
for (; c < '-'; c = getchar())
;
if (c == '-') m = c = getchar();
for (; c > '-'; r = r * 10 + c - '0', c = getchar())
;
return m ? -r : r;
}
int Map[1010][1010];
vector<pair<int, int>> need[10];
int n = read(), m = read(), p = read(), s[10];
int main() {
for (int i = 1; i <= p; i++) s[i] = read();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int t;
Map[i][j] = t = getchar();
if (t >= '1' && t <= '9') {
need[t - '0'].push_back(make_pair(i, j));
}
}
getchar();
}
while (true) {
int can = 0;
for (int now = 1; now <= p; now++) {
int moved = 0;
vector<pair<int, int>> nn;
queue<pair<int, int>> Q;
bool test = false;
for (auto aut : need[now]) {
Q.push(aut);
}
Q.push(make_pair(-1, s[now]));
while (!Q.empty()) {
auto aut2 = Q.front();
Q.pop();
if (aut2.first == -1) {
if (test) break;
test = true;
if (!--aut2.second) break;
Q.push(aut2);
continue;
} else {
test = false;
int i = aut2.first, j = aut2.second;
for (int dx = -1; dx <= 1; dx++)
for (int dy = -1; dy <= 1; dy++)
if ((!dx && dy) || (!dy && dx)) {
if (i + dx > 0 && i + dx <= n && j + dy > 0 && j + dy <= m &&
Map[i + dx][j + dy] == '.') {
Map[i + dx][j + dy] = now + '0';
Q.push(make_pair(i + dx, j + dy));
moved = 1;
nn.push_back(make_pair(i + dx, j + dy));
}
}
}
}
need[now] = nn;
if (moved) can = 1;
}
if (!can) break;
}
int ans[10] = {0};
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (Map[i][j] > '0' && Map[i][j] <= '9') {
ans[Map[i][j] - '0']++;
}
}
}
for (int i = 1; i <= p; i++) printf("%d ", ans[i]);
return 0;
};
| ### Prompt
Construct a CPP code solution to the problem outlined:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
register int r = 0, c = 0, m = 0;
for (; c < '-'; c = getchar())
;
if (c == '-') m = c = getchar();
for (; c > '-'; r = r * 10 + c - '0', c = getchar())
;
return m ? -r : r;
}
int Map[1010][1010];
vector<pair<int, int>> need[10];
int n = read(), m = read(), p = read(), s[10];
int main() {
for (int i = 1; i <= p; i++) s[i] = read();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int t;
Map[i][j] = t = getchar();
if (t >= '1' && t <= '9') {
need[t - '0'].push_back(make_pair(i, j));
}
}
getchar();
}
while (true) {
int can = 0;
for (int now = 1; now <= p; now++) {
int moved = 0;
vector<pair<int, int>> nn;
queue<pair<int, int>> Q;
bool test = false;
for (auto aut : need[now]) {
Q.push(aut);
}
Q.push(make_pair(-1, s[now]));
while (!Q.empty()) {
auto aut2 = Q.front();
Q.pop();
if (aut2.first == -1) {
if (test) break;
test = true;
if (!--aut2.second) break;
Q.push(aut2);
continue;
} else {
test = false;
int i = aut2.first, j = aut2.second;
for (int dx = -1; dx <= 1; dx++)
for (int dy = -1; dy <= 1; dy++)
if ((!dx && dy) || (!dy && dx)) {
if (i + dx > 0 && i + dx <= n && j + dy > 0 && j + dy <= m &&
Map[i + dx][j + dy] == '.') {
Map[i + dx][j + dy] = now + '0';
Q.push(make_pair(i + dx, j + dy));
moved = 1;
nn.push_back(make_pair(i + dx, j + dy));
}
}
}
}
need[now] = nn;
if (moved) can = 1;
}
if (!can) break;
}
int ans[10] = {0};
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (Map[i][j] > '0' && Map[i][j] <= '9') {
ans[Map[i][j] - '0']++;
}
}
}
for (int i = 1; i <= p; i++) printf("%d ", ans[i]);
return 0;
};
``` |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:252457298")
#pragma GCC optimize("Ofast")
using namespace std;
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int n, m, p;
vector<int> s, cnt;
vector<vector<char>> A;
vector<vector<pair<int, int>>> InitialCoor;
int dx[] = {-1, +0, +0, +1};
int dy[] = {+0, -1, +1, +0};
queue<tuple<int, int, int>> Q;
vector<vector<bool>> vis;
void Input() {
cin >> n >> m >> p;
A.resize(n, vector<char>(m));
InitialCoor.resize(p);
s.resize(p);
cnt.resize(p, 0);
vis.resize(n, vector<bool>(m, false));
for (auto& z : s) cin >> z;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> A[i][j];
if (A[i][j] >= '1' && A[i][j] <= '9') {
InitialCoor[A[i][j] - '1'].push_back({i, j});
}
}
}
}
void Solve() {
for (int i = 0; i < p; i++) {
for (auto Point : InitialCoor[i]) {
int x = Point.first, y = Point.second;
vis[x][y] = true;
Q.push({i, x, y});
}
}
while (!Q.empty()) {
tuple<int, int, int> T = Q.front();
int id = get<0>(T);
queue<tuple<int, int, int>> TmpQueue;
while (!Q.empty() && get<0>(Q.front()) == id) {
T = Q.front();
int firstx = get<1>(T), firsty = get<2>(T);
TmpQueue.push({s[id], firstx, firsty});
Q.pop();
}
while (!TmpQueue.empty()) {
tuple<int, int, int> xT = TmpQueue.front();
TmpQueue.pop();
int step = get<0>(xT), x = get<1>(xT), y = get<2>(xT);
if (step == 0) continue;
for (int dir = 0; dir < 4; dir++) {
if (x + dx[dir] < 0 || x + dx[dir] >= n) continue;
if (y + dy[dir] < 0 || y + dy[dir] >= m) continue;
if (vis[x + dx[dir]][y + dy[dir]]) continue;
if (A[x + dx[dir]][y + dy[dir]] != '.') continue;
vis[x + dx[dir]][y + dy[dir]] = true;
A[x + dx[dir]][y + dy[dir]] = char(49 + id);
TmpQueue.push({step - 1, x + dx[dir], y + dy[dir]});
Q.push({id, x + dx[dir], y + dy[dir]});
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (A[i][j] < '1' || A[i][j] > '9') continue;
cnt[A[i][j] - '1'] += 1;
}
}
for (auto x : cnt) cout << x << " ";
cout << '\n';
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0);
Input();
Solve();
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:252457298")
#pragma GCC optimize("Ofast")
using namespace std;
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int n, m, p;
vector<int> s, cnt;
vector<vector<char>> A;
vector<vector<pair<int, int>>> InitialCoor;
int dx[] = {-1, +0, +0, +1};
int dy[] = {+0, -1, +1, +0};
queue<tuple<int, int, int>> Q;
vector<vector<bool>> vis;
void Input() {
cin >> n >> m >> p;
A.resize(n, vector<char>(m));
InitialCoor.resize(p);
s.resize(p);
cnt.resize(p, 0);
vis.resize(n, vector<bool>(m, false));
for (auto& z : s) cin >> z;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> A[i][j];
if (A[i][j] >= '1' && A[i][j] <= '9') {
InitialCoor[A[i][j] - '1'].push_back({i, j});
}
}
}
}
void Solve() {
for (int i = 0; i < p; i++) {
for (auto Point : InitialCoor[i]) {
int x = Point.first, y = Point.second;
vis[x][y] = true;
Q.push({i, x, y});
}
}
while (!Q.empty()) {
tuple<int, int, int> T = Q.front();
int id = get<0>(T);
queue<tuple<int, int, int>> TmpQueue;
while (!Q.empty() && get<0>(Q.front()) == id) {
T = Q.front();
int firstx = get<1>(T), firsty = get<2>(T);
TmpQueue.push({s[id], firstx, firsty});
Q.pop();
}
while (!TmpQueue.empty()) {
tuple<int, int, int> xT = TmpQueue.front();
TmpQueue.pop();
int step = get<0>(xT), x = get<1>(xT), y = get<2>(xT);
if (step == 0) continue;
for (int dir = 0; dir < 4; dir++) {
if (x + dx[dir] < 0 || x + dx[dir] >= n) continue;
if (y + dy[dir] < 0 || y + dy[dir] >= m) continue;
if (vis[x + dx[dir]][y + dy[dir]]) continue;
if (A[x + dx[dir]][y + dy[dir]] != '.') continue;
vis[x + dx[dir]][y + dy[dir]] = true;
A[x + dx[dir]][y + dy[dir]] = char(49 + id);
TmpQueue.push({step - 1, x + dx[dir], y + dy[dir]});
Q.push({id, x + dx[dir], y + dy[dir]});
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (A[i][j] < '1' || A[i][j] > '9') continue;
cnt[A[i][j] - '1'] += 1;
}
}
for (auto x : cnt) cout << x << " ";
cout << '\n';
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0);
Input();
Solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long fn(long long x, long long rn[]) {
if (x == rn[x])
return x;
else
return rn[x] = fn(rn[x], rn);
}
bool un(long long x, long long y, long long rn[], long long sz[]) {
x = fn(x, rn);
y = fn(y, rn);
if (x == y) return false;
if (sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y];
rn[y] = x;
return true;
}
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long MOD = 9982453;
long long mod = 1000000007;
long long power(long long x, long long y, long long p) {
long long res = 1;
x %= p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long inv(long long val, long long MODx = MOD) {
return power(val, MODx - 2, MODx);
}
vector<long long> fac, ifac;
void preFac(long long sz) {
fac.resize(sz + 1), ifac.resize(sz + 1);
fac[0] = 1;
for (int i = 1; i <= sz; i++) {
fac[i] = (i * fac[i - 1]) % MOD;
}
ifac[sz] = inv(fac[sz]);
for (int i = sz - 1; i >= 0; i--) {
ifac[i] = ((i + 1) * ifac[i + 1]) % MOD;
}
}
long long nCr(long long N, long long R) {
if (R <= N and R >= 0) {
return ((fac[N] * ifac[R]) % MOD * ifac[N - R]) % MOD;
}
return 0;
}
static vector<vector<vector<pair<long long, long long>>>> v(
1001, vector<vector<pair<long long, long long>>>(1001));
static vector<long long> ans(10, 0);
static queue<pair<long long, long long>> q[10];
static vector<vector<long long>> vis(1001, vector<long long>(1001, 0));
static vector<long long> speed(10);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m, p;
cin >> n >> m >> p;
for (long long i = 1; i <= p; i++) {
cin >> speed[i];
}
string s[n];
for (long long i = 0; i <= n - 1; i++) {
cin >> s[i];
}
for (long long i = 0; i <= n - 1; i++) {
for (long long j = 0; j <= m - 1; j++) {
if (s[i][j] != '#' and s[i][j] != '.') {
q[s[i][j] - '0'].push({i, j});
vis[i][j] = 1;
}
}
}
for (long long i = 0; i <= n - 1; i++) {
for (long long j = 0; j <= m - 1; j++) {
if (s[i][j] != '#') {
if (i + 1 < n and s[i + 1][j] != '#') {
v[i][j].push_back({i + 1, j});
}
if (i - 1 >= 0 and s[i - 1][j] != '#') {
v[i][j].push_back({i - 1, j});
}
if (j + 1 < m and s[i][j + 1] != '#') {
v[i][j].push_back({i, j + 1});
}
if (j - 1 >= 0 and s[i][j - 1] != '#') {
v[i][j].push_back({i, j - 1});
}
}
}
}
while (1) {
long long flg = 0;
for (long long x = 1; x <= p; x++) {
map<pair<long long, long long>, long long> dis;
vector<pair<long long, long long>> endp;
while (!q[x].empty()) {
flg = 1;
pair<long long, long long> pp = q[x].front();
q[x].pop();
if (dis[pp] == speed[x]) {
endp.push_back(pp);
continue;
}
ans[x]++;
for (auto u : v[pp.first][pp.second]) {
if (vis[u.first][u.second]) {
continue;
}
dis[u] = dis[pp] + 1;
if (dis[u] <= speed[x]) {
vis[u.first][u.second] = 1;
}
q[x].push(u);
}
}
for (auto u : endp) {
q[x].push(u);
}
}
if (flg == 0) {
break;
}
}
for (long long i = 1; i <= p; i++) {
cout << ans[i] << " ";
}
cout << "\n";
}
| ### Prompt
Generate a CPP solution to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long fn(long long x, long long rn[]) {
if (x == rn[x])
return x;
else
return rn[x] = fn(rn[x], rn);
}
bool un(long long x, long long y, long long rn[], long long sz[]) {
x = fn(x, rn);
y = fn(y, rn);
if (x == y) return false;
if (sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y];
rn[y] = x;
return true;
}
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long MOD = 9982453;
long long mod = 1000000007;
long long power(long long x, long long y, long long p) {
long long res = 1;
x %= p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long inv(long long val, long long MODx = MOD) {
return power(val, MODx - 2, MODx);
}
vector<long long> fac, ifac;
void preFac(long long sz) {
fac.resize(sz + 1), ifac.resize(sz + 1);
fac[0] = 1;
for (int i = 1; i <= sz; i++) {
fac[i] = (i * fac[i - 1]) % MOD;
}
ifac[sz] = inv(fac[sz]);
for (int i = sz - 1; i >= 0; i--) {
ifac[i] = ((i + 1) * ifac[i + 1]) % MOD;
}
}
long long nCr(long long N, long long R) {
if (R <= N and R >= 0) {
return ((fac[N] * ifac[R]) % MOD * ifac[N - R]) % MOD;
}
return 0;
}
static vector<vector<vector<pair<long long, long long>>>> v(
1001, vector<vector<pair<long long, long long>>>(1001));
static vector<long long> ans(10, 0);
static queue<pair<long long, long long>> q[10];
static vector<vector<long long>> vis(1001, vector<long long>(1001, 0));
static vector<long long> speed(10);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m, p;
cin >> n >> m >> p;
for (long long i = 1; i <= p; i++) {
cin >> speed[i];
}
string s[n];
for (long long i = 0; i <= n - 1; i++) {
cin >> s[i];
}
for (long long i = 0; i <= n - 1; i++) {
for (long long j = 0; j <= m - 1; j++) {
if (s[i][j] != '#' and s[i][j] != '.') {
q[s[i][j] - '0'].push({i, j});
vis[i][j] = 1;
}
}
}
for (long long i = 0; i <= n - 1; i++) {
for (long long j = 0; j <= m - 1; j++) {
if (s[i][j] != '#') {
if (i + 1 < n and s[i + 1][j] != '#') {
v[i][j].push_back({i + 1, j});
}
if (i - 1 >= 0 and s[i - 1][j] != '#') {
v[i][j].push_back({i - 1, j});
}
if (j + 1 < m and s[i][j + 1] != '#') {
v[i][j].push_back({i, j + 1});
}
if (j - 1 >= 0 and s[i][j - 1] != '#') {
v[i][j].push_back({i, j - 1});
}
}
}
}
while (1) {
long long flg = 0;
for (long long x = 1; x <= p; x++) {
map<pair<long long, long long>, long long> dis;
vector<pair<long long, long long>> endp;
while (!q[x].empty()) {
flg = 1;
pair<long long, long long> pp = q[x].front();
q[x].pop();
if (dis[pp] == speed[x]) {
endp.push_back(pp);
continue;
}
ans[x]++;
for (auto u : v[pp.first][pp.second]) {
if (vis[u.first][u.second]) {
continue;
}
dis[u] = dis[pp] + 1;
if (dis[u] <= speed[x]) {
vis[u.first][u.second] = 1;
}
q[x].push(u);
}
}
for (auto u : endp) {
q[x].push(u);
}
}
if (flg == 0) {
break;
}
}
for (long long i = 1; i <= p; i++) {
cout << ans[i] << " ";
}
cout << "\n";
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> p) {
out << '(' << p.first << ',' << p.second << ')';
return out;
}
template <class T1, class T2>
istream& operator>>(istream& in, pair<T1, T2>& p) {
in >> p.first >> p.second;
return in;
}
template <class T>
istream& operator>>(istream& in, vector<T>& v) {
for (auto& x : v) in >> x;
return in;
}
template <class T>
ostream& operator<<(ostream& out, vector<vector<T>>& v) {
for (auto& x : v) out << x << '\n';
return out;
}
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
for (auto x : v) out << x << ' ';
return out;
}
long long gcd(long long a, long long b) {
if (b > a) swap(a, b);
return (b ? gcd(b, a % b) : a);
}
const long double pi = 2 * asin(1);
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.setstate(ios::failbit);
vector<pair<int, int>> dir = {make_pair(0, -1), make_pair(0, 1),
make_pair(1, 0), make_pair(-1, 0)};
int n, m, p, x, y;
cin >> n >> m >> p;
vector<int> s(p);
cin >> s;
vector<string> g(n);
cin >> g;
vector<queue<pair<pair<int, int>, int>>> q(p);
pair<pair<int, int>, int> P;
queue<pair<pair<int, int>, int>> Q;
for (register int k = 0; k < p; k++) {
for (register int i = 0; i < n; i++)
for (register int j = 0; j < m; j++) {
if (g[i][j] == '1' + k) {
q[k].push(make_pair(make_pair(i, j), s[k]));
}
}
}
for (register int _ = 0; _ < n * m + 2; _++) {
for (register int k = 0; k < p; k++) {
Q = q[k];
while (!q[k].empty()) q[k].pop();
while (!Q.empty()) {
P = Q.front();
Q.pop();
for (auto z : dir) {
x = P.first.first + z.first;
y = P.first.second + z.second;
if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == '.') {
g[x][y] = '1' + k;
if (P.second == 1) q[k].push(make_pair(make_pair(x, y), s[k]));
if (P.second != 1) Q.push(make_pair(make_pair(x, y), P.second - 1));
}
}
}
}
}
vector<int> ans(p);
for (register int i = 0; i < n; i++)
for (register int j = 0; j < m; j++) {
if (isdigit(g[i][j])) ans[g[i][j] - '1']++;
}
cout << ans;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> p) {
out << '(' << p.first << ',' << p.second << ')';
return out;
}
template <class T1, class T2>
istream& operator>>(istream& in, pair<T1, T2>& p) {
in >> p.first >> p.second;
return in;
}
template <class T>
istream& operator>>(istream& in, vector<T>& v) {
for (auto& x : v) in >> x;
return in;
}
template <class T>
ostream& operator<<(ostream& out, vector<vector<T>>& v) {
for (auto& x : v) out << x << '\n';
return out;
}
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
for (auto x : v) out << x << ' ';
return out;
}
long long gcd(long long a, long long b) {
if (b > a) swap(a, b);
return (b ? gcd(b, a % b) : a);
}
const long double pi = 2 * asin(1);
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.setstate(ios::failbit);
vector<pair<int, int>> dir = {make_pair(0, -1), make_pair(0, 1),
make_pair(1, 0), make_pair(-1, 0)};
int n, m, p, x, y;
cin >> n >> m >> p;
vector<int> s(p);
cin >> s;
vector<string> g(n);
cin >> g;
vector<queue<pair<pair<int, int>, int>>> q(p);
pair<pair<int, int>, int> P;
queue<pair<pair<int, int>, int>> Q;
for (register int k = 0; k < p; k++) {
for (register int i = 0; i < n; i++)
for (register int j = 0; j < m; j++) {
if (g[i][j] == '1' + k) {
q[k].push(make_pair(make_pair(i, j), s[k]));
}
}
}
for (register int _ = 0; _ < n * m + 2; _++) {
for (register int k = 0; k < p; k++) {
Q = q[k];
while (!q[k].empty()) q[k].pop();
while (!Q.empty()) {
P = Q.front();
Q.pop();
for (auto z : dir) {
x = P.first.first + z.first;
y = P.first.second + z.second;
if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == '.') {
g[x][y] = '1' + k;
if (P.second == 1) q[k].push(make_pair(make_pair(x, y), s[k]));
if (P.second != 1) Q.push(make_pair(make_pair(x, y), P.second - 1));
}
}
}
}
}
vector<int> ans(p);
for (register int i = 0; i < n; i++)
for (register int j = 0; j < m; j++) {
if (isdigit(g[i][j])) ans[g[i][j] - '1']++;
}
cout << ans;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, p, s[10], grid[1000][1000], sol[10];
list<pair<int, int>> cells[10];
bool seen[1000][1000];
queue<pair<int, int>> tmp;
pair<int, int> moves[4];
bool ok(const pair<int, int>& p) {
for (int i = 0; i < 4; i++) {
int x = p.first + moves[i].first;
int y = p.second + moves[i].second;
if (0 <= x && x < n && 0 <= y && y < m && grid[x][y] < 0) return true;
}
return false;
}
void update(int i) {
for (auto it = cells[i].begin(); it != cells[i].end();) {
if (!ok(*it))
it = cells[i].erase(it);
else
++it;
}
}
int main() {
moves[0] = make_pair(-1, 0);
moves[1] = make_pair(1, 0);
moves[2] = make_pair(0, 1);
moves[3] = make_pair(0, -1);
ios_base::sync_with_stdio(false);
cin >> n >> m >> p;
for (int i = 0; i < p; i++) cin >> s[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
if (c == '#')
grid[i][j] = p;
else if (c == '.')
grid[i][j] = -1;
else {
grid[i][j] = (int)c - '1';
cells[grid[i][j]].push_back(make_pair(i, j));
seen[i][j] = true;
}
}
}
int sum_size = 0;
for (int i = 0; i < p; i++) {
update(i);
sum_size += cells[i].size();
}
int i = 0;
while (sum_size > 0) {
sum_size -= cells[i].size();
for (int j = 0; j < s[i] && !cells[i].empty(); j++) {
for (pair<int, int>& p : cells[i]) {
for (int t = 0; t < 4; t++) {
int x = p.first + moves[t].first;
int y = p.second + moves[t].second;
if (0 <= x && x < n && 0 <= y && y < m && grid[x][y] < 0 &&
!seen[x][y]) {
seen[x][y] = true;
tmp.push(make_pair(x, y));
}
}
}
while (!tmp.empty()) {
pair<int, int> p = tmp.front();
tmp.pop();
grid[p.first][p.second] = i;
cells[i].push_back(p);
}
update(i);
}
sum_size += cells[i].size();
i = (i + 1) % p;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (0 <= grid[i][j] && grid[i][j] < p) sol[grid[i][j]]++;
}
}
for (int i = 0; i < p; i++) cout << sol[i] << " ";
cout << endl;
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, p, s[10], grid[1000][1000], sol[10];
list<pair<int, int>> cells[10];
bool seen[1000][1000];
queue<pair<int, int>> tmp;
pair<int, int> moves[4];
bool ok(const pair<int, int>& p) {
for (int i = 0; i < 4; i++) {
int x = p.first + moves[i].first;
int y = p.second + moves[i].second;
if (0 <= x && x < n && 0 <= y && y < m && grid[x][y] < 0) return true;
}
return false;
}
void update(int i) {
for (auto it = cells[i].begin(); it != cells[i].end();) {
if (!ok(*it))
it = cells[i].erase(it);
else
++it;
}
}
int main() {
moves[0] = make_pair(-1, 0);
moves[1] = make_pair(1, 0);
moves[2] = make_pair(0, 1);
moves[3] = make_pair(0, -1);
ios_base::sync_with_stdio(false);
cin >> n >> m >> p;
for (int i = 0; i < p; i++) cin >> s[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
if (c == '#')
grid[i][j] = p;
else if (c == '.')
grid[i][j] = -1;
else {
grid[i][j] = (int)c - '1';
cells[grid[i][j]].push_back(make_pair(i, j));
seen[i][j] = true;
}
}
}
int sum_size = 0;
for (int i = 0; i < p; i++) {
update(i);
sum_size += cells[i].size();
}
int i = 0;
while (sum_size > 0) {
sum_size -= cells[i].size();
for (int j = 0; j < s[i] && !cells[i].empty(); j++) {
for (pair<int, int>& p : cells[i]) {
for (int t = 0; t < 4; t++) {
int x = p.first + moves[t].first;
int y = p.second + moves[t].second;
if (0 <= x && x < n && 0 <= y && y < m && grid[x][y] < 0 &&
!seen[x][y]) {
seen[x][y] = true;
tmp.push(make_pair(x, y));
}
}
}
while (!tmp.empty()) {
pair<int, int> p = tmp.front();
tmp.pop();
grid[p.first][p.second] = i;
cells[i].push_back(p);
}
update(i);
}
sum_size += cells[i].size();
i = (i + 1) % p;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (0 <= grid[i][j] && grid[i][j] < p) sol[grid[i][j]]++;
}
}
for (int i = 0; i < p; i++) cout << sol[i] << " ";
cout << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
char board[1003][1003];
int t[1003][1003];
int speed[10];
int odw[1003][1003];
int main() {
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= p; ++i) scanf("%d", &speed[i]);
for (int i = 1; i <= n; ++i) {
scanf("%s", board[i]);
}
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < m; ++j) {
if (board[i][j] == '.')
t[i][j + 1] = 0;
else if (board[i][j] == '#')
t[i][j + 1] = -1;
else
t[i][j + 1] = board[i][j] - (int)'0';
}
}
queue<pair<pair<int, int>, pair<int, int>>> q;
for (int k = 1; k <= p; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (t[i][j] == k) q.push({{i, j}, {k, 0}});
}
}
}
for (int i = 1; i <= n; ++i) {
t[i][0] = -1;
t[i][m + 1] = -1;
}
for (int i = 1; i <= m; ++i) {
t[0][i] = -1;
t[n + 1][i] = -1;
}
queue<pair<pair<int, int>, pair<int, int>>> q2;
while (!q.empty()) {
auto w = q.front();
q.pop();
int x = w.first.first;
int y = w.first.second;
int playerId = w.second.first;
int dist = w.second.second;
if (t[x + 1][y] == 0) {
t[x + 1][y] = playerId;
q2.push({{x + 1, y}, {playerId, speed[playerId] - 1}});
}
if (t[x - 1][y] == 0) {
t[x - 1][y] = playerId;
q2.push({{x - 1, y}, {playerId, speed[playerId] - 1}});
}
if (t[x][y - 1] == 0) {
t[x][y - 1] = playerId;
q2.push({{x, y - 1}, {playerId, speed[playerId] - 1}});
}
if (t[x][y + 1] == 0) {
t[x][y + 1] = playerId;
q2.push({{x, y + 1}, {playerId, speed[playerId] - 1}});
}
if (!q.empty() && q.front().second.first == playerId) continue;
while (!q2.empty()) {
auto w = q2.front();
q2.pop();
int x = w.first.first;
int y = w.first.second;
int playerId = w.second.first;
int dist = w.second.second;
if (dist == 0) {
assert(t[x][y] == playerId);
q.push({{x, y}, {playerId, dist}});
continue;
}
if (t[x + 1][y] == 0) {
t[x + 1][y] = playerId;
q2.push({{x + 1, y}, {playerId, dist - 1}});
}
if (t[x - 1][y] == 0) {
t[x - 1][y] = playerId;
q2.push({{x - 1, y}, {playerId, dist - 1}});
}
if (t[x][y - 1] == 0) {
t[x][y - 1] = playerId;
q2.push({{x, y - 1}, {playerId, dist - 1}});
}
if (t[x][y + 1] == 0) {
t[x][y + 1] = playerId;
q2.push({{x, y + 1}, {playerId, dist - 1}});
}
}
}
for (int k = 1; k <= p; ++k) {
int cnt = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (t[i][j] == k) ++cnt;
}
}
printf("%d ", cnt);
}
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
char board[1003][1003];
int t[1003][1003];
int speed[10];
int odw[1003][1003];
int main() {
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= p; ++i) scanf("%d", &speed[i]);
for (int i = 1; i <= n; ++i) {
scanf("%s", board[i]);
}
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < m; ++j) {
if (board[i][j] == '.')
t[i][j + 1] = 0;
else if (board[i][j] == '#')
t[i][j + 1] = -1;
else
t[i][j + 1] = board[i][j] - (int)'0';
}
}
queue<pair<pair<int, int>, pair<int, int>>> q;
for (int k = 1; k <= p; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (t[i][j] == k) q.push({{i, j}, {k, 0}});
}
}
}
for (int i = 1; i <= n; ++i) {
t[i][0] = -1;
t[i][m + 1] = -1;
}
for (int i = 1; i <= m; ++i) {
t[0][i] = -1;
t[n + 1][i] = -1;
}
queue<pair<pair<int, int>, pair<int, int>>> q2;
while (!q.empty()) {
auto w = q.front();
q.pop();
int x = w.first.first;
int y = w.first.second;
int playerId = w.second.first;
int dist = w.second.second;
if (t[x + 1][y] == 0) {
t[x + 1][y] = playerId;
q2.push({{x + 1, y}, {playerId, speed[playerId] - 1}});
}
if (t[x - 1][y] == 0) {
t[x - 1][y] = playerId;
q2.push({{x - 1, y}, {playerId, speed[playerId] - 1}});
}
if (t[x][y - 1] == 0) {
t[x][y - 1] = playerId;
q2.push({{x, y - 1}, {playerId, speed[playerId] - 1}});
}
if (t[x][y + 1] == 0) {
t[x][y + 1] = playerId;
q2.push({{x, y + 1}, {playerId, speed[playerId] - 1}});
}
if (!q.empty() && q.front().second.first == playerId) continue;
while (!q2.empty()) {
auto w = q2.front();
q2.pop();
int x = w.first.first;
int y = w.first.second;
int playerId = w.second.first;
int dist = w.second.second;
if (dist == 0) {
assert(t[x][y] == playerId);
q.push({{x, y}, {playerId, dist}});
continue;
}
if (t[x + 1][y] == 0) {
t[x + 1][y] = playerId;
q2.push({{x + 1, y}, {playerId, dist - 1}});
}
if (t[x - 1][y] == 0) {
t[x - 1][y] = playerId;
q2.push({{x - 1, y}, {playerId, dist - 1}});
}
if (t[x][y - 1] == 0) {
t[x][y - 1] = playerId;
q2.push({{x, y - 1}, {playerId, dist - 1}});
}
if (t[x][y + 1] == 0) {
t[x][y + 1] = playerId;
q2.push({{x, y + 1}, {playerId, dist - 1}});
}
}
}
for (int k = 1; k <= p; ++k) {
int cnt = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (t[i][j] == k) ++cnt;
}
}
printf("%d ", cnt);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > p[15];
int s[15];
string a[1234];
int ans[15];
int n, m, np;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid(int y, int x) { return y >= 0 && x >= 0 && y < n && x < m; }
int bfs(int x) {
queue<pair<pair<int, int>, int> > q;
for (int i = 0; i < ((int)p[x].size()); i++) {
q.push(make_pair(make_pair(p[x][i].first, p[x][i].second), 0));
}
p[x].clear();
int max_depth = 0;
while (!q.empty()) {
pair<pair<int, int>, int> u = q.front();
q.pop();
int cur_y = u.first.first;
int cur_x = u.first.second;
int depth = u.second;
max_depth = max(max_depth, depth);
for (int j = 0; j < 4; j++) {
int next_y = cur_y + dy[j];
int next_x = cur_x + dx[j];
if (valid(next_y, next_x) && depth + 1 <= s[x] &&
a[next_y][next_x] == '.') {
q.push(make_pair(make_pair(next_y, next_x), depth + 1));
a[next_y][next_x] = '1' + x;
p[x].push_back(make_pair(next_y, next_x));
}
}
}
return (max_depth > 0);
}
bool can_go() {
int player_can_go = 0;
for (int i = 0; i < np; i++) {
player_can_go += bfs(i);
}
return (player_can_go > 0);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> np;
for (int i = 0; i < np; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] >= '1' && a[i][j] <= '9') {
p[a[i][j] - '1'].push_back(make_pair(i, j));
}
}
}
while (can_go())
;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] >= '1' && a[i][j] <= '9') {
ans[a[i][j] - '1']++;
}
}
}
for (int i = 0; i < np; i++) {
if (i > 0) cout << ' ';
cout << ans[i];
}
cout << '\n';
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > p[15];
int s[15];
string a[1234];
int ans[15];
int n, m, np;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid(int y, int x) { return y >= 0 && x >= 0 && y < n && x < m; }
int bfs(int x) {
queue<pair<pair<int, int>, int> > q;
for (int i = 0; i < ((int)p[x].size()); i++) {
q.push(make_pair(make_pair(p[x][i].first, p[x][i].second), 0));
}
p[x].clear();
int max_depth = 0;
while (!q.empty()) {
pair<pair<int, int>, int> u = q.front();
q.pop();
int cur_y = u.first.first;
int cur_x = u.first.second;
int depth = u.second;
max_depth = max(max_depth, depth);
for (int j = 0; j < 4; j++) {
int next_y = cur_y + dy[j];
int next_x = cur_x + dx[j];
if (valid(next_y, next_x) && depth + 1 <= s[x] &&
a[next_y][next_x] == '.') {
q.push(make_pair(make_pair(next_y, next_x), depth + 1));
a[next_y][next_x] = '1' + x;
p[x].push_back(make_pair(next_y, next_x));
}
}
}
return (max_depth > 0);
}
bool can_go() {
int player_can_go = 0;
for (int i = 0; i < np; i++) {
player_can_go += bfs(i);
}
return (player_can_go > 0);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> np;
for (int i = 0; i < np; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] >= '1' && a[i][j] <= '9') {
p[a[i][j] - '1'].push_back(make_pair(i, j));
}
}
}
while (can_go())
;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] >= '1' && a[i][j] <= '9') {
ans[a[i][j] - '1']++;
}
}
}
for (int i = 0; i < np; i++) {
if (i > 0) cout << ' ';
cout << ans[i];
}
cout << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1.1e3;
char maze[maxn][maxn];
int n, m, pe;
deque<pair<int, int> > q_point[11];
int dr[4] = {1, -1, 0, 0};
int dc[4] = {0, 0, 1, -1};
int re[11];
void init() {
for (int i = 0; i < 11; ++i) {
re[i] = 0;
}
}
pair<int, int> get_next_point(const pair<int, int> &p, int dir) {
pair<int, int> n_p;
n_p.first = p.first + dr[dir];
n_p.second = p.second + dc[dir];
return n_p;
}
bool judge(const pair<int, int> &p, int dir) {
pair<int, int> n_p = get_next_point(p, dir);
return n_p.first >= 0 && n_p.first < n && n_p.second >= 0 && n_p.second < m &&
maze[n_p.first][n_p.second] == '.';
}
bool bfs(deque<pair<int, int> > &q, int speed, int id) {
bool change_flag = false;
if (q.size() == 0) {
return change_flag;
}
pair<int, int> end = q.back();
int cur_layer = 0;
while (q.size()) {
pair<int, int> p = q.front();
q.pop_front();
for (int i = 0; i < 4; ++i) {
if (judge(p, i)) {
pair<int, int> n_p = get_next_point(p, i);
maze[n_p.first][n_p.second] = '#';
q.push_back(n_p);
re[id]++;
change_flag = true;
}
}
if (p == end) {
cur_layer++;
if (q.size() != 0) {
end = q.back();
}
}
if (cur_layer == speed) {
break;
}
}
return change_flag;
}
int main() {
init();
scanf("%d%d%d", &n, &m, &pe);
vector<int> speed;
for (int i = 0; i < pe; ++i) {
int s;
scanf("%d", &s);
speed.push_back(s);
}
for (int i = 0; i < n; ++i) {
scanf("%s", maze[i]);
}
pair<int, int> p;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
char c = maze[i][j];
if (c >= '1' && c <= '9') {
p = make_pair(i, j);
q_point[c - '0'].push_back(p);
re[c - '0']++;
}
}
}
while (1) {
int cnt = 0;
for (int i = 1; i <= pe; ++i) {
if (bfs(q_point[i], speed[i - 1], i)) {
cnt++;
}
}
if (cnt == 0) break;
}
for (int i = 1; i <= pe; ++i) {
printf("%d ", re[i]);
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1.1e3;
char maze[maxn][maxn];
int n, m, pe;
deque<pair<int, int> > q_point[11];
int dr[4] = {1, -1, 0, 0};
int dc[4] = {0, 0, 1, -1};
int re[11];
void init() {
for (int i = 0; i < 11; ++i) {
re[i] = 0;
}
}
pair<int, int> get_next_point(const pair<int, int> &p, int dir) {
pair<int, int> n_p;
n_p.first = p.first + dr[dir];
n_p.second = p.second + dc[dir];
return n_p;
}
bool judge(const pair<int, int> &p, int dir) {
pair<int, int> n_p = get_next_point(p, dir);
return n_p.first >= 0 && n_p.first < n && n_p.second >= 0 && n_p.second < m &&
maze[n_p.first][n_p.second] == '.';
}
bool bfs(deque<pair<int, int> > &q, int speed, int id) {
bool change_flag = false;
if (q.size() == 0) {
return change_flag;
}
pair<int, int> end = q.back();
int cur_layer = 0;
while (q.size()) {
pair<int, int> p = q.front();
q.pop_front();
for (int i = 0; i < 4; ++i) {
if (judge(p, i)) {
pair<int, int> n_p = get_next_point(p, i);
maze[n_p.first][n_p.second] = '#';
q.push_back(n_p);
re[id]++;
change_flag = true;
}
}
if (p == end) {
cur_layer++;
if (q.size() != 0) {
end = q.back();
}
}
if (cur_layer == speed) {
break;
}
}
return change_flag;
}
int main() {
init();
scanf("%d%d%d", &n, &m, &pe);
vector<int> speed;
for (int i = 0; i < pe; ++i) {
int s;
scanf("%d", &s);
speed.push_back(s);
}
for (int i = 0; i < n; ++i) {
scanf("%s", maze[i]);
}
pair<int, int> p;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
char c = maze[i][j];
if (c >= '1' && c <= '9') {
p = make_pair(i, j);
q_point[c - '0'].push_back(p);
re[c - '0']++;
}
}
}
while (1) {
int cnt = 0;
for (int i = 1; i <= pe; ++i) {
if (bfs(q_point[i], speed[i - 1], i)) {
cnt++;
}
}
if (cnt == 0) break;
}
for (int i = 1; i <= pe; ++i) {
printf("%d ", re[i]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1005;
long long n, m, p, remaining = 0, nochanges = 0;
long long s[20], mx[20], ans[20], stmx[20];
long long level[N][N];
char a[N][N];
queue<pair<long long, long long> > q[20];
long long dx[4] = {0, 0, +1, -1};
long long dy[4] = {+1, -1, 0, 0};
bool check(long long x, long long y) {
if (x < 1 || x > n || y < 1 || y > m) return 0;
return (a[x][y] == '.');
}
void work(long long idx) {
bool checker = 1;
while (q[idx].size() && remaining > 0) {
pair<long long, long long> p = q[idx].front();
long long x = p.first;
long long y = p.second;
if (level[x][y] == mx[idx]) {
nochanges += checker;
return;
}
q[idx].pop();
for (long long dir = 0; dir < 4; dir++) {
long long nx = x + dx[dir];
long long ny = y + dy[dir];
if (check(nx, ny)) {
checker = 0;
nochanges = 0;
ans[idx]++;
remaining--;
a[nx][ny] = (char)('0' + idx);
q[idx].push({nx, ny});
level[nx][ny] = level[x][y] + 1;
}
}
}
nochanges += checker;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m >> p;
for (long long i = 1; i <= p; i++) {
cin >> s[i];
mx[i] = s[i];
}
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) {
cin >> a[i][j];
remaining += (a[i][j] == '.');
if (isdigit(a[i][j])) {
q[a[i][j] - '0'].push({i, j});
level[i][j] = 0;
ans[a[i][j] - '0']++;
}
}
}
long long player = 0;
while (remaining > 0 && nochanges < p) {
work(player + 1);
mx[player + 1] += s[player + 1];
player++;
player %= p;
}
for (long long i = 1; i <= p; i++) cout << ans[i] << " ";
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 1005;
long long n, m, p, remaining = 0, nochanges = 0;
long long s[20], mx[20], ans[20], stmx[20];
long long level[N][N];
char a[N][N];
queue<pair<long long, long long> > q[20];
long long dx[4] = {0, 0, +1, -1};
long long dy[4] = {+1, -1, 0, 0};
bool check(long long x, long long y) {
if (x < 1 || x > n || y < 1 || y > m) return 0;
return (a[x][y] == '.');
}
void work(long long idx) {
bool checker = 1;
while (q[idx].size() && remaining > 0) {
pair<long long, long long> p = q[idx].front();
long long x = p.first;
long long y = p.second;
if (level[x][y] == mx[idx]) {
nochanges += checker;
return;
}
q[idx].pop();
for (long long dir = 0; dir < 4; dir++) {
long long nx = x + dx[dir];
long long ny = y + dy[dir];
if (check(nx, ny)) {
checker = 0;
nochanges = 0;
ans[idx]++;
remaining--;
a[nx][ny] = (char)('0' + idx);
q[idx].push({nx, ny});
level[nx][ny] = level[x][y] + 1;
}
}
}
nochanges += checker;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m >> p;
for (long long i = 1; i <= p; i++) {
cin >> s[i];
mx[i] = s[i];
}
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) {
cin >> a[i][j];
remaining += (a[i][j] == '.');
if (isdigit(a[i][j])) {
q[a[i][j] - '0'].push({i, j});
level[i][j] = 0;
ans[a[i][j] - '0']++;
}
}
}
long long player = 0;
while (remaining > 0 && nochanges < p) {
work(player + 1);
mx[player + 1] += s[player + 1];
player++;
player %= p;
}
for (long long i = 1; i <= p; i++) cout << ans[i] << " ";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int vis[1005][1005], sp[1005], cnt[1005];
char s[1005][1005];
int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};
struct node {
int x, y, steps;
};
int main() {
int n, m, p;
cin >> n >> m >> p;
for (int i = 1; i <= p; i++) cin >> sp[i];
queue<node> q[10];
for (int i = 1; i <= n; i++) {
cin >> s[i] + 1;
for (int j = 1; j <= m; j++)
if (s[i][j] >= '1' && s[i][j] <= '9') {
vis[i][j] = i;
cnt[s[i][j] - '0']++;
q[s[i][j] - '0'].push(node{i, j, 0});
} else if (s[i][j] == '#')
vis[i][j] = 10;
}
while (1) {
int ok = 0;
for (int i = 1; i <= p; i++)
if (!q[i].empty()) {
ok = 1;
break;
}
if (!ok) break;
for (int i = 1; i <= p; i++) {
queue<node> q2;
while (!q[i].empty()) {
q2.push(q[i].front());
q[i].pop();
}
while (!q2.empty()) {
node tmp = q2.front();
q2.pop();
int xx = tmp.x, yy = tmp.y;
for (int j = 0; j < 4; j++) {
int newx = xx + dx[j], newy = yy + dy[j], st = tmp.steps + 1;
if (!vis[newx][newy] && newx >= 1 && newx <= n && newy >= 1 &&
newy <= m && st <= sp[i]) {
q[i].push(node{newx, newy, 0});
q2.push(node{newx, newy, st});
vis[newx][newy] = 1;
cnt[i]++;
}
}
}
}
}
for (int i = 1; i <= p; i++) cout << cnt[i] << " ";
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int vis[1005][1005], sp[1005], cnt[1005];
char s[1005][1005];
int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};
struct node {
int x, y, steps;
};
int main() {
int n, m, p;
cin >> n >> m >> p;
for (int i = 1; i <= p; i++) cin >> sp[i];
queue<node> q[10];
for (int i = 1; i <= n; i++) {
cin >> s[i] + 1;
for (int j = 1; j <= m; j++)
if (s[i][j] >= '1' && s[i][j] <= '9') {
vis[i][j] = i;
cnt[s[i][j] - '0']++;
q[s[i][j] - '0'].push(node{i, j, 0});
} else if (s[i][j] == '#')
vis[i][j] = 10;
}
while (1) {
int ok = 0;
for (int i = 1; i <= p; i++)
if (!q[i].empty()) {
ok = 1;
break;
}
if (!ok) break;
for (int i = 1; i <= p; i++) {
queue<node> q2;
while (!q[i].empty()) {
q2.push(q[i].front());
q[i].pop();
}
while (!q2.empty()) {
node tmp = q2.front();
q2.pop();
int xx = tmp.x, yy = tmp.y;
for (int j = 0; j < 4; j++) {
int newx = xx + dx[j], newy = yy + dy[j], st = tmp.steps + 1;
if (!vis[newx][newy] && newx >= 1 && newx <= n && newy >= 1 &&
newy <= m && st <= sp[i]) {
q[i].push(node{newx, newy, 0});
q2.push(node{newx, newy, st});
vis[newx][newy] = 1;
cnt[i]++;
}
}
}
}
}
for (int i = 1; i <= p; i++) cout << cnt[i] << " ";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
long long get_rand() {
long long a = rand(), ft = 31, b = rand();
return (a << ft) ^ b;
}
int gcd(int a, int b) {
while (a && b) {
a %= b;
swap(a, b);
}
return a + b;
}
const int M = 1000000007;
long long cnt[10];
char f[1002][1002];
int str[10];
vector<queue<pair<pair<int, int>, pair<int, int> > > > vec;
int n, m;
void bfs() {
bool cond = true;
while (cond) {
cond = false;
for (int j = 0; j < vec.size(); j++) {
if (vec[j].empty()) continue;
int t = vec[j].front().second.second;
while (!vec[j].empty() && vec[j].front().second.second == t) {
++cnt[j];
auto p = vec[j].front();
vec[j].pop();
int x = p.first.first;
int y = p.first.second;
int d = p.second.first;
if (x && f[x - 1][y] == '.') {
f[x - 1][y] = '0' + j;
if (d > 1)
vec[j].push({{x - 1, y}, {d - 1, t}});
else
vec[j].push({{x - 1, y}, {str[j], t + 1}});
}
if (y && f[x][y - 1] == '.') {
f[x][y - 1] = '0' + j;
if (d > 1)
vec[j].push({{x, y - 1}, {d - 1, t}});
else
vec[j].push({{x, y - 1}, {str[j], t + 1}});
}
if (x < n - 1 && f[x + 1][y] == '.') {
f[x + 1][y] = '0' + j;
if (d > 1)
vec[j].push({{x + 1, y}, {d - 1, t}});
else
vec[j].push({{x + 1, y}, {str[j], t + 1}});
}
if (y < m - 1 && f[x][y + 1] == '.') {
f[x][y + 1] = '0' + j;
if (d > 1)
vec[j].push({{x, y + 1}, {d - 1, t}});
else
vec[j].push({{x, y + 1}, {str[j], t + 1}});
}
}
if (!vec[j].empty()) cond = true;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int p;
cin >> n >> m >> p;
for (int j = 1; j <= p; j++) cin >> str[j];
vec.resize(p + 1);
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
cin >> f[j][i];
if (f[j][i] >= '0' && f[j][i] <= '9') {
vec[f[j][i] - '0'].push({{j, i}, {str[f[j][i] - '0'], 0}});
}
}
}
bfs();
for (int j = 1; j <= p; j++) {
cout << cnt[j] << ' ';
}
}
| ### Prompt
Please create a solution in CPP to the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
long long get_rand() {
long long a = rand(), ft = 31, b = rand();
return (a << ft) ^ b;
}
int gcd(int a, int b) {
while (a && b) {
a %= b;
swap(a, b);
}
return a + b;
}
const int M = 1000000007;
long long cnt[10];
char f[1002][1002];
int str[10];
vector<queue<pair<pair<int, int>, pair<int, int> > > > vec;
int n, m;
void bfs() {
bool cond = true;
while (cond) {
cond = false;
for (int j = 0; j < vec.size(); j++) {
if (vec[j].empty()) continue;
int t = vec[j].front().second.second;
while (!vec[j].empty() && vec[j].front().second.second == t) {
++cnt[j];
auto p = vec[j].front();
vec[j].pop();
int x = p.first.first;
int y = p.first.second;
int d = p.second.first;
if (x && f[x - 1][y] == '.') {
f[x - 1][y] = '0' + j;
if (d > 1)
vec[j].push({{x - 1, y}, {d - 1, t}});
else
vec[j].push({{x - 1, y}, {str[j], t + 1}});
}
if (y && f[x][y - 1] == '.') {
f[x][y - 1] = '0' + j;
if (d > 1)
vec[j].push({{x, y - 1}, {d - 1, t}});
else
vec[j].push({{x, y - 1}, {str[j], t + 1}});
}
if (x < n - 1 && f[x + 1][y] == '.') {
f[x + 1][y] = '0' + j;
if (d > 1)
vec[j].push({{x + 1, y}, {d - 1, t}});
else
vec[j].push({{x + 1, y}, {str[j], t + 1}});
}
if (y < m - 1 && f[x][y + 1] == '.') {
f[x][y + 1] = '0' + j;
if (d > 1)
vec[j].push({{x, y + 1}, {d - 1, t}});
else
vec[j].push({{x, y + 1}, {str[j], t + 1}});
}
}
if (!vec[j].empty()) cond = true;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int p;
cin >> n >> m >> p;
for (int j = 1; j <= p; j++) cin >> str[j];
vec.resize(p + 1);
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
cin >> f[j][i];
if (f[j][i] >= '0' && f[j][i] <= '9') {
vec[f[j][i] - '0'].push({{j, i}, {str[f[j][i] - '0'], 0}});
}
}
}
bfs();
for (int j = 1; j <= p; j++) {
cout << cnt[j] << ' ';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > q[10];
int s[11];
int can[11];
int dist[1001][1001];
char a[1001][1001];
bool vis[1001][1001];
int num[100];
int n, m;
int mv[][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
bool bfs(int id) {
bool ret = false;
can[id] += s[id];
while (!q[id].empty()) {
pair<int, int> p = q[id].front();
int x = p.first, y = p.second;
if (dist[x][y] == can[id]) break;
q[id].pop();
for (int i = 0; i < 4; i++) {
int X = x + mv[i][0];
int Y = y + mv[i][1];
if (X < 0 || X >= n || Y < 0 || Y >= m) continue;
if (vis[X][Y] || a[X][Y] == '#') continue;
num[id]++;
dist[X][Y] = dist[x][y] + 1;
vis[X][Y] = 1;
ret = true;
q[id].push({X, Y});
}
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int p;
cin >> n >> m >> p;
for (int i = 1; i <= p; i++) cin >> s[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (a[i][j] >= '1' && a[i][j] <= '9') {
int id = a[i][j] - '0';
q[id].push({i, j});
num[id]++;
vis[i][j] = 1;
}
}
}
while (1) {
bool ok = false;
for (int i = 1; i <= p; i++) ok |= bfs(i);
if (!ok) break;
}
for (int i = 1; i <= p; i++) cout << num[i] << ' ';
cout << endl;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > q[10];
int s[11];
int can[11];
int dist[1001][1001];
char a[1001][1001];
bool vis[1001][1001];
int num[100];
int n, m;
int mv[][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
bool bfs(int id) {
bool ret = false;
can[id] += s[id];
while (!q[id].empty()) {
pair<int, int> p = q[id].front();
int x = p.first, y = p.second;
if (dist[x][y] == can[id]) break;
q[id].pop();
for (int i = 0; i < 4; i++) {
int X = x + mv[i][0];
int Y = y + mv[i][1];
if (X < 0 || X >= n || Y < 0 || Y >= m) continue;
if (vis[X][Y] || a[X][Y] == '#') continue;
num[id]++;
dist[X][Y] = dist[x][y] + 1;
vis[X][Y] = 1;
ret = true;
q[id].push({X, Y});
}
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int p;
cin >> n >> m >> p;
for (int i = 1; i <= p; i++) cin >> s[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (a[i][j] >= '1' && a[i][j] <= '9') {
int id = a[i][j] - '0';
q[id].push({i, j});
num[id]++;
vis[i][j] = 1;
}
}
}
while (1) {
bool ok = false;
for (int i = 1; i <= p; i++) ok |= bfs(i);
if (!ok) break;
}
for (int i = 1; i <= p; i++) cout << num[i] << ' ';
cout << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
const int mod = (1e9) + 7;
const double eps = 1e-10;
const int siz = 2e5 + 5, siz2 = 256, lg = 21, block = 317, block2 = 1000,
extrm[2] = {INT_MIN, INT_MAX};
int n, m, p, sp[10];
struct el {
int x, y, c;
;
};
queue<el> q[10];
int board[1005][1005], ver[1005][1005];
pair<int, int> d[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int ans[10];
bool valid(int v, int lim) { return v >= 0 && v < lim; }
int main() {
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= p; i++) {
scanf("%d", &sp[i]);
}
char tmp;
scanf("%c", &tmp);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%c", &board[i][j]);
if (board[i][j] >= '1' && board[i][j] <= '9') {
q[board[i][j] - '0'].push({i, j, 0});
}
}
char tmp;
scanf("%c", &tmp);
}
for (int i = 1;; i++) {
bool moved = false;
for (int j = 1; j <= p; j++) {
while (!q[j].empty()) {
el e = q[j].front();
int xv = e.x, yv = e.y, val = e.c;
if (ver[xv][yv] != i) {
val = 0;
}
if (val == sp[j]) {
break;
}
q[j].pop();
for (int ii = 0; ii < 4; ii++) {
int nx = xv + d[ii].first, ny = yv + d[ii].second;
if (valid(nx, n) && valid(ny, m) && board[nx][ny] == '.') {
moved = true;
ver[nx][ny] = i;
q[j].push({nx, ny, val + 1});
board[nx][ny] = j + '0';
}
}
}
}
if (!moved) {
break;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (board[i][j] >= '1' && board[i][j] <= '9') {
ans[board[i][j] - '0']++;
}
}
}
for (int i = 1; i <= p; i++) {
printf("%d%s", ans[i], (i < p ? " " : "\n"));
}
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
const int mod = (1e9) + 7;
const double eps = 1e-10;
const int siz = 2e5 + 5, siz2 = 256, lg = 21, block = 317, block2 = 1000,
extrm[2] = {INT_MIN, INT_MAX};
int n, m, p, sp[10];
struct el {
int x, y, c;
;
};
queue<el> q[10];
int board[1005][1005], ver[1005][1005];
pair<int, int> d[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int ans[10];
bool valid(int v, int lim) { return v >= 0 && v < lim; }
int main() {
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= p; i++) {
scanf("%d", &sp[i]);
}
char tmp;
scanf("%c", &tmp);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%c", &board[i][j]);
if (board[i][j] >= '1' && board[i][j] <= '9') {
q[board[i][j] - '0'].push({i, j, 0});
}
}
char tmp;
scanf("%c", &tmp);
}
for (int i = 1;; i++) {
bool moved = false;
for (int j = 1; j <= p; j++) {
while (!q[j].empty()) {
el e = q[j].front();
int xv = e.x, yv = e.y, val = e.c;
if (ver[xv][yv] != i) {
val = 0;
}
if (val == sp[j]) {
break;
}
q[j].pop();
for (int ii = 0; ii < 4; ii++) {
int nx = xv + d[ii].first, ny = yv + d[ii].second;
if (valid(nx, n) && valid(ny, m) && board[nx][ny] == '.') {
moved = true;
ver[nx][ny] = i;
q[j].push({nx, ny, val + 1});
board[nx][ny] = j + '0';
}
}
}
}
if (!moved) {
break;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (board[i][j] >= '1' && board[i][j] <= '9') {
ans[board[i][j] - '0']++;
}
}
}
for (int i = 1; i <= p; i++) {
printf("%d%s", ans[i], (i < p ? " " : "\n"));
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void bye() { exit(0); }
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
bool in(int x, int y, int n, int m) {
return (x >= 0 && y >= 0 && x < n && y < m);
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
long long int t, pro = 1, temp, k, n, m, i, j, l, r, mid, x, y, z, rem, ind,
ans = 0, mx = LONG_LONG_MIN, mn = LONG_LONG_MAX, cnt = 0,
curr = 0, prev, sum = 0, flag = 0, i1 = -1, i2 = -1;
cin >> n >> m >> k;
long long int a[k + 1], rg[k + 1];
for (i = 1; i <= k; i++) {
rg[i] = 0;
cin >> a[i];
a[i] = min(a[i], n * m);
}
string s[n];
vector<pair<int, int>> adj[k + 1];
for (i = 0; i <= n - 1; i++) {
cin >> s[i];
for (j = 0; j <= m - 1; j++) {
if (s[i][j] != '.' && s[i][j] != '#') {
adj[s[i][j] - '0'].push_back({i, j});
}
}
}
bool okay = true;
while (okay) {
okay = false;
for (i = 1; i <= k; i++) {
vector<pair<int, int>> prs;
queue<pair<pair<int, int>, int>> mq;
for (auto it : adj[i]) {
mq.push({it, a[i]});
}
while (!mq.empty()) {
auto it = mq.front();
mq.pop();
x = it.first.first;
y = it.first.second;
z = it.second;
for (j = 0; j <= 3; j++) {
if (in(x + dx[j], y + dy[j], n, m) &&
s[x + dx[j]][y + dy[j]] == '.') {
s[x + dx[j]][y + dy[j]] = i + '0';
prs.push_back({x + dx[j], y + dy[j]});
okay = true;
if (z > 1) {
mq.push({{x + dx[j], y + dy[j]}, z - 1});
}
}
}
}
adj[i] = prs;
}
}
for (i = 0; i <= n - 1; i++) {
for (j = 0; j <= m - 1; j++) {
if (s[i][j] != '.' && s[i][j] != '#') {
rg[s[i][j] - '0']++;
}
}
}
for (i = 1; i <= k; i++) {
cout << rg[i] << ' ';
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).
The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.
The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.
Input
The first line contains three integers n, m and p (1 β€ n, m β€ 1000, 1 β€ p β€ 9) β the size of the grid and the number of players.
The second line contains p integers s_i (1 β€ s β€ 10^9) β the speed of the expansion for every player.
The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 β€ x β€ p) denotes the castle owned by player x.
It is guaranteed, that each player has at least one castle on the grid.
Output
Print p integers β the number of cells controlled by each player after the game ends.
Examples
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
Note
The picture below show the game before it started, the game after the first round and game after the second round in the first example:
<image>
In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void bye() { exit(0); }
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
bool in(int x, int y, int n, int m) {
return (x >= 0 && y >= 0 && x < n && y < m);
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
long long int t, pro = 1, temp, k, n, m, i, j, l, r, mid, x, y, z, rem, ind,
ans = 0, mx = LONG_LONG_MIN, mn = LONG_LONG_MAX, cnt = 0,
curr = 0, prev, sum = 0, flag = 0, i1 = -1, i2 = -1;
cin >> n >> m >> k;
long long int a[k + 1], rg[k + 1];
for (i = 1; i <= k; i++) {
rg[i] = 0;
cin >> a[i];
a[i] = min(a[i], n * m);
}
string s[n];
vector<pair<int, int>> adj[k + 1];
for (i = 0; i <= n - 1; i++) {
cin >> s[i];
for (j = 0; j <= m - 1; j++) {
if (s[i][j] != '.' && s[i][j] != '#') {
adj[s[i][j] - '0'].push_back({i, j});
}
}
}
bool okay = true;
while (okay) {
okay = false;
for (i = 1; i <= k; i++) {
vector<pair<int, int>> prs;
queue<pair<pair<int, int>, int>> mq;
for (auto it : adj[i]) {
mq.push({it, a[i]});
}
while (!mq.empty()) {
auto it = mq.front();
mq.pop();
x = it.first.first;
y = it.first.second;
z = it.second;
for (j = 0; j <= 3; j++) {
if (in(x + dx[j], y + dy[j], n, m) &&
s[x + dx[j]][y + dy[j]] == '.') {
s[x + dx[j]][y + dy[j]] = i + '0';
prs.push_back({x + dx[j], y + dy[j]});
okay = true;
if (z > 1) {
mq.push({{x + dx[j], y + dy[j]}, z - 1});
}
}
}
}
adj[i] = prs;
}
}
for (i = 0; i <= n - 1; i++) {
for (j = 0; j <= m - 1; j++) {
if (s[i][j] != '.' && s[i][j] != '#') {
rg[s[i][j] - '0']++;
}
}
}
for (i = 1; i <= k; i++) {
cout << rg[i] << ' ';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename G>
struct triple {
G first, second, T;
};
struct segment_tree {
vector<int> st;
vector<int> lazy;
segment_tree(int n) : st(2 * n), lazy(2 * n) {}
inline int id(int b, int e) { return (b + e - 1) | (b != e - 1); }
void prop(int l, int r) {
int cur = id(l, r);
st[cur] += lazy[cur];
if (l != r - 1) {
int mid = (l + r + 1) >> 1;
lazy[id(l, mid)] += lazy[cur];
lazy[id(mid, r)] += lazy[cur];
}
lazy[cur] = 0;
}
void sum(int l, int r, int li, int ri, int v) {
if (li == ri) return;
int cur = id(l, r);
if (lazy[cur]) prop(l, r);
if (l >= ri || r <= li) return;
if (li <= l && r <= ri) {
lazy[cur] += v;
prop(l, r);
return;
}
int mid = (l + r + 1) >> 1;
sum(l, mid, li, ri, v);
sum(mid, r, li, ri, v);
st[cur] = max(st[id(l, mid)], st[id(mid, r)]);
}
int query(int l, int r, int li, int ri) {
int cur = id(l, r);
if (lazy[cur]) prop(l, r);
if (l >= ri || r <= li) return 0;
if (li <= l && r <= ri) return st[cur];
int mid = (l + r + 1) >> 1;
return max(query(0, mid, li, ri), query(mid, r, li, ri));
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> v(n);
for (auto &i : v) cin >> i;
vector<int> p(n + 1, -1);
stack<int> s;
for (int i = n - 1; i >= 0; i--) {
while (!s.empty() && v[s.top()] <= v[i]) s.pop();
if (!s.empty())
p[i] = s.top();
else
p[i] = n;
s.push(i);
}
++n;
vector<vector<int>> tree(n);
for (int i = 0; i < n - 1; i++) tree[p[i]].push_back(i);
vector<int> l(n), r(n);
int t = 0;
function<void(int)> dfs = [&](int c) {
l[c] = t++;
for (auto a : tree[c]) dfs(a);
r[c] = t;
};
dfs(n - 1);
segment_tree st(n);
for (int i = 0; i < k; i++) st.sum(0, n, l[i], r[i], 1);
for (int i = k; i < n - 1; i++) {
cout << st.query(0, n, 0, n) << " ";
st.sum(0, n, l[i - k], r[i - k], -1);
st.sum(0, n, l[i], r[i], 1);
}
cout << st.query(0, n, 0, n) << '\n';
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;
template <typename G>
struct triple {
G first, second, T;
};
struct segment_tree {
vector<int> st;
vector<int> lazy;
segment_tree(int n) : st(2 * n), lazy(2 * n) {}
inline int id(int b, int e) { return (b + e - 1) | (b != e - 1); }
void prop(int l, int r) {
int cur = id(l, r);
st[cur] += lazy[cur];
if (l != r - 1) {
int mid = (l + r + 1) >> 1;
lazy[id(l, mid)] += lazy[cur];
lazy[id(mid, r)] += lazy[cur];
}
lazy[cur] = 0;
}
void sum(int l, int r, int li, int ri, int v) {
if (li == ri) return;
int cur = id(l, r);
if (lazy[cur]) prop(l, r);
if (l >= ri || r <= li) return;
if (li <= l && r <= ri) {
lazy[cur] += v;
prop(l, r);
return;
}
int mid = (l + r + 1) >> 1;
sum(l, mid, li, ri, v);
sum(mid, r, li, ri, v);
st[cur] = max(st[id(l, mid)], st[id(mid, r)]);
}
int query(int l, int r, int li, int ri) {
int cur = id(l, r);
if (lazy[cur]) prop(l, r);
if (l >= ri || r <= li) return 0;
if (li <= l && r <= ri) return st[cur];
int mid = (l + r + 1) >> 1;
return max(query(0, mid, li, ri), query(mid, r, li, ri));
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> v(n);
for (auto &i : v) cin >> i;
vector<int> p(n + 1, -1);
stack<int> s;
for (int i = n - 1; i >= 0; i--) {
while (!s.empty() && v[s.top()] <= v[i]) s.pop();
if (!s.empty())
p[i] = s.top();
else
p[i] = n;
s.push(i);
}
++n;
vector<vector<int>> tree(n);
for (int i = 0; i < n - 1; i++) tree[p[i]].push_back(i);
vector<int> l(n), r(n);
int t = 0;
function<void(int)> dfs = [&](int c) {
l[c] = t++;
for (auto a : tree[c]) dfs(a);
r[c] = t;
};
dfs(n - 1);
segment_tree st(n);
for (int i = 0; i < k; i++) st.sum(0, n, l[i], r[i], 1);
for (int i = k; i < n - 1; i++) {
cout << st.query(0, n, 0, n) << " ";
st.sum(0, n, l[i - k], r[i - k], -1);
st.sum(0, n, l[i], r[i], 1);
}
cout << st.query(0, n, 0, n) << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> children[1000005];
int r_1[1000005], r_2[1000005], timer, n, k, p[1000005], mx[1000005 << 2],
inc[1000005 << 2];
stack<int> seq;
vector<int> ans;
void dfs(int node) {
timer++;
r_1[node] = timer;
for (int i = 0; i < children[node].size(); i++) {
int neb = children[node][i];
dfs(neb);
}
r_2[node] = timer;
}
int rt(int r1, int r2, int q1, int q2, int ind, int typ) {
if (q1 > q2) return 0;
if (q1 > r2 || q2 < r1) return 0;
if (q1 <= r1 && q2 >= r2) {
inc[ind] += typ;
return inc[ind] + mx[ind];
} else {
inc[ind * 2] += inc[ind];
inc[ind * 2 + 1] += inc[ind];
inc[ind] = 0;
int mid = (r1 + r2) / 2;
int left = 0, right = 0;
left = rt(r1, mid, q1, q2, ind * 2, typ);
right = rt(mid + 1, r2, q1, q2, ind * 2 + 1, typ);
mx[ind] =
max(mx[ind * 2] + inc[ind * 2], mx[ind * 2 + 1] + inc[ind * 2 + 1]);
return max(left, right);
}
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &p[i]);
p[n] = n + 1;
for (int i = 0; i <= n; i++) {
while (seq.size()) {
int top = seq.top();
if (p[top] >= p[i]) break;
children[i].push_back(top);
seq.pop();
}
seq.push(i);
}
dfs(n);
for (int i = n - 1; i >= 0; i--) {
if (i + k <= n) {
ans.push_back(rt(1, timer, r_1[i + k - 1], r_1[i], 1, 0) + 1);
rt(1, timer, r_1[i + k - 1] + 1, r_2[i + k - 1], 1, -1);
}
rt(1, timer, r_1[i] + 1, r_2[i], 1, 1);
}
reverse(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i++) {
printf("%d ", ans[i]);
}
printf("\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;
vector<int> children[1000005];
int r_1[1000005], r_2[1000005], timer, n, k, p[1000005], mx[1000005 << 2],
inc[1000005 << 2];
stack<int> seq;
vector<int> ans;
void dfs(int node) {
timer++;
r_1[node] = timer;
for (int i = 0; i < children[node].size(); i++) {
int neb = children[node][i];
dfs(neb);
}
r_2[node] = timer;
}
int rt(int r1, int r2, int q1, int q2, int ind, int typ) {
if (q1 > q2) return 0;
if (q1 > r2 || q2 < r1) return 0;
if (q1 <= r1 && q2 >= r2) {
inc[ind] += typ;
return inc[ind] + mx[ind];
} else {
inc[ind * 2] += inc[ind];
inc[ind * 2 + 1] += inc[ind];
inc[ind] = 0;
int mid = (r1 + r2) / 2;
int left = 0, right = 0;
left = rt(r1, mid, q1, q2, ind * 2, typ);
right = rt(mid + 1, r2, q1, q2, ind * 2 + 1, typ);
mx[ind] =
max(mx[ind * 2] + inc[ind * 2], mx[ind * 2 + 1] + inc[ind * 2 + 1]);
return max(left, right);
}
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &p[i]);
p[n] = n + 1;
for (int i = 0; i <= n; i++) {
while (seq.size()) {
int top = seq.top();
if (p[top] >= p[i]) break;
children[i].push_back(top);
seq.pop();
}
seq.push(i);
}
dfs(n);
for (int i = n - 1; i >= 0; i--) {
if (i + k <= n) {
ans.push_back(rt(1, timer, r_1[i + k - 1], r_1[i], 1, 0) + 1);
rt(1, timer, r_1[i + k - 1] + 1, r_2[i + k - 1], 1, -1);
}
rt(1, timer, r_1[i] + 1, r_2[i], 1, 1);
}
reverse(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i++) {
printf("%d ", ans[i]);
}
printf("\n");
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int INF = 1e9;
int a[N], maxv[N << 2], addv[N << 2];
int dmax[N][21];
int n, ans;
void maintain(int rt, int l, int r) {
maxv[rt] = 0;
if (l < r) maxv[rt] = max(maxv[rt << 1], maxv[rt << 1 | 1]);
maxv[rt] += addv[rt];
}
void update(int ql, int qr, int v, int rt, int l, int r) {
if (ql <= l && qr >= r)
addv[rt] += v;
else {
int mid = (l + r) >> 1;
if (ql <= mid) update(ql, qr, v, rt << 1, l, mid);
if (qr > mid) update(ql, qr, v, rt << 1 | 1, mid + 1, r);
}
maintain(rt, l, r);
}
void querymax(int ql, int qr, int rt, int l, int r, int add) {
if (ql <= l && qr >= r)
ans = max(ans, maxv[rt] + add);
else {
int mid = (l + r) >> 1;
if (ql <= mid) querymax(ql, qr, rt << 1, l, mid, add + addv[rt]);
if (qr > mid) querymax(ql, qr, rt << 1 | 1, mid + 1, r, add + addv[rt]);
}
}
void build() {
for (int i = 1; i <= n; i++) dmax[i][0] = a[i];
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
dmax[i][j] = max(dmax[i][j - 1], dmax[i + (1 << (j - 1))][j - 1]);
}
}
}
int query(int l, int r) {
int k = 0;
while ((1 << (k + 1)) <= r - l + 1) k++;
return max(dmax[l][k], dmax[r - (1 << k) + 1][k]);
}
int main() {
int k, num, l, r, mid;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
build();
memset(maxv, 0, sizeof(maxv));
for (int i = 1; i <= n; i++) {
l = 0;
r = i;
while (r - l > 1) {
mid = (l + r) >> 1;
if (query(mid, r - 1) >= a[i])
l = mid;
else
r = mid;
}
update(r, i, 1, 1, 1, n);
if (i >= k) {
ans = 0;
querymax(i - k + 1, i, 1, 1, n, 0);
printf("%d ", ans);
}
}
printf("\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 N = 1e6 + 10;
const int INF = 1e9;
int a[N], maxv[N << 2], addv[N << 2];
int dmax[N][21];
int n, ans;
void maintain(int rt, int l, int r) {
maxv[rt] = 0;
if (l < r) maxv[rt] = max(maxv[rt << 1], maxv[rt << 1 | 1]);
maxv[rt] += addv[rt];
}
void update(int ql, int qr, int v, int rt, int l, int r) {
if (ql <= l && qr >= r)
addv[rt] += v;
else {
int mid = (l + r) >> 1;
if (ql <= mid) update(ql, qr, v, rt << 1, l, mid);
if (qr > mid) update(ql, qr, v, rt << 1 | 1, mid + 1, r);
}
maintain(rt, l, r);
}
void querymax(int ql, int qr, int rt, int l, int r, int add) {
if (ql <= l && qr >= r)
ans = max(ans, maxv[rt] + add);
else {
int mid = (l + r) >> 1;
if (ql <= mid) querymax(ql, qr, rt << 1, l, mid, add + addv[rt]);
if (qr > mid) querymax(ql, qr, rt << 1 | 1, mid + 1, r, add + addv[rt]);
}
}
void build() {
for (int i = 1; i <= n; i++) dmax[i][0] = a[i];
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
dmax[i][j] = max(dmax[i][j - 1], dmax[i + (1 << (j - 1))][j - 1]);
}
}
}
int query(int l, int r) {
int k = 0;
while ((1 << (k + 1)) <= r - l + 1) k++;
return max(dmax[l][k], dmax[r - (1 << k) + 1][k]);
}
int main() {
int k, num, l, r, mid;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
build();
memset(maxv, 0, sizeof(maxv));
for (int i = 1; i <= n; i++) {
l = 0;
r = i;
while (r - l > 1) {
mid = (l + r) >> 1;
if (query(mid, r - 1) >= a[i])
l = mid;
else
r = mid;
}
update(r, i, 1, 1, 1, n);
if (i >= k) {
ans = 0;
querymax(i - k + 1, i, 1, 1, n, 0);
printf("%d ", ans);
}
}
printf("\n");
return 0;
}
``` |
#include <bits/stdc++.h>
std::mt19937 rng(
(int)std::chrono::steady_clock::now().time_since_epoch().count());
const int ms = 1 << 20;
const int INF = 1e9;
const int me = 20;
struct Node {
Node(int x = -INF) { mx = x; }
Node(Node &l, Node &r) { mx = std::max(l.mx, r.mx); }
void apply(int lazy) { mx += lazy; }
int mx;
};
template <class i_t, class e_t, class lazy_cont = int>
class SegmentTree {
public:
void init(std::vector<e_t> base) {
n = base.size();
h = 0;
while ((1 << h) < n) h++;
tree.resize(2 * n);
dirty.assign(n, false);
lazy.resize(n);
for (int i = 0; i < n; i++) {
tree[i + n] = i_t(base[i]);
}
for (int i = n - 1; i > 0; i--) {
tree[i] = i_t(tree[i + i], tree[i + i + 1]);
lazy[i] = 0;
}
}
i_t qry(int l, int r) {
if (l >= r) return i_t();
l += n, r += n;
push(l);
push(r - 1);
i_t lp, rp;
for (; l < r; l /= 2, r /= 2) {
if (l & 1) lp = i_t(lp, tree[l++]);
if (r & 1) rp = i_t(tree[--r], rp);
}
return i_t(lp, rp);
}
void upd(int l, int r, lazy_cont lc) {
if (l >= r) return;
l += n, r += n;
push(l);
push(r - 1);
int l0 = l, r0 = r;
for (; l < r; l /= 2, r /= 2) {
if (l & 1) apply(l++, lc);
if (r & 1) apply(--r, lc);
}
build(l0);
build(r0 - 1);
}
void upd(int x, e_t v) {
x += n;
push(x);
tree[x] = i_t(v);
build(x);
}
private:
int n, h;
std::vector<bool> dirty;
std::vector<i_t> tree;
std::vector<lazy_cont> lazy;
void apply(int p, lazy_cont &lc) {
tree[p].apply(lc);
if (p < n) {
dirty[p] = true;
lazy[p] += lc;
}
}
void push(int p) {
for (int s = h; s > 0; s--) {
int i = p >> s;
if (dirty[i]) {
apply(i + i, lazy[i]);
apply(i + i + 1, lazy[i]);
lazy[i] = 0;
dirty[i] = false;
}
}
}
void build(int p) {
for (p /= 2; p > 0; p /= 2) {
tree[p] = i_t(tree[p + p], tree[p + p + 1]);
if (dirty[p]) {
tree[p].apply(lazy[p]);
}
}
}
};
std::vector<int> edges[ms];
int to[ms];
int in[ms], out[ms], c = 0;
void dfs(int on) {
in[on] = c++;
for (auto too : edges[on]) {
dfs(too);
}
out[on] = c;
}
int ans[ms];
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n, k;
std::cin >> n >> k;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
a.push_back(n + 1);
std::vector<int> st;
st.push_back(n);
for (int i = n - 1; i >= 0; i--) {
while (a[st.back()] <= a[i]) {
st.pop_back();
}
to[i] = st.back();
edges[st.back()].push_back(i);
st.push_back(i);
}
SegmentTree<Node, int, int> tree, ok;
tree.init(std::vector<int>(n + 1, -INF));
ok.init(std::vector<int>(n + 1, 0));
dfs(n);
for (int i = n - 1; i >= 0; i--) {
{
ok.upd(in[i], out[i], 1);
tree.upd(in[i], out[i], 1);
tree.upd(in[i], ok.qry(in[i], in[i] + 1).mx);
}
if (i + k < n) {
ok.upd(in[i + k], out[i + k], -1);
tree.upd(in[i + k], out[i + k], -1);
}
ans[i] = tree.qry(0, n + 1).mx;
}
for (int i = 0; i + k <= n; i++) {
std::cout << ans[i] << '\n';
}
}
| ### 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>
std::mt19937 rng(
(int)std::chrono::steady_clock::now().time_since_epoch().count());
const int ms = 1 << 20;
const int INF = 1e9;
const int me = 20;
struct Node {
Node(int x = -INF) { mx = x; }
Node(Node &l, Node &r) { mx = std::max(l.mx, r.mx); }
void apply(int lazy) { mx += lazy; }
int mx;
};
template <class i_t, class e_t, class lazy_cont = int>
class SegmentTree {
public:
void init(std::vector<e_t> base) {
n = base.size();
h = 0;
while ((1 << h) < n) h++;
tree.resize(2 * n);
dirty.assign(n, false);
lazy.resize(n);
for (int i = 0; i < n; i++) {
tree[i + n] = i_t(base[i]);
}
for (int i = n - 1; i > 0; i--) {
tree[i] = i_t(tree[i + i], tree[i + i + 1]);
lazy[i] = 0;
}
}
i_t qry(int l, int r) {
if (l >= r) return i_t();
l += n, r += n;
push(l);
push(r - 1);
i_t lp, rp;
for (; l < r; l /= 2, r /= 2) {
if (l & 1) lp = i_t(lp, tree[l++]);
if (r & 1) rp = i_t(tree[--r], rp);
}
return i_t(lp, rp);
}
void upd(int l, int r, lazy_cont lc) {
if (l >= r) return;
l += n, r += n;
push(l);
push(r - 1);
int l0 = l, r0 = r;
for (; l < r; l /= 2, r /= 2) {
if (l & 1) apply(l++, lc);
if (r & 1) apply(--r, lc);
}
build(l0);
build(r0 - 1);
}
void upd(int x, e_t v) {
x += n;
push(x);
tree[x] = i_t(v);
build(x);
}
private:
int n, h;
std::vector<bool> dirty;
std::vector<i_t> tree;
std::vector<lazy_cont> lazy;
void apply(int p, lazy_cont &lc) {
tree[p].apply(lc);
if (p < n) {
dirty[p] = true;
lazy[p] += lc;
}
}
void push(int p) {
for (int s = h; s > 0; s--) {
int i = p >> s;
if (dirty[i]) {
apply(i + i, lazy[i]);
apply(i + i + 1, lazy[i]);
lazy[i] = 0;
dirty[i] = false;
}
}
}
void build(int p) {
for (p /= 2; p > 0; p /= 2) {
tree[p] = i_t(tree[p + p], tree[p + p + 1]);
if (dirty[p]) {
tree[p].apply(lazy[p]);
}
}
}
};
std::vector<int> edges[ms];
int to[ms];
int in[ms], out[ms], c = 0;
void dfs(int on) {
in[on] = c++;
for (auto too : edges[on]) {
dfs(too);
}
out[on] = c;
}
int ans[ms];
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n, k;
std::cin >> n >> k;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
a.push_back(n + 1);
std::vector<int> st;
st.push_back(n);
for (int i = n - 1; i >= 0; i--) {
while (a[st.back()] <= a[i]) {
st.pop_back();
}
to[i] = st.back();
edges[st.back()].push_back(i);
st.push_back(i);
}
SegmentTree<Node, int, int> tree, ok;
tree.init(std::vector<int>(n + 1, -INF));
ok.init(std::vector<int>(n + 1, 0));
dfs(n);
for (int i = n - 1; i >= 0; i--) {
{
ok.upd(in[i], out[i], 1);
tree.upd(in[i], out[i], 1);
tree.upd(in[i], ok.qry(in[i], in[i] + 1).mx);
}
if (i + k < n) {
ok.upd(in[i + k], out[i + k], -1);
tree.upd(in[i + k], out[i + k], -1);
}
ans[i] = tree.qry(0, n + 1).mx;
}
for (int i = 0; i + k <= n; i++) {
std::cout << ans[i] << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k, a[1000005];
struct node {
int to, nxt, w;
} e[1000005 << 1];
int bian = 0, head[1000005];
inline void add(int a, int b) {
e[++bian].to = b;
e[bian].nxt = head[a];
head[a] = bian;
}
inline void dkr() {
stack<int> s;
for (int i = n; i >= 1; i--) {
while (!s.empty() && a[s.top()] <= a[i]) s.pop();
if (!s.empty()) {
int f = s.top();
add(i, f), add(f, i);
} else
add(i, n + 1), add(n + 1, i);
s.push(i);
}
}
int in[1000005], out[1000005], t = 0;
inline void dfs(int x, int fa) {
in[x] = ++t;
for (int i = head[x]; i; i = e[i].nxt) {
int T = e[i].to;
if (T == fa) continue;
dfs(T, x);
}
out[x] = t;
}
struct xds {
int lazy, l, r, w;
} tre[1000005 << 2];
inline void build(int root, int L, int R) {
tre[root].l = L, tre[root].r = R;
if (L == R) return;
int M = (L + R) >> 1;
build(root << 1, L, M), build(root << 1 | 1, M + 1, R);
}
inline void pushup(int root) {
tre[root].w = max(tre[root << 1].w, tre[root << 1 | 1].w);
}
inline void pushdown(int root) {
if (tre[root].lazy) {
tre[root << 1].lazy += tre[root].lazy,
tre[root << 1 | 1].lazy += tre[root].lazy;
tre[root << 1].w += tre[root].lazy, tre[root << 1 | 1].w += tre[root].lazy;
tre[root].lazy = 0;
}
}
inline void update(int root, int L, int R, int pos) {
if (tre[root].l >= L && tre[root].r <= R) {
tre[root].w += pos, tre[root].lazy += pos;
return;
}
pushdown(root);
int mid = (tre[root].l + tre[root].r) >> 1;
if (L <= mid) update(root << 1, L, R, pos);
if (R > mid) update(root << 1 | 1, L, R, pos);
pushup(root);
}
inline int getsum(int root, int L, int R) {
if (tre[root].l > R || tre[root].r < L) return 0;
if (tre[root].l >= L && tre[root].r <= R) return tre[root].w;
pushdown(root);
int ans = 0, mid = (tre[root].l + tre[root].r) >> 1;
if (L <= mid) ans = max(ans, getsum(root << 1, L, R));
if (R > mid) ans = max(ans, getsum(root << 1 | 1, L, R));
return ans;
}
inline int read() {
int res = 0, f = 1;
char c;
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) res = (res << 1) + (res << 3) + (c ^ 48);
return res * f;
}
inline void write(int x) {
static int a[35];
int top = 0;
if (x < 0) putchar('-'), x = -x;
do {
a[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(a[--top] + 48);
putchar(' ');
}
signed main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
dkr();
dfs(n + 1, 0);
build(1, 1, n + 1);
for (int i = 1; i <= k; i++) update(1, in[i], out[i], 1);
for (int i = 1; i + k - 1 <= n; i++) {
int rr = i + k - 1;
write(getsum(1, 1, n + 1));
update(1, in[i], out[i], -1);
update(1, in[rr + 1], out[rr + 1], 1);
}
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;
int n, k, a[1000005];
struct node {
int to, nxt, w;
} e[1000005 << 1];
int bian = 0, head[1000005];
inline void add(int a, int b) {
e[++bian].to = b;
e[bian].nxt = head[a];
head[a] = bian;
}
inline void dkr() {
stack<int> s;
for (int i = n; i >= 1; i--) {
while (!s.empty() && a[s.top()] <= a[i]) s.pop();
if (!s.empty()) {
int f = s.top();
add(i, f), add(f, i);
} else
add(i, n + 1), add(n + 1, i);
s.push(i);
}
}
int in[1000005], out[1000005], t = 0;
inline void dfs(int x, int fa) {
in[x] = ++t;
for (int i = head[x]; i; i = e[i].nxt) {
int T = e[i].to;
if (T == fa) continue;
dfs(T, x);
}
out[x] = t;
}
struct xds {
int lazy, l, r, w;
} tre[1000005 << 2];
inline void build(int root, int L, int R) {
tre[root].l = L, tre[root].r = R;
if (L == R) return;
int M = (L + R) >> 1;
build(root << 1, L, M), build(root << 1 | 1, M + 1, R);
}
inline void pushup(int root) {
tre[root].w = max(tre[root << 1].w, tre[root << 1 | 1].w);
}
inline void pushdown(int root) {
if (tre[root].lazy) {
tre[root << 1].lazy += tre[root].lazy,
tre[root << 1 | 1].lazy += tre[root].lazy;
tre[root << 1].w += tre[root].lazy, tre[root << 1 | 1].w += tre[root].lazy;
tre[root].lazy = 0;
}
}
inline void update(int root, int L, int R, int pos) {
if (tre[root].l >= L && tre[root].r <= R) {
tre[root].w += pos, tre[root].lazy += pos;
return;
}
pushdown(root);
int mid = (tre[root].l + tre[root].r) >> 1;
if (L <= mid) update(root << 1, L, R, pos);
if (R > mid) update(root << 1 | 1, L, R, pos);
pushup(root);
}
inline int getsum(int root, int L, int R) {
if (tre[root].l > R || tre[root].r < L) return 0;
if (tre[root].l >= L && tre[root].r <= R) return tre[root].w;
pushdown(root);
int ans = 0, mid = (tre[root].l + tre[root].r) >> 1;
if (L <= mid) ans = max(ans, getsum(root << 1, L, R));
if (R > mid) ans = max(ans, getsum(root << 1 | 1, L, R));
return ans;
}
inline int read() {
int res = 0, f = 1;
char c;
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) res = (res << 1) + (res << 3) + (c ^ 48);
return res * f;
}
inline void write(int x) {
static int a[35];
int top = 0;
if (x < 0) putchar('-'), x = -x;
do {
a[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(a[--top] + 48);
putchar(' ');
}
signed main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
dkr();
dfs(n + 1, 0);
build(1, 1, n + 1);
for (int i = 1; i <= k; i++) update(1, in[i], out[i], 1);
for (int i = 1; i + k - 1 <= n; i++) {
int rr = i + k - 1;
write(getsum(1, 1, n + 1));
update(1, in[i], out[i], -1);
update(1, in[rr + 1], out[rr + 1], 1);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long inff = 0x3f3f3f3f3f3f3f3f;
int n, k, a[1000008], de[1000008], qw, num, ls[1000008], rs[1000008],
deep[1000008], s[1000008], zz;
bool vis[1000008];
vector<int> g[1000008];
struct as {
int l, r, ma, lz;
} tr[1000008 << 2];
void push(int d) { tr[d].ma = max(tr[d << 1].ma, tr[d << 1 | 1].ma); }
void build(int d, int l, int r) {
tr[d].l = l, tr[d].r = r, tr[d].lz = 0;
if (l == r) {
tr[d].ma = 0;
return;
}
int mid = (l + r) >> 1;
build(d << 1, l, mid);
build(d << 1 | 1, mid + 1, r);
push(d);
}
void pussh(int d) {
if (!tr[d].lz) return;
tr[d << 1].lz += tr[d].lz, tr[d << 1 | 1].lz += tr[d].lz;
tr[d << 1].ma += tr[d].lz, tr[d << 1 | 1].ma += tr[d].lz;
tr[d].lz = 0;
}
void add(int d, int l, int r, int pos) {
if (tr[d].l == l && tr[d].r == r) {
tr[d].ma += pos, tr[d].lz += pos;
return;
}
pussh(d);
int mid = (tr[d].l + tr[d].r) >> 1;
if (mid >= r)
add(d << 1, l, r, pos);
else if (l > mid)
add(d << 1 | 1, l, r, pos);
else
add(d << 1, l, mid, pos), add(d << 1 | 1, mid + 1, r, pos);
push(d);
}
int query(int d, int l, int r) {
if (tr[d].l == l && tr[d].r == r) {
return tr[d].ma;
}
int mid = (tr[d].l + tr[d].r) >> 1;
if (mid >= r)
return query(d << 1, l, r);
else if (l > mid)
return query(d << 1 | 1, l, r);
else
return max(query(d << 1, l, mid), query(d << 1 | 1, mid + 1, r));
}
void dfs(int x) {
ls[x] = ++num;
vis[x] = 1;
for (int i = 0; i < g[x].size(); i++)
deep[g[x][i]] = deep[x] + 1, dfs(g[x][i]);
rs[x] = num;
}
int main() {
cin.tie(0);
cout.tie(0);
cin >> n >> k;
int l = 1, r = 1;
for (int i(1); i <= (n); ++i) {
scanf("%d", &a[i]);
while (r > l && a[i] > a[de[r - 1]]) g[i].push_back(de[r - 1]), r--;
de[r++] = i;
}
for (int i(n); i >= (1); --i)
if (!vis[i]) deep[i] = 1, dfs(i);
build(1, 1, n);
for (int i(n); i >= (1); --i) {
add(1, ls[i], ls[i], deep[i]);
if (i + k - 1 > n) continue;
int ans = query(1, ls[i + k - 1], ls[i]);
s[++zz] = ans;
add(1, ls[i + k - 1], ls[i + k - 1], -int(0x3f3f3f3f));
if (ls[i + k - 1] != rs[i + k - 1])
add(1, ls[i + k - 1] + 1, rs[i + k - 1], -1);
}
for (int i(zz); i >= (1); --i) printf("%d ", s[i]);
puts("");
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 inff = 0x3f3f3f3f3f3f3f3f;
int n, k, a[1000008], de[1000008], qw, num, ls[1000008], rs[1000008],
deep[1000008], s[1000008], zz;
bool vis[1000008];
vector<int> g[1000008];
struct as {
int l, r, ma, lz;
} tr[1000008 << 2];
void push(int d) { tr[d].ma = max(tr[d << 1].ma, tr[d << 1 | 1].ma); }
void build(int d, int l, int r) {
tr[d].l = l, tr[d].r = r, tr[d].lz = 0;
if (l == r) {
tr[d].ma = 0;
return;
}
int mid = (l + r) >> 1;
build(d << 1, l, mid);
build(d << 1 | 1, mid + 1, r);
push(d);
}
void pussh(int d) {
if (!tr[d].lz) return;
tr[d << 1].lz += tr[d].lz, tr[d << 1 | 1].lz += tr[d].lz;
tr[d << 1].ma += tr[d].lz, tr[d << 1 | 1].ma += tr[d].lz;
tr[d].lz = 0;
}
void add(int d, int l, int r, int pos) {
if (tr[d].l == l && tr[d].r == r) {
tr[d].ma += pos, tr[d].lz += pos;
return;
}
pussh(d);
int mid = (tr[d].l + tr[d].r) >> 1;
if (mid >= r)
add(d << 1, l, r, pos);
else if (l > mid)
add(d << 1 | 1, l, r, pos);
else
add(d << 1, l, mid, pos), add(d << 1 | 1, mid + 1, r, pos);
push(d);
}
int query(int d, int l, int r) {
if (tr[d].l == l && tr[d].r == r) {
return tr[d].ma;
}
int mid = (tr[d].l + tr[d].r) >> 1;
if (mid >= r)
return query(d << 1, l, r);
else if (l > mid)
return query(d << 1 | 1, l, r);
else
return max(query(d << 1, l, mid), query(d << 1 | 1, mid + 1, r));
}
void dfs(int x) {
ls[x] = ++num;
vis[x] = 1;
for (int i = 0; i < g[x].size(); i++)
deep[g[x][i]] = deep[x] + 1, dfs(g[x][i]);
rs[x] = num;
}
int main() {
cin.tie(0);
cout.tie(0);
cin >> n >> k;
int l = 1, r = 1;
for (int i(1); i <= (n); ++i) {
scanf("%d", &a[i]);
while (r > l && a[i] > a[de[r - 1]]) g[i].push_back(de[r - 1]), r--;
de[r++] = i;
}
for (int i(n); i >= (1); --i)
if (!vis[i]) deep[i] = 1, dfs(i);
build(1, 1, n);
for (int i(n); i >= (1); --i) {
add(1, ls[i], ls[i], deep[i]);
if (i + k - 1 > n) continue;
int ans = query(1, ls[i + k - 1], ls[i]);
s[++zz] = ans;
add(1, ls[i + k - 1], ls[i + k - 1], -int(0x3f3f3f3f));
if (ls[i + k - 1] != rs[i + k - 1])
add(1, ls[i + k - 1] + 1, rs[i + k - 1], -1);
}
for (int i(zz); i >= (1); --i) printf("%d ", s[i]);
puts("");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 1e6 + 1;
int n, k, a[N_MAX], E[2 * N_MAX], fE[N_MAX], lE[N_MAX];
stack<int> st;
vector<int> G[N_MAX];
bool visited[N_MAX];
int Elen = 0;
void DFS(int u) {
E[Elen++] = u;
visited[u] = true;
for (int v : G[u]) {
if (visited[v]) continue;
DFS(v);
E[Elen++] = u;
}
}
int arr[2 * N_MAX], F[2 * N_MAX], lazy[2 * N_MAX];
void pass_lazy(int a, int b) {
int k = (a + b) / 2;
F[k] += lazy[k];
if (a == k)
arr[a] += lazy[k];
else
lazy[(a + k) / 2] += lazy[k];
if (k + 1 == b)
arr[b] += lazy[k];
else
lazy[(b + k + 1) / 2] += lazy[k];
lazy[k] = 0;
}
int add(int a, int b, int i, int j, int val) {
if (a == b) {
arr[a] += val;
return arr[a];
}
int k = (a + b) / 2;
if (a == i && b == j) {
lazy[k] += val;
return F[k] + lazy[k];
}
if (lazy[k] != 0) pass_lazy(a, b);
if (j <= k)
F[k] =
max(add(a, k, i, j, val),
(k + 1 == b ? arr[b] : F[(b + k + 1) / 2] + lazy[(b + k + 1) / 2]));
else if (k + 1 <= i)
F[k] = max((a == k ? arr[a] : F[(a + k) / 2] + lazy[(a + k) / 2]),
add(k + 1, b, i, j, val));
else
F[k] = max(add(a, k, i, k, val), add(k + 1, b, k + 1, j, val));
return F[k];
}
int query(int a, int b, int i, int j) {
if (a == b) return arr[a];
int k = (a + b) / 2;
if (lazy[k] != 0) pass_lazy(a, b);
if (a == i && b == j) return F[k];
if (j <= k) return query(a, k, i, j);
if (k + 1 <= i) return query(k + 1, b, i, j);
return max(query(a, k, i, k), query(k + 1, b, k + 1, j));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
a[n] = 1e6 + 1;
st.push(n);
for (int i = n - 1; 0 <= i; i--) {
while (a[st.top()] <= a[i]) st.pop();
G[i].push_back(st.top());
G[st.top()].push_back(i);
st.push(i);
}
DFS(n);
for (int i = 0; i < Elen; i++) lE[E[i]] = i;
for (int i = Elen - 1; 0 <= i; i--) fE[E[i]] = i;
for (int i = 0; i < n; i++) {
add(0, Elen - 1, fE[i], lE[i], 1);
if (k <= i) add(0, Elen - 1, fE[i - k], lE[i - k], -1);
if (k - 1 <= i) cout << query(0, Elen - 1, 0, Elen - 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_MAX = 1e6 + 1;
int n, k, a[N_MAX], E[2 * N_MAX], fE[N_MAX], lE[N_MAX];
stack<int> st;
vector<int> G[N_MAX];
bool visited[N_MAX];
int Elen = 0;
void DFS(int u) {
E[Elen++] = u;
visited[u] = true;
for (int v : G[u]) {
if (visited[v]) continue;
DFS(v);
E[Elen++] = u;
}
}
int arr[2 * N_MAX], F[2 * N_MAX], lazy[2 * N_MAX];
void pass_lazy(int a, int b) {
int k = (a + b) / 2;
F[k] += lazy[k];
if (a == k)
arr[a] += lazy[k];
else
lazy[(a + k) / 2] += lazy[k];
if (k + 1 == b)
arr[b] += lazy[k];
else
lazy[(b + k + 1) / 2] += lazy[k];
lazy[k] = 0;
}
int add(int a, int b, int i, int j, int val) {
if (a == b) {
arr[a] += val;
return arr[a];
}
int k = (a + b) / 2;
if (a == i && b == j) {
lazy[k] += val;
return F[k] + lazy[k];
}
if (lazy[k] != 0) pass_lazy(a, b);
if (j <= k)
F[k] =
max(add(a, k, i, j, val),
(k + 1 == b ? arr[b] : F[(b + k + 1) / 2] + lazy[(b + k + 1) / 2]));
else if (k + 1 <= i)
F[k] = max((a == k ? arr[a] : F[(a + k) / 2] + lazy[(a + k) / 2]),
add(k + 1, b, i, j, val));
else
F[k] = max(add(a, k, i, k, val), add(k + 1, b, k + 1, j, val));
return F[k];
}
int query(int a, int b, int i, int j) {
if (a == b) return arr[a];
int k = (a + b) / 2;
if (lazy[k] != 0) pass_lazy(a, b);
if (a == i && b == j) return F[k];
if (j <= k) return query(a, k, i, j);
if (k + 1 <= i) return query(k + 1, b, i, j);
return max(query(a, k, i, k), query(k + 1, b, k + 1, j));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
a[n] = 1e6 + 1;
st.push(n);
for (int i = n - 1; 0 <= i; i--) {
while (a[st.top()] <= a[i]) st.pop();
G[i].push_back(st.top());
G[st.top()].push_back(i);
st.push(i);
}
DFS(n);
for (int i = 0; i < Elen; i++) lE[E[i]] = i;
for (int i = Elen - 1; 0 <= i; i--) fE[E[i]] = i;
for (int i = 0; i < n; i++) {
add(0, Elen - 1, fE[i], lE[i], 1);
if (k <= i) add(0, Elen - 1, fE[i - k], lE[i - k], -1);
if (k - 1 <= i) cout << query(0, Elen - 1, 0, Elen - 1) << " ";
}
cout << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> v[65536 * 16];
int dep[65536 * 16];
int bel[65536 * 16];
int pa[65536 * 16];
int dfn[65536 * 16];
int siz[65536 * 16];
int DFN;
void dfs(int x, int rt) {
if (!rt) rt = x;
bel[x] = rt;
dfn[x] = ++DFN;
siz[x] = 1;
for (int i = 0; i < v[x].size(); i++)
dep[v[x][i]] = dep[x] + 1, dfs(v[x][i], rt), siz[x] += siz[v[x][i]];
}
int n;
int m;
int a[65536 * 16];
struct segmenttree {
int tree[65536 * 16 * 2 + 10];
int tag[65536 * 16 * 2 + 10];
void init() {
for (int i = 0; i < 65536 * 16 * 2 + 2; i++) tree[i] = -10000000;
}
void up(int x) {
int id = x;
tree[x] = max(tree[id << 1], tree[id << 1 | 1]);
}
void down(int x) {
int id = x;
tree[id << 1] += tag[x], tag[id << 1] += tag[x],
tree[id << 1 | 1] += tag[x], tag[id << 1 | 1] += tag[x];
tag[x] = 0;
}
void set(int x, int l, int r, int id, int y) {
if (l == r) {
tree[id] = y;
return;
}
down(id);
if (x <= (l + r) / 2)
set(x, l, (l + r) / 2, id << 1, y);
else
set(x, (l + r) / 2 + 1, r, id << 1 | 1, y);
up(id);
}
void upd(int a, int b, int l, int r, int id, int y) {
if (l >= a and r <= b) {
tree[id] += y;
tag[id] += y;
return;
}
down(id);
if (a <= (l + r) / 2) upd(a, b, l, (l + r) / 2, id << 1, y);
if (b > (l + r) / 2) upd(a, b, (l + r) / 2 + 1, r, id << 1 | 1, y);
up(id);
}
} trcyx;
void output() { printf("%d ", trcyx.tree[1]); }
void add(int x) {
trcyx.set(dfn[x], 1, 65536 * 16, 1, 0);
trcyx.upd(dfn[x], dfn[x] + siz[x] - 1, 1, 65536 * 16, 1, 1);
}
void sub(int x) { trcyx.set(dfn[x], 1, 65536 * 16, 1, -10000000); }
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
deque<int> dq;
for (int i = n; i >= 1; i--) {
while (dq.size() and a[dq.front()] <= a[i]) dq.pop_front();
if (dq.size()) pa[i] = dq.front();
dq.push_front(i);
}
trcyx.init();
for (int i = 1; i <= n; i++) v[pa[i]].push_back(i);
for (int i = 1; i <= n; i++)
if (!pa[i]) dfs(i, 0);
for (int i = 1; i <= m; i++) add(i);
output();
for (int i = m + 1; i <= n; i++) {
add(i);
sub(i - m);
output();
}
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;
vector<int> v[65536 * 16];
int dep[65536 * 16];
int bel[65536 * 16];
int pa[65536 * 16];
int dfn[65536 * 16];
int siz[65536 * 16];
int DFN;
void dfs(int x, int rt) {
if (!rt) rt = x;
bel[x] = rt;
dfn[x] = ++DFN;
siz[x] = 1;
for (int i = 0; i < v[x].size(); i++)
dep[v[x][i]] = dep[x] + 1, dfs(v[x][i], rt), siz[x] += siz[v[x][i]];
}
int n;
int m;
int a[65536 * 16];
struct segmenttree {
int tree[65536 * 16 * 2 + 10];
int tag[65536 * 16 * 2 + 10];
void init() {
for (int i = 0; i < 65536 * 16 * 2 + 2; i++) tree[i] = -10000000;
}
void up(int x) {
int id = x;
tree[x] = max(tree[id << 1], tree[id << 1 | 1]);
}
void down(int x) {
int id = x;
tree[id << 1] += tag[x], tag[id << 1] += tag[x],
tree[id << 1 | 1] += tag[x], tag[id << 1 | 1] += tag[x];
tag[x] = 0;
}
void set(int x, int l, int r, int id, int y) {
if (l == r) {
tree[id] = y;
return;
}
down(id);
if (x <= (l + r) / 2)
set(x, l, (l + r) / 2, id << 1, y);
else
set(x, (l + r) / 2 + 1, r, id << 1 | 1, y);
up(id);
}
void upd(int a, int b, int l, int r, int id, int y) {
if (l >= a and r <= b) {
tree[id] += y;
tag[id] += y;
return;
}
down(id);
if (a <= (l + r) / 2) upd(a, b, l, (l + r) / 2, id << 1, y);
if (b > (l + r) / 2) upd(a, b, (l + r) / 2 + 1, r, id << 1 | 1, y);
up(id);
}
} trcyx;
void output() { printf("%d ", trcyx.tree[1]); }
void add(int x) {
trcyx.set(dfn[x], 1, 65536 * 16, 1, 0);
trcyx.upd(dfn[x], dfn[x] + siz[x] - 1, 1, 65536 * 16, 1, 1);
}
void sub(int x) { trcyx.set(dfn[x], 1, 65536 * 16, 1, -10000000); }
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
deque<int> dq;
for (int i = n; i >= 1; i--) {
while (dq.size() and a[dq.front()] <= a[i]) dq.pop_front();
if (dq.size()) pa[i] = dq.front();
dq.push_front(i);
}
trcyx.init();
for (int i = 1; i <= n; i++) v[pa[i]].push_back(i);
for (int i = 1; i <= n; i++)
if (!pa[i]) dfs(i, 0);
for (int i = 1; i <= m; i++) add(i);
output();
for (int i = m + 1; i <= n; i++) {
add(i);
sub(i - m);
output();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mi = -20000000;
const int maxn = 1000050;
struct node {
int l;
int r;
int lc;
int rc;
int mx;
int tag;
} s[2 * maxn];
struct edge {
int t;
edge *next;
} e[maxn];
int a[maxn], p[maxn];
int deep[maxn], num[maxn];
int ans[maxn];
int n, k;
edge *head[maxn];
int etot, dfn, tot;
void ade(int f, int t) {
etot++;
e[etot].t = t;
e[etot].next = head[f];
head[f] = &e[etot];
}
stack<int> st;
int dfs(int x, int dep) {
p[x] = ++dfn;
deep[x] = dep;
num[x] = 1;
for (edge *i = head[x]; i; i = i->next) {
num[x] += dfs(i->t, dep + 1);
}
return num[x];
}
int build(int l, int r) {
int now = tot++;
s[now].l = l;
s[now].r = r;
if (l == r) return now;
int mid = (l + r) >> 1;
s[now].rc = build(mid + 1, r);
s[now].lc = build(l, mid);
return now;
}
void pushdown(int id) {
node now = s[id];
s[now.rc].tag += now.tag;
s[now.lc].tag += now.tag;
s[now.rc].mx += now.tag;
s[now.lc].mx += now.tag;
s[id].tag = 0;
}
void change(int id, int l, int r, int x) {
if (l <= s[id].l && s[id].r <= r) {
s[id].mx += x;
s[id].tag += x;
return;
}
int mid = (s[id].l + s[id].r) >> 1;
pushdown(id);
if (l <= mid) change(s[id].lc, l, r, x);
if (r > mid) change(s[id].rc, l, r, x);
s[id].mx = max(s[s[id].lc].mx, s[s[id].rc].mx);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (st.empty())
ade(n + 1, i);
else
ade(st.top(), i);
st.push(i);
}
dfs(n + 1, 0);
build(1, n + 1);
change(0, 1, n + 1, mi);
for (int i = n; i >= n - k + 2; i--) change(0, p[i], p[i], deep[i] - mi);
for (int i = n - k + 1; i >= 1; i--) {
change(0, p[i], p[i], deep[i] - mi);
ans[i] = s[0].mx;
change(0, p[i + k - 1], p[i + k - 1] + num[i + k - 1] - 1, -1);
}
for (int i = 1; i <= n - k + 1; i++) printf("%d ", ans[i]);
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 mi = -20000000;
const int maxn = 1000050;
struct node {
int l;
int r;
int lc;
int rc;
int mx;
int tag;
} s[2 * maxn];
struct edge {
int t;
edge *next;
} e[maxn];
int a[maxn], p[maxn];
int deep[maxn], num[maxn];
int ans[maxn];
int n, k;
edge *head[maxn];
int etot, dfn, tot;
void ade(int f, int t) {
etot++;
e[etot].t = t;
e[etot].next = head[f];
head[f] = &e[etot];
}
stack<int> st;
int dfs(int x, int dep) {
p[x] = ++dfn;
deep[x] = dep;
num[x] = 1;
for (edge *i = head[x]; i; i = i->next) {
num[x] += dfs(i->t, dep + 1);
}
return num[x];
}
int build(int l, int r) {
int now = tot++;
s[now].l = l;
s[now].r = r;
if (l == r) return now;
int mid = (l + r) >> 1;
s[now].rc = build(mid + 1, r);
s[now].lc = build(l, mid);
return now;
}
void pushdown(int id) {
node now = s[id];
s[now.rc].tag += now.tag;
s[now.lc].tag += now.tag;
s[now.rc].mx += now.tag;
s[now.lc].mx += now.tag;
s[id].tag = 0;
}
void change(int id, int l, int r, int x) {
if (l <= s[id].l && s[id].r <= r) {
s[id].mx += x;
s[id].tag += x;
return;
}
int mid = (s[id].l + s[id].r) >> 1;
pushdown(id);
if (l <= mid) change(s[id].lc, l, r, x);
if (r > mid) change(s[id].rc, l, r, x);
s[id].mx = max(s[s[id].lc].mx, s[s[id].rc].mx);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (st.empty())
ade(n + 1, i);
else
ade(st.top(), i);
st.push(i);
}
dfs(n + 1, 0);
build(1, n + 1);
change(0, 1, n + 1, mi);
for (int i = n; i >= n - k + 2; i--) change(0, p[i], p[i], deep[i] - mi);
for (int i = n - k + 1; i >= 1; i--) {
change(0, p[i], p[i], deep[i] - mi);
ans[i] = s[0].mx;
change(0, p[i + k - 1], p[i + k - 1] + num[i + k - 1] - 1, -1);
}
for (int i = 1; i <= n - k + 1; i++) printf("%d ", ans[i]);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2000005, M = 8000005;
int a[N], q[N], f[N], v[M], mx[M];
void add(int x, int l, int r, int ll, int rr) {
if (r < ll || l > rr) return;
if (ll <= l && r <= rr) {
v[x]++;
return;
}
if (v[x]) {
v[2 * x] += v[x];
v[2 * x + 1] += v[x];
v[x] = 0;
}
int m = (l + r) / 2;
add(x * 2, l, m, ll, rr);
add(x * 2 + 1, m + 1, r, ll, rr);
mx[x] = max(mx[x * 2] + v[x * 2], mx[x * 2 + 1] + v[x * 2 + 1]);
}
int qry(int x, int l, int r, int ll, int rr) {
if (r < ll || l > rr) return 0;
if (ll <= l && r <= rr) return v[x] + mx[x];
if (v[x]) {
v[2 * x] += v[x];
v[2 * x + 1] += v[x];
v[x] = 0;
}
int m = (l + r) / 2;
return max(qry(2 * x, l, m, ll, rr), qry(2 * x + 1, m + 1, r, ll, rr));
}
int main() {
int n, k, h = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i >= 1; i--) {
while (h > 0 && a[q[h - 1]] <= a[i]) {
f[q[h - 1]] = i;
h--;
}
q[h++] = i;
}
for (int i = 1; i <= n; i++) f[i] = max(f[i], i - k);
for (int i = 1; i <= n; i++) {
add(1, 1, n, f[i] + 1, i);
if (i >= k) printf("%d\n", qry(1, 1, n, i - k + 1, i));
}
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 = 2000005, M = 8000005;
int a[N], q[N], f[N], v[M], mx[M];
void add(int x, int l, int r, int ll, int rr) {
if (r < ll || l > rr) return;
if (ll <= l && r <= rr) {
v[x]++;
return;
}
if (v[x]) {
v[2 * x] += v[x];
v[2 * x + 1] += v[x];
v[x] = 0;
}
int m = (l + r) / 2;
add(x * 2, l, m, ll, rr);
add(x * 2 + 1, m + 1, r, ll, rr);
mx[x] = max(mx[x * 2] + v[x * 2], mx[x * 2 + 1] + v[x * 2 + 1]);
}
int qry(int x, int l, int r, int ll, int rr) {
if (r < ll || l > rr) return 0;
if (ll <= l && r <= rr) return v[x] + mx[x];
if (v[x]) {
v[2 * x] += v[x];
v[2 * x + 1] += v[x];
v[x] = 0;
}
int m = (l + r) / 2;
return max(qry(2 * x, l, m, ll, rr), qry(2 * x + 1, m + 1, r, ll, rr));
}
int main() {
int n, k, h = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i >= 1; i--) {
while (h > 0 && a[q[h - 1]] <= a[i]) {
f[q[h - 1]] = i;
h--;
}
q[h++] = i;
}
for (int i = 1; i <= n; i++) f[i] = max(f[i], i - k);
for (int i = 1; i <= n; i++) {
add(1, 1, n, f[i] + 1, i);
if (i >= k) printf("%d\n", qry(1, 1, n, i - k + 1, i));
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, k;
int a[2000000];
int head[2000000], pos;
struct edge {
int to, next;
} e[2000000 << 1];
void add(int a, int b) {
pos++;
e[pos].to = b, e[pos].next = head[a], head[a] = pos;
}
void insert(int a, int b) {
add(a, b);
add(b, a);
}
int in[2000000], out[2000000], T = -1;
bool p[2000000];
void dfs(int u, int fa) {
in[u] = ++T;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (v == fa) continue;
dfs(v, u);
}
out[u] = T;
}
struct seg {
struct edge {
int l, r, mx, tag;
} e[2000000 << 1];
void pushup(int u) { e[u].mx = max(e[u << 1].mx, e[u << 1 | 1].mx); }
void pushdown(int u) {
if (e[u].tag) {
e[u << 1].tag += e[u].tag, e[u << 1 | 1].tag += e[u].tag;
e[u << 1].mx += e[u].tag, e[u << 1 | 1].mx += e[u].tag;
e[u].tag = 0;
}
}
void build(int u, int l, int r) {
e[u].l = l, e[u].r = r;
int mid = (l + r) >> 1;
if (l == r) {
e[u].mx = 0;
return;
}
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void add(int u, int x, int y, int c) {
int l = e[u].l, r = e[u].r, mid = (l + r) >> 1;
if (l != r) pushdown(u);
if (x <= l && y >= r) {
e[u].mx += c, e[u].tag += c;
return;
}
if (y <= mid)
add(u << 1, x, y, c);
else if (x > mid)
add(u << 1 | 1, x, y, c);
else
add(u << 1, x, mid, c), add(u << 1 | 1, mid + 1, y, c);
pushup(u);
}
int ask() { return e[1].mx; }
} seg;
int st[2000000], top;
int main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
st[++top] = n;
for (int i = n - 1; i; i--) {
int ret = -1;
while (top && a[i] >= a[st[top]]) top--;
if (top) ret = st[top];
st[++top] = i;
if (ret != -1) insert(i, ret), p[i] = 1;
}
for (int i = 1; i <= n; i++)
if (!p[i]) insert(0, i);
dfs(0, 0);
seg.build(1, 1, n);
for (int i = 1; i < k; i++) seg.add(1, in[i], out[i], 1);
for (int i = 0; i <= n - k; i++) {
seg.add(1, in[i + k], out[i + k], 1);
printf("%d ", seg.ask());
seg.add(1, in[i + 1], out[i + 1], -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;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, k;
int a[2000000];
int head[2000000], pos;
struct edge {
int to, next;
} e[2000000 << 1];
void add(int a, int b) {
pos++;
e[pos].to = b, e[pos].next = head[a], head[a] = pos;
}
void insert(int a, int b) {
add(a, b);
add(b, a);
}
int in[2000000], out[2000000], T = -1;
bool p[2000000];
void dfs(int u, int fa) {
in[u] = ++T;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (v == fa) continue;
dfs(v, u);
}
out[u] = T;
}
struct seg {
struct edge {
int l, r, mx, tag;
} e[2000000 << 1];
void pushup(int u) { e[u].mx = max(e[u << 1].mx, e[u << 1 | 1].mx); }
void pushdown(int u) {
if (e[u].tag) {
e[u << 1].tag += e[u].tag, e[u << 1 | 1].tag += e[u].tag;
e[u << 1].mx += e[u].tag, e[u << 1 | 1].mx += e[u].tag;
e[u].tag = 0;
}
}
void build(int u, int l, int r) {
e[u].l = l, e[u].r = r;
int mid = (l + r) >> 1;
if (l == r) {
e[u].mx = 0;
return;
}
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void add(int u, int x, int y, int c) {
int l = e[u].l, r = e[u].r, mid = (l + r) >> 1;
if (l != r) pushdown(u);
if (x <= l && y >= r) {
e[u].mx += c, e[u].tag += c;
return;
}
if (y <= mid)
add(u << 1, x, y, c);
else if (x > mid)
add(u << 1 | 1, x, y, c);
else
add(u << 1, x, mid, c), add(u << 1 | 1, mid + 1, y, c);
pushup(u);
}
int ask() { return e[1].mx; }
} seg;
int st[2000000], top;
int main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
st[++top] = n;
for (int i = n - 1; i; i--) {
int ret = -1;
while (top && a[i] >= a[st[top]]) top--;
if (top) ret = st[top];
st[++top] = i;
if (ret != -1) insert(i, ret), p[i] = 1;
}
for (int i = 1; i <= n; i++)
if (!p[i]) insert(0, i);
dfs(0, 0);
seg.build(1, 1, n);
for (int i = 1; i < k; i++) seg.add(1, in[i], out[i], 1);
for (int i = 0; i <= n - k; i++) {
seg.add(1, in[i + k], out[i + k], 1);
printf("%d ", seg.ask());
seg.add(1, in[i + 1], out[i + 1], -1);
}
puts("");
}
``` |
#include <bits/stdc++.h>
using namespace std;
enum seg_type { seg_not_lazy, seg_lazy, seg_beats };
template <class node, seg_type type>
struct segment_tree {
using node_container = typename node::node_container;
using lazy_container = typename node::lazy_container;
private:
template <const bool T = (type != seg_not_lazy),
typename enable_if<T, bool>::type = 0>
inline void push(int x, int b, int e, int m) {
if (st[x].lazy()) {
st[x + 1].apply(b, m, st[x].lazy);
st[x + ((m - b) << 1)].apply(m, e, st[x].lazy);
st[x].lazy = lazy_container();
}
}
template <const bool T = (type != seg_not_lazy),
typename enable_if<!T, bool>::type = 0>
inline void push(int x, int b, int e, int m) {}
template <typename RAIter>
void build(int x, int b, int e, const RAIter &a) {
if (b + 1 == e) {
st[x].nod.build(a[b]);
return;
}
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
build(x + 1, b, m, a);
build(y, m, e, a);
st[x].nod = st[x + 1].nod + st[y].nod;
}
template <const bool T = (type == seg_beats),
typename enable_if<!T, bool>::type = 0>
void update_(int x, int b, int e) {
if (lo <= b && e <= hi) {
st[x].apply(b, e, lazy);
return;
}
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
push(x, b, e, m);
if (lo < m) update_(x + 1, b, m);
if (m < hi) update_(y, m, e);
st[x].nod = st[x + 1].nod + st[y].nod;
}
template <const bool T = (type == seg_beats),
typename enable_if<T, bool>::type = 0>
void update_(int x, int b, int e) {
if (st[x].break_condition(lazy)) return;
if (lo <= b && e <= hi && st[x].tag_condition(lazy)) {
st[x].apply(b, e, lazy);
return;
}
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
push(x, b, e, m);
if (lo < m) update_(x + 1, b, m);
if (m < hi) update_(y, m, e);
st[x].nod = st[x + 1].nod + st[y].nod;
}
node_container query(int x, int b, int e) {
if (lo <= b && e <= hi) return st[x].nod;
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
push(x, b, e, m);
if (m >= hi) return query(x + 1, b, m);
if (m <= lo) return query(y, m, e);
return query(x + 1, b, m) + query(y, m, e);
}
template <class P>
int find_first(int x, int b, int e, const P &f) {
if (b + 1 == e) return f(st[x]) ? b : -1;
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
push(x, b, e, m);
if (lo < m && f(st[x + 1])) {
auto t = find_first(x + 1, b, m, f);
if (t != -1) return t;
}
if (m < hi && f(st[y])) return find_first(y, m, e, f);
return -1;
}
template <class P>
int find_last(int x, int b, int e, const P &f) {
if (b + 1 == e) return f(st[x]) ? b : -1;
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
push(x, b, e, m);
if (m < hi && f(st[y])) {
auto t = find_last(y, m, e, f);
if (t != -1) return t;
}
if (lo < m && f(st[x + 1])) return find_last(x + 1, b, m, f);
return -1;
}
lazy_container lazy;
int n, lo, hi;
vector<node> st;
public:
template <typename RAIter>
void build(const RAIter &a) {
build(0, 0, n, a);
}
void update(int l, int r, const lazy_container &x) {
lo = l, hi = r, lazy = x, update_(0, 0, n);
}
node_container query(int l, int r) { return lo = l, hi = r, query(0, 0, n); }
template <class P>
int find_first(int l, int r, const P &f) {
return lo = l, hi = r, find_first(0, 0, n, f);
}
template <class P>
int find_last(int l, int r, const P &f) {
return lo = l, hi = r, find_last(0, 0, n, f);
}
segment_tree(int n) : n(n), st(2 * n - 1) {}
};
struct node {
struct node_container {
int x;
friend node_container operator+(node_container l, const node_container &r) {
l.x = max(l.x, r.x);
return l;
}
node_container() : x(-1e9) {}
} nod;
struct lazy_container {
int s;
inline bool operator()() { return s != 0; }
lazy_container(int s = 0) : s(s) {}
} lazy;
inline void apply(int l, int r, lazy_container &p) {
nod.x += p.s;
lazy.s += p.s;
}
};
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (auto &i : a) cin >> i;
a.push_back(1e9);
vector<int> stk = {n};
vector<vector<int>> adj(n + 1);
for (int i = n - 1; i >= 0; --i) {
while (a[i] >= a[stk.back()]) stk.pop_back();
adj[stk.back()].push_back(i);
stk.push_back(i);
}
int id = 0;
vector<int> dt(n + 1), ft(n + 1);
function<void(int)> dfs = [&](int u) {
dt[u] = id++;
for (auto v : adj[u]) dfs(v);
ft[u] = id;
};
dfs(n);
segment_tree<node, seg_lazy> st(n + 1);
auto upsub = [&](int u) {
st.update(dt[u], dt[u] + 1, -st.query(dt[u], dt[u] + 1).x);
st.update(dt[u], ft[u], +1);
};
for (int i = 0; i < k; ++i) upsub(i);
for (int i = k; i <= n; ++i) {
cout << st.query(0, n + 1).x << " \n"[i == n];
upsub(i);
st.update(dt[i - k], dt[i - k] + 1, -1e9);
}
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;
enum seg_type { seg_not_lazy, seg_lazy, seg_beats };
template <class node, seg_type type>
struct segment_tree {
using node_container = typename node::node_container;
using lazy_container = typename node::lazy_container;
private:
template <const bool T = (type != seg_not_lazy),
typename enable_if<T, bool>::type = 0>
inline void push(int x, int b, int e, int m) {
if (st[x].lazy()) {
st[x + 1].apply(b, m, st[x].lazy);
st[x + ((m - b) << 1)].apply(m, e, st[x].lazy);
st[x].lazy = lazy_container();
}
}
template <const bool T = (type != seg_not_lazy),
typename enable_if<!T, bool>::type = 0>
inline void push(int x, int b, int e, int m) {}
template <typename RAIter>
void build(int x, int b, int e, const RAIter &a) {
if (b + 1 == e) {
st[x].nod.build(a[b]);
return;
}
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
build(x + 1, b, m, a);
build(y, m, e, a);
st[x].nod = st[x + 1].nod + st[y].nod;
}
template <const bool T = (type == seg_beats),
typename enable_if<!T, bool>::type = 0>
void update_(int x, int b, int e) {
if (lo <= b && e <= hi) {
st[x].apply(b, e, lazy);
return;
}
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
push(x, b, e, m);
if (lo < m) update_(x + 1, b, m);
if (m < hi) update_(y, m, e);
st[x].nod = st[x + 1].nod + st[y].nod;
}
template <const bool T = (type == seg_beats),
typename enable_if<T, bool>::type = 0>
void update_(int x, int b, int e) {
if (st[x].break_condition(lazy)) return;
if (lo <= b && e <= hi && st[x].tag_condition(lazy)) {
st[x].apply(b, e, lazy);
return;
}
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
push(x, b, e, m);
if (lo < m) update_(x + 1, b, m);
if (m < hi) update_(y, m, e);
st[x].nod = st[x + 1].nod + st[y].nod;
}
node_container query(int x, int b, int e) {
if (lo <= b && e <= hi) return st[x].nod;
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
push(x, b, e, m);
if (m >= hi) return query(x + 1, b, m);
if (m <= lo) return query(y, m, e);
return query(x + 1, b, m) + query(y, m, e);
}
template <class P>
int find_first(int x, int b, int e, const P &f) {
if (b + 1 == e) return f(st[x]) ? b : -1;
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
push(x, b, e, m);
if (lo < m && f(st[x + 1])) {
auto t = find_first(x + 1, b, m, f);
if (t != -1) return t;
}
if (m < hi && f(st[y])) return find_first(y, m, e, f);
return -1;
}
template <class P>
int find_last(int x, int b, int e, const P &f) {
if (b + 1 == e) return f(st[x]) ? b : -1;
int m = (b + e) >> 1;
int y = x + ((m - b) << 1);
push(x, b, e, m);
if (m < hi && f(st[y])) {
auto t = find_last(y, m, e, f);
if (t != -1) return t;
}
if (lo < m && f(st[x + 1])) return find_last(x + 1, b, m, f);
return -1;
}
lazy_container lazy;
int n, lo, hi;
vector<node> st;
public:
template <typename RAIter>
void build(const RAIter &a) {
build(0, 0, n, a);
}
void update(int l, int r, const lazy_container &x) {
lo = l, hi = r, lazy = x, update_(0, 0, n);
}
node_container query(int l, int r) { return lo = l, hi = r, query(0, 0, n); }
template <class P>
int find_first(int l, int r, const P &f) {
return lo = l, hi = r, find_first(0, 0, n, f);
}
template <class P>
int find_last(int l, int r, const P &f) {
return lo = l, hi = r, find_last(0, 0, n, f);
}
segment_tree(int n) : n(n), st(2 * n - 1) {}
};
struct node {
struct node_container {
int x;
friend node_container operator+(node_container l, const node_container &r) {
l.x = max(l.x, r.x);
return l;
}
node_container() : x(-1e9) {}
} nod;
struct lazy_container {
int s;
inline bool operator()() { return s != 0; }
lazy_container(int s = 0) : s(s) {}
} lazy;
inline void apply(int l, int r, lazy_container &p) {
nod.x += p.s;
lazy.s += p.s;
}
};
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (auto &i : a) cin >> i;
a.push_back(1e9);
vector<int> stk = {n};
vector<vector<int>> adj(n + 1);
for (int i = n - 1; i >= 0; --i) {
while (a[i] >= a[stk.back()]) stk.pop_back();
adj[stk.back()].push_back(i);
stk.push_back(i);
}
int id = 0;
vector<int> dt(n + 1), ft(n + 1);
function<void(int)> dfs = [&](int u) {
dt[u] = id++;
for (auto v : adj[u]) dfs(v);
ft[u] = id;
};
dfs(n);
segment_tree<node, seg_lazy> st(n + 1);
auto upsub = [&](int u) {
st.update(dt[u], dt[u] + 1, -st.query(dt[u], dt[u] + 1).x);
st.update(dt[u], ft[u], +1);
};
for (int i = 0; i < k; ++i) upsub(i);
for (int i = k; i <= n; ++i) {
cout << st.query(0, n + 1).x << " \n"[i == n];
upsub(i);
st.update(dt[i - k], dt[i - k] + 1, -1e9);
}
return 0;
}
``` |
#include <bits/stdc++.h>
const int N = 1e6 + 10;
using namespace std;
int n, m;
vector<int> g[N];
int a[N], b[N], l[N], r[N], fa[N];
struct node {
int sum;
int mx, tag;
} f[N * 4];
void down(int l, int r, int s) {
if (f[s].tag) {
if (f[s].sum) f[s].mx += f[s].tag;
if (l != r) f[s << 1].tag += f[s].tag, f[s << 1 | 1].tag += f[s].tag;
f[s].tag = 0;
}
}
void combine(int s) {
f[s].sum = f[s + s].sum + f[s + s + 1].sum;
f[s].mx = max(f[s + s].mx, f[s + s + 1].mx);
}
void ins(int l, int r, int s, int ll, bool sig) {
down(l, r, s);
if (r < ll || l > ll) return;
if (l == r) {
if (sig)
f[s].sum = 1, f[s].mx = 0;
else
f[s].sum = f[s].mx = 0;
return;
}
ins(l, (l + r) / 2, s + s, ll, sig);
ins((l + r) / 2 + 1, r, s + s + 1, ll, sig);
combine(s);
}
void add(int l, int r, int s, int ll, int rr, int tag) {
down(l, r, s);
if (r < ll || rr < l) return;
if (ll <= l && r <= rr) {
f[s].tag += tag;
down(l, r, s);
return;
}
add(l, (l + r) / 2, s << 1, ll, rr, tag);
add((l + r) / 2 + 1, r, s + s + 1, ll, rr, tag);
combine(s);
}
void dfs(int x) {
static int tot = 0;
l[x] = ++tot;
for (auto u : g[x])
if (u != fa[x]) {
fa[u] = x;
dfs(u);
}
r[x] = tot;
}
void ins(int x, int y) {
while (x) {
b[x] = y;
x -= x & -x;
}
}
int get(int x) {
int v = 1e9;
while (x <= n) {
v = min(v, b[x]);
x += x & -x;
}
return v;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) b[i] = 1e9;
for (int i = n; i >= 1; i--) {
int v = get(a[i]);
if (v != 1e9) g[v].push_back(i);
ins(a[i] - 1, i);
}
for (int i = n; i >= 1; i--)
if (!l[i]) dfs(i);
for (int i = 1; i <= m; i++) {
add(1, n, 1, l[i], r[i], 1);
ins(1, n, 1, l[i], 1);
}
for (int i = 1; i + m - 1 <= n; i++) {
printf("%d ", f[1].mx + 1);
ins(1, n, 1, l[i], 0);
add(1, n, 1, l[i + m], r[i + m], 1);
ins(1, n, 1, l[i + m], 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>
const int N = 1e6 + 10;
using namespace std;
int n, m;
vector<int> g[N];
int a[N], b[N], l[N], r[N], fa[N];
struct node {
int sum;
int mx, tag;
} f[N * 4];
void down(int l, int r, int s) {
if (f[s].tag) {
if (f[s].sum) f[s].mx += f[s].tag;
if (l != r) f[s << 1].tag += f[s].tag, f[s << 1 | 1].tag += f[s].tag;
f[s].tag = 0;
}
}
void combine(int s) {
f[s].sum = f[s + s].sum + f[s + s + 1].sum;
f[s].mx = max(f[s + s].mx, f[s + s + 1].mx);
}
void ins(int l, int r, int s, int ll, bool sig) {
down(l, r, s);
if (r < ll || l > ll) return;
if (l == r) {
if (sig)
f[s].sum = 1, f[s].mx = 0;
else
f[s].sum = f[s].mx = 0;
return;
}
ins(l, (l + r) / 2, s + s, ll, sig);
ins((l + r) / 2 + 1, r, s + s + 1, ll, sig);
combine(s);
}
void add(int l, int r, int s, int ll, int rr, int tag) {
down(l, r, s);
if (r < ll || rr < l) return;
if (ll <= l && r <= rr) {
f[s].tag += tag;
down(l, r, s);
return;
}
add(l, (l + r) / 2, s << 1, ll, rr, tag);
add((l + r) / 2 + 1, r, s + s + 1, ll, rr, tag);
combine(s);
}
void dfs(int x) {
static int tot = 0;
l[x] = ++tot;
for (auto u : g[x])
if (u != fa[x]) {
fa[u] = x;
dfs(u);
}
r[x] = tot;
}
void ins(int x, int y) {
while (x) {
b[x] = y;
x -= x & -x;
}
}
int get(int x) {
int v = 1e9;
while (x <= n) {
v = min(v, b[x]);
x += x & -x;
}
return v;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) b[i] = 1e9;
for (int i = n; i >= 1; i--) {
int v = get(a[i]);
if (v != 1e9) g[v].push_back(i);
ins(a[i] - 1, i);
}
for (int i = n; i >= 1; i--)
if (!l[i]) dfs(i);
for (int i = 1; i <= m; i++) {
add(1, n, 1, l[i], r[i], 1);
ins(1, n, 1, l[i], 1);
}
for (int i = 1; i + m - 1 <= n; i++) {
printf("%d ", f[1].mx + 1);
ins(1, n, 1, l[i], 0);
add(1, n, 1, l[i + m], r[i + m], 1);
ins(1, n, 1, l[i + m], 1);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long x = 0, F = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') F = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * F;
}
int n, k, a[1000000 + 5], stk[1000000 + 5], tp;
int dfn[1000000 + 5], ot[1000000 + 5], ct;
vector<int> G[1000000 + 5];
struct Seg_tree {
int mx[(1000000 << 2) + 5], tag[(1000000 << 2) + 5];
void pushdown(int x) {
if (tag[x]) {
mx[(x << 1)] += tag[x], tag[(x << 1)] += tag[x];
mx[(x << 1 | 1)] += tag[x], tag[(x << 1 | 1)] += tag[x];
tag[x] = 0;
}
}
void Insert(int x, int l, int r, int L, int R, int v) {
if (r < L || R < l) return;
if (L <= l && r <= R) {
tag[x] += v, mx[x] += v;
return;
}
pushdown(x);
Insert((x << 1), l, (l + r >> 1), L, R, v),
Insert((x << 1 | 1), (l + r >> 1) + 1, r, L, R, v);
mx[x] = max(mx[(x << 1)], mx[(x << 1 | 1)]);
}
} T;
void dfs(int x) {
dfn[x] = ++ct;
for (int i = 0; i < G[x].size(); i++) dfs(G[x][i]);
ot[x] = ct;
}
int main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
a[n + 1] = n + 1;
for (int i = 1; i <= n + 1; i++) {
while (tp && a[stk[tp]] < a[i]) G[i].push_back(stk[tp]), tp--;
stk[++tp] = i;
}
dfs(n + 1);
for (int i = 1; i <= k; i++) T.Insert(1, 1, n + 1, dfn[i], ot[i], 1);
printf("%d ", T.mx[1]);
for (int i = k + 1; i <= n; i++) {
T.Insert(1, 1, n + 1, dfn[i - k], ot[i - k], -1);
T.Insert(1, 1, n + 1, dfn[i], ot[i], 1);
printf("%d ", T.mx[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;
long long read() {
long long x = 0, F = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') F = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * F;
}
int n, k, a[1000000 + 5], stk[1000000 + 5], tp;
int dfn[1000000 + 5], ot[1000000 + 5], ct;
vector<int> G[1000000 + 5];
struct Seg_tree {
int mx[(1000000 << 2) + 5], tag[(1000000 << 2) + 5];
void pushdown(int x) {
if (tag[x]) {
mx[(x << 1)] += tag[x], tag[(x << 1)] += tag[x];
mx[(x << 1 | 1)] += tag[x], tag[(x << 1 | 1)] += tag[x];
tag[x] = 0;
}
}
void Insert(int x, int l, int r, int L, int R, int v) {
if (r < L || R < l) return;
if (L <= l && r <= R) {
tag[x] += v, mx[x] += v;
return;
}
pushdown(x);
Insert((x << 1), l, (l + r >> 1), L, R, v),
Insert((x << 1 | 1), (l + r >> 1) + 1, r, L, R, v);
mx[x] = max(mx[(x << 1)], mx[(x << 1 | 1)]);
}
} T;
void dfs(int x) {
dfn[x] = ++ct;
for (int i = 0; i < G[x].size(); i++) dfs(G[x][i]);
ot[x] = ct;
}
int main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
a[n + 1] = n + 1;
for (int i = 1; i <= n + 1; i++) {
while (tp && a[stk[tp]] < a[i]) G[i].push_back(stk[tp]), tp--;
stk[++tp] = i;
}
dfs(n + 1);
for (int i = 1; i <= k; i++) T.Insert(1, 1, n + 1, dfn[i], ot[i], 1);
printf("%d ", T.mx[1]);
for (int i = k + 1; i <= n; i++) {
T.Insert(1, 1, n + 1, dfn[i - k], ot[i - k], -1);
T.Insert(1, 1, n + 1, dfn[i], ot[i], 1);
printf("%d ", T.mx[1]);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7;
int add(int a, int b) {
if ((a += b) >= P) a -= P;
return a < 0 ? a + P : a;
}
int mul(int a, int b) { return 1ll * a * b % P; }
int kpow(int a, int b) {
int r = 1;
for (; b; b >>= 1, a = mul(a, a)) {
if (b & 1) r = mul(r, a);
}
return r;
}
const int N = 1e6 + 7;
int a[N], b[N], tr[N * 4], tip[N * 4], lft[N], rht[N], tim, n, k;
vector<int> g[N];
void dfs(int u) {
lft[u] = ++tim;
for (auto v : g[u]) dfs(v);
rht[u] = tim;
}
void change(int l, int r, int now, int L, int R, int w) {
if (L <= l && R >= r) {
tip[now] += w;
return;
}
int mid = (l + r) >> 1;
if (L <= mid) change(l, mid, (now << 1), L, R, w);
if (R > mid) change(mid + 1, r, ((now << 1) | 1), L, R, w);
tr[now] = max(tr[(now << 1)] + tip[(now << 1)],
tr[((now << 1) | 1)] + tip[((now << 1) | 1)]);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> k;
for (int i = (1); i < (n + 1); i++) cin >> a[i];
for (int i = (n + 1) - 1; i >= (1); i--) {
for (b[i] = i + 1; b[i] <= n && a[b[i]] <= a[i]; b[i] = b[b[i]])
;
g[b[i]].push_back(i);
}
dfs(n + 1);
for (int i = (1); i < (k + 1); i++) change(1, tim, 1, lft[i], rht[i], 1);
cout << tip[1] + tr[1] << " ";
for (int i = (k + 1); i < (n + 1); i++) {
change(1, tim, 1, lft[i], rht[i], 1);
change(1, tim, 1, lft[i - k], lft[i - k], -(1ll << (30)));
cout << tr[1] + tip[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 P = 1e9 + 7;
int add(int a, int b) {
if ((a += b) >= P) a -= P;
return a < 0 ? a + P : a;
}
int mul(int a, int b) { return 1ll * a * b % P; }
int kpow(int a, int b) {
int r = 1;
for (; b; b >>= 1, a = mul(a, a)) {
if (b & 1) r = mul(r, a);
}
return r;
}
const int N = 1e6 + 7;
int a[N], b[N], tr[N * 4], tip[N * 4], lft[N], rht[N], tim, n, k;
vector<int> g[N];
void dfs(int u) {
lft[u] = ++tim;
for (auto v : g[u]) dfs(v);
rht[u] = tim;
}
void change(int l, int r, int now, int L, int R, int w) {
if (L <= l && R >= r) {
tip[now] += w;
return;
}
int mid = (l + r) >> 1;
if (L <= mid) change(l, mid, (now << 1), L, R, w);
if (R > mid) change(mid + 1, r, ((now << 1) | 1), L, R, w);
tr[now] = max(tr[(now << 1)] + tip[(now << 1)],
tr[((now << 1) | 1)] + tip[((now << 1) | 1)]);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> k;
for (int i = (1); i < (n + 1); i++) cin >> a[i];
for (int i = (n + 1) - 1; i >= (1); i--) {
for (b[i] = i + 1; b[i] <= n && a[b[i]] <= a[i]; b[i] = b[b[i]])
;
g[b[i]].push_back(i);
}
dfs(n + 1);
for (int i = (1); i < (k + 1); i++) change(1, tim, 1, lft[i], rht[i], 1);
cout << tip[1] + tr[1] << " ";
for (int i = (k + 1); i < (n + 1); i++) {
change(1, tim, 1, lft[i], rht[i], 1);
change(1, tim, 1, lft[i - k], lft[i - k], -(1ll << (30)));
cout << tr[1] + tip[1] << " ";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
int n, k;
int a[MAXN];
void read_input() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
}
int mi[MAXN];
vector<int> graph[MAXN];
int in[MAXN], out[MAXN];
int timer;
void dfs(int v) {
in[v] = timer++;
for (auto x : graph[v]) {
dfs(x);
}
out[v] = timer++;
}
const int TSIZE = (1 << 23);
int tree[TSIZE], lazy[TSIZE];
void push(int k, int l, int r) {
if (lazy[k]) {
tree[k] += lazy[k];
if (l != r) {
lazy[k * 2] += lazy[k];
lazy[k * 2 + 1] += lazy[k];
}
lazy[k] = 0;
}
}
void update(int k, int l, int r, int i, int j, int val) {
push(k, l, r);
if (r < i || l > j || l > r) return;
if (r <= j && l >= i) {
tree[k] += val;
if (l != r) {
lazy[k * 2] += val;
lazy[k * 2 + 1] += val;
}
return;
}
int middle = (l + r) >> 1;
update(k * 2, l, middle, i, j, val);
update(k * 2 + 1, middle + 1, r, i, j, val);
tree[k] = max(tree[k * 2], tree[k * 2 + 1]);
}
void solve() {
stack<pair<int, int>> st;
for (int i = n; i >= 1; i--) {
pair<int, int> temp = {i, a[i]};
while (!st.empty() && st.top().second <= a[i]) {
st.pop();
}
int ans = n + 1;
if (!st.empty()) ans = st.top().first;
st.push(temp);
graph[ans].push_back(i);
}
dfs(n + 1);
for (int i = 1; i <= k - 1; i++) {
update(1, 0, timer - 1, in[i], out[i], 1);
}
for (int i = k; i <= n; i++) {
update(1, 0, timer - 1, in[i], out[i], 1);
push(1, 0, timer - 1);
cout << tree[1] << " ";
update(1, 0, timer - 1, in[i - k + 1], out[i - k + 1], -1);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
read_input();
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 MAXN = 1e6 + 10;
int n, k;
int a[MAXN];
void read_input() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
}
int mi[MAXN];
vector<int> graph[MAXN];
int in[MAXN], out[MAXN];
int timer;
void dfs(int v) {
in[v] = timer++;
for (auto x : graph[v]) {
dfs(x);
}
out[v] = timer++;
}
const int TSIZE = (1 << 23);
int tree[TSIZE], lazy[TSIZE];
void push(int k, int l, int r) {
if (lazy[k]) {
tree[k] += lazy[k];
if (l != r) {
lazy[k * 2] += lazy[k];
lazy[k * 2 + 1] += lazy[k];
}
lazy[k] = 0;
}
}
void update(int k, int l, int r, int i, int j, int val) {
push(k, l, r);
if (r < i || l > j || l > r) return;
if (r <= j && l >= i) {
tree[k] += val;
if (l != r) {
lazy[k * 2] += val;
lazy[k * 2 + 1] += val;
}
return;
}
int middle = (l + r) >> 1;
update(k * 2, l, middle, i, j, val);
update(k * 2 + 1, middle + 1, r, i, j, val);
tree[k] = max(tree[k * 2], tree[k * 2 + 1]);
}
void solve() {
stack<pair<int, int>> st;
for (int i = n; i >= 1; i--) {
pair<int, int> temp = {i, a[i]};
while (!st.empty() && st.top().second <= a[i]) {
st.pop();
}
int ans = n + 1;
if (!st.empty()) ans = st.top().first;
st.push(temp);
graph[ans].push_back(i);
}
dfs(n + 1);
for (int i = 1; i <= k - 1; i++) {
update(1, 0, timer - 1, in[i], out[i], 1);
}
for (int i = k; i <= n; i++) {
update(1, 0, timer - 1, in[i], out[i], 1);
push(1, 0, timer - 1);
cout << tree[1] << " ";
update(1, 0, timer - 1, in[i - k + 1], out[i - k + 1], -1);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
read_input();
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename Monoid>
struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n) sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) L = f(L, seg[a++]);
if (b & 1) R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
template <typename Monoid, typename OperatorMonoid, typename F, typename G,
typename H, typename P>
struct LazySegmentTree {
int sz;
vector<Monoid> data;
vector<OperatorMonoid> lazy;
const F f;
const G g;
const H h;
const P p;
const Monoid M1;
const OperatorMonoid OM0;
LazySegmentTree(int n, const F f, const G g, const H h, const P p,
const Monoid &M1, const OperatorMonoid OM0)
: f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {
sz = 1;
while (sz < n) sz <<= 1;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void set(int k, const Monoid &x) { data[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
data[k] = f(data[2 * k + 0], data[2 * k + 1]);
}
}
void propagate(int k, int len) {
if (lazy[k] != OM0) {
if (k < sz) {
lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
}
data[k] = g(data[k], p(lazy[k], len));
lazy[k] = OM0;
}
}
Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return data[k];
} else if (a <= l && r <= b) {
lazy[k] = h(lazy[k], x);
propagate(k, r - l);
return data[k];
} else {
return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid update(int a, int b, const OperatorMonoid &x) {
return update(a, b, x, 1, 0, sz);
}
Monoid query(int a, int b, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return M1;
} else if (a <= l && r <= b) {
return data[k];
} else {
return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
query(a, b, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid query(int a, int b) { return query(a, b, 1, 0, sz); }
Monoid operator[](const int &k) { return query(k, k + 1); }
};
const int INF = 1 << 29;
vector<int> g[1000001];
int in[1000001], out[1000001], ptr;
void dfs(int idx) {
in[idx] = ptr++;
for (auto to : g[idx]) dfs(to);
out[idx] = ptr;
}
int main() {
int N, K, A[1000000];
scanf("%d %d", &N, &K);
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
--A[i];
}
auto f = [](int a, int b) { return min(a, b); };
SegmentTree<int> seg(N, f, N);
int to[1000000];
for (int i = N - 1; i >= 0; i--) {
to[i] = seg.query(A[i] + 1, N);
seg.update(A[i], i);
g[to[i]].emplace_back(i);
}
dfs(N);
using int64 = long long;
using pi = pair<int, int>;
auto f1 = [](int64 a, int64 b) { return max(a, b); };
auto g1 = [](int64 a, pi b) {
return b.second == 0 ? a + b.first : (b.second == 1 ? b.first : a);
};
auto h1 = [](pi a, pi b) {
if (b.second == -1) return a;
if (a.second == -1) return b;
return b.second == 0 ? pi(a.first + b.first, a.second) : b;
};
auto p1 = [](pi a, int p) { return a; };
LazySegmentTree<int64, pi, decltype(f1), decltype(g1), decltype(h1),
decltype(p1)>
seg2(N + 1, f1, g1, h1, p1, -INF, pi(0, -1));
vector<int> q(N + 1);
for (int i = N - 1; i >= 0; i--) {
seg2.update(in[i], in[i] + 1, pi(max(0LL, seg2[in[to[i]]]) + 1, 1));
q[i] = seg2.query(0, N + 1);
if (i + K <= N) {
seg2.update(in[i + K - 1], in[i + K - 1] + 1, pi(-INF, 1));
seg2.update(in[i + K - 1] + 1, out[i + K - 1], pi(-1, 0));
}
}
for (int i = 0; i <= N - K; i++) {
printf("%d ", q[i]);
}
}
| ### 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;
template <typename Monoid>
struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n) sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) L = f(L, seg[a++]);
if (b & 1) R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
};
template <typename Monoid, typename OperatorMonoid, typename F, typename G,
typename H, typename P>
struct LazySegmentTree {
int sz;
vector<Monoid> data;
vector<OperatorMonoid> lazy;
const F f;
const G g;
const H h;
const P p;
const Monoid M1;
const OperatorMonoid OM0;
LazySegmentTree(int n, const F f, const G g, const H h, const P p,
const Monoid &M1, const OperatorMonoid OM0)
: f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {
sz = 1;
while (sz < n) sz <<= 1;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void set(int k, const Monoid &x) { data[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
data[k] = f(data[2 * k + 0], data[2 * k + 1]);
}
}
void propagate(int k, int len) {
if (lazy[k] != OM0) {
if (k < sz) {
lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
}
data[k] = g(data[k], p(lazy[k], len));
lazy[k] = OM0;
}
}
Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return data[k];
} else if (a <= l && r <= b) {
lazy[k] = h(lazy[k], x);
propagate(k, r - l);
return data[k];
} else {
return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid update(int a, int b, const OperatorMonoid &x) {
return update(a, b, x, 1, 0, sz);
}
Monoid query(int a, int b, int k, int l, int r) {
propagate(k, r - l);
if (r <= a || b <= l) {
return M1;
} else if (a <= l && r <= b) {
return data[k];
} else {
return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
query(a, b, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid query(int a, int b) { return query(a, b, 1, 0, sz); }
Monoid operator[](const int &k) { return query(k, k + 1); }
};
const int INF = 1 << 29;
vector<int> g[1000001];
int in[1000001], out[1000001], ptr;
void dfs(int idx) {
in[idx] = ptr++;
for (auto to : g[idx]) dfs(to);
out[idx] = ptr;
}
int main() {
int N, K, A[1000000];
scanf("%d %d", &N, &K);
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
--A[i];
}
auto f = [](int a, int b) { return min(a, b); };
SegmentTree<int> seg(N, f, N);
int to[1000000];
for (int i = N - 1; i >= 0; i--) {
to[i] = seg.query(A[i] + 1, N);
seg.update(A[i], i);
g[to[i]].emplace_back(i);
}
dfs(N);
using int64 = long long;
using pi = pair<int, int>;
auto f1 = [](int64 a, int64 b) { return max(a, b); };
auto g1 = [](int64 a, pi b) {
return b.second == 0 ? a + b.first : (b.second == 1 ? b.first : a);
};
auto h1 = [](pi a, pi b) {
if (b.second == -1) return a;
if (a.second == -1) return b;
return b.second == 0 ? pi(a.first + b.first, a.second) : b;
};
auto p1 = [](pi a, int p) { return a; };
LazySegmentTree<int64, pi, decltype(f1), decltype(g1), decltype(h1),
decltype(p1)>
seg2(N + 1, f1, g1, h1, p1, -INF, pi(0, -1));
vector<int> q(N + 1);
for (int i = N - 1; i >= 0; i--) {
seg2.update(in[i], in[i] + 1, pi(max(0LL, seg2[in[to[i]]]) + 1, 1));
q[i] = seg2.query(0, N + 1);
if (i + K <= N) {
seg2.update(in[i + K - 1], in[i + K - 1] + 1, pi(-INF, 1));
seg2.update(in[i + K - 1] + 1, out[i + K - 1], pi(-1, 0));
}
}
for (int i = 0; i <= N - K; i++) {
printf("%d ", q[i]);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, tt, num[1001000], L[1001000];
struct Node {
int ls, rs, mx, sum;
} node[1001000 << 1];
inline void up(int now) {
int L = node[now].ls, R = node[now].rs;
node[now].mx = max(node[L].mx, node[R].mx) + node[now].sum;
}
void build(int now, int l, int r) {
if (l == r) return;
int mid = ((l + r) >> 1);
node[now].ls = ++tt;
build(tt, l, mid);
node[now].rs = ++tt;
build(tt, mid + 1, r);
}
void add(int now, int l, int r, int u, int v, int w) {
if (u <= l && r <= v) {
node[now].mx += w;
node[now].sum += w;
return;
}
int mid = ((l + r) >> 1);
if (u <= mid) add(node[now].ls, l, mid, u, v, w);
if (mid < v) add(node[now].rs, mid + 1, r, u, v, w);
up(now);
}
int ask(int now, int l, int r, int u, int v) {
if (u <= l && r <= v) return node[now].mx;
int res = 0, mid = ((l + r) >> 1);
if (u <= mid) res = max(res, ask(node[now].ls, l, mid, u, v));
if (mid < v) res = max(res, ask(node[now].rs, mid + 1, r, u, v));
return res + node[now].sum;
}
int main() {
scanf("%d%d", &n, &m), num[0] = 1001000;
for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
for (int i = 1; i <= n; i++) {
L[i] = i - 1;
for (; num[i] > num[L[i]]; L[i] = L[L[i]])
;
}
build(tt = 1, 1, n);
for (int i = 1; i < m; i++) add(1, 1, n, L[i] + 1, i, 1);
for (int i = 1, j = m; j <= n; i++, j++)
add(1, 1, n, L[j] + 1, j, 1), printf("%d ", ask(1, 1, n, i, j));
}
| ### 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, m, tt, num[1001000], L[1001000];
struct Node {
int ls, rs, mx, sum;
} node[1001000 << 1];
inline void up(int now) {
int L = node[now].ls, R = node[now].rs;
node[now].mx = max(node[L].mx, node[R].mx) + node[now].sum;
}
void build(int now, int l, int r) {
if (l == r) return;
int mid = ((l + r) >> 1);
node[now].ls = ++tt;
build(tt, l, mid);
node[now].rs = ++tt;
build(tt, mid + 1, r);
}
void add(int now, int l, int r, int u, int v, int w) {
if (u <= l && r <= v) {
node[now].mx += w;
node[now].sum += w;
return;
}
int mid = ((l + r) >> 1);
if (u <= mid) add(node[now].ls, l, mid, u, v, w);
if (mid < v) add(node[now].rs, mid + 1, r, u, v, w);
up(now);
}
int ask(int now, int l, int r, int u, int v) {
if (u <= l && r <= v) return node[now].mx;
int res = 0, mid = ((l + r) >> 1);
if (u <= mid) res = max(res, ask(node[now].ls, l, mid, u, v));
if (mid < v) res = max(res, ask(node[now].rs, mid + 1, r, u, v));
return res + node[now].sum;
}
int main() {
scanf("%d%d", &n, &m), num[0] = 1001000;
for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
for (int i = 1; i <= n; i++) {
L[i] = i - 1;
for (; num[i] > num[L[i]]; L[i] = L[L[i]])
;
}
build(tt = 1, 1, n);
for (int i = 1; i < m; i++) add(1, 1, n, L[i] + 1, i, 1);
for (int i = 1, j = m; j <= n; i++, j++)
add(1, 1, n, L[j] + 1, j, 1), printf("%d ", ask(1, 1, n, i, j));
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000100;
int tin[MAXN], tout[MAXN], a[MAXN];
int d[8 * MAXN], p[8 * MAXN];
vector<int> g[MAXN];
inline void push(int v, int l, int r) {
d[v] += p[v];
if (l != r) {
p[v << 1] += p[v];
p[v << 1 | 1] += p[v];
}
p[v] = 0;
}
void update(int v, int l, int r, int ql, int qr, const int &val) {
push(v, l, r);
if (l == ql && r == qr) {
p[v] += val;
push(v, l, r);
return;
}
int mid = l + r >> 1;
if (mid >= qr)
update(v << 1, l, mid, ql, qr, val);
else if (mid < ql)
update(v << 1 | 1, mid + 1, r, ql, qr, val);
else {
update(v << 1, l, mid, ql, mid, val);
update(v << 1 | 1, mid + 1, r, mid + 1, qr, val);
}
d[v] = max(d[v << 1] + p[v << 1], d[v << 1 | 1] + p[v << 1 | 1]);
}
int pos = 0;
void dfs(int v) {
tin[v] = pos++;
for (auto to : g[v]) dfs(to);
tout[v] = pos;
}
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];
}
stack<int> st;
for (int i = 1; i <= n; i++) {
while (!st.empty() && a[st.top()] < a[i]) {
g[i].push_back(st.top());
st.pop();
}
st.push(i);
}
while (!st.empty()) {
g[0].push_back(st.top());
st.pop();
}
dfs(0);
for (int i = 1; i <= k; i++) {
update(1, 0, pos, tin[i], tout[i] - 1, 1);
}
for (int i = k; i <= n; i++) {
cout << d[1] + p[1] << ' ';
if (i != n) {
update(1, 0, pos, tin[i + 1], tout[i + 1] - 1, +1);
update(1, 0, pos, tin[i - k + 1], tout[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 int MAXN = 1000100;
int tin[MAXN], tout[MAXN], a[MAXN];
int d[8 * MAXN], p[8 * MAXN];
vector<int> g[MAXN];
inline void push(int v, int l, int r) {
d[v] += p[v];
if (l != r) {
p[v << 1] += p[v];
p[v << 1 | 1] += p[v];
}
p[v] = 0;
}
void update(int v, int l, int r, int ql, int qr, const int &val) {
push(v, l, r);
if (l == ql && r == qr) {
p[v] += val;
push(v, l, r);
return;
}
int mid = l + r >> 1;
if (mid >= qr)
update(v << 1, l, mid, ql, qr, val);
else if (mid < ql)
update(v << 1 | 1, mid + 1, r, ql, qr, val);
else {
update(v << 1, l, mid, ql, mid, val);
update(v << 1 | 1, mid + 1, r, mid + 1, qr, val);
}
d[v] = max(d[v << 1] + p[v << 1], d[v << 1 | 1] + p[v << 1 | 1]);
}
int pos = 0;
void dfs(int v) {
tin[v] = pos++;
for (auto to : g[v]) dfs(to);
tout[v] = pos;
}
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];
}
stack<int> st;
for (int i = 1; i <= n; i++) {
while (!st.empty() && a[st.top()] < a[i]) {
g[i].push_back(st.top());
st.pop();
}
st.push(i);
}
while (!st.empty()) {
g[0].push_back(st.top());
st.pop();
}
dfs(0);
for (int i = 1; i <= k; i++) {
update(1, 0, pos, tin[i], tout[i] - 1, 1);
}
for (int i = k; i <= n; i++) {
cout << d[1] + p[1] << ' ';
if (i != n) {
update(1, 0, pos, tin[i + 1], tout[i + 1] - 1, +1);
update(1, 0, pos, tin[i - k + 1], tout[i - k + 1] - 1, -1);
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int ST[2100005];
int T[2100005 << 1], L[2100005 << 1];
vector<int> P[2100005];
int n, a[2100005], Nxt[2100005];
bool Vis[2100005];
int tot, St[2100005], Ed[2100005];
int IN() {
int x = 0;
char ch;
for ((ch = getchar()); ch > '9' || ch < '0'; (ch = getchar()))
;
for (; ch >= '0' && ch <= '9'; (ch = getchar())) (x *= 10) += ch - '0';
return x;
}
void Modify(int x, int y) {
for (; x; x -= x & -x) ST[x] = min(ST[x], y);
}
int Ask(int x) {
int tmp = 0x3f3f3f3f;
for (; x <= n; x += x & -x) tmp = min(tmp, ST[x]);
return tmp;
}
void Dfs(int x) {
Vis[x] = 1;
St[x] = ++tot;
for (auto v : P[x]) Dfs(v);
Ed[x] = tot;
}
void Add(int l, int r, int y) {
for (; l <= r; l >>= 1, r >>= 1) {
if (l & 1) {
L[l] += y;
for (int z = l >> 1; z; z >>= 1)
T[z] = max(T[z << 1] + L[z << 1], T[z << 1 | 1] + L[z << 1 | 1]);
l++;
}
if (!(r & 1)) {
L[r] += y;
for (int z = r; z > 1; z >>= 1)
T[z] = max(T[z << 1] + L[z << 1], T[z << 1 | 1] + L[z << 1 | 1]);
r--;
}
}
}
int Lazy(int x) {
int tmp = 0;
for (; x; x >>= 1) tmp += L[x];
return tmp;
}
int Query(int l, int r) {
int tmp = 0;
for (; l <= r; l >>= 1, r >>= 1) {
if (l & 1) {
tmp = max(tmp, T[l] + Lazy(l));
l++;
}
if (!(r & 1)) {
tmp = max(tmp, T[r] + Lazy(r));
r--;
}
}
return tmp;
}
int Len, ans[2100005], k;
int main() {
n = IN();
k = IN();
memset(ST, 0x3f, sizeof(ST));
for (int i = 1; i <= n; i++) a[i] = IN();
for (int i = n; i; i--) {
Modify(a[i], i);
Nxt[i] = Ask(a[i] + 1);
if (Nxt[i] != 0x3f3f3f3f) P[Nxt[i]].push_back(i);
}
for (int i = n; i; i--)
if (!Vis[i]) Dfs(i);
for (Len = 1; Len < n; Len <<= 1)
;
for (int i = n; i >= n - k + 1; i--) Add(St[i] + Len - 1, Ed[i] + Len - 1, 1);
ans[n - k + 1] = Query(Len, Len + n - 1);
for (int i = n - k; i; i--) {
Add(St[i] + Len - 1, Ed[i] + Len - 1, 1);
Add(St[i + k] + Len - 1, Ed[i + k] + Len - 1, -1);
ans[i] = Query(Len, Len + n - 1);
}
for (int i = 1; i <= n - k + 1; 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;
int ST[2100005];
int T[2100005 << 1], L[2100005 << 1];
vector<int> P[2100005];
int n, a[2100005], Nxt[2100005];
bool Vis[2100005];
int tot, St[2100005], Ed[2100005];
int IN() {
int x = 0;
char ch;
for ((ch = getchar()); ch > '9' || ch < '0'; (ch = getchar()))
;
for (; ch >= '0' && ch <= '9'; (ch = getchar())) (x *= 10) += ch - '0';
return x;
}
void Modify(int x, int y) {
for (; x; x -= x & -x) ST[x] = min(ST[x], y);
}
int Ask(int x) {
int tmp = 0x3f3f3f3f;
for (; x <= n; x += x & -x) tmp = min(tmp, ST[x]);
return tmp;
}
void Dfs(int x) {
Vis[x] = 1;
St[x] = ++tot;
for (auto v : P[x]) Dfs(v);
Ed[x] = tot;
}
void Add(int l, int r, int y) {
for (; l <= r; l >>= 1, r >>= 1) {
if (l & 1) {
L[l] += y;
for (int z = l >> 1; z; z >>= 1)
T[z] = max(T[z << 1] + L[z << 1], T[z << 1 | 1] + L[z << 1 | 1]);
l++;
}
if (!(r & 1)) {
L[r] += y;
for (int z = r; z > 1; z >>= 1)
T[z] = max(T[z << 1] + L[z << 1], T[z << 1 | 1] + L[z << 1 | 1]);
r--;
}
}
}
int Lazy(int x) {
int tmp = 0;
for (; x; x >>= 1) tmp += L[x];
return tmp;
}
int Query(int l, int r) {
int tmp = 0;
for (; l <= r; l >>= 1, r >>= 1) {
if (l & 1) {
tmp = max(tmp, T[l] + Lazy(l));
l++;
}
if (!(r & 1)) {
tmp = max(tmp, T[r] + Lazy(r));
r--;
}
}
return tmp;
}
int Len, ans[2100005], k;
int main() {
n = IN();
k = IN();
memset(ST, 0x3f, sizeof(ST));
for (int i = 1; i <= n; i++) a[i] = IN();
for (int i = n; i; i--) {
Modify(a[i], i);
Nxt[i] = Ask(a[i] + 1);
if (Nxt[i] != 0x3f3f3f3f) P[Nxt[i]].push_back(i);
}
for (int i = n; i; i--)
if (!Vis[i]) Dfs(i);
for (Len = 1; Len < n; Len <<= 1)
;
for (int i = n; i >= n - k + 1; i--) Add(St[i] + Len - 1, Ed[i] + Len - 1, 1);
ans[n - k + 1] = Query(Len, Len + n - 1);
for (int i = n - k; i; i--) {
Add(St[i] + Len - 1, Ed[i] + Len - 1, 1);
Add(St[i + k] + Len - 1, Ed[i + k] + Len - 1, -1);
ans[i] = Query(Len, Len + n - 1);
}
for (int i = 1; i <= n - k + 1; i++) printf("%d ", ans[i]);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e6 + 5;
int sta[M], tp, nxt[M], siz[M], head[M], cnt, ID[M], ord, w[M << 2],
lazy[M << 2];
long long n, k, a[M];
stack<int> S;
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return f * x;
}
struct edge {
int v, next;
} e[M];
void add(int u, int v) {
siz[u] += siz[v];
e[++cnt].v = v;
e[cnt].next = head[u];
head[u] = cnt;
}
void pushup(int rt) { w[rt] = max(w[rt << 1], w[rt << 1 | 1]); }
void pushdown(int rt) {
if (!lazy[rt]) return;
int l = rt << 1, r = rt << 1 | 1;
w[l] += lazy[rt];
w[r] += lazy[rt];
lazy[l] += lazy[rt];
lazy[r] += lazy[rt];
lazy[rt] = 0;
}
void update(int L, int R, int val, int l, int r, int rt) {
if (L <= l && r <= R) {
w[rt] += val;
lazy[rt] += val;
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
if (L <= mid) update(L, R, val, l, mid, rt << 1);
if (R > mid) update(L, R, val, mid + 1, r, rt << 1 | 1);
pushup(rt);
}
int main() {
n = read();
k = read();
for (int i = 1; i <= n; i++) a[i] = read(), siz[i] = 1;
for (int i = 1; i <= n; i++) {
while (a[i] > a[sta[tp]] && tp) nxt[sta[tp]] = i, add(i, sta[tp]), tp--;
sta[++tp] = i;
}
while (tp) nxt[sta[tp]] = n + 1, add(n + 1, sta[tp]), tp--;
S.push(++n);
while (!S.empty()) {
int s = S.top();
S.pop();
ID[s] = ++ord;
for (int i = head[s]; i != 0; i = e[i].next) S.push(e[i].v);
}
for (int i = 1; i <= k; i++) update(ID[i], ID[i] + siz[i] - 1, 1, 1, n, 1);
printf("%d ", w[1]);
for (int i = k + 1; i < n; i++) {
update(ID[i], ID[i] + siz[i] - 1, 1, 1, n, 1);
update(ID[i - k], ID[i - k] + siz[i - k] - 1, -1, 1, n, 1);
printf("%d ", w[1]);
}
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 M = 1e6 + 5;
int sta[M], tp, nxt[M], siz[M], head[M], cnt, ID[M], ord, w[M << 2],
lazy[M << 2];
long long n, k, a[M];
stack<int> S;
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return f * x;
}
struct edge {
int v, next;
} e[M];
void add(int u, int v) {
siz[u] += siz[v];
e[++cnt].v = v;
e[cnt].next = head[u];
head[u] = cnt;
}
void pushup(int rt) { w[rt] = max(w[rt << 1], w[rt << 1 | 1]); }
void pushdown(int rt) {
if (!lazy[rt]) return;
int l = rt << 1, r = rt << 1 | 1;
w[l] += lazy[rt];
w[r] += lazy[rt];
lazy[l] += lazy[rt];
lazy[r] += lazy[rt];
lazy[rt] = 0;
}
void update(int L, int R, int val, int l, int r, int rt) {
if (L <= l && r <= R) {
w[rt] += val;
lazy[rt] += val;
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
if (L <= mid) update(L, R, val, l, mid, rt << 1);
if (R > mid) update(L, R, val, mid + 1, r, rt << 1 | 1);
pushup(rt);
}
int main() {
n = read();
k = read();
for (int i = 1; i <= n; i++) a[i] = read(), siz[i] = 1;
for (int i = 1; i <= n; i++) {
while (a[i] > a[sta[tp]] && tp) nxt[sta[tp]] = i, add(i, sta[tp]), tp--;
sta[++tp] = i;
}
while (tp) nxt[sta[tp]] = n + 1, add(n + 1, sta[tp]), tp--;
S.push(++n);
while (!S.empty()) {
int s = S.top();
S.pop();
ID[s] = ++ord;
for (int i = head[s]; i != 0; i = e[i].next) S.push(e[i].v);
}
for (int i = 1; i <= k; i++) update(ID[i], ID[i] + siz[i] - 1, 1, 1, n, 1);
printf("%d ", w[1]);
for (int i = k + 1; i < n; i++) {
update(ID[i], ID[i] + siz[i] - 1, 1, 1, n, 1);
update(ID[i - k], ID[i - k] + siz[i - k] - 1, -1, 1, n, 1);
printf("%d ", w[1]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int tree[4 * 1000000 + 50];
int lazy[4 * 1000000 + 50];
inline void update(int node, int left, int right, int a, int b, int value) {
int l = (node << 1);
int r = (node << 1) + 1;
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (left != right) {
lazy[l] += lazy[node];
lazy[r] += lazy[node];
}
lazy[node] = 0;
}
if (left > b || right < a) return;
if (left >= a && right <= b) {
tree[node] += value;
if (left != right) {
lazy[l] += value;
lazy[r] += value;
}
return;
}
int mid = (left + right) >> 1;
update(l, left, mid, a, b, value);
update(r, mid + 1, right, a, b, value);
tree[node] = max(tree[l], tree[r]);
return;
}
inline int query(int node, int left, int right, int a, int b) {
int l = (node << 1);
int r = (node << 1) + 1;
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (left != right) {
lazy[l] += lazy[node];
lazy[r] += lazy[node];
}
lazy[node] = 0;
}
if (left > b || right < a) return 0;
if (left >= a && right <= b) return tree[node];
int mid = (left + right) >> 1;
int ret = max(query(l, left, mid, a, b), query(r, mid + 1, right, a, b));
tree[node] = max(tree[l], tree[r]);
return ret;
}
vector<int> vt[1000000 + 50];
int prnt[1000000 + 50];
int ans[1000000 + 50];
int a[1000000 + 50];
int l[1000000 + 50];
int r[1000000 + 50];
int cnt = 0;
inline void dfs(int u, int pre) {
prnt[u] = pre;
l[u] = ++cnt;
for (int v : vt[u]) {
dfs(v, u);
}
r[u] = cnt;
}
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
q;
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[n + 1] = n + 1;
q.push(make_pair(n + 1, n + 1));
for (int i = 1; i <= n + 1; i++) {
while (q.top().first < a[i]) {
vt[i].push_back(q.top().second);
q.pop();
}
q.push(make_pair(a[i], i));
}
dfs(n + 1, -1);
int s = n + 1;
int e = n;
for (int i = n; i - k + 1 > 0; i--) {
int st = i - k + 1;
while (st < s) {
s--;
int add = 1;
if (prnt[s] != n + 1)
add = query(1, 1, n + 1, l[prnt[s]], l[prnt[s]]) + 1;
int koto = query(1, 1, n + 1, l[s], l[s]);
update(1, 1, n + 1, l[s], r[s], add - koto);
}
while (e > i) {
update(1, 1, n + 1, l[e], r[e], -1);
e--;
}
ans[s] = query(1, 1, n + 1, 1, n + 1);
}
for (int i = 1; i + k - 1 <= n; i++) {
if (i != 1) printf(" ");
printf("%d", ans[i]);
}
printf("\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 tree[4 * 1000000 + 50];
int lazy[4 * 1000000 + 50];
inline void update(int node, int left, int right, int a, int b, int value) {
int l = (node << 1);
int r = (node << 1) + 1;
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (left != right) {
lazy[l] += lazy[node];
lazy[r] += lazy[node];
}
lazy[node] = 0;
}
if (left > b || right < a) return;
if (left >= a && right <= b) {
tree[node] += value;
if (left != right) {
lazy[l] += value;
lazy[r] += value;
}
return;
}
int mid = (left + right) >> 1;
update(l, left, mid, a, b, value);
update(r, mid + 1, right, a, b, value);
tree[node] = max(tree[l], tree[r]);
return;
}
inline int query(int node, int left, int right, int a, int b) {
int l = (node << 1);
int r = (node << 1) + 1;
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (left != right) {
lazy[l] += lazy[node];
lazy[r] += lazy[node];
}
lazy[node] = 0;
}
if (left > b || right < a) return 0;
if (left >= a && right <= b) return tree[node];
int mid = (left + right) >> 1;
int ret = max(query(l, left, mid, a, b), query(r, mid + 1, right, a, b));
tree[node] = max(tree[l], tree[r]);
return ret;
}
vector<int> vt[1000000 + 50];
int prnt[1000000 + 50];
int ans[1000000 + 50];
int a[1000000 + 50];
int l[1000000 + 50];
int r[1000000 + 50];
int cnt = 0;
inline void dfs(int u, int pre) {
prnt[u] = pre;
l[u] = ++cnt;
for (int v : vt[u]) {
dfs(v, u);
}
r[u] = cnt;
}
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
q;
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[n + 1] = n + 1;
q.push(make_pair(n + 1, n + 1));
for (int i = 1; i <= n + 1; i++) {
while (q.top().first < a[i]) {
vt[i].push_back(q.top().second);
q.pop();
}
q.push(make_pair(a[i], i));
}
dfs(n + 1, -1);
int s = n + 1;
int e = n;
for (int i = n; i - k + 1 > 0; i--) {
int st = i - k + 1;
while (st < s) {
s--;
int add = 1;
if (prnt[s] != n + 1)
add = query(1, 1, n + 1, l[prnt[s]], l[prnt[s]]) + 1;
int koto = query(1, 1, n + 1, l[s], l[s]);
update(1, 1, n + 1, l[s], r[s], add - koto);
}
while (e > i) {
update(1, 1, n + 1, l[e], r[e], -1);
e--;
}
ans[s] = query(1, 1, n + 1, 1, n + 1);
}
for (int i = 1; i + k - 1 <= n; i++) {
if (i != 1) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const int INF = 1e9 + 7;
int n, k;
int a[N];
int to[N];
vector<int> g[N];
int ptr = 0;
int e[N];
int l[N], r[N];
void dfs(int u, int p) {
l[u] = ptr;
e[ptr++] = u;
for (int v : g[u]) {
if (v != p) {
dfs(v, u);
}
}
r[u] = ptr;
}
int tree[N << 2], add[N << 2];
void push(int v) {
if (add[v]) {
add[v * 2 + 1] += add[v];
tree[v * 2 + 1] += add[v];
add[v * 2 + 2] += add[v];
tree[v * 2 + 2] += add[v];
add[v] = 0;
return;
}
}
void upd(int v, int tl, int tr, int p, int x) {
if (tl == tr) {
tree[v] = x;
return;
}
int tm = (tl + tr) >> 1;
push(v);
if (p <= tm)
upd(v * 2 + 1, tl, tm, p, x);
else
upd(v * 2 + 2, tm + 1, tr, p, x);
tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]);
}
void addt(int v, int tl, int tr, int l, int r) {
if (tr < l || r < tl) return;
if (l <= tl && tr <= r) {
tree[v]++;
add[v]++;
return;
}
int tm = (tl + tr) >> 1;
addt(v * 2 + 1, tl, tm, l, r);
addt(v * 2 + 2, tm + 1, tr, l, r);
tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]);
}
void addi(int i) {
upd(0, 0, n, l[i], 0);
addt(0, 0, n, l[i], r[i]);
}
void deli(int i) { upd(0, 0, n, l[i], -INF); }
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
a[n] = N - 1;
vector<int> v;
for (int i = 0; i <= n; ++i) {
while (v.size() && a[v.back()] < a[i]) {
to[v.back()] = i;
v.pop_back();
}
v.push_back(i);
}
for (int i = 0; i < n; ++i) {
g[to[i]].push_back(i);
g[i].push_back(to[i]);
}
dfs(n, n);
for (int i = 0; i < (N << 2); ++i) tree[i] = -INF;
for (int i = 0; i < k; ++i) {
addi(i);
}
cout << tree[0] << ' ';
for (int i = k; i < n; ++i) {
addi(i);
deli(i - k);
cout << tree[0] << ' ';
}
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;
const int N = 1e6 + 7;
const int INF = 1e9 + 7;
int n, k;
int a[N];
int to[N];
vector<int> g[N];
int ptr = 0;
int e[N];
int l[N], r[N];
void dfs(int u, int p) {
l[u] = ptr;
e[ptr++] = u;
for (int v : g[u]) {
if (v != p) {
dfs(v, u);
}
}
r[u] = ptr;
}
int tree[N << 2], add[N << 2];
void push(int v) {
if (add[v]) {
add[v * 2 + 1] += add[v];
tree[v * 2 + 1] += add[v];
add[v * 2 + 2] += add[v];
tree[v * 2 + 2] += add[v];
add[v] = 0;
return;
}
}
void upd(int v, int tl, int tr, int p, int x) {
if (tl == tr) {
tree[v] = x;
return;
}
int tm = (tl + tr) >> 1;
push(v);
if (p <= tm)
upd(v * 2 + 1, tl, tm, p, x);
else
upd(v * 2 + 2, tm + 1, tr, p, x);
tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]);
}
void addt(int v, int tl, int tr, int l, int r) {
if (tr < l || r < tl) return;
if (l <= tl && tr <= r) {
tree[v]++;
add[v]++;
return;
}
int tm = (tl + tr) >> 1;
addt(v * 2 + 1, tl, tm, l, r);
addt(v * 2 + 2, tm + 1, tr, l, r);
tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]);
}
void addi(int i) {
upd(0, 0, n, l[i], 0);
addt(0, 0, n, l[i], r[i]);
}
void deli(int i) { upd(0, 0, n, l[i], -INF); }
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
a[n] = N - 1;
vector<int> v;
for (int i = 0; i <= n; ++i) {
while (v.size() && a[v.back()] < a[i]) {
to[v.back()] = i;
v.pop_back();
}
v.push_back(i);
}
for (int i = 0; i < n; ++i) {
g[to[i]].push_back(i);
g[i].push_back(to[i]);
}
dfs(n, n);
for (int i = 0; i < (N << 2); ++i) tree[i] = -INF;
for (int i = 0; i < k; ++i) {
addi(i);
}
cout << tree[0] << ' ';
for (int i = k; i < n; ++i) {
addi(i);
deli(i - k);
cout << tree[0] << ' ';
}
cout << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int w = 1, s = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') w = -1;
ch = getchar();
}
while (isdigit(ch)) {
s = s * 10 + ch - '0';
ch = getchar();
}
return w * s;
}
struct Segment_Tree {
int Max[4000010], tag[4000010];
inline void Init() {
memset(Max, 0, sizeof(Max));
memset(tag, 0, sizeof(tag));
}
inline void Push(int x, int d) {
Max[x] += d;
tag[x] += d;
return;
}
inline void Pushdown(int now) {
if (!tag[now]) return;
Push(now << 1, tag[now]);
Push(now << 1 | 1, tag[now]);
tag[now] = 0;
}
inline void Modify(int now, int l, int r, int x, int y, int d) {
if (x <= l && r <= y) {
Push(now, d);
return;
}
int mid = (l + r) >> 1;
Pushdown(now);
if (x <= mid) Modify(now << 1, l, mid, x, y, d);
if (y > mid) Modify(now << 1 | 1, mid + 1, r, x, y, d);
Max[now] = max(Max[now << 1], Max[now << 1 | 1]);
}
inline int Query(int now, int l, int r, int x, int y) {
if (x <= l && r <= y) return Max[now];
int mid = (l + r) >> 1;
Pushdown(now);
int res = 0;
if (x <= mid) res = Query(now << 1, l, mid, x, y);
if (y > mid) res = max(res, Query(now << 1 | 1, mid + 1, r, x, y));
Max[now] = max(Max[now << 1], Max[now << 1 | 1]);
return res;
}
} t;
int n, k, A[1000010], L[1010010], Sta[1000100];
int main() {
n = read(), k = read();
for (register int i = 1; i <= n; ++i) A[i] = read();
int top = 0;
t.Init();
for (register int i = 1; i <= n; ++i) {
while (top && A[i] > A[Sta[top]]) top--;
L[i] = Sta[top];
Sta[++top] = i;
}
for (register int i = 1; i <= n; ++i) {
t.Modify(1, 1, n, L[i] + 1, i, 1);
if (i >= k) cout << t.Query(1, 1, n, i - k + 1, i) << " ";
}
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;
inline int read() {
int w = 1, s = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') w = -1;
ch = getchar();
}
while (isdigit(ch)) {
s = s * 10 + ch - '0';
ch = getchar();
}
return w * s;
}
struct Segment_Tree {
int Max[4000010], tag[4000010];
inline void Init() {
memset(Max, 0, sizeof(Max));
memset(tag, 0, sizeof(tag));
}
inline void Push(int x, int d) {
Max[x] += d;
tag[x] += d;
return;
}
inline void Pushdown(int now) {
if (!tag[now]) return;
Push(now << 1, tag[now]);
Push(now << 1 | 1, tag[now]);
tag[now] = 0;
}
inline void Modify(int now, int l, int r, int x, int y, int d) {
if (x <= l && r <= y) {
Push(now, d);
return;
}
int mid = (l + r) >> 1;
Pushdown(now);
if (x <= mid) Modify(now << 1, l, mid, x, y, d);
if (y > mid) Modify(now << 1 | 1, mid + 1, r, x, y, d);
Max[now] = max(Max[now << 1], Max[now << 1 | 1]);
}
inline int Query(int now, int l, int r, int x, int y) {
if (x <= l && r <= y) return Max[now];
int mid = (l + r) >> 1;
Pushdown(now);
int res = 0;
if (x <= mid) res = Query(now << 1, l, mid, x, y);
if (y > mid) res = max(res, Query(now << 1 | 1, mid + 1, r, x, y));
Max[now] = max(Max[now << 1], Max[now << 1 | 1]);
return res;
}
} t;
int n, k, A[1000010], L[1010010], Sta[1000100];
int main() {
n = read(), k = read();
for (register int i = 1; i <= n; ++i) A[i] = read();
int top = 0;
t.Init();
for (register int i = 1; i <= n; ++i) {
while (top && A[i] > A[Sta[top]]) top--;
L[i] = Sta[top];
Sta[++top] = i;
}
for (register int i = 1; i <= n; ++i) {
t.Modify(1, 1, n, L[i] + 1, i, 1);
if (i >= k) cout << t.Query(1, 1, n, i - k + 1, i) << " ";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int rd() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f * x;
}
struct node {
int mx, tag;
} tree[1000005 << 2];
int n, k, a[1000005], stk[1000005], tp;
vector<int> G[1000005];
int dfn[1000005], tim, siz[1000005];
void dfs(int u) {
dfn[u] = ++tim;
siz[u] = 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dfs(v);
siz[u] += siz[v];
}
}
void PushDown(int i) {
if (tree[i].tag) {
tree[i << 1].mx += tree[i].tag;
tree[i << 1].tag += tree[i].tag;
tree[i << 1 | 1].mx += tree[i].tag;
tree[i << 1 | 1].tag += tree[i].tag;
tree[i].tag = 0;
}
}
void PushUp(int i) { tree[i].mx = max(tree[i << 1].mx, tree[i << 1 | 1].mx); }
void Update(int i, int l, int r, int ql, int qr, int val) {
if (ql <= l && r <= qr) {
tree[i].mx += val;
tree[i].tag += val;
return;
}
PushDown(i);
int mid = (l + r) >> 1;
if (ql <= mid) Update(i << 1, l, mid, ql, qr, val);
if (qr > mid) Update(i << 1 | 1, mid + 1, r, ql, qr, val);
PushUp(i);
}
int main() {
n = rd(), k = rd();
for (int i = 1; i <= n; i++) a[i] = rd();
for (int i = 1; i <= n; i++) {
while (tp && a[stk[tp]] < a[i]) {
G[i].push_back(stk[tp]);
tp--;
}
stk[++tp] = i;
}
while (tp) {
G[n + 1].push_back(stk[tp]);
tp--;
}
dfs(n + 1);
for (int i = 1; i <= k; i++)
Update(1, 1, n + 1, dfn[i], dfn[i] + siz[i] - 1, 1);
printf("%d ", tree[1].mx);
for (int i = k + 1; i <= n; i++) {
Update(1, 1, n + 1, dfn[i], dfn[i] + siz[i] - 1, 1);
Update(1, 1, n + 1, dfn[i - k], dfn[i - k] + siz[i - k] - 1, -1);
printf("%d ", tree[1].mx);
}
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 rd() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f * x;
}
struct node {
int mx, tag;
} tree[1000005 << 2];
int n, k, a[1000005], stk[1000005], tp;
vector<int> G[1000005];
int dfn[1000005], tim, siz[1000005];
void dfs(int u) {
dfn[u] = ++tim;
siz[u] = 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dfs(v);
siz[u] += siz[v];
}
}
void PushDown(int i) {
if (tree[i].tag) {
tree[i << 1].mx += tree[i].tag;
tree[i << 1].tag += tree[i].tag;
tree[i << 1 | 1].mx += tree[i].tag;
tree[i << 1 | 1].tag += tree[i].tag;
tree[i].tag = 0;
}
}
void PushUp(int i) { tree[i].mx = max(tree[i << 1].mx, tree[i << 1 | 1].mx); }
void Update(int i, int l, int r, int ql, int qr, int val) {
if (ql <= l && r <= qr) {
tree[i].mx += val;
tree[i].tag += val;
return;
}
PushDown(i);
int mid = (l + r) >> 1;
if (ql <= mid) Update(i << 1, l, mid, ql, qr, val);
if (qr > mid) Update(i << 1 | 1, mid + 1, r, ql, qr, val);
PushUp(i);
}
int main() {
n = rd(), k = rd();
for (int i = 1; i <= n; i++) a[i] = rd();
for (int i = 1; i <= n; i++) {
while (tp && a[stk[tp]] < a[i]) {
G[i].push_back(stk[tp]);
tp--;
}
stk[++tp] = i;
}
while (tp) {
G[n + 1].push_back(stk[tp]);
tp--;
}
dfs(n + 1);
for (int i = 1; i <= k; i++)
Update(1, 1, n + 1, dfn[i], dfn[i] + siz[i] - 1, 1);
printf("%d ", tree[1].mx);
for (int i = k + 1; i <= n; i++) {
Update(1, 1, n + 1, dfn[i], dfn[i] + siz[i] - 1, 1);
Update(1, 1, n + 1, dfn[i - k], dfn[i - k] + siz[i - k] - 1, -1);
printf("%d ", tree[1].mx);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int N, K;
int vals[1123456];
vector<int> adj[1123456];
int timestamp;
int Tin[1123456], Tout[1123456];
int stree[4 * 1123456];
int lazy_tree[4 * 1123456];
void ETT(int u) {
Tin[u] = timestamp;
for (int v : adj[u]) ETT(v);
Tout[u] = timestamp++;
}
void generate_tree() {
stack<pair<int, int> > gt_stack;
for (int i = N; i >= 1; i--) {
while (!gt_stack.empty() && gt_stack.top().first <= vals[i]) {
gt_stack.pop();
}
if (gt_stack.empty()) {
adj[0].push_back(i);
} else {
adj[gt_stack.top().second].push_back(i);
}
gt_stack.push({vals[i], i});
}
ETT(0);
;
}
void lazy_update(int node, int l, int r) {
stree[node] += lazy_tree[node];
if (l != r) {
lazy_tree[node + node + 1] += lazy_tree[node];
lazy_tree[node + node + 2] += lazy_tree[node];
}
lazy_tree[node] = 0;
}
void update(int node, int l, int r, int tl, int tr, int offset) {
lazy_update(node, l, r);
if (l > tr || r < tl) {
return;
}
if (l >= tl && r <= tr) {
lazy_tree[node] += offset;
lazy_update(node, l, r);
return;
}
int mid = (l + r) / 2;
update(node + node + 1, l, mid, tl, tr, offset);
update(node + node + 2, mid + 1, r, tl, tr, offset);
stree[node] = max(stree[node + node + 1], stree[node + node + 2]);
}
int query(int node, int l, int r, int tl, int tr) {
lazy_update(node, l, r);
if (l > tr || r < tl) {
return 0;
}
if (l >= tl && r <= tr) {
return stree[node];
}
int mid = (l + r) / 2;
return query(node + node + 1, l, mid, tl, tr) +
query(node + node + 2, mid + 1, r, tl, tr);
}
void enable(int u) { update(0, 0, N, Tin[u], Tout[u], 1); }
void disable(int u) { update(0, 0, N, Tin[u], Tout[u], -1); }
void print_seg() {
for (int i = 0; i < N; i++) {
printf("%d ", query(0, 0, N, i, i));
}
printf("\n");
}
int main() {
scanf("%d%d", &N, &K);
for (int i = 1; i <= N; i++) {
scanf("%d", &vals[i]);
}
generate_tree();
for (int i = 1; i <= N; i++) {
enable(i);
if (i >= K) {
printf("%d\n", query(0, 0, N - 1, 0, N - 1));
disable(i - K + 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, K;
int vals[1123456];
vector<int> adj[1123456];
int timestamp;
int Tin[1123456], Tout[1123456];
int stree[4 * 1123456];
int lazy_tree[4 * 1123456];
void ETT(int u) {
Tin[u] = timestamp;
for (int v : adj[u]) ETT(v);
Tout[u] = timestamp++;
}
void generate_tree() {
stack<pair<int, int> > gt_stack;
for (int i = N; i >= 1; i--) {
while (!gt_stack.empty() && gt_stack.top().first <= vals[i]) {
gt_stack.pop();
}
if (gt_stack.empty()) {
adj[0].push_back(i);
} else {
adj[gt_stack.top().second].push_back(i);
}
gt_stack.push({vals[i], i});
}
ETT(0);
;
}
void lazy_update(int node, int l, int r) {
stree[node] += lazy_tree[node];
if (l != r) {
lazy_tree[node + node + 1] += lazy_tree[node];
lazy_tree[node + node + 2] += lazy_tree[node];
}
lazy_tree[node] = 0;
}
void update(int node, int l, int r, int tl, int tr, int offset) {
lazy_update(node, l, r);
if (l > tr || r < tl) {
return;
}
if (l >= tl && r <= tr) {
lazy_tree[node] += offset;
lazy_update(node, l, r);
return;
}
int mid = (l + r) / 2;
update(node + node + 1, l, mid, tl, tr, offset);
update(node + node + 2, mid + 1, r, tl, tr, offset);
stree[node] = max(stree[node + node + 1], stree[node + node + 2]);
}
int query(int node, int l, int r, int tl, int tr) {
lazy_update(node, l, r);
if (l > tr || r < tl) {
return 0;
}
if (l >= tl && r <= tr) {
return stree[node];
}
int mid = (l + r) / 2;
return query(node + node + 1, l, mid, tl, tr) +
query(node + node + 2, mid + 1, r, tl, tr);
}
void enable(int u) { update(0, 0, N, Tin[u], Tout[u], 1); }
void disable(int u) { update(0, 0, N, Tin[u], Tout[u], -1); }
void print_seg() {
for (int i = 0; i < N; i++) {
printf("%d ", query(0, 0, N, i, i));
}
printf("\n");
}
int main() {
scanf("%d%d", &N, &K);
for (int i = 1; i <= N; i++) {
scanf("%d", &vals[i]);
}
generate_tree();
for (int i = 1; i <= N; i++) {
enable(i);
if (i >= K) {
printf("%d\n", query(0, 0, N - 1, 0, N - 1));
disable(i - K + 1);
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
const int M = N << 1;
int n, K, a[N];
int st[N], L;
int f[N];
int cnt, head[N], Next[M], v[M];
int pre[N], dfn[N], clo = -1;
int t[N << 2], lazy[N << 2];
void read(int &x) {
char ch = getchar();
x = 0;
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 3) + (x << 1) + ch - '0';
}
void add(int x, int y) {
Next[++cnt] = head[x];
head[x] = cnt;
v[cnt] = y;
}
void dfs(int x, int fa) {
pre[x] = ++clo;
for (int i = head[x]; i; i = Next[i])
if (v[i] != fa) dfs(v[i], x);
dfn[x] = clo;
}
void push(int node) {
int &v = lazy[node];
lazy[(node << 1)] += v;
t[(node << 1)] += v;
lazy[(node << 1 | 1)] += v;
t[(node << 1 | 1)] += v;
v = 0;
}
void ADD(int node, int L, int r, int x, int y, int v) {
if (L >= x && r <= y) {
t[node] += v;
lazy[node] += v;
return;
}
int mid = (L + r) >> 1;
push(node);
if (x <= mid) ADD((node << 1), L, mid, x, y, v);
if (y > mid) ADD((node << 1 | 1), mid + 1, r, x, y, v);
t[node] = max(t[(node << 1)], t[(node << 1 | 1)]);
}
int QRY(int node, int L, int r, int x, int y) { return t[1]; }
int main() {
read(n);
read(K);
for (int i = (1); i <= (n); i++) read(a[i]);
a[0] = 1e9;
for (int i = (n); i >= (1); i--) {
while (a[st[L]] <= a[i]) L--;
f[i] = st[L];
st[++L] = i;
}
for (int i = (1); i <= (n); i++) add(f[i], i);
dfs(0, 0);
for (int i = (1); i <= (K); i++) ADD(1, 1, n, pre[i], dfn[i], 1);
printf("%d ", QRY(1, 1, n, 1, n));
for (int i = (K + 1); i <= (n); i++) {
ADD(1, 1, n, pre[i - K], dfn[i - K], -1);
ADD(1, 1, n, pre[i], dfn[i], 1);
printf("%d ", QRY(1, 1, n, 1, n));
}
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;
const int N = 1000005;
const int M = N << 1;
int n, K, a[N];
int st[N], L;
int f[N];
int cnt, head[N], Next[M], v[M];
int pre[N], dfn[N], clo = -1;
int t[N << 2], lazy[N << 2];
void read(int &x) {
char ch = getchar();
x = 0;
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 3) + (x << 1) + ch - '0';
}
void add(int x, int y) {
Next[++cnt] = head[x];
head[x] = cnt;
v[cnt] = y;
}
void dfs(int x, int fa) {
pre[x] = ++clo;
for (int i = head[x]; i; i = Next[i])
if (v[i] != fa) dfs(v[i], x);
dfn[x] = clo;
}
void push(int node) {
int &v = lazy[node];
lazy[(node << 1)] += v;
t[(node << 1)] += v;
lazy[(node << 1 | 1)] += v;
t[(node << 1 | 1)] += v;
v = 0;
}
void ADD(int node, int L, int r, int x, int y, int v) {
if (L >= x && r <= y) {
t[node] += v;
lazy[node] += v;
return;
}
int mid = (L + r) >> 1;
push(node);
if (x <= mid) ADD((node << 1), L, mid, x, y, v);
if (y > mid) ADD((node << 1 | 1), mid + 1, r, x, y, v);
t[node] = max(t[(node << 1)], t[(node << 1 | 1)]);
}
int QRY(int node, int L, int r, int x, int y) { return t[1]; }
int main() {
read(n);
read(K);
for (int i = (1); i <= (n); i++) read(a[i]);
a[0] = 1e9;
for (int i = (n); i >= (1); i--) {
while (a[st[L]] <= a[i]) L--;
f[i] = st[L];
st[++L] = i;
}
for (int i = (1); i <= (n); i++) add(f[i], i);
dfs(0, 0);
for (int i = (1); i <= (K); i++) ADD(1, 1, n, pre[i], dfn[i], 1);
printf("%d ", QRY(1, 1, n, 1, n));
for (int i = (K + 1); i <= (n); i++) {
ADD(1, 1, n, pre[i - K], dfn[i - K], -1);
ADD(1, 1, n, pre[i], dfn[i], 1);
printf("%d ", QRY(1, 1, n, 1, n));
}
printf("\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline long long read() {
long long x = 0;
char ch = getchar();
bool positive = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') positive = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(long long a) {
if (a < 0) {
a = -a;
putchar('-');
}
if (a >= 10) write(a / 10);
putchar('0' + a % 10);
}
inline void writeln(long long a) {
write(a);
puts("");
}
inline void wri(long long a) {
write(a);
putchar(' ');
}
const int M = 1 << 21 | 2, N = 1000005, inf = 1e9;
int n, k, ti, in[N], out[N], a[N], tr[M], lazy[M];
vector<int> v[N];
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
q;
void push_up(int nod) {
tr[nod] = max(tr[nod << 1], tr[nod << 1 | 1]) + lazy[nod];
}
void cao(int nod, int de) {
tr[nod] += de;
lazy[nod] += de;
}
void insert(int l, int r, int i, int j, int de, int nod) {
if (l == i && r == j) {
cao(nod, de);
return;
}
int mid = (l + r) >> 1;
if (j <= mid)
insert(l, mid, i, j, de, nod << 1);
else if (i > mid)
insert(mid + 1, r, i, j, de, nod << 1 | 1);
else {
insert(l, mid, i, mid, de, nod << 1);
insert(mid + 1, r, mid + 1, j, de, nod << 1 | 1);
}
push_up(nod);
}
void dfs(int p) {
in[p] = ++ti;
for (auto i : v[p]) dfs(i);
out[p] = ti;
}
int main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
while (q.size() && q.top().first < a[i]) {
v[i].push_back(q.top().second);
q.pop();
}
q.push(make_pair(a[i], i));
}
for (int i = n; i; i--)
if (!in[i]) dfs(i);
for (int i = 1; i < k; i++) {
insert(1, n, in[i], out[i], 1, 1);
}
for (int i = k; i <= n; i++) {
insert(1, n, in[i], out[i], 1, 1);
wri(tr[1]);
insert(1, n, in[i - k + 1], in[i - k + 1], -inf, 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;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline long long read() {
long long x = 0;
char ch = getchar();
bool positive = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') positive = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(long long a) {
if (a < 0) {
a = -a;
putchar('-');
}
if (a >= 10) write(a / 10);
putchar('0' + a % 10);
}
inline void writeln(long long a) {
write(a);
puts("");
}
inline void wri(long long a) {
write(a);
putchar(' ');
}
const int M = 1 << 21 | 2, N = 1000005, inf = 1e9;
int n, k, ti, in[N], out[N], a[N], tr[M], lazy[M];
vector<int> v[N];
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
q;
void push_up(int nod) {
tr[nod] = max(tr[nod << 1], tr[nod << 1 | 1]) + lazy[nod];
}
void cao(int nod, int de) {
tr[nod] += de;
lazy[nod] += de;
}
void insert(int l, int r, int i, int j, int de, int nod) {
if (l == i && r == j) {
cao(nod, de);
return;
}
int mid = (l + r) >> 1;
if (j <= mid)
insert(l, mid, i, j, de, nod << 1);
else if (i > mid)
insert(mid + 1, r, i, j, de, nod << 1 | 1);
else {
insert(l, mid, i, mid, de, nod << 1);
insert(mid + 1, r, mid + 1, j, de, nod << 1 | 1);
}
push_up(nod);
}
void dfs(int p) {
in[p] = ++ti;
for (auto i : v[p]) dfs(i);
out[p] = ti;
}
int main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
while (q.size() && q.top().first < a[i]) {
v[i].push_back(q.top().second);
q.pop();
}
q.push(make_pair(a[i], i));
}
for (int i = n; i; i--)
if (!in[i]) dfs(i);
for (int i = 1; i < k; i++) {
insert(1, n, in[i], out[i], 1, 1);
}
for (int i = k; i <= n; i++) {
insert(1, n, in[i], out[i], 1, 1);
wri(tr[1]);
insert(1, n, in[i - k + 1], in[i - k + 1], -inf, 1);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
int a[maxn], n, p[8 * maxn], tree[8 * maxn], k, q[maxn], pre[maxn];
void down(int cha, int con) {
tree[con] += p[cha];
p[con] += p[cha];
}
void update(int node, int l, int r, int u, int v) {
down(node, 2 * node);
down(node, 2 * node + 1);
p[node] = 0;
if (u > r || v < l) return;
if (u <= l && v >= r) {
tree[node]++;
p[node] = 1;
return;
}
int mid = (l + r) >> 1;
update(node * 2, l, mid, u, v);
update(node * 2 + 1, mid + 1, r, u, v);
tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}
int get(int node, int l, int r, int u, int v) {
down(node, 2 * node);
down(node, 2 * node + 1);
p[node] = 0;
if (u > r || v < l) return 0;
if (u <= l && v >= r) return tree[node];
int mid = (l + r) >> 1;
int res1 = get(2 * node, l, mid, u, v);
int res2 = get(2 * node + 1, mid + 1, r, u, v);
return max(res1, res2);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
int top = 0;
for (int i = n; i >= 1; i--) {
while (top && a[q[top]] <= a[i]) pre[q[top]] = i, top--;
q[++top] = i;
}
for (int i = 1; i <= n; i++) {
update(1, 1, n, pre[i] + 1, i);
if (i >= k) cout << get(1, 1, n, i - k + 1, i) << " ";
}
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 + 100;
int a[maxn], n, p[8 * maxn], tree[8 * maxn], k, q[maxn], pre[maxn];
void down(int cha, int con) {
tree[con] += p[cha];
p[con] += p[cha];
}
void update(int node, int l, int r, int u, int v) {
down(node, 2 * node);
down(node, 2 * node + 1);
p[node] = 0;
if (u > r || v < l) return;
if (u <= l && v >= r) {
tree[node]++;
p[node] = 1;
return;
}
int mid = (l + r) >> 1;
update(node * 2, l, mid, u, v);
update(node * 2 + 1, mid + 1, r, u, v);
tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}
int get(int node, int l, int r, int u, int v) {
down(node, 2 * node);
down(node, 2 * node + 1);
p[node] = 0;
if (u > r || v < l) return 0;
if (u <= l && v >= r) return tree[node];
int mid = (l + r) >> 1;
int res1 = get(2 * node, l, mid, u, v);
int res2 = get(2 * node + 1, mid + 1, r, u, v);
return max(res1, res2);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
int top = 0;
for (int i = n; i >= 1; i--) {
while (top && a[q[top]] <= a[i]) pre[q[top]] = i, top--;
q[++top] = i;
}
for (int i = 1; i <= n; i++) {
update(1, 1, n, pre[i] + 1, i);
if (i >= k) cout << get(1, 1, n, i - k + 1, i) << " ";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[1005000], now = 1, lazy[5000500], size[1005000], xu[1005000], cn, n, m,
maxn[5000050], head[1000500], nex[1000500], to[1005000], cnt,
p[1005000], zhan[1005000], o;
void add(int x, int y) {
nex[++cnt] = head[x];
head[x] = cnt;
to[cnt] = y;
}
void push(int x) {
lazy[x * 2] += lazy[x];
lazy[x * 2 + 1] += lazy[x];
maxn[x * 2] += lazy[x];
maxn[x * 2 + 1] += lazy[x];
lazy[x] = 0;
}
void addd(int l, int r, int rt, int ql, int qr, int w) {
if (ql <= l && qr >= r) {
maxn[rt] += w;
lazy[rt] += w;
return;
}
int mid = (l + r) / 2;
if (lazy[rt]) push(rt);
if (ql <= mid) addd(l, mid, rt * 2, ql, qr, w);
if (qr > mid) addd(mid + 1, r, rt * 2 + 1, ql, qr, w);
maxn[rt] = max(maxn[rt * 2], maxn[rt * 2 + 1]);
}
void dfs(int x) {
xu[x] = ++cn;
size[x] = 1;
for (int i = head[x]; i; i = nex[i]) {
dfs(to[i]);
size[x] += size[to[i]];
}
}
int get(int l, int r, int rt, int q) {
if (l == r) return maxn[rt];
int mid = (l + r) / 2;
if (q <= mid) return get(l, mid, rt * 2, q);
return get(mid + 1, r, rt * 2 + 1, q);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
while (o && zhan[o] < a[i]) {
add(i, p[o]);
o--;
}
zhan[++o] = a[i];
p[o] = i;
}
for (int i = 1; i <= o; i++) add(n + 1, p[i]);
dfs(n + 1);
for (int i = 1; i <= n; i++) {
addd(1, n + 1, 1, xu[i], xu[i] + size[i] - 1, 1);
while (i - now >= m) addd(1, n + 1, 1, xu[now], xu[now], -1000000), now++;
if (i >= m) printf("%d ", maxn[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[1005000], now = 1, lazy[5000500], size[1005000], xu[1005000], cn, n, m,
maxn[5000050], head[1000500], nex[1000500], to[1005000], cnt,
p[1005000], zhan[1005000], o;
void add(int x, int y) {
nex[++cnt] = head[x];
head[x] = cnt;
to[cnt] = y;
}
void push(int x) {
lazy[x * 2] += lazy[x];
lazy[x * 2 + 1] += lazy[x];
maxn[x * 2] += lazy[x];
maxn[x * 2 + 1] += lazy[x];
lazy[x] = 0;
}
void addd(int l, int r, int rt, int ql, int qr, int w) {
if (ql <= l && qr >= r) {
maxn[rt] += w;
lazy[rt] += w;
return;
}
int mid = (l + r) / 2;
if (lazy[rt]) push(rt);
if (ql <= mid) addd(l, mid, rt * 2, ql, qr, w);
if (qr > mid) addd(mid + 1, r, rt * 2 + 1, ql, qr, w);
maxn[rt] = max(maxn[rt * 2], maxn[rt * 2 + 1]);
}
void dfs(int x) {
xu[x] = ++cn;
size[x] = 1;
for (int i = head[x]; i; i = nex[i]) {
dfs(to[i]);
size[x] += size[to[i]];
}
}
int get(int l, int r, int rt, int q) {
if (l == r) return maxn[rt];
int mid = (l + r) / 2;
if (q <= mid) return get(l, mid, rt * 2, q);
return get(mid + 1, r, rt * 2 + 1, q);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
while (o && zhan[o] < a[i]) {
add(i, p[o]);
o--;
}
zhan[++o] = a[i];
p[o] = i;
}
for (int i = 1; i <= o; i++) add(n + 1, p[i]);
dfs(n + 1);
for (int i = 1; i <= n; i++) {
addd(1, n + 1, 1, xu[i], xu[i] + size[i] - 1, 1);
while (i - now >= m) addd(1, n + 1, 1, xu[now], xu[now], -1000000), now++;
if (i >= m) printf("%d ", maxn[1]);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 500;
const int OFF = (1 << 20);
const int INF = 0x3f3f3f3f;
int n, k, a[N], st[N], cur, dp[N], prop[2 * OFF], tour[2 * OFF], ans[N], C[N],
nxt[N], L[N], R[N];
vector<int> v[N];
void refresh(int i) {
if (!prop[i]) return;
tour[i] += prop[i];
if (i < OFF) {
prop[2 * i] += prop[i];
prop[2 * i + 1] += prop[i];
}
prop[i] = 0;
}
void update(int i, int a, int b, int lo, int hi, int x) {
refresh(i);
if (lo <= a && b <= hi) {
prop[i] += x;
refresh(i);
return;
}
if (a > hi || b < lo) return;
update(2 * i, a, (a + b) / 2, lo, hi, x);
update(2 * i + 1, (a + b) / 2 + 1, b, lo, hi, x);
tour[i] = max(tour[2 * i], tour[2 * i + 1]);
}
void dfs(int x) {
C[x] = cur++;
L[x] = C[x];
for (int y : v[x]) dfs(y);
R[x] = cur - 1;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", a + i);
cur = 1, st[0] = n;
a[n] = n + 1;
for (int i = n - 1; i >= 0; i--) {
while (cur > 0 && a[st[cur - 1]] <= a[i]) cur--;
dp[i] = 1;
nxt[i] = st[cur - 1];
v[st[cur - 1]].push_back(i);
dp[i] = dp[st[cur - 1]] + 1;
st[cur++] = i;
}
cur = 0;
dfs(n);
for (int i = 0; i < n; i++) update(1, 0, OFF - 1, C[i], C[i], -INF + dp[i]);
for (int i = n - 1; i >= 0; i--) {
update(1, 0, OFF - 1, C[i], C[i], INF);
if (i + k < n) {
update(1, 0, OFF - 1, L[i + k], R[i + k], -1);
update(1, 0, OFF - 1, C[i + k], C[i + k], -INF);
}
if (i + k <= n) {
refresh(1);
ans[i] = tour[1];
}
}
for (int i = 0; i + k <= n; i++) {
printf("%d ", ans[i]);
}
printf("\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 N = 1e6 + 500;
const int OFF = (1 << 20);
const int INF = 0x3f3f3f3f;
int n, k, a[N], st[N], cur, dp[N], prop[2 * OFF], tour[2 * OFF], ans[N], C[N],
nxt[N], L[N], R[N];
vector<int> v[N];
void refresh(int i) {
if (!prop[i]) return;
tour[i] += prop[i];
if (i < OFF) {
prop[2 * i] += prop[i];
prop[2 * i + 1] += prop[i];
}
prop[i] = 0;
}
void update(int i, int a, int b, int lo, int hi, int x) {
refresh(i);
if (lo <= a && b <= hi) {
prop[i] += x;
refresh(i);
return;
}
if (a > hi || b < lo) return;
update(2 * i, a, (a + b) / 2, lo, hi, x);
update(2 * i + 1, (a + b) / 2 + 1, b, lo, hi, x);
tour[i] = max(tour[2 * i], tour[2 * i + 1]);
}
void dfs(int x) {
C[x] = cur++;
L[x] = C[x];
for (int y : v[x]) dfs(y);
R[x] = cur - 1;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", a + i);
cur = 1, st[0] = n;
a[n] = n + 1;
for (int i = n - 1; i >= 0; i--) {
while (cur > 0 && a[st[cur - 1]] <= a[i]) cur--;
dp[i] = 1;
nxt[i] = st[cur - 1];
v[st[cur - 1]].push_back(i);
dp[i] = dp[st[cur - 1]] + 1;
st[cur++] = i;
}
cur = 0;
dfs(n);
for (int i = 0; i < n; i++) update(1, 0, OFF - 1, C[i], C[i], -INF + dp[i]);
for (int i = n - 1; i >= 0; i--) {
update(1, 0, OFF - 1, C[i], C[i], INF);
if (i + k < n) {
update(1, 0, OFF - 1, L[i + k], R[i + k], -1);
update(1, 0, OFF - 1, C[i + k], C[i + k], -INF);
}
if (i + k <= n) {
refresh(1);
ans[i] = tour[1];
}
}
for (int i = 0; i + k <= n; i++) {
printf("%d ", ans[i]);
}
printf("\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct node {
long long nxt, to;
} o[1000010];
long long n, k, a[1000010], sta[1000010], top, head[1000010], I, s[1000010],
sz[1000010], t[1000010 << 2], lz[1000010 << 2];
void read(long long& x) {
x = 0;
char s = getchar();
while (s < '0' || s > '9') s = getchar();
while (s >= '0' && s <= '9') x = (x << 3) + (x << 1) + s - 48, s = getchar();
}
void add(long long u, long long v) {
o[++I] = (node){head[u], v};
head[u] = I;
}
void bfs() {
stack<long long> q;
q.push(n);
while (!q.empty()) {
long long now = q.top();
s[now] = ++s[0], q.pop();
for (long long i = head[now]; i; i = o[i].nxt) q.push(o[i].to);
}
}
long long max(long long a, long long b) { return a > b ? a : b; }
long long ls(long long id) { return id << 1; }
long long rs(long long id) { return id << 1 | 1; }
void up(long long id) { t[id] = max(t[ls(id)], t[rs(id)]); }
void down(long long id) {
long long l = ls(id), r = rs(id);
lz[l] += lz[id], lz[r] += lz[id];
t[l] += lz[id], t[r] += lz[id];
lz[id] = 0;
}
void pls(long long l, long long r, long long id, long long L, long long R,
long long w) {
if (L <= l && r <= R) {
lz[id] += w, t[id] += w;
return;
}
down(id);
long long mid = l + r >> 1;
if (L <= mid) pls(l, mid, ls(id), L, R, w);
if (R > mid) pls(mid + 1, r, rs(id), L, R, w);
up(id);
}
signed main() {
read(n), read(k);
for (long long i = 1; i <= n + 1; i++) sz[i] = 1;
for (long long i = 1; i <= n; i++) {
read(a[i]);
while (a[i] > a[sta[top]] && top) sz[i] += sz[sta[top]], add(i, sta[top--]);
sta[++top] = i;
}
while (top) sz[n + 1] += sz[sta[top]], add(n + 1, sta[top--]);
n++, bfs();
for (long long i = 1; i <= k; i++) pls(1, n, 1, s[i], s[i] + sz[i] - 1, 1);
printf("%I64d ", t[1]);
for (long long i = k + 1; i < n; i++) {
pls(1, n, 1, s[i - k], s[i - k] + sz[i - k] - 1, -1);
pls(1, n, 1, s[i], s[i] + sz[i] - 1, 1);
printf("%I64d ", 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;
struct node {
long long nxt, to;
} o[1000010];
long long n, k, a[1000010], sta[1000010], top, head[1000010], I, s[1000010],
sz[1000010], t[1000010 << 2], lz[1000010 << 2];
void read(long long& x) {
x = 0;
char s = getchar();
while (s < '0' || s > '9') s = getchar();
while (s >= '0' && s <= '9') x = (x << 3) + (x << 1) + s - 48, s = getchar();
}
void add(long long u, long long v) {
o[++I] = (node){head[u], v};
head[u] = I;
}
void bfs() {
stack<long long> q;
q.push(n);
while (!q.empty()) {
long long now = q.top();
s[now] = ++s[0], q.pop();
for (long long i = head[now]; i; i = o[i].nxt) q.push(o[i].to);
}
}
long long max(long long a, long long b) { return a > b ? a : b; }
long long ls(long long id) { return id << 1; }
long long rs(long long id) { return id << 1 | 1; }
void up(long long id) { t[id] = max(t[ls(id)], t[rs(id)]); }
void down(long long id) {
long long l = ls(id), r = rs(id);
lz[l] += lz[id], lz[r] += lz[id];
t[l] += lz[id], t[r] += lz[id];
lz[id] = 0;
}
void pls(long long l, long long r, long long id, long long L, long long R,
long long w) {
if (L <= l && r <= R) {
lz[id] += w, t[id] += w;
return;
}
down(id);
long long mid = l + r >> 1;
if (L <= mid) pls(l, mid, ls(id), L, R, w);
if (R > mid) pls(mid + 1, r, rs(id), L, R, w);
up(id);
}
signed main() {
read(n), read(k);
for (long long i = 1; i <= n + 1; i++) sz[i] = 1;
for (long long i = 1; i <= n; i++) {
read(a[i]);
while (a[i] > a[sta[top]] && top) sz[i] += sz[sta[top]], add(i, sta[top--]);
sta[++top] = i;
}
while (top) sz[n + 1] += sz[sta[top]], add(n + 1, sta[top--]);
n++, bfs();
for (long long i = 1; i <= k; i++) pls(1, n, 1, s[i], s[i] + sz[i] - 1, 1);
printf("%I64d ", t[1]);
for (long long i = k + 1; i < n; i++) {
pls(1, n, 1, s[i - k], s[i - k] + sz[i - k] - 1, -1);
pls(1, n, 1, s[i], s[i] + sz[i] - 1, 1);
printf("%I64d ", t[1]);
}
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 {
long long 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, long long 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)]);
}
long long 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
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 = 200000000000000000LL;
const long double EPS = 1e-9;
const int inf = 0x3f3f3f3f;
struct SegmentTree {
long long 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, long long 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)]);
}
long long 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 N = 1e6 + 5;
int n, k, a[N], l[N];
long long seg[N << 2], lazy[N << 2];
stack<int> s;
void shift(int id) {
if (lazy[id]) {
int lc = id * 2, rc = lc | 1;
seg[lc] += lazy[id];
seg[rc] += lazy[id];
lazy[lc] += lazy[id];
lazy[rc] += lazy[id];
lazy[id] = 0;
}
}
void add(int l, int r, int val, int id = 1, int b = 0, int e = n) {
if (l >= e || b >= r) return;
if (l <= b && e <= r) {
seg[id] += val;
lazy[id] += val;
return;
}
shift(id);
int mid = (b + e) >> 1, lc = id * 2, rc = lc | 1;
add(l, r, val, lc, b, mid);
add(l, r, val, rc, mid, e);
seg[id] = max(seg[lc], seg[rc]);
}
long long get(int l, int r, int id = 1, int b = 0, int e = n) {
if (l >= e || b >= r) return 0;
if (l <= b && e <= r) return seg[id];
shift(id);
int mid = (b + e) >> 1, lc = id * 2, rc = lc | 1;
return max(get(l, r, lc, b, mid), get(l, r, rc, mid, e));
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
l[i] = -1;
}
for (int i = 0; i < n; i++) {
while (!s.empty() && a[s.top()] < a[i]) s.pop();
if (s.size()) l[i] = s.top();
s.push(i);
}
for (int i = 0; i < k - 1; i++) add(l[i] + 1, i + 1, 1);
for (int i = k - 1; i < n; i++) {
add(l[i] + 1, i + 1, 1);
cout << get(max(0, 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;
const int N = 1e6 + 5;
int n, k, a[N], l[N];
long long seg[N << 2], lazy[N << 2];
stack<int> s;
void shift(int id) {
if (lazy[id]) {
int lc = id * 2, rc = lc | 1;
seg[lc] += lazy[id];
seg[rc] += lazy[id];
lazy[lc] += lazy[id];
lazy[rc] += lazy[id];
lazy[id] = 0;
}
}
void add(int l, int r, int val, int id = 1, int b = 0, int e = n) {
if (l >= e || b >= r) return;
if (l <= b && e <= r) {
seg[id] += val;
lazy[id] += val;
return;
}
shift(id);
int mid = (b + e) >> 1, lc = id * 2, rc = lc | 1;
add(l, r, val, lc, b, mid);
add(l, r, val, rc, mid, e);
seg[id] = max(seg[lc], seg[rc]);
}
long long get(int l, int r, int id = 1, int b = 0, int e = n) {
if (l >= e || b >= r) return 0;
if (l <= b && e <= r) return seg[id];
shift(id);
int mid = (b + e) >> 1, lc = id * 2, rc = lc | 1;
return max(get(l, r, lc, b, mid), get(l, r, rc, mid, e));
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
l[i] = -1;
}
for (int i = 0; i < n; i++) {
while (!s.empty() && a[s.top()] < a[i]) s.pop();
if (s.size()) l[i] = s.top();
s.push(i);
}
for (int i = 0; i < k - 1; i++) add(l[i] + 1, i + 1, 1);
for (int i = k - 1; i < n; i++) {
add(l[i] + 1, i + 1, 1);
cout << get(max(0, i - k + 1), i + 1) << " ";
}
cout << "\n";
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k, c, a[1000010], d[1000010], n1[1000010], n2[1000010], f1[1000010],
f2[1000010], t[1000010];
bool v[1000010];
int main() {
scanf("%d%d", &n, &k);
if (k == 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 >= k) {
printf("%d ", a[t[c]] - a[c] + 1);
if (!v[i - k + 1]) {
v[i - k + 1] = 1;
t[c] = n1[i - k + 1];
f1[n1[i - k + 1]] = 0;
if (n2[c] && a[t[c]] - a[c] <= a[t[n2[c]]] - a[n2[c]]) {
for (int j = i - k + 1; j; j = n1[j]) v[j] = 1;
f2[n2[c]] = 0;
c = n2[c];
}
}
}
}
}
| ### 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, k, c, a[1000010], d[1000010], n1[1000010], n2[1000010], f1[1000010],
f2[1000010], t[1000010];
bool v[1000010];
int main() {
scanf("%d%d", &n, &k);
if (k == 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 >= k) {
printf("%d ", a[t[c]] - a[c] + 1);
if (!v[i - k + 1]) {
v[i - k + 1] = 1;
t[c] = n1[i - k + 1];
f1[n1[i - k + 1]] = 0;
if (n2[c] && a[t[c]] - a[c] <= a[t[n2[c]]] - a[n2[c]]) {
for (int j = i - k + 1; j; j = n1[j]) v[j] = 1;
f2[n2[c]] = 0;
c = n2[c];
}
}
}
}
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long double eps = 1e-7;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 1000010, LOG = 20;
int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN], R[MAXN];
int stt[MAXN], fnt[MAXN], timer;
int seg[MAXN << 2], lazy[MAXN << 2];
vector<int> G[MAXN];
void dfs(int node) {
stt[node] = timer++;
for (int v : G[node]) dfs(v);
fnt[node] = timer;
}
void add_lazy(int id, int val) {
seg[id] += val;
lazy[id] += val;
}
void shift(int id) {
if (!lazy[id]) return;
add_lazy(id << 1, lazy[id]);
add_lazy(id << 1 | 1, lazy[id]);
lazy[id] = 0;
}
void Add(int id, int tl, int tr, int l, int r, int val) {
if (r <= tl || tr <= l) return;
if (l <= tl && tr <= r) {
add_lazy(id, val);
return;
}
shift(id);
int mid = (tl + tr) >> 1;
Add(id << 1, tl, mid, l, r, val);
Add(id << 1 | 1, mid, tr, l, r, val);
seg[id] = max(seg[id << 1], seg[id << 1 | 1]);
}
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];
for (int i = n; i; i--) {
for (R[i] = i + 1; R[i] <= n && A[R[i]] <= A[i]; R[i] = R[R[i]])
;
G[R[i]].push_back(i);
}
dfs(n + 1);
for (int i = 1; i <= n; i++) {
Add(1, 1, n + 1, stt[i], fnt[i], +1);
if (k <= i) {
Add(1, 1, n + 1, stt[i - k], stt[i - k] + 1, -inf);
cout << seg[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 GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long double eps = 1e-7;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 1000010, LOG = 20;
int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN], R[MAXN];
int stt[MAXN], fnt[MAXN], timer;
int seg[MAXN << 2], lazy[MAXN << 2];
vector<int> G[MAXN];
void dfs(int node) {
stt[node] = timer++;
for (int v : G[node]) dfs(v);
fnt[node] = timer;
}
void add_lazy(int id, int val) {
seg[id] += val;
lazy[id] += val;
}
void shift(int id) {
if (!lazy[id]) return;
add_lazy(id << 1, lazy[id]);
add_lazy(id << 1 | 1, lazy[id]);
lazy[id] = 0;
}
void Add(int id, int tl, int tr, int l, int r, int val) {
if (r <= tl || tr <= l) return;
if (l <= tl && tr <= r) {
add_lazy(id, val);
return;
}
shift(id);
int mid = (tl + tr) >> 1;
Add(id << 1, tl, mid, l, r, val);
Add(id << 1 | 1, mid, tr, l, r, val);
seg[id] = max(seg[id << 1], seg[id << 1 | 1]);
}
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];
for (int i = n; i; i--) {
for (R[i] = i + 1; R[i] <= n && A[R[i]] <= A[i]; R[i] = R[R[i]])
;
G[R[i]].push_back(i);
}
dfs(n + 1);
for (int i = 1; i <= n; i++) {
Add(1, 1, n + 1, stt[i], fnt[i], +1);
if (k <= i) {
Add(1, 1, n + 1, stt[i - k], stt[i - k] + 1, -inf);
cout << seg[1] << ' ';
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1123456;
struct Seg {
int l, r, val, lzy;
} T[maxn << 2];
void build(int o, int l, int r) {
T[o].l = l, T[o].r = r;
if (l != r) {
int m = l + r >> 1;
build(o << 1, l, m);
build(o << 1 | 1, m + 1, r);
}
}
inline void push_down(int o) {
T[o << 1].lzy += T[o].lzy;
T[o << 1 | 1].lzy += T[o].lzy;
T[o].lzy = 0;
}
inline void push_up(int o) {
T[o].val =
max(T[o << 1].val + T[o << 1].lzy, T[o << 1 | 1].val + T[o << 1 | 1].lzy);
}
void add(int o, int l, int r) {
if (l <= T[o].l && T[o].r <= r) {
T[o].lzy++;
return;
}
push_down(o);
int m = T[o].l + T[o].r >> 1;
if (l <= m) add(o << 1, l, r);
if (r > m) add(o << 1 | 1, l, r);
push_up(o);
}
int query(int o, int l, int r) {
if (l <= T[o].l && T[o].r <= r) return T[o].val + T[o].lzy;
push_down(o);
int m = T[o].l + T[o].r >> 1, ret = 0;
if (l <= m) ret = max(ret, query(o << 1, l, r));
if (r > m) ret = max(ret, query(o << 1 | 1, l, r));
push_up(o);
return ret;
}
deque<pair<int, int> > q;
void add(int pos, int a) {
int cnt = 1;
while (!q.empty()) {
if (q.back().first < a) {
cnt += q.back().second;
q.pop_back();
} else if (q.back().first == a) {
add(1, pos - cnt + 1, pos);
q.back().second += cnt;
return;
} else
break;
}
q.emplace_back(a, cnt);
add(1, pos - cnt + 1, pos);
}
void del() {
if (q.front().second == 1)
q.pop_front();
else
q.front().second--;
}
int k, n;
int a[maxn];
int main() {
scanf("%d%d", &n, &k);
for (int(i) = (1); (i) <= (n); (i)++) scanf("%d", a + i);
build(1, 1, 1 << 20);
for (int(i) = (1); (i) <= (k); (i)++) add(i, a[i]);
printf("%d", query(1, 1, k));
for (int(i) = (k + 1); (i) <= (n); (i)++) {
del();
add(i, a[i]);
printf(" %d", query(1, i - k + 1, i));
}
}
| ### 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 = 1123456;
struct Seg {
int l, r, val, lzy;
} T[maxn << 2];
void build(int o, int l, int r) {
T[o].l = l, T[o].r = r;
if (l != r) {
int m = l + r >> 1;
build(o << 1, l, m);
build(o << 1 | 1, m + 1, r);
}
}
inline void push_down(int o) {
T[o << 1].lzy += T[o].lzy;
T[o << 1 | 1].lzy += T[o].lzy;
T[o].lzy = 0;
}
inline void push_up(int o) {
T[o].val =
max(T[o << 1].val + T[o << 1].lzy, T[o << 1 | 1].val + T[o << 1 | 1].lzy);
}
void add(int o, int l, int r) {
if (l <= T[o].l && T[o].r <= r) {
T[o].lzy++;
return;
}
push_down(o);
int m = T[o].l + T[o].r >> 1;
if (l <= m) add(o << 1, l, r);
if (r > m) add(o << 1 | 1, l, r);
push_up(o);
}
int query(int o, int l, int r) {
if (l <= T[o].l && T[o].r <= r) return T[o].val + T[o].lzy;
push_down(o);
int m = T[o].l + T[o].r >> 1, ret = 0;
if (l <= m) ret = max(ret, query(o << 1, l, r));
if (r > m) ret = max(ret, query(o << 1 | 1, l, r));
push_up(o);
return ret;
}
deque<pair<int, int> > q;
void add(int pos, int a) {
int cnt = 1;
while (!q.empty()) {
if (q.back().first < a) {
cnt += q.back().second;
q.pop_back();
} else if (q.back().first == a) {
add(1, pos - cnt + 1, pos);
q.back().second += cnt;
return;
} else
break;
}
q.emplace_back(a, cnt);
add(1, pos - cnt + 1, pos);
}
void del() {
if (q.front().second == 1)
q.pop_front();
else
q.front().second--;
}
int k, n;
int a[maxn];
int main() {
scanf("%d%d", &n, &k);
for (int(i) = (1); (i) <= (n); (i)++) scanf("%d", a + i);
build(1, 1, 1 << 20);
for (int(i) = (1); (i) <= (k); (i)++) add(i, a[i]);
printf("%d", query(1, 1, k));
for (int(i) = (k + 1); (i) <= (n); (i)++) {
del();
add(i, a[i]);
printf(" %d", query(1, i - k + 1, i));
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 4e6 + 7, M = 2e6;
const long long mod = 1e9 + 7;
inline int read() {
int ret = 0;
char ch = getchar();
bool f = 1;
for (; !isdigit(ch); ch = getchar()) f ^= !(ch ^ '-');
for (; isdigit(ch); ch = getchar()) ret = (ret << 1) + (ret << 3) + ch - 48;
return f ? ret : -ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long ksm(long long a, long long b, long long mod) {
int ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); }
int head[N], NEXT[M], ver[M], tot;
void link(int u, int v) {
ver[++tot] = v;
NEXT[tot] = head[u];
head[u] = tot;
}
int indeg[N], in[N], out[N], lazy[N], a[N];
int ant;
struct node {
int l, r, mid;
int val;
int maxn;
} tree[N];
void dfs(int x) {
in[x] = ++ant;
for (int i = head[x]; i; i = NEXT[i]) {
int y = ver[i];
dfs(y);
}
out[x] = ant;
}
void build(int x, int l, int r) {
tree[x].l = l;
tree[x].r = r;
lazy[x] = 0;
if (l == r) {
tree[x].maxn = 0;
return;
}
int mi = tree[x].mid = l + r >> 1;
build(x << 1, l, mi);
build(x << 1 | 1, mi + 1, r);
tree[x].maxn = max(tree[x << 1].maxn, tree[x << 1 | 1].maxn);
}
void down(int x) {
if (lazy[x] != 0) {
lazy[x << 1] += lazy[x];
lazy[x << 1 | 1] += lazy[x];
tree[x << 1].maxn += lazy[x];
tree[x << 1 | 1].maxn += lazy[x];
lazy[x] = 0;
}
}
void change(int x, int l, int r, int num) {
if (l > tree[x].r || r < tree[x].l) return;
if (l <= tree[x].l && r >= tree[x].r) {
tree[x].maxn += num;
lazy[x] += num;
return;
} else {
down(x);
if (l <= tree[x].mid) change(x << 1, l, r, num);
if (r > tree[x].mid) change(x << 1 | 1, l, r, num);
tree[x].maxn = max(tree[x << 1].maxn, tree[x << 1 | 1].maxn);
}
}
int ask(int x, int l, int r) { return tree[x].maxn; }
int main() {
int n, k;
int ant = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
stack<int> s;
for (int i = 1; i <= n; i++) {
if (s.empty())
s.push(i);
else {
while (!s.empty() && a[s.top()] < a[i]) {
link(i, s.top());
indeg[s.top()]++;
s.pop();
}
s.push(i);
}
}
for (int i = 1; i <= n; i++) {
if (indeg[i]) continue;
link(n + 1, i);
}
dfs(n + 1);
build(1, in[n + 1], out[n + 1]);
for (int i = 1; i <= n; i++) {
if (i > k) {
change(1, in[i - k], out[i - k], -1);
change(1, in[i], out[i], 1);
printf(" %d", ask(1, in[n + 1], out[n + 1]));
} else {
change(1, in[i], out[i], 1);
if (i == k) printf("%d", ask(1, in[n + 1], out[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 = 4e6 + 7, M = 2e6;
const long long mod = 1e9 + 7;
inline int read() {
int ret = 0;
char ch = getchar();
bool f = 1;
for (; !isdigit(ch); ch = getchar()) f ^= !(ch ^ '-');
for (; isdigit(ch); ch = getchar()) ret = (ret << 1) + (ret << 3) + ch - 48;
return f ? ret : -ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long ksm(long long a, long long b, long long mod) {
int ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); }
int head[N], NEXT[M], ver[M], tot;
void link(int u, int v) {
ver[++tot] = v;
NEXT[tot] = head[u];
head[u] = tot;
}
int indeg[N], in[N], out[N], lazy[N], a[N];
int ant;
struct node {
int l, r, mid;
int val;
int maxn;
} tree[N];
void dfs(int x) {
in[x] = ++ant;
for (int i = head[x]; i; i = NEXT[i]) {
int y = ver[i];
dfs(y);
}
out[x] = ant;
}
void build(int x, int l, int r) {
tree[x].l = l;
tree[x].r = r;
lazy[x] = 0;
if (l == r) {
tree[x].maxn = 0;
return;
}
int mi = tree[x].mid = l + r >> 1;
build(x << 1, l, mi);
build(x << 1 | 1, mi + 1, r);
tree[x].maxn = max(tree[x << 1].maxn, tree[x << 1 | 1].maxn);
}
void down(int x) {
if (lazy[x] != 0) {
lazy[x << 1] += lazy[x];
lazy[x << 1 | 1] += lazy[x];
tree[x << 1].maxn += lazy[x];
tree[x << 1 | 1].maxn += lazy[x];
lazy[x] = 0;
}
}
void change(int x, int l, int r, int num) {
if (l > tree[x].r || r < tree[x].l) return;
if (l <= tree[x].l && r >= tree[x].r) {
tree[x].maxn += num;
lazy[x] += num;
return;
} else {
down(x);
if (l <= tree[x].mid) change(x << 1, l, r, num);
if (r > tree[x].mid) change(x << 1 | 1, l, r, num);
tree[x].maxn = max(tree[x << 1].maxn, tree[x << 1 | 1].maxn);
}
}
int ask(int x, int l, int r) { return tree[x].maxn; }
int main() {
int n, k;
int ant = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
stack<int> s;
for (int i = 1; i <= n; i++) {
if (s.empty())
s.push(i);
else {
while (!s.empty() && a[s.top()] < a[i]) {
link(i, s.top());
indeg[s.top()]++;
s.pop();
}
s.push(i);
}
}
for (int i = 1; i <= n; i++) {
if (indeg[i]) continue;
link(n + 1, i);
}
dfs(n + 1);
build(1, in[n + 1], out[n + 1]);
for (int i = 1; i <= n; i++) {
if (i > k) {
change(1, in[i - k], out[i - k], -1);
change(1, in[i], out[i], 1);
printf(" %d", ask(1, in[n + 1], out[n + 1]));
} else {
change(1, in[i], out[i], 1);
if (i == k) printf("%d", ask(1, in[n + 1], out[n + 1]));
}
}
puts("");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
const int MAX = 1e6 + 6;
int n, k, a[MAX];
int p[MAX];
int compnxt(int i) {
if (p[i]) return p[i];
p[i] = i + 1;
while (p[i] <= n && a[p[i]] <= a[i]) {
p[i] = compnxt(p[i]);
}
return p[i];
}
vector<int> ch[MAX];
int disc[MAX], leave[MAX], dfstack[MAX];
void dfs() {
int dfst = 0;
int s = 0;
dfstack[s++] = n + 1;
while (s) {
int u = dfstack[s - 1];
if (disc[u]) {
leave[u] = ++dfst;
--s;
} else {
disc[u] = ++dfst;
for (int v : ch[u]) {
dfstack[s++] = v;
}
}
}
assert(dfst == 2 * (n + 1));
}
int seg[8 * MAX], lazy[8 * MAX];
void pushdown(int u) {
lazy[2 * u] += lazy[u];
seg[2 * u] += lazy[u];
lazy[2 * u + 1] += lazy[u];
seg[2 * u + 1] += lazy[u];
lazy[u] = 0;
}
void upd(int u, int tl, int tr, int l, int r, int v) {
if (l > tr || r < tl) return;
if (lazy[u] && tl != tr) pushdown(u);
if (l <= tl && tr <= r) {
seg[u] += v;
lazy[u] = v;
} else {
int tm = (tl + tr) / 2;
upd(2 * u, tl, tm, l, r, v);
upd(2 * u + 1, tm + 1, tr, l, r, v);
seg[u] = max(seg[2 * u], seg[2 * u + 1]);
}
}
int ans[MAX];
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) {
compnxt(i);
ch[p[i]].push_back(i);
}
dfs();
for (int i = n; i; --i) {
if (i + k <= n) {
upd(1, 1, 2 * (n + 1), disc[i + k], leave[i + k], -1);
}
upd(1, 1, 2 * (n + 1), disc[i], leave[i], 1);
if (i <= n - k + 1) {
ans[i] = seg[1];
}
}
for (int i = 1; i <= n - k + 1; ++i) {
printf("%d ", ans[i]);
}
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;
using ii = pair<int, int>;
const int MAX = 1e6 + 6;
int n, k, a[MAX];
int p[MAX];
int compnxt(int i) {
if (p[i]) return p[i];
p[i] = i + 1;
while (p[i] <= n && a[p[i]] <= a[i]) {
p[i] = compnxt(p[i]);
}
return p[i];
}
vector<int> ch[MAX];
int disc[MAX], leave[MAX], dfstack[MAX];
void dfs() {
int dfst = 0;
int s = 0;
dfstack[s++] = n + 1;
while (s) {
int u = dfstack[s - 1];
if (disc[u]) {
leave[u] = ++dfst;
--s;
} else {
disc[u] = ++dfst;
for (int v : ch[u]) {
dfstack[s++] = v;
}
}
}
assert(dfst == 2 * (n + 1));
}
int seg[8 * MAX], lazy[8 * MAX];
void pushdown(int u) {
lazy[2 * u] += lazy[u];
seg[2 * u] += lazy[u];
lazy[2 * u + 1] += lazy[u];
seg[2 * u + 1] += lazy[u];
lazy[u] = 0;
}
void upd(int u, int tl, int tr, int l, int r, int v) {
if (l > tr || r < tl) return;
if (lazy[u] && tl != tr) pushdown(u);
if (l <= tl && tr <= r) {
seg[u] += v;
lazy[u] = v;
} else {
int tm = (tl + tr) / 2;
upd(2 * u, tl, tm, l, r, v);
upd(2 * u + 1, tm + 1, tr, l, r, v);
seg[u] = max(seg[2 * u], seg[2 * u + 1]);
}
}
int ans[MAX];
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) {
compnxt(i);
ch[p[i]].push_back(i);
}
dfs();
for (int i = n; i; --i) {
if (i + k <= n) {
upd(1, 1, 2 * (n + 1), disc[i + k], leave[i + k], -1);
}
upd(1, 1, 2 * (n + 1), disc[i], leave[i], 1);
if (i <= n - k + 1) {
ans[i] = seg[1];
}
}
for (int i = 1; i <= n - k + 1; ++i) {
printf("%d ", ans[i]);
}
printf("\n");
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> v[2000006];
bitset<2000006> is;
int in[2000006], out[2000006], t;
struct SEG {
struct Node {
int tag, mx;
} node[4 * 2000006];
inline void pull(int id) {
node[id].mx = max(node[id << 1].mx, node[id << 1 | 1].mx);
}
inline void push(int id) {
int tmp = node[id].tag;
if (tmp == 0) return;
node[id].tag = 0;
node[id << 1].tag += tmp;
node[id << 1].mx += tmp;
node[id << 1 | 1].tag += tmp;
node[id << 1 | 1].mx += tmp;
}
inline void update(int l, int r, int ll, int rr, int k, int id) {
if (l > rr || r < ll) return;
push(id);
if (l >= ll && r <= rr) {
node[id].tag += k;
node[id].mx += k;
return;
}
update(l, ((l + r) >> 1), ll, rr, k, id << 1);
update(((l + r) >> 1) + 1, r, ll, rr, k, id << 1 | 1);
pull(id);
}
} seg;
int n, k, a[2000006], la[2000006];
inline void DFS(int now) {
is[now] = 1;
in[now] = ++t;
for (int i : v[now]) {
DFS(i);
}
out[now] = t;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
stack<int> st;
for (int i = n; i >= 1; --i) {
while (st.size() && a[st.top()] <= a[i]) st.pop();
if (st.size()) {
v[st.top()].push_back(i);
la[i]++;
}
st.push(i);
}
for (int i = 1; i <= n; ++i) {
if (la[i] == 0) DFS(i);
}
for (int i = 1; i <= n; i++) {
if (i - k >= 1) {
seg.update(1, t, in[i - k], out[i - k], -1, 1);
}
seg.update(1, t, in[i], out[i], 1, 1);
if (i >= k) cout << seg.node[1].mx << ' ';
}
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;
vector<int> v[2000006];
bitset<2000006> is;
int in[2000006], out[2000006], t;
struct SEG {
struct Node {
int tag, mx;
} node[4 * 2000006];
inline void pull(int id) {
node[id].mx = max(node[id << 1].mx, node[id << 1 | 1].mx);
}
inline void push(int id) {
int tmp = node[id].tag;
if (tmp == 0) return;
node[id].tag = 0;
node[id << 1].tag += tmp;
node[id << 1].mx += tmp;
node[id << 1 | 1].tag += tmp;
node[id << 1 | 1].mx += tmp;
}
inline void update(int l, int r, int ll, int rr, int k, int id) {
if (l > rr || r < ll) return;
push(id);
if (l >= ll && r <= rr) {
node[id].tag += k;
node[id].mx += k;
return;
}
update(l, ((l + r) >> 1), ll, rr, k, id << 1);
update(((l + r) >> 1) + 1, r, ll, rr, k, id << 1 | 1);
pull(id);
}
} seg;
int n, k, a[2000006], la[2000006];
inline void DFS(int now) {
is[now] = 1;
in[now] = ++t;
for (int i : v[now]) {
DFS(i);
}
out[now] = t;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
stack<int> st;
for (int i = n; i >= 1; --i) {
while (st.size() && a[st.top()] <= a[i]) st.pop();
if (st.size()) {
v[st.top()].push_back(i);
la[i]++;
}
st.push(i);
}
for (int i = 1; i <= n; ++i) {
if (la[i] == 0) DFS(i);
}
for (int i = 1; i <= n; i++) {
if (i - k >= 1) {
seg.update(1, t, in[i - k], out[i - k], -1, 1);
}
seg.update(1, t, in[i], out[i], 1, 1);
if (i >= k) cout << seg.node[1].mx << ' ';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
const int MN = 1e6 + 5;
int n, k, a[MN];
vi g[MN];
int tin[MN], tout[MN], tt;
template <typename T>
struct SegmentTree {
T t[MN * 4], lz[MN * 4];
const T ID = 0;
T merge(T x, T y) { return max(x, y); }
void push(int i, int l, int r) {
if (lz[i]) {
t[i] += lz[i];
if (l != r) {
lz[(i << 1)] += lz[i];
lz[(i << 1 | 1)] += lz[i];
}
lz[i] = 0;
}
}
void upd(int ql, int qr, int v, int i = 1, int l = 1, int r = n + 1) {
push(i, l, r);
if (l > qr || r < ql || ql > qr) return;
if (l >= ql && r <= qr) {
lz[i] += v, push(i, l, r);
return;
}
int m = (l + r) / 2;
upd(ql, qr, v, (i << 1), l, m), upd(ql, qr, v, (i << 1 | 1), m + 1, r);
t[i] = merge(t[(i << 1)], t[(i << 1 | 1)]);
}
T get(int ql, int qr, int i = 1, int l = 1, int r = n + 1) {
push(i, l, r);
if (l > qr || r < ql) return ID;
if (l >= ql && r <= qr) return t[i];
int m = (l + r) / 2;
return merge(get(ql, qr, (i << 1), l, m),
get(ql, qr, (i << 1 | 1), m + 1, r));
}
};
SegmentTree<int> seg;
void ett(int u) {
tin[u] = ++tt;
for (int v : g[u]) ett(v);
tout[u] = tt;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
stack<int> s;
for (int i = n - 1; i >= 0; i--) {
while (!s.empty() && a[i] >= a[s.top()]) s.pop();
int nxt = s.empty() ? n : s.top();
g[nxt].push_back(i);
s.push(i);
}
ett(n);
for (int i = 0; i < k - 1; i++) seg.upd(tin[i], tout[i], 1);
for (int i = k - 1; i < n; i++) {
seg.upd(tin[i], tout[i], 1);
cout << seg.get(1, n + 1) << " ";
seg.upd(tin[i - k + 1], tout[i - k + 1], -1);
}
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;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
const int MN = 1e6 + 5;
int n, k, a[MN];
vi g[MN];
int tin[MN], tout[MN], tt;
template <typename T>
struct SegmentTree {
T t[MN * 4], lz[MN * 4];
const T ID = 0;
T merge(T x, T y) { return max(x, y); }
void push(int i, int l, int r) {
if (lz[i]) {
t[i] += lz[i];
if (l != r) {
lz[(i << 1)] += lz[i];
lz[(i << 1 | 1)] += lz[i];
}
lz[i] = 0;
}
}
void upd(int ql, int qr, int v, int i = 1, int l = 1, int r = n + 1) {
push(i, l, r);
if (l > qr || r < ql || ql > qr) return;
if (l >= ql && r <= qr) {
lz[i] += v, push(i, l, r);
return;
}
int m = (l + r) / 2;
upd(ql, qr, v, (i << 1), l, m), upd(ql, qr, v, (i << 1 | 1), m + 1, r);
t[i] = merge(t[(i << 1)], t[(i << 1 | 1)]);
}
T get(int ql, int qr, int i = 1, int l = 1, int r = n + 1) {
push(i, l, r);
if (l > qr || r < ql) return ID;
if (l >= ql && r <= qr) return t[i];
int m = (l + r) / 2;
return merge(get(ql, qr, (i << 1), l, m),
get(ql, qr, (i << 1 | 1), m + 1, r));
}
};
SegmentTree<int> seg;
void ett(int u) {
tin[u] = ++tt;
for (int v : g[u]) ett(v);
tout[u] = tt;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
stack<int> s;
for (int i = n - 1; i >= 0; i--) {
while (!s.empty() && a[i] >= a[s.top()]) s.pop();
int nxt = s.empty() ? n : s.top();
g[nxt].push_back(i);
s.push(i);
}
ett(n);
for (int i = 0; i < k - 1; i++) seg.upd(tin[i], tout[i], 1);
for (int i = k - 1; i < n; i++) {
seg.upd(tin[i], tout[i], 1);
cout << seg.get(1, n + 1) << " ";
seg.upd(tin[i - k + 1], tout[i - k + 1], -1);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using dd = double;
using ldd = long double;
using si = short int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
vector<vector<int>> gr;
void build_gr(vector<int>& arr) {
int n = arr.size();
gr.resize(n);
vector<int> ind;
for (int i = 0; i < n; ++i) {
while (ind.size() && arr[ind.back()] < arr[i]) {
gr[i].push_back(ind.back());
ind.pop_back();
}
ind.push_back(i);
}
}
int _size = 1 << 20;
vector<ll> fir, las, tree(_size << 1, 0);
int sz = 0;
vector<bool> was(_size);
vector<int> _upd(_size << 1);
void dfs(int v) {
was[v] = 1;
fir[v] = sz;
++sz;
for (int i : gr[v]) {
dfs(i);
}
las[v] = sz - 1;
}
void build_eu() {
fir.resize(gr.size());
las.resize(gr.size());
for (int i = gr.size() - 1; i >= 0; --i)
if (!was[i]) dfs(i);
}
void push(int v) {
if (v >= _size) return;
_upd[v << 1] += _upd[v];
_upd[v << 1 | 1] += _upd[v];
tree[v << 1] += _upd[v];
tree[v << 1 | 1] += _upd[v];
_upd[v] = 0;
}
void upd(int v, int l, int r, int fl, int fr, ll pl) {
if (r <= l || r <= fl || fr <= l) return;
if (fl <= l && r <= fr) {
if (pl == -1) {
tree[v] = _upd[v] = -1e9;
return;
}
_upd[v] += pl;
tree[v] += pl;
return;
}
push(v);
upd(v << 1, l, (r + l) >> 1, fl, fr, pl);
upd(v << 1 | 1, (r + l) >> 1, r, fl, fr, pl);
tree[v] = max(tree[v << 1], tree[v << 1 | 1]);
}
ll get(int v, int l, int r, int fl, int fr) {
if (r <= l || r <= fl || fr <= l) return -1e9;
if (fl <= l && r <= fr) return tree[v];
push(v);
return max(get(v << 1, l, (r + l) >> 1, fl, fr),
get(v << 1 | 1, (r + l) >> 1, r, fl, fr));
}
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> arr(n);
for (int i = 0; i < n; ++i) cin >> arr[i];
build_gr(arr);
build_eu();
for (int i = 0; i < k; ++i) {
upd(1, 0, _size, fir[i], las[i] + 1, 1);
}
for (int i = k; i < n; ++i) {
cout << tree[1] << " ";
upd(1, 0, _size, fir[i], las[i] + 1, 1);
upd(1, 0, _size, fir[i - k], las[i - k] + 1, -1);
}
cout << tree[1];
0;
0;
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;
using ll = long long int;
using ull = unsigned long long int;
using dd = double;
using ldd = long double;
using si = short int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
vector<vector<int>> gr;
void build_gr(vector<int>& arr) {
int n = arr.size();
gr.resize(n);
vector<int> ind;
for (int i = 0; i < n; ++i) {
while (ind.size() && arr[ind.back()] < arr[i]) {
gr[i].push_back(ind.back());
ind.pop_back();
}
ind.push_back(i);
}
}
int _size = 1 << 20;
vector<ll> fir, las, tree(_size << 1, 0);
int sz = 0;
vector<bool> was(_size);
vector<int> _upd(_size << 1);
void dfs(int v) {
was[v] = 1;
fir[v] = sz;
++sz;
for (int i : gr[v]) {
dfs(i);
}
las[v] = sz - 1;
}
void build_eu() {
fir.resize(gr.size());
las.resize(gr.size());
for (int i = gr.size() - 1; i >= 0; --i)
if (!was[i]) dfs(i);
}
void push(int v) {
if (v >= _size) return;
_upd[v << 1] += _upd[v];
_upd[v << 1 | 1] += _upd[v];
tree[v << 1] += _upd[v];
tree[v << 1 | 1] += _upd[v];
_upd[v] = 0;
}
void upd(int v, int l, int r, int fl, int fr, ll pl) {
if (r <= l || r <= fl || fr <= l) return;
if (fl <= l && r <= fr) {
if (pl == -1) {
tree[v] = _upd[v] = -1e9;
return;
}
_upd[v] += pl;
tree[v] += pl;
return;
}
push(v);
upd(v << 1, l, (r + l) >> 1, fl, fr, pl);
upd(v << 1 | 1, (r + l) >> 1, r, fl, fr, pl);
tree[v] = max(tree[v << 1], tree[v << 1 | 1]);
}
ll get(int v, int l, int r, int fl, int fr) {
if (r <= l || r <= fl || fr <= l) return -1e9;
if (fl <= l && r <= fr) return tree[v];
push(v);
return max(get(v << 1, l, (r + l) >> 1, fl, fr),
get(v << 1 | 1, (r + l) >> 1, r, fl, fr));
}
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> arr(n);
for (int i = 0; i < n; ++i) cin >> arr[i];
build_gr(arr);
build_eu();
for (int i = 0; i < k; ++i) {
upd(1, 0, _size, fir[i], las[i] + 1, 1);
}
for (int i = k; i < n; ++i) {
cout << tree[1] << " ";
upd(1, 0, _size, fir[i], las[i] + 1, 1);
upd(1, 0, _size, fir[i - k], las[i - k] + 1, -1);
}
cout << tree[1];
0;
0;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000000 + 10;
int n, k, a[N];
int mx[N << 2], lz[N << 2], pre[N];
int stk[N], top;
void pushdown(int rt) {
if (lz[rt]) {
mx[rt << 1] = mx[rt << 1] + lz[rt];
lz[rt << 1] = lz[rt << 1] + lz[rt];
mx[rt << 1 | 1] = mx[rt << 1 | 1] + lz[rt];
lz[rt << 1 | 1] = lz[rt << 1 | 1] + lz[rt];
lz[rt] = 0;
}
}
void update(int l, int r, int rt, int L, int R) {
if (L <= l && r <= R) {
lz[rt]++;
mx[rt]++;
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
if (L <= mid) update(l, mid, rt << 1, L, R);
if (R > mid) update(mid + 1, r, rt << 1 | 1, L, R);
mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
}
int query(int l, int r, int rt, int L, int R) {
if (L <= l && r <= R) {
return mx[rt];
}
pushdown(rt);
int mid = (l + r) >> 1;
int ans = 0;
if (L <= mid) ans = max(ans, query(l, mid, rt << 1, L, R));
if (R > mid) ans = max(ans, query(mid + 1, r, rt << 1 | 1, L, R));
return ans;
}
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 (top && a[stk[top]] < a[i]) --top;
pre[i] = stk[top];
stk[++top] = i;
}
for (int i = 1; i <= n; i++) {
int L = max(pre[i] + 1, i - k + 1);
int R = i;
update(1, n, 1, L, R);
if (i >= k) {
int lef = max(i - k + 1, 1);
printf("%d ", query(1, n, 1, lef, R));
}
}
}
| ### 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 = 1000000 + 10;
int n, k, a[N];
int mx[N << 2], lz[N << 2], pre[N];
int stk[N], top;
void pushdown(int rt) {
if (lz[rt]) {
mx[rt << 1] = mx[rt << 1] + lz[rt];
lz[rt << 1] = lz[rt << 1] + lz[rt];
mx[rt << 1 | 1] = mx[rt << 1 | 1] + lz[rt];
lz[rt << 1 | 1] = lz[rt << 1 | 1] + lz[rt];
lz[rt] = 0;
}
}
void update(int l, int r, int rt, int L, int R) {
if (L <= l && r <= R) {
lz[rt]++;
mx[rt]++;
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
if (L <= mid) update(l, mid, rt << 1, L, R);
if (R > mid) update(mid + 1, r, rt << 1 | 1, L, R);
mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
}
int query(int l, int r, int rt, int L, int R) {
if (L <= l && r <= R) {
return mx[rt];
}
pushdown(rt);
int mid = (l + r) >> 1;
int ans = 0;
if (L <= mid) ans = max(ans, query(l, mid, rt << 1, L, R));
if (R > mid) ans = max(ans, query(mid + 1, r, rt << 1 | 1, L, R));
return ans;
}
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 (top && a[stk[top]] < a[i]) --top;
pre[i] = stk[top];
stk[++top] = i;
}
for (int i = 1; i <= n; i++) {
int L = max(pre[i] + 1, i - k + 1);
int R = i;
update(1, n, 1, L, R);
if (i >= k) {
int lef = max(i - k + 1, 1);
printf("%d ", query(1, n, 1, lef, R));
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int n, k, v[maxn], sta[maxn], id[maxn], topt, root, l[maxn], top;
struct da {
int lc, rc, tag, ma;
} a[4 * maxn];
inline void updata(int n) { a[n].ma = max(a[a[n].lc].ma, a[a[n].rc].ma); }
void build_tree(int &n, int l, int r) {
n = ++topt;
if (l == r) return;
int mid = (l + r) >> 1;
build_tree(a[n].lc, l, mid);
build_tree(a[n].rc, mid + 1, r);
}
void push_down(int n) {
if (!a[n].tag) return;
a[a[n].lc].tag += a[n].tag;
a[a[n].lc].ma += a[n].tag;
a[a[n].rc].tag += a[n].tag;
a[a[n].rc].ma += a[n].tag;
a[n].tag = 0;
}
void tree_add(int n, int L, int R, int l, int r, int kk) {
if (L == l && R == r) {
a[n].tag += kk;
a[n].ma += kk;
return;
}
int mid = (L + R) >> 1;
push_down(n);
if (r <= mid)
tree_add(a[n].lc, L, mid, l, r, kk);
else if (l >= mid + 1)
tree_add(a[n].rc, mid + 1, R, l, r, kk);
else
tree_add(a[n].lc, L, mid, l, mid, kk),
tree_add(a[n].rc, mid + 1, R, mid + 1, r, kk);
updata(n);
}
int qury(int n, int L, int R, int l, int r) {
if (L == l && R == r) return a[n].ma;
int mid = (L + R) >> 1;
push_down(n);
if (r <= mid)
return qury(a[n].lc, L, mid, l, r);
else if (l >= mid + 1)
return qury(a[n].rc, mid + 1, R, l, r);
else
return max(qury(a[n].lc, L, mid, l, mid),
qury(a[n].rc, mid + 1, R, mid + 1, r));
updata(n);
}
int main() {
scanf("%d%d", &n, &k);
build_tree(root, 1, n);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
for (int i = 1; i <= n; i++) {
while (top && sta[top] < v[i]) top--;
l[i] = id[top] + 1;
sta[++top] = v[i];
id[top] = i;
}
for (int i = 1; i <= n; i++) {
tree_add(root, 1, n, l[i], i, 1);
if (i >= k)
printf("%d%c", qury(root, 1, n, i - k + 1, i), (i == n ? '\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;
const int maxn = 1e6 + 10;
int n, k, v[maxn], sta[maxn], id[maxn], topt, root, l[maxn], top;
struct da {
int lc, rc, tag, ma;
} a[4 * maxn];
inline void updata(int n) { a[n].ma = max(a[a[n].lc].ma, a[a[n].rc].ma); }
void build_tree(int &n, int l, int r) {
n = ++topt;
if (l == r) return;
int mid = (l + r) >> 1;
build_tree(a[n].lc, l, mid);
build_tree(a[n].rc, mid + 1, r);
}
void push_down(int n) {
if (!a[n].tag) return;
a[a[n].lc].tag += a[n].tag;
a[a[n].lc].ma += a[n].tag;
a[a[n].rc].tag += a[n].tag;
a[a[n].rc].ma += a[n].tag;
a[n].tag = 0;
}
void tree_add(int n, int L, int R, int l, int r, int kk) {
if (L == l && R == r) {
a[n].tag += kk;
a[n].ma += kk;
return;
}
int mid = (L + R) >> 1;
push_down(n);
if (r <= mid)
tree_add(a[n].lc, L, mid, l, r, kk);
else if (l >= mid + 1)
tree_add(a[n].rc, mid + 1, R, l, r, kk);
else
tree_add(a[n].lc, L, mid, l, mid, kk),
tree_add(a[n].rc, mid + 1, R, mid + 1, r, kk);
updata(n);
}
int qury(int n, int L, int R, int l, int r) {
if (L == l && R == r) return a[n].ma;
int mid = (L + R) >> 1;
push_down(n);
if (r <= mid)
return qury(a[n].lc, L, mid, l, r);
else if (l >= mid + 1)
return qury(a[n].rc, mid + 1, R, l, r);
else
return max(qury(a[n].lc, L, mid, l, mid),
qury(a[n].rc, mid + 1, R, mid + 1, r));
updata(n);
}
int main() {
scanf("%d%d", &n, &k);
build_tree(root, 1, n);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
for (int i = 1; i <= n; i++) {
while (top && sta[top] < v[i]) top--;
l[i] = id[top] + 1;
sta[++top] = v[i];
id[top] = i;
}
for (int i = 1; i <= n; i++) {
tree_add(root, 1, n, l[i], i, 1);
if (i >= k)
printf("%d%c", qury(root, 1, n, i - k + 1, i), (i == n ? '\n' : ' '));
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[4000005], d[4000005], ad[4000005], last[4000005], inf = 1e9;
void build(int k, int l, int r) {
d[k] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(k * 2, l, mid);
build(k * 2 + 1, mid + 1, r);
}
int query(int k, int l, int r, int x, int y) {
if (x > y) return 0;
if (x <= l && y >= r) return d[k];
int mid = (l + r) >> 1;
if (y <= mid)
return query(k * 2, l, mid, x, y);
else if (x > mid)
return query(k * 2 + 1, mid + 1, r, x, y);
else
return max(query(k * 2, l, mid, x, mid),
query(k * 2 + 1, mid + 1, r, mid + 1, y));
}
void update(int k, int l, int r, int x, int y) {
if (l == r) {
d[k] = max(d[k], y);
return;
}
int mid = (l + r) >> 1;
if (x <= mid)
update(k * 2, l, mid, x, y);
else
update(k * 2 + 1, mid + 1, r, x, y);
d[k] = max(d[k * 2], d[k * 2 + 1]);
}
void build1(int k, int l, int r) {
d[k] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build1(k * 2, l, mid);
build1(k * 2 + 1, mid + 1, r);
}
void update1(int k, int l, int r, int x, int y, int z) {
if (x > y) return;
if (x <= l && y >= r) {
ad[k] += z;
d[k] += z;
return;
}
int mid = (l + r) >> 1;
if (y <= mid)
update1(k * 2, l, mid, x, y, z);
else if (x > mid)
update1(k * 2 + 1, mid + 1, r, x, y, z);
else
update1(k * 2, l, mid, x, mid, z),
update1(k * 2 + 1, mid + 1, r, mid + 1, y, z);
d[k] = max(d[k * 2], d[k * 2 + 1]) + ad[k];
}
int query1(int k, int l, int r, int x, int y, int add) {
if (x <= l && y >= r) return add + d[k];
int mid = (l + r) >> 1;
if (y <= mid)
return query1(k * 2, l, mid, x, y, add + ad[k]);
else if (x > mid)
return query1(k * 2 + 1, mid + 1, r, x, y, add + ad[k]);
else
return max(query1(k * 2, l, mid, x, mid, add + ad[k]),
query1(k * 2 + 1, mid + 1, r, mid + 1, y, add + ad[k]));
}
int main() {
scanf("%d%d", &n, &m);
build(1, 1, n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
last[i] = query(1, 1, n, a[i], n);
update(1, 1, n, a[i], i);
}
build1(1, 1, n);
for (int i = 1; i < m; i++) update1(1, 1, n, last[i] + 1, i, 1);
for (int i = m; i <= n; i++) {
update1(1, 1, n, last[i] + 1, i, 1);
printf("%d ", query1(1, 1, n, i - m + 1, i, 0));
}
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 n, m, a[4000005], d[4000005], ad[4000005], last[4000005], inf = 1e9;
void build(int k, int l, int r) {
d[k] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(k * 2, l, mid);
build(k * 2 + 1, mid + 1, r);
}
int query(int k, int l, int r, int x, int y) {
if (x > y) return 0;
if (x <= l && y >= r) return d[k];
int mid = (l + r) >> 1;
if (y <= mid)
return query(k * 2, l, mid, x, y);
else if (x > mid)
return query(k * 2 + 1, mid + 1, r, x, y);
else
return max(query(k * 2, l, mid, x, mid),
query(k * 2 + 1, mid + 1, r, mid + 1, y));
}
void update(int k, int l, int r, int x, int y) {
if (l == r) {
d[k] = max(d[k], y);
return;
}
int mid = (l + r) >> 1;
if (x <= mid)
update(k * 2, l, mid, x, y);
else
update(k * 2 + 1, mid + 1, r, x, y);
d[k] = max(d[k * 2], d[k * 2 + 1]);
}
void build1(int k, int l, int r) {
d[k] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build1(k * 2, l, mid);
build1(k * 2 + 1, mid + 1, r);
}
void update1(int k, int l, int r, int x, int y, int z) {
if (x > y) return;
if (x <= l && y >= r) {
ad[k] += z;
d[k] += z;
return;
}
int mid = (l + r) >> 1;
if (y <= mid)
update1(k * 2, l, mid, x, y, z);
else if (x > mid)
update1(k * 2 + 1, mid + 1, r, x, y, z);
else
update1(k * 2, l, mid, x, mid, z),
update1(k * 2 + 1, mid + 1, r, mid + 1, y, z);
d[k] = max(d[k * 2], d[k * 2 + 1]) + ad[k];
}
int query1(int k, int l, int r, int x, int y, int add) {
if (x <= l && y >= r) return add + d[k];
int mid = (l + r) >> 1;
if (y <= mid)
return query1(k * 2, l, mid, x, y, add + ad[k]);
else if (x > mid)
return query1(k * 2 + 1, mid + 1, r, x, y, add + ad[k]);
else
return max(query1(k * 2, l, mid, x, mid, add + ad[k]),
query1(k * 2 + 1, mid + 1, r, mid + 1, y, add + ad[k]));
}
int main() {
scanf("%d%d", &n, &m);
build(1, 1, n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
last[i] = query(1, 1, n, a[i], n);
update(1, 1, n, a[i], i);
}
build1(1, 1, n);
for (int i = 1; i < m; i++) update1(1, 1, n, last[i] + 1, i, 1);
for (int i = m; i <= n; i++) {
update1(1, 1, n, last[i] + 1, i, 1);
printf("%d ", query1(1, 1, n, i - m + 1, i, 0));
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int c = 1000002, f = 21, cc = 4194304, kk = cc / 2;
int n, k, t[c], cnt, be[c], ki[c], kezd[cc], veg[cc], maxi[cc], lp[cc];
vector<int> sz[c], p;
bool v[c];
void dfs(int a) {
v[a] = true;
cnt++, be[a] = cnt;
for (int x : sz[a]) {
dfs(x);
}
cnt++, ki[a] = cnt;
}
void add(int a, int l, int r, int e) {
if (kezd[a] > r || veg[a] < l) return;
if (l <= kezd[a] && veg[a] <= r) {
lp[a] += e;
return;
}
int x = 2 * a, y = 2 * a + 1;
add(x, l, r, e), add(y, l, r, e);
maxi[a] = max(maxi[x] + lp[x], maxi[y] + lp[y]);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> t[i];
while (p.size() > 0 && t[i] > t[p.back()]) {
sz[i].push_back(p.back());
p.pop_back();
}
p.push_back(i);
}
for (int i = n; i; i--) {
if (!v[i]) {
dfs(i);
}
}
for (int i = kk; i < cc; i++) kezd[i] = i - kk, veg[i] = i - kk;
for (int i = kk - 1; i; i--) kezd[i] = kezd[2 * i], veg[i] = veg[2 * i + 1];
for (int i = 1; i < k; i++) {
add(1, be[i], ki[i], 1);
}
for (int i = k; i <= n; i++) {
add(1, be[i], ki[i], 1);
cout << maxi[1] + lp[1] << " ";
int s = i - k + 1;
add(1, be[s], ki[s], -1);
}
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 c = 1000002, f = 21, cc = 4194304, kk = cc / 2;
int n, k, t[c], cnt, be[c], ki[c], kezd[cc], veg[cc], maxi[cc], lp[cc];
vector<int> sz[c], p;
bool v[c];
void dfs(int a) {
v[a] = true;
cnt++, be[a] = cnt;
for (int x : sz[a]) {
dfs(x);
}
cnt++, ki[a] = cnt;
}
void add(int a, int l, int r, int e) {
if (kezd[a] > r || veg[a] < l) return;
if (l <= kezd[a] && veg[a] <= r) {
lp[a] += e;
return;
}
int x = 2 * a, y = 2 * a + 1;
add(x, l, r, e), add(y, l, r, e);
maxi[a] = max(maxi[x] + lp[x], maxi[y] + lp[y]);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> t[i];
while (p.size() > 0 && t[i] > t[p.back()]) {
sz[i].push_back(p.back());
p.pop_back();
}
p.push_back(i);
}
for (int i = n; i; i--) {
if (!v[i]) {
dfs(i);
}
}
for (int i = kk; i < cc; i++) kezd[i] = i - kk, veg[i] = i - kk;
for (int i = kk - 1; i; i--) kezd[i] = kezd[2 * i], veg[i] = veg[2 * i + 1];
for (int i = 1; i < k; i++) {
add(1, be[i], ki[i], 1);
}
for (int i = k; i <= n; i++) {
add(1, be[i], ki[i], 1);
cout << maxi[1] + lp[1] << " ";
int s = i - k + 1;
add(1, be[s], ki[s], -1);
}
cout << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int read() {
char cc = getchar();
int cn = 0, flus = 1;
while (cc < '0' || cc > '9') {
if (cc == '-') flus = -flus;
cc = getchar();
}
while (cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar();
return cn * flus;
}
const int N = 1e6 + 5;
int n, m, cnt, num, top, idnex, a[N], st[N];
int head[N], dep[N], L[N], R[N], ans[N];
struct E {
int to, next;
} e[N];
struct Tr {
int mx, ad;
} tr[N * 4];
void input() {
n = read(), m = read();
for (register int i = 1; i <= n; ++i) a[i] = read();
}
void add(int x, int y) { e[++cnt] = (E){y, head[x]}, head[x] = cnt; }
void dfs(int x) {
L[x] = ++idnex;
for (register int i = head[x]; i; i = e[i].next)
dep[e[i].to] = dep[x] + 1, dfs(e[i].to);
R[x] = idnex;
}
void init() {
++n, a[n] = 123456789;
for (register int i = 1; i <= n; ++i) {
while (top && a[st[top]] < a[i]) add(i, st[top]), --top;
st[++top] = i;
}
dfs(n);
}
void pushup(int x) {
if (tr[x].ad) {
tr[x * 2].mx += tr[x].ad, tr[x * 2].ad += tr[x].ad;
tr[x * 2 + 1].mx += tr[x].ad, tr[x * 2 + 1].ad += tr[x].ad;
tr[x].ad = 0;
}
}
void update(int x, int l, int r, int ql, int qr, int ad) {
if (l > qr || r < ql) return;
if (l >= ql && r <= qr) {
tr[x].mx += ad, tr[x].ad += ad;
return;
}
pushup(x);
int mid = (l + r) >> 1;
update(x * 2, l, mid, ql, qr, ad), update(x * 2 + 1, mid + 1, r, ql, qr, ad);
tr[x].mx = max(tr[x * 2].mx, tr[x * 2 + 1].mx);
}
void solve() {
for (int i = n - 1; i > 0; --i) {
if (i + m < n) update(1, 1, n, L[i + m], R[i + m], -1);
update(1, 1, n, L[i], L[i], dep[i]);
if (i + m <= n) ans[++num] = tr[1].mx;
}
for (int i = num; i > 0; --i) printf("%d ", ans[i]);
}
signed main() {
input(), init(), 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;
int read() {
char cc = getchar();
int cn = 0, flus = 1;
while (cc < '0' || cc > '9') {
if (cc == '-') flus = -flus;
cc = getchar();
}
while (cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar();
return cn * flus;
}
const int N = 1e6 + 5;
int n, m, cnt, num, top, idnex, a[N], st[N];
int head[N], dep[N], L[N], R[N], ans[N];
struct E {
int to, next;
} e[N];
struct Tr {
int mx, ad;
} tr[N * 4];
void input() {
n = read(), m = read();
for (register int i = 1; i <= n; ++i) a[i] = read();
}
void add(int x, int y) { e[++cnt] = (E){y, head[x]}, head[x] = cnt; }
void dfs(int x) {
L[x] = ++idnex;
for (register int i = head[x]; i; i = e[i].next)
dep[e[i].to] = dep[x] + 1, dfs(e[i].to);
R[x] = idnex;
}
void init() {
++n, a[n] = 123456789;
for (register int i = 1; i <= n; ++i) {
while (top && a[st[top]] < a[i]) add(i, st[top]), --top;
st[++top] = i;
}
dfs(n);
}
void pushup(int x) {
if (tr[x].ad) {
tr[x * 2].mx += tr[x].ad, tr[x * 2].ad += tr[x].ad;
tr[x * 2 + 1].mx += tr[x].ad, tr[x * 2 + 1].ad += tr[x].ad;
tr[x].ad = 0;
}
}
void update(int x, int l, int r, int ql, int qr, int ad) {
if (l > qr || r < ql) return;
if (l >= ql && r <= qr) {
tr[x].mx += ad, tr[x].ad += ad;
return;
}
pushup(x);
int mid = (l + r) >> 1;
update(x * 2, l, mid, ql, qr, ad), update(x * 2 + 1, mid + 1, r, ql, qr, ad);
tr[x].mx = max(tr[x * 2].mx, tr[x * 2 + 1].mx);
}
void solve() {
for (int i = n - 1; i > 0; --i) {
if (i + m < n) update(1, 1, n, L[i + m], R[i + m], -1);
update(1, 1, n, L[i], L[i], dep[i]);
if (i + m <= n) ans[++num] = tr[1].mx;
}
for (int i = num; i > 0; --i) printf("%d ", ans[i]);
}
signed main() {
input(), init(), solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
using v2d = vector<vector<T> >;
template <class T>
bool uin(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
return a < b ? (a = b, true) : false;
}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 10;
const int inf = 1e9;
int n, k, a[maxN], p[maxN], start[maxN], finish[maxN], h[maxN], s, ans[maxN],
f[maxN * 4], lazy[maxN * 4], L, R, idx, val;
stack<int> st;
vector<int> adj[maxN];
void down(int x) {
int t = lazy[x];
if (t != 0) {
lazy[x] = 0;
lazy[x * 2] += t;
lazy[x * 2 + 1] += t;
f[x * 2] += t;
f[x * 2 + 1] += t;
}
}
void update_r(int x, int lo, int hi) {
if (lo > R || hi < L) {
return;
}
if (lo >= L && hi <= R) {
f[x] += val;
lazy[x] += val;
return;
}
down(x);
int mid = (lo + hi) / 2;
update_r(x * 2, lo, mid);
update_r(x * 2 + 1, mid + 1, hi);
f[x] = max(f[x * 2], f[x * 2 + 1]);
}
void update_p(int x, int lo, int hi) {
if (lo == hi && lo == idx) {
f[x] = val;
return;
}
down(x);
int mid = (lo + hi) / 2;
if (idx <= mid) {
update_p(x * 2, lo, mid);
} else {
update_p(x * 2 + 1, mid + 1, hi);
}
f[x] = max(f[x * 2], f[x * 2 + 1]);
}
int query(int x, int lo, int hi) {
if (lo > R || hi < L) {
return -inf;
}
if (lo >= L && hi <= R) {
return f[x];
}
down(x);
int mid = (lo + hi) / 2;
return max(query(x * 2, lo, mid), query(x * 2 + 1, mid + 1, hi));
}
void dfs(int u, int d) {
start[u] = ++s;
h[u] = d;
for (auto &v : adj[u]) {
dfs(v, d + 1);
}
finish[u] = s;
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= (int)(n); ++i) {
cin >> a[i];
}
for (int i = n; i; i--) {
while (!st.empty() && a[st.top()] <= a[i]) {
st.pop();
}
p[i] = (st.empty() ? n + 1 : st.top());
adj[p[i]].emplace_back(i);
st.push(i);
}
dfs(n + 1, 1);
for (int i = (int)(n - k + 2); i <= (int)(n + 1); ++i) {
L = R = start[i], val = h[i];
update_r(1, 1, s);
}
for (int i = n - k + 1; i; i--) {
L = start[i + k], R = finish[i + k], val = -1;
update_r(1, 1, s);
int d = 1;
if (p[i] < i + k) {
L = R = start[p[i]];
d += query(1, 1, s);
}
idx = start[i], val = d;
update_p(1, 1, s);
ans[i] = f[1];
}
for (int i = 1; i <= (int)(n - k + 1); ++i) {
cout << ans[i] << " ";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
while (T--) {
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;
template <class T>
using v2d = vector<vector<T> >;
template <class T>
bool uin(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
return a < b ? (a = b, true) : false;
}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 10;
const int inf = 1e9;
int n, k, a[maxN], p[maxN], start[maxN], finish[maxN], h[maxN], s, ans[maxN],
f[maxN * 4], lazy[maxN * 4], L, R, idx, val;
stack<int> st;
vector<int> adj[maxN];
void down(int x) {
int t = lazy[x];
if (t != 0) {
lazy[x] = 0;
lazy[x * 2] += t;
lazy[x * 2 + 1] += t;
f[x * 2] += t;
f[x * 2 + 1] += t;
}
}
void update_r(int x, int lo, int hi) {
if (lo > R || hi < L) {
return;
}
if (lo >= L && hi <= R) {
f[x] += val;
lazy[x] += val;
return;
}
down(x);
int mid = (lo + hi) / 2;
update_r(x * 2, lo, mid);
update_r(x * 2 + 1, mid + 1, hi);
f[x] = max(f[x * 2], f[x * 2 + 1]);
}
void update_p(int x, int lo, int hi) {
if (lo == hi && lo == idx) {
f[x] = val;
return;
}
down(x);
int mid = (lo + hi) / 2;
if (idx <= mid) {
update_p(x * 2, lo, mid);
} else {
update_p(x * 2 + 1, mid + 1, hi);
}
f[x] = max(f[x * 2], f[x * 2 + 1]);
}
int query(int x, int lo, int hi) {
if (lo > R || hi < L) {
return -inf;
}
if (lo >= L && hi <= R) {
return f[x];
}
down(x);
int mid = (lo + hi) / 2;
return max(query(x * 2, lo, mid), query(x * 2 + 1, mid + 1, hi));
}
void dfs(int u, int d) {
start[u] = ++s;
h[u] = d;
for (auto &v : adj[u]) {
dfs(v, d + 1);
}
finish[u] = s;
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= (int)(n); ++i) {
cin >> a[i];
}
for (int i = n; i; i--) {
while (!st.empty() && a[st.top()] <= a[i]) {
st.pop();
}
p[i] = (st.empty() ? n + 1 : st.top());
adj[p[i]].emplace_back(i);
st.push(i);
}
dfs(n + 1, 1);
for (int i = (int)(n - k + 2); i <= (int)(n + 1); ++i) {
L = R = start[i], val = h[i];
update_r(1, 1, s);
}
for (int i = n - k + 1; i; i--) {
L = start[i + k], R = finish[i + k], val = -1;
update_r(1, 1, s);
int d = 1;
if (p[i] < i + k) {
L = R = start[p[i]];
d += query(1, 1, s);
}
idx = start[i], val = d;
update_p(1, 1, s);
ans[i] = f[1];
}
for (int i = 1; i <= (int)(n - k + 1); ++i) {
cout << ans[i] << " ";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
while (T--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int f[N], a[N], tp[N], tg[N << 2], s[N << 2], ans[N], n, k, sta[N], top, p[N];
void add(int k, int z) {
s[k] += z;
tg[k] += z;
}
void push_d(int k) {
if (tg[k] == 0) return;
add(k << 1, tg[k]);
add(k << 1 | 1, tg[k]);
tg[k] = 0;
}
void push_up(int k) { s[k] = max(s[k << 1], s[k << 1 | 1]); }
void modify(int k, int l, int r, int x, int y, int z) {
if (x <= l && r <= y) return add(k, z);
int mid = l + r >> 1;
push_d(k);
if (x <= mid) modify(k << 1, l, mid, x, y, z);
if (mid < y) modify(k << 1 | 1, mid + 1, r, x, y, z);
push_up(k);
return;
}
int query(int k, int l, int r, int x, int y) {
if (x <= l && r <= y) return s[k];
int mid = l + r >> 1, tp = 0;
push_d(k);
if (x <= mid) tp = query(k << 1, l, mid, x, y);
if (mid < y) tp = max(tp, query(k << 1 | 1, mid + 1, r, x, y));
push_up(k);
return tp;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (register int i = (1); i <= (n); ++i) cin >> a[i];
for (register int i = (1); i <= (n); ++i) {
while (top && a[sta[top]] < a[i]) top--;
p[i] = sta[top] + 1;
sta[++top] = i;
}
top = 0;
for (register int i = (n); i >= (1); --i) {
while (top && a[sta[top]] <= a[i]) top--;
f[i] = f[sta[top]] + 1;
sta[++top] = i;
modify(1, 1, n, i, i, f[i]);
if (i + k <= n) modify(1, 1, n, p[i + k], i + k, -1);
if (i + k - 1 <= n) ans[i] = query(1, 1, n, i, i + k - 1);
}
for (register int i = (1); i <= (n - k + 1); ++i) cout << ans[i] << ' ';
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 N = 1e6 + 10;
int f[N], a[N], tp[N], tg[N << 2], s[N << 2], ans[N], n, k, sta[N], top, p[N];
void add(int k, int z) {
s[k] += z;
tg[k] += z;
}
void push_d(int k) {
if (tg[k] == 0) return;
add(k << 1, tg[k]);
add(k << 1 | 1, tg[k]);
tg[k] = 0;
}
void push_up(int k) { s[k] = max(s[k << 1], s[k << 1 | 1]); }
void modify(int k, int l, int r, int x, int y, int z) {
if (x <= l && r <= y) return add(k, z);
int mid = l + r >> 1;
push_d(k);
if (x <= mid) modify(k << 1, l, mid, x, y, z);
if (mid < y) modify(k << 1 | 1, mid + 1, r, x, y, z);
push_up(k);
return;
}
int query(int k, int l, int r, int x, int y) {
if (x <= l && r <= y) return s[k];
int mid = l + r >> 1, tp = 0;
push_d(k);
if (x <= mid) tp = query(k << 1, l, mid, x, y);
if (mid < y) tp = max(tp, query(k << 1 | 1, mid + 1, r, x, y));
push_up(k);
return tp;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (register int i = (1); i <= (n); ++i) cin >> a[i];
for (register int i = (1); i <= (n); ++i) {
while (top && a[sta[top]] < a[i]) top--;
p[i] = sta[top] + 1;
sta[++top] = i;
}
top = 0;
for (register int i = (n); i >= (1); --i) {
while (top && a[sta[top]] <= a[i]) top--;
f[i] = f[sta[top]] + 1;
sta[++top] = i;
modify(1, 1, n, i, i, f[i]);
if (i + k <= n) modify(1, 1, n, p[i + k], i + k, -1);
if (i + k - 1 <= n) ans[i] = query(1, 1, n, i, i + k - 1);
}
for (register int i = (1); i <= (n - k + 1); ++i) cout << ans[i] << ' ';
cout << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
ifstream in;
ofstream out;
const long long kk = 1000;
const long long ml = kk * kk;
const long long mod = ml * kk + 7;
struct vertex {
long max, push;
};
vector<vertex> t;
long n, i, j, s, k;
vector<long> m, las;
bool viv = false;
void read() {
cin >> n >> k;
for (i = 0; i < n; i++) {
long a;
cin >> a;
m.push_back(a);
}
}
void build() {
s = 1;
while (s < n) s <<= 1;
t.resize(2 * s);
}
void prepare() {
vector<long> st;
for (long i = 0; i < n; i++) {
while (st.size() && m[st.back()] < m[i]) st.pop_back();
if (st.size())
las.push_back(st.back());
else
las.push_back(-1);
st.push_back(i);
}
build();
}
void upd(long v) {
if (v >= s) return;
long val = max(t[2 * v].max, t[2 * v + 1].max);
t[v].max = max(t[v].max, val);
}
void push(long v) {
if (v >= s) return;
long add = t[v].push;
t[v].push = 0;
t[2 * v].max += add;
t[2 * v + 1].max += add;
t[2 * v].push += add;
t[2 * v + 1].push += add;
}
void add(long l, long r, long v, long tl, long tr, long val) {
if (r < tl || tr < l) return;
push(v);
if (l <= tl && tr <= r) {
t[v].push += val;
t[v].max += val;
return;
}
long tm = tl + tr >> 1;
add(l, r, 2 * v, tl, tm, val);
add(l, r, 2 * v + 1, tm + 1, tr, val);
upd(v);
}
long get(long l, long r, long v, long tl, long tr) {
if (r < tl || tr < l) return 0;
push(v);
if (l <= tl && tr <= r) return t[v].max;
long tm = tl + tr >> 1;
long v1 = get(l, r, 2 * v, tl, tm);
long v2 = get(l, r, 2 * v + 1, tm + 1, tr);
return max(v1, v2);
}
void work(long pl) {
long l = las[pl] + 1;
long r = pl;
add(l, r, 1, 0, s - 1, 1);
if (pl >= k - 1) cout << get(pl - k + 1, pl, 1, 0, s - 1) << ' ';
}
void write() {
if (!viv) return;
cout << "Segment tree:" << endl;
for (long i = 1; i < 2 * s; i++)
cout << "{" << t[i].max << ' ' << t[i].push << "} ";
cout << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
prepare();
if (viv) {
cout << "Better:" << endl;
for (auto i : las) cout << i << ' ';
cout << endl;
}
for (long i = 0; i < n; i++) work(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;
ifstream in;
ofstream out;
const long long kk = 1000;
const long long ml = kk * kk;
const long long mod = ml * kk + 7;
struct vertex {
long max, push;
};
vector<vertex> t;
long n, i, j, s, k;
vector<long> m, las;
bool viv = false;
void read() {
cin >> n >> k;
for (i = 0; i < n; i++) {
long a;
cin >> a;
m.push_back(a);
}
}
void build() {
s = 1;
while (s < n) s <<= 1;
t.resize(2 * s);
}
void prepare() {
vector<long> st;
for (long i = 0; i < n; i++) {
while (st.size() && m[st.back()] < m[i]) st.pop_back();
if (st.size())
las.push_back(st.back());
else
las.push_back(-1);
st.push_back(i);
}
build();
}
void upd(long v) {
if (v >= s) return;
long val = max(t[2 * v].max, t[2 * v + 1].max);
t[v].max = max(t[v].max, val);
}
void push(long v) {
if (v >= s) return;
long add = t[v].push;
t[v].push = 0;
t[2 * v].max += add;
t[2 * v + 1].max += add;
t[2 * v].push += add;
t[2 * v + 1].push += add;
}
void add(long l, long r, long v, long tl, long tr, long val) {
if (r < tl || tr < l) return;
push(v);
if (l <= tl && tr <= r) {
t[v].push += val;
t[v].max += val;
return;
}
long tm = tl + tr >> 1;
add(l, r, 2 * v, tl, tm, val);
add(l, r, 2 * v + 1, tm + 1, tr, val);
upd(v);
}
long get(long l, long r, long v, long tl, long tr) {
if (r < tl || tr < l) return 0;
push(v);
if (l <= tl && tr <= r) return t[v].max;
long tm = tl + tr >> 1;
long v1 = get(l, r, 2 * v, tl, tm);
long v2 = get(l, r, 2 * v + 1, tm + 1, tr);
return max(v1, v2);
}
void work(long pl) {
long l = las[pl] + 1;
long r = pl;
add(l, r, 1, 0, s - 1, 1);
if (pl >= k - 1) cout << get(pl - k + 1, pl, 1, 0, s - 1) << ' ';
}
void write() {
if (!viv) return;
cout << "Segment tree:" << endl;
for (long i = 1; i < 2 * s; i++)
cout << "{" << t[i].max << ' ' << t[i].push << "} ";
cout << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
prepare();
if (viv) {
cout << "Better:" << endl;
for (auto i : las) cout << i << ' ';
cout << endl;
}
for (long i = 0; i < n; i++) work(i);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int mx[N << 2], lazy[N << 2], n, k, l[N], a[N];
void pushup(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); }
void pushdown(int rt) {
if (lazy[rt]) {
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 rt, int l, int r, int L, int R, int val) {
if (L <= l && r <= R) {
mx[rt] += val;
lazy[rt] += val;
return;
}
pushdown(rt);
int mid = l + r >> 1;
if (mid >= L) update(rt << 1, l, mid, L, R, val);
if (mid < R) update(rt << 1 | 1, mid + 1, r, L, R, val);
pushup(rt);
}
int query(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) return mx[rt];
pushdown(rt);
int mid = l + r >> 1, ans = 0;
if (mid >= L) ans = max(ans, query(rt << 1, l, mid, L, R));
if (mid < R) ans = max(ans, query(rt << 1 | 1, mid + 1, r, L, R));
return ans;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
stack<int> stk;
a[0] = 1e9;
stk.push(0);
for (int i = 1; i <= n; ++i) {
while (a[stk.top()] < a[i]) stk.pop();
l[i] = stk.top();
stk.push(i);
}
for (int i = 1; i <= n; ++i) {
update(1, 1, n, l[i] + 1, i, 1);
if (i >= k) printf("%d%c", query(1, 1, n, i - k + 1, i), " \n"[i == 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;
const int N = 1e6 + 10;
int mx[N << 2], lazy[N << 2], n, k, l[N], a[N];
void pushup(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); }
void pushdown(int rt) {
if (lazy[rt]) {
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 rt, int l, int r, int L, int R, int val) {
if (L <= l && r <= R) {
mx[rt] += val;
lazy[rt] += val;
return;
}
pushdown(rt);
int mid = l + r >> 1;
if (mid >= L) update(rt << 1, l, mid, L, R, val);
if (mid < R) update(rt << 1 | 1, mid + 1, r, L, R, val);
pushup(rt);
}
int query(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) return mx[rt];
pushdown(rt);
int mid = l + r >> 1, ans = 0;
if (mid >= L) ans = max(ans, query(rt << 1, l, mid, L, R));
if (mid < R) ans = max(ans, query(rt << 1 | 1, mid + 1, r, L, R));
return ans;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
stack<int> stk;
a[0] = 1e9;
stk.push(0);
for (int i = 1; i <= n; ++i) {
while (a[stk.top()] < a[i]) stk.pop();
l[i] = stk.top();
stk.push(i);
}
for (int i = 1; i <= n; ++i) {
update(1, 1, n, l[i] + 1, i, 1);
if (i >= k) printf("%d%c", query(1, 1, n, i - k + 1, i), " \n"[i == n]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
const int unit = 450;
int a[maxn], l[maxn], r[maxn], tim;
int n, m;
stack<int> s;
long long t[maxn << 2], lazy[maxn << 2];
vector<int> v[maxn];
void dfs(int x) {
l[x] = ++tim;
for (auto u : v[x]) dfs(u);
r[x] = tim;
}
void add(int rt, int d) {
lazy[rt] += 1ll * d;
t[rt] += 1ll * d;
}
void pd(int rt) {
if (lazy[rt]) {
add(((rt << 1) | 1), lazy[rt]);
add((rt << 1), lazy[rt]);
lazy[rt] = 0;
}
}
void pu(int rt) { t[rt] = max(t[((rt << 1) | 1)], t[(rt << 1)]); }
void update(int L, int R, int d, int l, int r, int rt) {
if (L <= l && r <= R) {
add(rt, d);
return;
}
int mid = l + r >> 1;
pd(rt);
if (L <= mid) update(L, R, d, l, mid, rt << 1);
if (mid < R) update(L, R, d, mid + 1, r, rt << 1 | 1);
pu(rt);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = (1); i < (n + 1); i++) {
cin >> a[i];
while ((int)s.size() && a[i] > a[s.top()]) {
v[i].push_back(s.top());
s.pop();
}
s.push(i);
}
while ((int)s.size()) v[n + 1].push_back(s.top()), s.pop();
dfs(n + 1);
for (int i = (1); i < (m + 1); i++) update(l[i], r[i], 1, 1, tim, 1);
cout << t[1] << " ";
for (int i = (m + 1); i < (n + 1); i++) {
update(l[i], r[i], 1, 1, tim, 1);
update(l[i - m], r[i - m], -n, 1, tim, 1);
cout << t[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 int maxn = 1000005;
const int unit = 450;
int a[maxn], l[maxn], r[maxn], tim;
int n, m;
stack<int> s;
long long t[maxn << 2], lazy[maxn << 2];
vector<int> v[maxn];
void dfs(int x) {
l[x] = ++tim;
for (auto u : v[x]) dfs(u);
r[x] = tim;
}
void add(int rt, int d) {
lazy[rt] += 1ll * d;
t[rt] += 1ll * d;
}
void pd(int rt) {
if (lazy[rt]) {
add(((rt << 1) | 1), lazy[rt]);
add((rt << 1), lazy[rt]);
lazy[rt] = 0;
}
}
void pu(int rt) { t[rt] = max(t[((rt << 1) | 1)], t[(rt << 1)]); }
void update(int L, int R, int d, int l, int r, int rt) {
if (L <= l && r <= R) {
add(rt, d);
return;
}
int mid = l + r >> 1;
pd(rt);
if (L <= mid) update(L, R, d, l, mid, rt << 1);
if (mid < R) update(L, R, d, mid + 1, r, rt << 1 | 1);
pu(rt);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = (1); i < (n + 1); i++) {
cin >> a[i];
while ((int)s.size() && a[i] > a[s.top()]) {
v[i].push_back(s.top());
s.pop();
}
s.push(i);
}
while ((int)s.size()) v[n + 1].push_back(s.top()), s.pop();
dfs(n + 1);
for (int i = (1); i < (m + 1); i++) update(l[i], r[i], 1, 1, tim, 1);
cout << t[1] << " ";
for (int i = (m + 1); i < (n + 1); i++) {
update(l[i], r[i], 1, 1, tim, 1);
update(l[i - m], r[i - m], -n, 1, tim, 1);
cout << t[1] << " ";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
const int MAXLog = 20;
struct SegmentTree {
int maxVal, lazy;
int l, r;
SegmentTree *L, *R;
SegmentTree() {}
SegmentTree(int l, int r) {
this->maxVal = this->lazy = 0;
this->l = l;
this->r = r;
this->L = this->R = nullptr;
}
void build() {
if (l == r) return;
L = new SegmentTree(l, (l + r) / 2);
R = new SegmentTree((l + r) / 2 + 1, r);
L->build();
R->build();
}
void reset() {
maxVal = lazy = 0;
if (L != nullptr) L->reset();
if (R != nullptr) R->reset();
}
void updateLazy() {
maxVal += lazy;
if (L != nullptr) {
L->lazy += lazy;
R->lazy += lazy;
}
lazy = 0;
}
int query(int ql, int qr) {
updateLazy();
if (l >= ql && qr <= r) return maxVal;
if (r < ql || l > qr) return 0;
return max(L->query(ql, qr), R->query(ql, qr));
}
void update(int ql, int qr, int val) {
updateLazy();
if (l >= ql && r <= qr) {
lazy += val;
updateLazy();
return;
}
if (r < ql || l > qr) return;
L->update(ql, qr, val);
R->update(ql, qr, val);
maxVal = max(L->maxVal, R->maxVal);
}
};
int n, k;
int a[MAXN];
int nxt[MAXN];
SegmentTree *T;
int orderInd[MAXN], treeSize[MAXN];
vector<int> graph[MAXN], dfsOrder;
void DFS(int x) {
orderInd[x] = dfsOrder.size();
dfsOrder.push_back(x);
treeSize[x] = 1;
for (int y : graph[x]) {
DFS(y);
treeSize[x] += treeSize[y];
}
}
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);
graph[nxt[i]].push_back(i);
}
DFS(n);
T = new SegmentTree(0, dfsOrder.size() - 1);
T->build();
}
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();
for (int i = n - 1; i >= n - k; i--)
T->update(orderInd[i], orderInd[i] + treeSize[i] - 1, +1);
output.push_back(T->query(0, dfsOrder.size() - 1));
for (int i = n - 2; i >= k - 1; i--) {
T->update(orderInd[i - k + 1],
orderInd[i - k + 1] + treeSize[i - k + 1] - 1, +1);
T->update(orderInd[i + 1], orderInd[i + 1] + treeSize[i + 1] - 1, -1);
output.push_back(T->query(0, dfsOrder.size() - 1));
}
reverse(output.begin(), output.end());
for (int x : output) cout << x << " ";
cout << '\n';
}
| ### 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;
const int MAXLog = 20;
struct SegmentTree {
int maxVal, lazy;
int l, r;
SegmentTree *L, *R;
SegmentTree() {}
SegmentTree(int l, int r) {
this->maxVal = this->lazy = 0;
this->l = l;
this->r = r;
this->L = this->R = nullptr;
}
void build() {
if (l == r) return;
L = new SegmentTree(l, (l + r) / 2);
R = new SegmentTree((l + r) / 2 + 1, r);
L->build();
R->build();
}
void reset() {
maxVal = lazy = 0;
if (L != nullptr) L->reset();
if (R != nullptr) R->reset();
}
void updateLazy() {
maxVal += lazy;
if (L != nullptr) {
L->lazy += lazy;
R->lazy += lazy;
}
lazy = 0;
}
int query(int ql, int qr) {
updateLazy();
if (l >= ql && qr <= r) return maxVal;
if (r < ql || l > qr) return 0;
return max(L->query(ql, qr), R->query(ql, qr));
}
void update(int ql, int qr, int val) {
updateLazy();
if (l >= ql && r <= qr) {
lazy += val;
updateLazy();
return;
}
if (r < ql || l > qr) return;
L->update(ql, qr, val);
R->update(ql, qr, val);
maxVal = max(L->maxVal, R->maxVal);
}
};
int n, k;
int a[MAXN];
int nxt[MAXN];
SegmentTree *T;
int orderInd[MAXN], treeSize[MAXN];
vector<int> graph[MAXN], dfsOrder;
void DFS(int x) {
orderInd[x] = dfsOrder.size();
dfsOrder.push_back(x);
treeSize[x] = 1;
for (int y : graph[x]) {
DFS(y);
treeSize[x] += treeSize[y];
}
}
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);
graph[nxt[i]].push_back(i);
}
DFS(n);
T = new SegmentTree(0, dfsOrder.size() - 1);
T->build();
}
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();
for (int i = n - 1; i >= n - k; i--)
T->update(orderInd[i], orderInd[i] + treeSize[i] - 1, +1);
output.push_back(T->query(0, dfsOrder.size() - 1));
for (int i = n - 2; i >= k - 1; i--) {
T->update(orderInd[i - k + 1],
orderInd[i - k + 1] + treeSize[i - k + 1] - 1, +1);
T->update(orderInd[i + 1], orderInd[i + 1] + treeSize[i + 1] - 1, -1);
output.push_back(T->query(0, dfsOrder.size() - 1));
}
reverse(output.begin(), output.end());
for (int x : output) cout << x << " ";
cout << '\n';
}
``` |
#include <bits/stdc++.h>
const int inf = 1e9;
using namespace std;
int v[1000005], st[1000005], t[1000005], aux[1000005];
int findt(int nod) {
if (nod == t[nod]) return nod;
return t[nod] = findt(t[nod]);
}
int main() {
int n, k, i;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++) scanf("%d", &v[i]);
for (i = 1; i <= n; i++) {
st[i] = i - 1;
while (st[i] && v[st[i]] < v[i]) st[i] = st[st[i]];
}
int j = 1, sum = 0;
for (i = 1; i <= n; i++) {
t[i] = i;
int poz = findt(st[i] + 1);
if (poz <= j) sum++;
aux[poz]++;
aux[i + 1]--;
if (poz) t[poz] = poz - 1;
if (i >= k) printf("%d ", sum), sum += aux[++j];
}
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>
const int inf = 1e9;
using namespace std;
int v[1000005], st[1000005], t[1000005], aux[1000005];
int findt(int nod) {
if (nod == t[nod]) return nod;
return t[nod] = findt(t[nod]);
}
int main() {
int n, k, i;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++) scanf("%d", &v[i]);
for (i = 1; i <= n; i++) {
st[i] = i - 1;
while (st[i] && v[st[i]] < v[i]) st[i] = st[st[i]];
}
int j = 1, sum = 0;
for (i = 1; i <= n; i++) {
t[i] = i;
int poz = findt(st[i] + 1);
if (poz <= j) sum++;
aux[poz]++;
aux[i + 1]--;
if (poz) t[poz] = poz - 1;
if (i >= k) printf("%d ", sum), sum += aux[++j];
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long INF = 1e18;
const long double PI = acos((long double)-1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
const int MAXN = 1000005;
int N, K, A[MAXN], par[MAXN], in[MAXN], out[MAXN], cnt;
long long tree[4 * MAXN], lazy[4 * MAXN];
vector<int> v[MAXN];
inline void prop(int rt) {
tree[rt << 1] += lazy[rt], tree[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 ql, int qr, int rt, long long v) {
if (l == ql && r == qr) {
tree[rt] += v, lazy[rt] += v;
return;
}
if (lazy[rt]) prop(rt);
int mid = (l + r) / 2;
if (qr <= mid)
update(l, mid, ql, qr, rt << 1, v);
else if (ql > mid)
update(mid + 1, r, ql, qr, rt << 1 | 1, v);
else
update(l, mid, ql, mid, rt << 1, v),
update(mid + 1, r, mid + 1, qr, rt << 1 | 1, v);
tree[rt] = max(tree[rt << 1], tree[rt << 1 | 1]);
}
void dfs(int u) {
in[u] = ++cnt;
for (int ck : v[u]) dfs(ck);
out[u] = cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
for (int i = 1; i <= N; i++) cin >> A[i];
stack<pair<int, int> > second;
for (int i = N; i >= 1; i--) {
while (second.size() && second.top().first <= A[i]) second.pop();
if (second.empty())
par[i] = i;
else
par[i] = second.top().second, v[second.top().second].push_back(i);
second.push(make_pair(A[i], i));
}
for (int i = 1; i <= N; i++)
if (par[i] == i) dfs(i);
for (int i = 1; i <= N; i++) update(1, cnt, in[i], in[i], 1, -INT_MAX);
for (int i = 1; i <= N; i++) {
update(1, cnt, in[i], in[i], 1, INT_MAX);
update(1, cnt, in[i], out[i], 1, 1);
if (i >= K) {
cout << tree[1] << " ";
update(1, cnt, in[i - K + 1], in[i - K + 1], 1, -INT_MAX);
}
}
}
| ### 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 MOD = 1000000007;
const long long INF = 1e18;
const long double PI = acos((long double)-1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
const int MAXN = 1000005;
int N, K, A[MAXN], par[MAXN], in[MAXN], out[MAXN], cnt;
long long tree[4 * MAXN], lazy[4 * MAXN];
vector<int> v[MAXN];
inline void prop(int rt) {
tree[rt << 1] += lazy[rt], tree[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 ql, int qr, int rt, long long v) {
if (l == ql && r == qr) {
tree[rt] += v, lazy[rt] += v;
return;
}
if (lazy[rt]) prop(rt);
int mid = (l + r) / 2;
if (qr <= mid)
update(l, mid, ql, qr, rt << 1, v);
else if (ql > mid)
update(mid + 1, r, ql, qr, rt << 1 | 1, v);
else
update(l, mid, ql, mid, rt << 1, v),
update(mid + 1, r, mid + 1, qr, rt << 1 | 1, v);
tree[rt] = max(tree[rt << 1], tree[rt << 1 | 1]);
}
void dfs(int u) {
in[u] = ++cnt;
for (int ck : v[u]) dfs(ck);
out[u] = cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
for (int i = 1; i <= N; i++) cin >> A[i];
stack<pair<int, int> > second;
for (int i = N; i >= 1; i--) {
while (second.size() && second.top().first <= A[i]) second.pop();
if (second.empty())
par[i] = i;
else
par[i] = second.top().second, v[second.top().second].push_back(i);
second.push(make_pair(A[i], i));
}
for (int i = 1; i <= N; i++)
if (par[i] == i) dfs(i);
for (int i = 1; i <= N; i++) update(1, cnt, in[i], in[i], 1, -INT_MAX);
for (int i = 1; i <= N; i++) {
update(1, cnt, in[i], in[i], 1, INT_MAX);
update(1, cnt, in[i], out[i], 1, 1);
if (i >= K) {
cout << tree[1] << " ";
update(1, cnt, in[i - K + 1], in[i - K + 1], 1, -INT_MAX);
}
}
}
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.