output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {1, -1, 0, 0};
struct edge {
int a, b, c, f;
};
vector<edge> ed;
vector<int> eds[1000];
int S, T;
int dd[1000];
int pp[1000];
void make_edge(int a, int b, int c) {
edge x;
x.a = a;
x.b = b;
x.c = c;
x.f = 0;
eds[a].push_back(ed.size());
ed.push_back(x);
swap(x.a, x.b);
x.c = 0;
eds[b].push_back(ed.size());
ed.push_back(x);
}
int dfs(int v, int fl, int mn) {
if (v == T) return fl;
if (dd[v] >= dd[T]) return 0;
int now = 0;
for (; fl >= mn && pp[v] != (int)eds[v].size(); ++pp[v]) {
int e = eds[v][pp[v]];
int u = ed[e].b;
if (dd[u] != dd[v] + 1) continue;
int go = ed[e].c - ed[e].f;
if (go < mn) continue;
go = dfs(u, min(fl, go), mn);
fl -= go;
now += go;
ed[e].f += go;
ed[e ^ 1].f -= go;
if (fl == 0) break;
}
return now;
}
int dinic(int mn) {
queue<int> qu;
fill(dd, dd + T + 1, INF);
dd[S] = 0;
qu.push(S);
while (!qu.empty()) {
int x = qu.front();
qu.pop();
for (int e : eds[x]) {
if (ed[e].f + mn > ed[e].c) continue;
int u = ed[e].b;
if (dd[u] > dd[x] + 1) dd[u] = dd[x] + 1, qu.push(u);
}
}
if (dd[T] == INF) return 0;
fill(pp, pp + T + 1, 0);
return dfs(S, INF, mn);
}
int flow() {
int ans = 0;
for (int now = 8; now > 0; now /= 2) {
while (true) {
int x = dinic(now);
if (!x) break;
ans += x;
}
}
return ans;
}
int n, t;
string s1[20];
string s2[20];
void calc(int sx, int sy, int dp[20][20]) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) dp[i][j] = 100000;
queue<pair<int, int> > qu;
qu.push(make_pair(sx, sy));
dp[sx][sy] = 0;
while (!qu.empty()) {
int x, y;
tie(x, y) = qu.front();
qu.pop();
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || s1[nx][ny] == 'Z' ||
s1[nx][ny] == 'Y')
continue;
if (dp[nx][ny] > dp[x][y] + 1)
dp[nx][ny] = dp[x][y] + 1, qu.push(make_pair(nx, ny));
}
}
}
int dp[20][20];
int dp2[20][20];
int main() {
scanf("%d%d", &n, &t);
for (int i = 0; i < n; ++i) cin >> s1[i];
for (int i = 0; i < n; ++i) cin >> s2[i];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (s1[i][j] == 'Z') calc(i, j, dp2);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (s1[i][j] != 'Y' && s1[i][j] != 'Z') {
calc(i, j, dp);
for (int i1 = 0; i1 < n; ++i1)
for (int j1 = 0; j1 < n; ++j1) {
if (dp[i1][j1] > t || dp[i1][j1] > dp2[i1][j1]) continue;
int fl = 0;
for (int x = 0; x < 4; ++x) {
int ni = i1 + dx[x];
int nj = j1 + dy[x];
if (ni < 0 || nj < 0 || ni >= n || nj >= n || s1[ni][nj] == 'Y' ||
s1[ni][nj] == 'Z')
continue;
if (dp[ni][nj] < dp2[ni][nj]) fl = 1;
}
if (dp[i1][j1] < dp2[i1][j1]) fl = 1;
if (fl == 1) make_edge(i * n + j, i1 * n + j1 + n * n, 10000);
}
}
S = 2 * n * n;
T = S + 1;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) {
if (s1[i][j] != 'Y' && s1[i][j] != 'Z') {
make_edge(S, i * n + j, s1[i][j] - '0');
make_edge(i * n + j + n * n, T, s2[i][j] - '0');
}
}
cout << flow() << "\n";
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {1, -1, 0, 0};
struct edge {
int a, b, c, f;
};
vector<edge> ed;
vector<int> eds[1000];
int S, T;
int dd[1000];
int pp[1000];
void make_edge(int a, int b, int c) {
edge x;
x.a = a;
x.b = b;
x.c = c;
x.f = 0;
eds[a].push_back(ed.size());
ed.push_back(x);
swap(x.a, x.b);
x.c = 0;
eds[b].push_back(ed.size());
ed.push_back(x);
}
int dfs(int v, int fl, int mn) {
if (v == T) return fl;
if (dd[v] >= dd[T]) return 0;
int now = 0;
for (; fl >= mn && pp[v] != (int)eds[v].size(); ++pp[v]) {
int e = eds[v][pp[v]];
int u = ed[e].b;
if (dd[u] != dd[v] + 1) continue;
int go = ed[e].c - ed[e].f;
if (go < mn) continue;
go = dfs(u, min(fl, go), mn);
fl -= go;
now += go;
ed[e].f += go;
ed[e ^ 1].f -= go;
if (fl == 0) break;
}
return now;
}
int dinic(int mn) {
queue<int> qu;
fill(dd, dd + T + 1, INF);
dd[S] = 0;
qu.push(S);
while (!qu.empty()) {
int x = qu.front();
qu.pop();
for (int e : eds[x]) {
if (ed[e].f + mn > ed[e].c) continue;
int u = ed[e].b;
if (dd[u] > dd[x] + 1) dd[u] = dd[x] + 1, qu.push(u);
}
}
if (dd[T] == INF) return 0;
fill(pp, pp + T + 1, 0);
return dfs(S, INF, mn);
}
int flow() {
int ans = 0;
for (int now = 8; now > 0; now /= 2) {
while (true) {
int x = dinic(now);
if (!x) break;
ans += x;
}
}
return ans;
}
int n, t;
string s1[20];
string s2[20];
void calc(int sx, int sy, int dp[20][20]) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) dp[i][j] = 100000;
queue<pair<int, int> > qu;
qu.push(make_pair(sx, sy));
dp[sx][sy] = 0;
while (!qu.empty()) {
int x, y;
tie(x, y) = qu.front();
qu.pop();
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || s1[nx][ny] == 'Z' ||
s1[nx][ny] == 'Y')
continue;
if (dp[nx][ny] > dp[x][y] + 1)
dp[nx][ny] = dp[x][y] + 1, qu.push(make_pair(nx, ny));
}
}
}
int dp[20][20];
int dp2[20][20];
int main() {
scanf("%d%d", &n, &t);
for (int i = 0; i < n; ++i) cin >> s1[i];
for (int i = 0; i < n; ++i) cin >> s2[i];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (s1[i][j] == 'Z') calc(i, j, dp2);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (s1[i][j] != 'Y' && s1[i][j] != 'Z') {
calc(i, j, dp);
for (int i1 = 0; i1 < n; ++i1)
for (int j1 = 0; j1 < n; ++j1) {
if (dp[i1][j1] > t || dp[i1][j1] > dp2[i1][j1]) continue;
int fl = 0;
for (int x = 0; x < 4; ++x) {
int ni = i1 + dx[x];
int nj = j1 + dy[x];
if (ni < 0 || nj < 0 || ni >= n || nj >= n || s1[ni][nj] == 'Y' ||
s1[ni][nj] == 'Z')
continue;
if (dp[ni][nj] < dp2[ni][nj]) fl = 1;
}
if (dp[i1][j1] < dp2[i1][j1]) fl = 1;
if (fl == 1) make_edge(i * n + j, i1 * n + j1 + n * n, 10000);
}
}
S = 2 * n * n;
T = S + 1;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) {
if (s1[i][j] != 'Y' && s1[i][j] != 'Z') {
make_edge(S, i * n + j, s1[i][j] - '0');
make_edge(i * n + j + n * n, T, s2[i][j] - '0');
}
}
cout << flow() << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, flow, cap, inv;
edge(int to, int flow, int cap, int inv)
: to(to), flow(flow), cap(cap), inv(inv) {}
};
struct graph {
static const int oo = (int)2e9;
int n;
vector<vector<edge> > e;
vector<int> ptr, d;
graph(int n) : n(n), e(n), ptr(n), d(n){};
void add_edge(int a, int b, int cap) {
e[a].push_back(edge(b, 0, cap, e[b].size()));
e[b].push_back(edge(a, 0, 0, e[a].size() - 1));
}
int maxflow(int s, int t) {
int flow = 0;
while (true) {
fill(d.begin(), d.end(), -1);
deque<int> q;
q.push_back(s);
d[s] = 0;
while (!q.empty()) {
int v = q.front();
q.pop_front();
for (size_t i = 0; i < e[v].size(); i++) {
int to = e[v][i].to;
if (e[v][i].flow < e[v][i].cap && d[to] == -1) {
d[to] = d[v] + 1;
if (to == t) {
q.clear();
break;
}
q.push_back(to);
}
}
}
if (d[t] == -1) return flow;
fill(ptr.begin(), ptr.end(), 0);
while (int pushed = dfs(s, oo, t)) flow += pushed;
}
}
int dfs(int v, int flow, int t) {
if (!flow || v == t) return flow;
for (; ptr[v] < (int)e[v].size(); ptr[v]++) {
int to = e[v][ptr[v]].to;
if (d[to] != d[v] + 1) continue;
int pushed = dfs(to, min(flow, e[v][ptr[v]].cap - e[v][ptr[v]].flow), t);
if (pushed) {
e[v][ptr[v]].flow += pushed;
e[to][e[v][ptr[v]].inv].flow -= pushed;
return pushed;
}
}
return 0;
}
};
char mas[110][110], mas2[110][110];
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int main() {
int n, t;
scanf("%d %d", &n, &t);
for (int i = 1; i <= n; i++) scanf("%s", mas[i] + 1);
for (int i = 1; i <= n; i++) scanf("%s", mas2[i] + 1);
vector<pair<int, int> > people, capsules;
int sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (isdigit(mas[i][j]))
for (int k = 0; k < mas[i][j] - '0'; k++)
people.push_back(make_pair(i, j));
if (isdigit(mas2[i][j]))
for (int k = 0; k < mas2[i][j] - '0'; k++)
capsules.push_back(make_pair(i, j));
if (isdigit(mas2[i][j])) sum += mas2[i][j] - '0';
}
int bdi = -1, bdj = -1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (mas[i][j] == 'Z') {
bdi = i;
bdj = j;
}
}
const int oo = (int)2e9;
vector<vector<int> > distbad(n + 2, vector<int>(n + 2, oo));
deque<pair<int, int> > q;
q.push_back(make_pair(bdi, bdj));
distbad[bdi][bdj] = 0;
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop_front();
for (int l = 0; l < 4; l++) {
int yy = cur.first + dy[l];
int xx = cur.second + dx[l];
if (mas[yy][xx] != 0 && isdigit(mas[yy][xx]) && distbad[yy][xx] == oo) {
distbad[yy][xx] = distbad[cur.first][cur.second] + 1;
q.push_back(make_pair(yy, xx));
}
}
}
vector<vector<int> > dist[11][11];
vector<vector<pair<int, int> > > pr[11][11];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
dist[i][j] = vector<vector<int> >(n + 2, vector<int>(n + 2, oo));
pr[i][j] = vector<vector<pair<int, int> > >(
n + 2, vector<pair<int, int> >(n + 2, make_pair(-1, -1)));
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (isdigit(mas[i][j])) {
q.clear();
q.push_back(make_pair(i, j));
dist[i][j][i][j] = 0;
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop_front();
for (int l = 0; l < 4; l++) {
int yy = cur.first + dy[l];
int xx = cur.second + dx[l];
if (mas[yy][xx] != 0 && isdigit(mas[yy][xx]) &&
dist[i][j][yy][xx] == oo &&
distbad[yy][xx] >= dist[i][j][cur.first][cur.second] + 1) {
dist[i][j][yy][xx] = dist[i][j][cur.first][cur.second] + 1;
pr[i][j][yy][xx] = make_pair(cur.first, cur.second);
if (distbad[yy][xx] != dist[i][j][yy][xx])
q.push_back(make_pair(yy, xx));
}
}
}
}
}
}
int nn = n * n;
graph gr(2 * nn + 2);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (mas[i][j] >= '1' && mas[i][j] <= '9')
gr.add_edge(0, (i - 1) * n + j, mas[i][j] - '0');
if (mas2[i][j] >= '1' && mas2[i][j] <= '9')
gr.add_edge(nn + (i - 1) * n + j, 2 * nn + 1, mas2[i][j] - '0');
}
for (int y = 1; y <= n; y++)
for (int x = 1; x <= n; x++)
for (int yy = 1; yy <= n; yy++)
for (int xx = 1; xx <= n; xx++) {
if (!isdigit(mas[y][x]) || !isdigit(mas2[y][x])) continue;
if (dist[y][x][yy][xx] > t) continue;
gr.add_edge((y - 1) * n + x, nn + (yy - 1) * n + xx, oo);
}
printf("%d", gr.maxflow(0, 2 * nn + 1));
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, flow, cap, inv;
edge(int to, int flow, int cap, int inv)
: to(to), flow(flow), cap(cap), inv(inv) {}
};
struct graph {
static const int oo = (int)2e9;
int n;
vector<vector<edge> > e;
vector<int> ptr, d;
graph(int n) : n(n), e(n), ptr(n), d(n){};
void add_edge(int a, int b, int cap) {
e[a].push_back(edge(b, 0, cap, e[b].size()));
e[b].push_back(edge(a, 0, 0, e[a].size() - 1));
}
int maxflow(int s, int t) {
int flow = 0;
while (true) {
fill(d.begin(), d.end(), -1);
deque<int> q;
q.push_back(s);
d[s] = 0;
while (!q.empty()) {
int v = q.front();
q.pop_front();
for (size_t i = 0; i < e[v].size(); i++) {
int to = e[v][i].to;
if (e[v][i].flow < e[v][i].cap && d[to] == -1) {
d[to] = d[v] + 1;
if (to == t) {
q.clear();
break;
}
q.push_back(to);
}
}
}
if (d[t] == -1) return flow;
fill(ptr.begin(), ptr.end(), 0);
while (int pushed = dfs(s, oo, t)) flow += pushed;
}
}
int dfs(int v, int flow, int t) {
if (!flow || v == t) return flow;
for (; ptr[v] < (int)e[v].size(); ptr[v]++) {
int to = e[v][ptr[v]].to;
if (d[to] != d[v] + 1) continue;
int pushed = dfs(to, min(flow, e[v][ptr[v]].cap - e[v][ptr[v]].flow), t);
if (pushed) {
e[v][ptr[v]].flow += pushed;
e[to][e[v][ptr[v]].inv].flow -= pushed;
return pushed;
}
}
return 0;
}
};
char mas[110][110], mas2[110][110];
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int main() {
int n, t;
scanf("%d %d", &n, &t);
for (int i = 1; i <= n; i++) scanf("%s", mas[i] + 1);
for (int i = 1; i <= n; i++) scanf("%s", mas2[i] + 1);
vector<pair<int, int> > people, capsules;
int sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (isdigit(mas[i][j]))
for (int k = 0; k < mas[i][j] - '0'; k++)
people.push_back(make_pair(i, j));
if (isdigit(mas2[i][j]))
for (int k = 0; k < mas2[i][j] - '0'; k++)
capsules.push_back(make_pair(i, j));
if (isdigit(mas2[i][j])) sum += mas2[i][j] - '0';
}
int bdi = -1, bdj = -1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (mas[i][j] == 'Z') {
bdi = i;
bdj = j;
}
}
const int oo = (int)2e9;
vector<vector<int> > distbad(n + 2, vector<int>(n + 2, oo));
deque<pair<int, int> > q;
q.push_back(make_pair(bdi, bdj));
distbad[bdi][bdj] = 0;
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop_front();
for (int l = 0; l < 4; l++) {
int yy = cur.first + dy[l];
int xx = cur.second + dx[l];
if (mas[yy][xx] != 0 && isdigit(mas[yy][xx]) && distbad[yy][xx] == oo) {
distbad[yy][xx] = distbad[cur.first][cur.second] + 1;
q.push_back(make_pair(yy, xx));
}
}
}
vector<vector<int> > dist[11][11];
vector<vector<pair<int, int> > > pr[11][11];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
dist[i][j] = vector<vector<int> >(n + 2, vector<int>(n + 2, oo));
pr[i][j] = vector<vector<pair<int, int> > >(
n + 2, vector<pair<int, int> >(n + 2, make_pair(-1, -1)));
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (isdigit(mas[i][j])) {
q.clear();
q.push_back(make_pair(i, j));
dist[i][j][i][j] = 0;
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop_front();
for (int l = 0; l < 4; l++) {
int yy = cur.first + dy[l];
int xx = cur.second + dx[l];
if (mas[yy][xx] != 0 && isdigit(mas[yy][xx]) &&
dist[i][j][yy][xx] == oo &&
distbad[yy][xx] >= dist[i][j][cur.first][cur.second] + 1) {
dist[i][j][yy][xx] = dist[i][j][cur.first][cur.second] + 1;
pr[i][j][yy][xx] = make_pair(cur.first, cur.second);
if (distbad[yy][xx] != dist[i][j][yy][xx])
q.push_back(make_pair(yy, xx));
}
}
}
}
}
}
int nn = n * n;
graph gr(2 * nn + 2);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (mas[i][j] >= '1' && mas[i][j] <= '9')
gr.add_edge(0, (i - 1) * n + j, mas[i][j] - '0');
if (mas2[i][j] >= '1' && mas2[i][j] <= '9')
gr.add_edge(nn + (i - 1) * n + j, 2 * nn + 1, mas2[i][j] - '0');
}
for (int y = 1; y <= n; y++)
for (int x = 1; x <= n; x++)
for (int yy = 1; yy <= n; yy++)
for (int xx = 1; xx <= n; xx++) {
if (!isdigit(mas[y][x]) || !isdigit(mas2[y][x])) continue;
if (dist[y][x][yy][xx] > t) continue;
gr.add_edge((y - 1) * n + x, nn + (yy - 1) * n + xx, oo);
}
printf("%d", gr.maxflow(0, 2 * nn + 1));
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int v, w;
} edge[21000];
int adj[21000], head[210], e;
void addedge(int u, int v, int w) {
edge[e].v = v;
edge[e].w = w;
adj[e] = head[u];
head[u] = e++;
edge[e].v = u;
edge[e].w = 0;
adj[e] = head[v];
head[v] = e++;
}
int S, T;
int dist[210], q[210 * 2], arc[210], gap[210], inq[210], pre[210];
int SAP(int s, int t) {
memset(arc, -1, sizeof(arc));
memset(pre, -1, sizeof(pre));
int qf = 0, qr = 0;
q[qr++] = t;
inq[t] = 1;
gap[0]++;
dist[t] = 0;
while (qf < qr) {
int u = q[qf++];
for (int i = head[u]; i != -1; i = adj[i])
if (!inq[edge[i].v]) {
int v = edge[i].v;
q[qr++] = v;
inq[v] = 1;
dist[v] = dist[u] + 1;
gap[dist[v]]++;
arc[v] = head[v];
}
}
int low = 10000000, u = s, ans = 0;
pre[s] = s;
while (dist[s] < t) {
bool flag = false;
for (int &i = arc[u]; i != -1; i = adj[i])
if (edge[i].w && dist[edge[i].v] + 1 == dist[u]) {
flag = true;
low = min(low, edge[i].w);
pre[edge[i].v] = u;
u = edge[i].v;
if (u == t) {
while (u != s) {
u = pre[u];
edge[arc[u]].w -= low;
edge[arc[u] ^ 1].w += low;
}
ans += low;
low = 10000000;
}
break;
}
if (flag) continue;
int mindis = t + 1;
for (int i = head[u]; i != -1; i = adj[i])
if (edge[i].w && mindis > dist[edge[i].v]) {
mindis = dist[edge[i].v];
arc[u] = i;
}
gap[dist[u]]--;
if (gap[dist[u]] == 0) return ans;
dist[u] = mindis + 1;
gap[dist[u]]++;
u = pre[u];
}
return ans;
}
char str[12][12], str2[12][12];
int sci[12][12], ca[12][12], stone[62][12][12];
int dp[62][12][12][12][12];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int n, t;
void expand() {
for (int k = 1; k <= t; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
if (stone[k - 1][i][j] == 1)
stone[k][i][j] = 1;
else if (stone[k - 1][i][j] == 2)
stone[k][i][j] = 2;
else {
int nx, ny;
for (int z = 0; z < 4; ++z) {
nx = i + dir[z][0];
ny = j + dir[z][1];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= n) {
if (stone[k - 1][nx][ny] == 2) stone[k][i][j] = 2;
}
}
}
}
}
int vis[12][12];
void dfs(int x, int y, int nowx, int nowy, int k) {
vis[nowx][nowy] = 1;
dp[k][x][y][nowx][nowy] = 1;
if (k >= t || stone[k][nowx][nowy]) return;
int nx, ny;
for (int z = 0; z < 4; ++z) {
nx = nowx + dir[z][0];
ny = nowy + dir[z][1];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && !vis[nx][ny]) {
if (stone[k][nx][ny]) continue;
dfs(x, y, nx, ny, k + 1);
}
}
}
int main() {
scanf("%d%d", &n, &t);
for (int i = 1; i <= n; ++i) {
scanf("%s", str[i] + 1);
for (int j = 1; j <= n; ++j) {
if (str[i][j] == 'Z')
stone[0][i][j] = 2;
else if (str[i][j] == 'Y')
stone[0][i][j] = 1;
else
sci[i][j] = str[i][j] - '0';
}
}
for (int i = 1; i <= n; ++i) {
scanf("%s", str2[i] + 1);
for (int j = 1; j <= n; ++j) {
if (str2[i][j] >= '0' && str2[i][j] <= '9') ca[i][j] = str2[i][j] - '0';
}
}
expand();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (sci[i][j]) {
memset(vis, 0, sizeof(vis));
dfs(i, j, i, j, 0);
}
for (int k = 1; k <= t; ++k)
for (int x = 1; x <= n; ++x)
for (int y = 1; y <= n; ++y)
for (int nx = 1; nx <= n; ++nx)
for (int ny = 1; ny <= n; ++ny) {
dp[k][x][y][nx][ny] += dp[k - 1][x][y][nx][ny];
}
S = 0;
T = n * n * 2 + 1;
memset(head, -1, sizeof(head));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) addedge(S, (i - 1) * n + j, sci[i][j]);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) addedge(n * n + (i - 1) * n + j, T, ca[i][j]);
for (int x = 1; x <= n; ++x)
for (int y = 1; y <= n; ++y)
for (int nx = 1; nx <= n; ++nx)
for (int ny = 1; ny <= n; ++ny)
if (sci[x][y] && ca[nx][ny]) {
if (dp[t][x][y][nx][ny])
addedge((x - 1) * n + y, n * n + (nx - 1) * n + ny, 10000000);
}
int ans = SAP(S, T);
printf("%d\n", ans);
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int v, w;
} edge[21000];
int adj[21000], head[210], e;
void addedge(int u, int v, int w) {
edge[e].v = v;
edge[e].w = w;
adj[e] = head[u];
head[u] = e++;
edge[e].v = u;
edge[e].w = 0;
adj[e] = head[v];
head[v] = e++;
}
int S, T;
int dist[210], q[210 * 2], arc[210], gap[210], inq[210], pre[210];
int SAP(int s, int t) {
memset(arc, -1, sizeof(arc));
memset(pre, -1, sizeof(pre));
int qf = 0, qr = 0;
q[qr++] = t;
inq[t] = 1;
gap[0]++;
dist[t] = 0;
while (qf < qr) {
int u = q[qf++];
for (int i = head[u]; i != -1; i = adj[i])
if (!inq[edge[i].v]) {
int v = edge[i].v;
q[qr++] = v;
inq[v] = 1;
dist[v] = dist[u] + 1;
gap[dist[v]]++;
arc[v] = head[v];
}
}
int low = 10000000, u = s, ans = 0;
pre[s] = s;
while (dist[s] < t) {
bool flag = false;
for (int &i = arc[u]; i != -1; i = adj[i])
if (edge[i].w && dist[edge[i].v] + 1 == dist[u]) {
flag = true;
low = min(low, edge[i].w);
pre[edge[i].v] = u;
u = edge[i].v;
if (u == t) {
while (u != s) {
u = pre[u];
edge[arc[u]].w -= low;
edge[arc[u] ^ 1].w += low;
}
ans += low;
low = 10000000;
}
break;
}
if (flag) continue;
int mindis = t + 1;
for (int i = head[u]; i != -1; i = adj[i])
if (edge[i].w && mindis > dist[edge[i].v]) {
mindis = dist[edge[i].v];
arc[u] = i;
}
gap[dist[u]]--;
if (gap[dist[u]] == 0) return ans;
dist[u] = mindis + 1;
gap[dist[u]]++;
u = pre[u];
}
return ans;
}
char str[12][12], str2[12][12];
int sci[12][12], ca[12][12], stone[62][12][12];
int dp[62][12][12][12][12];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int n, t;
void expand() {
for (int k = 1; k <= t; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
if (stone[k - 1][i][j] == 1)
stone[k][i][j] = 1;
else if (stone[k - 1][i][j] == 2)
stone[k][i][j] = 2;
else {
int nx, ny;
for (int z = 0; z < 4; ++z) {
nx = i + dir[z][0];
ny = j + dir[z][1];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= n) {
if (stone[k - 1][nx][ny] == 2) stone[k][i][j] = 2;
}
}
}
}
}
int vis[12][12];
void dfs(int x, int y, int nowx, int nowy, int k) {
vis[nowx][nowy] = 1;
dp[k][x][y][nowx][nowy] = 1;
if (k >= t || stone[k][nowx][nowy]) return;
int nx, ny;
for (int z = 0; z < 4; ++z) {
nx = nowx + dir[z][0];
ny = nowy + dir[z][1];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && !vis[nx][ny]) {
if (stone[k][nx][ny]) continue;
dfs(x, y, nx, ny, k + 1);
}
}
}
int main() {
scanf("%d%d", &n, &t);
for (int i = 1; i <= n; ++i) {
scanf("%s", str[i] + 1);
for (int j = 1; j <= n; ++j) {
if (str[i][j] == 'Z')
stone[0][i][j] = 2;
else if (str[i][j] == 'Y')
stone[0][i][j] = 1;
else
sci[i][j] = str[i][j] - '0';
}
}
for (int i = 1; i <= n; ++i) {
scanf("%s", str2[i] + 1);
for (int j = 1; j <= n; ++j) {
if (str2[i][j] >= '0' && str2[i][j] <= '9') ca[i][j] = str2[i][j] - '0';
}
}
expand();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (sci[i][j]) {
memset(vis, 0, sizeof(vis));
dfs(i, j, i, j, 0);
}
for (int k = 1; k <= t; ++k)
for (int x = 1; x <= n; ++x)
for (int y = 1; y <= n; ++y)
for (int nx = 1; nx <= n; ++nx)
for (int ny = 1; ny <= n; ++ny) {
dp[k][x][y][nx][ny] += dp[k - 1][x][y][nx][ny];
}
S = 0;
T = n * n * 2 + 1;
memset(head, -1, sizeof(head));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) addedge(S, (i - 1) * n + j, sci[i][j]);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) addedge(n * n + (i - 1) * n + j, T, ca[i][j]);
for (int x = 1; x <= n; ++x)
for (int y = 1; y <= n; ++y)
for (int nx = 1; nx <= n; ++nx)
for (int ny = 1; ny <= n; ++ny)
if (sci[x][y] && ca[nx][ny]) {
if (dp[t][x][y][nx][ny])
addedge((x - 1) * n + y, n * n + (nx - 1) * n + ny, 10000000);
}
int ans = SAP(S, T);
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 10 + 5, inf = 1e9 + 5;
int n, boom;
char sci[N][N], cap[N][N];
int id[N][N];
pair<int, int> reactor;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
bool valid(int r, int c) {
return 1 <= r && r <= n && 1 <= c && c <= n && sci[r][c] != 'Y';
}
int vis[N][N];
int distTox[N][N], dist[N][N], timer = 0;
void reactorSim(pair<int, int> second) {
++timer;
queue<pair<int, int> > q;
q.push(second);
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
vis[r][c] = timer;
if (distTox[r][c] == inf) distTox[r][c] = 0;
for (auto i = (0) - ((0) > (4)); i != 4 - ((0) > (4));
i += 1 - 2 * ((0) > (4))) {
int nr = r + dr[i];
int nc = c + dc[i];
if (valid(nr, nc) && vis[nr][nc] != timer) {
q.emplace(nr, nc);
distTox[nr][nc] = distTox[r][c] + 1;
}
}
}
}
bool reach(pair<int, int> second, pair<int, int> e) {
;
++timer;
memset(dist, 0, sizeof dist);
queue<pair<int, int> > q;
q.push(second);
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
vis[r][c] = timer;
;
;
if (dist[r][c] <= distTox[r][c] && pair<int, int>(r, c) == e &&
dist[r][c] <= boom)
return true;
if (dist[r][c] >= distTox[r][c]) continue;
for (auto i = (0) - ((0) > (4)); i != 4 - ((0) > (4));
i += 1 - 2 * ((0) > (4))) {
int nr = r + dr[i];
int nc = c + dc[i];
if (valid(nr, nc) && vis[nr][nc] != timer) {
q.emplace(nr, nc);
dist[nr][nc] = dist[r][c] + 1;
}
}
}
return false;
}
template <class F>
struct Dinic {
static constexpr F eps = (F)1e-9;
struct Edge {
int v, inv;
F cap, flow;
Edge(int v, F cap, int inv) : v(v), cap(cap), flow(0), inv(inv) {}
};
int second, t, n, m = 0;
vector<vector<Edge> > g;
vector<int> dist, ptr;
Dinic(int n, int ss = -1, int tt = -1)
: n(n), g(n + 5), dist(n + 5), ptr(n + 5) {
second = ss == -1 ? n + 1 : ss;
t = tt == -1 ? n + 2 : tt;
}
void add(int u, int v, F cap) {
g[u].push_back(Edge(v, cap, int(g[v].size())));
g[v].push_back(Edge(u, 0, int(g[u].size()) - 1));
m += 2;
}
bool bfs() {
fill(begin(dist), end(dist), -1);
queue<int> qu({second});
dist[second] = 0;
while (int(qu.size())) {
int u = qu.front();
qu.pop();
for (Edge &e : g[u])
if (e.cap - e.flow > eps)
if (dist[e.v] == -1) {
dist[e.v] = dist[u] + 1;
qu.push(e.v);
}
}
return dist[t] != -1;
}
F dfs(int u, F flow = numeric_limits<F>::max()) {
if (flow <= eps || u == t) return max<F>(0, flow);
for (int &i = ptr[u]; i < int(g[u].size()); i++) {
Edge &e = g[u][i];
if (e.cap - e.flow > eps && dist[u] + 1 == dist[e.v]) {
F nflow = dfs(e.v, min<F>(flow, e.cap - e.flow));
if (nflow > eps) {
e.flow += nflow;
g[e.v][e.inv].flow -= nflow;
return nflow;
}
}
}
return 0;
}
F maxFlow() {
F flow = 0;
while (bfs()) {
fill(begin(ptr), end(ptr), 0);
while (F nflow = dfs(second)) flow += nflow;
}
return flow;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0), cout.tie(0);
while (cin >> n >> boom) {
int cnt = 0;
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
id[r][c] = ++cnt;
cin >> sci[r][c];
if (sci[r][c] == 'Z') reactor = {r, c};
}
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
cin >> cap[r][c];
distTox[r][c] = inf;
}
Dinic<int> g(n * n * 2);
reactorSim(reactor);
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
if (isdigit(sci[r][c])) g.add(g.second, id[r][c], sci[r][c] - '0');
if (isdigit(cap[r][c])) g.add(n * n + id[r][c], g.t, cap[r][c] - '0');
for (auto rr = (1) - ((1) > (n + 1)); rr != n + 1 - ((1) > (n + 1));
rr += 1 - 2 * ((1) > (n + 1)))
for (auto cc = (1) - ((1) > (n + 1)); cc != n + 1 - ((1) > (n + 1));
cc += 1 - 2 * ((1) > (n + 1))) {
if ('1' > sci[r][c] || sci[r][c] > '9') continue;
if ('1' > cap[rr][cc] || cap[rr][cc] > '9') continue;
if (reach({r, c}, {rr, cc}))
g.add(id[r][c], n * n + id[rr][cc], inf);
}
}
int saved = g.maxFlow();
cout << saved << '\n';
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 10 + 5, inf = 1e9 + 5;
int n, boom;
char sci[N][N], cap[N][N];
int id[N][N];
pair<int, int> reactor;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
bool valid(int r, int c) {
return 1 <= r && r <= n && 1 <= c && c <= n && sci[r][c] != 'Y';
}
int vis[N][N];
int distTox[N][N], dist[N][N], timer = 0;
void reactorSim(pair<int, int> second) {
++timer;
queue<pair<int, int> > q;
q.push(second);
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
vis[r][c] = timer;
if (distTox[r][c] == inf) distTox[r][c] = 0;
for (auto i = (0) - ((0) > (4)); i != 4 - ((0) > (4));
i += 1 - 2 * ((0) > (4))) {
int nr = r + dr[i];
int nc = c + dc[i];
if (valid(nr, nc) && vis[nr][nc] != timer) {
q.emplace(nr, nc);
distTox[nr][nc] = distTox[r][c] + 1;
}
}
}
}
bool reach(pair<int, int> second, pair<int, int> e) {
;
++timer;
memset(dist, 0, sizeof dist);
queue<pair<int, int> > q;
q.push(second);
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
vis[r][c] = timer;
;
;
if (dist[r][c] <= distTox[r][c] && pair<int, int>(r, c) == e &&
dist[r][c] <= boom)
return true;
if (dist[r][c] >= distTox[r][c]) continue;
for (auto i = (0) - ((0) > (4)); i != 4 - ((0) > (4));
i += 1 - 2 * ((0) > (4))) {
int nr = r + dr[i];
int nc = c + dc[i];
if (valid(nr, nc) && vis[nr][nc] != timer) {
q.emplace(nr, nc);
dist[nr][nc] = dist[r][c] + 1;
}
}
}
return false;
}
template <class F>
struct Dinic {
static constexpr F eps = (F)1e-9;
struct Edge {
int v, inv;
F cap, flow;
Edge(int v, F cap, int inv) : v(v), cap(cap), flow(0), inv(inv) {}
};
int second, t, n, m = 0;
vector<vector<Edge> > g;
vector<int> dist, ptr;
Dinic(int n, int ss = -1, int tt = -1)
: n(n), g(n + 5), dist(n + 5), ptr(n + 5) {
second = ss == -1 ? n + 1 : ss;
t = tt == -1 ? n + 2 : tt;
}
void add(int u, int v, F cap) {
g[u].push_back(Edge(v, cap, int(g[v].size())));
g[v].push_back(Edge(u, 0, int(g[u].size()) - 1));
m += 2;
}
bool bfs() {
fill(begin(dist), end(dist), -1);
queue<int> qu({second});
dist[second] = 0;
while (int(qu.size())) {
int u = qu.front();
qu.pop();
for (Edge &e : g[u])
if (e.cap - e.flow > eps)
if (dist[e.v] == -1) {
dist[e.v] = dist[u] + 1;
qu.push(e.v);
}
}
return dist[t] != -1;
}
F dfs(int u, F flow = numeric_limits<F>::max()) {
if (flow <= eps || u == t) return max<F>(0, flow);
for (int &i = ptr[u]; i < int(g[u].size()); i++) {
Edge &e = g[u][i];
if (e.cap - e.flow > eps && dist[u] + 1 == dist[e.v]) {
F nflow = dfs(e.v, min<F>(flow, e.cap - e.flow));
if (nflow > eps) {
e.flow += nflow;
g[e.v][e.inv].flow -= nflow;
return nflow;
}
}
}
return 0;
}
F maxFlow() {
F flow = 0;
while (bfs()) {
fill(begin(ptr), end(ptr), 0);
while (F nflow = dfs(second)) flow += nflow;
}
return flow;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0), cout.tie(0);
while (cin >> n >> boom) {
int cnt = 0;
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
id[r][c] = ++cnt;
cin >> sci[r][c];
if (sci[r][c] == 'Z') reactor = {r, c};
}
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
cin >> cap[r][c];
distTox[r][c] = inf;
}
Dinic<int> g(n * n * 2);
reactorSim(reactor);
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
if (isdigit(sci[r][c])) g.add(g.second, id[r][c], sci[r][c] - '0');
if (isdigit(cap[r][c])) g.add(n * n + id[r][c], g.t, cap[r][c] - '0');
for (auto rr = (1) - ((1) > (n + 1)); rr != n + 1 - ((1) > (n + 1));
rr += 1 - 2 * ((1) > (n + 1)))
for (auto cc = (1) - ((1) > (n + 1)); cc != n + 1 - ((1) > (n + 1));
cc += 1 - 2 * ((1) > (n + 1))) {
if ('1' > sci[r][c] || sci[r][c] > '9') continue;
if ('1' > cap[rr][cc] || cap[rr][cc] > '9') continue;
if (reach({r, c}, {rr, cc}))
g.add(id[r][c], n * n + id[rr][cc], inf);
}
}
int saved = g.maxFlow();
cout << saved << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char C[10][10], S[10][10];
int C1[10][10], S1[10][10];
int G[202][202];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
int n, t;
void bfsC();
void bfsS(int sx, int sy);
int getFlow(int a[][202], int v, int s, int t);
int main() {
scanf("%d %d", &n, &t);
for (int(i) = (0); (i) < (n); ++(i)) scanf("%s", S[i]);
for (int(i) = (0); (i) < (n); ++(i)) scanf("%s", C[i]);
bfsC();
int scnt = 1;
for (int(i) = (0); (i) < (n); ++(i)) {
for (int(j) = (0); (j) < (n); ++(j)) {
if (S[i][j] > '0' && S[i][j] <= '9') {
bfsS(i, j);
G[0][scnt] = S[i][j] - '0';
int ccnt = 101;
for (int(k) = (0); (k) < (n); ++(k)) {
for (int(l) = (0); (l) < (n); ++(l)) {
if (C[k][l] > '0' && C[k][l] <= '9') {
if (S1[k][l] < 1000000) G[scnt][ccnt] = 1000000;
G[ccnt][201] = C[k][l] - '0';
}
ccnt++;
}
}
}
scnt++;
}
}
cout << getFlow(G, 202, 0, 201) << endl;
return 0;
}
int getFlow(int a[][202], int v, int s, int t) {
bool done = false;
int flow = 0;
while (!done) {
list<int> l;
vector<bool> visited(v, false);
vector<int> prev(v, -1);
l.push_back(s);
visited[s] = true;
while (!l.empty()) {
int node = l.front();
l.pop_front();
for (int i = 0; i < v && !done; i++) {
if (a[node][i] && !visited[i]) {
l.push_back(i);
prev[i] = node;
visited[i] = true;
if (i == t) {
done = true;
break;
}
}
}
if (done) break;
}
if (!done) break;
done = false;
int tmp = t;
int cap = 100000000;
while (prev[tmp] != -1) {
cap = min(cap, a[prev[tmp]][tmp]);
tmp = prev[tmp];
}
flow += cap;
tmp = t;
while (prev[tmp] != -1) {
a[prev[tmp]][tmp] -= cap;
a[tmp][prev[tmp]] += cap;
tmp = prev[tmp];
}
}
return flow;
}
void bfsC() {
int i, j;
for ((i) = (0); (i) < (n); ++(i))
for ((j) = (0); (j) < (n); ++(j)) C1[i][j] = t;
for (int(k) = (0); (k) < (n); ++(k))
for (int(l) = (0); (l) < (n); ++(l))
if (C[k][l] == 'Z') i = k, j = l;
list<int> q;
q.push_back((i << 4) | j);
C1[i][j] = 0;
while (!q.empty()) {
int nt = q.front();
int x = nt >> 4;
int y = nt & 15;
q.pop_front();
for (int(k) = (0); (k) < (4); ++(k)) {
if (x + dx[k] >= 0 && x + dx[k] < n && y + dy[k] >= 0 && y + dy[k] < n &&
C[x + dx[k]][y + dy[k]] != 'Y' && C[x + dx[k]][y + dy[k]] != 'Z' &&
C1[x + dx[k]][y + dy[k]] > C1[x][y] + 1) {
C1[x + dx[k]][y + dy[k]] = C1[x][y] + 1;
q.push_back(((x + dx[k]) << 4) | (y + dy[k]));
}
}
}
return;
}
void bfsS(int sx, int sy) {
list<int> q;
memset(S1, 1000000, sizeof(S1));
q.push_back((sx << 4) | sy);
S1[sx][sy] = 0;
while (!q.empty()) {
int nt = q.front();
int x = nt >> 4;
int y = nt & 15;
q.pop_front();
for (int(i) = (0); (i) < (4); ++(i)) {
if (x + dx[i] >= 0 && x + dx[i] < n && y + dy[i] >= 0 && y + dy[i] < n &&
S[x + dx[i]][y + dy[i]] != 'Y' && S[x + dx[i]][y + dy[i]] != 'Z' &&
S1[x + dx[i]][y + dy[i]] > S1[x][y] + 1) {
if (S1[x][y] + 1 < C1[x + dx[i]][y + dy[i]] ||
(S1[x][y] + 1 == C1[x + dx[i]][y + dy[i]] &&
C[x + dx[i]][y + dy[i]] > '0' && C[x + dx[i]][y + dy[i]] <= '9')) {
S1[x + dx[i]][y + dy[i]] = S1[x][y] + 1;
if (S1[x][y] + 1 < C1[x + dx[i]][y + dy[i]])
q.push_back(((x + dx[i]) << 4) | (y + dy[i]));
}
}
}
}
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char C[10][10], S[10][10];
int C1[10][10], S1[10][10];
int G[202][202];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
int n, t;
void bfsC();
void bfsS(int sx, int sy);
int getFlow(int a[][202], int v, int s, int t);
int main() {
scanf("%d %d", &n, &t);
for (int(i) = (0); (i) < (n); ++(i)) scanf("%s", S[i]);
for (int(i) = (0); (i) < (n); ++(i)) scanf("%s", C[i]);
bfsC();
int scnt = 1;
for (int(i) = (0); (i) < (n); ++(i)) {
for (int(j) = (0); (j) < (n); ++(j)) {
if (S[i][j] > '0' && S[i][j] <= '9') {
bfsS(i, j);
G[0][scnt] = S[i][j] - '0';
int ccnt = 101;
for (int(k) = (0); (k) < (n); ++(k)) {
for (int(l) = (0); (l) < (n); ++(l)) {
if (C[k][l] > '0' && C[k][l] <= '9') {
if (S1[k][l] < 1000000) G[scnt][ccnt] = 1000000;
G[ccnt][201] = C[k][l] - '0';
}
ccnt++;
}
}
}
scnt++;
}
}
cout << getFlow(G, 202, 0, 201) << endl;
return 0;
}
int getFlow(int a[][202], int v, int s, int t) {
bool done = false;
int flow = 0;
while (!done) {
list<int> l;
vector<bool> visited(v, false);
vector<int> prev(v, -1);
l.push_back(s);
visited[s] = true;
while (!l.empty()) {
int node = l.front();
l.pop_front();
for (int i = 0; i < v && !done; i++) {
if (a[node][i] && !visited[i]) {
l.push_back(i);
prev[i] = node;
visited[i] = true;
if (i == t) {
done = true;
break;
}
}
}
if (done) break;
}
if (!done) break;
done = false;
int tmp = t;
int cap = 100000000;
while (prev[tmp] != -1) {
cap = min(cap, a[prev[tmp]][tmp]);
tmp = prev[tmp];
}
flow += cap;
tmp = t;
while (prev[tmp] != -1) {
a[prev[tmp]][tmp] -= cap;
a[tmp][prev[tmp]] += cap;
tmp = prev[tmp];
}
}
return flow;
}
void bfsC() {
int i, j;
for ((i) = (0); (i) < (n); ++(i))
for ((j) = (0); (j) < (n); ++(j)) C1[i][j] = t;
for (int(k) = (0); (k) < (n); ++(k))
for (int(l) = (0); (l) < (n); ++(l))
if (C[k][l] == 'Z') i = k, j = l;
list<int> q;
q.push_back((i << 4) | j);
C1[i][j] = 0;
while (!q.empty()) {
int nt = q.front();
int x = nt >> 4;
int y = nt & 15;
q.pop_front();
for (int(k) = (0); (k) < (4); ++(k)) {
if (x + dx[k] >= 0 && x + dx[k] < n && y + dy[k] >= 0 && y + dy[k] < n &&
C[x + dx[k]][y + dy[k]] != 'Y' && C[x + dx[k]][y + dy[k]] != 'Z' &&
C1[x + dx[k]][y + dy[k]] > C1[x][y] + 1) {
C1[x + dx[k]][y + dy[k]] = C1[x][y] + 1;
q.push_back(((x + dx[k]) << 4) | (y + dy[k]));
}
}
}
return;
}
void bfsS(int sx, int sy) {
list<int> q;
memset(S1, 1000000, sizeof(S1));
q.push_back((sx << 4) | sy);
S1[sx][sy] = 0;
while (!q.empty()) {
int nt = q.front();
int x = nt >> 4;
int y = nt & 15;
q.pop_front();
for (int(i) = (0); (i) < (4); ++(i)) {
if (x + dx[i] >= 0 && x + dx[i] < n && y + dy[i] >= 0 && y + dy[i] < n &&
S[x + dx[i]][y + dy[i]] != 'Y' && S[x + dx[i]][y + dy[i]] != 'Z' &&
S1[x + dx[i]][y + dy[i]] > S1[x][y] + 1) {
if (S1[x][y] + 1 < C1[x + dx[i]][y + dy[i]] ||
(S1[x][y] + 1 == C1[x + dx[i]][y + dy[i]] &&
C[x + dx[i]][y + dy[i]] > '0' && C[x + dx[i]][y + dy[i]] <= '9')) {
S1[x + dx[i]][y + dy[i]] = S1[x][y] + 1;
if (S1[x][y] + 1 < C1[x + dx[i]][y + dy[i]])
q.push_back(((x + dx[i]) << 4) | (y + dy[i]));
}
}
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int a[20][20], deep[210], g[210][210];
int tmp[20][20], vis[20][20];
char buf[20];
vector<pair<pair<int, int>, int> > start, End;
queue<pair<pair<int, int>, int> > q1, q2;
queue<int> q;
pair<int, int> pos;
int n, m, t;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
string read() {
scanf("%s", buf);
return buf;
}
bool IN(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
int can_out(pair<int, int> s, pair<int, int> e) {
if (s == e) return 1;
while (!q1.empty()) q1.pop();
while (!q2.empty()) q2.pop();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
tmp[i][j] = a[i][j];
}
}
memset(vis, 0, sizeof(vis));
q1.push(make_pair(pos, 0));
q2.push(make_pair(s, 0)), vis[s.first][s.second] = 1;
for (int p = 1; p <= t; p++) {
if (tmp[e.first][e.second]) return 0;
while (!q2.empty() && q2.front().second == p - 1) {
pair<pair<int, int>, int> top = q2.front();
q2.pop();
int x = top.first.first, y = top.first.second, time = top.second + 1;
if (tmp[x][y]) continue;
for (int i = 0; i < 4; i++) {
int xx = x + dir[i][0], yy = y + dir[i][1];
if (!IN(xx, yy) || tmp[xx][yy] || vis[xx][yy]) continue;
vis[xx][yy] = 1, q2.push(make_pair(make_pair(xx, yy), time));
if (make_pair(xx, yy) == e) return 1;
}
}
while (!q1.empty() && q1.front().second == p - 1) {
pair<pair<int, int>, int> top = q1.front();
q1.pop();
int x = top.first.first, y = top.first.second, time = top.second + 1;
for (int i = 0; i < 4; i++) {
int xx = x + dir[i][0], yy = y + dir[i][1];
if (!IN(xx, yy) || tmp[xx][yy]) continue;
tmp[xx][yy] = 1, q1.push(make_pair(make_pair(xx, yy), time));
}
}
}
return 0;
}
bool bfs(int s, int t) {
memset(deep, -1, sizeof(deep));
q.push(s), deep[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i <= t; i++) {
if (~deep[i] || g[u][i] <= 0) continue;
deep[i] = deep[u] + 1, q.push(i);
}
}
return deep[t] != -1;
}
int dfs(int x, int flow, int t) {
if (x == t || !flow) return flow;
long long res = 0;
for (int i = 0; i <= t; i++) {
if (deep[i] != deep[x] + 1 || g[x][i] == 0) continue;
long long tmp = dfs(i, min(flow, g[x][i]), t);
if (tmp == 0) continue;
g[x][i] -= tmp, g[i][x] += tmp;
res += tmp, flow -= tmp;
if (flow == 0) break;
}
return res;
}
int max_flow(int s, int t) {
int ans = 0;
while (bfs(s, t)) {
ans += dfs(s, INF, t);
}
return ans;
}
int main() {
scanf("%d%d", &n, &t);
m = n;
for (int i = 0; i < n; i++) {
string s = read();
for (int j = 0; j < m; j++) {
if (s[j] == 'Z') pos = make_pair(i, j);
if (s[j] == 'Y' || s[j] == 'Z')
a[i][j] = 1;
else if (s[j] != '0')
start.push_back(make_pair(make_pair(i, j), s[j] - '0'));
}
}
for (int i = 0; i < n; i++) {
string s = read();
for (int j = 0; j < m; j++) {
if (s[j] == 'Y' || s[j] == 'Z')
a[i][j] = 1;
else if (s[j] != '0')
End.push_back(make_pair(make_pair(i, j), s[j] - '0'));
}
}
for (int i = 0; i < start.size(); i++) {
pair<int, int> ps = start[i].first;
for (int j = 0; j < End.size(); j++) {
pair<int, int> pe = End[j].first;
if (can_out(ps, pe)) g[i + 1][j + start.size() + 1] = INF;
}
}
int s = 0, t = start.size() + End.size() + 1;
for (int i = 0; i < start.size(); i++) {
g[s][i + 1] = start[i].second;
}
for (int i = 0; i < End.size(); i++) {
g[i + start.size() + 1][t] = End[i].second;
}
printf("%d\n", max_flow(s, t));
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int a[20][20], deep[210], g[210][210];
int tmp[20][20], vis[20][20];
char buf[20];
vector<pair<pair<int, int>, int> > start, End;
queue<pair<pair<int, int>, int> > q1, q2;
queue<int> q;
pair<int, int> pos;
int n, m, t;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
string read() {
scanf("%s", buf);
return buf;
}
bool IN(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
int can_out(pair<int, int> s, pair<int, int> e) {
if (s == e) return 1;
while (!q1.empty()) q1.pop();
while (!q2.empty()) q2.pop();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
tmp[i][j] = a[i][j];
}
}
memset(vis, 0, sizeof(vis));
q1.push(make_pair(pos, 0));
q2.push(make_pair(s, 0)), vis[s.first][s.second] = 1;
for (int p = 1; p <= t; p++) {
if (tmp[e.first][e.second]) return 0;
while (!q2.empty() && q2.front().second == p - 1) {
pair<pair<int, int>, int> top = q2.front();
q2.pop();
int x = top.first.first, y = top.first.second, time = top.second + 1;
if (tmp[x][y]) continue;
for (int i = 0; i < 4; i++) {
int xx = x + dir[i][0], yy = y + dir[i][1];
if (!IN(xx, yy) || tmp[xx][yy] || vis[xx][yy]) continue;
vis[xx][yy] = 1, q2.push(make_pair(make_pair(xx, yy), time));
if (make_pair(xx, yy) == e) return 1;
}
}
while (!q1.empty() && q1.front().second == p - 1) {
pair<pair<int, int>, int> top = q1.front();
q1.pop();
int x = top.first.first, y = top.first.second, time = top.second + 1;
for (int i = 0; i < 4; i++) {
int xx = x + dir[i][0], yy = y + dir[i][1];
if (!IN(xx, yy) || tmp[xx][yy]) continue;
tmp[xx][yy] = 1, q1.push(make_pair(make_pair(xx, yy), time));
}
}
}
return 0;
}
bool bfs(int s, int t) {
memset(deep, -1, sizeof(deep));
q.push(s), deep[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i <= t; i++) {
if (~deep[i] || g[u][i] <= 0) continue;
deep[i] = deep[u] + 1, q.push(i);
}
}
return deep[t] != -1;
}
int dfs(int x, int flow, int t) {
if (x == t || !flow) return flow;
long long res = 0;
for (int i = 0; i <= t; i++) {
if (deep[i] != deep[x] + 1 || g[x][i] == 0) continue;
long long tmp = dfs(i, min(flow, g[x][i]), t);
if (tmp == 0) continue;
g[x][i] -= tmp, g[i][x] += tmp;
res += tmp, flow -= tmp;
if (flow == 0) break;
}
return res;
}
int max_flow(int s, int t) {
int ans = 0;
while (bfs(s, t)) {
ans += dfs(s, INF, t);
}
return ans;
}
int main() {
scanf("%d%d", &n, &t);
m = n;
for (int i = 0; i < n; i++) {
string s = read();
for (int j = 0; j < m; j++) {
if (s[j] == 'Z') pos = make_pair(i, j);
if (s[j] == 'Y' || s[j] == 'Z')
a[i][j] = 1;
else if (s[j] != '0')
start.push_back(make_pair(make_pair(i, j), s[j] - '0'));
}
}
for (int i = 0; i < n; i++) {
string s = read();
for (int j = 0; j < m; j++) {
if (s[j] == 'Y' || s[j] == 'Z')
a[i][j] = 1;
else if (s[j] != '0')
End.push_back(make_pair(make_pair(i, j), s[j] - '0'));
}
}
for (int i = 0; i < start.size(); i++) {
pair<int, int> ps = start[i].first;
for (int j = 0; j < End.size(); j++) {
pair<int, int> pe = End[j].first;
if (can_out(ps, pe)) g[i + 1][j + start.size() + 1] = INF;
}
}
int s = 0, t = start.size() + End.size() + 1;
for (int i = 0; i < start.size(); i++) {
g[s][i + 1] = start[i].second;
}
for (int i = 0; i < End.size(); i++) {
g[i + start.size() + 1][t] = End[i].second;
}
printf("%d\n", max_flow(s, t));
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char s1[15][15];
char s2[15][15];
int tim[15][15];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, t;
int start = 0, ex = 1;
const int maxn = 100010;
const int maxm = 400110;
const int INF = 0X3f3f3f3f;
struct Edge {
int to, next, cap, flow;
} edge[maxm];
int tol;
int head[maxn];
int gap[maxn], dep[maxn], cur[maxn];
void init() {
tol = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w, int rw = 0) {
edge[tol].to = v;
edge[tol].cap = w;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = rw;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
int Q[maxn];
void BFS(int start, int end) {
memset(dep, -1, sizeof(dep));
memset(gap, 0, sizeof(gap));
gap[0] = 1;
int front = 0, rear = 0;
dep[end] = 0;
Q[rear++] = end;
while (front != rear) {
int u = Q[front++];
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (dep[v] != -1) continue;
Q[rear++] = v;
dep[v] = dep[u] + 1;
gap[dep[v]]++;
}
}
}
int S[maxn];
int sap(int start, int end, int N) {
BFS(start, end);
memcpy(cur, head, sizeof(head));
int top = 0;
int u = start;
int ans = 0;
while (dep[start] < N) {
if (u == end) {
int Min = INF;
int inser;
for (int i = 0; i < top; ++i)
if (Min > edge[S[i]].cap - edge[S[i]].flow) {
Min = edge[S[i]].cap - edge[S[i]].flow;
inser = i;
}
for (int i = 0; i < top; ++i) {
edge[S[i]].flow += Min;
edge[S[i] ^ 1].flow -= Min;
}
ans += Min;
top = inser;
u = edge[S[top] ^ 1].to;
continue;
}
bool flag = false;
int v;
for (int i = cur[u]; i != -1; i = edge[i].next) {
v = edge[i].to;
if (edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u]) {
flag = true;
cur[u] = i;
break;
}
}
if (flag) {
S[top++] = cur[u];
u = v;
continue;
}
int Min = N;
for (int i = head[u]; i != -1; i = edge[i].next)
if (edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) {
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if (!gap[dep[u]]) return ans;
dep[u] = Min + 1;
gap[dep[u]]++;
if (u != start) u = edge[S[--top] ^ 1].to;
}
return ans;
}
int cnt;
pair<int, int> safe[maxn];
void mark_safe() {
cnt = 2;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (s2[i][j] > '0' && s2[i][j] <= '9') {
addedge(cnt, ex, s2[i][j] - '0');
safe[cnt].first = i;
safe[cnt++].second = j;
}
}
}
}
void bfs_bomb() {
int xx, yy;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (s1[i][j] == 'Z') {
xx = i;
yy = j;
}
}
}
queue<pair<int, int> > Q;
int nx, ny, x, y;
while (!Q.empty()) Q.pop();
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) tim[i][j] = INF;
Q.push(pair<int, int>(xx, yy));
tim[xx][yy] = 0;
while (!Q.empty()) {
x = Q.front().first, y = Q.front().second;
Q.pop();
for (int i = 0; i < 4; ++i) {
nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || s1[nx][ny] == 'Y') continue;
if (tim[nx][ny] <= tim[x][y] + 1) continue;
tim[nx][ny] = tim[x][y] + 1;
Q.push(pair<int, int>(nx, ny));
}
}
}
int dis[15][15];
void bfs_man(int xx, int yy) {
queue<pair<int, int> > Q;
while (!Q.empty()) Q.pop();
Q.push(pair<int, int>(xx, yy));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) dis[i][j] = INF;
dis[xx][yy] = 0;
int nx, ny, x, y;
while (!Q.empty()) {
x = Q.front().first, y = Q.front().second;
Q.pop();
if (tim[x][y] <= dis[x][y]) continue;
for (int i = 0; i < 4; ++i) {
nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || s2[nx][ny] == 'Y' ||
s2[nx][ny] == 'Z')
continue;
if (dis[nx][ny] <= dis[x][y] + 1) continue;
if (tim[nx][ny] < dis[x][y] + 1) continue;
dis[nx][ny] = dis[x][y] + 1;
Q.push(pair<int, int>(nx, ny));
}
}
}
int main() {
init();
scanf("%d%d", &n, &t);
for (int i = 0; i < n; ++i) scanf("%s", s1[i]);
for (int i = 0; i < n; ++i) scanf("%s", s2[i]);
mark_safe();
int cx = cnt;
bfs_bomb();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (s1[i][j] > '0' && s1[i][j] <= '9') {
addedge(start, cnt, s1[i][j] - '0');
cnt++;
bfs_man(i, j);
for (int k = 2; k < cx; ++k) {
if (dis[safe[k].first][safe[k].second] <= t) {
addedge(cnt - 1, k, INF);
}
}
}
}
}
printf("%d\n", sap(start, ex, cnt));
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s1[15][15];
char s2[15][15];
int tim[15][15];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, t;
int start = 0, ex = 1;
const int maxn = 100010;
const int maxm = 400110;
const int INF = 0X3f3f3f3f;
struct Edge {
int to, next, cap, flow;
} edge[maxm];
int tol;
int head[maxn];
int gap[maxn], dep[maxn], cur[maxn];
void init() {
tol = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w, int rw = 0) {
edge[tol].to = v;
edge[tol].cap = w;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = rw;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
int Q[maxn];
void BFS(int start, int end) {
memset(dep, -1, sizeof(dep));
memset(gap, 0, sizeof(gap));
gap[0] = 1;
int front = 0, rear = 0;
dep[end] = 0;
Q[rear++] = end;
while (front != rear) {
int u = Q[front++];
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (dep[v] != -1) continue;
Q[rear++] = v;
dep[v] = dep[u] + 1;
gap[dep[v]]++;
}
}
}
int S[maxn];
int sap(int start, int end, int N) {
BFS(start, end);
memcpy(cur, head, sizeof(head));
int top = 0;
int u = start;
int ans = 0;
while (dep[start] < N) {
if (u == end) {
int Min = INF;
int inser;
for (int i = 0; i < top; ++i)
if (Min > edge[S[i]].cap - edge[S[i]].flow) {
Min = edge[S[i]].cap - edge[S[i]].flow;
inser = i;
}
for (int i = 0; i < top; ++i) {
edge[S[i]].flow += Min;
edge[S[i] ^ 1].flow -= Min;
}
ans += Min;
top = inser;
u = edge[S[top] ^ 1].to;
continue;
}
bool flag = false;
int v;
for (int i = cur[u]; i != -1; i = edge[i].next) {
v = edge[i].to;
if (edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u]) {
flag = true;
cur[u] = i;
break;
}
}
if (flag) {
S[top++] = cur[u];
u = v;
continue;
}
int Min = N;
for (int i = head[u]; i != -1; i = edge[i].next)
if (edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) {
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if (!gap[dep[u]]) return ans;
dep[u] = Min + 1;
gap[dep[u]]++;
if (u != start) u = edge[S[--top] ^ 1].to;
}
return ans;
}
int cnt;
pair<int, int> safe[maxn];
void mark_safe() {
cnt = 2;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (s2[i][j] > '0' && s2[i][j] <= '9') {
addedge(cnt, ex, s2[i][j] - '0');
safe[cnt].first = i;
safe[cnt++].second = j;
}
}
}
}
void bfs_bomb() {
int xx, yy;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (s1[i][j] == 'Z') {
xx = i;
yy = j;
}
}
}
queue<pair<int, int> > Q;
int nx, ny, x, y;
while (!Q.empty()) Q.pop();
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) tim[i][j] = INF;
Q.push(pair<int, int>(xx, yy));
tim[xx][yy] = 0;
while (!Q.empty()) {
x = Q.front().first, y = Q.front().second;
Q.pop();
for (int i = 0; i < 4; ++i) {
nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || s1[nx][ny] == 'Y') continue;
if (tim[nx][ny] <= tim[x][y] + 1) continue;
tim[nx][ny] = tim[x][y] + 1;
Q.push(pair<int, int>(nx, ny));
}
}
}
int dis[15][15];
void bfs_man(int xx, int yy) {
queue<pair<int, int> > Q;
while (!Q.empty()) Q.pop();
Q.push(pair<int, int>(xx, yy));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) dis[i][j] = INF;
dis[xx][yy] = 0;
int nx, ny, x, y;
while (!Q.empty()) {
x = Q.front().first, y = Q.front().second;
Q.pop();
if (tim[x][y] <= dis[x][y]) continue;
for (int i = 0; i < 4; ++i) {
nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || s2[nx][ny] == 'Y' ||
s2[nx][ny] == 'Z')
continue;
if (dis[nx][ny] <= dis[x][y] + 1) continue;
if (tim[nx][ny] < dis[x][y] + 1) continue;
dis[nx][ny] = dis[x][y] + 1;
Q.push(pair<int, int>(nx, ny));
}
}
}
int main() {
init();
scanf("%d%d", &n, &t);
for (int i = 0; i < n; ++i) scanf("%s", s1[i]);
for (int i = 0; i < n; ++i) scanf("%s", s2[i]);
mark_safe();
int cx = cnt;
bfs_bomb();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (s1[i][j] > '0' && s1[i][j] <= '9') {
addedge(start, cnt, s1[i][j] - '0');
cnt++;
bfs_man(i, j);
for (int k = 2; k < cx; ++k) {
if (dis[safe[k].first][safe[k].second] <= t) {
addedge(cnt - 1, k, INF);
}
}
}
}
}
printf("%d\n", sap(start, ex, cnt));
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 210;
char a[maxn][maxn], ss[maxn][maxn], can[maxn][maxn][2];
int och[200][2], was[maxn][maxn], n, q[maxn][maxn];
void bfs(int x, int y, int t) {
int b, e;
b = e = 1;
och[b][0] = x;
och[b][1] = y;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int tx, ty, i, j;
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j) was[i][j] = -1;
was[x][y] = 0;
while (b <= e) {
x = och[b][0];
y = och[b][1];
if (was[x][y] == t) {
++b;
continue;
}
for (i = 0; i < 4; ++i) {
tx = x + dx[i];
ty = y + dy[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= n || a[tx][ty] == 'Y') continue;
if (was[tx][ty] == -1) {
was[tx][ty] = was[x][y] + 1;
och[++e][0] = tx;
och[e][1] = ty;
}
}
++b;
}
}
struct edge {
int to;
int cap;
};
vector<edge> e;
vector<int> v[maxn];
int flag[maxn] = {0}, pos[maxn] = {0};
void addE(int from, int to, int d) {
edge ee;
ee.to = to;
ee.cap = d;
e.push_back(ee);
v[from].push_back(e.size() - 1);
ee.to = from;
ee.cap = 0;
e.push_back(ee);
v[to].push_back(e.size() - 1);
}
int s, t, mn;
bool dfs(int w) {
int i, reb, u, tmp;
flag[w] = 1;
if (w == t) return true;
for (i = pos[w]; i < v[w].size(); i++) {
reb = v[w][i];
u = e[reb].to;
tmp = mn;
if (e[reb].cap > 0 && e[reb].cap < mn) mn = e[reb].cap;
if (e[reb].cap > 0 && (flag[u] == 0 && dfs(u))) {
e[reb].cap -= mn;
e[reb ^ 1].cap += mn;
pos[w] = i + 1;
return true;
}
mn = tmp;
}
for (i = 0; i < pos[w]; i++) {
reb = v[w][i];
u = e[reb].to;
if (e[reb].cap > 0 && (flag[u] == 0 && dfs(u))) {
e[reb].cap -= mn;
e[reb ^ 1].cap += mn;
pos[w] = i + 1;
return true;
}
}
return false;
}
const int inf = 0x3f3f3f3f;
int flow() {
int i, fl = 1, res = 0;
while (fl) {
fl = 0;
for (i = 0; i <= t; i++) {
flag[i] = 0;
pos[i] = 0;
}
while (1) {
mn = inf;
if (!dfs(s)) break;
fl = 1;
flag[s] = flag[t] = 0;
res += mn;
}
}
return res;
}
void bfs2(int x, int y, int t) {
int fx, fy;
fx = x;
fy = y;
int cap = a[x][y] - '0';
int b, e;
b = e = 1;
och[b][0] = x;
och[b][1] = y;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int tx, ty, i, j;
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j) q[i][j] = -1;
q[x][y] = 0;
addE(fx * n + fy, fx * n + fy + n * n, cap);
while (b <= e) {
x = och[b][0];
y = och[b][1];
if (q[x][y] == t || q[x][y] == was[x][y]) {
++b;
continue;
}
for (i = 0; i < 4; ++i) {
tx = x + dx[i];
ty = y + dy[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= n || a[tx][ty] == 'Z' ||
a[tx][ty] == 'Y')
continue;
if (q[tx][ty] == -1 &&
(q[x][y] + 1 <= was[tx][ty] || was[tx][ty] == -1)) {
q[tx][ty] = q[x][y] + 1;
addE(fx * n + fy, tx * n + ty + n * n, cap);
och[++e][0] = tx;
och[e][1] = ty;
}
}
++b;
}
}
int main() {
int T, i, j, x, y;
cin >> n >> T;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
cin >> a[i][j];
if (a[i][j] == 'Z') {
x = i;
y = j;
}
}
}
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) cin >> ss[i][j];
}
bfs(x, y, T);
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (a[i][j] >= '0' && a[i][j] <= '9') {
bfs2(i, j, T);
}
}
}
s = n * n * 2 + 1;
t = n * n * 2 + 2;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (a[i][j] >= '0' && a[i][j] <= '9') addE(s, i * n + j, a[i][j] - '0');
if (ss[i][j] >= '1' && ss[i][j] <= '9')
addE(i * n + j + n * n, t, ss[i][j] - '0');
}
}
int res = flow();
cout << res;
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 210;
char a[maxn][maxn], ss[maxn][maxn], can[maxn][maxn][2];
int och[200][2], was[maxn][maxn], n, q[maxn][maxn];
void bfs(int x, int y, int t) {
int b, e;
b = e = 1;
och[b][0] = x;
och[b][1] = y;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int tx, ty, i, j;
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j) was[i][j] = -1;
was[x][y] = 0;
while (b <= e) {
x = och[b][0];
y = och[b][1];
if (was[x][y] == t) {
++b;
continue;
}
for (i = 0; i < 4; ++i) {
tx = x + dx[i];
ty = y + dy[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= n || a[tx][ty] == 'Y') continue;
if (was[tx][ty] == -1) {
was[tx][ty] = was[x][y] + 1;
och[++e][0] = tx;
och[e][1] = ty;
}
}
++b;
}
}
struct edge {
int to;
int cap;
};
vector<edge> e;
vector<int> v[maxn];
int flag[maxn] = {0}, pos[maxn] = {0};
void addE(int from, int to, int d) {
edge ee;
ee.to = to;
ee.cap = d;
e.push_back(ee);
v[from].push_back(e.size() - 1);
ee.to = from;
ee.cap = 0;
e.push_back(ee);
v[to].push_back(e.size() - 1);
}
int s, t, mn;
bool dfs(int w) {
int i, reb, u, tmp;
flag[w] = 1;
if (w == t) return true;
for (i = pos[w]; i < v[w].size(); i++) {
reb = v[w][i];
u = e[reb].to;
tmp = mn;
if (e[reb].cap > 0 && e[reb].cap < mn) mn = e[reb].cap;
if (e[reb].cap > 0 && (flag[u] == 0 && dfs(u))) {
e[reb].cap -= mn;
e[reb ^ 1].cap += mn;
pos[w] = i + 1;
return true;
}
mn = tmp;
}
for (i = 0; i < pos[w]; i++) {
reb = v[w][i];
u = e[reb].to;
if (e[reb].cap > 0 && (flag[u] == 0 && dfs(u))) {
e[reb].cap -= mn;
e[reb ^ 1].cap += mn;
pos[w] = i + 1;
return true;
}
}
return false;
}
const int inf = 0x3f3f3f3f;
int flow() {
int i, fl = 1, res = 0;
while (fl) {
fl = 0;
for (i = 0; i <= t; i++) {
flag[i] = 0;
pos[i] = 0;
}
while (1) {
mn = inf;
if (!dfs(s)) break;
fl = 1;
flag[s] = flag[t] = 0;
res += mn;
}
}
return res;
}
void bfs2(int x, int y, int t) {
int fx, fy;
fx = x;
fy = y;
int cap = a[x][y] - '0';
int b, e;
b = e = 1;
och[b][0] = x;
och[b][1] = y;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int tx, ty, i, j;
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j) q[i][j] = -1;
q[x][y] = 0;
addE(fx * n + fy, fx * n + fy + n * n, cap);
while (b <= e) {
x = och[b][0];
y = och[b][1];
if (q[x][y] == t || q[x][y] == was[x][y]) {
++b;
continue;
}
for (i = 0; i < 4; ++i) {
tx = x + dx[i];
ty = y + dy[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= n || a[tx][ty] == 'Z' ||
a[tx][ty] == 'Y')
continue;
if (q[tx][ty] == -1 &&
(q[x][y] + 1 <= was[tx][ty] || was[tx][ty] == -1)) {
q[tx][ty] = q[x][y] + 1;
addE(fx * n + fy, tx * n + ty + n * n, cap);
och[++e][0] = tx;
och[e][1] = ty;
}
}
++b;
}
}
int main() {
int T, i, j, x, y;
cin >> n >> T;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
cin >> a[i][j];
if (a[i][j] == 'Z') {
x = i;
y = j;
}
}
}
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) cin >> ss[i][j];
}
bfs(x, y, T);
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (a[i][j] >= '0' && a[i][j] <= '9') {
bfs2(i, j, T);
}
}
}
s = n * n * 2 + 1;
t = n * n * 2 + 2;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (a[i][j] >= '0' && a[i][j] <= '9') addE(s, i * n + j, a[i][j] - '0');
if (ss[i][j] >= '1' && ss[i][j] <= '9')
addE(i * n + j + n * n, t, ss[i][j] - '0');
}
}
int res = flow();
cout << res;
return 0;
}
``` |
#include <bits/stdc++.h>
const int N = 12;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
int n, m, d[N][N], q[N * N][2], sx, sy, q1, q2, v[N][N], x, y;
char a[N][N], b[N][N];
struct edge {
int p, pre, cap;
} ed[N * N * N * N * 2];
int la[N * N * 2], num[N * N * 2], le[N * N * 2], ee, ns, nt;
int dfs(int x, int fx) {
if (x == nt) return fx;
int sum = 0, del;
for (int j = la[x]; j > -1; j = ed[j].pre)
if (le[x] == le[ed[j].p] + 1 && ed[j].cap > 0) {
if (ed[j].cap > fx)
del = fx;
else
del = ed[j].cap;
del = dfs(ed[j].p, del);
if (del == 0) continue;
sum += del;
fx -= del;
ed[j].cap -= del;
ed[j ^ 1].cap += del;
if (fx == 0) return sum;
}
if (le[ns] >= nt) return sum;
if (sum == 0) {
num[le[x]]--;
if (num[le[x]] == 0) le[ns] = nt;
le[x]++;
num[le[x]]++;
}
return sum;
}
void add(int x, int y, int ca) {
ee++;
ed[ee].pre = la[x];
ed[ee].p = y;
ed[ee].cap = ca;
la[x] = ee;
}
int t;
int main() {
scanf("%d%d", &n, &t);
for (int i = 0; i < n; i++) scanf("%s", a[i]);
for (int i = 0; i < n; i++) {
scanf("%s", b[i]);
for (int j = 0; j < n; j++)
if (b[i][j] == 'Z') {
sx = i;
sy = j;
}
}
q1 = 0;
q2 = 1;
q[1][0] = sx;
q[1][1] = sy;
memset(d, -1, sizeof(d));
d[sx][sy] = 0;
while (q1 < q2) {
x = q[++q1][0];
y = q[q1][1];
for (int k = 0; k < 4; k++)
if (x + dx[k] >= 0 && x + dx[k] < n && y + dy[k] >= 0 && y + dy[k] < n &&
d[x + dx[k]][y + dy[k]] == -1 && a[x + dx[k]][y + dy[k]] != 'Y') {
d[x + dx[k]][y + dy[k]] = d[x][y] + 1;
q[++q2][0] = x + dx[k];
q[q2][1] = y + dy[k];
}
}
memset(la, -1, sizeof(la));
ee = -1;
ns = n * n * 2 + 1;
nt = ns + 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i][j] >= '1' && a[i][j] <= '9') {
memset(v, -1, sizeof(v));
q1 = 0;
q2 = 1;
q[1][0] = i;
q[1][1] = j;
v[i][j] = 0;
while (q1 < q2) {
x = q[++q1][0];
y = q[q1][1];
if (v[x][y] > t) continue;
if (b[x][y] >= '1' && b[x][y] <= '9') {
add(i * n + j + 1, x * n + y + 1 + n * n, 100000);
add(x * n + y + 1 + n * n, i * n + j + 1, 0);
}
if (v[x][y] == d[x][y]) continue;
for (int k = 0; k < 4; k++)
if (x + dx[k] >= 0 && x + dx[k] < n && y + dy[k] >= 0 &&
y + dy[k] < n && v[x + dx[k]][y + dy[k]] == -1 &&
a[x + dx[k]][y + dy[k]] != 'Y')
if (d[x + dx[k]][y + dy[k]] == -1 ||
d[x + dx[k]][y + dy[k]] > v[x][y] + 1 ||
d[x + dx[k]][y + dy[k]] == v[x][y] + 1 &&
b[x + dx[k]][y + dy[k]] >= '1' &&
b[x + dx[k]][y + dy[k]] <= '9') {
v[x + dx[k]][y + dy[k]] = v[x][y] + 1;
q[++q2][0] = x + dx[k];
q[q2][1] = y + dy[k];
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (a[i][j] >= '1' && a[i][j] <= '9') {
add(ns, i * n + j + 1, a[i][j] - '0');
add(i * n + j + 1, ns, 0);
}
if (b[i][j] >= '1' && b[i][j] <= '9') {
add(i * n + j + 1 + n * n, nt, b[i][j] - '0');
add(nt, i * n + j + 1 + n * n, 0);
}
}
memset(le, 0, sizeof(le));
memset(num, 0, sizeof(num));
num[0] = nt;
int del = 0;
while (le[ns] < nt) del += dfs(ns, 100000);
printf("%d\n", del);
}
| ### Prompt
Create a solution in Cpp for the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
const int N = 12;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
int n, m, d[N][N], q[N * N][2], sx, sy, q1, q2, v[N][N], x, y;
char a[N][N], b[N][N];
struct edge {
int p, pre, cap;
} ed[N * N * N * N * 2];
int la[N * N * 2], num[N * N * 2], le[N * N * 2], ee, ns, nt;
int dfs(int x, int fx) {
if (x == nt) return fx;
int sum = 0, del;
for (int j = la[x]; j > -1; j = ed[j].pre)
if (le[x] == le[ed[j].p] + 1 && ed[j].cap > 0) {
if (ed[j].cap > fx)
del = fx;
else
del = ed[j].cap;
del = dfs(ed[j].p, del);
if (del == 0) continue;
sum += del;
fx -= del;
ed[j].cap -= del;
ed[j ^ 1].cap += del;
if (fx == 0) return sum;
}
if (le[ns] >= nt) return sum;
if (sum == 0) {
num[le[x]]--;
if (num[le[x]] == 0) le[ns] = nt;
le[x]++;
num[le[x]]++;
}
return sum;
}
void add(int x, int y, int ca) {
ee++;
ed[ee].pre = la[x];
ed[ee].p = y;
ed[ee].cap = ca;
la[x] = ee;
}
int t;
int main() {
scanf("%d%d", &n, &t);
for (int i = 0; i < n; i++) scanf("%s", a[i]);
for (int i = 0; i < n; i++) {
scanf("%s", b[i]);
for (int j = 0; j < n; j++)
if (b[i][j] == 'Z') {
sx = i;
sy = j;
}
}
q1 = 0;
q2 = 1;
q[1][0] = sx;
q[1][1] = sy;
memset(d, -1, sizeof(d));
d[sx][sy] = 0;
while (q1 < q2) {
x = q[++q1][0];
y = q[q1][1];
for (int k = 0; k < 4; k++)
if (x + dx[k] >= 0 && x + dx[k] < n && y + dy[k] >= 0 && y + dy[k] < n &&
d[x + dx[k]][y + dy[k]] == -1 && a[x + dx[k]][y + dy[k]] != 'Y') {
d[x + dx[k]][y + dy[k]] = d[x][y] + 1;
q[++q2][0] = x + dx[k];
q[q2][1] = y + dy[k];
}
}
memset(la, -1, sizeof(la));
ee = -1;
ns = n * n * 2 + 1;
nt = ns + 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i][j] >= '1' && a[i][j] <= '9') {
memset(v, -1, sizeof(v));
q1 = 0;
q2 = 1;
q[1][0] = i;
q[1][1] = j;
v[i][j] = 0;
while (q1 < q2) {
x = q[++q1][0];
y = q[q1][1];
if (v[x][y] > t) continue;
if (b[x][y] >= '1' && b[x][y] <= '9') {
add(i * n + j + 1, x * n + y + 1 + n * n, 100000);
add(x * n + y + 1 + n * n, i * n + j + 1, 0);
}
if (v[x][y] == d[x][y]) continue;
for (int k = 0; k < 4; k++)
if (x + dx[k] >= 0 && x + dx[k] < n && y + dy[k] >= 0 &&
y + dy[k] < n && v[x + dx[k]][y + dy[k]] == -1 &&
a[x + dx[k]][y + dy[k]] != 'Y')
if (d[x + dx[k]][y + dy[k]] == -1 ||
d[x + dx[k]][y + dy[k]] > v[x][y] + 1 ||
d[x + dx[k]][y + dy[k]] == v[x][y] + 1 &&
b[x + dx[k]][y + dy[k]] >= '1' &&
b[x + dx[k]][y + dy[k]] <= '9') {
v[x + dx[k]][y + dy[k]] = v[x][y] + 1;
q[++q2][0] = x + dx[k];
q[q2][1] = y + dy[k];
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (a[i][j] >= '1' && a[i][j] <= '9') {
add(ns, i * n + j + 1, a[i][j] - '0');
add(i * n + j + 1, ns, 0);
}
if (b[i][j] >= '1' && b[i][j] <= '9') {
add(i * n + j + 1 + n * n, nt, b[i][j] - '0');
add(nt, i * n + j + 1 + n * n, 0);
}
}
memset(le, 0, sizeof(le));
memset(num, 0, sizeof(num));
num[0] = nt;
int del = 0;
while (le[ns] < nt) del += dfs(ns, 100000);
printf("%d\n", del);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int M = 200005;
const int INF = 0x3f3f3f3f;
using namespace std;
int n;
struct Edge {
int v, next;
int cap, flow;
} edge[200005 * 20];
int cur[200005], pre[200005], gap[200005], path[200005], dep[200005];
int cnt = 0;
void isap_init() {
cnt = 0;
memset(pre, -1, sizeof(pre));
}
void isap_add(int u, int v, int w) {
edge[cnt].v = v;
edge[cnt].cap = w;
edge[cnt].flow = 0;
edge[cnt].next = pre[u];
pre[u] = cnt++;
}
void add(int u, int v, int w) {
isap_add(u, v, w);
isap_add(v, u, 0);
}
bool bfs(int s, int t) {
memset(dep, -1, sizeof(dep));
memset(gap, 0, sizeof(gap));
gap[0] = 1;
dep[t] = 0;
queue<int> q;
while (!q.empty()) q.pop();
q.push(t);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = pre[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if (dep[v] == -1 && edge[i ^ 1].cap > edge[i ^ 1].flow) {
dep[v] = dep[u] + 1;
gap[dep[v]]++;
q.push(v);
}
}
}
return dep[s] != -1;
}
int isap(int s, int t) {
if (!bfs(s, t)) return 0;
memcpy(cur, pre, sizeof(pre));
int u = s;
path[u] = -1;
int ans = 0;
while (dep[s] < n) {
if (u == t) {
int f = INF;
for (int i = path[u]; i != -1; i = path[edge[i ^ 1].v])
f = min(f, edge[i].cap - edge[i].flow);
for (int i = path[u]; i != -1; i = path[edge[i ^ 1].v]) {
edge[i].flow += f;
edge[i ^ 1].flow -= f;
}
ans += f;
u = s;
continue;
}
bool flag = false;
int v;
for (int i = cur[u]; i != -1; i = edge[i].next) {
v = edge[i].v;
if (dep[v] + 1 == dep[u] && edge[i].cap - edge[i].flow) {
cur[u] = path[v] = i;
flag = true;
break;
}
}
if (flag) {
u = v;
continue;
}
int x = n;
if (!(--gap[dep[u]])) return ans;
for (int i = pre[u]; i != -1; i = edge[i].next) {
if (edge[i].cap - edge[i].flow && dep[edge[i].v] < x) {
x = dep[edge[i].v];
cur[u] = i;
}
}
dep[u] = x + 1;
gap[dep[u]]++;
if (u != s) u = edge[path[u] ^ 1].v;
}
return ans;
}
int t;
string people[15], cap[15];
int book[15][15];
struct sair {
int x, y, step;
};
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
void bfs1(int x, int y) {
sair s, e;
s.step = 0, s.x = x, s.y = y;
memset(book, -1, sizeof(book));
queue<sair> Q;
Q.push(s);
book[s.x][s.y] = 0;
while (!Q.empty()) {
s = Q.front();
Q.pop();
if (s.step >= t) break;
for (int i = 0; i < 4; i++) {
e.x = s.x + dir[i][0];
e.y = s.y + dir[i][1];
if (e.x >= 0 && e.x < n && e.y >= 0 && e.y < n && book[e.x][e.y] == -1 &&
people[e.x][e.y] != 'Y') {
e.step = s.step + 1;
book[e.x][e.y] = e.step;
Q.push(e);
}
}
}
}
void bfs2(int x, int y) {
sair s, e;
int tmp[15][15];
memset(tmp, -1, sizeof(tmp));
s.x = x, s.y = y, s.step = 0;
tmp[s.x][s.y] = 0;
queue<sair> Q;
Q.push(s);
int w = people[x][y] - '0';
add(s.x * n + s.y, s.x * n + s.y + n * n, w);
while (!Q.empty()) {
s = Q.front();
Q.pop();
if (s.step >= t) break;
for (int i = 0; i < 4; i++) {
e.x = s.x + dir[i][0];
e.y = s.y + dir[i][1];
if (e.x < 0 || e.x >= n || e.y < 0 || e.y >= n ||
people[e.x][e.y] == 'Z' || people[e.x][e.y] == 'Y')
continue;
if (tmp[e.x][e.y] == -1 &&
(book[e.x][e.y] == -1 || book[e.x][e.y] >= s.step + 1)) {
if (book[e.x][e.y] == s.step + 1) {
if (cap[e.x][e.y] >= '0' && cap[e.x][e.y] <= '9') {
add(x * n + y, e.x * n + e.y + n * n, w);
}
} else {
e.step = s.step + 1;
tmp[e.x][e.y] = e.step;
add(x * n + y, e.x * n + e.y + n * n, w);
Q.push(e);
}
}
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n >> t;
int sx, sy;
isap_init();
for (int i = 0; i < n; i++) {
cin >> people[i];
for (int j = 0; j < n; j++) {
if (people[i][j] == 'Z') {
sx = i, sy = j;
}
}
}
for (int i = 0; i < n; i++) cin >> cap[i];
bfs1(sx, sy);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (people[i][j] >= '1' && people[i][j] <= '9') {
bfs2(i, j);
}
}
}
int S = n * n * 2;
int T = n * n * 2 + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (people[i][j] >= '0' && people[i][j] <= '9') {
add(S, i * n + j, people[i][j] - '0');
}
if (cap[i][j] >= '1' && cap[i][j] <= '9') {
add(i * n + j + n * n, T, cap[i][j] - '0');
}
}
}
n = n * n * 2 + 2;
int ans = isap(S, T);
cout << ans << endl;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int M = 200005;
const int INF = 0x3f3f3f3f;
using namespace std;
int n;
struct Edge {
int v, next;
int cap, flow;
} edge[200005 * 20];
int cur[200005], pre[200005], gap[200005], path[200005], dep[200005];
int cnt = 0;
void isap_init() {
cnt = 0;
memset(pre, -1, sizeof(pre));
}
void isap_add(int u, int v, int w) {
edge[cnt].v = v;
edge[cnt].cap = w;
edge[cnt].flow = 0;
edge[cnt].next = pre[u];
pre[u] = cnt++;
}
void add(int u, int v, int w) {
isap_add(u, v, w);
isap_add(v, u, 0);
}
bool bfs(int s, int t) {
memset(dep, -1, sizeof(dep));
memset(gap, 0, sizeof(gap));
gap[0] = 1;
dep[t] = 0;
queue<int> q;
while (!q.empty()) q.pop();
q.push(t);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = pre[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if (dep[v] == -1 && edge[i ^ 1].cap > edge[i ^ 1].flow) {
dep[v] = dep[u] + 1;
gap[dep[v]]++;
q.push(v);
}
}
}
return dep[s] != -1;
}
int isap(int s, int t) {
if (!bfs(s, t)) return 0;
memcpy(cur, pre, sizeof(pre));
int u = s;
path[u] = -1;
int ans = 0;
while (dep[s] < n) {
if (u == t) {
int f = INF;
for (int i = path[u]; i != -1; i = path[edge[i ^ 1].v])
f = min(f, edge[i].cap - edge[i].flow);
for (int i = path[u]; i != -1; i = path[edge[i ^ 1].v]) {
edge[i].flow += f;
edge[i ^ 1].flow -= f;
}
ans += f;
u = s;
continue;
}
bool flag = false;
int v;
for (int i = cur[u]; i != -1; i = edge[i].next) {
v = edge[i].v;
if (dep[v] + 1 == dep[u] && edge[i].cap - edge[i].flow) {
cur[u] = path[v] = i;
flag = true;
break;
}
}
if (flag) {
u = v;
continue;
}
int x = n;
if (!(--gap[dep[u]])) return ans;
for (int i = pre[u]; i != -1; i = edge[i].next) {
if (edge[i].cap - edge[i].flow && dep[edge[i].v] < x) {
x = dep[edge[i].v];
cur[u] = i;
}
}
dep[u] = x + 1;
gap[dep[u]]++;
if (u != s) u = edge[path[u] ^ 1].v;
}
return ans;
}
int t;
string people[15], cap[15];
int book[15][15];
struct sair {
int x, y, step;
};
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
void bfs1(int x, int y) {
sair s, e;
s.step = 0, s.x = x, s.y = y;
memset(book, -1, sizeof(book));
queue<sair> Q;
Q.push(s);
book[s.x][s.y] = 0;
while (!Q.empty()) {
s = Q.front();
Q.pop();
if (s.step >= t) break;
for (int i = 0; i < 4; i++) {
e.x = s.x + dir[i][0];
e.y = s.y + dir[i][1];
if (e.x >= 0 && e.x < n && e.y >= 0 && e.y < n && book[e.x][e.y] == -1 &&
people[e.x][e.y] != 'Y') {
e.step = s.step + 1;
book[e.x][e.y] = e.step;
Q.push(e);
}
}
}
}
void bfs2(int x, int y) {
sair s, e;
int tmp[15][15];
memset(tmp, -1, sizeof(tmp));
s.x = x, s.y = y, s.step = 0;
tmp[s.x][s.y] = 0;
queue<sair> Q;
Q.push(s);
int w = people[x][y] - '0';
add(s.x * n + s.y, s.x * n + s.y + n * n, w);
while (!Q.empty()) {
s = Q.front();
Q.pop();
if (s.step >= t) break;
for (int i = 0; i < 4; i++) {
e.x = s.x + dir[i][0];
e.y = s.y + dir[i][1];
if (e.x < 0 || e.x >= n || e.y < 0 || e.y >= n ||
people[e.x][e.y] == 'Z' || people[e.x][e.y] == 'Y')
continue;
if (tmp[e.x][e.y] == -1 &&
(book[e.x][e.y] == -1 || book[e.x][e.y] >= s.step + 1)) {
if (book[e.x][e.y] == s.step + 1) {
if (cap[e.x][e.y] >= '0' && cap[e.x][e.y] <= '9') {
add(x * n + y, e.x * n + e.y + n * n, w);
}
} else {
e.step = s.step + 1;
tmp[e.x][e.y] = e.step;
add(x * n + y, e.x * n + e.y + n * n, w);
Q.push(e);
}
}
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n >> t;
int sx, sy;
isap_init();
for (int i = 0; i < n; i++) {
cin >> people[i];
for (int j = 0; j < n; j++) {
if (people[i][j] == 'Z') {
sx = i, sy = j;
}
}
}
for (int i = 0; i < n; i++) cin >> cap[i];
bfs1(sx, sy);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (people[i][j] >= '1' && people[i][j] <= '9') {
bfs2(i, j);
}
}
}
int S = n * n * 2;
int T = n * n * 2 + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (people[i][j] >= '0' && people[i][j] <= '9') {
add(S, i * n + j, people[i][j] - '0');
}
if (cap[i][j] >= '1' && cap[i][j] <= '9') {
add(i * n + j + n * n, T, cap[i][j] - '0');
}
}
}
n = n * n * 2 + 2;
int ans = isap(S, T);
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char A[12][12];
char B[12][12];
int n, t;
bool _dp[70][12][12][12][12];
bool dp[12][12][12][12];
int tim[12][12];
void setdp(int tt, int x, int y, int x0, int y0) {
if (t >= 70) return;
if (x < 0 || x >= n || y < 0 || y >= n) return;
if (A[x][y] < '0' || A[x][y] > '9') return;
if (tt > t + 1) return;
if (_dp[tt][x][y][x0][y0]) return;
if (tt > tim[x][y] && tim[x][y] != 0) return;
if (tt == tim[x][y] && tim[x][y] != 0 && (B[x][y] > '9' || B[x][y] <= '0'))
return;
_dp[tt][x][y][x0][y0] = true;
if (tt < tim[x][y]) {
setdp(tt + 1, x, y, x0, y0);
setdp(tt + 1, x + 1, y, x0, y0);
setdp(tt + 1, x - 1, y, x0, y0);
setdp(tt + 1, x, y + 1, x0, y0);
setdp(tt + 1, x, y - 1, x0, y0);
}
}
vector<vector<int> > G;
int F[210][210];
int E[210], H[210];
int C[210][210];
int pN[11][11];
int main() {
cin >> n >> t;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> A[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> B[i][j];
deque<pair<int, int> > q;
{
memset(pN, 0, sizeof(pN));
memset(dp, 0, sizeof(dp));
memset(_dp, 0, sizeof(_dp));
G.clear();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) tim[i][j] = -1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (A[i][j] == 'Z') {
tim[i][j] = 0;
q.push_back(make_pair(i, j));
}
int di[] = {-1, 0, 1, 0};
int dj[] = {0, -1, 0, 1};
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop_front();
if (tim[p.first][p.second] >= t) continue;
for (int d = 0; d <= 3; d++) {
pair<int, int> np = make_pair(p.first + di[d], p.second + dj[d]);
if (np.first >= 0 && np.first < n && np.second >= 0 && np.second < n &&
A[np.first][np.second] >= '0' && A[np.first][np.second] <= '9' &&
tim[np.first][np.second] == -1) {
tim[np.first][np.second] = tim[p.first][p.second] + 1;
q.push_back(np);
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (tim[i][j] == -1) tim[i][j] = t;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (A[i][j] >= '0' && A[i][j] <= '9') setdp(0, i, j, i, j);
vector<int> st;
G.push_back(st);
for (int i1 = 0; i1 < n; i1++)
for (int j1 = 0; j1 < n; j1++)
for (int i2 = 0; i2 < n; i2++)
for (int j2 = 0; j2 < n; j2++)
for (int nt = 0; nt <= t; nt++)
dp[i1][j1][i2][j2] |= _dp[nt][i1][j1][i2][j2];
for (int i1 = 0; i1 < n; i1++)
for (int j1 = 0; j1 < n; j1++)
if (A[i1][j1] > '0' && A[i1][j1] <= '9') {
int k = G.size();
vector<int> g;
G.push_back(g);
E[k] = A[i1][j1] - '0';
for (int i2 = 0; i2 < n; i2++)
for (int j2 = 0; j2 < n; j2++)
if (B[i2][j2] > '0' && B[i2][j2] <= '9')
if (dp[i2][j2][i1][j1]) {
int _k;
if (pN[i2][j2] == 0) {
vector<int> _g;
_k = G.size();
G.push_back(_g);
pN[i2][j2] = _k;
} else {
_k = pN[i2][j2];
}
G[k].push_back(_k);
G[_k].push_back(k);
C[k][_k] = 10000;
C[_k][k] = 0;
}
}
for (int i2 = 0; i2 < n; i2++)
for (int j2 = 0; j2 < n; j2++)
if (B[i2][j2] > '0' && B[i2][j2] <= '9' && pN[i2][j2] > 0) {
int _k;
_k = pN[i2][j2];
G[_k].push_back(0);
C[_k][0] = B[i2][j2] - '0';
C[0][_k] = 0;
}
}
deque<int> en0;
en0.clear();
for (int i = 1; i < G.size(); i++)
if (E[i] > 0) en0.push_back(i);
int cn = 0;
while (!en0.empty() && cn <= en0.size()) {
int i = en0.front();
en0.pop_front();
int minH = 10000;
for (int _j = 0; _j < G[i].size(); _j++) {
int j = G[i][_j];
if (C[i][j] - F[i][j] > 0) {
minH = min(minH, H[j]);
if (H[i] == H[j] + 1) {
int d = min(C[i][j] - F[i][j], E[i]);
F[i][j] += d;
F[j][i] -= d;
E[i] -= d;
E[j] += d;
cn = 0;
if (E[j] == d && j != 0) en0.push_front(j);
if (E[i] == 0) goto next;
}
}
}
if (minH != 10000)
cn = 0, H[i] = minH + 1;
else
cn++;
en0.push_back(i);
next:;
}
exit:
cout << E[0];
return (0);
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char A[12][12];
char B[12][12];
int n, t;
bool _dp[70][12][12][12][12];
bool dp[12][12][12][12];
int tim[12][12];
void setdp(int tt, int x, int y, int x0, int y0) {
if (t >= 70) return;
if (x < 0 || x >= n || y < 0 || y >= n) return;
if (A[x][y] < '0' || A[x][y] > '9') return;
if (tt > t + 1) return;
if (_dp[tt][x][y][x0][y0]) return;
if (tt > tim[x][y] && tim[x][y] != 0) return;
if (tt == tim[x][y] && tim[x][y] != 0 && (B[x][y] > '9' || B[x][y] <= '0'))
return;
_dp[tt][x][y][x0][y0] = true;
if (tt < tim[x][y]) {
setdp(tt + 1, x, y, x0, y0);
setdp(tt + 1, x + 1, y, x0, y0);
setdp(tt + 1, x - 1, y, x0, y0);
setdp(tt + 1, x, y + 1, x0, y0);
setdp(tt + 1, x, y - 1, x0, y0);
}
}
vector<vector<int> > G;
int F[210][210];
int E[210], H[210];
int C[210][210];
int pN[11][11];
int main() {
cin >> n >> t;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> A[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> B[i][j];
deque<pair<int, int> > q;
{
memset(pN, 0, sizeof(pN));
memset(dp, 0, sizeof(dp));
memset(_dp, 0, sizeof(_dp));
G.clear();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) tim[i][j] = -1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (A[i][j] == 'Z') {
tim[i][j] = 0;
q.push_back(make_pair(i, j));
}
int di[] = {-1, 0, 1, 0};
int dj[] = {0, -1, 0, 1};
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop_front();
if (tim[p.first][p.second] >= t) continue;
for (int d = 0; d <= 3; d++) {
pair<int, int> np = make_pair(p.first + di[d], p.second + dj[d]);
if (np.first >= 0 && np.first < n && np.second >= 0 && np.second < n &&
A[np.first][np.second] >= '0' && A[np.first][np.second] <= '9' &&
tim[np.first][np.second] == -1) {
tim[np.first][np.second] = tim[p.first][p.second] + 1;
q.push_back(np);
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (tim[i][j] == -1) tim[i][j] = t;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (A[i][j] >= '0' && A[i][j] <= '9') setdp(0, i, j, i, j);
vector<int> st;
G.push_back(st);
for (int i1 = 0; i1 < n; i1++)
for (int j1 = 0; j1 < n; j1++)
for (int i2 = 0; i2 < n; i2++)
for (int j2 = 0; j2 < n; j2++)
for (int nt = 0; nt <= t; nt++)
dp[i1][j1][i2][j2] |= _dp[nt][i1][j1][i2][j2];
for (int i1 = 0; i1 < n; i1++)
for (int j1 = 0; j1 < n; j1++)
if (A[i1][j1] > '0' && A[i1][j1] <= '9') {
int k = G.size();
vector<int> g;
G.push_back(g);
E[k] = A[i1][j1] - '0';
for (int i2 = 0; i2 < n; i2++)
for (int j2 = 0; j2 < n; j2++)
if (B[i2][j2] > '0' && B[i2][j2] <= '9')
if (dp[i2][j2][i1][j1]) {
int _k;
if (pN[i2][j2] == 0) {
vector<int> _g;
_k = G.size();
G.push_back(_g);
pN[i2][j2] = _k;
} else {
_k = pN[i2][j2];
}
G[k].push_back(_k);
G[_k].push_back(k);
C[k][_k] = 10000;
C[_k][k] = 0;
}
}
for (int i2 = 0; i2 < n; i2++)
for (int j2 = 0; j2 < n; j2++)
if (B[i2][j2] > '0' && B[i2][j2] <= '9' && pN[i2][j2] > 0) {
int _k;
_k = pN[i2][j2];
G[_k].push_back(0);
C[_k][0] = B[i2][j2] - '0';
C[0][_k] = 0;
}
}
deque<int> en0;
en0.clear();
for (int i = 1; i < G.size(); i++)
if (E[i] > 0) en0.push_back(i);
int cn = 0;
while (!en0.empty() && cn <= en0.size()) {
int i = en0.front();
en0.pop_front();
int minH = 10000;
for (int _j = 0; _j < G[i].size(); _j++) {
int j = G[i][_j];
if (C[i][j] - F[i][j] > 0) {
minH = min(minH, H[j]);
if (H[i] == H[j] + 1) {
int d = min(C[i][j] - F[i][j], E[i]);
F[i][j] += d;
F[j][i] -= d;
E[i] -= d;
E[j] += d;
cn = 0;
if (E[j] == d && j != 0) en0.push_front(j);
if (E[i] == 0) goto next;
}
}
}
if (minH != 10000)
cn = 0, H[i] = minH + 1;
else
cn++;
en0.push_back(i);
next:;
}
exit:
cout << E[0];
return (0);
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, vis[15][15][15][15], res, sci, bx, by, qx[215], qy[215], boom;
char map1[15][15], map2[15][15];
int x, y, l[215], v[215], g[215], qd[100005], qn[100005], qc[100005], ret, S, T,
V, t;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void flood(int sx, int sy, int par) {
int cx, cy, tx, ty;
memset(vis[sx][sy], -1, sizeof(vis[sx][sy]));
vis[sx][sy][sx][sy] = 0;
qx[0] = sx;
qy[0] = sy;
for (int i = 0, j = 0; j >= i; i++) {
cx = qx[i];
cy = qy[i];
if (par == 1 && vis[bx][by][cx][cy] != -1 &&
vis[sx][sy][cx][cy] >= vis[bx][by][cx][cy])
continue;
for (int k = 0; k < 4; k++) {
tx = cx + dx[k];
ty = cy + dy[k];
if (tx < 0 || ty < 0 || tx >= n | ty >= n) continue;
if (map1[tx][ty] == 'Y') continue;
if (vis[sx][sy][tx][ty] == -1) {
vis[sx][sy][tx][ty] = vis[sx][sy][cx][cy] + 1;
j++;
qx[j] = tx;
qy[j] = ty;
}
}
}
}
void bd(int x, int y, int w) {
qd[++t] = y;
qc[t] = w;
qn[t] = l[x];
l[x] = t;
}
int ISAP(int x, int y) {
int mn = V - 1, F = 0, R = y, t;
if (x == T) return y;
for (int i = l[x]; i; i = qn[i]) {
if (qc[i] > 0) {
if (v[x] == v[qd[i]] + 1) {
t = ISAP(qd[i], min(qc[i], R));
qc[i] -= t;
qc[i ^ 1] += t;
R -= t;
F += t;
if (v[S] >= V) return F;
if (!R) break;
}
if (v[qd[i]] < mn) mn = v[qd[i]];
}
}
if (!F) {
g[v[x]]--;
if (!g[v[x]]) v[S] = V;
v[x] = mn + 1;
g[v[x]]++;
}
return F;
}
int main() {
scanf("%d%d", &n, &boom);
for (int i = (0); i < (int)(n); i++) scanf("%s", map1[i]);
for (int i = (0); i < (int)(n); i++) {
scanf("%s", map2[i]);
for (int j = (0); j < (int)(n); j++)
if (map2[i][j] == 'Z') bx = i, by = j;
}
flood(bx, by, 0);
S = 2 * n * n;
T = S + 1;
V = T + 1;
t = 1;
for (int i = (0); i < (int)(n); i++) {
for (int j = (0); j < (int)(n); j++) {
sci = map1[i][j] - '0';
if (sci >= 1 && sci <= 9) {
flood(i, j, 1);
x = i * n + j;
bd(S, x, sci);
bd(x, S, 0);
for (int k1 = (0); k1 < (int)(n); k1++) {
for (int k2 = (0); k2 < (int)(n); k2++) {
res = map2[k1][k2] - '0';
if (res >= 1 && res <= 9) {
if (vis[i][j][k1][k2] == -1) continue;
if (vis[i][j][k1][k2] > boom) continue;
if (vis[i][j][k1][k2] > vis[bx][by][k1][k2] &&
vis[bx][by][k1][k2] != -1)
continue;
y = n * n + k1 * n + k2;
bd(x, y, sci);
bd(y, x, 0);
}
}
}
}
}
}
for (int k1 = (0); k1 < (int)(n); k1++) {
for (int k2 = (0); k2 < (int)(n); k2++) {
res = map2[k1][k2] - '0';
if (res >= 1 && res <= 9) {
y = n * n + k1 * n + k2;
bd(y, T, res);
bd(T, y, 0);
}
}
}
memset(v, 0, sizeof(v));
memset(g, 0, sizeof(g));
ret = 0;
g[0] = V;
while (v[S] < V) ret += ISAP(S, 1 << 29);
printf("%d\n", ret);
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, vis[15][15][15][15], res, sci, bx, by, qx[215], qy[215], boom;
char map1[15][15], map2[15][15];
int x, y, l[215], v[215], g[215], qd[100005], qn[100005], qc[100005], ret, S, T,
V, t;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void flood(int sx, int sy, int par) {
int cx, cy, tx, ty;
memset(vis[sx][sy], -1, sizeof(vis[sx][sy]));
vis[sx][sy][sx][sy] = 0;
qx[0] = sx;
qy[0] = sy;
for (int i = 0, j = 0; j >= i; i++) {
cx = qx[i];
cy = qy[i];
if (par == 1 && vis[bx][by][cx][cy] != -1 &&
vis[sx][sy][cx][cy] >= vis[bx][by][cx][cy])
continue;
for (int k = 0; k < 4; k++) {
tx = cx + dx[k];
ty = cy + dy[k];
if (tx < 0 || ty < 0 || tx >= n | ty >= n) continue;
if (map1[tx][ty] == 'Y') continue;
if (vis[sx][sy][tx][ty] == -1) {
vis[sx][sy][tx][ty] = vis[sx][sy][cx][cy] + 1;
j++;
qx[j] = tx;
qy[j] = ty;
}
}
}
}
void bd(int x, int y, int w) {
qd[++t] = y;
qc[t] = w;
qn[t] = l[x];
l[x] = t;
}
int ISAP(int x, int y) {
int mn = V - 1, F = 0, R = y, t;
if (x == T) return y;
for (int i = l[x]; i; i = qn[i]) {
if (qc[i] > 0) {
if (v[x] == v[qd[i]] + 1) {
t = ISAP(qd[i], min(qc[i], R));
qc[i] -= t;
qc[i ^ 1] += t;
R -= t;
F += t;
if (v[S] >= V) return F;
if (!R) break;
}
if (v[qd[i]] < mn) mn = v[qd[i]];
}
}
if (!F) {
g[v[x]]--;
if (!g[v[x]]) v[S] = V;
v[x] = mn + 1;
g[v[x]]++;
}
return F;
}
int main() {
scanf("%d%d", &n, &boom);
for (int i = (0); i < (int)(n); i++) scanf("%s", map1[i]);
for (int i = (0); i < (int)(n); i++) {
scanf("%s", map2[i]);
for (int j = (0); j < (int)(n); j++)
if (map2[i][j] == 'Z') bx = i, by = j;
}
flood(bx, by, 0);
S = 2 * n * n;
T = S + 1;
V = T + 1;
t = 1;
for (int i = (0); i < (int)(n); i++) {
for (int j = (0); j < (int)(n); j++) {
sci = map1[i][j] - '0';
if (sci >= 1 && sci <= 9) {
flood(i, j, 1);
x = i * n + j;
bd(S, x, sci);
bd(x, S, 0);
for (int k1 = (0); k1 < (int)(n); k1++) {
for (int k2 = (0); k2 < (int)(n); k2++) {
res = map2[k1][k2] - '0';
if (res >= 1 && res <= 9) {
if (vis[i][j][k1][k2] == -1) continue;
if (vis[i][j][k1][k2] > boom) continue;
if (vis[i][j][k1][k2] > vis[bx][by][k1][k2] &&
vis[bx][by][k1][k2] != -1)
continue;
y = n * n + k1 * n + k2;
bd(x, y, sci);
bd(y, x, 0);
}
}
}
}
}
}
for (int k1 = (0); k1 < (int)(n); k1++) {
for (int k2 = (0); k2 < (int)(n); k2++) {
res = map2[k1][k2] - '0';
if (res >= 1 && res <= 9) {
y = n * n + k1 * n + k2;
bd(y, T, res);
bd(T, y, 0);
}
}
}
memset(v, 0, sizeof(v));
memset(g, 0, sizeof(g));
ret = 0;
g[0] = V;
while (v[S] < V) ret += ISAP(S, 1 << 29);
printf("%d\n", ret);
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
struct Maxflow {
struct edge {
int from, to;
long long flow, capacity;
};
int s, t;
vector<edge> edg;
vector<int> g[7100];
int dist[7100], visited[7100];
int cur[7100];
void init() {
edg.clear();
int i;
for (i = 0; i < 7100; i++) {
g[i].clear();
}
}
void addedge(int from, int to, long long capacity) {
edge e1 = edge{from, to, 0ll, capacity};
edge e2 = edge{to, from, 0ll, 0ll};
edg.push_back(e1), edg.push_back(e2);
g[from].push_back((int)edg.size() - 2);
g[to].push_back((int)edg.size() - 1);
}
bool bfs() {
memset(visited, 0, sizeof(visited));
memset(dist, 0, sizeof(dist));
queue<int> q;
q.push(s);
visited[s] = 1;
dist[s] = 0;
while (!q.empty()) {
int v = q.front();
q.pop();
for (int i = 0; i < (int)g[v].size(); i++) {
edge &e = edg[g[v][i]];
int nxt = e.to;
if (!visited[nxt] && e.capacity > e.flow) {
dist[nxt] = dist[v] + 1;
q.push(nxt);
visited[nxt] = 1;
}
}
}
return visited[t];
}
long long dfs(int x, long long cp) {
if (x == t || cp == 0) {
return cp;
}
long long flow = 0, newflow;
for (int &y = cur[x]; y < (int)g[x].size(); y++) {
edge &e = edg[g[x][y]];
if (dist[x] + 1 == dist[e.to]) {
long long minn = min(cp, e.capacity - e.flow);
newflow = dfs(e.to, minn);
if (newflow > 0) {
e.flow += newflow;
edg[g[x][y] ^ 1].flow -= newflow;
flow += newflow;
cp -= newflow;
if (cp == 0) {
break;
}
}
}
}
return flow;
}
long long Dinic() {
long long flow = 0;
while (bfs()) {
memset(cur, 0, sizeof(cur));
flow += dfs(s, 1000000000);
}
return flow;
}
};
Maxflow mf;
char g1[14][14];
char g2[14][14];
bool visited[14][14];
int dist[14][14];
int n;
int tgt[103];
void BFS(int x, int y) {
queue<pair<pair<int, int>, int> > q;
int i, j;
for (i = 0; i < 14; i++) {
for (j = 0; j < 14; j++) {
dist[i][j] = 111;
}
}
dist[x][y] = 0;
q.push(make_pair(make_pair(x, y), 0));
int dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
while (!q.empty()) {
auto p = q.front();
q.pop();
if (visited[p.first.first][p.first.second]) continue;
visited[p.first.first][p.first.second] = true;
dist[p.first.first][p.first.second] = p.second;
for (i = 0; i < 4; i++) {
int tx = dx[i] + p.first.first, ty = dy[i] + p.first.second;
if (tx <= 0 || tx > n || ty <= 0 || ty > n || visited[tx][ty] ||
g1[tx][ty] == 'Y')
continue;
q.push(make_pair(make_pair(tx, ty), p.second + 1));
}
}
}
int index(int level, int x, int y) { return level * n * n + (x - 1) * n + y; }
int main() {
int i, t, j, k;
scanf("%d%d", &n, &t);
for (i = 1; i <= n; i++) scanf("%s", g1[i] + 1);
for (i = 1; i <= n; i++) scanf("%s", g2[i] + 1);
mf.s = 0, mf.t = 7100 - 2;
int x = 0, y = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (g1[i][j] == 'Z') {
x = i, y = j;
break;
}
}
}
for (i = 1; i <= n * n; i++) tgt[i] = 7100 - 3 - (n * n - i);
BFS(x, y);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (g1[i][j] >= '0' && g1[i][j] <= '9')
mf.addedge(mf.s, index(0, i, j), g1[i][j] - '0');
if (g2[i][j] >= '0' && g2[i][j] <= '9')
mf.addedge(tgt[index(0, i, j)], mf.t, g2[i][j] - '0');
mf.addedge(index(0, i, j), tgt[index(0, i, j)], 1000000000);
}
}
int dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
for (i = 1; i <= t; i++) {
for (j = 1; j <= n; j++) {
for (k = 1; k <= n; k++) {
if (g1[j][k] == 'Y' || g1[j][k] == 'Z') continue;
for (int l = 0; l < 4; l++) {
int tx = dx[l] + j, ty = dy[l] + k;
if (tx >= 1 && tx <= n && ty >= 1 && ty <= n && g1[tx][ty] != 'Y' &&
g1[tx][ty] != 'Z') {
if (dist[j][k] >= i && dist[tx][ty] >= i && g1[j][k] != 'Y' &&
g1[j][k] != 'Z') {
mf.addedge(index(i - 1, tx, ty), index(i, j, k), 1000000000);
}
}
}
mf.addedge(index(i, j, k), tgt[index(0, j, k)], 1000000000);
}
}
}
printf("%lld\n", mf.Dinic());
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
struct Maxflow {
struct edge {
int from, to;
long long flow, capacity;
};
int s, t;
vector<edge> edg;
vector<int> g[7100];
int dist[7100], visited[7100];
int cur[7100];
void init() {
edg.clear();
int i;
for (i = 0; i < 7100; i++) {
g[i].clear();
}
}
void addedge(int from, int to, long long capacity) {
edge e1 = edge{from, to, 0ll, capacity};
edge e2 = edge{to, from, 0ll, 0ll};
edg.push_back(e1), edg.push_back(e2);
g[from].push_back((int)edg.size() - 2);
g[to].push_back((int)edg.size() - 1);
}
bool bfs() {
memset(visited, 0, sizeof(visited));
memset(dist, 0, sizeof(dist));
queue<int> q;
q.push(s);
visited[s] = 1;
dist[s] = 0;
while (!q.empty()) {
int v = q.front();
q.pop();
for (int i = 0; i < (int)g[v].size(); i++) {
edge &e = edg[g[v][i]];
int nxt = e.to;
if (!visited[nxt] && e.capacity > e.flow) {
dist[nxt] = dist[v] + 1;
q.push(nxt);
visited[nxt] = 1;
}
}
}
return visited[t];
}
long long dfs(int x, long long cp) {
if (x == t || cp == 0) {
return cp;
}
long long flow = 0, newflow;
for (int &y = cur[x]; y < (int)g[x].size(); y++) {
edge &e = edg[g[x][y]];
if (dist[x] + 1 == dist[e.to]) {
long long minn = min(cp, e.capacity - e.flow);
newflow = dfs(e.to, minn);
if (newflow > 0) {
e.flow += newflow;
edg[g[x][y] ^ 1].flow -= newflow;
flow += newflow;
cp -= newflow;
if (cp == 0) {
break;
}
}
}
}
return flow;
}
long long Dinic() {
long long flow = 0;
while (bfs()) {
memset(cur, 0, sizeof(cur));
flow += dfs(s, 1000000000);
}
return flow;
}
};
Maxflow mf;
char g1[14][14];
char g2[14][14];
bool visited[14][14];
int dist[14][14];
int n;
int tgt[103];
void BFS(int x, int y) {
queue<pair<pair<int, int>, int> > q;
int i, j;
for (i = 0; i < 14; i++) {
for (j = 0; j < 14; j++) {
dist[i][j] = 111;
}
}
dist[x][y] = 0;
q.push(make_pair(make_pair(x, y), 0));
int dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
while (!q.empty()) {
auto p = q.front();
q.pop();
if (visited[p.first.first][p.first.second]) continue;
visited[p.first.first][p.first.second] = true;
dist[p.first.first][p.first.second] = p.second;
for (i = 0; i < 4; i++) {
int tx = dx[i] + p.first.first, ty = dy[i] + p.first.second;
if (tx <= 0 || tx > n || ty <= 0 || ty > n || visited[tx][ty] ||
g1[tx][ty] == 'Y')
continue;
q.push(make_pair(make_pair(tx, ty), p.second + 1));
}
}
}
int index(int level, int x, int y) { return level * n * n + (x - 1) * n + y; }
int main() {
int i, t, j, k;
scanf("%d%d", &n, &t);
for (i = 1; i <= n; i++) scanf("%s", g1[i] + 1);
for (i = 1; i <= n; i++) scanf("%s", g2[i] + 1);
mf.s = 0, mf.t = 7100 - 2;
int x = 0, y = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (g1[i][j] == 'Z') {
x = i, y = j;
break;
}
}
}
for (i = 1; i <= n * n; i++) tgt[i] = 7100 - 3 - (n * n - i);
BFS(x, y);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (g1[i][j] >= '0' && g1[i][j] <= '9')
mf.addedge(mf.s, index(0, i, j), g1[i][j] - '0');
if (g2[i][j] >= '0' && g2[i][j] <= '9')
mf.addedge(tgt[index(0, i, j)], mf.t, g2[i][j] - '0');
mf.addedge(index(0, i, j), tgt[index(0, i, j)], 1000000000);
}
}
int dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
for (i = 1; i <= t; i++) {
for (j = 1; j <= n; j++) {
for (k = 1; k <= n; k++) {
if (g1[j][k] == 'Y' || g1[j][k] == 'Z') continue;
for (int l = 0; l < 4; l++) {
int tx = dx[l] + j, ty = dy[l] + k;
if (tx >= 1 && tx <= n && ty >= 1 && ty <= n && g1[tx][ty] != 'Y' &&
g1[tx][ty] != 'Z') {
if (dist[j][k] >= i && dist[tx][ty] >= i && g1[j][k] != 'Y' &&
g1[j][k] != 'Z') {
mf.addedge(index(i - 1, tx, ty), index(i, j, k), 1000000000);
}
}
}
mf.addedge(index(i, j, k), tgt[index(0, j, k)], 1000000000);
}
}
}
printf("%lld\n", mf.Dinic());
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, expltime;
string scientist[11];
string capsules[11];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int sd[200][200];
int flow[205][205];
int cap[205][205];
vector<int> adjlist[205];
vector<int> caps;
vector<int> people;
int par[205];
bool find_path() {
queue<int> q;
q.push(200);
while (!q.empty()) {
int curnode = q.front();
q.pop();
if (curnode == 201) return true;
for (int i = 0; i < adjlist[curnode].size(); i++) {
if (par[adjlist[curnode][i]] == -1 &&
cap[curnode][adjlist[curnode][i]] -
flow[curnode][adjlist[curnode][i]] >
0) {
par[adjlist[curnode][i]] = curnode;
q.push(adjlist[curnode][i]);
}
}
}
return false;
}
int max_flow() {
for (int i = 0; i < 205; i++) par[i] = -1;
int totflow = 0;
while (find_path()) {
int pathflow = 1000000;
for (int v = 201, u = par[v]; v != 200; v = u, u = par[v]) {
pathflow = min(pathflow, cap[u][v] - flow[u][v]);
}
for (int v = 201, u = par[v]; v != 200; v = u, u = par[v]) {
flow[u][v] += pathflow;
flow[v][u] -= pathflow;
}
for (int i = 0; i < 205; i++) par[i] = -1;
totflow += pathflow;
}
return totflow;
}
bool in_bounds(int ro, int co) {
return ro >= 0 && ro < n && co >= 0 && co < n;
}
int rc(int ro, int co) { return ro * n + co; }
bool ontime(int personloc, int capsuleloc, int reactorloc) {
for (int k = 0; k < 4; k++) {
int capsulesurround = rc(capsuleloc / n + dx[k], capsuleloc % n + dy[k]);
if (in_bounds(capsuleloc / n + dx[k], capsuleloc % n + dy[k]) &&
sd[personloc][capsulesurround] < sd[capsulesurround][reactorloc]) {
return true;
}
}
return false;
}
int main() {
int reactorloc;
cin >> n >> expltime;
for (int i = 0; i < n; i++) {
cin >> scientist[i];
}
for (int i = 0; i < n; i++) {
cin >> capsules[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (capsules[i][j] > '0' && capsules[i][j] <= '9') {
caps.push_back(rc(i, j));
}
if (scientist[i][j] > '0' && scientist[i][j] <= '9') {
people.push_back(rc(i, j));
}
if (capsules[i][j] == 'Z') reactorloc = rc(i, j);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < 4; k++) {
if (in_bounds(i + dx[k], j + dy[k])) {
if (!(scientist[i][j] == 'Y' ||
scientist[i + dx[k]][j + dy[k]] == 'Y')) {
sd[rc(i, j)][rc(i + dx[k], j + dy[k])] = 1;
}
}
}
}
}
for (int i = 0; i < n * n; i++) {
for (int j = 0; j < n * n; j++) {
if (sd[i][j] == 0 && i != j) sd[i][j] = 1000000;
}
}
for (int k = 0; k < n * n; k++) {
for (int i = 0; i < n * n; i++) {
for (int j = 0; j < n * n; j++) {
sd[i][j] = min(sd[i][k] + sd[k][j], sd[i][j]);
}
}
}
for (int i = 0; i < people.size(); i++) {
for (int j = 0; j < caps.size(); j++) {
if (sd[people[i]][caps[j]] <= expltime &&
(sd[people[i]][caps[j]] < sd[reactorloc][caps[j]] ||
(sd[people[i]][caps[j]] == sd[reactorloc][caps[j]] &&
ontime(people[i], caps[j], reactorloc)))) {
adjlist[people[i] + 100].push_back(caps[j]);
adjlist[caps[j]].push_back(people[i] + 100);
cap[people[i] + 100][caps[j]] = 1000000;
}
}
}
for (int i = 0; i < people.size(); i++) {
adjlist[200].push_back(people[i] + 100);
cap[200][people[i] + 100] = (scientist[people[i] / n][people[i] % n] - '0');
}
for (int i = 0; i < caps.size(); i++) {
adjlist[caps[i]].push_back(201);
cap[caps[i]][201] = (capsules[caps[i] / n][caps[i] % n] - '0');
}
cout << max_flow() << endl;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, expltime;
string scientist[11];
string capsules[11];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int sd[200][200];
int flow[205][205];
int cap[205][205];
vector<int> adjlist[205];
vector<int> caps;
vector<int> people;
int par[205];
bool find_path() {
queue<int> q;
q.push(200);
while (!q.empty()) {
int curnode = q.front();
q.pop();
if (curnode == 201) return true;
for (int i = 0; i < adjlist[curnode].size(); i++) {
if (par[adjlist[curnode][i]] == -1 &&
cap[curnode][adjlist[curnode][i]] -
flow[curnode][adjlist[curnode][i]] >
0) {
par[adjlist[curnode][i]] = curnode;
q.push(adjlist[curnode][i]);
}
}
}
return false;
}
int max_flow() {
for (int i = 0; i < 205; i++) par[i] = -1;
int totflow = 0;
while (find_path()) {
int pathflow = 1000000;
for (int v = 201, u = par[v]; v != 200; v = u, u = par[v]) {
pathflow = min(pathflow, cap[u][v] - flow[u][v]);
}
for (int v = 201, u = par[v]; v != 200; v = u, u = par[v]) {
flow[u][v] += pathflow;
flow[v][u] -= pathflow;
}
for (int i = 0; i < 205; i++) par[i] = -1;
totflow += pathflow;
}
return totflow;
}
bool in_bounds(int ro, int co) {
return ro >= 0 && ro < n && co >= 0 && co < n;
}
int rc(int ro, int co) { return ro * n + co; }
bool ontime(int personloc, int capsuleloc, int reactorloc) {
for (int k = 0; k < 4; k++) {
int capsulesurround = rc(capsuleloc / n + dx[k], capsuleloc % n + dy[k]);
if (in_bounds(capsuleloc / n + dx[k], capsuleloc % n + dy[k]) &&
sd[personloc][capsulesurround] < sd[capsulesurround][reactorloc]) {
return true;
}
}
return false;
}
int main() {
int reactorloc;
cin >> n >> expltime;
for (int i = 0; i < n; i++) {
cin >> scientist[i];
}
for (int i = 0; i < n; i++) {
cin >> capsules[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (capsules[i][j] > '0' && capsules[i][j] <= '9') {
caps.push_back(rc(i, j));
}
if (scientist[i][j] > '0' && scientist[i][j] <= '9') {
people.push_back(rc(i, j));
}
if (capsules[i][j] == 'Z') reactorloc = rc(i, j);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < 4; k++) {
if (in_bounds(i + dx[k], j + dy[k])) {
if (!(scientist[i][j] == 'Y' ||
scientist[i + dx[k]][j + dy[k]] == 'Y')) {
sd[rc(i, j)][rc(i + dx[k], j + dy[k])] = 1;
}
}
}
}
}
for (int i = 0; i < n * n; i++) {
for (int j = 0; j < n * n; j++) {
if (sd[i][j] == 0 && i != j) sd[i][j] = 1000000;
}
}
for (int k = 0; k < n * n; k++) {
for (int i = 0; i < n * n; i++) {
for (int j = 0; j < n * n; j++) {
sd[i][j] = min(sd[i][k] + sd[k][j], sd[i][j]);
}
}
}
for (int i = 0; i < people.size(); i++) {
for (int j = 0; j < caps.size(); j++) {
if (sd[people[i]][caps[j]] <= expltime &&
(sd[people[i]][caps[j]] < sd[reactorloc][caps[j]] ||
(sd[people[i]][caps[j]] == sd[reactorloc][caps[j]] &&
ontime(people[i], caps[j], reactorloc)))) {
adjlist[people[i] + 100].push_back(caps[j]);
adjlist[caps[j]].push_back(people[i] + 100);
cap[people[i] + 100][caps[j]] = 1000000;
}
}
}
for (int i = 0; i < people.size(); i++) {
adjlist[200].push_back(people[i] + 100);
cap[200][people[i] + 100] = (scientist[people[i] / n][people[i] % n] - '0');
}
for (int i = 0; i < caps.size(); i++) {
adjlist[caps[i]].push_back(201);
cap[caps[i]][201] = (capsules[caps[i] / n][caps[i] % n] - '0');
}
cout << max_flow() << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 7;
struct triple {
int a, b, f, c;
triple(int first = 0, int second = 0, int z = 0, int w = 0) {
a = first, b = second, f = z, c = w;
}
};
bool ok(int first, int second, int n) {
return (first > 0) && (first <= n) && (second > 0) && (second <= n);
}
triple edge[100005];
int quant = 0, fir[100005], dist[100005];
char uch[15][15], kap[15][15];
bool used[15][15], dp[65][15][15][15][15], cop[15][15];
vector<int> g[100005];
queue<int> qu;
bool bfs(int start, int finish, int B) {
for (int i = start; i <= finish; i++) dist[i] = INF;
dist[start] = 0;
qu.push(start);
while (!qu.empty()) {
int v = qu.front();
qu.pop();
for (int i = 0; i < (int)g[v].size(); i++) {
int num = g[v][i];
int u = edge[num].b;
if (edge[num].f + B <= edge[num].c && dist[v] + 1 < dist[u]) {
dist[u] = dist[v] + 1;
qu.push(u);
}
}
}
return dist[finish] != INF;
}
int dfs(int v, int finish, int cmin, int B) {
if (v == finish) return cmin;
for (; fir[v] < (int)g[v].size(); fir[v]++) {
int num = g[v][fir[v]];
int u = edge[num].b;
if (dist[u] == dist[v] + 1 && edge[num].f + B <= edge[num].c) {
int d = dfs(u, finish, min(cmin, edge[num].c - edge[num].f), B);
if (d != 0) {
edge[num].f += d;
edge[num ^ 1].f -= d;
return d;
}
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, t;
cin >> n >> t;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
cin >> uch[i][j];
if (uch[i][j] == 'Z') used[i][j] = true;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> kap[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (uch[i][j] != 'Z' && uch[i][j] != 'Y') dp[0][i][j][i][j] = true;
for (int rep = 1; rep <= t; rep++) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
for (int l = 1; l <= n; l++) {
if (uch[k][l] == 'Z' || uch[k][l] == 'Y' || used[k][l]) continue;
if (dp[rep - 1][i][j][k][l])
dp[rep][i][j][k][l] = true;
else {
if (ok(k - 1, l, n) && !used[k - 1][l] &&
dp[rep - 1][i][j][k - 1][l])
dp[rep][i][j][k][l] = true;
if (ok(k + 1, l, n) && !used[k + 1][l] &&
dp[rep - 1][i][j][k + 1][l])
dp[rep][i][j][k][l] = true;
if (ok(k, l - 1, n) && !used[k][l - 1] &&
dp[rep - 1][i][j][k][l - 1])
dp[rep][i][j][k][l] = true;
if (ok(k, l + 1, n) && !used[k][l + 1] &&
dp[rep - 1][i][j][k][l + 1])
dp[rep][i][j][k][l] = true;
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cop[i][j] = false;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (used[i][j]) {
if (ok(i - 1, j, n) && uch[i - 1][j] != 'Y' && uch[i - 1][j] != 'Z')
cop[i - 1][j] = true;
if (ok(i + 1, j, n) && uch[i + 1][j] != 'Y' && uch[i + 1][j] != 'Z')
cop[i + 1][j] = true;
if (ok(i, j - 1, n) && uch[i][j - 1] != 'Y' && uch[i][j - 1] != 'Z')
cop[i][j - 1] = true;
if (ok(i, j + 1, n) && uch[i][j + 1] != 'Y' && uch[i][j + 1] != 'Z')
cop[i][j + 1] = true;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) used[i][j] = used[i][j] || cop[i][j];
}
int start = 0, finish = 2 * n * n + 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int num = (i - 1) * n + j;
if (uch[i][j] == 'Y' || uch[i][j] == 'Z') continue;
int c = uch[i][j] - '0';
edge[quant] = triple(start, num, 0, c);
g[start].push_back(quant);
quant++;
edge[quant] = triple(num, start, 0, 0);
g[num].push_back(quant);
quant++;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int num = (i - 1) * n + j + n * n;
if (kap[i][j] == 'Y' || kap[i][j] == 'Z') continue;
int c = kap[i][j] - '0';
edge[quant] = triple(num, finish, 0, c);
g[num].push_back(quant);
quant++;
edge[quant] = triple(finish, num, 0, 0);
g[finish].push_back(quant);
quant++;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
for (int l = 1; l <= n; l++) {
int num1 = (i - 1) * n + j, num2 = (k - 1) * n + l + n * n;
bool fl = false;
for (int rep = 0; rep <= t; rep++) fl = (fl || dp[rep][i][j][k][l]);
if (fl) {
edge[quant] = triple(num1, num2, 0, INF);
g[num1].push_back(quant);
quant++;
edge[quant] = triple(num2, num1, 0, 0);
g[num2].push_back(quant);
quant++;
}
}
for (int B = 8; B > 0; B /= 2) {
while (bfs(start, finish, B)) {
for (int i = start; i <= finish; i++) fir[i] = 0;
while (dfs(start, finish, INF, B) > 0)
;
}
}
int ans = 0;
for (int i = 0; i < (int)g[start].size(); i++) ans += edge[g[start][i]].f;
cout << ans;
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 7;
struct triple {
int a, b, f, c;
triple(int first = 0, int second = 0, int z = 0, int w = 0) {
a = first, b = second, f = z, c = w;
}
};
bool ok(int first, int second, int n) {
return (first > 0) && (first <= n) && (second > 0) && (second <= n);
}
triple edge[100005];
int quant = 0, fir[100005], dist[100005];
char uch[15][15], kap[15][15];
bool used[15][15], dp[65][15][15][15][15], cop[15][15];
vector<int> g[100005];
queue<int> qu;
bool bfs(int start, int finish, int B) {
for (int i = start; i <= finish; i++) dist[i] = INF;
dist[start] = 0;
qu.push(start);
while (!qu.empty()) {
int v = qu.front();
qu.pop();
for (int i = 0; i < (int)g[v].size(); i++) {
int num = g[v][i];
int u = edge[num].b;
if (edge[num].f + B <= edge[num].c && dist[v] + 1 < dist[u]) {
dist[u] = dist[v] + 1;
qu.push(u);
}
}
}
return dist[finish] != INF;
}
int dfs(int v, int finish, int cmin, int B) {
if (v == finish) return cmin;
for (; fir[v] < (int)g[v].size(); fir[v]++) {
int num = g[v][fir[v]];
int u = edge[num].b;
if (dist[u] == dist[v] + 1 && edge[num].f + B <= edge[num].c) {
int d = dfs(u, finish, min(cmin, edge[num].c - edge[num].f), B);
if (d != 0) {
edge[num].f += d;
edge[num ^ 1].f -= d;
return d;
}
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, t;
cin >> n >> t;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
cin >> uch[i][j];
if (uch[i][j] == 'Z') used[i][j] = true;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> kap[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (uch[i][j] != 'Z' && uch[i][j] != 'Y') dp[0][i][j][i][j] = true;
for (int rep = 1; rep <= t; rep++) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
for (int l = 1; l <= n; l++) {
if (uch[k][l] == 'Z' || uch[k][l] == 'Y' || used[k][l]) continue;
if (dp[rep - 1][i][j][k][l])
dp[rep][i][j][k][l] = true;
else {
if (ok(k - 1, l, n) && !used[k - 1][l] &&
dp[rep - 1][i][j][k - 1][l])
dp[rep][i][j][k][l] = true;
if (ok(k + 1, l, n) && !used[k + 1][l] &&
dp[rep - 1][i][j][k + 1][l])
dp[rep][i][j][k][l] = true;
if (ok(k, l - 1, n) && !used[k][l - 1] &&
dp[rep - 1][i][j][k][l - 1])
dp[rep][i][j][k][l] = true;
if (ok(k, l + 1, n) && !used[k][l + 1] &&
dp[rep - 1][i][j][k][l + 1])
dp[rep][i][j][k][l] = true;
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cop[i][j] = false;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (used[i][j]) {
if (ok(i - 1, j, n) && uch[i - 1][j] != 'Y' && uch[i - 1][j] != 'Z')
cop[i - 1][j] = true;
if (ok(i + 1, j, n) && uch[i + 1][j] != 'Y' && uch[i + 1][j] != 'Z')
cop[i + 1][j] = true;
if (ok(i, j - 1, n) && uch[i][j - 1] != 'Y' && uch[i][j - 1] != 'Z')
cop[i][j - 1] = true;
if (ok(i, j + 1, n) && uch[i][j + 1] != 'Y' && uch[i][j + 1] != 'Z')
cop[i][j + 1] = true;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) used[i][j] = used[i][j] || cop[i][j];
}
int start = 0, finish = 2 * n * n + 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int num = (i - 1) * n + j;
if (uch[i][j] == 'Y' || uch[i][j] == 'Z') continue;
int c = uch[i][j] - '0';
edge[quant] = triple(start, num, 0, c);
g[start].push_back(quant);
quant++;
edge[quant] = triple(num, start, 0, 0);
g[num].push_back(quant);
quant++;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int num = (i - 1) * n + j + n * n;
if (kap[i][j] == 'Y' || kap[i][j] == 'Z') continue;
int c = kap[i][j] - '0';
edge[quant] = triple(num, finish, 0, c);
g[num].push_back(quant);
quant++;
edge[quant] = triple(finish, num, 0, 0);
g[finish].push_back(quant);
quant++;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
for (int l = 1; l <= n; l++) {
int num1 = (i - 1) * n + j, num2 = (k - 1) * n + l + n * n;
bool fl = false;
for (int rep = 0; rep <= t; rep++) fl = (fl || dp[rep][i][j][k][l]);
if (fl) {
edge[quant] = triple(num1, num2, 0, INF);
g[num1].push_back(quant);
quant++;
edge[quant] = triple(num2, num1, 0, 0);
g[num2].push_back(quant);
quant++;
}
}
for (int B = 8; B > 0; B /= 2) {
while (bfs(start, finish, B)) {
for (int i = start; i <= finish; i++) fir[i] = 0;
while (dfs(start, finish, INF, B) > 0)
;
}
}
int ans = 0;
for (int i = 0; i < (int)g[start].size(); i++) ans += edge[g[start][i]].f;
cout << ans;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int rd() { return RAND_MAX == 32767 ? ((rand() << 15) ^ rand()) : rand(); }
const int maxn = 210;
const int maxe = 1000000;
const int inf = 1000000000;
int tot;
int d[maxn], g[maxn], h[maxn], cur[maxn], pre[maxn], flow[maxn],
c[12][12][12][12], f[12][12][12][12],
p[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int cap[maxe], nxt[maxe], vec[maxe];
char a[12][12], b[12][12];
queue<int> S;
queue<pair<int, int> > Q;
int max_flow(int s, int t, int n) {
for (int i = 0; i < n; i++) d[i] = n;
d[t] = 0;
S.push(t);
while (!S.empty()) {
int k = S.front();
S.pop();
for (int i = g[k]; i != -1; i = nxt[i])
if (cap[i ^ 1] && d[k] + 1 < d[vec[i]]) {
d[vec[i]] = d[k] + 1;
S.push(vec[i]);
}
}
for (int i = 0; i <= n; i++) h[i] = 0;
for (int i = 0; i < n; i++) {
h[d[i]]++;
cur[i] = g[i];
}
int k = s, res = 0;
flow[s] = inf;
while (d[s] < n) {
int kk = -1;
for (int i = cur[k]; i != -1; i = nxt[i])
if (cap[i] && d[k] == d[vec[i]] + 1) {
kk = vec[i];
pre[kk] = i ^ 1;
flow[kk] = min(flow[k], cap[i]);
cur[k] = i;
k = kk;
break;
}
if (kk == -1) {
if (--h[d[k]] == 0) break;
d[k] = n;
cur[k] = -1;
for (int i = g[k]; i != -1; i = nxt[i])
if (cap[i] && d[k] > d[vec[i]] + 1) {
d[k] = d[vec[i]] + 1;
cur[k] = i;
}
h[d[k]]++;
if (k != s) k = vec[pre[k]];
} else if (k == t) {
res += flow[t];
for (; k != s; k = vec[pre[k]]) {
cap[pre[k]] += flow[t];
cap[pre[k] ^ 1] -= flow[t];
}
}
}
return res;
}
void add_edge(int u, int v, int w) {
cap[tot] = w, vec[tot] = v, nxt[tot] = g[u], g[u] = tot++;
cap[tot] = 0, vec[tot] = u, nxt[tot] = g[v], g[v] = tot++;
}
int main() {
int n, m, sx, sy;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i][j] == 'Z') {
sx = i;
sy = j;
}
memset(c, -1, sizeof(c));
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++) {
c[x][y][x][y] = 0;
Q.push(make_pair(x, y));
while (!Q.empty()) {
int x0 = Q.front().first;
int y0 = Q.front().second;
Q.pop();
for (int i = 0; i < 4; i++) {
int x1 = x0 + p[i][0], y1 = y0 + p[i][1];
if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < n && a[x1][y1] != 'Y' &&
a[x1][y1] != 'Z' && c[x][y][x1][y1] == -1) {
c[x][y][x1][y1] = c[x][y][x0][y0] + 1;
Q.push(make_pair(x1, y1));
}
}
}
}
memset(f, -1, sizeof(f));
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++)
if (a[x][y] != 'Y' && a[x][y] != 'Z') {
f[x][y][x][y] = 0;
Q.push(make_pair(x, y));
while (!Q.empty()) {
int x0 = Q.front().first;
int y0 = Q.front().second;
Q.pop();
for (int i = 0; i < 4; i++) {
int x1 = x0 + p[i][0], y1 = y0 + p[i][1];
if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < n && a[x1][y1] != 'Y' &&
a[x1][y1] != 'Z' && f[x][y][x1][y1] == -1)
if (c[sx][sy][x1][y1] == -1 ||
f[x][y][x0][y0] + 1 <= c[sx][sy][x1][y1]) {
f[x][y][x1][y1] = f[x][y][x0][y0] + 1;
if (c[sx][sy][x1][y1] == -1 ||
f[x][y][x1][y1] < c[sx][sy][x1][y1])
Q.push(make_pair(x1, y1));
}
}
}
}
int s = n * n * 2, t = n * n * 2 + 1;
tot = 0;
memset(g, -1, sizeof(g));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (a[i][j] != 'Y' && a[i][j] != 'Z' && a[i][j] != '0') {
add_edge(s, i * n + j, a[i][j] - '0');
for (int u = 0; u < n; u++)
for (int v = 0; v < n; v++)
if (b[u][v] != 'Y' && b[u][v] != 'Z' && b[u][v] != '0' &&
f[i][j][u][v] != -1 && f[i][j][u][v] <= m)
add_edge(i * n + j, u * n + v + n * n, inf);
}
if (b[i][j] != 'Y' && b[i][j] != 'Z' && b[i][j] != '0')
add_edge(i * n + j + n * n, t, b[i][j] - '0');
}
cout << max_flow(s, t, n * n * 2 + 2);
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int rd() { return RAND_MAX == 32767 ? ((rand() << 15) ^ rand()) : rand(); }
const int maxn = 210;
const int maxe = 1000000;
const int inf = 1000000000;
int tot;
int d[maxn], g[maxn], h[maxn], cur[maxn], pre[maxn], flow[maxn],
c[12][12][12][12], f[12][12][12][12],
p[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int cap[maxe], nxt[maxe], vec[maxe];
char a[12][12], b[12][12];
queue<int> S;
queue<pair<int, int> > Q;
int max_flow(int s, int t, int n) {
for (int i = 0; i < n; i++) d[i] = n;
d[t] = 0;
S.push(t);
while (!S.empty()) {
int k = S.front();
S.pop();
for (int i = g[k]; i != -1; i = nxt[i])
if (cap[i ^ 1] && d[k] + 1 < d[vec[i]]) {
d[vec[i]] = d[k] + 1;
S.push(vec[i]);
}
}
for (int i = 0; i <= n; i++) h[i] = 0;
for (int i = 0; i < n; i++) {
h[d[i]]++;
cur[i] = g[i];
}
int k = s, res = 0;
flow[s] = inf;
while (d[s] < n) {
int kk = -1;
for (int i = cur[k]; i != -1; i = nxt[i])
if (cap[i] && d[k] == d[vec[i]] + 1) {
kk = vec[i];
pre[kk] = i ^ 1;
flow[kk] = min(flow[k], cap[i]);
cur[k] = i;
k = kk;
break;
}
if (kk == -1) {
if (--h[d[k]] == 0) break;
d[k] = n;
cur[k] = -1;
for (int i = g[k]; i != -1; i = nxt[i])
if (cap[i] && d[k] > d[vec[i]] + 1) {
d[k] = d[vec[i]] + 1;
cur[k] = i;
}
h[d[k]]++;
if (k != s) k = vec[pre[k]];
} else if (k == t) {
res += flow[t];
for (; k != s; k = vec[pre[k]]) {
cap[pre[k]] += flow[t];
cap[pre[k] ^ 1] -= flow[t];
}
}
}
return res;
}
void add_edge(int u, int v, int w) {
cap[tot] = w, vec[tot] = v, nxt[tot] = g[u], g[u] = tot++;
cap[tot] = 0, vec[tot] = u, nxt[tot] = g[v], g[v] = tot++;
}
int main() {
int n, m, sx, sy;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i][j] == 'Z') {
sx = i;
sy = j;
}
memset(c, -1, sizeof(c));
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++) {
c[x][y][x][y] = 0;
Q.push(make_pair(x, y));
while (!Q.empty()) {
int x0 = Q.front().first;
int y0 = Q.front().second;
Q.pop();
for (int i = 0; i < 4; i++) {
int x1 = x0 + p[i][0], y1 = y0 + p[i][1];
if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < n && a[x1][y1] != 'Y' &&
a[x1][y1] != 'Z' && c[x][y][x1][y1] == -1) {
c[x][y][x1][y1] = c[x][y][x0][y0] + 1;
Q.push(make_pair(x1, y1));
}
}
}
}
memset(f, -1, sizeof(f));
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++)
if (a[x][y] != 'Y' && a[x][y] != 'Z') {
f[x][y][x][y] = 0;
Q.push(make_pair(x, y));
while (!Q.empty()) {
int x0 = Q.front().first;
int y0 = Q.front().second;
Q.pop();
for (int i = 0; i < 4; i++) {
int x1 = x0 + p[i][0], y1 = y0 + p[i][1];
if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < n && a[x1][y1] != 'Y' &&
a[x1][y1] != 'Z' && f[x][y][x1][y1] == -1)
if (c[sx][sy][x1][y1] == -1 ||
f[x][y][x0][y0] + 1 <= c[sx][sy][x1][y1]) {
f[x][y][x1][y1] = f[x][y][x0][y0] + 1;
if (c[sx][sy][x1][y1] == -1 ||
f[x][y][x1][y1] < c[sx][sy][x1][y1])
Q.push(make_pair(x1, y1));
}
}
}
}
int s = n * n * 2, t = n * n * 2 + 1;
tot = 0;
memset(g, -1, sizeof(g));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (a[i][j] != 'Y' && a[i][j] != 'Z' && a[i][j] != '0') {
add_edge(s, i * n + j, a[i][j] - '0');
for (int u = 0; u < n; u++)
for (int v = 0; v < n; v++)
if (b[u][v] != 'Y' && b[u][v] != 'Z' && b[u][v] != '0' &&
f[i][j][u][v] != -1 && f[i][j][u][v] <= m)
add_edge(i * n + j, u * n + v + n * n, inf);
}
if (b[i][j] != 'Y' && b[i][j] != 'Z' && b[i][j] != '0')
add_edge(i * n + j + n * n, t, b[i][j] - '0');
}
cout << max_flow(s, t, n * n * 2 + 2);
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn = 300 + 10, maxm = 4e4 + 10;
const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
const int INF = 0x3f3f3f3f;
struct Graph {
int ndn, edn, last[maxn];
int u[maxm], v[maxm], flw[maxm], cap[maxm], nxt[maxm];
void init(int _n) {
ndn = _n;
edn = 0;
memset(last, -1, sizeof(last));
memset(flw, 0, sizeof(flw));
memset(cap, 0, sizeof(cap));
}
void adde(int _u, int _v, int _c) {
u[edn] = _u;
v[edn] = _v;
cap[edn] = _c;
nxt[edn] = last[_u];
last[_u] = edn++;
}
};
struct Dinic {
Graph *G;
int S, T, dist[maxn], cur[maxn];
int BFS();
int DFS(int, int);
int solve(Graph *, int, int);
};
int N, T;
char sci[15][15], cap[15][15];
int death[15][15], idsci[15][15], idcap[15][15];
int dist[15][15];
Graph G;
Dinic dinic;
bool judge(int x, int y) {
return x < 0 || x >= N || y < 0 || y >= N || !isdigit(sci[x][y]);
}
int main() {
int zx, zy;
G.init(1);
scanf("%d%d", &N, &T);
for (int i = 0; i < N; i++) {
scanf(" %s", sci[i]);
for (int j = 0; j < N; j++) {
if (isdigit(sci[i][j]) && sci[i][j] >= '1') {
idsci[i][j] = ++G.ndn;
G.adde(0, G.ndn, sci[i][j] - '0');
G.adde(G.ndn, 0, 0);
}
if (sci[i][j] == 'Z') {
zx = i;
zy = j;
}
}
}
for (int i = 0; i < N; i++) {
scanf(" %s", cap[i]);
for (int j = 0; j < N; j++) {
if (isdigit(cap[i][j]) && cap[i][j] >= '1') {
idcap[i][j] = ++G.ndn;
G.adde(G.ndn, 1, cap[i][j] - '0');
G.adde(1, G.ndn, 0);
}
}
}
queue<pair<int, int> > que;
memset(death, 0x3f, sizeof(death));
death[zx][zy] = 0;
que.push({zx, zy});
while (que.size()) {
pair<int, int> u = que.front();
que.pop();
int d = death[u.first][u.second] + 1;
for (int k = 0; k < 4; k++) {
int x = u.first + dir[k][0];
int y = u.second + dir[k][1];
if (judge(x, y) || death[x][y] < INF) continue;
death[x][y] = d;
que.push({x, y});
}
}
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (isdigit(sci[i][j]) && sci[i][j] >= '1') {
while (que.size()) que.pop();
memset(dist, 0x3f, sizeof(dist));
dist[i][j] = 0;
que.push({i, j});
while (que.size()) {
pair<int, int> u = que.front();
que.pop();
if (idcap[u.first][u.second]) {
G.adde(idsci[i][j], idcap[u.first][u.second], sci[i][j] - '0');
G.adde(idcap[u.first][u.second], idsci[i][j], 0);
}
int d = dist[u.first][u.second] + 1;
if (d > T || dist[u.first][u.second] >= death[u.first][u.second])
continue;
for (int k = 0; k < 4; k++) {
int x = u.first + dir[k][0];
int y = u.second + dir[k][1];
if (judge(x, y) || dist[x][y] < INF || death[x][y] < d) continue;
dist[x][y] = d;
que.push({x, y});
}
}
}
printf("%d\n", dinic.solve(&G, 0, 1));
return 0;
}
int Dinic::solve(Graph *g, int ts, int tt) {
G = g;
S = ts;
T = tt;
int res = 0;
while (BFS()) {
for (int i = 0; i <= G->ndn; i++) cur[i] = G->last[i];
res += DFS(S, 1e9);
}
return res;
}
int Dinic::BFS() {
memset(dist, -1, sizeof(dist));
dist[S] = 0;
queue<int> que;
que.push(S);
while (que.size()) {
int u = que.front();
que.pop();
for (int e = G->last[u]; ~e; e = G->nxt[e]) {
int v = G->v[e];
if (dist[v] == -1 && G->cap[e] > G->flw[e]) {
dist[v] = dist[u] + 1;
que.push(v);
}
}
}
return ~dist[T];
}
int Dinic::DFS(int u, int tmin) {
if (u == T || tmin == 0) return tmin;
int nflw = 0, f;
for (int &e = cur[u]; ~e; e = G->nxt[e]) {
int v = G->v[e];
if (dist[u] + 1 == dist[v] &&
(f = DFS(v, min(tmin, G->cap[e] - G->flw[e]))) > 0) {
G->flw[e] += f;
G->flw[e ^ 1] -= f;
nflw += f;
tmin -= f;
if (tmin == 0) break;
}
}
return nflw;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn = 300 + 10, maxm = 4e4 + 10;
const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
const int INF = 0x3f3f3f3f;
struct Graph {
int ndn, edn, last[maxn];
int u[maxm], v[maxm], flw[maxm], cap[maxm], nxt[maxm];
void init(int _n) {
ndn = _n;
edn = 0;
memset(last, -1, sizeof(last));
memset(flw, 0, sizeof(flw));
memset(cap, 0, sizeof(cap));
}
void adde(int _u, int _v, int _c) {
u[edn] = _u;
v[edn] = _v;
cap[edn] = _c;
nxt[edn] = last[_u];
last[_u] = edn++;
}
};
struct Dinic {
Graph *G;
int S, T, dist[maxn], cur[maxn];
int BFS();
int DFS(int, int);
int solve(Graph *, int, int);
};
int N, T;
char sci[15][15], cap[15][15];
int death[15][15], idsci[15][15], idcap[15][15];
int dist[15][15];
Graph G;
Dinic dinic;
bool judge(int x, int y) {
return x < 0 || x >= N || y < 0 || y >= N || !isdigit(sci[x][y]);
}
int main() {
int zx, zy;
G.init(1);
scanf("%d%d", &N, &T);
for (int i = 0; i < N; i++) {
scanf(" %s", sci[i]);
for (int j = 0; j < N; j++) {
if (isdigit(sci[i][j]) && sci[i][j] >= '1') {
idsci[i][j] = ++G.ndn;
G.adde(0, G.ndn, sci[i][j] - '0');
G.adde(G.ndn, 0, 0);
}
if (sci[i][j] == 'Z') {
zx = i;
zy = j;
}
}
}
for (int i = 0; i < N; i++) {
scanf(" %s", cap[i]);
for (int j = 0; j < N; j++) {
if (isdigit(cap[i][j]) && cap[i][j] >= '1') {
idcap[i][j] = ++G.ndn;
G.adde(G.ndn, 1, cap[i][j] - '0');
G.adde(1, G.ndn, 0);
}
}
}
queue<pair<int, int> > que;
memset(death, 0x3f, sizeof(death));
death[zx][zy] = 0;
que.push({zx, zy});
while (que.size()) {
pair<int, int> u = que.front();
que.pop();
int d = death[u.first][u.second] + 1;
for (int k = 0; k < 4; k++) {
int x = u.first + dir[k][0];
int y = u.second + dir[k][1];
if (judge(x, y) || death[x][y] < INF) continue;
death[x][y] = d;
que.push({x, y});
}
}
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (isdigit(sci[i][j]) && sci[i][j] >= '1') {
while (que.size()) que.pop();
memset(dist, 0x3f, sizeof(dist));
dist[i][j] = 0;
que.push({i, j});
while (que.size()) {
pair<int, int> u = que.front();
que.pop();
if (idcap[u.first][u.second]) {
G.adde(idsci[i][j], idcap[u.first][u.second], sci[i][j] - '0');
G.adde(idcap[u.first][u.second], idsci[i][j], 0);
}
int d = dist[u.first][u.second] + 1;
if (d > T || dist[u.first][u.second] >= death[u.first][u.second])
continue;
for (int k = 0; k < 4; k++) {
int x = u.first + dir[k][0];
int y = u.second + dir[k][1];
if (judge(x, y) || dist[x][y] < INF || death[x][y] < d) continue;
dist[x][y] = d;
que.push({x, y});
}
}
}
printf("%d\n", dinic.solve(&G, 0, 1));
return 0;
}
int Dinic::solve(Graph *g, int ts, int tt) {
G = g;
S = ts;
T = tt;
int res = 0;
while (BFS()) {
for (int i = 0; i <= G->ndn; i++) cur[i] = G->last[i];
res += DFS(S, 1e9);
}
return res;
}
int Dinic::BFS() {
memset(dist, -1, sizeof(dist));
dist[S] = 0;
queue<int> que;
que.push(S);
while (que.size()) {
int u = que.front();
que.pop();
for (int e = G->last[u]; ~e; e = G->nxt[e]) {
int v = G->v[e];
if (dist[v] == -1 && G->cap[e] > G->flw[e]) {
dist[v] = dist[u] + 1;
que.push(v);
}
}
}
return ~dist[T];
}
int Dinic::DFS(int u, int tmin) {
if (u == T || tmin == 0) return tmin;
int nflw = 0, f;
for (int &e = cur[u]; ~e; e = G->nxt[e]) {
int v = G->v[e];
if (dist[u] + 1 == dist[v] &&
(f = DFS(v, min(tmin, G->cap[e] - G->flw[e]))) > 0) {
G->flw[e] += f;
G->flw[e ^ 1] -= f;
nflw += f;
tmin -= f;
if (tmin == 0) break;
}
}
return nflw;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int inf = 199999999;
const int maxn = 6600;
const int maxm = 66000;
int n, add, ans, s, t, low;
int hea[maxn], nex[maxm], vv[maxm], cap[maxm], flow[maxm];
int dis[maxn], arc[maxn], gap[maxn], pre[maxn];
inline bool relax(int &u) {
for (int &e = arc[u]; e != -1; e = nex[e])
if (dis[vv[e]] == dis[u] - 1 && cap[e] > flow[e]) {
low = min(low, cap[e] - flow[e]);
pre[vv[e]] = u;
u = vv[e];
if (u == t) {
while (u != s) {
u = pre[u];
flow[arc[u]] += low;
flow[arc[u] ^ 1] -= low;
}
ans += low;
low = inf;
}
return true;
}
return false;
}
void sap(int n) {
memset(flow, 0, sizeof(flow));
memset(arc, -1, sizeof(arc));
memset(dis, 0, sizeof(dis));
memset(gap, 0, sizeof(gap));
int u, e, mind;
gap[0] = n;
ans = 0;
low = inf;
u = s;
pre[s] = s;
while (dis[s] < n)
if (!relax(u)) {
mind = n;
for (e = hea[u]; e != -1; e = nex[e])
if (mind > dis[vv[e]] && cap[e] > flow[e]) {
arc[u] = e;
mind = dis[vv[e]];
}
int &tmp = dis[u];
u = pre[u];
if (gap[tmp] == 1) return;
gap[tmp]--;
tmp = mind + 1;
gap[tmp]++;
}
}
inline void adj(int a, int b, int c) {
vv[add] = b;
cap[add] = c;
nex[add] = hea[a];
hea[a] = add++;
vv[add] = a;
cap[add] = 0;
nex[add] = hea[b];
hea[b] = add++;
}
int nn, tt, tim[12][12];
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
char aa[12][12], bb[12][12];
queue<int> q;
void bfs() {
for (int i = 0; i < nn; ++i)
for (int j = 0; j < nn; ++j) tim[i][j] = tt;
int x = 0, y = 0;
for (int i = 0; i < nn; ++i)
for (int j = 0; j < nn; ++j)
if (aa[i][j] == 'Z') x = i, y = j;
q.push(x);
q.push(y);
tim[x][y] = 0;
while (!q.empty()) {
x = q.front();
q.pop();
y = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {
int xx = dx[i] + x, yy = dy[i] + y;
if (xx < 0 || xx >= nn) continue;
if (yy < 0 || yy >= nn) continue;
if (aa[xx][yy] == 'Y') continue;
if (tim[xx][yy] <= tim[x][y] + 1) continue;
tim[xx][yy] = tim[x][y] + 1;
q.push(xx);
q.push(yy);
}
}
}
void init() {
memset(hea, -1, sizeof(hea));
add = 0;
s = 0;
t = 6210;
for (int i = 0; i < nn; ++i)
for (int j = 0; j < nn; ++j)
if ('0' <= aa[i][j] && aa[i][j] <= '9')
adj(s, i * nn + j + 1, aa[i][j] - '0');
for (int i = 0; i < nn; ++i)
for (int j = 0; j < nn; ++j)
if ('0' <= bb[i][j] && bb[i][j] <= '9')
adj((tt + 1) * nn * nn + i * nn + j + 1, t, bb[i][j] - '0');
for (int i = 0; i < nn; ++i) {
for (int j = 0; j < nn; ++j) {
if (aa[i][j] == 'Y' || aa[i][j] == 'Z') continue;
for (int k = 0; k <= tim[i][j]; ++k) {
adj(k * nn * nn + i * nn + j + 1, (tt + 1) * nn * nn + i * nn + j + 1,
1100);
if (k == tim[i][j]) continue;
for (int kk = 0; kk < 4; ++kk) {
int ii = i + dx[kk], jj = j + dy[kk];
if (ii < 0 || ii >= nn) continue;
if (jj < 0 || jj >= nn) continue;
if (aa[ii][jj] == 'Y' || aa[ii][jj] == 'Z') continue;
if (tim[ii][jj] < k + 1) continue;
adj(k * nn * nn + i * nn + j + 1,
(k + 1) * nn * nn + ii * nn + jj + 1, 1100);
}
}
}
}
}
int main() {
scanf("%d %d", &nn, &tt);
for (int i = 0; i < nn; ++i) scanf("%s", aa[i]);
for (int i = 0; i < nn; ++i) scanf("%s", bb[i]);
bfs();
init();
sap(6211);
printf("%d\n", ans);
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 199999999;
const int maxn = 6600;
const int maxm = 66000;
int n, add, ans, s, t, low;
int hea[maxn], nex[maxm], vv[maxm], cap[maxm], flow[maxm];
int dis[maxn], arc[maxn], gap[maxn], pre[maxn];
inline bool relax(int &u) {
for (int &e = arc[u]; e != -1; e = nex[e])
if (dis[vv[e]] == dis[u] - 1 && cap[e] > flow[e]) {
low = min(low, cap[e] - flow[e]);
pre[vv[e]] = u;
u = vv[e];
if (u == t) {
while (u != s) {
u = pre[u];
flow[arc[u]] += low;
flow[arc[u] ^ 1] -= low;
}
ans += low;
low = inf;
}
return true;
}
return false;
}
void sap(int n) {
memset(flow, 0, sizeof(flow));
memset(arc, -1, sizeof(arc));
memset(dis, 0, sizeof(dis));
memset(gap, 0, sizeof(gap));
int u, e, mind;
gap[0] = n;
ans = 0;
low = inf;
u = s;
pre[s] = s;
while (dis[s] < n)
if (!relax(u)) {
mind = n;
for (e = hea[u]; e != -1; e = nex[e])
if (mind > dis[vv[e]] && cap[e] > flow[e]) {
arc[u] = e;
mind = dis[vv[e]];
}
int &tmp = dis[u];
u = pre[u];
if (gap[tmp] == 1) return;
gap[tmp]--;
tmp = mind + 1;
gap[tmp]++;
}
}
inline void adj(int a, int b, int c) {
vv[add] = b;
cap[add] = c;
nex[add] = hea[a];
hea[a] = add++;
vv[add] = a;
cap[add] = 0;
nex[add] = hea[b];
hea[b] = add++;
}
int nn, tt, tim[12][12];
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
char aa[12][12], bb[12][12];
queue<int> q;
void bfs() {
for (int i = 0; i < nn; ++i)
for (int j = 0; j < nn; ++j) tim[i][j] = tt;
int x = 0, y = 0;
for (int i = 0; i < nn; ++i)
for (int j = 0; j < nn; ++j)
if (aa[i][j] == 'Z') x = i, y = j;
q.push(x);
q.push(y);
tim[x][y] = 0;
while (!q.empty()) {
x = q.front();
q.pop();
y = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {
int xx = dx[i] + x, yy = dy[i] + y;
if (xx < 0 || xx >= nn) continue;
if (yy < 0 || yy >= nn) continue;
if (aa[xx][yy] == 'Y') continue;
if (tim[xx][yy] <= tim[x][y] + 1) continue;
tim[xx][yy] = tim[x][y] + 1;
q.push(xx);
q.push(yy);
}
}
}
void init() {
memset(hea, -1, sizeof(hea));
add = 0;
s = 0;
t = 6210;
for (int i = 0; i < nn; ++i)
for (int j = 0; j < nn; ++j)
if ('0' <= aa[i][j] && aa[i][j] <= '9')
adj(s, i * nn + j + 1, aa[i][j] - '0');
for (int i = 0; i < nn; ++i)
for (int j = 0; j < nn; ++j)
if ('0' <= bb[i][j] && bb[i][j] <= '9')
adj((tt + 1) * nn * nn + i * nn + j + 1, t, bb[i][j] - '0');
for (int i = 0; i < nn; ++i) {
for (int j = 0; j < nn; ++j) {
if (aa[i][j] == 'Y' || aa[i][j] == 'Z') continue;
for (int k = 0; k <= tim[i][j]; ++k) {
adj(k * nn * nn + i * nn + j + 1, (tt + 1) * nn * nn + i * nn + j + 1,
1100);
if (k == tim[i][j]) continue;
for (int kk = 0; kk < 4; ++kk) {
int ii = i + dx[kk], jj = j + dy[kk];
if (ii < 0 || ii >= nn) continue;
if (jj < 0 || jj >= nn) continue;
if (aa[ii][jj] == 'Y' || aa[ii][jj] == 'Z') continue;
if (tim[ii][jj] < k + 1) continue;
adj(k * nn * nn + i * nn + j + 1,
(k + 1) * nn * nn + ii * nn + jj + 1, 1100);
}
}
}
}
}
int main() {
scanf("%d %d", &nn, &tt);
for (int i = 0; i < nn; ++i) scanf("%s", aa[i]);
for (int i = 0; i < nn; ++i) scanf("%s", bb[i]);
bfs();
init();
sap(6211);
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, T, m, tempo[12][12], vis[12][12][80], ok[12][12];
char matA[12][12], matB[12][12];
vector<int> A[12][12], B[12][12], adj[4004];
int p[4004], vi[4004];
void bfs(int x, int y) {
queue<pair<int, int> > q;
memset(tempo, 0x3f3f3f3f, sizeof(tempo));
tempo[x][y] = 0;
q.push(pair<int, int>(x, y));
while (!q.empty()) {
int i = q.front().first, j = q.front().second;
q.pop();
if (i > 0 && matA[i - 1][j] != 'Y' && matA[i - 1][j] != 'Z' &&
tempo[i - 1][j] == 0x3f3f3f3f) {
tempo[i - 1][j] = tempo[i][j] + 1;
q.push(pair<int, int>(i - 1, j));
}
if (i < m - 1 && matA[i + 1][j] != 'Y' && matA[i + 1][j] != 'Z' &&
tempo[i + 1][j] == 0x3f3f3f3f) {
tempo[i + 1][j] = tempo[i][j] + 1;
q.push(pair<int, int>(i + 1, j));
}
if (j > 0 && matA[i][j - 1] != 'Y' && matA[i][j - 1] != 'Z' &&
tempo[i][j - 1] == 0x3f3f3f3f) {
tempo[i][j - 1] = tempo[i][j] + 1;
q.push(pair<int, int>(i, j - 1));
}
if (j < m - 1 && matA[i][j + 1] != 'Y' && matA[i][j + 1] != 'Z' &&
tempo[i][j + 1] == 0x3f3f3f3f) {
tempo[i][j + 1] = tempo[i][j] + 1;
q.push(pair<int, int>(i, j + 1));
}
}
return;
}
void dfs(int i, int j, int t, int x, int y) {
if (t > T) return;
vis[i][j][t] = 1;
if (!ok[i][j]) {
ok[i][j] = 1;
for (int a = 0; a < A[x][y].size(); a++)
for (int b = 0; b < B[i][j].size(); b++) {
int u, v;
u = A[x][y][a];
v = B[i][j][b];
adj[u].push_back(v);
}
}
if (tempo[i][j] == t) return;
if (i > 0 && matA[i - 1][j] != 'Y' && matA[i - 1][j] != 'Z' &&
tempo[i - 1][j] >= t + 1 && !vis[i - 1][j][t + 1])
dfs(i - 1, j, t + 1, x, y);
if (i < m - 1 && matA[i + 1][j] != 'Y' && matA[i + 1][j] != 'Z' &&
tempo[i + 1][j] >= t + 1 && !vis[i + 1][j][t + 1])
dfs(i + 1, j, t + 1, x, y);
if (j > 0 && matA[i][j - 1] != 'Y' && matA[i][j - 1] != 'Z' &&
tempo[i][j - 1] >= t + 1 && !vis[i][j - 1][t + 1])
dfs(i, j - 1, t + 1, x, y);
if (j < m - 1 && matA[i][j + 1] != 'Y' && matA[i][j + 1] != 'Z' &&
tempo[i][j + 1] >= t + 1 && !vis[i][j + 1][t + 1])
dfs(i, j + 1, t + 1, x, y);
return;
}
bool dfs2(int x) {
for (int i = 0; i < adj[x].size(); i++) {
int y = adj[x][i];
if (vi[y]) continue;
vi[y] = 1;
if (p[y] == -1 || dfs2(p[y])) {
p[y] = x;
return true;
}
}
return false;
}
int max_match() {
int res = 0;
memset(p, -1, sizeof(p));
for (int i = 0; i < n; i++) {
memset(vi, 0, sizeof(vi));
if (dfs2(i)) ++res;
}
return res;
}
int main() {
int x, y;
cin >> m >> T;
n = 0;
for (int i = 0; i < m; i++) cin >> matA[i];
for (int i = 0; i < m; i++) cin >> matB[i];
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++) {
if (matA[i][j] != 'Y' && matA[i][j] != 'Z') {
int d = matA[i][j] - '0';
while (d--) A[i][j].push_back(n++);
d = matB[i][j] - '0';
while (d--) B[i][j].push_back(n++);
}
if (matA[i][j] == 'Z') {
x = i;
y = j;
}
}
bfs(x, y);
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
if (matA[i][j] != 'Y' && matA[i][j] != 'Z') {
memset(vis, 0, sizeof(vis));
memset(ok, 0, sizeof(ok));
dfs(i, j, 0, i, j);
}
cout << max_match() << endl;
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, T, m, tempo[12][12], vis[12][12][80], ok[12][12];
char matA[12][12], matB[12][12];
vector<int> A[12][12], B[12][12], adj[4004];
int p[4004], vi[4004];
void bfs(int x, int y) {
queue<pair<int, int> > q;
memset(tempo, 0x3f3f3f3f, sizeof(tempo));
tempo[x][y] = 0;
q.push(pair<int, int>(x, y));
while (!q.empty()) {
int i = q.front().first, j = q.front().second;
q.pop();
if (i > 0 && matA[i - 1][j] != 'Y' && matA[i - 1][j] != 'Z' &&
tempo[i - 1][j] == 0x3f3f3f3f) {
tempo[i - 1][j] = tempo[i][j] + 1;
q.push(pair<int, int>(i - 1, j));
}
if (i < m - 1 && matA[i + 1][j] != 'Y' && matA[i + 1][j] != 'Z' &&
tempo[i + 1][j] == 0x3f3f3f3f) {
tempo[i + 1][j] = tempo[i][j] + 1;
q.push(pair<int, int>(i + 1, j));
}
if (j > 0 && matA[i][j - 1] != 'Y' && matA[i][j - 1] != 'Z' &&
tempo[i][j - 1] == 0x3f3f3f3f) {
tempo[i][j - 1] = tempo[i][j] + 1;
q.push(pair<int, int>(i, j - 1));
}
if (j < m - 1 && matA[i][j + 1] != 'Y' && matA[i][j + 1] != 'Z' &&
tempo[i][j + 1] == 0x3f3f3f3f) {
tempo[i][j + 1] = tempo[i][j] + 1;
q.push(pair<int, int>(i, j + 1));
}
}
return;
}
void dfs(int i, int j, int t, int x, int y) {
if (t > T) return;
vis[i][j][t] = 1;
if (!ok[i][j]) {
ok[i][j] = 1;
for (int a = 0; a < A[x][y].size(); a++)
for (int b = 0; b < B[i][j].size(); b++) {
int u, v;
u = A[x][y][a];
v = B[i][j][b];
adj[u].push_back(v);
}
}
if (tempo[i][j] == t) return;
if (i > 0 && matA[i - 1][j] != 'Y' && matA[i - 1][j] != 'Z' &&
tempo[i - 1][j] >= t + 1 && !vis[i - 1][j][t + 1])
dfs(i - 1, j, t + 1, x, y);
if (i < m - 1 && matA[i + 1][j] != 'Y' && matA[i + 1][j] != 'Z' &&
tempo[i + 1][j] >= t + 1 && !vis[i + 1][j][t + 1])
dfs(i + 1, j, t + 1, x, y);
if (j > 0 && matA[i][j - 1] != 'Y' && matA[i][j - 1] != 'Z' &&
tempo[i][j - 1] >= t + 1 && !vis[i][j - 1][t + 1])
dfs(i, j - 1, t + 1, x, y);
if (j < m - 1 && matA[i][j + 1] != 'Y' && matA[i][j + 1] != 'Z' &&
tempo[i][j + 1] >= t + 1 && !vis[i][j + 1][t + 1])
dfs(i, j + 1, t + 1, x, y);
return;
}
bool dfs2(int x) {
for (int i = 0; i < adj[x].size(); i++) {
int y = adj[x][i];
if (vi[y]) continue;
vi[y] = 1;
if (p[y] == -1 || dfs2(p[y])) {
p[y] = x;
return true;
}
}
return false;
}
int max_match() {
int res = 0;
memset(p, -1, sizeof(p));
for (int i = 0; i < n; i++) {
memset(vi, 0, sizeof(vi));
if (dfs2(i)) ++res;
}
return res;
}
int main() {
int x, y;
cin >> m >> T;
n = 0;
for (int i = 0; i < m; i++) cin >> matA[i];
for (int i = 0; i < m; i++) cin >> matB[i];
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++) {
if (matA[i][j] != 'Y' && matA[i][j] != 'Z') {
int d = matA[i][j] - '0';
while (d--) A[i][j].push_back(n++);
d = matB[i][j] - '0';
while (d--) B[i][j].push_back(n++);
}
if (matA[i][j] == 'Z') {
x = i;
y = j;
}
}
bfs(x, y);
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
if (matA[i][j] != 'Y' && matA[i][j] != 'Z') {
memset(vis, 0, sizeof(vis));
memset(ok, 0, sizeof(ok));
dfs(i, j, 0, i, j);
}
cout << max_match() << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3F3F3F3F;
int K, T;
int dist[20][20][20][20];
int cont[20][20];
char SCI[20][20];
char CAP[20][20];
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};
const int MAXV = 2000;
int M, N;
int PAR[MAXV];
int MATCH;
int X[MAXV], Y[MAXV];
vector<int> adj[MAXV];
int disc[MAXV];
int augment(int u, int c) {
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (disc[v] != c) {
disc[v] = c;
if (PAR[v] == -1 || augment(PAR[v], c))
return (PAR[v] = u, PAR[u] = v, 1);
}
}
return 0;
}
int bpm() {
MATCH = 0;
for (int i = 0; i < M + N; i++) disc[i] = PAR[i] = -1;
for (int i = 0; i < M; i++)
if (adj[i].size() && PAR[i] == -1) MATCH += augment(i, i);
return MATCH;
}
int main() {
scanf(" %d %d ", &K, &T);
for (int i = 0; i < K; i++) {
scanf(" %s ", SCI[i]);
}
for (int i = 0; i < K; i++) {
scanf(" %s ", CAP[i]);
}
{
int y, x;
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++)
if (SCI[i][j] == 'Z') {
y = i;
x = j;
break;
}
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++) cont[i][j] = INF;
queue<pair<int, int> > q;
q.push(pair<int, int>(y, x));
cont[y][x] = 0;
while (!q.empty()) {
pair<int, int> u = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int ny = u.first + dy[i];
int nx = u.second + dx[i];
if (ny < 0 || nx < 0 || ny >= K || nx >= K) continue;
if (SCI[ny][nx] == 'Z' || SCI[ny][nx] == 'Y' || cont[ny][nx] != INF)
continue;
cont[ny][nx] = cont[u.first][u.second] + 1;
q.push(pair<int, int>(ny, nx));
}
}
}
for (int y = 0; y < K; y++)
for (int x = 0; x < K; x++) {
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++) dist[y][x][i][j] = INF;
queue<pair<int, int> > q;
q.push(pair<int, int>(y, x));
dist[y][x][y][x] = 0;
while (!q.empty()) {
pair<int, int> u = q.front();
q.pop();
if (dist[y][x][u.first][u.second] >= min(T, cont[u.first][u.second]))
continue;
for (int i = 0; i < 4; i++) {
int ny = u.first + dy[i];
int nx = u.second + dx[i];
if (ny < 0 || nx < 0 || ny >= K || nx >= K) continue;
if (SCI[ny][nx] == 'Z' || SCI[ny][nx] == 'Y' ||
dist[y][x][ny][nx] != INF)
continue;
if (dist[y][x][u.first][u.second] + 1 > min(T, cont[ny][nx]))
continue;
dist[y][x][ny][nx] = dist[y][x][u.first][u.second] + 1;
q.push(pair<int, int>(ny, nx));
}
}
}
M = N = 0;
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++)
if (SCI[i][j] > '0' && SCI[i][j] <= '9') {
int c = SCI[i][j] - '0';
for (int k = 0; k < c; k++) {
X[M] = i;
Y[M] = j;
M++;
}
}
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++)
if (CAP[i][j] > '0' && CAP[i][j] <= '9') {
int c = CAP[i][j] - '0';
for (int k = 0; k < c; k++) {
X[M + N] = i;
Y[M + N] = j;
N++;
}
}
for (int i = 0; i < M; i++)
for (int j = M; j < M + N; j++)
if (dist[X[i]][Y[i]][X[j]][Y[j]] != INF) {
adj[i].push_back(j);
adj[j].push_back(i);
}
printf("%d\n", bpm());
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3F3F3F3F;
int K, T;
int dist[20][20][20][20];
int cont[20][20];
char SCI[20][20];
char CAP[20][20];
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};
const int MAXV = 2000;
int M, N;
int PAR[MAXV];
int MATCH;
int X[MAXV], Y[MAXV];
vector<int> adj[MAXV];
int disc[MAXV];
int augment(int u, int c) {
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (disc[v] != c) {
disc[v] = c;
if (PAR[v] == -1 || augment(PAR[v], c))
return (PAR[v] = u, PAR[u] = v, 1);
}
}
return 0;
}
int bpm() {
MATCH = 0;
for (int i = 0; i < M + N; i++) disc[i] = PAR[i] = -1;
for (int i = 0; i < M; i++)
if (adj[i].size() && PAR[i] == -1) MATCH += augment(i, i);
return MATCH;
}
int main() {
scanf(" %d %d ", &K, &T);
for (int i = 0; i < K; i++) {
scanf(" %s ", SCI[i]);
}
for (int i = 0; i < K; i++) {
scanf(" %s ", CAP[i]);
}
{
int y, x;
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++)
if (SCI[i][j] == 'Z') {
y = i;
x = j;
break;
}
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++) cont[i][j] = INF;
queue<pair<int, int> > q;
q.push(pair<int, int>(y, x));
cont[y][x] = 0;
while (!q.empty()) {
pair<int, int> u = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int ny = u.first + dy[i];
int nx = u.second + dx[i];
if (ny < 0 || nx < 0 || ny >= K || nx >= K) continue;
if (SCI[ny][nx] == 'Z' || SCI[ny][nx] == 'Y' || cont[ny][nx] != INF)
continue;
cont[ny][nx] = cont[u.first][u.second] + 1;
q.push(pair<int, int>(ny, nx));
}
}
}
for (int y = 0; y < K; y++)
for (int x = 0; x < K; x++) {
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++) dist[y][x][i][j] = INF;
queue<pair<int, int> > q;
q.push(pair<int, int>(y, x));
dist[y][x][y][x] = 0;
while (!q.empty()) {
pair<int, int> u = q.front();
q.pop();
if (dist[y][x][u.first][u.second] >= min(T, cont[u.first][u.second]))
continue;
for (int i = 0; i < 4; i++) {
int ny = u.first + dy[i];
int nx = u.second + dx[i];
if (ny < 0 || nx < 0 || ny >= K || nx >= K) continue;
if (SCI[ny][nx] == 'Z' || SCI[ny][nx] == 'Y' ||
dist[y][x][ny][nx] != INF)
continue;
if (dist[y][x][u.first][u.second] + 1 > min(T, cont[ny][nx]))
continue;
dist[y][x][ny][nx] = dist[y][x][u.first][u.second] + 1;
q.push(pair<int, int>(ny, nx));
}
}
}
M = N = 0;
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++)
if (SCI[i][j] > '0' && SCI[i][j] <= '9') {
int c = SCI[i][j] - '0';
for (int k = 0; k < c; k++) {
X[M] = i;
Y[M] = j;
M++;
}
}
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++)
if (CAP[i][j] > '0' && CAP[i][j] <= '9') {
int c = CAP[i][j] - '0';
for (int k = 0; k < c; k++) {
X[M + N] = i;
Y[M + N] = j;
N++;
}
}
for (int i = 0; i < M; i++)
for (int j = M; j < M + N; j++)
if (dist[X[i]][Y[i]][X[j]][Y[j]] != INF) {
adj[i].push_back(j);
adj[j].push_back(i);
}
printf("%d\n", bpm());
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
const int M = 2001;
const int INF = 1e9;
const int X[] = {-1, 0, 1, 0};
const int Y[] = {0, 1, 0, -1};
int n, t, caps;
char scy[N][N];
char cap[N][N];
int r_dist[N][N];
int s_dist[N][N];
int c_id[N][N][10];
int s_id[N][N][10];
vector<int> graph[M];
bool valid(int i, int j) {
if (i < 0 or j < 0 or i >= n or j >= n) return false;
return isdigit(scy[i][j]);
}
void bfs(int si, int sj, int dist[N][N], bool flag) {
queue<pair<int, int> > q;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) dist[i][j] = INF;
dist[si][sj] = 0;
q.push({si, sj});
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
q.pop();
for (int dir = 0; dir < 4; dir++) {
int x = i + X[dir], y = j + Y[dir];
if (!valid(x, y)) continue;
if (dist[x][y] > dist[i][j] + 1) {
dist[x][y] = dist[i][j] + 1;
if (flag or dist[x][y] < r_dist[x][y]) q.push({x, y});
}
}
}
}
void build_graph(int x, int y) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (isdigit(cap[i][j]) and s_dist[i][j] <= min(t, r_dist[i][j]))
for (int k = 0; k < (scy[x][y] - '0'); k++)
for (int l = 0; l < (cap[i][j] - '0'); l++)
graph[s_id[x][y][k]].push_back(c_id[i][j][l]);
}
int match[M];
bool mark[M];
int augment(int u) {
if (mark[u]) return 0;
mark[u] = true;
for (int v : graph[u])
if (match[v] == -1 or augment(match[v])) {
match[v] = u;
return 1;
}
return 0;
}
int main() {
int scys = 0;
scanf("%d %d", &n, &t);
for (int i = 0; i < n; i++) {
scanf("%s", scy[i]);
for (int j = 0; j < n; j++)
if (isdigit(scy[i][j]))
for (int k = 0; k < (scy[i][j] - '0'); k++) s_id[i][j][k] = scys++;
}
int caps = scys;
for (int i = 0; i < n; i++) {
scanf("%s", cap[i]);
for (int j = 0; j < n; j++) {
if (isdigit(cap[i][j]))
for (int k = 0; k < (cap[i][j] - '0'); k++) c_id[i][j][k] = caps++;
if (cap[i][j] == 'Z') bfs(i, j, r_dist, true);
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (isdigit(scy[i][j])) {
bfs(i, j, s_dist, false);
build_graph(i, j);
}
int ans = 0;
memset(match, -1, sizeof match);
for (int i = 0; i < scys; i++) {
memset(mark, false, sizeof mark);
ans += augment(i);
}
printf("%d\n", ans);
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
const int M = 2001;
const int INF = 1e9;
const int X[] = {-1, 0, 1, 0};
const int Y[] = {0, 1, 0, -1};
int n, t, caps;
char scy[N][N];
char cap[N][N];
int r_dist[N][N];
int s_dist[N][N];
int c_id[N][N][10];
int s_id[N][N][10];
vector<int> graph[M];
bool valid(int i, int j) {
if (i < 0 or j < 0 or i >= n or j >= n) return false;
return isdigit(scy[i][j]);
}
void bfs(int si, int sj, int dist[N][N], bool flag) {
queue<pair<int, int> > q;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) dist[i][j] = INF;
dist[si][sj] = 0;
q.push({si, sj});
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
q.pop();
for (int dir = 0; dir < 4; dir++) {
int x = i + X[dir], y = j + Y[dir];
if (!valid(x, y)) continue;
if (dist[x][y] > dist[i][j] + 1) {
dist[x][y] = dist[i][j] + 1;
if (flag or dist[x][y] < r_dist[x][y]) q.push({x, y});
}
}
}
}
void build_graph(int x, int y) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (isdigit(cap[i][j]) and s_dist[i][j] <= min(t, r_dist[i][j]))
for (int k = 0; k < (scy[x][y] - '0'); k++)
for (int l = 0; l < (cap[i][j] - '0'); l++)
graph[s_id[x][y][k]].push_back(c_id[i][j][l]);
}
int match[M];
bool mark[M];
int augment(int u) {
if (mark[u]) return 0;
mark[u] = true;
for (int v : graph[u])
if (match[v] == -1 or augment(match[v])) {
match[v] = u;
return 1;
}
return 0;
}
int main() {
int scys = 0;
scanf("%d %d", &n, &t);
for (int i = 0; i < n; i++) {
scanf("%s", scy[i]);
for (int j = 0; j < n; j++)
if (isdigit(scy[i][j]))
for (int k = 0; k < (scy[i][j] - '0'); k++) s_id[i][j][k] = scys++;
}
int caps = scys;
for (int i = 0; i < n; i++) {
scanf("%s", cap[i]);
for (int j = 0; j < n; j++) {
if (isdigit(cap[i][j]))
for (int k = 0; k < (cap[i][j] - '0'); k++) c_id[i][j][k] = caps++;
if (cap[i][j] == 'Z') bfs(i, j, r_dist, true);
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (isdigit(scy[i][j])) {
bfs(i, j, s_dist, false);
build_graph(i, j);
}
int ans = 0;
memset(match, -1, sizeof match);
for (int i = 0; i < scys; i++) {
memset(mark, false, sizeof mark);
ans += augment(i);
}
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 10 + 5, inf = 1e9 + 5;
int n, boom;
char sci[N][N], cap[N][N];
int id[N][N];
pair<int, int> reactor;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
bool valid(int r, int c) {
return 1 <= r && r <= n && 1 <= c && c <= n && sci[r][c] != 'Y';
}
int vis[N][N];
int distTox[N][N], dist[N][N], timer = 0;
void reactorSim(pair<int, int> second) {
++timer;
queue<pair<int, int> > q;
q.push(second);
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
vis[r][c] = timer;
if (distTox[r][c] == inf) distTox[r][c] = 0;
for (auto i = (0) - ((0) > (4)); i != 4 - ((0) > (4));
i += 1 - 2 * ((0) > (4))) {
int nr = r + dr[i];
int nc = c + dc[i];
if (valid(nr, nc) && vis[nr][nc] != timer) {
q.emplace(nr, nc);
distTox[nr][nc] = distTox[r][c] + 1;
}
}
}
}
bool reach(pair<int, int> second, pair<int, int> e) {
;
++timer;
memset(dist, 0, sizeof dist);
queue<pair<int, int> > q;
q.push(second);
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
vis[r][c] = timer;
;
;
if (dist[r][c] <= distTox[r][c] && pair<int, int>(r, c) == e &&
dist[r][c] <= boom)
return true;
if (dist[r][c] >= distTox[r][c]) continue;
for (auto i = (0) - ((0) > (4)); i != 4 - ((0) > (4));
i += 1 - 2 * ((0) > (4))) {
int nr = r + dr[i];
int nc = c + dc[i];
if (valid(nr, nc) && vis[nr][nc] != timer) {
q.emplace(nr, nc);
dist[nr][nc] = dist[r][c] + 1;
}
}
}
return false;
}
template <class F>
struct Dinic {
static constexpr F eps = (F)1e-9;
struct Edge {
int v, inv;
F cap, flow;
Edge(int v, F cap, int inv) : v(v), cap(cap), flow(0), inv(inv) {}
};
int second, t, n, m = 0;
vector<vector<Edge> > g;
vector<int> dist, ptr;
Dinic(int n, int ss = -1, int tt = -1)
: n(n), g(n + 5), dist(n + 5), ptr(n + 5) {
second = ss == -1 ? n + 1 : ss;
t = tt == -1 ? n + 2 : tt;
}
void add(int u, int v, F cap) {
g[u].push_back(Edge(v, cap, int(g[v].size())));
g[v].push_back(Edge(u, 0, int(g[u].size()) - 1));
m += 2;
}
bool bfs() {
fill(begin(dist), end(dist), -1);
queue<int> qu({second});
dist[second] = 0;
while (int(qu.size())) {
int u = qu.front();
qu.pop();
for (Edge &e : g[u])
if (e.cap - e.flow > eps)
if (dist[e.v] == -1) {
dist[e.v] = dist[u] + 1;
qu.push(e.v);
}
}
return dist[t] != -1;
}
F dfs(int u, F flow = numeric_limits<F>::max()) {
if (flow <= eps || u == t) return max<F>(0, flow);
for (int &i = ptr[u]; i < int(g[u].size()); i++) {
Edge &e = g[u][i];
if (e.cap - e.flow > eps && dist[u] + 1 == dist[e.v]) {
F nflow = dfs(e.v, min<F>(flow, e.cap - e.flow));
if (nflow > eps) {
e.flow += nflow;
g[e.v][e.inv].flow -= nflow;
return nflow;
}
}
}
return 0;
}
F maxFlow() {
F flow = 0;
while (bfs()) {
fill(begin(ptr), end(ptr), 0);
while (F nflow = dfs(second)) flow += nflow;
}
return flow;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0), cout.tie(0);
while (cin >> n >> boom) {
int cnt = 0;
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
id[r][c] = ++cnt;
cin >> sci[r][c];
if (sci[r][c] == 'Z') reactor = {r, c};
}
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
cin >> cap[r][c];
distTox[r][c] = inf;
}
Dinic<int> g(n * n * 2);
reactorSim(reactor);
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1)))
for (auto rr = (1) - ((1) > (n + 1)); rr != n + 1 - ((1) > (n + 1));
rr += 1 - 2 * ((1) > (n + 1)))
for (auto cc = (1) - ((1) > (n + 1)); cc != n + 1 - ((1) > (n + 1));
cc += 1 - 2 * ((1) > (n + 1)))
if (isdigit(sci[r][c]) && isdigit(cap[r][c]) &&
reach({r, c}, {rr, cc}))
g.add(id[r][c], n * n + id[rr][cc], inf);
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
if (isdigit(sci[r][c])) g.add(g.second, id[r][c], sci[r][c] - '0');
if (isdigit(cap[r][c])) g.add(n * n + id[r][c], g.t, cap[r][c] - '0');
}
int saved = g.maxFlow();
cout << saved << '\n';
}
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 10 + 5, inf = 1e9 + 5;
int n, boom;
char sci[N][N], cap[N][N];
int id[N][N];
pair<int, int> reactor;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
bool valid(int r, int c) {
return 1 <= r && r <= n && 1 <= c && c <= n && sci[r][c] != 'Y';
}
int vis[N][N];
int distTox[N][N], dist[N][N], timer = 0;
void reactorSim(pair<int, int> second) {
++timer;
queue<pair<int, int> > q;
q.push(second);
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
vis[r][c] = timer;
if (distTox[r][c] == inf) distTox[r][c] = 0;
for (auto i = (0) - ((0) > (4)); i != 4 - ((0) > (4));
i += 1 - 2 * ((0) > (4))) {
int nr = r + dr[i];
int nc = c + dc[i];
if (valid(nr, nc) && vis[nr][nc] != timer) {
q.emplace(nr, nc);
distTox[nr][nc] = distTox[r][c] + 1;
}
}
}
}
bool reach(pair<int, int> second, pair<int, int> e) {
;
++timer;
memset(dist, 0, sizeof dist);
queue<pair<int, int> > q;
q.push(second);
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
vis[r][c] = timer;
;
;
if (dist[r][c] <= distTox[r][c] && pair<int, int>(r, c) == e &&
dist[r][c] <= boom)
return true;
if (dist[r][c] >= distTox[r][c]) continue;
for (auto i = (0) - ((0) > (4)); i != 4 - ((0) > (4));
i += 1 - 2 * ((0) > (4))) {
int nr = r + dr[i];
int nc = c + dc[i];
if (valid(nr, nc) && vis[nr][nc] != timer) {
q.emplace(nr, nc);
dist[nr][nc] = dist[r][c] + 1;
}
}
}
return false;
}
template <class F>
struct Dinic {
static constexpr F eps = (F)1e-9;
struct Edge {
int v, inv;
F cap, flow;
Edge(int v, F cap, int inv) : v(v), cap(cap), flow(0), inv(inv) {}
};
int second, t, n, m = 0;
vector<vector<Edge> > g;
vector<int> dist, ptr;
Dinic(int n, int ss = -1, int tt = -1)
: n(n), g(n + 5), dist(n + 5), ptr(n + 5) {
second = ss == -1 ? n + 1 : ss;
t = tt == -1 ? n + 2 : tt;
}
void add(int u, int v, F cap) {
g[u].push_back(Edge(v, cap, int(g[v].size())));
g[v].push_back(Edge(u, 0, int(g[u].size()) - 1));
m += 2;
}
bool bfs() {
fill(begin(dist), end(dist), -1);
queue<int> qu({second});
dist[second] = 0;
while (int(qu.size())) {
int u = qu.front();
qu.pop();
for (Edge &e : g[u])
if (e.cap - e.flow > eps)
if (dist[e.v] == -1) {
dist[e.v] = dist[u] + 1;
qu.push(e.v);
}
}
return dist[t] != -1;
}
F dfs(int u, F flow = numeric_limits<F>::max()) {
if (flow <= eps || u == t) return max<F>(0, flow);
for (int &i = ptr[u]; i < int(g[u].size()); i++) {
Edge &e = g[u][i];
if (e.cap - e.flow > eps && dist[u] + 1 == dist[e.v]) {
F nflow = dfs(e.v, min<F>(flow, e.cap - e.flow));
if (nflow > eps) {
e.flow += nflow;
g[e.v][e.inv].flow -= nflow;
return nflow;
}
}
}
return 0;
}
F maxFlow() {
F flow = 0;
while (bfs()) {
fill(begin(ptr), end(ptr), 0);
while (F nflow = dfs(second)) flow += nflow;
}
return flow;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0), cout.tie(0);
while (cin >> n >> boom) {
int cnt = 0;
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
id[r][c] = ++cnt;
cin >> sci[r][c];
if (sci[r][c] == 'Z') reactor = {r, c};
}
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
cin >> cap[r][c];
distTox[r][c] = inf;
}
Dinic<int> g(n * n * 2);
reactorSim(reactor);
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1)))
for (auto rr = (1) - ((1) > (n + 1)); rr != n + 1 - ((1) > (n + 1));
rr += 1 - 2 * ((1) > (n + 1)))
for (auto cc = (1) - ((1) > (n + 1)); cc != n + 1 - ((1) > (n + 1));
cc += 1 - 2 * ((1) > (n + 1)))
if (isdigit(sci[r][c]) && isdigit(cap[r][c]) &&
reach({r, c}, {rr, cc}))
g.add(id[r][c], n * n + id[rr][cc], inf);
for (auto r = (1) - ((1) > (n + 1)); r != n + 1 - ((1) > (n + 1));
r += 1 - 2 * ((1) > (n + 1)))
for (auto c = (1) - ((1) > (n + 1)); c != n + 1 - ((1) > (n + 1));
c += 1 - 2 * ((1) > (n + 1))) {
if (isdigit(sci[r][c])) g.add(g.second, id[r][c], sci[r][c] - '0');
if (isdigit(cap[r][c])) g.add(n * n + id[r][c], g.t, cap[r][c] - '0');
}
int saved = g.maxFlow();
cout << saved << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300;
const int MAXM = 65536;
inline int RE(int i) { return i ^ 1; }
struct Edge {
int v;
int c;
};
struct FlowNetwork {
int n, m, source, sink;
vector<int> e[MAXN];
Edge edge[MAXM * 2];
void init(int n, int source, int sink) {
this->n = n;
this->m = 0;
this->source = source;
this->sink = sink;
for (int i = 0; i < n; ++i) {
e[i].clear();
}
}
void addEdge(int a, int b, int c) {
edge[m].v = b;
edge[m].c = c;
e[a].push_back(m);
edge[m + 1].v = a;
edge[m + 1].c = 0;
e[b].push_back(m + 1);
m += 2;
}
int c[MAXN];
int d[MAXN];
int done[MAXN];
int path[MAXN];
int len;
void _bfs() {
queue<int> q;
fill(c, c + n, 0);
fill(d, d + n, n);
d[sink] = 0;
q.push(sink);
while (!q.empty()) {
int cur = q.front();
++c[d[cur]];
for (size_t i = 0; i < e[cur].size(); ++i) {
int id = e[cur][i];
if (d[edge[id].v] == n && edge[RE(id)].c > 0) {
d[edge[id].v] = d[cur] + 1;
q.push(edge[id].v);
}
}
q.pop();
}
}
void _retreat(int v) {
--c[d[v]];
d[v] = n;
for (size_t i = 0; i < e[v].size(); ++i) {
Edge &arc = edge[e[v][i]];
if (d[v] > d[arc.v] + 1 && arc.c > 0) {
d[v] = d[arc.v] + 1;
done[v] = i;
}
}
++c[d[v]];
}
int _augment() {
int todo = -1;
int flow = 0x7fffff;
for (int i = 0; i < len; ++i) {
Edge &arc = edge[e[path[i]][done[path[i]]]];
if (arc.c < flow) {
flow = arc.c;
todo = i;
}
}
for (int i = 0; i < len; ++i) {
int id = e[path[i]][done[path[i]]];
edge[id].c -= flow;
edge[RE(id)].c += flow;
}
len = todo;
return flow;
}
int sap() {
int flow = 0;
_bfs();
fill(done, done + n, 0);
len = 0;
path[0] = source;
while (d[source] != n) {
int back = path[len];
if (back == sink) {
flow += _augment();
} else {
while (done[back] < (int)e[back].size()) {
Edge &arc = edge[e[back][done[back]]];
if (d[arc.v] == d[back] - 1 && arc.c > 0) {
break;
} else {
++done[back];
}
}
if (done[back] == (int)e[back].size()) {
if (c[d[back]] == 1) {
break;
} else {
_retreat(back);
if (back != source) {
--len;
}
}
} else {
path[++len] = edge[e[back][done[back]]].v;
}
}
}
return flow;
}
} fn;
const int MAX = 12;
const int INF = 1000000007;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
int n;
char sci[MAX][MAX], rec[MAX][MAX];
int d[MAX][MAX], t[MAX][MAX];
inline int X(int i, int j) { return i * MAX + j; };
inline int Y(int i, int j) { return MAX * MAX + i * MAX + j; }
inline int S() { return MAX * MAX * 2; }
inline int T() { return S() + 1; }
inline int N() { return T() + 1; }
void bfs(int n) {
queue<pair<int, int> > q;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
d[i][j] = INF;
if (sci[i][j] == 'Z') {
q.push(make_pair(i, j));
d[i][j] = 0;
}
}
}
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int k = 0; k < 4; ++k) {
int xx = x + dx[k];
int yy = y + dy[k];
if (0 <= xx && xx < n && 0 <= yy && yy < n && isdigit(sci[xx][yy]) &&
d[xx][yy] == INF) {
d[xx][yy] = d[x][y] + 1;
q.push(make_pair(xx, yy));
}
}
}
}
void gao(int n, int m, int sx, int sy) {
queue<pair<int, int> > q;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
t[i][j] = INF;
}
}
q.push(make_pair(sx, sy));
t[sx][sy] = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
if (t[x][y] > m) {
break;
}
if (t[x][y] <= d[x][y] && isdigit(rec[x][y]) && rec[x][y] != '0') {
fn.addEdge(X(sx, sy), Y(x, y), 11);
}
if (t[x][y] >= d[x][y]) {
continue;
}
for (int k = 0; k < 4; ++k) {
int xx = x + dx[k];
int yy = y + dy[k];
if (0 <= xx && xx < n && 0 <= yy && yy < n && isdigit(sci[xx][yy]) &&
t[xx][yy] == INF) {
t[xx][yy] = t[x][y] + 1;
q.push(make_pair(xx, yy));
}
}
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
fn.init(N(), S(), T());
for (int i = 0; i < n; ++i) {
scanf("%s", sci[i]);
for (int j = 0; j < n; ++j) {
if (isdigit(sci[i][j]) && sci[i][j] != '0') {
fn.addEdge(S(), X(i, j), sci[i][j] - '0');
}
}
}
for (int i = 0; i < n; ++i) {
scanf("%s", rec[i]);
for (int j = 0; j < n; ++j) {
if (isdigit(rec[i][j]) && rec[i][j] != '0') {
fn.addEdge(Y(i, j), T(), rec[i][j] - '0');
}
}
}
bfs(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (isdigit(sci[i][j]) && sci[i][j] != '0') {
gao(n, m, i, j);
}
}
}
printf("%d\n", fn.sap());
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300;
const int MAXM = 65536;
inline int RE(int i) { return i ^ 1; }
struct Edge {
int v;
int c;
};
struct FlowNetwork {
int n, m, source, sink;
vector<int> e[MAXN];
Edge edge[MAXM * 2];
void init(int n, int source, int sink) {
this->n = n;
this->m = 0;
this->source = source;
this->sink = sink;
for (int i = 0; i < n; ++i) {
e[i].clear();
}
}
void addEdge(int a, int b, int c) {
edge[m].v = b;
edge[m].c = c;
e[a].push_back(m);
edge[m + 1].v = a;
edge[m + 1].c = 0;
e[b].push_back(m + 1);
m += 2;
}
int c[MAXN];
int d[MAXN];
int done[MAXN];
int path[MAXN];
int len;
void _bfs() {
queue<int> q;
fill(c, c + n, 0);
fill(d, d + n, n);
d[sink] = 0;
q.push(sink);
while (!q.empty()) {
int cur = q.front();
++c[d[cur]];
for (size_t i = 0; i < e[cur].size(); ++i) {
int id = e[cur][i];
if (d[edge[id].v] == n && edge[RE(id)].c > 0) {
d[edge[id].v] = d[cur] + 1;
q.push(edge[id].v);
}
}
q.pop();
}
}
void _retreat(int v) {
--c[d[v]];
d[v] = n;
for (size_t i = 0; i < e[v].size(); ++i) {
Edge &arc = edge[e[v][i]];
if (d[v] > d[arc.v] + 1 && arc.c > 0) {
d[v] = d[arc.v] + 1;
done[v] = i;
}
}
++c[d[v]];
}
int _augment() {
int todo = -1;
int flow = 0x7fffff;
for (int i = 0; i < len; ++i) {
Edge &arc = edge[e[path[i]][done[path[i]]]];
if (arc.c < flow) {
flow = arc.c;
todo = i;
}
}
for (int i = 0; i < len; ++i) {
int id = e[path[i]][done[path[i]]];
edge[id].c -= flow;
edge[RE(id)].c += flow;
}
len = todo;
return flow;
}
int sap() {
int flow = 0;
_bfs();
fill(done, done + n, 0);
len = 0;
path[0] = source;
while (d[source] != n) {
int back = path[len];
if (back == sink) {
flow += _augment();
} else {
while (done[back] < (int)e[back].size()) {
Edge &arc = edge[e[back][done[back]]];
if (d[arc.v] == d[back] - 1 && arc.c > 0) {
break;
} else {
++done[back];
}
}
if (done[back] == (int)e[back].size()) {
if (c[d[back]] == 1) {
break;
} else {
_retreat(back);
if (back != source) {
--len;
}
}
} else {
path[++len] = edge[e[back][done[back]]].v;
}
}
}
return flow;
}
} fn;
const int MAX = 12;
const int INF = 1000000007;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
int n;
char sci[MAX][MAX], rec[MAX][MAX];
int d[MAX][MAX], t[MAX][MAX];
inline int X(int i, int j) { return i * MAX + j; };
inline int Y(int i, int j) { return MAX * MAX + i * MAX + j; }
inline int S() { return MAX * MAX * 2; }
inline int T() { return S() + 1; }
inline int N() { return T() + 1; }
void bfs(int n) {
queue<pair<int, int> > q;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
d[i][j] = INF;
if (sci[i][j] == 'Z') {
q.push(make_pair(i, j));
d[i][j] = 0;
}
}
}
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int k = 0; k < 4; ++k) {
int xx = x + dx[k];
int yy = y + dy[k];
if (0 <= xx && xx < n && 0 <= yy && yy < n && isdigit(sci[xx][yy]) &&
d[xx][yy] == INF) {
d[xx][yy] = d[x][y] + 1;
q.push(make_pair(xx, yy));
}
}
}
}
void gao(int n, int m, int sx, int sy) {
queue<pair<int, int> > q;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
t[i][j] = INF;
}
}
q.push(make_pair(sx, sy));
t[sx][sy] = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
if (t[x][y] > m) {
break;
}
if (t[x][y] <= d[x][y] && isdigit(rec[x][y]) && rec[x][y] != '0') {
fn.addEdge(X(sx, sy), Y(x, y), 11);
}
if (t[x][y] >= d[x][y]) {
continue;
}
for (int k = 0; k < 4; ++k) {
int xx = x + dx[k];
int yy = y + dy[k];
if (0 <= xx && xx < n && 0 <= yy && yy < n && isdigit(sci[xx][yy]) &&
t[xx][yy] == INF) {
t[xx][yy] = t[x][y] + 1;
q.push(make_pair(xx, yy));
}
}
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
fn.init(N(), S(), T());
for (int i = 0; i < n; ++i) {
scanf("%s", sci[i]);
for (int j = 0; j < n; ++j) {
if (isdigit(sci[i][j]) && sci[i][j] != '0') {
fn.addEdge(S(), X(i, j), sci[i][j] - '0');
}
}
}
for (int i = 0; i < n; ++i) {
scanf("%s", rec[i]);
for (int j = 0; j < n; ++j) {
if (isdigit(rec[i][j]) && rec[i][j] != '0') {
fn.addEdge(Y(i, j), T(), rec[i][j] - '0');
}
}
}
bfs(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (isdigit(sci[i][j]) && sci[i][j] != '0') {
gao(n, m, i, j);
}
}
}
printf("%d\n", fn.sap());
return 0;
}
``` |
#include <bits/stdc++.h>
const double pi = acos(-1.0);
const int INF = 0x7FFFFFFF;
using namespace std;
const int maxn = 10010;
int p[maxn];
bool sorting(int l, int r, int index) {
int low = 0;
for (int i = l - 1; i < r; i++)
if (p[i] < p[index - 1]) low++;
if (l + low == index) return 1;
return 0;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", p + i);
for (int i = 0; i < m; i++) {
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
if (sorting(l, r, x))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
const double pi = acos(-1.0);
const int INF = 0x7FFFFFFF;
using namespace std;
const int maxn = 10010;
int p[maxn];
bool sorting(int l, int r, int index) {
int low = 0;
for (int i = l - 1; i < r; i++)
if (p[i] < p[index - 1]) low++;
if (l + low == index) return 1;
return 0;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", p + i);
for (int i = 0; i < m; i++) {
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
if (sorting(l, r, x))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int a[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int l, r, x, d; m--;) {
scanf("%d%d%d", &l, &r, &x);
d = 0;
for (int i = l; i <= r; i++) d += a[i] < a[x];
if (d + l == x)
puts("Yes");
else
puts("No");
}
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int a[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int l, r, x, d; m--;) {
scanf("%d%d%d", &l, &r, &x);
d = 0;
for (int i = l; i <= r; i++) d += a[i] < a[x];
if (d + l == x)
puts("Yes");
else
puts("No");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> v, u;
int n;
int main() {
int m;
int a, b, x;
cin >> n >> m;
vector<int> v, u;
for (int i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
u.push_back(x);
}
for (int i = 0; i < m; i++) {
cin >> a >> b >> x;
a--;
b--;
x--;
int c = 0;
for (int j = a; j <= b; j++) {
if (v[j] < v[x]) {
c++;
}
}
if (a + c == x) {
cout << "Yes" << endl;
} else
cout << "No" << endl;
}
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> v, u;
int n;
int main() {
int m;
int a, b, x;
cin >> n >> m;
vector<int> v, u;
for (int i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
u.push_back(x);
}
for (int i = 0; i < m; i++) {
cin >> a >> b >> x;
a--;
b--;
x--;
int c = 0;
for (int j = a; j <= b; j++) {
if (v[j] < v[x]) {
c++;
}
}
if (a + c == x) {
cout << "Yes" << endl;
} else
cout << "No" << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int p[10010];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> p[i];
for (int i = 1; i <= m; ++i) {
int l, r, x;
cin >> l >> r >> x;
int a = 0;
for (int j = l; j <= r; ++j) {
if (p[j] < p[x]) a++;
}
if (a == x - l)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
int p[10010];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> p[i];
for (int i = 1; i <= m; ++i) {
int l, r, x;
cin >> l >> r >> x;
int a = 0;
for (int j = l; j <= r; ++j) {
if (p[j] < p[x]) a++;
}
if (a == x - l)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void doublade() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
}
bool solve(const vector<long long> &p, long long l, long long r, long long x) {
if (x < l || x > r)
return true;
else {
long long cnt = 0;
for (long long i = l - 1; i <= r - 1; i++) {
if (p[i] < p[x - 1]) cnt++;
}
if (cnt == x - l)
return true;
else
return false;
}
}
int main() {
doublade();
long long n, m;
cin >> n >> m;
vector<long long> p(n);
for (long long i = 0; i < n; i++) {
cin >> p[i];
}
for (long long i = 0; i < m; i++) {
long long l, r, x;
cin >> l >> r >> x;
if (solve(p, l, r, x))
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void doublade() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
}
bool solve(const vector<long long> &p, long long l, long long r, long long x) {
if (x < l || x > r)
return true;
else {
long long cnt = 0;
for (long long i = l - 1; i <= r - 1; i++) {
if (p[i] < p[x - 1]) cnt++;
}
if (cnt == x - l)
return true;
else
return false;
}
}
int main() {
doublade();
long long n, m;
cin >> n >> m;
vector<long long> p(n);
for (long long i = 0; i < n; i++) {
cin >> p[i];
}
for (long long i = 0; i < m; i++) {
long long l, r, x;
cin >> l >> r >> x;
if (solve(p, l, r, x))
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
long long int fexpo(long long int a, long long int b) {
if (b == 0) return 1LL;
if (b == 1) return a;
if (b == 2) return ((a % mod) * (a % mod)) % mod;
if (b % 2 == 0)
return fexpo(fexpo(a, b / 2), 2);
else
return ((a % mod) * (fexpo(fexpo(a, (b - 1) / 2), 2) % mod));
}
int ar[100000];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> ar[i];
}
while (q--) {
int l, r, num;
cin >> l >> r >> num;
int range = abs(l - r) + 1;
if (num > r || num < l) {
cout << "Yes" << endl;
continue;
} else {
int leftmb = 0;
int rightmc = 0;
for (int i = l; i < num; i++) {
if (ar[num] < ar[i]) leftmb++;
}
for (int i = num + 1; i <= r; i++) {
if (ar[num] > ar[i]) rightmc++;
}
if (rightmc == leftmb)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
long long int fexpo(long long int a, long long int b) {
if (b == 0) return 1LL;
if (b == 1) return a;
if (b == 2) return ((a % mod) * (a % mod)) % mod;
if (b % 2 == 0)
return fexpo(fexpo(a, b / 2), 2);
else
return ((a % mod) * (fexpo(fexpo(a, (b - 1) / 2), 2) % mod));
}
int ar[100000];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> ar[i];
}
while (q--) {
int l, r, num;
cin >> l >> r >> num;
int range = abs(l - r) + 1;
if (num > r || num < l) {
cout << "Yes" << endl;
continue;
} else {
int leftmb = 0;
int rightmc = 0;
for (int i = l; i < num; i++) {
if (ar[num] < ar[i]) leftmb++;
}
for (int i = num + 1; i <= r; i++) {
if (ar[num] > ar[i]) rightmc++;
}
if (rightmc == leftmb)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <class T>
inline T LCM(T a, T b) {
if (a < 0) return LCM(-a, b);
if (b < 0) return LCM(a, -b);
return a * (b / gcd(a, b));
}
vector<int> arr;
int main() {
int n, m;
scanf("%d", &n);
scanf("%d", &m);
int i, temp;
for (i = (int)0; i < (int)n; ++i) {
scanf("%d", &temp);
arr.push_back(temp);
}
int l, r, p;
for (i = (int)0; i < (int)m; ++i) {
scanf("%d", &l);
scanf("%d", &r);
scanf("%d", &p);
int less = 0;
p--;
l--;
r--;
for (int j = l; j <= r; j++) {
if (arr[p] > arr[j]) {
less++;
}
}
if (less + l == p) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <class T>
inline T LCM(T a, T b) {
if (a < 0) return LCM(-a, b);
if (b < 0) return LCM(a, -b);
return a * (b / gcd(a, b));
}
vector<int> arr;
int main() {
int n, m;
scanf("%d", &n);
scanf("%d", &m);
int i, temp;
for (i = (int)0; i < (int)n; ++i) {
scanf("%d", &temp);
arr.push_back(temp);
}
int l, r, p;
for (i = (int)0; i < (int)m; ++i) {
scanf("%d", &l);
scanf("%d", &r);
scanf("%d", &p);
int less = 0;
p--;
l--;
r--;
for (int j = l; j <= r; j++) {
if (arr[p] > arr[j]) {
less++;
}
}
if (less + l == p) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[10001], b[10001];
int main() {
int n, m;
;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
int L, R, x, cnt = 1;
vector<string> ans;
for (int i = 1; i <= m; ++i) {
cin >> L >> R >> x;
if (x < L || x > R) {
ans.push_back("Yes");
} else {
for (int j = L; j <= R; ++j) {
b[a[j]] = cnt;
}
int h = x - L + 1, q = 0;
for (int k = 1; k <= 10000; ++k) {
if (b[k] == cnt) q++;
if (h == q) {
if (k == a[x])
ans.push_back("Yes");
else
ans.push_back("No");
break;
}
}
}
++cnt;
}
for (vector<string>::iterator it = ans.begin(); it != ans.end(); ++it)
cout << *it << "\n";
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[10001], b[10001];
int main() {
int n, m;
;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
int L, R, x, cnt = 1;
vector<string> ans;
for (int i = 1; i <= m; ++i) {
cin >> L >> R >> x;
if (x < L || x > R) {
ans.push_back("Yes");
} else {
for (int j = L; j <= R; ++j) {
b[a[j]] = cnt;
}
int h = x - L + 1, q = 0;
for (int k = 1; k <= 10000; ++k) {
if (b[k] == cnt) q++;
if (h == q) {
if (k == a[x])
ans.push_back("Yes");
else
ans.push_back("No");
break;
}
}
}
++cnt;
}
for (vector<string>::iterator it = ans.begin(); it != ans.end(); ++it)
cout << *it << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int i;
int arr[n + 1];
for (i = 0; i < n; i++) scanf("%d", &arr[i]);
for (i = 0; i < m; i++) {
int l, r, x;
cin >> l >> r >> x;
l--;
r--;
x--;
int c = 0;
for (int j = l; j <= r; j++) {
if (arr[j] < arr[x]) c++;
}
if (l + c == x)
cout << "Yes\n";
else
cout << "No\n";
}
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int i;
int arr[n + 1];
for (i = 0; i < n; i++) scanf("%d", &arr[i]);
for (i = 0; i < m; i++) {
int l, r, x;
cin >> l >> r >> x;
l--;
r--;
x--;
int c = 0;
for (int j = l; j <= r; j++) {
if (arr[j] < arr[x]) c++;
}
if (l + c == x)
cout << "Yes\n";
else
cout << "No\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int pos[10005];
int arr[10005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int l, r, x;
while (m--) {
cin >> l >> r >> x;
int c = 0;
for (int i = l; i <= r; i++) {
if (arr[i] < arr[x]) c++;
}
if (l - 1 + c + 1 == x)
cout << "Yes\n";
else
cout << "No\n";
}
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int pos[10005];
int arr[10005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int l, r, x;
while (m--) {
cin >> l >> r >> x;
int c = 0;
for (int i = l; i <= r; i++) {
if (arr[i] < arr[x]) c++;
}
if (l - 1 + c + 1 == x)
cout << "Yes\n";
else
cout << "No\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> vec(n);
for (int i = 0; i < n; i++) cin >> vec[i];
int i, l, r, u, cnt = 0;
while (m--) {
cin >> l >> r >> u;
l--;
u--;
cnt = 0;
for (int i = l; i < r; i++) {
if (vec[i] < vec[u]) {
cnt++;
}
}
if (l + cnt == u) {
cout << "Yes" << endl;
} else
cout << "No" << endl;
}
}
| ### Prompt
Please create a solution in cpp to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> vec(n);
for (int i = 0; i < n; i++) cin >> vec[i];
int i, l, r, u, cnt = 0;
while (m--) {
cin >> l >> r >> u;
l--;
u--;
cnt = 0;
for (int i = l; i < r; i++) {
if (vec[i] < vec[u]) {
cnt++;
}
}
if (l + cnt == u) {
cout << "Yes" << endl;
} else
cout << "No" << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)(1e9 + 7);
long long power(long long x, long long n) {
if (n == 0) return 1;
if (n == 1) return x % mod;
if (n % 2 == 0) {
long long y = power(x, n / 2) % mod;
return (y * y) % mod;
}
if (n & 1) {
long long y = power(x, n - 1);
return (x % mod * y % mod) % mod;
}
return 0;
}
long long dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
long long dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const long long maxn = 100005;
void solve() {
long long n, i, j, k, m;
cin >> n >> m;
vector<long long> ar(n);
for (i = 0; i < n; i++) cin >> ar[i];
while (m--) {
long long l, r, x;
cin >> l >> r >> x;
l--, r--, x--;
long long cnt = 0;
for (long long i = l; i <= r; i++) {
if (ar[i] < ar[x]) cnt++;
}
if (l + cnt == x)
cout << "Yes\n";
else
cout << "No\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t = 1;
while (t--) solve();
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)(1e9 + 7);
long long power(long long x, long long n) {
if (n == 0) return 1;
if (n == 1) return x % mod;
if (n % 2 == 0) {
long long y = power(x, n / 2) % mod;
return (y * y) % mod;
}
if (n & 1) {
long long y = power(x, n - 1);
return (x % mod * y % mod) % mod;
}
return 0;
}
long long dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
long long dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const long long maxn = 100005;
void solve() {
long long n, i, j, k, m;
cin >> n >> m;
vector<long long> ar(n);
for (i = 0; i < n; i++) cin >> ar[i];
while (m--) {
long long l, r, x;
cin >> l >> r >> x;
l--, r--, x--;
long long cnt = 0;
for (long long i = l; i <= r; i++) {
if (ar[i] < ar[x]) cnt++;
}
if (l + cnt == x)
cout << "Yes\n";
else
cout << "No\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t = 1;
while (t--) solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d %d", &n, &m);
int arr[n + 1];
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
while (m--) {
int l, r, x;
scanf("%d %d %d", &l, &r, &x);
int k = 0;
for (int i = l; i <= r; i++) {
if (arr[i] < arr[x]) k++;
}
if (x - l == k)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d %d", &n, &m);
int arr[n + 1];
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
while (m--) {
int l, r, x;
scanf("%d %d %d", &l, &r, &x);
int k = 0;
for (int i = l; i <= r; i++) {
if (arr[i] < arr[x]) k++;
}
if (x - l == k)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[10000], t[10000], n, m;
bool solve(int l, int r, int x) {
int s = 0;
for (int i = l; i <= r; i++)
if (a[i] <= a[x]) s++;
if (s == x - l + 1) return 1;
return 0;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) {
int l, r, x;
cin >> l >> r >> x;
l -= 1;
r -= 1;
x -= 1;
if (solve(l, r, x))
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[10000], t[10000], n, m;
bool solve(int l, int r, int x) {
int s = 0;
for (int i = l; i <= r; i++)
if (a[i] <= a[x]) s++;
if (s == x - l + 1) return 1;
return 0;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) {
int l, r, x;
cin >> l >> r >> x;
l -= 1;
r -= 1;
x -= 1;
if (solve(l, r, x))
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10003;
const int maxk = 1003;
int arr[maxn];
int n, m, l, r, x;
int32_t main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (int i = 0; i < m; i++) {
cin >> l >> r >> x;
x = arr[x];
int idx = -1;
int lesser = 0;
for (int j = l; j <= r; j++) {
if (arr[j] < x) {
lesser++;
}
if (arr[j] == x) {
idx = j - l;
}
}
if (idx == lesser) {
cout << "Yes" << '\n';
} else {
cout << "No" << '\n';
}
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10003;
const int maxk = 1003;
int arr[maxn];
int n, m, l, r, x;
int32_t main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (int i = 0; i < m; i++) {
cin >> l >> r >> x;
x = arr[x];
int idx = -1;
int lesser = 0;
for (int j = l; j <= r; j++) {
if (arr[j] < x) {
lesser++;
}
if (arr[j] == x) {
idx = j - l;
}
}
if (idx == lesser) {
cout << "Yes" << '\n';
} else {
cout << "No" << '\n';
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> p(n);
for (int i = 0; i < int(n); i++) cin >> p[i];
for (int i = 0; i < int(m); i++) {
int l, r, x;
cin >> l >> r >> x;
l -= 1;
r -= 1;
x -= 1;
int pos = l;
for (int i = l; i <= r; i++) {
if (p[i] < p[x]) {
pos += 1;
}
}
if (pos == x) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> p(n);
for (int i = 0; i < int(n); i++) cin >> p[i];
for (int i = 0; i < int(m); i++) {
int l, r, x;
cin >> l >> r >> x;
l -= 1;
r -= 1;
x -= 1;
int pos = l;
for (int i = l; i <= r; i++) {
if (p[i] < p[x]) {
pos += 1;
}
}
if (pos == x) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, n, m, k, l, r, x;
cin >> n >> m;
int A[n];
int pos[n + 1];
for (i = 0; i < n; ++i) {
cin >> A[i];
pos[A[i]] = i;
}
while (m--) {
cin >> l >> r >> x;
l--;
r--;
x = A[x - 1];
if (pos[x] < l || pos[x] > r) {
cout << "No\n";
} else {
int coun = 0;
for (i = l; i <= r; ++i) {
if (A[i] < x) {
coun++;
}
}
if (coun == pos[x] - l) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, n, m, k, l, r, x;
cin >> n >> m;
int A[n];
int pos[n + 1];
for (i = 0; i < n; ++i) {
cin >> A[i];
pos[A[i]] = i;
}
while (m--) {
cin >> l >> r >> x;
l--;
r--;
x = A[x - 1];
if (pos[x] < l || pos[x] > r) {
cout << "No\n";
} else {
int coun = 0;
for (i = l; i <= r; ++i) {
if (A[i] < x) {
coun++;
}
}
if (coun == pos[x] - l) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
int a[10001];
int l, r, x;
int s;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++) {
s = 0;
cin >> l >> r >> x;
for (int j = l; j <= r; j++)
if (a[x] >= a[j]) s++;
if (s == x - l + 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
int a[10001];
int l, r, x;
int s;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++) {
s = 0;
cin >> l >> r >> x;
for (int j = l; j <= r; j++)
if (a[x] >= a[j]) s++;
if (s == x - l + 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> vi(n + 1);
for (int i = 1; i <= n; i++) {
cin >> vi[i];
}
for (int i = 0; i < m; i++) {
int l, r, x;
cin >> l >> r >> x;
int pos = l;
for (int i = l; i <= r; i++) {
if (vi[i] < vi[x]) pos++;
}
if (pos == x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> vi(n + 1);
for (int i = 1; i <= n; i++) {
cin >> vi[i];
}
for (int i = 0; i < m; i++) {
int l, r, x;
cin >> l >> r >> x;
int pos = l;
for (int i = l; i <= r; i++) {
if (vi[i] < vi[x]) pos++;
}
if (pos == x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int a[N];
int n, q;
struct node {
int cnt;
node* L;
node* R;
node(int _cnt = 0, node* _L = NULL, node* _R = NULL) {
L = _L;
R = _R;
cnt = _cnt;
}
node* insert(int l, int r, int val);
};
node* null = new node();
node* node ::insert(int l, int r, int val) {
if (l == r) {
return new node(this->cnt + 1, NULL, NULL);
}
int mid = (l + r) / 2;
if (val <= mid) {
return new node(this->cnt + 1, this->L->insert(l, mid, val), this->R);
}
return new node(this->cnt + 1, this->L, this->R->insert(mid + 1, r, val));
}
int getkth(node* A, node* B, int l, int r, int k) {
if (l == r) {
return l;
}
int mid = (l + r) / 2;
int cnt = (B->L->cnt - A->L->cnt);
if (cnt >= k) {
return getkth(A->L, B->L, l, mid, k);
} else {
return getkth(A->R, B->R, mid + 1, r, k - cnt);
}
}
node* root[N];
map<int, int> pos;
map<int, int> rev;
int main() {
scanf("%d", &n);
;
scanf("%d", &q);
;
for (int i = 1; i < n + 1; i++) {
scanf("%d", &a[i]);
;
pos[a[i]];
}
int cnt = 0;
for (auto x : pos) {
pos[x.first] = ++cnt;
rev[cnt] = x.first;
}
null->L = null->R = null;
root[0] = null;
for (int i = 1; i < n + 1; i++) {
root[i] = root[i - 1]->insert(1, cnt, pos[a[i]]);
}
while (q--) {
int x, y, k;
scanf("%d%d%d", &x, &y, &k);
int temp = k;
k = k - x + 1;
int ans = rev[getkth(root[x - 1], root[y], 1, cnt, k)];
if (ans == a[temp]) {
puts("Yes");
} else {
puts("No");
}
}
}
| ### Prompt
Please formulate a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int a[N];
int n, q;
struct node {
int cnt;
node* L;
node* R;
node(int _cnt = 0, node* _L = NULL, node* _R = NULL) {
L = _L;
R = _R;
cnt = _cnt;
}
node* insert(int l, int r, int val);
};
node* null = new node();
node* node ::insert(int l, int r, int val) {
if (l == r) {
return new node(this->cnt + 1, NULL, NULL);
}
int mid = (l + r) / 2;
if (val <= mid) {
return new node(this->cnt + 1, this->L->insert(l, mid, val), this->R);
}
return new node(this->cnt + 1, this->L, this->R->insert(mid + 1, r, val));
}
int getkth(node* A, node* B, int l, int r, int k) {
if (l == r) {
return l;
}
int mid = (l + r) / 2;
int cnt = (B->L->cnt - A->L->cnt);
if (cnt >= k) {
return getkth(A->L, B->L, l, mid, k);
} else {
return getkth(A->R, B->R, mid + 1, r, k - cnt);
}
}
node* root[N];
map<int, int> pos;
map<int, int> rev;
int main() {
scanf("%d", &n);
;
scanf("%d", &q);
;
for (int i = 1; i < n + 1; i++) {
scanf("%d", &a[i]);
;
pos[a[i]];
}
int cnt = 0;
for (auto x : pos) {
pos[x.first] = ++cnt;
rev[cnt] = x.first;
}
null->L = null->R = null;
root[0] = null;
for (int i = 1; i < n + 1; i++) {
root[i] = root[i - 1]->insert(1, cnt, pos[a[i]]);
}
while (q--) {
int x, y, k;
scanf("%d%d%d", &x, &y, &k);
int temp = k;
k = k - x + 1;
int ans = rev[getkth(root[x - 1], root[y], 1, cnt, k)];
if (ans == a[temp]) {
puts("Yes");
} else {
puts("No");
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int Set(int N, int p) { return N |= (1 << p); }
int Reset(int N, int p) { return N &= ~(1 << p); }
bool Check(int N, int p) { return N & (1 << p); }
const int dr[] = {0, -1, 0, 1, -1, -1, 1, 1};
const int dc[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int maxx = 0;
int arr[10000 + 5];
inline bool ok(int l, int r, int pos) {
int smaller = 0;
for (int i = l; i <= r; i++) {
if (arr[i] < arr[pos]) smaller++;
}
if (pos - l == smaller)
return true;
else
return false;
}
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (int i = 0; i < k; i++) {
int l, r, p;
cin >> l >> r >> p;
if (ok(l, r, p))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int Set(int N, int p) { return N |= (1 << p); }
int Reset(int N, int p) { return N &= ~(1 << p); }
bool Check(int N, int p) { return N & (1 << p); }
const int dr[] = {0, -1, 0, 1, -1, -1, 1, 1};
const int dc[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int maxx = 0;
int arr[10000 + 5];
inline bool ok(int l, int r, int pos) {
int smaller = 0;
for (int i = l; i <= r; i++) {
if (arr[i] < arr[pos]) smaller++;
}
if (pos - l == smaller)
return true;
else
return false;
}
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (int i = 0; i < k; i++) {
int l, r, p;
cin >> l >> r >> p;
if (ok(l, r, p))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void read(vector<long long>& c, long long size) {
for (int i = 0; i < size; i++) {
int q;
scanf("%d", &q);
c.push_back(q);
}
}
class Bit {
map<int, int> vals;
};
bool check(vector<long long>& a, long long s, long long k, long long& ans) {
if (k > a.size()) {
return false;
}
vector<long long> b = a;
for (int i = 0; i < b.size(); i++) {
b[i] = b[i] + (i + 1) * k;
}
sort(b.begin(), b.end());
long long sum = 0;
for (int i = 0; i < k; i++) {
sum = sum + b[i];
}
ans = sum;
return sum <= s;
}
int main() {
long long m, n;
long long t;
long long i, j;
long long s;
vector<long long> a;
cin >> n >> m;
read(a, n);
while (m--) {
int l, r, x;
cin >> l >> r >> x;
l--;
r--;
x--;
int k = 0;
for (i = l; i <= r; i++) {
if (a[i] < a[x]) {
k++;
}
}
if (k == x - l) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void read(vector<long long>& c, long long size) {
for (int i = 0; i < size; i++) {
int q;
scanf("%d", &q);
c.push_back(q);
}
}
class Bit {
map<int, int> vals;
};
bool check(vector<long long>& a, long long s, long long k, long long& ans) {
if (k > a.size()) {
return false;
}
vector<long long> b = a;
for (int i = 0; i < b.size(); i++) {
b[i] = b[i] + (i + 1) * k;
}
sort(b.begin(), b.end());
long long sum = 0;
for (int i = 0; i < k; i++) {
sum = sum + b[i];
}
ans = sum;
return sum <= s;
}
int main() {
long long m, n;
long long t;
long long i, j;
long long s;
vector<long long> a;
cin >> n >> m;
read(a, n);
while (m--) {
int l, r, x;
cin >> l >> r >> x;
l--;
r--;
x--;
int k = 0;
for (i = l; i <= r; i++) {
if (a[i] < a[x]) {
k++;
}
}
if (k == x - l) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int a[n];
for (int i = 1; i <= n; i++) cin >> a[i];
while (m--) {
int l, r, x;
cin >> l >> r >> x;
int cnt = 0;
for (int i = l; i <= r; i++)
if (a[i] < a[x]) cnt++;
if (cnt + l == x)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int a[n];
for (int i = 1; i <= n; i++) cin >> a[i];
while (m--) {
int l, r, x;
cin >> l >> r >> x;
int cnt = 0;
for (int i = l; i <= r; i++)
if (a[i] < a[x]) cnt++;
if (cnt + l == x)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, x, l, r, b;
cin >> n >> m;
vector<int> v(n), vcopy;
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < m; i++) {
cin >> l >> r >> x;
b = 0;
for (int k = l - 1; k <= r - 1; k++) {
if (v[k] < v[x - 1]) b++;
}
{
if ((l - 1) + b == x - 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, x, l, r, b;
cin >> n >> m;
vector<int> v(n), vcopy;
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < m; i++) {
cin >> l >> r >> x;
b = 0;
for (int k = l - 1; k <= r - 1; k++) {
if (v[k] < v[x - 1]) b++;
}
{
if ((l - 1) + b == x - 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int l, int r) {
int x = arr[r], i = l;
for (int j = l; j <= r - 1; j++) {
if (arr[j] <= x) {
swap(&arr[i], &arr[j]);
i++;
}
}
swap(&arr[i], &arr[r]);
return i;
}
int randomPartition(int arr[], int l, int r) {
int n = r - l + 1;
int pivot = rand() % n;
swap(&arr[l + pivot], &arr[r]);
return partition(arr, l, r);
}
int kthSmallest(int arr[], int l, int r, int k) {
if (k > 0 && k <= r - l + 1) {
int pos = randomPartition(arr, l, r);
if (pos - l == k - 1) return arr[pos];
if (pos - l > k - 1) return kthSmallest(arr, l, pos - 1, k);
return kthSmallest(arr, pos + 1, r, k - pos + l - 1);
}
return INT_MAX;
}
int main() {
int n, m, l, r, x;
cin >> n >> m;
int a[n], temp[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
temp[i] = a[i];
}
while (m--) {
cin >> l >> r >> x;
if (kthSmallest(temp, l - 1, r - 1, x - l + 1) == a[x - 1]) {
cout << "Yes\n";
} else {
cout << "No\n";
}
for (int i = l - 1; i <= r - 1; i++) temp[i] = a[i];
}
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int l, int r) {
int x = arr[r], i = l;
for (int j = l; j <= r - 1; j++) {
if (arr[j] <= x) {
swap(&arr[i], &arr[j]);
i++;
}
}
swap(&arr[i], &arr[r]);
return i;
}
int randomPartition(int arr[], int l, int r) {
int n = r - l + 1;
int pivot = rand() % n;
swap(&arr[l + pivot], &arr[r]);
return partition(arr, l, r);
}
int kthSmallest(int arr[], int l, int r, int k) {
if (k > 0 && k <= r - l + 1) {
int pos = randomPartition(arr, l, r);
if (pos - l == k - 1) return arr[pos];
if (pos - l > k - 1) return kthSmallest(arr, l, pos - 1, k);
return kthSmallest(arr, pos + 1, r, k - pos + l - 1);
}
return INT_MAX;
}
int main() {
int n, m, l, r, x;
cin >> n >> m;
int a[n], temp[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
temp[i] = a[i];
}
while (m--) {
cin >> l >> r >> x;
if (kthSmallest(temp, l - 1, r - 1, x - l + 1) == a[x - 1]) {
cout << "Yes\n";
} else {
cout << "No\n";
}
for (int i = l - 1; i <= r - 1; i++) temp[i] = a[i];
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int a[n + 2];
for (int i = 1; i <= n; i++) cin >> a[i];
while (m--) {
int l, r, x;
cin >> l >> r >> x;
int c = 0;
for (int i = l; i <= r; i++) {
if (a[i] < a[x]) c++;
}
if (c == x - l) {
puts("Yes");
} else
puts("No");
}
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int a[n + 2];
for (int i = 1; i <= n; i++) cin >> a[i];
while (m--) {
int l, r, x;
cin >> l >> r >> x;
int c = 0;
for (int i = l; i <= r; i++) {
if (a[i] < a[x]) c++;
}
if (c == x - l) {
puts("Yes");
} else
puts("No");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, x, l, r, i, s, a[10179];
int main() {
cin >> n >> m;
for (i = 1; i <= n; i++) cin >> a[i];
while (m--) {
cin >> l >> r >> x;
for (s = 0, i = l; i <= r; i++)
if (a[i] < a[x]) s++;
if (s + l == x)
cout << "Yes\n";
else
cout << "No\n";
}
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, x, l, r, i, s, a[10179];
int main() {
cin >> n >> m;
for (i = 1; i <= n; i++) cin >> a[i];
while (m--) {
cin >> l >> r >> x;
for (s = 0, i = l; i <= r; i++)
if (a[i] < a[x]) s++;
if (s + l == x)
cout << "Yes\n";
else
cout << "No\n";
}
}
``` |
#include <bits/stdc++.h>
const int maxn = 10005;
int val[maxn];
std::vector<std::vector<int>> v[maxn];
int fen[maxn];
int pos[maxn];
int res[maxn];
int query(int pos) {
pos += 2;
int res = 0;
while (pos > 0) {
res += fen[pos];
pos -= pos & -pos;
}
return res;
}
void update(int pos, int val) {
pos += 2;
while (pos < maxn) {
fen[pos] += val;
pos += pos & -pos;
}
}
int main() {
std::ios::sync_with_stdio(false);
int n, q;
std::cin >> n >> q;
for (int i = 0; i < n; i++) {
std::cin >> val[i];
pos[val[i]] = i;
}
for (int i = 0; i < q; i++) {
int l, r, x;
std::cin >> l >> r >> x;
l--, r--, x--;
v[x].push_back({l, r, x - l, i});
}
for (int i = 1; i <= n; i++) {
int loc = pos[i];
for (int j = 0; j < v[loc].size(); j++) {
int order = 0;
order += query(v[loc][j][1]) - query(v[loc][j][0] - 1);
if (order != v[loc][j][2]) {
res[v[loc][j][3]] = false;
} else {
res[v[loc][j][3]] = true;
}
}
update(loc, 1);
}
for (int i = 0; i < q; i++) {
if (res[i]) {
std::cout << "Yes\n";
} else {
std::cout << "No\n";
}
}
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
const int maxn = 10005;
int val[maxn];
std::vector<std::vector<int>> v[maxn];
int fen[maxn];
int pos[maxn];
int res[maxn];
int query(int pos) {
pos += 2;
int res = 0;
while (pos > 0) {
res += fen[pos];
pos -= pos & -pos;
}
return res;
}
void update(int pos, int val) {
pos += 2;
while (pos < maxn) {
fen[pos] += val;
pos += pos & -pos;
}
}
int main() {
std::ios::sync_with_stdio(false);
int n, q;
std::cin >> n >> q;
for (int i = 0; i < n; i++) {
std::cin >> val[i];
pos[val[i]] = i;
}
for (int i = 0; i < q; i++) {
int l, r, x;
std::cin >> l >> r >> x;
l--, r--, x--;
v[x].push_back({l, r, x - l, i});
}
for (int i = 1; i <= n; i++) {
int loc = pos[i];
for (int j = 0; j < v[loc].size(); j++) {
int order = 0;
order += query(v[loc][j][1]) - query(v[loc][j][0] - 1);
if (order != v[loc][j][2]) {
res[v[loc][j][3]] = false;
} else {
res[v[loc][j][3]] = true;
}
}
update(loc, 1);
}
for (int i = 0; i < q; i++) {
if (res[i]) {
std::cout << "Yes\n";
} else {
std::cout << "No\n";
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T, class T1>
int chkmin(T &x, const T1 &y) {
return x > y ? x = y, 1 : 0;
}
template <class T, class T1>
int chkmax(T &x, const T1 &y) {
return x < y ? x = y, 1 : 0;
}
const int MAXN = (1 << 20);
int check(long long n) {
long long s = sqrt(n);
if (s * s == n)
return 1;
else
return 0;
}
set<long long> dp;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long T = 1;
for (long long t = 1; t <= T; t++) {
long long n, q;
cin >> n >> q;
vector<long long> arr(n);
for (long long i = 0; i < n; i++) {
cin >> arr[i];
}
while (q--) {
long long l, r, ci, count = 0;
cin >> l >> r >> ci;
ci--;
for (long long i = l - 1; i < ci; i++) {
if (arr[i] > arr[ci]) count++;
}
for (long long i = ci + 1; i < r; i++) {
if (arr[i] < arr[ci]) count--;
}
if (count == 0)
cout << "Yes" << '\n';
else
cout << "No" << '\n';
}
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T, class T1>
int chkmin(T &x, const T1 &y) {
return x > y ? x = y, 1 : 0;
}
template <class T, class T1>
int chkmax(T &x, const T1 &y) {
return x < y ? x = y, 1 : 0;
}
const int MAXN = (1 << 20);
int check(long long n) {
long long s = sqrt(n);
if (s * s == n)
return 1;
else
return 0;
}
set<long long> dp;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long T = 1;
for (long long t = 1; t <= T; t++) {
long long n, q;
cin >> n >> q;
vector<long long> arr(n);
for (long long i = 0; i < n; i++) {
cin >> arr[i];
}
while (q--) {
long long l, r, ci, count = 0;
cin >> l >> r >> ci;
ci--;
for (long long i = l - 1; i < ci; i++) {
if (arr[i] > arr[ci]) count++;
}
for (long long i = ci + 1; i < r; i++) {
if (arr[i] < arr[ci]) count--;
}
if (count == 0)
cout << "Yes" << '\n';
else
cout << "No" << '\n';
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int n, m, l, r, x, p[10001], a[10001];
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &p[i]);
}
while (m--) {
scanf("%d %d %d", &l, &r, &x);
if (x < l || x > r) {
printf("Yes\n");
continue;
}
int cnt1 = 0, cnt2 = 0;
for (int i = l; i <= r; ++i) {
if (i < x && p[i] > p[x]) cnt1++;
if (i > x && p[i] < p[x]) cnt2++;
}
if (cnt1 == cnt2)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int n, m, l, r, x, p[10001], a[10001];
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &p[i]);
}
while (m--) {
scanf("%d %d %d", &l, &r, &x);
if (x < l || x > r) {
printf("Yes\n");
continue;
}
int cnt1 = 0, cnt2 = 0;
for (int i = l; i <= r; ++i) {
if (i < x && p[i] > p[x]) cnt1++;
if (i > x && p[i] < p[x]) cnt2++;
}
if (cnt1 == cnt2)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
const long long int INF = 1e18L + 5;
using namespace std;
void input() {}
long long p1(long long base, long long power) {
long long res = 1;
while (power != 0) {
if (power % 2 == 0) {
base = ((base % 1000000007) * (base % 1000000007)) % 1000000007;
power = floor(power / 2);
} else {
res = ((res % 1000000007) * (base % 1000000007)) % 1000000007;
power = power - 1;
}
}
return res;
}
bool compare(int a, int b) { return a > b; }
vector<long long> graph[10001];
long long color[10001];
long long visited[10001];
long long step = 0;
void dfs(long long x) {
visited[x] = 1;
for (auto u : graph[x]) {
if (!visited[u]) {
if (color[u] != color[x]) {
step++;
}
dfs(u);
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, m;
cin >> n >> m;
vector<long long> arr(n + 1);
for (long long i = 1; i <= n; ++i) {
cin >> arr[i];
}
while (m--) {
long long l, r, x;
cin >> l >> r >> x;
long long cnt = 0;
for (long long i = l; i <= r; ++i) {
if (arr[i] < arr[x]) {
cnt++;
}
}
if (l + cnt == x) {
cout << "Yes"
<< "\n";
} else {
cout << "No"
<< "\n";
}
}
}
| ### Prompt
Create a solution in CPP for the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
const long long int INF = 1e18L + 5;
using namespace std;
void input() {}
long long p1(long long base, long long power) {
long long res = 1;
while (power != 0) {
if (power % 2 == 0) {
base = ((base % 1000000007) * (base % 1000000007)) % 1000000007;
power = floor(power / 2);
} else {
res = ((res % 1000000007) * (base % 1000000007)) % 1000000007;
power = power - 1;
}
}
return res;
}
bool compare(int a, int b) { return a > b; }
vector<long long> graph[10001];
long long color[10001];
long long visited[10001];
long long step = 0;
void dfs(long long x) {
visited[x] = 1;
for (auto u : graph[x]) {
if (!visited[u]) {
if (color[u] != color[x]) {
step++;
}
dfs(u);
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, m;
cin >> n >> m;
vector<long long> arr(n + 1);
for (long long i = 1; i <= n; ++i) {
cin >> arr[i];
}
while (m--) {
long long l, r, x;
cin >> l >> r >> x;
long long cnt = 0;
for (long long i = l; i <= r; ++i) {
if (arr[i] < arr[x]) {
cnt++;
}
}
if (l + cnt == x) {
cout << "Yes"
<< "\n";
} else {
cout << "No"
<< "\n";
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int arr[10005];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) scanf("%d", arr + i);
int l, r, x;
while (m--) {
scanf("%d%d%d", &l, &r, &x);
x--;
r--;
l--;
if (x > r || x < l)
cout << "Yes" << endl;
else {
int count = 0;
for (int i = l; i <= r; i++) {
if (arr[i] < arr[x]) count++;
}
if (l + count == x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int arr[10005];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) scanf("%d", arr + i);
int l, r, x;
while (m--) {
scanf("%d%d%d", &l, &r, &x);
x--;
r--;
l--;
if (x > r || x < l)
cout << "Yes" << endl;
else {
int count = 0;
for (int i = l; i <= r; i++) {
if (arr[i] < arr[x]) count++;
}
if (l + count == x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) {
return {l.first + r.first, l.second + r.second};
}
typedef void (*callback_function)(void);
const long long ZERO = 0LL;
const long long INF64 = 1e18;
const long long INF32 = 1e9;
const long long MOD = 1e9 + 7;
const long double PI = acos(-1.0L);
const long double EPS = static_cast<long double>(1e-9);
inline long long Pow(long long a, long long k) {
long long s = 1;
for (; k; k >>= 1) {
k& 1 ? s = 1LL * s * a % MOD : 0;
a = 1LL * a * a % MOD;
}
return s;
}
const long long N = 1e6 + 7;
void input() {}
void preprocess() {}
void debug() {
if (true) {
}
}
void solve() {
preprocess();
long long n, m;
cin >> n >> m;
long long a[n];
for (long long i = (0); i < (n); i++) {
cin >> a[i];
}
for (long long i = (0); i < (m); i++) {
long long L, R, x;
cin >> L >> R >> x;
L--;
x--;
long long y = L;
for (long long j = (L); j < (R); j++) {
if (a[j] < a[x]) {
y++;
}
}
cout << (y == x ? "Yes" : "No") << endl;
}
}
void output() {}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
{
input();
solve();
output();
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) {
return {l.first + r.first, l.second + r.second};
}
typedef void (*callback_function)(void);
const long long ZERO = 0LL;
const long long INF64 = 1e18;
const long long INF32 = 1e9;
const long long MOD = 1e9 + 7;
const long double PI = acos(-1.0L);
const long double EPS = static_cast<long double>(1e-9);
inline long long Pow(long long a, long long k) {
long long s = 1;
for (; k; k >>= 1) {
k& 1 ? s = 1LL * s * a % MOD : 0;
a = 1LL * a * a % MOD;
}
return s;
}
const long long N = 1e6 + 7;
void input() {}
void preprocess() {}
void debug() {
if (true) {
}
}
void solve() {
preprocess();
long long n, m;
cin >> n >> m;
long long a[n];
for (long long i = (0); i < (n); i++) {
cin >> a[i];
}
for (long long i = (0); i < (m); i++) {
long long L, R, x;
cin >> L >> R >> x;
L--;
x--;
long long y = L;
for (long long j = (L); j < (R); j++) {
if (a[j] < a[x]) {
y++;
}
}
cout << (y == x ? "Yes" : "No") << endl;
}
}
void output() {}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
{
input();
solve();
output();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
int num[maxn];
int main() {
int n, m, l, r, q;
while (scanf("%d %d", &n, &m) == 2) {
for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
while (m--) {
scanf("%d%d%d", &l, &r, &q);
if (l > q || r < q || l == r == q)
puts("Yes");
else {
int cnt = 0, p = num[q];
for (int i = l; i <= r; i++) {
if (num[i] < p) cnt++;
}
if (cnt + l == q)
puts("Yes");
else
puts("No");
}
}
}
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
int num[maxn];
int main() {
int n, m, l, r, q;
while (scanf("%d %d", &n, &m) == 2) {
for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
while (m--) {
scanf("%d%d%d", &l, &r, &q);
if (l > q || r < q || l == r == q)
puts("Yes");
else {
int cnt = 0, p = num[q];
for (int i = l; i <= r; i++) {
if (num[i] < p) cnt++;
}
if (cnt + l == q)
puts("Yes");
else
puts("No");
}
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
int arr[10005];
int main() {
std::ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> arr[i];
for (int i = 0; i < m; i++) {
int l, r, x;
cin >> l >> r >> x;
if (x <= r && x >= l) {
int curr_pos = x - l;
int gt = 0, lt = 0;
for (int j = l; j <= r; j++) {
if (arr[j] > arr[x]) gt++;
if (arr[j] < arr[x]) lt++;
}
if (lt == curr_pos)
cout << "Yes\n";
else
cout << "No\n";
} else
cout << "No\n";
}
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
int arr[10005];
int main() {
std::ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> arr[i];
for (int i = 0; i < m; i++) {
int l, r, x;
cin >> l >> r >> x;
if (x <= r && x >= l) {
int curr_pos = x - l;
int gt = 0, lt = 0;
for (int j = l; j <= r; j++) {
if (arr[j] > arr[x]) gt++;
if (arr[j] < arr[x]) lt++;
}
if (lt == curr_pos)
cout << "Yes\n";
else
cout << "No\n";
} else
cout << "No\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int v[112324];
bool pos(int c, int f, int e) {
c = c - 1;
f = f - 1;
e = v[e - 1];
int ori, mae = 0;
for (int i = c; i <= f; i++) {
if (v[i] == e) {
ori = i - c;
}
if (v[i] < e) {
mae++;
}
}
return mae == ori;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int a, b;
cin >> a >> b;
for (int i = 0; i < a; i++) {
cin >> v[i];
}
for (int i = 0; i < b; i++) {
int c, f, e;
cin >> c >> f >> e;
if (pos(c, f, e)) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int v[112324];
bool pos(int c, int f, int e) {
c = c - 1;
f = f - 1;
e = v[e - 1];
int ori, mae = 0;
for (int i = c; i <= f; i++) {
if (v[i] == e) {
ori = i - c;
}
if (v[i] < e) {
mae++;
}
}
return mae == ori;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int a, b;
cin >> a >> b;
for (int i = 0; i < a; i++) {
cin >> v[i];
}
for (int i = 0; i < b; i++) {
int c, f, e;
cin >> c >> f >> e;
if (pos(c, f, e)) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> num[10005];
int main() {
int n, m, a;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
num[i] = make_pair(a, i + 1);
}
sort(num, num + n);
for (int i = 0; i < m; i++) {
int l, r, x, c = 0;
scanf("%d%d%d", &l, &r, &x);
for (int i = 0; i < n; i++) {
int ind = num[i].second;
if (l <= ind && ind <= r) {
if (ind == x) break;
c++;
}
}
if ((l + c) == x)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
pair<int, int> num[10005];
int main() {
int n, m, a;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
num[i] = make_pair(a, i + 1);
}
sort(num, num + n);
for (int i = 0; i < m; i++) {
int l, r, x, c = 0;
scanf("%d%d%d", &l, &r, &x);
for (int i = 0; i < n; i++) {
int ind = num[i].second;
if (l <= ind && ind <= r) {
if (ind == x) break;
c++;
}
}
if ((l + c) == x)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, j;
cin >> n >> m;
int arr[n];
for (i = 0; i < n; i++) cin >> arr[i];
int l, r, x;
for (i = 0; i < m; i++) {
cin >> l >> r >> x;
l--, r--, x--;
int cnt = 0;
for (j = l; j <= r; j++) {
if (arr[j] < arr[x]) cnt++;
}
if ((l + cnt) == x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, j;
cin >> n >> m;
int arr[n];
for (i = 0; i < n; i++) cin >> arr[i];
int l, r, x;
for (i = 0; i < m; i++) {
cin >> l >> r >> x;
l--, r--, x--;
int cnt = 0;
for (j = l; j <= r; j++) {
if (arr[j] < arr[x]) cnt++;
}
if ((l + cnt) == x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
double pi = 3.141592653589793238462;
string numkey = "0123456789";
string uni = "abcdefghijklmnopqrstuvwxyz";
signed main() {
long long int n, m;
cin >> n >> m;
vector<long long int> arr(n + 1);
for (long long int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (long long int i = 0; i < m; i++) {
long long int l, r, pos;
cin >> l >> r >> pos;
long long int ctr = 0;
long long int e = arr[pos];
ctr += (l - 1);
for (long long int i = l; i <= r; i++) ctr += (arr[i] < e);
if (ctr == pos - 1) {
cout << "Yes\n";
} else
cout << "No\n";
}
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
double pi = 3.141592653589793238462;
string numkey = "0123456789";
string uni = "abcdefghijklmnopqrstuvwxyz";
signed main() {
long long int n, m;
cin >> n >> m;
vector<long long int> arr(n + 1);
for (long long int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (long long int i = 0; i < m; i++) {
long long int l, r, pos;
cin >> l >> r >> pos;
long long int ctr = 0;
long long int e = arr[pos];
ctr += (l - 1);
for (long long int i = l; i <= r; i++) ctr += (arr[i] < e);
if (ctr == pos - 1) {
cout << "Yes\n";
} else
cout << "No\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int MAXN = 1e6 + 1;
long long int num[10005], a[10005];
long long int work(long long int l, long long int r, long long int x) {
long long int val = a[x];
long long int tot = 0;
memset(num, 0, sizeof(num));
for (long long int i = l; i <= r; i++) {
if (a[i] < val) {
tot++;
}
}
if (l + tot == x) return 1;
return 0;
}
signed main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
;
long long int m, n, i;
cin >> n >> m;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 1; i <= m; i++) {
long long int l, r, x;
cin >> l >> r >> x;
if (l == r) {
cout << "Yes"
<< "\n";
} else if (work(l, r, x) == 1) {
cout << "Yes"
<< "\n";
} else {
cout << "No"
<< "\n";
}
}
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int MAXN = 1e6 + 1;
long long int num[10005], a[10005];
long long int work(long long int l, long long int r, long long int x) {
long long int val = a[x];
long long int tot = 0;
memset(num, 0, sizeof(num));
for (long long int i = l; i <= r; i++) {
if (a[i] < val) {
tot++;
}
}
if (l + tot == x) return 1;
return 0;
}
signed main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
;
long long int m, n, i;
cin >> n >> m;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 1; i <= m; i++) {
long long int l, r, x;
cin >> l >> r >> x;
if (l == r) {
cout << "Yes"
<< "\n";
} else if (work(l, r, x) == 1) {
cout << "Yes"
<< "\n";
} else {
cout << "No"
<< "\n";
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int i, s[12000], c[12000];
for (i = 1; i <= n; i++) {
cin >> s[i];
}
while (m--) {
int a, b, cn;
cin >> a >> b >> cn;
int cn1 = 0;
if (cn < a || cn > b) {
printf("Yes\n");
break;
} else {
for (i = a; i <= b; i++) {
if (s[i] < s[cn]) cn1++;
}
int zz = cn1;
if (a + zz == cn)
printf("Yes\n");
else
printf("No\n");
}
}
}
| ### Prompt
Generate a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int i, s[12000], c[12000];
for (i = 1; i <= n; i++) {
cin >> s[i];
}
while (m--) {
int a, b, cn;
cin >> a >> b >> cn;
int cn1 = 0;
if (cn < a || cn > b) {
printf("Yes\n");
break;
} else {
for (i = a; i <= b; i++) {
if (s[i] < s[cn]) cn1++;
}
int zz = cn1;
if (a + zz == cn)
printf("Yes\n");
else
printf("No\n");
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) {
return {l.first + r.first, l.second + r.second};
}
typedef void (*callback_function)(void);
const long long ZERO = 0LL;
const long long INF64 = 1e18;
const long long INF32 = 1e9;
const long long MOD = 1e9 + 7;
const long double PI = acos(-1.0L);
const long double EPS = static_cast<long double>(1e-9);
inline long long Pow(long long a, long long k) {
long long s = 1;
for (; k; k >>= 1) {
k& 1 ? s = 1LL * s * a % MOD : 0;
a = 1LL * a * a % MOD;
}
return s;
}
const long long N = 1e6 + 7;
void input() {}
void preprocess() {}
void debug() {
if (true) {
}
}
void solve() {
preprocess();
long long n, m;
cin >> n >> m;
long long a[n];
for (long long i = (0); i < (n); i++) {
cin >> a[i];
}
for (long long i = (0); i < (m); i++) {
long long L, R, x;
cin >> L >> R >> x;
L--;
x--;
long long y = L;
for (long long j = (L); j < (R); j++) {
if (a[j] < a[x]) {
y++;
}
}
cout << (y == x ? "Yes" : "No") << endl;
}
}
void output() {}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
{
input();
solve();
output();
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) {
return {l.first + r.first, l.second + r.second};
}
typedef void (*callback_function)(void);
const long long ZERO = 0LL;
const long long INF64 = 1e18;
const long long INF32 = 1e9;
const long long MOD = 1e9 + 7;
const long double PI = acos(-1.0L);
const long double EPS = static_cast<long double>(1e-9);
inline long long Pow(long long a, long long k) {
long long s = 1;
for (; k; k >>= 1) {
k& 1 ? s = 1LL * s * a % MOD : 0;
a = 1LL * a * a % MOD;
}
return s;
}
const long long N = 1e6 + 7;
void input() {}
void preprocess() {}
void debug() {
if (true) {
}
}
void solve() {
preprocess();
long long n, m;
cin >> n >> m;
long long a[n];
for (long long i = (0); i < (n); i++) {
cin >> a[i];
}
for (long long i = (0); i < (m); i++) {
long long L, R, x;
cin >> L >> R >> x;
L--;
x--;
long long y = L;
for (long long j = (L); j < (R); j++) {
if (a[j] < a[x]) {
y++;
}
}
cout << (y == x ? "Yes" : "No") << endl;
}
}
void output() {}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
{
input();
solve();
output();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 10;
int p[maxn], cp[maxn];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i]);
}
for (int i = 1; i <= m; i++) {
int s, e, x;
scanf("%d%d%d", &s, &e, &x);
int cnt = 0;
for (int j = s; j <= e; j++) {
if (p[j] < p[x]) cnt++;
}
if (cnt == x - s)
printf("Yes\n");
else
printf("No\n");
}
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 10;
int p[maxn], cp[maxn];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i]);
}
for (int i = 1; i <= m; i++) {
int s, e, x;
scanf("%d%d%d", &s, &e, &x);
int cnt = 0;
for (int j = s; j <= e; j++) {
if (p[j] < p[x]) cnt++;
}
if (cnt == x - s)
printf("Yes\n");
else
printf("No\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int a[n + 5];
int b[n + 5];
memset(b, 0, sizeof(b));
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
while (m--) {
int f, t, x;
cin >> f >> t >> x;
int d = 0;
for (int i = f; i <= t; i++) {
if (a[i] < a[x]) d++;
}
if (f + d == x) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int a[n + 5];
int b[n + 5];
memset(b, 0, sizeof(b));
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
while (m--) {
int f, t, x;
cin >> f >> t >> x;
int d = 0;
for (int i = f; i <= t; i++) {
if (a[i] < a[x]) d++;
}
if (f + d == x) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void changed(vector<int> v, int l, int r, int x) {
x--;
l--;
r--;
for (int n = l; n <= r; n++)
if (v[n] < v[x]) l++;
printf("%s\n", l == x ? "Yes" : "No");
}
int main() {
int n, m;
int k;
int li, ri, xi;
cin >> n >> m;
vector<int> v, l, r, x;
for (int i = 0; i < n; i++) {
cin >> k;
v.push_back(k);
}
for (int i = 0; i < m; i++) {
cin >> li >> ri >> xi;
l.push_back(li);
r.push_back(ri);
x.push_back(xi);
}
for (int i = 0; i < m; i++) {
changed(v, l[i], r[i], x[i]);
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void changed(vector<int> v, int l, int r, int x) {
x--;
l--;
r--;
for (int n = l; n <= r; n++)
if (v[n] < v[x]) l++;
printf("%s\n", l == x ? "Yes" : "No");
}
int main() {
int n, m;
int k;
int li, ri, xi;
cin >> n >> m;
vector<int> v, l, r, x;
for (int i = 0; i < n; i++) {
cin >> k;
v.push_back(k);
}
for (int i = 0; i < m; i++) {
cin >> li >> ri >> xi;
l.push_back(li);
r.push_back(ri);
x.push_back(xi);
}
for (int i = 0; i < m; i++) {
changed(v, l[i], r[i], x[i]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
string DBG_DLM(int& i) { return (i++ == 0 ? "" : ", "); }
template <class T>
ostream& operator<<(ostream& os, vector<T> v);
template <class T>
ostream& operator<<(ostream& os, set<T> v);
template <class T>
ostream& operator<<(ostream& os, queue<T> q);
template <class T>
ostream& operator<<(ostream& os, priority_queue<T> q);
template <class T, class K>
ostream& operator<<(ostream& os, pair<T, K> p);
template <class T, class K>
ostream& operator<<(ostream& os, map<T, K> mp);
template <class T, class K>
ostream& operator<<(ostream& os, unordered_map<T, K> mp);
template <int I, class TPL>
void DBG(ostream& os, TPL t) {}
template <int I, class TPL, class H, class... Ts>
void DBG(ostream& os, TPL t) {
os << (I == 0 ? "" : ", ") << get<I>(t);
DBG<I + 1, TPL, Ts...>(os, t);
}
template <class T, class K>
void DBG(ostream& os, pair<T, K> p, string delim) {
os << "(" << p.first << delim << p.second << ")";
}
template <class... Ts>
ostream& operator<<(ostream& os, tuple<Ts...> t) {
os << "(";
DBG<0, tuple<Ts...>, Ts...>(os, t);
os << ")";
return os;
}
template <class T, class K>
ostream& operator<<(ostream& os, pair<T, K> p) {
DBG(os, p, ", ");
return os;
}
template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
{
int i = 0;
os << "{";
{
for (auto&& t : v) {
os << DBG_DLM(i) << t;
};
}
os << "}";
return os;
};
}
template <class T>
ostream& operator<<(ostream& os, set<T> s) {
{
int i = 0;
os << "{";
{
for (auto&& t : s) {
os << DBG_DLM(i) << t;
};
}
os << "}";
return os;
};
}
template <class T>
ostream& operator<<(ostream& os, queue<T> q) {
{
int i = 0;
os << "{";
{
for (; q.size(); q.pop()) {
os << DBG_DLM(i) << q.front();
};
}
os << "}";
return os;
};
}
template <class T>
ostream& operator<<(ostream& os, priority_queue<T> q) {
{
int i = 0;
os << "{";
{
for (; q.size(); q.pop()) {
os << DBG_DLM(i) << q.top();
};
}
os << "}";
return os;
};
}
template <class T, class K>
ostream& operator<<(ostream& os, map<T, K> m) {
{
int i = 0;
os << "{";
{
for (auto&& p : m) {
os << DBG_DLM(i);
DBG(os, p, "->");
};
}
os << "}";
return os;
};
}
template <class T, class K>
ostream& operator<<(ostream& os, unordered_map<T, K> m) {
{
int i = 0;
os << "{";
{
for (auto&& p : m) {
os << DBG_DLM(i);
DBG(os, p, "->");
};
}
os << "}";
return os;
};
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vvvpii = vector<vvpii>;
template <class T>
bool amax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
bool amin(T& a, const T& b) {
return a > b ? a = b, 1 : 0;
}
ll ri() {
ll l;
cin >> l;
return l;
}
string rs() {
string s;
cin >> s;
return s;
}
void Main() {
int n = ri();
int q = ri();
vector<int> A(n);
for (int i = int(0); i < int(n); ++i) A[i] = ri();
while (q--) {
int l = ri() - 1;
int r = ri();
int x = ri() - 1;
int y = x - l;
int z = A[x];
int cnt = 0;
for (int i = int(l); i < int(r); ++i) {
if (A[i] < z) cnt++;
}
if (cnt == y)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
Main();
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string DBG_DLM(int& i) { return (i++ == 0 ? "" : ", "); }
template <class T>
ostream& operator<<(ostream& os, vector<T> v);
template <class T>
ostream& operator<<(ostream& os, set<T> v);
template <class T>
ostream& operator<<(ostream& os, queue<T> q);
template <class T>
ostream& operator<<(ostream& os, priority_queue<T> q);
template <class T, class K>
ostream& operator<<(ostream& os, pair<T, K> p);
template <class T, class K>
ostream& operator<<(ostream& os, map<T, K> mp);
template <class T, class K>
ostream& operator<<(ostream& os, unordered_map<T, K> mp);
template <int I, class TPL>
void DBG(ostream& os, TPL t) {}
template <int I, class TPL, class H, class... Ts>
void DBG(ostream& os, TPL t) {
os << (I == 0 ? "" : ", ") << get<I>(t);
DBG<I + 1, TPL, Ts...>(os, t);
}
template <class T, class K>
void DBG(ostream& os, pair<T, K> p, string delim) {
os << "(" << p.first << delim << p.second << ")";
}
template <class... Ts>
ostream& operator<<(ostream& os, tuple<Ts...> t) {
os << "(";
DBG<0, tuple<Ts...>, Ts...>(os, t);
os << ")";
return os;
}
template <class T, class K>
ostream& operator<<(ostream& os, pair<T, K> p) {
DBG(os, p, ", ");
return os;
}
template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
{
int i = 0;
os << "{";
{
for (auto&& t : v) {
os << DBG_DLM(i) << t;
};
}
os << "}";
return os;
};
}
template <class T>
ostream& operator<<(ostream& os, set<T> s) {
{
int i = 0;
os << "{";
{
for (auto&& t : s) {
os << DBG_DLM(i) << t;
};
}
os << "}";
return os;
};
}
template <class T>
ostream& operator<<(ostream& os, queue<T> q) {
{
int i = 0;
os << "{";
{
for (; q.size(); q.pop()) {
os << DBG_DLM(i) << q.front();
};
}
os << "}";
return os;
};
}
template <class T>
ostream& operator<<(ostream& os, priority_queue<T> q) {
{
int i = 0;
os << "{";
{
for (; q.size(); q.pop()) {
os << DBG_DLM(i) << q.top();
};
}
os << "}";
return os;
};
}
template <class T, class K>
ostream& operator<<(ostream& os, map<T, K> m) {
{
int i = 0;
os << "{";
{
for (auto&& p : m) {
os << DBG_DLM(i);
DBG(os, p, "->");
};
}
os << "}";
return os;
};
}
template <class T, class K>
ostream& operator<<(ostream& os, unordered_map<T, K> m) {
{
int i = 0;
os << "{";
{
for (auto&& p : m) {
os << DBG_DLM(i);
DBG(os, p, "->");
};
}
os << "}";
return os;
};
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vvvpii = vector<vvpii>;
template <class T>
bool amax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
bool amin(T& a, const T& b) {
return a > b ? a = b, 1 : 0;
}
ll ri() {
ll l;
cin >> l;
return l;
}
string rs() {
string s;
cin >> s;
return s;
}
void Main() {
int n = ri();
int q = ri();
vector<int> A(n);
for (int i = int(0); i < int(n); ++i) A[i] = ri();
while (q--) {
int l = ri() - 1;
int r = ri();
int x = ri() - 1;
int y = x - l;
int z = A[x];
int cnt = 0;
for (int i = int(l); i < int(r); ++i) {
if (A[i] < z) cnt++;
}
if (cnt == y)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
Main();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10010;
int num[MAXN], a[MAXN];
int n;
int work(int l, int r, int x) {
int v = a[x], tot = 0;
memset(num, 0, sizeof(num));
for (int i = l; i <= r; i++) {
if (a[i] < v) {
tot++;
}
}
if (l + tot == x) {
return 1;
} else {
return 0;
}
}
int main() {
int l, r, x;
int m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &l, &r, &x);
if (l == r) {
cout << "Yes" << endl;
} else if (work(l, r, x) == 1) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10010;
int num[MAXN], a[MAXN];
int n;
int work(int l, int r, int x) {
int v = a[x], tot = 0;
memset(num, 0, sizeof(num));
for (int i = l; i <= r; i++) {
if (a[i] < v) {
tot++;
}
}
if (l + tot == x) {
return 1;
} else {
return 0;
}
}
int main() {
int l, r, x;
int m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &l, &r, &x);
if (l == r) {
cout << "Yes" << endl;
} else if (work(l, r, x) == 1) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
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;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
inline void writeln(int x) {
write(x);
puts("");
}
int n, m, a[10100];
int main() {
n = read();
m = read();
for (int i = 1; i <= n; ++i) a[i] = read();
while (m--) {
int l = read(), r = read(), x = read(), less = x - l;
for (int i = l; i <= r; ++i) less -= a[i] < a[x];
puts(less ? "No" : "Yes");
}
}
| ### Prompt
Please formulate a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### 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;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
inline void writeln(int x) {
write(x);
puts("");
}
int n, m, a[10100];
int main() {
n = read();
m = read();
for (int i = 1; i <= n; ++i) a[i] = read();
while (m--) {
int l = read(), r = read(), x = read(), less = x - l;
for (int i = l; i <= r; ++i) less -= a[i] < a[x];
puts(less ? "No" : "Yes");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int arr[n + 1];
for (int i = 1; i <= n; i++) cin >> arr[i];
while (m--) {
int l, r, x;
cin >> l >> r >> x;
int ct = 0;
int index;
for (int i = l; i <= r; i++) {
if (arr[x] > arr[i]) ct++;
}
ct += l;
if (ct == x)
cout << "Yes\n";
else
cout << "No\n";
}
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int arr[n + 1];
for (int i = 1; i <= n; i++) cin >> arr[i];
while (m--) {
int l, r, x;
cin >> l >> r >> x;
int ct = 0;
int index;
for (int i = l; i <= r; i++) {
if (arr[x] > arr[i]) ct++;
}
ct += l;
if (ct == x)
cout << "Yes\n";
else
cout << "No\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, p[10001];
int l, r, x;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= m; i++) {
cin >> l >> r >> x;
int c = 0;
for (int j = l; j <= r; j++) {
if (p[j] < p[x]) {
c++;
}
}
if (c == (x - l)) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, p[10001];
int l, r, x;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= m; i++) {
cin >> l >> r >> x;
int c = 0;
for (int j = l; j <= r; j++) {
if (p[j] < p[x]) {
c++;
}
}
if (c == (x - l)) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int check(long long int a) {
if (a >= 1000000000 + 7) a %= 1000000000 + 7;
return a;
}
long long int check2(long long int a) {
if (a > 0) return a;
long long int b = a / 1000000000 + 7;
a -= b * 1000000000 + 7;
if (a < 0) a += 1000000000 + 7;
return a;
}
long long int exp(long long int a, long long int n) {
if (n == 0) return 1;
if (n == 1) return check(a);
long long int b = exp(a, n / 2);
if (n % 2 == 0) return check(b * b);
return check(b * check(b * a));
}
long long int arr[100005 + 5];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> arr[i];
while (m--) {
int l, r;
cin >> l >> r;
l--;
r--;
int first;
cin >> first;
first--;
int d = arr[first];
int coun = 0;
for (int i = l; i <= r; i++)
if (arr[i] < d) coun++;
if (first < l || first > r)
cout << "Yes\n";
else if (first == l + coun)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int check(long long int a) {
if (a >= 1000000000 + 7) a %= 1000000000 + 7;
return a;
}
long long int check2(long long int a) {
if (a > 0) return a;
long long int b = a / 1000000000 + 7;
a -= b * 1000000000 + 7;
if (a < 0) a += 1000000000 + 7;
return a;
}
long long int exp(long long int a, long long int n) {
if (n == 0) return 1;
if (n == 1) return check(a);
long long int b = exp(a, n / 2);
if (n % 2 == 0) return check(b * b);
return check(b * check(b * a));
}
long long int arr[100005 + 5];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> arr[i];
while (m--) {
int l, r;
cin >> l >> r;
l--;
r--;
int first;
cin >> first;
first--;
int d = arr[first];
int coun = 0;
for (int i = l; i <= r; i++)
if (arr[i] < d) coun++;
if (first < l || first > r)
cout << "Yes\n";
else if (first == l + coun)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long sum(long long n, long long a, long long d) {
if (n <= 0) return 0;
return (n * (2 * a + (n - 1) * d)) / 2;
}
int main() {
ios::sync_with_stdio(false);
int t, n, m, i, j, k, a[10004], l, r, x;
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> a[i];
}
while (m--) {
cin >> l >> r >> x;
for (i = l - 1, j = 0; i < r; i++) {
if (a[i] < a[x - 1]) {
j++;
}
}
if (x - l == j) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long sum(long long n, long long a, long long d) {
if (n <= 0) return 0;
return (n * (2 * a + (n - 1) * d)) / 2;
}
int main() {
ios::sync_with_stdio(false);
int t, n, m, i, j, k, a[10004], l, r, x;
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> a[i];
}
while (m--) {
cin >> l >> r >> x;
for (i = l - 1, j = 0; i < r; i++) {
if (a[i] < a[x - 1]) {
j++;
}
}
if (x - l == j) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
using namespace std;
int n, m, tot;
int v[maxn], rt[maxn];
struct sag {
int ls, rs, siz;
} s[3000000];
void insert(int x, int &y, int l, int r, int pos) {
if (pos > r) return;
y = ++tot;
if (l == r) {
s[y].siz = 1;
return;
}
int mid = l + r >> 1;
if (pos <= mid)
s[y].rs = s[x].rs, insert(s[x].ls, s[y].ls, l, mid, pos);
else
s[y].ls = s[x].ls, insert(s[x].rs, s[y].rs, mid + 1, r, pos);
s[y].siz = s[s[y].ls].siz + s[s[y].rs].siz;
}
int query(int x, int y, int l, int r, int a, int b) {
if (a <= l && r <= b) return s[x].siz - s[y].siz;
int mid = l + r >> 1;
if (b <= mid) return query(s[x].ls, s[y].ls, l, mid, a, b);
if (a > mid) return query(s[x].rs, s[y].ls, mid + 1, r, a, b);
return query(s[x].ls, s[y].ls, l, mid, a, b) +
query(s[x].rs, s[y].rs, mid + 1, r, a, b);
}
int main() {
scanf("%d%d", &n, &m);
int i, a, b, c, d;
for (i = 1; i <= n; i++)
scanf("%d", &v[i]), insert(rt[i - 1], rt[i], 1, n, v[i]);
for (i = 1; i <= m; i++) {
scanf("%d%d%d", &a, &b, &c);
d = query(rt[b], rt[a - 1], 1, n, 1, v[c]);
if (d == c - a + 1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
using namespace std;
int n, m, tot;
int v[maxn], rt[maxn];
struct sag {
int ls, rs, siz;
} s[3000000];
void insert(int x, int &y, int l, int r, int pos) {
if (pos > r) return;
y = ++tot;
if (l == r) {
s[y].siz = 1;
return;
}
int mid = l + r >> 1;
if (pos <= mid)
s[y].rs = s[x].rs, insert(s[x].ls, s[y].ls, l, mid, pos);
else
s[y].ls = s[x].ls, insert(s[x].rs, s[y].rs, mid + 1, r, pos);
s[y].siz = s[s[y].ls].siz + s[s[y].rs].siz;
}
int query(int x, int y, int l, int r, int a, int b) {
if (a <= l && r <= b) return s[x].siz - s[y].siz;
int mid = l + r >> 1;
if (b <= mid) return query(s[x].ls, s[y].ls, l, mid, a, b);
if (a > mid) return query(s[x].rs, s[y].ls, mid + 1, r, a, b);
return query(s[x].ls, s[y].ls, l, mid, a, b) +
query(s[x].rs, s[y].rs, mid + 1, r, a, b);
}
int main() {
scanf("%d%d", &n, &m);
int i, a, b, c, d;
for (i = 1; i <= n; i++)
scanf("%d", &v[i]), insert(rt[i - 1], rt[i], 1, n, v[i]);
for (i = 1; i <= m; i++) {
scanf("%d%d%d", &a, &b, &c);
d = query(rt[b], rt[a - 1], 1, n, 1, v[c]);
if (d == c - a + 1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
long long n, m, x, l, r, minn = 0, maxx = 0;
cin >> n >> m;
vector<long long> v;
for (int i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
}
for (int i = 0; i < m; i++) {
cin >> l >> r >> x;
minn = 0, maxx = 0;
l--, r--, x--;
for (int h = l; h < x; h++) {
if (v[h] > v[x]) maxx++;
}
for (int h = x + 1; h <= r; h++) {
if (v[h] < v[x]) minn++;
}
if (maxx == minn) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
long long n, m, x, l, r, minn = 0, maxx = 0;
cin >> n >> m;
vector<long long> v;
for (int i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
}
for (int i = 0; i < m; i++) {
cin >> l >> r >> x;
minn = 0, maxx = 0;
l--, r--, x--;
for (int h = l; h < x; h++) {
if (v[h] > v[x]) maxx++;
}
for (int h = x + 1; h <= r; h++) {
if (v[h] < v[x]) minn++;
}
if (maxx == minn) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int arr[100005];
int main() {
int n, m, l, r, x;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &l, &r, &x);
int cnt = 0;
for (int i = l - 1; i < r; i++) {
if (arr[i] < arr[x - 1]) cnt++;
}
if (cnt == x - l)
puts("Yes");
else
puts("No");
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int arr[100005];
int main() {
int n, m, l, r, x;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &l, &r, &x);
int cnt = 0;
for (int i = l - 1; i < r; i++) {
if (arr[i] < arr[x - 1]) cnt++;
}
if (cnt == x - l)
puts("Yes");
else
puts("No");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long Mypow(long long X, long long Y) {
if (Y < 0) return 0;
if (Y == 0) return 1;
if (Y == 1) {
return X;
} else {
long long x = Mypow(X, Y / 2);
x *= x;
if (Y % 2)
return (x * X);
else
return x;
}
}
long long n, m, l, r, x;
long long p[100100];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
while (m--) {
scanf("%I64d %I64d %I64d", &l, &r, &x);
if (x < l || x > r) {
printf("Yes\n");
continue;
}
int sum = 0;
for (int i = l; i <= r; i++) {
if (p[i] < p[x]) {
sum++;
}
}
if (x == l + sum) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long Mypow(long long X, long long Y) {
if (Y < 0) return 0;
if (Y == 0) return 1;
if (Y == 1) {
return X;
} else {
long long x = Mypow(X, Y / 2);
x *= x;
if (Y % 2)
return (x * X);
else
return x;
}
}
long long n, m, l, r, x;
long long p[100100];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
while (m--) {
scanf("%I64d %I64d %I64d", &l, &r, &x);
if (x < l || x > r) {
printf("Yes\n");
continue;
}
int sum = 0;
for (int i = l; i <= r; i++) {
if (p[i] < p[x]) {
sum++;
}
}
if (x == l + sum) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 0;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = 1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return f ? -x : x;
}
int a[10020];
int main() {
int n = read(), m = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= m; i++) {
int l = read(), r = read(), k = read(), sum = 0;
for (int i = l; i <= r; i++)
if (a[i] < a[k]) sum++;
puts(sum == k - l ? "Yes" : "No");
}
}
| ### Prompt
Please create a solution in cpp to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 0;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = 1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return f ? -x : x;
}
int a[10020];
int main() {
int n = read(), m = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= m; i++) {
int l = read(), r = read(), k = read(), sum = 0;
for (int i = l; i <= r; i++)
if (a[i] < a[k]) sum++;
puts(sum == k - l ? "Yes" : "No");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long inf = LLONG_MAX;
const long long mod = 998244353;
void solve() {
long long n, m;
cin >> n >> m;
vector<long long> p(n);
for (long long i = (0); i < (n); i++) {
cin >> p[i];
}
for (long long i = (0); i < (m); i++) {
long long l, r, x, cnt = 0;
cin >> l >> r >> x;
for (long long j = (l - 1); j < (r); j++) {
if (p[x - 1] > p[j]) {
cnt++;
}
}
cout << ((cnt == x - l) ? "Yes" : "No") << "\n";
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long inf = LLONG_MAX;
const long long mod = 998244353;
void solve() {
long long n, m;
cin >> n >> m;
vector<long long> p(n);
for (long long i = (0); i < (n); i++) {
cin >> p[i];
}
for (long long i = (0); i < (m); i++) {
long long l, r, x, cnt = 0;
cin >> l >> r >> x;
for (long long j = (l - 1); j < (r); j++) {
if (p[x - 1] > p[j]) {
cnt++;
}
}
cout << ((cnt == x - l) ? "Yes" : "No") << "\n";
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
long long minVal(long long x, long long y) { return (x < y) ? x : y; }
long long getMid(long long s, long long e) { return s + (e - s) / 2; }
long long RMQUtil(long long *st, long long ss, long long se, long long qs,
long long qe, long long index) {
if (qs <= ss && qe >= se) return st[index];
if (se < qs || ss > qe) return INT_MAX;
long long mid = getMid(ss, se);
return minVal(RMQUtil(st, ss, mid, qs, qe, 2 * index + 1),
RMQUtil(st, mid + 1, se, qs, qe, 2 * index + 2));
}
long long RMQ(long long *st, long long n, long long qs, long long qe) {
return RMQUtil(st, 0, n - 1, qs, qe, 0);
}
long long constructSTUtil(vector<long long> arr, long long ss, long long se,
long long *st, long long si) {
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
long long mid = getMid(ss, se);
st[si] = minVal(constructSTUtil(arr, ss, mid, st, si * 2 + 1),
constructSTUtil(arr, mid + 1, se, st, si * 2 + 2));
return st[si];
}
long long *constructST(vector<long long> arr, long long n) {
long long x = (long long)(ceil(log2(n)));
long long max_size = 2 * (long long)pow(2, x) - 1;
long long *st = new long long[max_size];
constructSTUtil(arr, 0, n - 1, st, 0);
return st;
}
struct node {
long long c, k, x1, y1, oc, i, j;
};
struct CompareHeight {
bool operator()(node const &p1, node const &p2) { return p1.c > p2.c; }
};
long long minimum(long long x, long long y) { return min(x, y); }
bool comp1(pair<long long, long long> p1, pair<long long, long long> p2) {
return p1.first < p2.first;
}
bool comp2(pair<long long, long long> p1, pair<long long, long long> p2) {
return p1.second < p2.second;
}
vector<long long> a, b;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, m;
cin >> n >> m;
vector<long long> x(n + 1);
vector<long long> m1(n + 1);
for (int i = 1; i <= n; i++) {
cin >> x[i];
m1[x[i]] = i;
}
while (m-- > 0) {
long long l, r, x1;
cin >> l >> r >> x1;
long long cnt = 0;
for (int i = 1; i < x[x1]; i++) {
if (m1[i] >= l && m1[i] <= r) cnt++;
}
if (cnt == x1 - l)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
| ### Prompt
Develop a solution in CPP to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
long long minVal(long long x, long long y) { return (x < y) ? x : y; }
long long getMid(long long s, long long e) { return s + (e - s) / 2; }
long long RMQUtil(long long *st, long long ss, long long se, long long qs,
long long qe, long long index) {
if (qs <= ss && qe >= se) return st[index];
if (se < qs || ss > qe) return INT_MAX;
long long mid = getMid(ss, se);
return minVal(RMQUtil(st, ss, mid, qs, qe, 2 * index + 1),
RMQUtil(st, mid + 1, se, qs, qe, 2 * index + 2));
}
long long RMQ(long long *st, long long n, long long qs, long long qe) {
return RMQUtil(st, 0, n - 1, qs, qe, 0);
}
long long constructSTUtil(vector<long long> arr, long long ss, long long se,
long long *st, long long si) {
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
long long mid = getMid(ss, se);
st[si] = minVal(constructSTUtil(arr, ss, mid, st, si * 2 + 1),
constructSTUtil(arr, mid + 1, se, st, si * 2 + 2));
return st[si];
}
long long *constructST(vector<long long> arr, long long n) {
long long x = (long long)(ceil(log2(n)));
long long max_size = 2 * (long long)pow(2, x) - 1;
long long *st = new long long[max_size];
constructSTUtil(arr, 0, n - 1, st, 0);
return st;
}
struct node {
long long c, k, x1, y1, oc, i, j;
};
struct CompareHeight {
bool operator()(node const &p1, node const &p2) { return p1.c > p2.c; }
};
long long minimum(long long x, long long y) { return min(x, y); }
bool comp1(pair<long long, long long> p1, pair<long long, long long> p2) {
return p1.first < p2.first;
}
bool comp2(pair<long long, long long> p1, pair<long long, long long> p2) {
return p1.second < p2.second;
}
vector<long long> a, b;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, m;
cin >> n >> m;
vector<long long> x(n + 1);
vector<long long> m1(n + 1);
for (int i = 1; i <= n; i++) {
cin >> x[i];
m1[x[i]] = i;
}
while (m-- > 0) {
long long l, r, x1;
cin >> l >> r >> x1;
long long cnt = 0;
for (int i = 1; i < x[x1]; i++) {
if (m1[i] >= l && m1[i] <= r) cnt++;
}
if (cnt == x1 - l)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector<int> arr;
for (int i = 0; i < n; ++i) {
int a;
scanf("%d", &a);
arr.push_back(a);
}
for (int i = 0; i < m; ++i) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
--a;
--b;
--c;
vector<int> temp(arr);
swap(temp[a], temp[c]);
int l = a + 1;
int j = b;
while (1) {
while (l <= b && temp[l] <= temp[a]) ++l;
while (j >= a && temp[j] > temp[a]) --j;
if (l >= j) break;
swap(temp[l], temp[j]);
}
if (c == j)
puts("Yes");
else
puts("No");
}
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector<int> arr;
for (int i = 0; i < n; ++i) {
int a;
scanf("%d", &a);
arr.push_back(a);
}
for (int i = 0; i < m; ++i) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
--a;
--b;
--c;
vector<int> temp(arr);
swap(temp[a], temp[c]);
int l = a + 1;
int j = b;
while (1) {
while (l <= b && temp[l] <= temp[a]) ++l;
while (j >= a && temp[j] > temp[a]) --j;
if (l >= j) break;
swap(temp[l], temp[j]);
}
if (c == j)
puts("Yes");
else
puts("No");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[10001], i, l, r, x, j, tam;
int main() {
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 1; i <= m; i++) {
scanf("%d%d%d", &l, &r, &x);
tam = x - l;
for (j = l; j <= r; j++)
if (a[j] < a[x]) --tam;
puts(tam == 0 ? "Yes" : "No");
}
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, a[10001], i, l, r, x, j, tam;
int main() {
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 1; i <= m; i++) {
scanf("%d%d%d", &l, &r, &x);
tam = x - l;
for (j = l; j <= r; j++)
if (a[j] < a[x]) --tam;
puts(tam == 0 ? "Yes" : "No");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int pos[100001];
int arr[100001];
void init(int *arr, int *pos, int l, int r) {
for (int i = l; i <= r; ++i) {
arr[i] = pos[i];
}
}
int main() {
int n, m, val, pst, cnt1 = 0, cnt2 = 0;
int l, r, x;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%d", arr + i);
pos[i] = arr[i];
}
while (m--) {
scanf("%d %d %d", &l, &r, &x);
l--;
r--;
x--;
pst = arr[x];
for (int i = x + 1; i <= r; ++i) {
if (pst > arr[i]) {
cnt1++;
}
}
for (int i = x - 1; i >= l; --i) {
if (pst < arr[i]) {
cnt2++;
}
}
if (cnt1 == cnt2) {
printf("Yes\n");
} else {
printf("No\n");
}
cnt1 = 0;
cnt2 = 0;
}
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int pos[100001];
int arr[100001];
void init(int *arr, int *pos, int l, int r) {
for (int i = l; i <= r; ++i) {
arr[i] = pos[i];
}
}
int main() {
int n, m, val, pst, cnt1 = 0, cnt2 = 0;
int l, r, x;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%d", arr + i);
pos[i] = arr[i];
}
while (m--) {
scanf("%d %d %d", &l, &r, &x);
l--;
r--;
x--;
pst = arr[x];
for (int i = x + 1; i <= r; ++i) {
if (pst > arr[i]) {
cnt1++;
}
}
for (int i = x - 1; i >= l; --i) {
if (pst < arr[i]) {
cnt2++;
}
}
if (cnt1 == cnt2) {
printf("Yes\n");
} else {
printf("No\n");
}
cnt1 = 0;
cnt2 = 0;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, j, k, P[10000], l, r, x;
cin >> n >> m;
for (i = 0; i < n; i++) {
scanf("%d", &P[i]);
}
int count = 0;
for (i = 0; i < m; i++) {
count = 0;
scanf("%d%d%d", &l, &r, &x);
for (j = l - 1; j < x - 1; j++) {
if (P[j] > P[x - 1]) {
count++;
}
}
for (j = x; j <= r - 1; j++) {
if (P[j] < P[x - 1]) {
count--;
}
}
if (count == 0)
printf("Yes\n");
else
printf("No\n");
}
}
| ### Prompt
Please create a solution in cpp to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, j, k, P[10000], l, r, x;
cin >> n >> m;
for (i = 0; i < n; i++) {
scanf("%d", &P[i]);
}
int count = 0;
for (i = 0; i < m; i++) {
count = 0;
scanf("%d%d%d", &l, &r, &x);
for (j = l - 1; j < x - 1; j++) {
if (P[j] > P[x - 1]) {
count++;
}
}
for (j = x; j <= r - 1; j++) {
if (P[j] < P[x - 1]) {
count--;
}
}
if (count == 0)
printf("Yes\n");
else
printf("No\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[10004], b[10004];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
while (m--) {
int l, r, index;
cin >> l >> r >> index;
int c = 0, d = 0;
for (int i = l; i <= r; i++)
if (a[i] < a[index])
c++;
else if (a[i] == a[index])
d++;
index -= (l - 1);
if (index > c && index <= c + d)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[10004], b[10004];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
while (m--) {
int l, r, index;
cin >> l >> r >> index;
int c = 0, d = 0;
for (int i = l; i <= r; i++)
if (a[i] < a[index])
c++;
else if (a[i] == a[index])
d++;
index -= (l - 1);
if (index > c && index <= c + d)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-7;
const long long MOD = 1000000009;
const int INF = 0x3f3f3f3f;
const int maxn = 1e4 + 10;
int n, m, arr[maxn];
int main(void) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &arr[i]);
for (int i = 0, l, r, x; i < m; ++i) {
scanf("%d%d%d", &l, &r, &x);
int cnt = 0;
for (int j = l; j <= r; ++j)
if (arr[j] < arr[x]) ++cnt;
printf(cnt == x - l ? "Yes\n" : "No\n");
}
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-7;
const long long MOD = 1000000009;
const int INF = 0x3f3f3f3f;
const int maxn = 1e4 + 10;
int n, m, arr[maxn];
int main(void) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &arr[i]);
for (int i = 0, l, r, x; i < m; ++i) {
scanf("%d%d%d", &l, &r, &x);
int cnt = 0;
for (int j = l; j <= r; ++j)
if (arr[j] < arr[x]) ++cnt;
printf(cnt == x - l ? "Yes\n" : "No\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, x, l, r, b;
cin >> n >> m;
vector<int> v(n), vcopy;
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < m; i++) {
cin >> l >> r >> x;
b = 0;
for (int k = l - 1; k <= r - 1; k++) {
if (v[k] < v[x - 1]) b++;
}
if (x < l or x > r)
cout << "Yes" << endl;
else {
if ((l - 1) + b == x - 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, x, l, r, b;
cin >> n >> m;
vector<int> v(n), vcopy;
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < m; i++) {
cin >> l >> r >> x;
b = 0;
for (int k = l - 1; k <= r - 1; k++) {
if (v[k] < v[x - 1]) b++;
}
if (x < l or x > r)
cout << "Yes" << endl;
else {
if ((l - 1) + b == x - 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int m;
cin >> n >> m;
vector<int> zahlen(n);
for (int i = 0; i < n; i++) cin >> zahlen[i];
for (int i = 0; i < m; i++) {
int l, r, x;
cin >> l >> r >> x;
l--;
x--;
r--;
int tr = zahlen[x];
int count = 0;
for (int i = l; i <= r; i++) {
if (zahlen[i] < tr) count++;
}
if ((l + count) == x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int m;
cin >> n >> m;
vector<int> zahlen(n);
for (int i = 0; i < n; i++) cin >> zahlen[i];
for (int i = 0; i < m; i++) {
int l, r, x;
cin >> l >> r >> x;
l--;
x--;
r--;
int tr = zahlen[x];
int count = 0;
for (int i = l; i <= r; i++) {
if (zahlen[i] < tr) count++;
}
if ((l + count) == x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[20001];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 0; i < m; i++) {
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
int an = a[x];
int length = 0;
for (int j = l; j <= r; j++) {
if (a[j] < an) length++;
}
if (length == x - l)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[20001];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 0; i < m; i++) {
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
int an = a[x];
int length = 0;
for (int j = l; j <= r; j++) {
if (a[j] < an) length++;
}
if (length == x - l)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void getint(int &v) {
char ch, fu = 0;
for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
;
if (ch == '-') fu = 1, ch = getchar();
for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0';
if (fu) v = -v;
}
int n, m, l, r, x, a[1000010], b[1000010];
int main() {
srand(613);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
while (m--) {
scanf("%d%d%d", &l, &r, &x);
int s = 0;
for (int i = l; i <= r; ++i) b[++s] = a[i];
random_shuffle(b + 1, b + s + 1);
nth_element(b + 1, b + (x - l + 1), b + s + 1);
if (b[x - l + 1] == a[x])
puts("Yes");
else
puts("No");
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void getint(int &v) {
char ch, fu = 0;
for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
;
if (ch == '-') fu = 1, ch = getchar();
for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0';
if (fu) v = -v;
}
int n, m, l, r, x, a[1000010], b[1000010];
int main() {
srand(613);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
while (m--) {
scanf("%d%d%d", &l, &r, &x);
int s = 0;
for (int i = l; i <= r; ++i) b[++s] = a[i];
random_shuffle(b + 1, b + s + 1);
nth_element(b + 1, b + (x - l + 1), b + s + 1);
if (b[x - l + 1] == a[x])
puts("Yes");
else
puts("No");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int p[n + 1];
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 0; i < m; i++) {
int cnt = 0;
int l, r, x;
cin >> l >> r >> x;
for (int s = l - 1; s < r; s++) {
if (p[s] < p[x - 1]) cnt++;
}
if (l + cnt != x)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int p[n + 1];
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 0; i < m; i++) {
int cnt = 0;
int l, r, x;
cin >> l >> r >> x;
for (int s = l - 1; s < r; s++) {
if (p[s] < p[x - 1]) cnt++;
}
if (l + cnt != x)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1e4;
int n, m;
int l, r, x;
int a[MaxN + 5];
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) {
int big = 0, small = 0;
scanf("%d %d %d", &l, &r, &x);
for (int j = l; j <= r; j++) {
if (a[j] < a[x])
small++;
else if (a[j] > a[x])
big++;
}
if (x - l == small)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1e4;
int n, m;
int l, r, x;
int a[MaxN + 5];
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) {
int big = 0, small = 0;
scanf("%d %d %d", &l, &r, &x);
for (int j = l; j <= r; j++) {
if (a[j] < a[x])
small++;
else if (a[j] > a[x])
big++;
}
if (x - l == small)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[10005], b[10005], n, m;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
int l, r, x;
cin >> l >> r >> x;
int cnt = 1;
for (int i = l; i <= r; i++) {
if (a[x] > a[i]) cnt++;
}
if (x == l + cnt - 1) {
cout << "Yes" << endl;
} else
cout << "No" << endl;
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[10005], b[10005], n, m;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
int l, r, x;
cin >> l >> r >> x;
int cnt = 1;
for (int i = l; i <= r; i++) {
if (a[x] > a[i]) cnt++;
}
if (x == l + cnt - 1) {
cout << "Yes" << endl;
} else
cout << "No" << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int l, r, x;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
while (m--) {
cin >> l >> r >> x;
l--;
r--;
x--;
vector<int> t(r - l + 1);
for (int i = 0; i < r - l + 1; i++) t[i] = a[l + i];
int temp = t[x - l];
t[x - l] = t[0];
t[0] = temp;
temp = 1;
for (int i = 1; i < r - l + 1; i++) {
if (t[i] < t[0]) {
int k = t[temp];
t[temp] = t[i];
t[i] = k;
temp++;
}
}
if (temp - 1 == x - l)
cout << "Yes";
else
cout << "No";
cout << endl;
}
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int l, r, x;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
while (m--) {
cin >> l >> r >> x;
l--;
r--;
x--;
vector<int> t(r - l + 1);
for (int i = 0; i < r - l + 1; i++) t[i] = a[l + i];
int temp = t[x - l];
t[x - l] = t[0];
t[0] = temp;
temp = 1;
for (int i = 1; i < r - l + 1; i++) {
if (t[i] < t[0]) {
int k = t[temp];
t[temp] = t[i];
t[i] = k;
temp++;
}
}
if (temp - 1 == x - l)
cout << "Yes";
else
cout << "No";
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int arr[10004];
int main() {
int n, m, i, j, k;
scanf("%d", &n);
scanf("%d", &m);
for (i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < m; i++) {
int l, r, x, t1 = 0;
scanf("%d", &l);
scanf("%d", &r);
scanf("%d", &x);
if (l == r) {
printf("Yes\n");
continue;
}
for (j = l, k = r; j < k; j++, k--) {
if (arr[j] < arr[x]) t1++;
if (arr[k] < arr[x]) t1++;
}
if ((r + l) % 2 == 0)
if (arr[(l + r) / 2] < arr[x]) t1++;
if (x == l + t1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int arr[10004];
int main() {
int n, m, i, j, k;
scanf("%d", &n);
scanf("%d", &m);
for (i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < m; i++) {
int l, r, x, t1 = 0;
scanf("%d", &l);
scanf("%d", &r);
scanf("%d", &x);
if (l == r) {
printf("Yes\n");
continue;
}
for (j = l, k = r; j < k; j++, k--) {
if (arr[j] < arr[x]) t1++;
if (arr[k] < arr[x]) t1++;
}
if ((r + l) % 2 == 0)
if (arr[(l + r) / 2] < arr[x]) t1++;
if (x == l + t1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e9 + 7;
const long long mod = 1e9 + 7;
const long long maxn = 1e4 + 7;
int a, b;
int s[maxn];
int main() {
scanf("%d%d", &a, &b);
for (int i = 0; i < a; i++) scanf("%d", &s[i]);
int l, r, xx;
for (int i = 0; i < b; i++) {
scanf("%d%d%d", &l, &r, &xx);
set<int> q;
if (xx > r || xx < l) {
printf("Yes\n");
continue;
}
int ans = xx - l, sa = s[xx - 1], k = 0;
for (int j = l - 1; j < r; j++)
if (s[j] < sa) k++;
if (ans == k)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e9 + 7;
const long long mod = 1e9 + 7;
const long long maxn = 1e4 + 7;
int a, b;
int s[maxn];
int main() {
scanf("%d%d", &a, &b);
for (int i = 0; i < a; i++) scanf("%d", &s[i]);
int l, r, xx;
for (int i = 0; i < b; i++) {
scanf("%d%d%d", &l, &r, &xx);
set<int> q;
if (xx > r || xx < l) {
printf("Yes\n");
continue;
}
int ans = xx - l, sa = s[xx - 1], k = 0;
for (int j = l - 1; j < r; j++)
if (s[j] < sa) k++;
if (ans == k)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long i, j, k, l, p, q, x, y, z, n, m, c, t, h, r;
cin >> n >> m;
long long arr[n + 1];
for (i = 1; i < n + 1; i++) cin >> arr[i];
for (j = 0; j < m; j++) {
cin >> l >> r >> x;
if (r - l + 1 == n) {
if (arr[x] == x)
cout << "Yes\n";
else
cout << "No\n";
continue;
}
k = l;
for (i = l; i <= r; i++) {
if (arr[i] < arr[x]) k++;
}
if (arr[x] == arr[k])
cout << "Yes\n";
else
cout << "No\n";
}
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long i, j, k, l, p, q, x, y, z, n, m, c, t, h, r;
cin >> n >> m;
long long arr[n + 1];
for (i = 1; i < n + 1; i++) cin >> arr[i];
for (j = 0; j < m; j++) {
cin >> l >> r >> x;
if (r - l + 1 == n) {
if (arr[x] == x)
cout << "Yes\n";
else
cout << "No\n";
continue;
}
k = l;
for (i = l; i <= r; i++) {
if (arr[i] < arr[x]) k++;
}
if (arr[x] == arr[k])
cout << "Yes\n";
else
cout << "No\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int arr[10000];
vector<int> segTree[4 * 10000 + 50];
void createTree(int idx, int start, int end) {
if (start == end) {
segTree[idx].push_back(arr[start]);
return;
}
int mid = (start + end) / 2;
createTree(2 * idx + 1, start, mid);
createTree(2 * idx + 2, mid + 1, end);
int i, j;
i = 0;
j = 0;
int l = 2 * idx + 1;
int r = 2 * idx + 2;
while (i != segTree[l].size() || j != segTree[r].size()) {
if (j == segTree[r].size() ||
(i < segTree[l].size() && segTree[l][i] < segTree[r][j])) {
segTree[idx].push_back(segTree[l][i]);
i++;
} else {
segTree[idx].push_back(segTree[r][j]);
j++;
}
}
}
void printTree(int idx, int start, int end) {
cout << idx << " " << start << " " << end << ":"
<< "(" << segTree[idx].size() << ")";
for (int i = 0; i < segTree[idx].size(); i++) cout << segTree[idx][i] << " ";
cout << endl;
int mid = (start + end) / 2;
if (start != end) {
printTree(2 * idx + 1, start, mid);
printTree(2 * idx + 2, mid + 1, end);
}
}
int binSearch(vector<int> &data, int start, int end, int val) {
if (data[start] > val) return 0;
if (start >= end) return (start + 1);
int mid = (start + end) / 2;
if (data[mid] <= val && data[mid + 1] > val) return (mid + 1);
if (data[mid] < val)
return binSearch(data, mid + 1, end, val);
else
return binSearch(data, start, mid - 1, val);
}
int query(int startQ, int endQ, int val, int idx, int start, int end) {
if (startQ <= start && endQ >= end) {
int count = binSearch(segTree[idx], 0, segTree[idx].size() - 1, val);
return count;
}
if (endQ < start || startQ > end) return 0;
int mid = (start + end) / 2;
int count = 0;
count += query(startQ, endQ, val, 2 * idx + 1, start, mid);
count += query(startQ, endQ, val, 2 * idx + 2, mid + 1, end);
return count;
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> arr[i];
createTree(0, 0, n - 1);
for (int i = 0; i < m; i++) {
int a, b, x;
cin >> a >> b >> x;
a--;
b--;
x--;
int pos = query(a, b, arr[x], 0, 0, n - 1);
if (pos == x - a + 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
Examples
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:
1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int arr[10000];
vector<int> segTree[4 * 10000 + 50];
void createTree(int idx, int start, int end) {
if (start == end) {
segTree[idx].push_back(arr[start]);
return;
}
int mid = (start + end) / 2;
createTree(2 * idx + 1, start, mid);
createTree(2 * idx + 2, mid + 1, end);
int i, j;
i = 0;
j = 0;
int l = 2 * idx + 1;
int r = 2 * idx + 2;
while (i != segTree[l].size() || j != segTree[r].size()) {
if (j == segTree[r].size() ||
(i < segTree[l].size() && segTree[l][i] < segTree[r][j])) {
segTree[idx].push_back(segTree[l][i]);
i++;
} else {
segTree[idx].push_back(segTree[r][j]);
j++;
}
}
}
void printTree(int idx, int start, int end) {
cout << idx << " " << start << " " << end << ":"
<< "(" << segTree[idx].size() << ")";
for (int i = 0; i < segTree[idx].size(); i++) cout << segTree[idx][i] << " ";
cout << endl;
int mid = (start + end) / 2;
if (start != end) {
printTree(2 * idx + 1, start, mid);
printTree(2 * idx + 2, mid + 1, end);
}
}
int binSearch(vector<int> &data, int start, int end, int val) {
if (data[start] > val) return 0;
if (start >= end) return (start + 1);
int mid = (start + end) / 2;
if (data[mid] <= val && data[mid + 1] > val) return (mid + 1);
if (data[mid] < val)
return binSearch(data, mid + 1, end, val);
else
return binSearch(data, start, mid - 1, val);
}
int query(int startQ, int endQ, int val, int idx, int start, int end) {
if (startQ <= start && endQ >= end) {
int count = binSearch(segTree[idx], 0, segTree[idx].size() - 1, val);
return count;
}
if (endQ < start || startQ > end) return 0;
int mid = (start + end) / 2;
int count = 0;
count += query(startQ, endQ, val, 2 * idx + 1, start, mid);
count += query(startQ, endQ, val, 2 * idx + 2, mid + 1, end);
return count;
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> arr[i];
createTree(0, 0, n - 1);
for (int i = 0; i < m; i++) {
int a, b, x;
cin >> a >> b >> x;
a--;
b--;
x--;
int pos = query(a, b, arr[x], 0, 0, n - 1);
if (pos == x - a + 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.