output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; int main() { int a[5]; scanf("%d%d%d", &a[0], &a[1], &a[2]); sort(a, a + 3); if (a[0] + a[1] > a[2]) { printf("0\n"); } else { printf("%d\n", a[2] - a[1] - a[0] + 1); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a[5]; scanf("%d%d%d", &a[0], &a[1], &a[2]); sort(a, a + 3); if (a[0] + a[1] > a[2]) { printf("0\n"); } else { printf("%d\n", a[2] - a[1] - a[0] + 1); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int max; int a[3]; for (int i = 0; i < 3; i++) { cin >> a[i]; sort(a, a + 3); max = 1 + a[2] - (a[0] + a[1]); } if (max > 0) cout << max; else cout << "0"; }
### Prompt Please formulate a Cpp solution to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int max; int a[3]; for (int i = 0; i < 3; i++) { cin >> a[i]; sort(a, a + 3); max = 1 + a[2] - (a[0] + a[1]); } if (max > 0) cout << max; else cout << "0"; } ```
#include <bits/stdc++.h> int main() { int a, b, c; scanf("%d%d%d", &a, &b, &c); if ((a + b) == c || (c + b) == a || (a + c) == b) { printf("1"); } else { if ((a + b) < c) { printf("%d", (c - b - a + 1)); } else if (b + c < a) { printf("%d", (a - b - c + 1)); } else if (c + a < b) { printf("%d", (b - c - a + 1)); } else { printf("0"); } } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> int main() { int a, b, c; scanf("%d%d%d", &a, &b, &c); if ((a + b) == c || (c + b) == a || (a + c) == b) { printf("1"); } else { if ((a + b) < c) { printf("%d", (c - b - a + 1)); } else if (b + c < a) { printf("%d", (a - b - c + 1)); } else if (c + a < b) { printf("%d", (b - c - a + 1)); } else { printf("0"); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline int read() { char c = getchar(); int x = 0, fl = 1; while (!isdigit(c)) { if (c == '-') fl = -1; c = getchar(); } do { x = x * 10 + (c ^ 48); c = getchar(); } while (isdigit(c)); return x * fl; } int a[4]; int main() { for (int i = 1; i <= 3; ++i) a[i] = read(); sort(a + 1, a + 4); if (a[1] + a[2] > a[3]) printf("0\n"); else printf("%d\n", a[3] + 1 - a[1] - a[2]); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int read() { char c = getchar(); int x = 0, fl = 1; while (!isdigit(c)) { if (c == '-') fl = -1; c = getchar(); } do { x = x * 10 + (c ^ 48); c = getchar(); } while (isdigit(c)); return x * fl; } int a[4]; int main() { for (int i = 1; i <= 3; ++i) a[i] = read(); sort(a + 1, a + 4); if (a[1] + a[2] > a[3]) printf("0\n"); else printf("%d\n", a[3] + 1 - a[1] - a[2]); return 0; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:268435456") using namespace std; template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& t) { return in >> t.first >> t.second; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2>& t) { return out << t.first << " " << t.second; } template <typename T> istream& operator>>(istream& in, vector<T>& t) { for (long long i = 0; i < t.size(); i++) in >> t[i]; return in; } template <typename T> ostream& operator<<(ostream& out, vector<T>& t) { for (long long i = 0; i < t.size(); i++) out << t[i] << " "; return out; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long rrrr = 3e7 + 1; vector<long long> ve(3); cin >> ve; sort(ve.begin(), ve.end()); long long d = ve[2] - ve[1] - ve[0] + 1; d = max(0ll, d); cout << d << endl; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:268435456") using namespace std; template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& t) { return in >> t.first >> t.second; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2>& t) { return out << t.first << " " << t.second; } template <typename T> istream& operator>>(istream& in, vector<T>& t) { for (long long i = 0; i < t.size(); i++) in >> t[i]; return in; } template <typename T> ostream& operator<<(ostream& out, vector<T>& t) { for (long long i = 0; i < t.size(); i++) out << t[i] << " "; return out; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long rrrr = 3e7 + 1; vector<long long> ve(3); cin >> ve; sort(ve.begin(), ve.end()); long long d = ve[2] - ve[1] - ve[0] + 1; d = max(0ll, d); cout << d << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int d; d = max(a, b); d = max(d, c); if (d == a) { if (a - (b + c) >= 0) { cout << a - (b + c) + 1; } else { cout << "0"; } } else if (d == b) { if (b - (a + c) >= 0) { cout << b - (a + c) + 1; } else { cout << "0"; } } else { if (c - (b + a) >= 0) { cout << c - (b + a) + 1; } else { cout << "0"; } } }
### Prompt Your task is to create a cpp solution to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int d; d = max(a, b); d = max(d, c); if (d == a) { if (a - (b + c) >= 0) { cout << a - (b + c) + 1; } else { cout << "0"; } } else if (d == b) { if (b - (a + c) >= 0) { cout << b - (a + c) + 1; } else { cout << "0"; } } else { if (c - (b + a) >= 0) { cout << c - (b + a) + 1; } else { cout << "0"; } } } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; int ans = INT_MAX, f = 0; cin >> a >> b >> c; if (a + b <= c) ans = min(ans, c + 1 - (a + b)), f = 1; if (a + c <= b) ans = min(ans, b + 1 - (a + c)), f = 1; if (b + c <= a) ans = min(ans, a + 1 - (b + c)), f = 1; if (f == 0) cout << 0; else cout << ans; }
### Prompt In CPP, your task is to solve the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; int ans = INT_MAX, f = 0; cin >> a >> b >> c; if (a + b <= c) ans = min(ans, c + 1 - (a + b)), f = 1; if (a + c <= b) ans = min(ans, b + 1 - (a + c)), f = 1; if (b + c <= a) ans = min(ans, a + 1 - (b + c)), f = 1; if (f == 0) cout << 0; else cout << ans; } ```
#include <bits/stdc++.h> using namespace std; int a, b, c, i; int main() { cin >> a >> b >> c; while (a >= b + c) { b++; i++; } while (b >= a + c) { a++; i++; } while (c >= a + b) { a++; i++; } cout << i; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a, b, c, i; int main() { cin >> a >> b >> c; while (a >= b + c) { b++; i++; } while (b >= a + c) { a++; i++; } while (c >= a + b) { a++; i++; } cout << i; return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[4], ans; int main() { ios::sync_with_stdio(0); cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); if (a[2] >= a[1] + a[0]) ans = a[2] - (a[0] + a[1] - 1); cout << ans; return 0; }
### Prompt Generate a CPP solution to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[4], ans; int main() { ios::sync_with_stdio(0); cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); if (a[2] >= a[1] + a[0]) ans = a[2] - (a[0] + a[1] - 1); cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[17]; int main() { cin >> a[1] >> a[2] >> a[3]; sort(a + 1, a + 4); cout << max(0, a[3] - (a[1] + a[2]) + 1); return 0; }
### Prompt In CPP, your task is to solve the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[17]; int main() { cin >> a[1] >> a[2] >> a[3]; sort(a + 1, a + 4); cout << max(0, a[3] - (a[1] + a[2]) + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; void compare(int &a, int &b, int &c) { if (c > b) swap(c, b); if (b > a) swap(b, a); if (c > b) swap(c, b); } int main() { int a, b, c; cin >> a >> b >> c; compare(a, b, c); int res = 0; while (true) { if (a + b > c && b + c > a && c + a > b) { break; } c++; res++; compare(a, b, c); } cout << res; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void compare(int &a, int &b, int &c) { if (c > b) swap(c, b); if (b > a) swap(b, a); if (c > b) swap(c, b); } int main() { int a, b, c; cin >> a >> b >> c; compare(a, b, c); int res = 0; while (true) { if (a + b > c && b + c > a && c + a > b) { break; } c++; res++; compare(a, b, c); } cout << res; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { vector<int> a(3); cin >> a[0] >> a[1] >> a[2]; sort(a.begin(), a.end()); if (a[2] >= a[0] + a[1]) cout << a[2] - (a[0] + a[1]) + 1; else cout << 0; return 0; }
### Prompt Create a solution in Cpp for the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { vector<int> a(3); cin >> a[0] >> a[1] >> a[2]; sort(a.begin(), a.end()); if (a[2] >= a[0] + a[1]) cout << a[2] - (a[0] + a[1]) + 1; else cout << 0; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int mx = 0; mx = max(a, b); mx = max(mx, c); int ans = a + b + c - mx; if (ans > mx) cout << 0; else cout << mx - ans + 1; return 0; }
### Prompt Please create a solution in CPP to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int mx = 0; mx = max(a, b); mx = max(mx, c); int ans = a + b + c - mx; if (ans > mx) cout << 0; else cout << mx - ans + 1; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { vector<int> abc(3); for (int i = 0; i < 3; i++) cin >> abc[i]; sort(abc.begin(), abc.end()); if (abc[0] + abc[1] > abc[2]) return cout << 0 << endl, 0; cout << abc[2] - abc[1] - abc[0] + 1 << endl; }
### Prompt Create a solution in CPP for the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { vector<int> abc(3); for (int i = 0; i < 3; i++) cin >> abc[i]; sort(abc.begin(), abc.end()); if (abc[0] + abc[1] > abc[2]) return cout << 0 << endl, 0; cout << abc[2] - abc[1] - abc[0] + 1 << endl; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> void dprint(T arg) { cout << arg << "\n"; } template <typename T> void dprint(const vector<T>& arg) { for_each(begin(arg), end(arg), [](T value) { cout << value << " "; }); cout << "\n"; } template <typename T> void dprint(const vector<vector<T>>& arg) { for_each(begin(arg), end(arg), [=](vector<T> arg2) { dprint(arg2); cout << "\n"; }); } using namespace std; int main() { long long a, b, c; long long x, y; cin >> a >> b >> c; vector<long long> d; d.push_back(a); d.push_back(b); d.push_back(c); sort((d).begin(), (d).end()); x = d[0] + d[1]; y = d[2]; if (x > y) { cout << 0 << "\n"; } else { cout << y - x + 1 << "\n"; } }
### Prompt Create a solution in CPP for the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> void dprint(T arg) { cout << arg << "\n"; } template <typename T> void dprint(const vector<T>& arg) { for_each(begin(arg), end(arg), [](T value) { cout << value << " "; }); cout << "\n"; } template <typename T> void dprint(const vector<vector<T>>& arg) { for_each(begin(arg), end(arg), [=](vector<T> arg2) { dprint(arg2); cout << "\n"; }); } using namespace std; int main() { long long a, b, c; long long x, y; cin >> a >> b >> c; vector<long long> d; d.push_back(a); d.push_back(b); d.push_back(c); sort((d).begin(), (d).end()); x = d[0] + d[1]; y = d[2]; if (x > y) { cout << 0 << "\n"; } else { cout << y - x + 1 << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int a, b, c, d; cin >> a >> b >> c; if (a > b) { if (a > c) { d = a - b - c; } else { d = c - b - a; } } else { if (b > c) { d = b - a - c; } else { d = c - b - a; } } if (d < 0) { d = 0; } else d = d + 1; cout << d; return 0; }
### Prompt Develop a solution in cpp to the problem described below: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int a, b, c, d; cin >> a >> b >> c; if (a > b) { if (a > c) { d = a - b - c; } else { d = c - b - a; } } else { if (b > c) { d = b - a - c; } else { d = c - b - a; } } if (d < 0) { d = 0; } else d = d + 1; cout << d; return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; double calcArea(int a, int b, int c) { const double p = static_cast<double>(a + b + c) / 2.0; try { return sqrt(p * (p - a) * (p - b) * (p - c)); } catch (exception& e) { return 0; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int a, b, c, inc = 0; cin >> a >> b >> c; if (calcArea(a, b, c) == 0) { inc = 100 * 3; for (int ai = a; ai <= 100; ai++) { for (int bi = b; bi <= 100; bi++) { for (int ci = c; ci <= 100; ci++) { if (calcArea(ai, bi, ci) > 0) inc = min(inc, (ai - a) + (bi - b) + (ci - c)); } } } } cout << inc; }
### Prompt Create a solution in cpp for the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; double calcArea(int a, int b, int c) { const double p = static_cast<double>(a + b + c) / 2.0; try { return sqrt(p * (p - a) * (p - b) * (p - c)); } catch (exception& e) { return 0; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int a, b, c, inc = 0; cin >> a >> b >> c; if (calcArea(a, b, c) == 0) { inc = 100 * 3; for (int ai = a; ai <= 100; ai++) { for (int bi = b; bi <= 100; bi++) { for (int ci = c; ci <= 100; ci++) { if (calcArea(ai, bi, ci) > 0) inc = min(inc, (ai - a) + (bi - b) + (ci - c)); } } } } cout << inc; } ```
#include <bits/stdc++.h> int main() { int a, b, c, t = 0; scanf("%d%d%d", &a, &b, &c); if ((a + b) <= c) t = (c - (a + b)) + 1; if ((b + c) <= a) t = (a - (c + b)) + 1; if ((a + c) <= b) t = (b - (a + c)) + 1; printf("%d", t); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> int main() { int a, b, c, t = 0; scanf("%d%d%d", &a, &b, &c); if ((a + b) <= c) t = (c - (a + b)) + 1; if ((b + c) <= a) t = (a - (c + b)) + 1; if ((a + c) <= b) t = (b - (a + c)) + 1; printf("%d", t); return 0; } ```
#include <bits/stdc++.h> using namespace std; signed main() { int a, b, c; scanf("%d %d %d", &a, &b, &c); if (a + b > c && b + c > a && a + c > b) { printf("0"); return 0; } else if (b + c <= a) { printf("%d", a - (b + c) + 1); } else if (a + c <= b) { printf("%d", b - (a + c) + 1); } else { printf("%d", c - (b + a) + 1); } return 0; }
### Prompt Create a solution in CPP for the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; signed main() { int a, b, c; scanf("%d %d %d", &a, &b, &c); if (a + b > c && b + c > a && a + c > b) { printf("0"); return 0; } else if (b + c <= a) { printf("%d", a - (b + c) + 1); } else if (a + c <= b) { printf("%d", b - (a + c) + 1); } else { printf("%d", c - (b + a) + 1); } return 0; } ```
#include <bits/stdc++.h> int main() { int a, b, c, t, i = 0; scanf("%d%d%d", &a, &b, &c); if (a > b) { t = a; a = b; b = t; } if (a > c) { t = a; a = c; c = t; } if (b > c) { t = b; b = c; c = t; } while (c - a >= b) { a++; i++; } printf("%d", i); return 0; }
### Prompt Please create a solution in cpp to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> int main() { int a, b, c, t, i = 0; scanf("%d%d%d", &a, &b, &c); if (a > b) { t = a; a = b; b = t; } if (a > c) { t = a; a = c; c = t; } if (b > c) { t = b; b = c; c = t; } while (c - a >= b) { a++; i++; } printf("%d", i); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int s[3]; for (int i = 0; i < 3; i++) { cin >> s[i]; } sort(s, s + 3); if (s[0] + 1 == s[1] && s[1] + 1 == s[2] || s[1] == s[2] && s[0] == s[1]) cout << '0'; else { int s1 = s[2] - (s[1] + 1); int s2 = s1 - (s[0] + 1); if (s2 + 3 < 0) cout << 0; else cout << s2 + 3; } }
### Prompt Please create a solution in Cpp to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int s[3]; for (int i = 0; i < 3; i++) { cin >> s[i]; } sort(s, s + 3); if (s[0] + 1 == s[1] && s[1] + 1 == s[2] || s[1] == s[2] && s[0] == s[1]) cout << '0'; else { int s1 = s[2] - (s[1] + 1); int s2 = s1 - (s[0] + 1); if (s2 + 3 < 0) cout << 0; else cout << s2 + 3; } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 10; int n, m, num[maxn]; int main() { int ans = 0; scanf("%d%d%d", &num[0], &num[1], &num[2]); sort(num, num + 3); if (num[1] + num[0] <= num[2]) ans += num[2] - num[1] - num[0] + 1; printf("%d\n", ans); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 10; int n, m, num[maxn]; int main() { int ans = 0; scanf("%d%d%d", &num[0], &num[1], &num[2]); sort(num, num + 3); if (num[1] + num[0] <= num[2]) ans += num[2] - num[1] - num[0] + 1; printf("%d\n", ans); return 0; } ```
#include <bits/stdc++.h> int min(int a, int b) { return ((a <= b) ? a : b); } int main() { int t = 1; for (int tc = 1; tc <= t; tc++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); int fx = a + b + c; int xx = a + b, xx2 = a + c, xx3 = b + c; int xl = min(xx, min(xx2, xx3)); fx = fx - xl; if (xl > fx) { printf("0\n"); } else { printf("%d\n", (fx + 1 - xl)); } } }
### Prompt Your challenge is to write a Cpp solution to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> int min(int a, int b) { return ((a <= b) ? a : b); } int main() { int t = 1; for (int tc = 1; tc <= t; tc++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); int fx = a + b + c; int xx = a + b, xx2 = a + c, xx3 = b + c; int xl = min(xx, min(xx2, xx3)); fx = fx - xl; if (xl > fx) { printf("0\n"); } else { printf("%d\n", (fx + 1 - xl)); } } } ```
#include <bits/stdc++.h> using namespace std; int main() { int arr[3]; cin >> arr[0] >> arr[1] >> arr[2]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2 - i; j++) { if (arr[j] < arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } if (arr[0] < arr[1] + arr[2]) cout << "0" << endl; else cout << arr[0] + 1 - (arr[1] + arr[2]) << endl; return 0; }
### Prompt In cpp, your task is to solve the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int arr[3]; cin >> arr[0] >> arr[1] >> arr[2]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2 - i; j++) { if (arr[j] < arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } if (arr[0] < arr[1] + arr[2]) cout << "0" << endl; else cout << arr[0] + 1 - (arr[1] + arr[2]) << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c; cin >> a >> b >> c; long long m, mi, mid; long long v[3]; v[0] = a; v[1] = b; v[2] = c; sort(v, v + 3); a = v[2]; b = v[0]; c = v[1]; if (a - b < c) { cout << "0"; } else { m = a - b - c + 1; cout << m; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long a, b, c; cin >> a >> b >> c; long long m, mi, mid; long long v[3]; v[0] = a; v[1] = b; v[2] = c; sort(v, v + 3); a = v[2]; b = v[0]; c = v[1]; if (a - b < c) { cout << "0"; } else { m = a - b - c + 1; cout << m; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[3]; int main(void) { for (int e = 0; e < 3; e++) cin >> a[e]; sort(a, a + 3); if (a[0] + a[1] > a[2]) { cout << "0"; } else { cout << a[2] - (a[0] + a[1]) + 1 << endl; } }
### Prompt Please provide a Cpp coded solution to the problem described below: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[3]; int main(void) { for (int e = 0; e < 3; e++) cin >> a[e]; sort(a, a + 3); if (a[0] + a[1] > a[2]) { cout << "0"; } else { cout << a[2] - (a[0] + a[1]) + 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, t; cin >> a >> b >> c; t = 0; d = a + b; e = a - b; if (d < 0) d = -d; if (e < 0) e = -e; if (d > c && e < c) cout << '0'; else { if (d <= c) t = t + c - d + 1; if (e >= c) t = t + e - c + 1; cout << t; } }
### Prompt Generate a cpp solution to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, t; cin >> a >> b >> c; t = 0; d = a + b; e = a - b; if (d < 0) d = -d; if (e < 0) e = -e; if (d > c && e < c) cout << '0'; else { if (d <= c) t = t + c - d + 1; if (e >= c) t = t + e - c + 1; cout << t; } } ```
#include <bits/stdc++.h> using namespace std; bool check(int a, int b, int c) { if (a + b > c && b + c > a && a + c > b) return true; return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int a, b, c; cin >> a >> b >> c; int mi = 1000; for (int i = 0; i <= max(a, max(b, c)); i++) for (int j = 0; j <= max(a, max(b, c)); j++) for (int k = 0; k <= max(a, max(b, c)); k++) if (check(a + i, b + j, c + k)) mi = min(mi, i + j + k); cout << mi; }
### Prompt Please provide a Cpp coded solution to the problem described below: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool check(int a, int b, int c) { if (a + b > c && b + c > a && a + c > b) return true; return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int a, b, c; cin >> a >> b >> c; int mi = 1000; for (int i = 0; i <= max(a, max(b, c)); i++) for (int j = 0; j <= max(a, max(b, c)); j++) for (int k = 0; k <= max(a, max(b, c)); k++) if (check(a + i, b + j, c + k)) mi = min(mi, i + j + k); cout << mi; } ```
#include <bits/stdc++.h> using namespace std; int a, b, c; int main() { scanf("%d%d%d", &a, &b, &c); if (a < b) swap(a, b); if (a < c) swap(a, c); if (b + c > a) printf("0"); else printf("%d", a - b - c + 1); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a, b, c; int main() { scanf("%d%d%d", &a, &b, &c); if (a < b) swap(a, b); if (a < c) swap(a, c); if (b + c > a) printf("0"); else printf("%d", a - b - c + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[3]; int main() { cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); if (a[0] + a[1] > a[2] && a[2] - a[0] < a[1]) cout << "0\n"; else { cout << a[2] - a[0] - a[1] + 1 << endl; } }
### Prompt Generate a cpp solution to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[3]; int main() { cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); if (a[0] + a[1] > a[2] && a[2] - a[0] < a[1]) cout << "0\n"; else { cout << a[2] - a[0] - a[1] + 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, t; scanf("%d%d%d", &a, &b, &c); if ((a < b + c) && (b < a + c) && (c < a + b)) { printf("0"); } else { if (a >= b + c) t = a - b - c; if (b >= a + c) t = b - a - c; if (c >= a + b) t = c - a - b; printf("%d", t + 1); } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c, t; scanf("%d%d%d", &a, &b, &c); if ((a < b + c) && (b < a + c) && (c < a + b)) { printf("0"); } else { if (a >= b + c) t = a - b - c; if (b >= a + c) t = b - a - c; if (c >= a + b) t = c - a - b; printf("%d", t + 1); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long a[10]; int main() { for (long long i = 1; i <= 3; i++) cin >> a[i]; sort(a + 1, a + 1 + 3); long long zr = 0; cout << max(zr, a[3] - (a[2] + a[1]) + 1); }
### Prompt Your task is to create a Cpp solution to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a[10]; int main() { for (long long i = 1; i <= 3; i++) cin >> a[i]; sort(a + 1, a + 1 + 3); long long zr = 0; cout << max(zr, a[3] - (a[2] + a[1]) + 1); } ```
#include <bits/stdc++.h> using namespace std; void swapstick(int &a, int &b, int &c) { int s; if (a > b) { s = b; b = a; a = s; } if (a > c) { s = c; c = a; a = s; } if (b > c) { s = c; c = b; b = s; } } bool succeed(int a, int b, int c) { if (a + b > c && a + c > b && b + c > a && a - b < c && a - c < b && b - c < a) { return true; } return false; } int main(int argc, char **argv) { int a, b, c; int min = 500; cin >> a >> b >> c; swapstick(a, b, c); for (int i = a; i <= c; i++) { for (int j = b; j <= c; j++) { if (succeed(i, j, c)) { if (i - a + j - b < min) { min = i - a + j - b; } } } } cout << min << endl; return 0; }
### Prompt Please formulate a CPP solution to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void swapstick(int &a, int &b, int &c) { int s; if (a > b) { s = b; b = a; a = s; } if (a > c) { s = c; c = a; a = s; } if (b > c) { s = c; c = b; b = s; } } bool succeed(int a, int b, int c) { if (a + b > c && a + c > b && b + c > a && a - b < c && a - c < b && b - c < a) { return true; } return false; } int main(int argc, char **argv) { int a, b, c; int min = 500; cin >> a >> b >> c; swapstick(a, b, c); for (int i = a; i <= c; i++) { for (int j = b; j <= c; j++) { if (succeed(i, j, c)) { if (i - a + j - b < min) { min = i - a + j - b; } } } } cout << min << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c; cin >> a >> b >> c; if (a >= (b + c)) { cout << abs(b + c - a) + 1 << endl; } else if (b >= (c + a)) { cout << abs(c + a - b) + 1 << endl; } else if (c >= (a + b)) { cout << abs(a + b - c) + 1 << endl; } else { cout << '0' << endl; } }
### Prompt Please create a solution in CPP to the following problem: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c; cin >> a >> b >> c; if (a >= (b + c)) { cout << abs(b + c - a) + 1 << endl; } else if (b >= (c + a)) { cout << abs(c + a - b) + 1 << endl; } else if (c >= (a + b)) { cout << abs(a + b - c) + 1 << endl; } else { cout << '0' << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { vector<int> tab(3, 0); for (int i = 0; i < 3; i++) cin >> tab[i]; sort(tab.begin(), tab.end()); cout << max(0, tab[2] - tab[1] - tab[0] + 1) << "\n"; }
### Prompt Construct a cpp code solution to the problem outlined: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { vector<int> tab(3, 0); for (int i = 0; i < 3; i++) cin >> tab[i]; sort(tab.begin(), tab.end()); cout << max(0, tab[2] - tab[1] - tab[0] + 1) << "\n"; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a[3]; cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); int diff = a[2] - (a[1] + a[0]) + 1; if (diff < 0) diff = 0; cout << diff << endl; }
### Prompt Construct a cpp code solution to the problem outlined: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a[3]; cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); int diff = a[2] - (a[1] + a[0]) + 1; if (diff < 0) diff = 0; cout << diff << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int area = 0; if (a > b && a > c) { area = a * 2 + 1 - a - b - c; } else if (b > a && b > c) { area = b * 2 + 1 - a - b - c; } else if (c > a && c > b) { area = c * 2 + 1 - a - b - c; } cout << max(area, 0) << "\n"; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Masha has three sticks of length a, b and c centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks. What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices. Input The only line contains tree integers a, b and c (1 ≀ a, b, c ≀ 100) β€” the lengths of sticks Masha possesses. Output Print a single integer β€” the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks. Examples Input 3 4 5 Output 0 Input 2 5 3 Output 1 Input 100 10 10 Output 81 Note In the first example, Masha can make a triangle from the sticks without increasing the length of any of them. In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 2 centimeter stick by one and after that form a triangle with sides 3, 3 and 5 centimeters. In the third example, Masha can take 33 minutes to increase one of the 10 centimeters sticks by 33 centimeters, and after that take 48 minutes to increase another 10 centimeters stick by 48 centimeters. This way she can form a triangle with lengths 43, 58 and 100 centimeters in 81 minutes. One can show that it is impossible to get a valid triangle faster. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int area = 0; if (a > b && a > c) { area = a * 2 + 1 - a - b - c; } else if (b > a && b > c) { area = b * 2 + 1 - a - b - c; } else if (c > a && c > b) { area = c * 2 + 1 - a - b - c; } cout << max(area, 0) << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; char getc() { char c = getchar(); while ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9')) c = getchar(); return c; } int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); } long long n, m, tree[3][200010], a[200010]; char s[200010]; long long val(char c) { if (c == 'R') return 0; if (c == 'P') return 1; if (c == 'S') return 2; } void add(int p, int k, int x) { while (k <= n) tree[p][k] += x, k += k & -k; } long long query(int p, int k) { int s = 0; while (k) s += tree[p][k], k -= k & -k; return s; } set<int> q[3]; int main() { cin >> n >> m; scanf("%s", s + 1); for (int i = 1; i <= n; i++) add(a[i] = val(s[i]), i, 1), q[val(s[i])].insert(i); for (int j = 0, x; j <= m; j++) { if (j) { cin >> x; long long y = val(getc()); add(a[x], x, -1), add(y, x, 1); q[a[x]].erase(x), a[x] = y, q[a[x]].insert(x); } long long ans = 0; for (int i = 0; i < 3; i++) { int sc = (i + 2) % 3, pp = (i + 1) % 3; if (q[pp].empty()) ans += query(i, n); else if (q[sc].empty()) ; else { ans += query(i, n); set<int>::iterator it = q[sc].begin(); int x = *it; it = q[sc].end(); it--; int y = *it; it = q[pp].begin(); if ((*it) < x) ans -= query(i, x) - query(i, (*it)); it = q[pp].end(); it--; if ((*it) > y) ans -= query(i, (*it)) - query(i, y); } } printf("%d\n", ans); } return 0; }
### Prompt Please formulate a cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; char getc() { char c = getchar(); while ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9')) c = getchar(); return c; } int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); } long long n, m, tree[3][200010], a[200010]; char s[200010]; long long val(char c) { if (c == 'R') return 0; if (c == 'P') return 1; if (c == 'S') return 2; } void add(int p, int k, int x) { while (k <= n) tree[p][k] += x, k += k & -k; } long long query(int p, int k) { int s = 0; while (k) s += tree[p][k], k -= k & -k; return s; } set<int> q[3]; int main() { cin >> n >> m; scanf("%s", s + 1); for (int i = 1; i <= n; i++) add(a[i] = val(s[i]), i, 1), q[val(s[i])].insert(i); for (int j = 0, x; j <= m; j++) { if (j) { cin >> x; long long y = val(getc()); add(a[x], x, -1), add(y, x, 1); q[a[x]].erase(x), a[x] = y, q[a[x]].insert(x); } long long ans = 0; for (int i = 0; i < 3; i++) { int sc = (i + 2) % 3, pp = (i + 1) % 3; if (q[pp].empty()) ans += query(i, n); else if (q[sc].empty()) ; else { ans += query(i, n); set<int>::iterator it = q[sc].begin(); int x = *it; it = q[sc].end(); it--; int y = *it; it = q[pp].begin(); if ((*it) < x) ans -= query(i, x) - query(i, (*it)); it = q[pp].end(); it--; if ((*it) > y) ans -= query(i, (*it)) - query(i, y); } } printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100, M = 3; int n, q, dex, ans, f[N], good[M], bad[M], fen[N][M]; string s; set<int> st[M]; void add(int dex, int type, int val) { for (; dex < N; dex += (dex & -dex)) fen[dex][type] += val; } int get(int dex, int type) { return (dex > 0 ? fen[dex][type] + get(dex ^ (dex & -dex), type) : 0); } void check(int l1, int r1, int l2, int r2, int type) { int l = max(l1, l2), r = min(r1, r2); if (r >= l) ans += get(r, type) - get(l - 1, type); } int solve() { ans = 0; for (int i = 0; i < M; i++) { if (st[i].empty()) continue; else if (st[i].size() == n) { ans = n; break; } if (st[bad[i]].empty()) { ans += st[i].size(); continue; } else if (st[good[i]].empty()) continue; int a, b, A, B; a = (*st[bad[i]].begin()) - 1; auto x = st[good[i]].begin(); b = (st[good[i]].empty() ? n + 1 : max(a + 1, *x)); B = (*st[bad[i]].rbegin()) + 1; x = --st[good[i]].end(); A = (st[good[i]].empty() ? 0 : min(*x, B - 1)); check(0, a, 0, A, i); check(0, a, B, n, i); check(b, n, 0, A, i); check(b, n, B, n, i); } return ans; } void add_element(int dex) { add(dex, f[s[dex]], 1); st[f[s[dex]]].insert(dex); } void del_element(int dex) { add(dex, f[s[dex]], -1); st[f[s[dex]]].erase(dex); } int main() { ios::sync_with_stdio(false), cin.tie(0); f['R'] = 0, f['P'] = 1, f['S'] = 2; for (int i = 0; i < M; i++) bad[i] = (i + 1) % M, good[i] = (i - 1 + M) % M; cin >> n >> q >> s; s = "#" + s; for (int i = 1; i <= n; i++) add_element(i); cout << solve() << '\n'; while (q--) { cin >> dex; del_element(dex); cin >> s[dex]; add_element(dex); cout << solve() << '\n'; } return 0; }
### Prompt Develop a solution in cpp to the problem described below: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100, M = 3; int n, q, dex, ans, f[N], good[M], bad[M], fen[N][M]; string s; set<int> st[M]; void add(int dex, int type, int val) { for (; dex < N; dex += (dex & -dex)) fen[dex][type] += val; } int get(int dex, int type) { return (dex > 0 ? fen[dex][type] + get(dex ^ (dex & -dex), type) : 0); } void check(int l1, int r1, int l2, int r2, int type) { int l = max(l1, l2), r = min(r1, r2); if (r >= l) ans += get(r, type) - get(l - 1, type); } int solve() { ans = 0; for (int i = 0; i < M; i++) { if (st[i].empty()) continue; else if (st[i].size() == n) { ans = n; break; } if (st[bad[i]].empty()) { ans += st[i].size(); continue; } else if (st[good[i]].empty()) continue; int a, b, A, B; a = (*st[bad[i]].begin()) - 1; auto x = st[good[i]].begin(); b = (st[good[i]].empty() ? n + 1 : max(a + 1, *x)); B = (*st[bad[i]].rbegin()) + 1; x = --st[good[i]].end(); A = (st[good[i]].empty() ? 0 : min(*x, B - 1)); check(0, a, 0, A, i); check(0, a, B, n, i); check(b, n, 0, A, i); check(b, n, B, n, i); } return ans; } void add_element(int dex) { add(dex, f[s[dex]], 1); st[f[s[dex]]].insert(dex); } void del_element(int dex) { add(dex, f[s[dex]], -1); st[f[s[dex]]].erase(dex); } int main() { ios::sync_with_stdio(false), cin.tie(0); f['R'] = 0, f['P'] = 1, f['S'] = 2; for (int i = 0; i < M; i++) bad[i] = (i + 1) % M, good[i] = (i - 1 + M) % M; cin >> n >> q >> s; s = "#" + s; for (int i = 1; i <= n; i++) add_element(i); cout << solve() << '\n'; while (q--) { cin >> dex; del_element(dex); cin >> s[dex]; add_element(dex); cout << solve() << '\n'; } return 0; } ```
#include <bits/stdc++.h> std::set<int> P[4]; int D[200005], n, q, cnt[10]; char opt[10], s[200005]; struct segmentTree { int sum[200005 << 2]; void modify(int p, int v, int l = 1, int r = n, int rt = 1) { if (l == r) { sum[rt] = v; return; } int mid = (l + r) >> 1; if (p <= mid) modify(p, v, l, mid, rt << 1); else modify(p, v, mid + 1, r, rt << 1 | 1); sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } int query(int l, int r, int L = 1, int R = n, int rt = 1) { if (l > R || r < L || l > r) return 0; if (l <= L && R <= r) return sum[rt]; return query(l, r, L, (L + R) >> 1, rt << 1) + query(l, r, ((L + R) >> 1) + 1, R, rt << 1 | 1); } } A[4]; int first(int x) { return *P[x].begin(); } int last(int x) { auto it = P[x].end(); it--; return *it; } void ins(int p, int x) { cnt[x]++; A[x].modify(p, 1); P[x].insert(p); D[p] = x; } void del(int p) { cnt[D[p]]--; A[D[p]].modify(p, 0); P[D[p]].erase(p); D[p] = 0; } void answer() { if (cnt[1] == n || cnt[2] == n || cnt[3] == n) { printf("%d\n", n); return; } if (!cnt[1]) printf("%d\n", cnt[2]); else if (!cnt[2]) printf("%d\n", cnt[3]); else if (!cnt[3]) printf("%d\n", cnt[1]); else { int dec = 0; dec += A[1].query(first(3), first(2)) + A[1].query(last(2), last(3)); dec += A[2].query(first(1), first(3)) + A[2].query(last(3), last(1)); dec += A[3].query(first(2), first(1)) + A[3].query(last(1), last(2)); printf("%d\n", n - dec); } } int main() { scanf("%d%d%s", &n, &q, s + 1); for (int i = 1; i <= n; ++i) { if (s[i] == 'R') ins(i, 1); if (s[i] == 'S') ins(i, 2); if (s[i] == 'P') ins(i, 3); } answer(); while (q--) { int p, d; scanf("%d%s", &p, opt + 1); if (opt[1] == 'R') d = 1; if (opt[1] == 'S') d = 2; if (opt[1] == 'P') d = 3; del(p); ins(p, d); answer(); } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> std::set<int> P[4]; int D[200005], n, q, cnt[10]; char opt[10], s[200005]; struct segmentTree { int sum[200005 << 2]; void modify(int p, int v, int l = 1, int r = n, int rt = 1) { if (l == r) { sum[rt] = v; return; } int mid = (l + r) >> 1; if (p <= mid) modify(p, v, l, mid, rt << 1); else modify(p, v, mid + 1, r, rt << 1 | 1); sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } int query(int l, int r, int L = 1, int R = n, int rt = 1) { if (l > R || r < L || l > r) return 0; if (l <= L && R <= r) return sum[rt]; return query(l, r, L, (L + R) >> 1, rt << 1) + query(l, r, ((L + R) >> 1) + 1, R, rt << 1 | 1); } } A[4]; int first(int x) { return *P[x].begin(); } int last(int x) { auto it = P[x].end(); it--; return *it; } void ins(int p, int x) { cnt[x]++; A[x].modify(p, 1); P[x].insert(p); D[p] = x; } void del(int p) { cnt[D[p]]--; A[D[p]].modify(p, 0); P[D[p]].erase(p); D[p] = 0; } void answer() { if (cnt[1] == n || cnt[2] == n || cnt[3] == n) { printf("%d\n", n); return; } if (!cnt[1]) printf("%d\n", cnt[2]); else if (!cnt[2]) printf("%d\n", cnt[3]); else if (!cnt[3]) printf("%d\n", cnt[1]); else { int dec = 0; dec += A[1].query(first(3), first(2)) + A[1].query(last(2), last(3)); dec += A[2].query(first(1), first(3)) + A[2].query(last(3), last(1)); dec += A[3].query(first(2), first(1)) + A[3].query(last(1), last(2)); printf("%d\n", n - dec); } } int main() { scanf("%d%d%s", &n, &q, s + 1); for (int i = 1; i <= n; ++i) { if (s[i] == 'R') ins(i, 1); if (s[i] == 'S') ins(i, 2); if (s[i] == 'P') ins(i, 3); } answer(); while (q--) { int p, d; scanf("%d%s", &p, opt + 1); if (opt[1] == 'R') d = 1; if (opt[1] == 'S') d = 2; if (opt[1] == 'P') d = 3; del(p); ins(p, d); answer(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct BIT { int s[200005]; void fix(int x, int op) { for (; x < 200005; x += x & -x) s[x] += op; } int find(int x) { int ret = 0; for (; x; x -= x & -x) ret += s[x]; return ret; } int get(int l, int r) { return find(r) - find(l - 1); } } b[3]; set<int> s[3]; int a[128], n, Q; char str[200005], opt[3]; int calc() { int ans = 0; for (int i = 0; i < 3; i++) { if (s[(i + 1) % 3].empty()) { ans += s[i].size(); continue; } if (s[(i + 2) % 3].empty()) continue; int L = *s[(i + 2) % 3].begin(), R = *(--s[(i + 2) % 3].end()); ans += b[i].get(L, R); int X = *s[(i + 1) % 3].begin(), Y = *(--s[(i + 1) % 3].end()); ans += b[i].find(min(L, X) - 1) + b[i].get(max(R, Y) + 1, n); } return ans; } int main() { a['R'] = 0; a['P'] = 1; a['S'] = 2; scanf("%d%d%s", &n, &Q, str + 1); for (int i = 1; i <= n; i++) s[a[str[i]]].insert(i), b[a[str[i]]].fix(i, 1); printf("%d\n", calc()); while (Q--) { int x; scanf("%d%s", &x, opt); s[a[str[x]]].erase(x); b[a[str[x]]].fix(x, -1); str[x] = opt[0]; s[a[str[x]]].insert(x), b[a[str[x]]].fix(x, 1); printf("%d\n", calc()); } }
### Prompt Your task is to create a Cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct BIT { int s[200005]; void fix(int x, int op) { for (; x < 200005; x += x & -x) s[x] += op; } int find(int x) { int ret = 0; for (; x; x -= x & -x) ret += s[x]; return ret; } int get(int l, int r) { return find(r) - find(l - 1); } } b[3]; set<int> s[3]; int a[128], n, Q; char str[200005], opt[3]; int calc() { int ans = 0; for (int i = 0; i < 3; i++) { if (s[(i + 1) % 3].empty()) { ans += s[i].size(); continue; } if (s[(i + 2) % 3].empty()) continue; int L = *s[(i + 2) % 3].begin(), R = *(--s[(i + 2) % 3].end()); ans += b[i].get(L, R); int X = *s[(i + 1) % 3].begin(), Y = *(--s[(i + 1) % 3].end()); ans += b[i].find(min(L, X) - 1) + b[i].get(max(R, Y) + 1, n); } return ans; } int main() { a['R'] = 0; a['P'] = 1; a['S'] = 2; scanf("%d%d%s", &n, &Q, str + 1); for (int i = 1; i <= n; i++) s[a[str[i]]].insert(i), b[a[str[i]]].fix(i, 1); printf("%d\n", calc()); while (Q--) { int x; scanf("%d%s", &x, opt); s[a[str[x]]].erase(x); b[a[str[x]]].fix(x, -1); str[x] = opt[0]; s[a[str[x]]].insert(x), b[a[str[x]]].fix(x, 1); printf("%d\n", calc()); } } ```
#include <bits/stdc++.h> using namespace std; vector<int> res; class segtree { public: struct node { int r = 0, s = 0, p = 0; void apply(char u) { r = 0, s = 0, p = 0; if (u == 'R') r = 1; if (u == 'P') p = 1; if (u == 'S') s = 1; } }; node unite(const node &a, const node &b) { node res; res.r = a.r + b.r; res.s = a.s + b.s; res.p = a.p + b.p; return res; } inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); } int n; vector<node> tree; template <typename M> void build(int x, int l, int r, const vector<M> &v) { if (l == r) { tree[x].apply(v[l]); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); build(x + 1, l, y, v); build(z, y + 1, r, v); pull(x, z); } node get(int x, int l, int r, int ll, int rr) { if (l >= ll && rr >= r) { return tree[x]; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); node res{}; if (rr <= y) { res = get(x + 1, l, y, ll, rr); } else { if (ll > y) { res = get(z, y + 1, r, ll, rr); } else { res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr)); } } return res; } node get(int l, int r) { assert(0 <= l && l <= r && r <= n - 1); return get(0, 0, n - 1, l, r); } void modify(int x, int l, int r, int p, char u) { if (l == r) { tree[x].apply(u); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); if (p <= y) { modify(x + 1, l, y, p, u); } else { modify(z, y + 1, r, p, u); } pull(x, z); } template <typename M> segtree(const vector<M> &v) { n = v.size(); assert(n > 0); tree.resize(2 * n - 1); build(0, 0, n - 1, v); } }; set<int> rock, paper, scissor; int n, q; string tour; vector<char> har; int main() { cin >> n >> q; cin >> tour; for (int i = 0; i < n; i++) har.push_back(tour[i]); for (int i = 0; i < n; i++) { if (tour[i] == 'R') rock.insert(i); if (tour[i] == 'P') paper.insert(i); if (tour[i] == 'S') scissor.insert(i); } segtree rev(har); auto calc = [&]() { int res = 0; { if (!scissor.size() && !paper.size()) res += n; if (scissor.size() > 0) { if (paper.size() > 0) { int u = *scissor.begin(), v = *scissor.rbegin(), s = *paper.begin(), t = *paper.rbegin(); res += rev.get(0, min(u, s)).r + rev.get(max(v, t), n - 1).r + rev.get(u, v).r; } else res += n - scissor.size(); } } { if (!paper.size() && !rock.size()) res += n; if (paper.size() > 0) { if (rock.size() > 0) { int u = *paper.begin(), v = *paper.rbegin(), s = *rock.begin(), t = *rock.rbegin(); res += rev.get(0, min(u, s)).s + rev.get(max(v, t), n - 1).s + rev.get(u, v).s; } else res += n - paper.size(); } } { if (!rock.size() && !scissor.size()) res += n; if (rock.size() > 0) { if (scissor.size() > 0) { int u = *rock.begin(), v = *rock.rbegin(), s = *scissor.begin(), t = *scissor.rbegin(); res += rev.get(0, min(u, s)).p + rev.get(max(v, t), n - 1).p + rev.get(u, v).p; } else res += n - rock.size(); } } return res; }; res.push_back(calc()); while (q--) { int i; char u; cin >> i >> u; i--; if (tour[i] == 'R') rock.erase(i); if (tour[i] == 'P') paper.erase(i); if (tour[i] == 'S') scissor.erase(i); tour[i] = u; rev.modify(0, 0, n - 1, i, u); if (u == 'R') rock.insert(i); if (u == 'P') paper.insert(i); if (u == 'S') scissor.insert(i); res.push_back(calc()); } for (auto i : res) cout << i << endl; return 0; }
### Prompt Please formulate a CPP solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> res; class segtree { public: struct node { int r = 0, s = 0, p = 0; void apply(char u) { r = 0, s = 0, p = 0; if (u == 'R') r = 1; if (u == 'P') p = 1; if (u == 'S') s = 1; } }; node unite(const node &a, const node &b) { node res; res.r = a.r + b.r; res.s = a.s + b.s; res.p = a.p + b.p; return res; } inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); } int n; vector<node> tree; template <typename M> void build(int x, int l, int r, const vector<M> &v) { if (l == r) { tree[x].apply(v[l]); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); build(x + 1, l, y, v); build(z, y + 1, r, v); pull(x, z); } node get(int x, int l, int r, int ll, int rr) { if (l >= ll && rr >= r) { return tree[x]; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); node res{}; if (rr <= y) { res = get(x + 1, l, y, ll, rr); } else { if (ll > y) { res = get(z, y + 1, r, ll, rr); } else { res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr)); } } return res; } node get(int l, int r) { assert(0 <= l && l <= r && r <= n - 1); return get(0, 0, n - 1, l, r); } void modify(int x, int l, int r, int p, char u) { if (l == r) { tree[x].apply(u); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); if (p <= y) { modify(x + 1, l, y, p, u); } else { modify(z, y + 1, r, p, u); } pull(x, z); } template <typename M> segtree(const vector<M> &v) { n = v.size(); assert(n > 0); tree.resize(2 * n - 1); build(0, 0, n - 1, v); } }; set<int> rock, paper, scissor; int n, q; string tour; vector<char> har; int main() { cin >> n >> q; cin >> tour; for (int i = 0; i < n; i++) har.push_back(tour[i]); for (int i = 0; i < n; i++) { if (tour[i] == 'R') rock.insert(i); if (tour[i] == 'P') paper.insert(i); if (tour[i] == 'S') scissor.insert(i); } segtree rev(har); auto calc = [&]() { int res = 0; { if (!scissor.size() && !paper.size()) res += n; if (scissor.size() > 0) { if (paper.size() > 0) { int u = *scissor.begin(), v = *scissor.rbegin(), s = *paper.begin(), t = *paper.rbegin(); res += rev.get(0, min(u, s)).r + rev.get(max(v, t), n - 1).r + rev.get(u, v).r; } else res += n - scissor.size(); } } { if (!paper.size() && !rock.size()) res += n; if (paper.size() > 0) { if (rock.size() > 0) { int u = *paper.begin(), v = *paper.rbegin(), s = *rock.begin(), t = *rock.rbegin(); res += rev.get(0, min(u, s)).s + rev.get(max(v, t), n - 1).s + rev.get(u, v).s; } else res += n - paper.size(); } } { if (!rock.size() && !scissor.size()) res += n; if (rock.size() > 0) { if (scissor.size() > 0) { int u = *rock.begin(), v = *rock.rbegin(), s = *scissor.begin(), t = *scissor.rbegin(); res += rev.get(0, min(u, s)).p + rev.get(max(v, t), n - 1).p + rev.get(u, v).p; } else res += n - rock.size(); } } return res; }; res.push_back(calc()); while (q--) { int i; char u; cin >> i >> u; i--; if (tour[i] == 'R') rock.erase(i); if (tour[i] == 'P') paper.erase(i); if (tour[i] == 'S') scissor.erase(i); tour[i] = u; rev.modify(0, 0, n - 1, i, u); if (u == 'R') rock.insert(i); if (u == 'P') paper.insert(i); if (u == 'S') scissor.insert(i); res.push_back(calc()); } for (auto i : res) cout << i << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int _ = 2e5 + 3; int N, Q; map<char, int> to; char str[_]; struct BIT { int arr[_]; void add(int x, int num) { while (x <= N) { arr[x] += num; x += ((x) & -(x)); } } int qry(int x) { int sum = 0; while (x) { sum += arr[x]; x -= ((x) & -(x)); } return sum; } } val[3]; set<int> plc[3]; int calc() { int ans = 0; for (int i = 0; i < 3; ++i) if (plc[(i + 2) % 3].empty()) ans += plc[i].size(); else if (!plc[(i + 1) % 3].empty()) { int pb = *plc[(i + 1) % 3].begin(), pe = *--plc[(i + 1) % 3].end(); int qb = *plc[(i + 2) % 3].begin(), qe = *--plc[(i + 2) % 3].end(); ans += plc[i].size() - (pb > qb ? val[i].qry(pb) - val[i].qry(qb) : 0) - (pe < qe ? val[i].qry(qe) - val[i].qry(pe) : 0); } return ans; } int main() { to['R'] = 0; to['S'] = 1; to['P'] = 2; scanf("%d %d", &N, &Q); scanf("%s", str + 1); for (int i = 1; i <= N; ++i) { plc[to[str[i]]].insert(i); val[to[str[i]]].add(i, 1); } printf("%d\n", calc()); while (Q--) { int x; scanf("%d", &x); char c = getchar(); while (!isupper(c)) c = getchar(); plc[to[str[x]]].erase(x); val[to[str[x]]].add(x, -1); plc[to[str[x] = c]].insert(x); val[to[c]].add(x, 1); printf("%d\n", calc()); } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int _ = 2e5 + 3; int N, Q; map<char, int> to; char str[_]; struct BIT { int arr[_]; void add(int x, int num) { while (x <= N) { arr[x] += num; x += ((x) & -(x)); } } int qry(int x) { int sum = 0; while (x) { sum += arr[x]; x -= ((x) & -(x)); } return sum; } } val[3]; set<int> plc[3]; int calc() { int ans = 0; for (int i = 0; i < 3; ++i) if (plc[(i + 2) % 3].empty()) ans += plc[i].size(); else if (!plc[(i + 1) % 3].empty()) { int pb = *plc[(i + 1) % 3].begin(), pe = *--plc[(i + 1) % 3].end(); int qb = *plc[(i + 2) % 3].begin(), qe = *--plc[(i + 2) % 3].end(); ans += plc[i].size() - (pb > qb ? val[i].qry(pb) - val[i].qry(qb) : 0) - (pe < qe ? val[i].qry(qe) - val[i].qry(pe) : 0); } return ans; } int main() { to['R'] = 0; to['S'] = 1; to['P'] = 2; scanf("%d %d", &N, &Q); scanf("%s", str + 1); for (int i = 1; i <= N; ++i) { plc[to[str[i]]].insert(i); val[to[str[i]]].add(i, 1); } printf("%d\n", calc()); while (Q--) { int x; scanf("%d", &x); char c = getchar(); while (!isupper(c)) c = getchar(); plc[to[str[x]]].erase(x); val[to[str[x]]].add(x, -1); plc[to[str[x] = c]].insert(x); val[to[c]].add(x, 1); printf("%d\n", calc()); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, q, u; char c, s[N]; struct STree { int tr[4 * N]; void update(int l, int r, int i, int u, int v) { tr[i] += v; if (l == r) return; if (u <= (l + r) / 2) update(l, (l + r) / 2, i * 2, u, v); else update((l + r) / 2 + 1, r, i * 2 + 1, u, v); } int query(int l, int r, int i, int L, int R) { if (l > R || r < L || L > R) return 0; if (L <= l && r <= R) return tr[i]; return query(l, (l + r) / 2, i * 2, L, R) + query((l + r) / 2 + 1, r, i * 2 + 1, L, R); } int fpos(int l, int r, int i, bool fi = true) { if (l == r) return l; if (fi) return (tr[i * 2] >= 1 ? fpos(l, (l + r) / 2, i * 2, fi) : fpos((l + r) / 2 + 1, r, i * 2 + 1, fi)); else return (tr[i * 2 + 1] >= 1 ? fpos((l + r) / 2 + 1, r, i * 2 + 1, fi) : fpos(l, (l + r) / 2, i * 2, fi)); } int lpos(int l, int r, int i, bool fi = true) { if (l == r) return fi ? l - 1 : n - l; if (fi) return (tr[i * 2] == (l + r) / 2 - l + 1 ? lpos((l + r) / 2 + 1, r, i * 2 + 1, fi) : lpos(l, (l + r) / 2, i * 2, fi)); else return (tr[i * 2 + 1] == r - (l + r) / 2 ? lpos(l, (l + r) / 2, i * 2, fi) : lpos((l + r) / 2 + 1, r, i * 2 + 1, fi)); } } seg[3]; int conv(char c) { if (c == 'R') return 0; else if (c == 'S') return 1; else return 2; } int query(int typ) { int nxt = (typ + 1) % 3; int l = seg[nxt].fpos(1, n, 1, true), r = seg[nxt].fpos(1, n, 1, false); if (l > r) return (seg[typ].query(1, n, 1, 1, n) == n ? n : 0); else return seg[typ].query(1, n, 1, l, r) + seg[typ].lpos(1, n, 1, true) + seg[typ].lpos(1, n, 1, false); } int find_ans() { int ans = 0; for (int i = 0; i < 3; i++) ans += query(i); return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> q >> (s + 1); for (int i = 1; i <= n; i++) seg[conv(s[i])].update(1, n, 1, i, 1); cout << find_ans() << '\n'; while (q--) { cin >> u >> c; seg[conv(s[u])].update(1, n, 1, u, -1); s[u] = c; seg[conv(s[u])].update(1, n, 1, u, 1); cout << find_ans() << '\n'; } }
### Prompt In CPP, your task is to solve the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 200005; int n, q, u; char c, s[N]; struct STree { int tr[4 * N]; void update(int l, int r, int i, int u, int v) { tr[i] += v; if (l == r) return; if (u <= (l + r) / 2) update(l, (l + r) / 2, i * 2, u, v); else update((l + r) / 2 + 1, r, i * 2 + 1, u, v); } int query(int l, int r, int i, int L, int R) { if (l > R || r < L || L > R) return 0; if (L <= l && r <= R) return tr[i]; return query(l, (l + r) / 2, i * 2, L, R) + query((l + r) / 2 + 1, r, i * 2 + 1, L, R); } int fpos(int l, int r, int i, bool fi = true) { if (l == r) return l; if (fi) return (tr[i * 2] >= 1 ? fpos(l, (l + r) / 2, i * 2, fi) : fpos((l + r) / 2 + 1, r, i * 2 + 1, fi)); else return (tr[i * 2 + 1] >= 1 ? fpos((l + r) / 2 + 1, r, i * 2 + 1, fi) : fpos(l, (l + r) / 2, i * 2, fi)); } int lpos(int l, int r, int i, bool fi = true) { if (l == r) return fi ? l - 1 : n - l; if (fi) return (tr[i * 2] == (l + r) / 2 - l + 1 ? lpos((l + r) / 2 + 1, r, i * 2 + 1, fi) : lpos(l, (l + r) / 2, i * 2, fi)); else return (tr[i * 2 + 1] == r - (l + r) / 2 ? lpos(l, (l + r) / 2, i * 2, fi) : lpos((l + r) / 2 + 1, r, i * 2 + 1, fi)); } } seg[3]; int conv(char c) { if (c == 'R') return 0; else if (c == 'S') return 1; else return 2; } int query(int typ) { int nxt = (typ + 1) % 3; int l = seg[nxt].fpos(1, n, 1, true), r = seg[nxt].fpos(1, n, 1, false); if (l > r) return (seg[typ].query(1, n, 1, 1, n) == n ? n : 0); else return seg[typ].query(1, n, 1, l, r) + seg[typ].lpos(1, n, 1, true) + seg[typ].lpos(1, n, 1, false); } int find_ans() { int ans = 0; for (int i = 0; i < 3; i++) ans += query(i); return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> q >> (s + 1); for (int i = 1; i <= n; i++) seg[conv(s[i])].update(1, n, 1, i, 1); cout << find_ans() << '\n'; while (q--) { cin >> u >> c; seg[conv(s[u])].update(1, n, 1, u, -1); s[u] = c; seg[conv(s[u])].update(1, n, 1, u, 1); cout << find_ans() << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; struct BIT { int n; vector<int> bit; BIT(int _n = 0) { n = _n; bit.assign(n + 5, 0); } void update(int pos, int val) { for (; pos <= n; pos += pos & -pos) bit[pos] += val; } int getsum(int pos) { int res = 0; for (; pos; pos -= pos & -pos) res += bit[pos]; return res; } }; BIT bit[3]; set<int> pos[3]; int a[N]; int n, q; int fix(char c) { if (c == 'R') return 0; if (c == 'S') return 1; return 2; } int getans() { int res = 0; for (int x = 0; x < 3; x++) if (pos[x].size()) { int y = (x + 1) % 3; int z = (x + 2) % 3; if (!pos[y].size()) res += n * (!pos[z].size()); else { res += pos[x].size(); if (pos[z].size()) { int l = *pos[y].begin(); int r = *prev(pos[y].end()); int u = *pos[z].begin(); int v = *prev(pos[z].end()); if (r < v) res -= bit[x].getsum(v) - bit[x].getsum(r - 1); if (u < l) res -= bit[x].getsum(l) - bit[x].getsum(u - 1); } } } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q; for (int i = 0; i < 3; i++) bit[i] = BIT(n); for (int i = 1; i <= n; i++) { char c; cin >> c; a[i] = fix(c); pos[a[i]].insert(i); bit[a[i]].update(i, 1); } cout << getans() << "\n"; while (q--) { int i; char c; cin >> i >> c; pos[a[i]].erase(i); bit[a[i]].update(i, -1); a[i] = fix(c); pos[a[i]].insert(i); bit[a[i]].update(i, 1); cout << getans() << "\n"; } return 0; }
### Prompt Please create a solution in cpp to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; struct BIT { int n; vector<int> bit; BIT(int _n = 0) { n = _n; bit.assign(n + 5, 0); } void update(int pos, int val) { for (; pos <= n; pos += pos & -pos) bit[pos] += val; } int getsum(int pos) { int res = 0; for (; pos; pos -= pos & -pos) res += bit[pos]; return res; } }; BIT bit[3]; set<int> pos[3]; int a[N]; int n, q; int fix(char c) { if (c == 'R') return 0; if (c == 'S') return 1; return 2; } int getans() { int res = 0; for (int x = 0; x < 3; x++) if (pos[x].size()) { int y = (x + 1) % 3; int z = (x + 2) % 3; if (!pos[y].size()) res += n * (!pos[z].size()); else { res += pos[x].size(); if (pos[z].size()) { int l = *pos[y].begin(); int r = *prev(pos[y].end()); int u = *pos[z].begin(); int v = *prev(pos[z].end()); if (r < v) res -= bit[x].getsum(v) - bit[x].getsum(r - 1); if (u < l) res -= bit[x].getsum(l) - bit[x].getsum(u - 1); } } } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q; for (int i = 0; i < 3; i++) bit[i] = BIT(n); for (int i = 1; i <= n; i++) { char c; cin >> c; a[i] = fix(c); pos[a[i]].insert(i); bit[a[i]].update(i, 1); } cout << getans() << "\n"; while (q--) { int i; char c; cin >> i >> c; pos[a[i]].erase(i); bit[a[i]].update(i, -1); a[i] = fix(c); pos[a[i]].insert(i); bit[a[i]].update(i, 1); cout << getans() << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; char mps[255]; set<int> pos[3]; int SEGTREE[3][200005 * 4]; char s[200005]; void update(int *seg, int pos, int l, int r, int ind, int val) { if (l == r) { seg[pos] += val; return; } int mid = (l + r) / 2; if (ind <= mid) update(seg, pos * 2, l, mid, ind, val); else update(seg, pos * 2 + 1, mid + 1, r, ind, val); seg[pos] = seg[pos * 2] + seg[pos * 2 + 1]; } int query(int *seg, int pos, int l, int r, int ql, int qr) { if (r < ql || qr < l) { return 0; } if (ql <= l && r <= qr) { return seg[pos]; } int mid = (l + r) / 2; return query(seg, pos * 2, l, mid, ql, qr) + query(seg, pos * 2 + 1, mid + 1, r, ql, qr); } int getQuery(int id, int n) { int opId = (id + 1) % 3, weakID = (id + 2) % 3; if (pos[opId].size() == 0) { return pos[id].size(); } if (pos[weakID].size() == 0 || pos[id].size() == 0) { return 0; } int ret = 0; int firstEnemy = *pos[opId].begin(), lastEnemy = *prev(pos[opId].end()); int firstWeak = *pos[weakID].begin(), lastWeak = *prev(pos[weakID].end()); int index; if (firstEnemy <= lastWeak) { index = firstEnemy; } else { index = lastWeak; } ret += query(SEGTREE[id], 1, 1, n, 1, index); int l = max(firstEnemy, firstWeak); int r = min(lastEnemy, lastWeak); if (l < r) { ret += query(SEGTREE[id], 1, 1, n, l, r); } if (firstWeak <= lastEnemy) { index = lastEnemy; } else { index = firstWeak; } ret += query(SEGTREE[id], 1, 1, n, index, n); return ret; } int getTotal(int n) { return getQuery(0, n) + getQuery(1, n) + getQuery(2, n); } void solve() { int n, q; scanf("%d %d ", &n, &q); mps['R'] = 0; mps['P'] = 1; mps['S'] = 2; scanf(" %s ", s + 1); for (int i = 1; i <= n; ++i) { int id = mps[s[i]]; pos[id].insert(i); update(SEGTREE[id], 1, 1, n, i, 1); } printf("%d\n", getTotal(n)); char t; while (q--) { int index; int id; scanf("%d %c ", &index, &t); id = mps[s[index]]; update(SEGTREE[id], 1, 1, n, index, -1); pos[id].erase(index); s[index] = t; id = mps[s[index]]; update(SEGTREE[id], 1, 1, n, index, 1); pos[id].insert(index); printf("%d\n", getTotal(n)); } } int main() { solve(); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; char mps[255]; set<int> pos[3]; int SEGTREE[3][200005 * 4]; char s[200005]; void update(int *seg, int pos, int l, int r, int ind, int val) { if (l == r) { seg[pos] += val; return; } int mid = (l + r) / 2; if (ind <= mid) update(seg, pos * 2, l, mid, ind, val); else update(seg, pos * 2 + 1, mid + 1, r, ind, val); seg[pos] = seg[pos * 2] + seg[pos * 2 + 1]; } int query(int *seg, int pos, int l, int r, int ql, int qr) { if (r < ql || qr < l) { return 0; } if (ql <= l && r <= qr) { return seg[pos]; } int mid = (l + r) / 2; return query(seg, pos * 2, l, mid, ql, qr) + query(seg, pos * 2 + 1, mid + 1, r, ql, qr); } int getQuery(int id, int n) { int opId = (id + 1) % 3, weakID = (id + 2) % 3; if (pos[opId].size() == 0) { return pos[id].size(); } if (pos[weakID].size() == 0 || pos[id].size() == 0) { return 0; } int ret = 0; int firstEnemy = *pos[opId].begin(), lastEnemy = *prev(pos[opId].end()); int firstWeak = *pos[weakID].begin(), lastWeak = *prev(pos[weakID].end()); int index; if (firstEnemy <= lastWeak) { index = firstEnemy; } else { index = lastWeak; } ret += query(SEGTREE[id], 1, 1, n, 1, index); int l = max(firstEnemy, firstWeak); int r = min(lastEnemy, lastWeak); if (l < r) { ret += query(SEGTREE[id], 1, 1, n, l, r); } if (firstWeak <= lastEnemy) { index = lastEnemy; } else { index = firstWeak; } ret += query(SEGTREE[id], 1, 1, n, index, n); return ret; } int getTotal(int n) { return getQuery(0, n) + getQuery(1, n) + getQuery(2, n); } void solve() { int n, q; scanf("%d %d ", &n, &q); mps['R'] = 0; mps['P'] = 1; mps['S'] = 2; scanf(" %s ", s + 1); for (int i = 1; i <= n; ++i) { int id = mps[s[i]]; pos[id].insert(i); update(SEGTREE[id], 1, 1, n, i, 1); } printf("%d\n", getTotal(n)); char t; while (q--) { int index; int id; scanf("%d %c ", &index, &t); id = mps[s[index]]; update(SEGTREE[id], 1, 1, n, index, -1); pos[id].erase(index); s[index] = t; id = mps[s[index]]; update(SEGTREE[id], 1, 1, n, index, 1); pos[id].insert(index); printf("%d\n", getTotal(n)); } } int main() { solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e6 + 10; int n, q; string second; set<int> pos[3]; int a[MAXN][3]; map<char, int> m; int get(int id, int p) { int res = 0; while (p >= 0) { res += a[p][id]; p = (p & (p + 1)) - 1; } return res; } pair<int, int> intr(pair<int, int> x, pair<int, int> y) { return {max(x.first, y.first), min(x.second, y.second)}; } int calc(int id) { int good = (id + 1) % 3, bad = (id + 2) % 3; vector<pair<int, int> > fi, se; if (pos[bad].size() == 0) { return pos[id].size(); } fi.push_back({0, *pos[bad].begin() - 1}); se.push_back({*pos[bad].rbegin() + 1, n - 1}); if (pos[good].size() > 0) { fi.push_back({*pos[good].begin() + 1, n - 1}); se.push_back({0, *pos[good].rbegin() - 1}); } vector<pair<int, int> > v; for (int i = 0; i < fi.size(); i++) { for (int j = 0; j < se.size(); j++) { pair<int, int> cur = intr(fi[i], se[j]); if (cur.first <= cur.second) v.push_back(cur); } } sort(v.begin(), v.end()); int sum = 0, maxr = -1; for (int i = 0; i < v.size(); i++) { int from = max(v[i].first, maxr + 1), to = v[i].second; if (from <= to) { maxr = max(maxr, to); sum += get(id, to) - get(id, from - 1); } } return sum; } int solve() { int res = 0; for (int i = 0; i < 3; i++) { res += calc(i); } return res; } void upd(int id, int p, int add) { while (p < n) { a[p][id] += add; p |= (p + 1); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q >> second; m['R'] = 0; m['S'] = 1; m['P'] = 2; for (int i = 0; i < n; i++) { upd(m[second[i]], i, 1); pos[m[second[i]]].insert(i); } cout << solve() << '\n'; while (q--) { int p; char c; cin >> p >> c; p--; pos[m[second[p]]].erase(p); upd(m[second[p]], p, -1); pos[m[c]].insert(p); upd(m[c], p, 1); second[p] = c; cout << solve() << '\n'; } return 0; }
### Prompt Generate a CPP solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MAXN = 1e6 + 10; int n, q; string second; set<int> pos[3]; int a[MAXN][3]; map<char, int> m; int get(int id, int p) { int res = 0; while (p >= 0) { res += a[p][id]; p = (p & (p + 1)) - 1; } return res; } pair<int, int> intr(pair<int, int> x, pair<int, int> y) { return {max(x.first, y.first), min(x.second, y.second)}; } int calc(int id) { int good = (id + 1) % 3, bad = (id + 2) % 3; vector<pair<int, int> > fi, se; if (pos[bad].size() == 0) { return pos[id].size(); } fi.push_back({0, *pos[bad].begin() - 1}); se.push_back({*pos[bad].rbegin() + 1, n - 1}); if (pos[good].size() > 0) { fi.push_back({*pos[good].begin() + 1, n - 1}); se.push_back({0, *pos[good].rbegin() - 1}); } vector<pair<int, int> > v; for (int i = 0; i < fi.size(); i++) { for (int j = 0; j < se.size(); j++) { pair<int, int> cur = intr(fi[i], se[j]); if (cur.first <= cur.second) v.push_back(cur); } } sort(v.begin(), v.end()); int sum = 0, maxr = -1; for (int i = 0; i < v.size(); i++) { int from = max(v[i].first, maxr + 1), to = v[i].second; if (from <= to) { maxr = max(maxr, to); sum += get(id, to) - get(id, from - 1); } } return sum; } int solve() { int res = 0; for (int i = 0; i < 3; i++) { res += calc(i); } return res; } void upd(int id, int p, int add) { while (p < n) { a[p][id] += add; p |= (p + 1); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q >> second; m['R'] = 0; m['S'] = 1; m['P'] = 2; for (int i = 0; i < n; i++) { upd(m[second[i]], i, 1); pos[m[second[i]]].insert(i); } cout << solve() << '\n'; while (q--) { int p; char c; cin >> p >> c; p--; pos[m[second[p]]].erase(p); upd(m[second[p]], p, -1); pos[m[c]].insert(p); upd(m[c], p, 1); second[p] = c; cout << solve() << '\n'; } return 0; } ```
#include <bits/stdc++.h> using i64 = long long; int readToken() { char x; std::cin >> x; if (x == 'R') { return 0; } else if (x == 'P') { return 1; } else { return 2; } } template <typename T> struct Fenwick { const int n; std::vector<T> a; Fenwick(int n) : n(n), a(n) {} void add(int x, T v) { for (int i = x + 1; i <= n; i += i & -i) { a[i - 1] += v; } } T sum(int x) { T ans = 0; for (int i = x; i > 0; i -= i & -i) { ans += a[i - 1]; } return ans; } T rangeSum(int l, int r) { return sum(r) - sum(l); } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, q; std::cin >> n >> q; std::set<int> s[3]; std::vector<Fenwick<int>> fen(3, n); std::vector<int> a(n); for (int i = 0; i < n; i++) { a[i] = readToken(); fen[a[i]].add(i, 1); s[a[i]].insert(i); } auto answer = [&]() { int ans = 0; for (int i = 0; i < 3; i++) { if (s[(i + 1) % 3].empty()) { ans += fen[i].sum(n); continue; } if (s[(i + 2) % 3].empty()) { continue; } ans += fen[i].sum(n); int x = *s[(i + 1) % 3].begin(), y = *s[(i + 2) % 3].begin(); if (x < y) { ans -= fen[i].rangeSum(x, y); } x = *s[(i + 1) % 3].rbegin(); y = *s[(i + 2) % 3].rbegin(); if (x > y) { ans -= fen[i].rangeSum(y, x); } } std::cout << ans << "\n"; }; answer(); while (q--) { int p; std::cin >> p; p--; int x = readToken(); fen[a[p]].add(p, -1); s[a[p]].erase(p); a[p] = x; fen[x].add(p, 1); s[x].insert(p); answer(); } return 0; }
### Prompt Create a solution in CPP for the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using i64 = long long; int readToken() { char x; std::cin >> x; if (x == 'R') { return 0; } else if (x == 'P') { return 1; } else { return 2; } } template <typename T> struct Fenwick { const int n; std::vector<T> a; Fenwick(int n) : n(n), a(n) {} void add(int x, T v) { for (int i = x + 1; i <= n; i += i & -i) { a[i - 1] += v; } } T sum(int x) { T ans = 0; for (int i = x; i > 0; i -= i & -i) { ans += a[i - 1]; } return ans; } T rangeSum(int l, int r) { return sum(r) - sum(l); } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, q; std::cin >> n >> q; std::set<int> s[3]; std::vector<Fenwick<int>> fen(3, n); std::vector<int> a(n); for (int i = 0; i < n; i++) { a[i] = readToken(); fen[a[i]].add(i, 1); s[a[i]].insert(i); } auto answer = [&]() { int ans = 0; for (int i = 0; i < 3; i++) { if (s[(i + 1) % 3].empty()) { ans += fen[i].sum(n); continue; } if (s[(i + 2) % 3].empty()) { continue; } ans += fen[i].sum(n); int x = *s[(i + 1) % 3].begin(), y = *s[(i + 2) % 3].begin(); if (x < y) { ans -= fen[i].rangeSum(x, y); } x = *s[(i + 1) % 3].rbegin(); y = *s[(i + 2) % 3].rbegin(); if (x > y) { ans -= fen[i].rangeSum(y, x); } } std::cout << ans << "\n"; }; answer(); while (q--) { int p; std::cin >> p; p--; int x = readToken(); fen[a[p]].add(p, -1); s[a[p]].erase(p); a[p] = x; fen[x].add(p, 1); s[x].insert(p); answer(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2E5 + 10, M = 3; const int inf = 1 << 30; long long read() { long long a = 0; char b = 1, c; do if ((c = getchar()) == 45) b = -1; while (c < 48 || c > 57); do a = (a << 3) + (a << 1) + (c & 15); while ((c = getchar()) > 47 && c < 58); return a * b; } void write(long long x, char c) { if (x < 0) putchar(45), x = -x; char a[20], s = 0; do a[++s] = x % 10 | 48; while (x /= 10); do putchar(a[s]); while (--s); putchar(c); } char c[N], a[N], val[N]; int n, m, pos[N], ans[N], q[M][N]; bool b; void push(char op, int x, int val) { if (!x) return; while (x <= n) { q[op][x] += val; x += x & -x; } } int ask(int op) { int res = 0; for (int i = 1 << 18; i >>= 1;) { int t = res + i; if (t <= n && !q[op][t]) res = t; } return res + 1; } int sum(char op, int l, int r) { int res = 0; while (l) { res -= q[op][l]; l -= l & -l; } --r; while (r) { res += q[op][r]; r -= r & -r; } return res; } void solve() { memcpy(a + 1, c + 1, n); for (int i = 1; i <= n; ++i) push(a[i], i, 1); for (int i = 0; i <= m; ++i) { push(a[pos[i]], pos[i], -1); push(val[i], pos[i], 1); a[pos[i]] = val[i]; for (int j = 0, k = 2; j < M; ++j, ++k %= M) { int l = ask(j), r = ask(k); if (l >= r) continue; if (r <= n) ans[i] += sum(j ^ k ^ M, l, r); else if (b) ans[i] += sum(j ^ k ^ M, 0, r); } } } int main() { n = read(), m = read(); scanf("%s", c + 1); for (int i = 1; i <= n; ++i) c[i] = c[i] - 79 >> 1; for (int i = 1; i <= m; ++i) { pos[i] = read(); do val[i] = getchar(); while (val[i] < 80 || val[i] > 83); val[i] = val[i] - 79 >> 1; } solve(); reverse(c + 1, c + 1 + n); for (int i = 1; i <= m; ++i) pos[i] = n + 1 - pos[i]; for (int i = 0; i < M; ++i) memset(q[i] + 1, 0, n << 2); b = 1; solve(); for (int i = 0; i <= m; ++i) write(n - ans[i], '\n'); }
### Prompt Your challenge is to write a CPP solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2E5 + 10, M = 3; const int inf = 1 << 30; long long read() { long long a = 0; char b = 1, c; do if ((c = getchar()) == 45) b = -1; while (c < 48 || c > 57); do a = (a << 3) + (a << 1) + (c & 15); while ((c = getchar()) > 47 && c < 58); return a * b; } void write(long long x, char c) { if (x < 0) putchar(45), x = -x; char a[20], s = 0; do a[++s] = x % 10 | 48; while (x /= 10); do putchar(a[s]); while (--s); putchar(c); } char c[N], a[N], val[N]; int n, m, pos[N], ans[N], q[M][N]; bool b; void push(char op, int x, int val) { if (!x) return; while (x <= n) { q[op][x] += val; x += x & -x; } } int ask(int op) { int res = 0; for (int i = 1 << 18; i >>= 1;) { int t = res + i; if (t <= n && !q[op][t]) res = t; } return res + 1; } int sum(char op, int l, int r) { int res = 0; while (l) { res -= q[op][l]; l -= l & -l; } --r; while (r) { res += q[op][r]; r -= r & -r; } return res; } void solve() { memcpy(a + 1, c + 1, n); for (int i = 1; i <= n; ++i) push(a[i], i, 1); for (int i = 0; i <= m; ++i) { push(a[pos[i]], pos[i], -1); push(val[i], pos[i], 1); a[pos[i]] = val[i]; for (int j = 0, k = 2; j < M; ++j, ++k %= M) { int l = ask(j), r = ask(k); if (l >= r) continue; if (r <= n) ans[i] += sum(j ^ k ^ M, l, r); else if (b) ans[i] += sum(j ^ k ^ M, 0, r); } } } int main() { n = read(), m = read(); scanf("%s", c + 1); for (int i = 1; i <= n; ++i) c[i] = c[i] - 79 >> 1; for (int i = 1; i <= m; ++i) { pos[i] = read(); do val[i] = getchar(); while (val[i] < 80 || val[i] > 83); val[i] = val[i] - 79 >> 1; } solve(); reverse(c + 1, c + 1 + n); for (int i = 1; i <= m; ++i) pos[i] = n + 1 - pos[i]; for (int i = 0; i < M; ++i) memset(q[i] + 1, 0, n << 2); b = 1; solve(); for (int i = 0; i <= m; ++i) write(n - ans[i], '\n'); } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100, M = 3; int n, q, dex, ans, f[N], good[M], bad[M], fen[N][M]; string s; set<int> st[M]; inline void add(int dex, int type, int val) { for (; dex < N; dex += (dex & -dex)) fen[dex][type] += val; } inline int get(int dex, int type) { return (dex > 0 ? fen[dex][type] + get(dex ^ (dex & -dex), type) : 0); } inline void check(int l1, int r1, int l2, int r2, int type) { int l = max(l1, l2), r = min(r1, r2); if (r >= l) ans += get(r, type) - get(l - 1, type); } inline int solve() { ans = 0; for (int i = 0; i < M; i++) { if (st[i].empty()) continue; else if (st[i].size() == n) { ans = n; break; } if (st[bad[i]].empty()) { ans += st[i].size(); continue; } else if (st[good[i]].empty()) continue; int a, b, A, B; a = (*st[bad[i]].begin()) - 1; auto x = st[good[i]].begin(); b = (st[good[i]].empty() ? n + 1 : max(a + 1, *x)); B = (*st[bad[i]].rbegin()) + 1; x = --st[good[i]].end(); A = (st[good[i]].empty() ? 0 : min(*x, B - 1)); check(0, a, 0, A, i); check(0, a, B, n, i); check(b, n, 0, A, i); check(b, n, B, n, i); } return ans; } inline void add_element(int dex) { add(dex, f[s[dex]], 1); st[f[s[dex]]].insert(dex); } inline void del_element(int dex) { add(dex, f[s[dex]], -1); st[f[s[dex]]].erase(dex); } int main() { ios::sync_with_stdio(false), cin.tie(0); f['R'] = 0, f['P'] = 1, f['S'] = 2; for (int i = 0; i < M; i++) bad[i] = (i + 1) % M, good[i] = (i - 1 + M) % M; cin >> n >> q >> s; s = "#" + s; for (int i = 1; i <= n; i++) add_element(i); cout << solve() << '\n'; while (q--) { cin >> dex; del_element(dex); cin >> s[dex]; add_element(dex); cout << solve() << '\n'; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100, M = 3; int n, q, dex, ans, f[N], good[M], bad[M], fen[N][M]; string s; set<int> st[M]; inline void add(int dex, int type, int val) { for (; dex < N; dex += (dex & -dex)) fen[dex][type] += val; } inline int get(int dex, int type) { return (dex > 0 ? fen[dex][type] + get(dex ^ (dex & -dex), type) : 0); } inline void check(int l1, int r1, int l2, int r2, int type) { int l = max(l1, l2), r = min(r1, r2); if (r >= l) ans += get(r, type) - get(l - 1, type); } inline int solve() { ans = 0; for (int i = 0; i < M; i++) { if (st[i].empty()) continue; else if (st[i].size() == n) { ans = n; break; } if (st[bad[i]].empty()) { ans += st[i].size(); continue; } else if (st[good[i]].empty()) continue; int a, b, A, B; a = (*st[bad[i]].begin()) - 1; auto x = st[good[i]].begin(); b = (st[good[i]].empty() ? n + 1 : max(a + 1, *x)); B = (*st[bad[i]].rbegin()) + 1; x = --st[good[i]].end(); A = (st[good[i]].empty() ? 0 : min(*x, B - 1)); check(0, a, 0, A, i); check(0, a, B, n, i); check(b, n, 0, A, i); check(b, n, B, n, i); } return ans; } inline void add_element(int dex) { add(dex, f[s[dex]], 1); st[f[s[dex]]].insert(dex); } inline void del_element(int dex) { add(dex, f[s[dex]], -1); st[f[s[dex]]].erase(dex); } int main() { ios::sync_with_stdio(false), cin.tie(0); f['R'] = 0, f['P'] = 1, f['S'] = 2; for (int i = 0; i < M; i++) bad[i] = (i + 1) % M, good[i] = (i - 1 + M) % M; cin >> n >> q >> s; s = "#" + s; for (int i = 1; i <= n; i++) add_element(i); cout << solve() << '\n'; while (q--) { cin >> dex; del_element(dex); cin >> s[dex]; add_element(dex); cout << solve() << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int inp(char c) { if (c == 'R') return 0; if (c == 'P') return 1; if (c == 'S') return 2; } int t[3][N]; int upd(int i, int p, int v) { for (; p < N; p += p & -p) t[i][p] += v; return 1; } int get(int i, int p) { int res = 0; for (; p > 0; p -= p & -p) res += t[i][p]; return res; } int get(int i, int l, int r) { return get(i, r) - get(i, l - 1); } int n, q; string s; int FullC(int i, bool Pre) { int L = 0, R = n + 1; while (L < R) { int M = (L + R) / 2; int l = Pre ? 1 : M; int r = Pre ? M : n; if ((get(i, l, r) == r - l + 1) != Pre) R = M; else L = M + 1; } return L; } int First(int i, bool Pre) { int L = 0, R = n + 1; while (L < R) { int M = (L + R) / 2; int l = Pre ? 1 : M; int r = Pre ? M : n; if ((get(i, l, r) > 0) == Pre) R = M; else L = M + 1; } return L; } int work() { int ans = 0; for (int i = 0; i < 3; ++i) { if (get(i, n) == n) return n; if (get(i, n) == 0) continue; int j = (i + 2) % 3; if (get(j, n)) { ans += get(i, n); ans -= get(i, FullC(i, 1), FullC(i, 0) - 1); ans += get(i, First(j, 1), First(j, 0) - 1); } } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q >> s; s = "#" + s; for (int i = 1; i <= n; ++i) upd(inp(s[i]), i, 1); cout << work() << "\n"; while (q--) { int i; cin >> i; upd(inp(s[i]), i, -1); cin >> s[i]; upd(inp(s[i]), i, 1); cout << work() << "\n"; } }
### Prompt Please create a solution in cpp to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int inp(char c) { if (c == 'R') return 0; if (c == 'P') return 1; if (c == 'S') return 2; } int t[3][N]; int upd(int i, int p, int v) { for (; p < N; p += p & -p) t[i][p] += v; return 1; } int get(int i, int p) { int res = 0; for (; p > 0; p -= p & -p) res += t[i][p]; return res; } int get(int i, int l, int r) { return get(i, r) - get(i, l - 1); } int n, q; string s; int FullC(int i, bool Pre) { int L = 0, R = n + 1; while (L < R) { int M = (L + R) / 2; int l = Pre ? 1 : M; int r = Pre ? M : n; if ((get(i, l, r) == r - l + 1) != Pre) R = M; else L = M + 1; } return L; } int First(int i, bool Pre) { int L = 0, R = n + 1; while (L < R) { int M = (L + R) / 2; int l = Pre ? 1 : M; int r = Pre ? M : n; if ((get(i, l, r) > 0) == Pre) R = M; else L = M + 1; } return L; } int work() { int ans = 0; for (int i = 0; i < 3; ++i) { if (get(i, n) == n) return n; if (get(i, n) == 0) continue; int j = (i + 2) % 3; if (get(j, n)) { ans += get(i, n); ans -= get(i, FullC(i, 1), FullC(i, 0) - 1); ans += get(i, First(j, 1), First(j, 0) - 1); } } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q >> s; s = "#" + s; for (int i = 1; i <= n; ++i) upd(inp(s[i]), i, 1); cout << work() << "\n"; while (q--) { int i; cin >> i; upd(inp(s[i]), i, -1); cin >> s[i]; upd(inp(s[i]), i, 1); cout << work() << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } pair<int, int> operator*(const pair<int, int> &p, const pair<int, int> &x) { pair<int, int> np = make_pair(p.first * x.first, p.second * x.second); return np; } pair<long long, long long> operator%(const pair<long long, long long> &p, const pair<long long, long long> &x) { pair<long long, long long> np = make_pair(p.first % x.first, p.second % x.second); return np; } pair<long long, long long> operator*(const pair<long long, long long> &p, const pair<int, int> &x) { pair<long long, long long> np = make_pair(p.first * x.first, p.second * x.second); return np; } pair<long long, long long> operator*(const pair<long long, long long> &p, const pair<long long, long long> &x) { pair<long long, long long> np = make_pair(p.first * x.first, p.second * x.second); return np; } pair<long long, long long> operator+(const pair<long long, long long> &p, const pair<long long, long long> &x) { pair<long long, long long> np = make_pair(p.first + x.first, p.second + x.second); return np; } pair<long long, long long> operator-(const pair<long long, long long> &p, const pair<long long, long long> &x) { pair<long long, long long> np = make_pair(p.first - x.first, p.second - x.second); return np; } pair<int, int> operator+(const pair<int, int> &p, const pair<int, int> &x) { pair<int, int> np = make_pair(p.first + x.first, p.second + x.second); return np; } pair<int, int> operator-(const pair<int, int> &p, const pair<int, int> &x) { pair<int, int> np = make_pair(p.first - x.first, p.second - x.second); return np; } vector<int> prime; bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { return (a.first > b.first); } bool sortbysecdesc(const pair<int, int> &a, const pair<int, int> &b) { return a.second > b.second; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } long long powerm(long long a, long long k) { if (!k) { return 1; } long long b = powerm(a, k / 2); b = b * b % 998244353; if (k % 2) { return a * b % 998244353; } else { return b; } } long long power(long long a, long long b) { if (b == 1) return a; if (b == 0) return 1; long long m1 = power(a, b / 2); if (b % 2) return m1 * m1 * a; return m1 * m1; } void PFactor(int x) { prime.clear(); for (int i = 2; i <= x / i; i++) if (x % i == 0) { while (x % i == 0) x /= i, prime.push_back(i); } if (x > 1) prime.push_back(x); } bool isprime(long long a) { if (a <= 1) return false; if (a == 2 || a == 3) return true; if (a % 2 == 0 || a % 3 == 0) return false; for (long long i = 5; i * i <= a; i = i + 6) { if (a % i == 0 || a % (i + 2) == 0) return false; } return true; } long long read() { long long a = 0; char b = 1, c; do if ((c = getchar()) == 45) b = -1; while (c < 48 || c > 57); do a = (a << 3) + (a << 1) + (c & 15); while ((c = getchar()) > 47 && c < 58); return a * b; } void write(long long x, char c) { if (x < 0) putchar(45), x = -x; char a[20], s = 0; do a[++s] = x % 10 | 48; while (x /= 10); do putchar(a[s]); while (--s); putchar(c); } const int N = 5e6 + 20; vector<int> g[N]; int n = 0, m = 0, k = 0; string str; int tree[N][3]; void modify(int L, int R, int p, int pos, int x, int y) { if (L == R) { if (x != -1) tree[p][x]--; tree[p][y]++; return; } int mid = (L + R) >> 1; if (pos <= mid) modify(L, mid, (p << 1), pos, x, y); else modify(mid + 1, R, ((p << 1) | 1), pos, x, y); for (int i = 0; i <= 2; i++) tree[p][i] = tree[(p << 1)][i] + tree[((p << 1) | 1)][i]; } int query(int L, int R, int l, int r, int p, int x) { if (l == L && r == R) return tree[p][x]; int mid = (L + R) >> 1; if (r <= mid) return query(L, mid, l, r, (p << 1), x); else if (l >= mid + 1) return query(mid + 1, R, l, r, ((p << 1) | 1), x); else return query(L, mid, l, mid, (p << 1), x) + query(mid + 1, R, mid + 1, r, ((p << 1) | 1), x); } int cal(set<int> &d, set<int> &a, set<int> &b, int x) { if (b.empty()) return (int)(d).size(); if (a.empty()) return 0; int la = *a.begin(), ra = *a.rbegin(); int lb = *b.begin(), rb = *b.rbegin(); int ans = (int)(d).size(); if (la > lb) ans -= query(1, n, lb, la, 1, x); if (ra < rb) ans -= query(1, n, ra, rb, 1, x); return ans; } set<int> rock, paper, scissors; int solve() { int ans = 0; ans += cal(rock, scissors, paper, 0); ans += cal(scissors, paper, rock, 1); ans += cal(paper, rock, scissors, 2); return ans; } int d[N]; void build_tree(int L, int R, int p) { if (L == R) { tree[p][d[L]]++; return; } int mid = (L + R) >> 1; build_tree(L, mid, (p << 1)); build_tree(mid + 1, R, ((p << 1) | 1)); for (int i = 0; i <= 2; i++) tree[p][i] = tree[(p << 1)][i] + tree[((p << 1) | 1)][i]; } int main() { cin >> n >> k; cin >> str; for (int i = 0; i <= n - 1; i++) { if (str[i] == 'R') rock.insert(i + 1), d[i + 1] = 0; if (str[i] == 'P') paper.insert(i + 1), d[i + 1] = 2; if (str[i] == 'S') scissors.insert(i + 1), d[i + 1] = 1; } build_tree(1, n, 1); cout << solve() << endl; for (int i = 1; i <= k; i++) { int pos; char ch; pos = read(); ch = getchar(); if (d[pos] == 0) rock.erase(rock.find(pos)); else if (d[pos] == 1) scissors.erase(scissors.find(pos)); else paper.erase(paper.find(pos)); if (ch == 'R') { modify(1, n, 1, pos, d[pos], 0); d[pos] = 0; rock.insert(pos); } else if (ch == 'S') { modify(1, n, 1, pos, d[pos], 1); d[pos] = 1; scissors.insert(pos); } else { modify(1, n, 1, pos, d[pos], 2); d[pos] = 2; paper.insert(pos); } write(solve(), '\n'); } }
### Prompt Create a solution in Cpp for the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } pair<int, int> operator*(const pair<int, int> &p, const pair<int, int> &x) { pair<int, int> np = make_pair(p.first * x.first, p.second * x.second); return np; } pair<long long, long long> operator%(const pair<long long, long long> &p, const pair<long long, long long> &x) { pair<long long, long long> np = make_pair(p.first % x.first, p.second % x.second); return np; } pair<long long, long long> operator*(const pair<long long, long long> &p, const pair<int, int> &x) { pair<long long, long long> np = make_pair(p.first * x.first, p.second * x.second); return np; } pair<long long, long long> operator*(const pair<long long, long long> &p, const pair<long long, long long> &x) { pair<long long, long long> np = make_pair(p.first * x.first, p.second * x.second); return np; } pair<long long, long long> operator+(const pair<long long, long long> &p, const pair<long long, long long> &x) { pair<long long, long long> np = make_pair(p.first + x.first, p.second + x.second); return np; } pair<long long, long long> operator-(const pair<long long, long long> &p, const pair<long long, long long> &x) { pair<long long, long long> np = make_pair(p.first - x.first, p.second - x.second); return np; } pair<int, int> operator+(const pair<int, int> &p, const pair<int, int> &x) { pair<int, int> np = make_pair(p.first + x.first, p.second + x.second); return np; } pair<int, int> operator-(const pair<int, int> &p, const pair<int, int> &x) { pair<int, int> np = make_pair(p.first - x.first, p.second - x.second); return np; } vector<int> prime; bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { return (a.first > b.first); } bool sortbysecdesc(const pair<int, int> &a, const pair<int, int> &b) { return a.second > b.second; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } long long powerm(long long a, long long k) { if (!k) { return 1; } long long b = powerm(a, k / 2); b = b * b % 998244353; if (k % 2) { return a * b % 998244353; } else { return b; } } long long power(long long a, long long b) { if (b == 1) return a; if (b == 0) return 1; long long m1 = power(a, b / 2); if (b % 2) return m1 * m1 * a; return m1 * m1; } void PFactor(int x) { prime.clear(); for (int i = 2; i <= x / i; i++) if (x % i == 0) { while (x % i == 0) x /= i, prime.push_back(i); } if (x > 1) prime.push_back(x); } bool isprime(long long a) { if (a <= 1) return false; if (a == 2 || a == 3) return true; if (a % 2 == 0 || a % 3 == 0) return false; for (long long i = 5; i * i <= a; i = i + 6) { if (a % i == 0 || a % (i + 2) == 0) return false; } return true; } long long read() { long long a = 0; char b = 1, c; do if ((c = getchar()) == 45) b = -1; while (c < 48 || c > 57); do a = (a << 3) + (a << 1) + (c & 15); while ((c = getchar()) > 47 && c < 58); return a * b; } void write(long long x, char c) { if (x < 0) putchar(45), x = -x; char a[20], s = 0; do a[++s] = x % 10 | 48; while (x /= 10); do putchar(a[s]); while (--s); putchar(c); } const int N = 5e6 + 20; vector<int> g[N]; int n = 0, m = 0, k = 0; string str; int tree[N][3]; void modify(int L, int R, int p, int pos, int x, int y) { if (L == R) { if (x != -1) tree[p][x]--; tree[p][y]++; return; } int mid = (L + R) >> 1; if (pos <= mid) modify(L, mid, (p << 1), pos, x, y); else modify(mid + 1, R, ((p << 1) | 1), pos, x, y); for (int i = 0; i <= 2; i++) tree[p][i] = tree[(p << 1)][i] + tree[((p << 1) | 1)][i]; } int query(int L, int R, int l, int r, int p, int x) { if (l == L && r == R) return tree[p][x]; int mid = (L + R) >> 1; if (r <= mid) return query(L, mid, l, r, (p << 1), x); else if (l >= mid + 1) return query(mid + 1, R, l, r, ((p << 1) | 1), x); else return query(L, mid, l, mid, (p << 1), x) + query(mid + 1, R, mid + 1, r, ((p << 1) | 1), x); } int cal(set<int> &d, set<int> &a, set<int> &b, int x) { if (b.empty()) return (int)(d).size(); if (a.empty()) return 0; int la = *a.begin(), ra = *a.rbegin(); int lb = *b.begin(), rb = *b.rbegin(); int ans = (int)(d).size(); if (la > lb) ans -= query(1, n, lb, la, 1, x); if (ra < rb) ans -= query(1, n, ra, rb, 1, x); return ans; } set<int> rock, paper, scissors; int solve() { int ans = 0; ans += cal(rock, scissors, paper, 0); ans += cal(scissors, paper, rock, 1); ans += cal(paper, rock, scissors, 2); return ans; } int d[N]; void build_tree(int L, int R, int p) { if (L == R) { tree[p][d[L]]++; return; } int mid = (L + R) >> 1; build_tree(L, mid, (p << 1)); build_tree(mid + 1, R, ((p << 1) | 1)); for (int i = 0; i <= 2; i++) tree[p][i] = tree[(p << 1)][i] + tree[((p << 1) | 1)][i]; } int main() { cin >> n >> k; cin >> str; for (int i = 0; i <= n - 1; i++) { if (str[i] == 'R') rock.insert(i + 1), d[i + 1] = 0; if (str[i] == 'P') paper.insert(i + 1), d[i + 1] = 2; if (str[i] == 'S') scissors.insert(i + 1), d[i + 1] = 1; } build_tree(1, n, 1); cout << solve() << endl; for (int i = 1; i <= k; i++) { int pos; char ch; pos = read(); ch = getchar(); if (d[pos] == 0) rock.erase(rock.find(pos)); else if (d[pos] == 1) scissors.erase(scissors.find(pos)); else paper.erase(paper.find(pos)); if (ch == 'R') { modify(1, n, 1, pos, d[pos], 0); d[pos] = 0; rock.insert(pos); } else if (ch == 'S') { modify(1, n, 1, pos, d[pos], 1); d[pos] = 1; scissors.insert(pos); } else { modify(1, n, 1, pos, d[pos], 2); d[pos] = 2; paper.insert(pos); } write(solve(), '\n'); } } ```
#include <bits/stdc++.h> using namespace std; char s[200005], to[128], ch; set<int> p[3]; set<int>::iterator it; int c[3][200005], a, n, q, i; void add(int x, int y, int v) { while (y < 200005) c[x][y] += v, y += (y & (-y)); } int sum(int x, int y) { int ret = 0; while (y) ret += c[x][y], y -= (y & (-y)); return ret; } int F(int x, int y, int z) { if (!p[x].size()) return 0; if (!p[y].size()) return sum(x, n); if (!p[z].size()) return 0; int ret = sum(x, n), f1 = *p[y].begin(), f2 = *p[z].begin(); if (f2 > f1) ret -= sum(x, f2) - sum(x, f1); it = p[y].end(), f1 = *--it; it = p[z].end(), f2 = *--it; if (f1 > f2) ret -= sum(x, f1) - sum(x, f2); return ret; } int cal() { return F(0, 2, 1) + F(1, 0, 2) + F(2, 1, 0); } int main() { scanf("%d%d%s", &n, &q, s + 1); to['R'] = 1; to['S'] = 2; for (i = 1; i <= n; ++i) p[to[s[i]]].insert(i), add(to[s[i]], i, 1); cout << cal() << "\n"; while (q--) { cin >> a >> ch; add(to[s[a]], a, -1); p[to[s[a]]].erase(a); s[a] = ch; add(to[s[a]], a, 1); p[to[s[a]]].insert(a); cout << cal() << "\n"; } }
### Prompt Generate a cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[200005], to[128], ch; set<int> p[3]; set<int>::iterator it; int c[3][200005], a, n, q, i; void add(int x, int y, int v) { while (y < 200005) c[x][y] += v, y += (y & (-y)); } int sum(int x, int y) { int ret = 0; while (y) ret += c[x][y], y -= (y & (-y)); return ret; } int F(int x, int y, int z) { if (!p[x].size()) return 0; if (!p[y].size()) return sum(x, n); if (!p[z].size()) return 0; int ret = sum(x, n), f1 = *p[y].begin(), f2 = *p[z].begin(); if (f2 > f1) ret -= sum(x, f2) - sum(x, f1); it = p[y].end(), f1 = *--it; it = p[z].end(), f2 = *--it; if (f1 > f2) ret -= sum(x, f1) - sum(x, f2); return ret; } int cal() { return F(0, 2, 1) + F(1, 0, 2) + F(2, 1, 0); } int main() { scanf("%d%d%s", &n, &q, s + 1); to['R'] = 1; to['S'] = 2; for (i = 1; i <= n; ++i) p[to[s[i]]].insert(i), add(to[s[i]], i, 1); cout << cal() << "\n"; while (q--) { cin >> a >> ch; add(to[s[a]], a, -1); p[to[s[a]]].erase(a); s[a] = ch; add(to[s[a]], a, 1); p[to[s[a]]].insert(a); cout << cal() << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; int n, m; struct BIT { int c[maxn]; void add(int x, int y) { for (; x <= n; x += (x & (-x))) c[x] += y; } int ask(int x) { int ans = 0; for (; x; x -= (x & (-x))) ans += c[x]; return ans; } int query(int l, int r) { return ask(r) - ask(l - 1); } }; BIT b[3]; set<int> s[3]; map<char, int> mp; int a[maxn]; char str[maxn]; int solve(void) { int ans = 0; for (int i = 0; i < 3; i++) { if (s[(i + 1) % 3].empty()) { ans += s[i].size(); continue; } if (s[(i + 2) % 3].empty()) continue; int l1 = *(s[(i + 2) % 3].begin()), r1 = *(--s[(i + 2) % 3].end()); ans += b[i].query(l1, r1); int l2 = *(s[(i + 1) % 3].begin()), r2 = *(--s[(i + 1) % 3].end()); ans += b[i].query(1, min(l1, l2) - 1); ans += b[i].query(max(r1, r2) + 1, n); } return ans; } int main() { mp['R'] = 0, mp['P'] = 1, mp['S'] = 2; scanf("%d%d", &n, &m); scanf("%s", str + 1); for (int i = 1; i <= n; i++) { a[i] = mp[str[i]]; b[a[i]].add(i, 1); s[a[i]].insert(i); } printf("%d\n", solve()); for (int i = 1; i <= m; i++) { int x; scanf("%d%s", &x, str + 1); b[a[x]].add(x, -1); s[a[x]].erase(x); a[x] = mp[str[1]]; b[a[x]].add(x, 1); s[a[x]].insert(x); printf("%d\n", solve()); } }
### Prompt Your challenge is to write a Cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 200010; int n, m; struct BIT { int c[maxn]; void add(int x, int y) { for (; x <= n; x += (x & (-x))) c[x] += y; } int ask(int x) { int ans = 0; for (; x; x -= (x & (-x))) ans += c[x]; return ans; } int query(int l, int r) { return ask(r) - ask(l - 1); } }; BIT b[3]; set<int> s[3]; map<char, int> mp; int a[maxn]; char str[maxn]; int solve(void) { int ans = 0; for (int i = 0; i < 3; i++) { if (s[(i + 1) % 3].empty()) { ans += s[i].size(); continue; } if (s[(i + 2) % 3].empty()) continue; int l1 = *(s[(i + 2) % 3].begin()), r1 = *(--s[(i + 2) % 3].end()); ans += b[i].query(l1, r1); int l2 = *(s[(i + 1) % 3].begin()), r2 = *(--s[(i + 1) % 3].end()); ans += b[i].query(1, min(l1, l2) - 1); ans += b[i].query(max(r1, r2) + 1, n); } return ans; } int main() { mp['R'] = 0, mp['P'] = 1, mp['S'] = 2; scanf("%d%d", &n, &m); scanf("%s", str + 1); for (int i = 1; i <= n; i++) { a[i] = mp[str[i]]; b[a[i]].add(i, 1); s[a[i]].insert(i); } printf("%d\n", solve()); for (int i = 1; i <= m; i++) { int x; scanf("%d%s", &x, str + 1); b[a[x]].add(x, -1); s[a[x]].erase(x); a[x] = mp[str[1]]; b[a[x]].add(x, 1); s[a[x]].insert(x); printf("%d\n", solve()); } } ```
#include <bits/stdc++.h> using namespace std; const int N = int(4e5) + 5; const int inf = (int)1e9 + 7; struct bit { int t[N]; void inc(int pos, int val) { for (; pos < N; pos += pos & -pos) { t[pos] += val; } } int get(int pos) { int ans = 0; for (; pos > 0; pos -= pos & -pos) { ans += t[pos]; } return ans; } } s[4]; int n, q; int l[4], w[4]; char a[N]; set<int> pos[4]; int get(char x) { return (x == 'R' ? 1 : (x == 'P' ? 2 : 3)); } pair<int, int> intersect(pair<int, int> a, pair<int, int> b) { return make_pair(max(a.first, b.first), min(a.second, b.second)); } int calc() { int ans = 0; for (int t = 1; t <= 3; ++t) { if (pos[l[t]].empty()) { ans += (int)((pos[t]).size()); continue; } if (pos[w[t]].empty()) { continue; } vector<pair<int, int> > pf, sf; int F = *pos[l[t]].begin(); int S = *pos[w[t]].begin(); pair<int, int> TF = make_pair(-1, -1); if (F < S) { TF = make_pair(F, S - 1); } pair<int, int> TS = make_pair(-1, -1); F = *pos[l[t]].rbegin(); S = *pos[w[t]].rbegin(); if (F > S) { TS = make_pair(S + 1, F); } int cur = s[t].get(n); if (TF.first != -1) { cur -= s[t].get(TF.second) - s[t].get(TF.first - 1); } if (TS.first != -1) { cur -= s[t].get(TS.second) - s[t].get(TS.first - 1); } if (TF.first != -1 && TS.first != -1) { pair<int, int> q = intersect(TF, TS); if (q.first <= q.second) { cur += s[t].get(q.second) - s[t].get(q.first - 1); } } ans += cur; } return ans; } int main() { l[1] = 2, w[1] = 3; l[2] = 3, w[2] = 1; l[3] = 1, w[3] = 2; scanf("%d %d", &n, &q); for (int i = 1; i <= n; ++i) { scanf(" %c", a + i); pos[get(a[i])].insert(i); s[get(a[i])].inc(i, 1); } printf("%d\n", calc()); for (int i = 1; i <= q; ++i) { int p; char x; scanf("%d %c", &p, &x); pos[get(a[p])].erase(p); s[get(a[p])].inc(p, -1); a[p] = x; pos[get(a[p])].insert(p); s[get(a[p])].inc(p, 1); printf("%d\n", calc()); } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = int(4e5) + 5; const int inf = (int)1e9 + 7; struct bit { int t[N]; void inc(int pos, int val) { for (; pos < N; pos += pos & -pos) { t[pos] += val; } } int get(int pos) { int ans = 0; for (; pos > 0; pos -= pos & -pos) { ans += t[pos]; } return ans; } } s[4]; int n, q; int l[4], w[4]; char a[N]; set<int> pos[4]; int get(char x) { return (x == 'R' ? 1 : (x == 'P' ? 2 : 3)); } pair<int, int> intersect(pair<int, int> a, pair<int, int> b) { return make_pair(max(a.first, b.first), min(a.second, b.second)); } int calc() { int ans = 0; for (int t = 1; t <= 3; ++t) { if (pos[l[t]].empty()) { ans += (int)((pos[t]).size()); continue; } if (pos[w[t]].empty()) { continue; } vector<pair<int, int> > pf, sf; int F = *pos[l[t]].begin(); int S = *pos[w[t]].begin(); pair<int, int> TF = make_pair(-1, -1); if (F < S) { TF = make_pair(F, S - 1); } pair<int, int> TS = make_pair(-1, -1); F = *pos[l[t]].rbegin(); S = *pos[w[t]].rbegin(); if (F > S) { TS = make_pair(S + 1, F); } int cur = s[t].get(n); if (TF.first != -1) { cur -= s[t].get(TF.second) - s[t].get(TF.first - 1); } if (TS.first != -1) { cur -= s[t].get(TS.second) - s[t].get(TS.first - 1); } if (TF.first != -1 && TS.first != -1) { pair<int, int> q = intersect(TF, TS); if (q.first <= q.second) { cur += s[t].get(q.second) - s[t].get(q.first - 1); } } ans += cur; } return ans; } int main() { l[1] = 2, w[1] = 3; l[2] = 3, w[2] = 1; l[3] = 1, w[3] = 2; scanf("%d %d", &n, &q); for (int i = 1; i <= n; ++i) { scanf(" %c", a + i); pos[get(a[i])].insert(i); s[get(a[i])].inc(i, 1); } printf("%d\n", calc()); for (int i = 1; i <= q; ++i) { int p; char x; scanf("%d %c", &p, &x); pos[get(a[p])].erase(p); s[get(a[p])].inc(p, -1); a[p] = x; pos[get(a[p])].insert(p); s[get(a[p])].inc(p, 1); printf("%d\n", calc()); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 200000; int N, Q; char state[MAXN + 5]; set<int> rock, paper, scissors; int bitRock[MAXN + 5], bitScissors[MAXN + 5], bitPaper[MAXN + 5]; void bitAdd(int* BIT, int idx, int val) { while (idx <= N) { BIT[idx] += val; idx += (idx & (-idx)); } } int bitGet(int* BIT, int idx) { int ret = 0; while (idx > 0) { ret += BIT[idx]; idx -= (idx & (-idx)); } return ret; } void deleteIn(int idx) { if (state[idx] == 'R') { rock.erase(idx); bitAdd(bitRock, idx, -1); } else if (state[idx] == 'S') { scissors.erase(idx); bitAdd(bitScissors, idx, -1); } else if (state[idx] == 'P') { paper.erase(idx); bitAdd(bitPaper, idx, -1); } } void addInWith(int idx, char kar) { state[idx] = kar; if (state[idx] == 'R') { rock.insert(idx); bitAdd(bitRock, idx, 1); } else if (state[idx] == 'S') { scissors.insert(idx); bitAdd(bitScissors, idx, 1); } else if (state[idx] == 'P') { paper.insert(idx); bitAdd(bitPaper, idx, 1); } } int leftPaper() { return *paper.begin(); } int leftRock() { return *rock.begin(); } int leftScissors() { return *scissors.begin(); } int rightPaper() { return *paper.rbegin(); } int rightRock() { return *rock.rbegin(); } int rightScissors() { return *scissors.rbegin(); } int rockIn(int le, int ri) { if (le >= ri) { return 0; } return bitGet(bitRock, ri) - bitGet(bitRock, le - 1); } int paperIn(int le, int ri) { if (le >= ri) { return 0; } return bitGet(bitPaper, ri) - bitGet(bitPaper, le - 1); } int scissorsIn(int le, int ri) { if (le >= ri) { return 0; } return bitGet(bitScissors, ri) - bitGet(bitScissors, le - 1); } int retriveAns() { if (rock.size() && scissors.size() && paper.size()) { return rock.size() - rockIn(leftPaper(), leftScissors()) - rockIn(rightScissors(), rightPaper()) + paper.size() - paperIn(leftScissors(), leftRock()) - paperIn(rightRock(), rightScissors()) + scissors.size() - scissorsIn(leftRock(), leftPaper()) - scissorsIn(rightPaper(), rightRock()); } int cnt = (rock.size() > 0) + (scissors.size() > 0) + (paper.size() > 0); if (cnt == 1) { return rock.size() + scissors.size() + paper.size(); } if (rock.size() && paper.size()) { return paper.size(); } if (paper.size() && scissors.size()) { return scissors.size(); } if (scissors.size() && rock.size()) { return rock.size(); } assert(false); } int main(int argc, char** argv, char** envp) { scanf("%d %d", &N, &Q); scanf("%s", state + 1); for (int i = 1; i <= N; i++) { if (state[i] == 'R') { bitAdd(bitRock, i, 1); rock.insert(i); } else if (state[i] == 'S') { bitAdd(bitScissors, i, 1); scissors.insert(i); } else if (state[i] == 'P') { bitAdd(bitPaper, i, 1); paper.insert(i); } } printf("%d\n", retriveAns()); while (Q--) { int idx; char kar; scanf("%d %c", &idx, &kar); deleteIn(idx); addInWith(idx, kar); printf("%d\n", retriveAns()); } }
### Prompt Please provide a CPP coded solution to the problem described below: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 200000; int N, Q; char state[MAXN + 5]; set<int> rock, paper, scissors; int bitRock[MAXN + 5], bitScissors[MAXN + 5], bitPaper[MAXN + 5]; void bitAdd(int* BIT, int idx, int val) { while (idx <= N) { BIT[idx] += val; idx += (idx & (-idx)); } } int bitGet(int* BIT, int idx) { int ret = 0; while (idx > 0) { ret += BIT[idx]; idx -= (idx & (-idx)); } return ret; } void deleteIn(int idx) { if (state[idx] == 'R') { rock.erase(idx); bitAdd(bitRock, idx, -1); } else if (state[idx] == 'S') { scissors.erase(idx); bitAdd(bitScissors, idx, -1); } else if (state[idx] == 'P') { paper.erase(idx); bitAdd(bitPaper, idx, -1); } } void addInWith(int idx, char kar) { state[idx] = kar; if (state[idx] == 'R') { rock.insert(idx); bitAdd(bitRock, idx, 1); } else if (state[idx] == 'S') { scissors.insert(idx); bitAdd(bitScissors, idx, 1); } else if (state[idx] == 'P') { paper.insert(idx); bitAdd(bitPaper, idx, 1); } } int leftPaper() { return *paper.begin(); } int leftRock() { return *rock.begin(); } int leftScissors() { return *scissors.begin(); } int rightPaper() { return *paper.rbegin(); } int rightRock() { return *rock.rbegin(); } int rightScissors() { return *scissors.rbegin(); } int rockIn(int le, int ri) { if (le >= ri) { return 0; } return bitGet(bitRock, ri) - bitGet(bitRock, le - 1); } int paperIn(int le, int ri) { if (le >= ri) { return 0; } return bitGet(bitPaper, ri) - bitGet(bitPaper, le - 1); } int scissorsIn(int le, int ri) { if (le >= ri) { return 0; } return bitGet(bitScissors, ri) - bitGet(bitScissors, le - 1); } int retriveAns() { if (rock.size() && scissors.size() && paper.size()) { return rock.size() - rockIn(leftPaper(), leftScissors()) - rockIn(rightScissors(), rightPaper()) + paper.size() - paperIn(leftScissors(), leftRock()) - paperIn(rightRock(), rightScissors()) + scissors.size() - scissorsIn(leftRock(), leftPaper()) - scissorsIn(rightPaper(), rightRock()); } int cnt = (rock.size() > 0) + (scissors.size() > 0) + (paper.size() > 0); if (cnt == 1) { return rock.size() + scissors.size() + paper.size(); } if (rock.size() && paper.size()) { return paper.size(); } if (paper.size() && scissors.size()) { return scissors.size(); } if (scissors.size() && rock.size()) { return rock.size(); } assert(false); } int main(int argc, char** argv, char** envp) { scanf("%d %d", &N, &Q); scanf("%s", state + 1); for (int i = 1; i <= N; i++) { if (state[i] == 'R') { bitAdd(bitRock, i, 1); rock.insert(i); } else if (state[i] == 'S') { bitAdd(bitScissors, i, 1); scissors.insert(i); } else if (state[i] == 'P') { bitAdd(bitPaper, i, 1); paper.insert(i); } } printf("%d\n", retriveAns()); while (Q--) { int idx; char kar; scanf("%d %c", &idx, &kar); deleteIn(idx); addInWith(idx, kar); printf("%d\n", retriveAns()); } } ```
#include <bits/stdc++.h> using namespace std; namespace fast_IO { inline int read_int() { register int ret = 0, f = 1; register char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { ret = (ret << 1) + (ret << 3) + int(c - 48); c = getchar(); } return ret * f; } } // namespace fast_IO using namespace fast_IO; int N, Q; int a[200005]; char s[200005]; struct BIT { int c[200005]; inline int lowbit(int x) { return x & (-x); } inline void add(int pos, int w) { while (pos <= N) c[pos] += w, pos += lowbit(pos); } inline int query(int pos) { int res = 0; while (pos) res += c[pos], pos -= lowbit(pos); return res; } } BIT[3]; set<int> S[3]; inline int decode(char c) { if (c == 'R') return 0; if (c == 'P') return 1; if (c == 'S') return 2; } inline void init() { N = read_int(), Q = read_int(); scanf("%s", s + 1); for (register int i = 0; i < 3; i++) S[i].insert(-0x3f3f3f3f), S[i].insert(0x3f3f3f3f); for (register int i = 1; i <= N; i++) { a[i] = decode(s[i]); BIT[a[i]].add(i, 1); S[a[i]].insert(i); } } inline int calc() { if (S[0].size() == N + 2 || S[1].size() == N + 2 || S[2].size() == N + 2) return N; if (S[0].size() == 2) return S[2].size() - 2; if (S[1].size() == 2) return S[0].size() - 2; if (S[2].size() == 2) return S[1].size() - 2; set<int>::iterator it_min[3], it_max[3]; for (register int i = 0; i < 3; i++) it_min[i] = S[i].begin(), it_min[i]++; for (register int i = 0; i < 3; i++) it_max[i] = S[i].end(), it_max[i]--, it_max[i]--; int ans = N; for (register int i = 0; i < 3; i++) { int l1 = (i + 1) % 3, r1 = (i + 2) % 3; int L = *it_min[l1], R = *it_min[r1] - 1; ans -= max(0, BIT[i].query(R) - BIT[i].query(L - 1)); L = *it_max[r1], R = *it_max[l1]; ans -= max(0, BIT[i].query(R) - BIT[i].query(L - 1)); } return ans; } inline void ans() { printf("%d\n", calc()); register int pos, nw; static char opt[20]; while (Q--) { pos = read_int(); scanf("%s", opt); nw = decode(opt[0]); S[a[pos]].erase(pos); BIT[a[pos]].add(pos, -1); a[pos] = nw, S[a[pos]].insert(pos), BIT[a[pos]].add(pos, 1); printf("%d\n", calc()); } } int main() { init(); ans(); return 0; }
### Prompt Create a solution in Cpp for the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; namespace fast_IO { inline int read_int() { register int ret = 0, f = 1; register char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { ret = (ret << 1) + (ret << 3) + int(c - 48); c = getchar(); } return ret * f; } } // namespace fast_IO using namespace fast_IO; int N, Q; int a[200005]; char s[200005]; struct BIT { int c[200005]; inline int lowbit(int x) { return x & (-x); } inline void add(int pos, int w) { while (pos <= N) c[pos] += w, pos += lowbit(pos); } inline int query(int pos) { int res = 0; while (pos) res += c[pos], pos -= lowbit(pos); return res; } } BIT[3]; set<int> S[3]; inline int decode(char c) { if (c == 'R') return 0; if (c == 'P') return 1; if (c == 'S') return 2; } inline void init() { N = read_int(), Q = read_int(); scanf("%s", s + 1); for (register int i = 0; i < 3; i++) S[i].insert(-0x3f3f3f3f), S[i].insert(0x3f3f3f3f); for (register int i = 1; i <= N; i++) { a[i] = decode(s[i]); BIT[a[i]].add(i, 1); S[a[i]].insert(i); } } inline int calc() { if (S[0].size() == N + 2 || S[1].size() == N + 2 || S[2].size() == N + 2) return N; if (S[0].size() == 2) return S[2].size() - 2; if (S[1].size() == 2) return S[0].size() - 2; if (S[2].size() == 2) return S[1].size() - 2; set<int>::iterator it_min[3], it_max[3]; for (register int i = 0; i < 3; i++) it_min[i] = S[i].begin(), it_min[i]++; for (register int i = 0; i < 3; i++) it_max[i] = S[i].end(), it_max[i]--, it_max[i]--; int ans = N; for (register int i = 0; i < 3; i++) { int l1 = (i + 1) % 3, r1 = (i + 2) % 3; int L = *it_min[l1], R = *it_min[r1] - 1; ans -= max(0, BIT[i].query(R) - BIT[i].query(L - 1)); L = *it_max[r1], R = *it_max[l1]; ans -= max(0, BIT[i].query(R) - BIT[i].query(L - 1)); } return ans; } inline void ans() { printf("%d\n", calc()); register int pos, nw; static char opt[20]; while (Q--) { pos = read_int(); scanf("%s", opt); nw = decode(opt[0]); S[a[pos]].erase(pos); BIT[a[pos]].add(pos, -1); a[pos] = nw, S[a[pos]].insert(pos), BIT[a[pos]].add(pos, 1); printf("%d\n", calc()); } } int main() { init(); ans(); return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, true : false; } template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, true : false; } int readint() { 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; } struct node { int val[600000]; void change(int id, int l, int r, int x, int c) { val[id] += c; if (l == r) return; int mid = (l + r) / 2; if (x <= mid) change(id << 1, l, mid, x, c); else change(id << 1 | 1, mid + 1, r, x, c); } int findl(int id, int l, int r) { if (l == r) return l; int mid = (l + r) / 2; if (val[id << 1]) return findl(id << 1, l, mid); else return findl(id << 1 | 1, mid + 1, r); } int findr(int id, int l, int r) { if (l == r) return r; int mid = (l + r) / 2; if (val[id << 1 | 1]) return findr(id << 1 | 1, mid + 1, r); else return findr(id << 1, l, mid); } int query(int id, int l, int r, int ql, int qr) { if (l == ql && r == qr) return val[id]; int mid = (l + r) / 2; if (qr <= mid) return query(id << 1, l, mid, ql, qr); else if (ql > mid) return query(id << 1 | 1, mid + 1, r, ql, qr); else return query(id << 1, l, mid, ql, mid) + query(id << 1 | 1, mid + 1, r, mid + 1, qr); } int getl(int id, int l, int r) { if (l == r) return l; int mid = (l + r) / 2; if (val[id << 1] != mid - l + 1) return getl(id << 1, l, mid); else return getl(id << 1 | 1, mid + 1, r); } int getr(int id, int l, int r) { if (l == r) return r; int mid = (l + r) / 2; if (val[id << 1 | 1] != r - mid) return getr(id << 1 | 1, mid + 1, r); else return getr(id << 1, l, mid); } } t[4]; int n, q; int a[200005]; char s[200005]; int getans() { int ans = 0; for (int i = 1; i <= 3; i++) { int l = t[i % 3 + 1].findl(1, 1, n), r = t[i % 3 + 1].findr(1, 1, n); if (a[l] == i % 3 + 1) { ans += t[i].query(1, 1, n, l, r); int pl = t[i].getl(1, 1, n), pr = t[i].getr(1, 1, n); ans += pl - 1 + n - pr; } else if (t[i].val[1] == n) ans += n; } return ans; } int main() { n = readint(); q = readint(); scanf("%s", s + 1); for (int i = 1; i <= n; i++) a[i] = s[i] == 'R' ? 1 : s[i] == 'S' ? 2 : 3; for (int i = 1; i <= n; i++) t[a[i]].change(1, 1, n, i, 1); printf("%d\n", getans()); int x, y; char z[3]; while (q--) { x = readint(); scanf("%s", z); y = z[0] == 'R' ? 1 : z[0] == 'S' ? 2 : 3; t[a[x]].change(1, 1, n, x, -1); t[y].change(1, 1, n, x, 1); a[x] = y; printf("%d\n", getans()); } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, true : false; } template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, true : false; } int readint() { 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; } struct node { int val[600000]; void change(int id, int l, int r, int x, int c) { val[id] += c; if (l == r) return; int mid = (l + r) / 2; if (x <= mid) change(id << 1, l, mid, x, c); else change(id << 1 | 1, mid + 1, r, x, c); } int findl(int id, int l, int r) { if (l == r) return l; int mid = (l + r) / 2; if (val[id << 1]) return findl(id << 1, l, mid); else return findl(id << 1 | 1, mid + 1, r); } int findr(int id, int l, int r) { if (l == r) return r; int mid = (l + r) / 2; if (val[id << 1 | 1]) return findr(id << 1 | 1, mid + 1, r); else return findr(id << 1, l, mid); } int query(int id, int l, int r, int ql, int qr) { if (l == ql && r == qr) return val[id]; int mid = (l + r) / 2; if (qr <= mid) return query(id << 1, l, mid, ql, qr); else if (ql > mid) return query(id << 1 | 1, mid + 1, r, ql, qr); else return query(id << 1, l, mid, ql, mid) + query(id << 1 | 1, mid + 1, r, mid + 1, qr); } int getl(int id, int l, int r) { if (l == r) return l; int mid = (l + r) / 2; if (val[id << 1] != mid - l + 1) return getl(id << 1, l, mid); else return getl(id << 1 | 1, mid + 1, r); } int getr(int id, int l, int r) { if (l == r) return r; int mid = (l + r) / 2; if (val[id << 1 | 1] != r - mid) return getr(id << 1 | 1, mid + 1, r); else return getr(id << 1, l, mid); } } t[4]; int n, q; int a[200005]; char s[200005]; int getans() { int ans = 0; for (int i = 1; i <= 3; i++) { int l = t[i % 3 + 1].findl(1, 1, n), r = t[i % 3 + 1].findr(1, 1, n); if (a[l] == i % 3 + 1) { ans += t[i].query(1, 1, n, l, r); int pl = t[i].getl(1, 1, n), pr = t[i].getr(1, 1, n); ans += pl - 1 + n - pr; } else if (t[i].val[1] == n) ans += n; } return ans; } int main() { n = readint(); q = readint(); scanf("%s", s + 1); for (int i = 1; i <= n; i++) a[i] = s[i] == 'R' ? 1 : s[i] == 'S' ? 2 : 3; for (int i = 1; i <= n; i++) t[a[i]].change(1, 1, n, i, 1); printf("%d\n", getans()); int x, y; char z[3]; while (q--) { x = readint(); scanf("%s", z); y = z[0] == 'R' ? 1 : z[0] == 'S' ? 2 : 3; t[a[x]].change(1, 1, n, x, -1); t[y].change(1, 1, n, x, 1); a[x] = y; printf("%d\n", getans()); } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline int getid(char c) { if (c == 'R') return 0; if (c == 'P') return 1; return 2; } const int beat[3][3] = {{0, -1, 1}, {1, 0, -1}, {-1, 1, 0}}; int n, q, a[200010]; char s[200010]; set<int> st[3]; inline int lowbit(int x) { return x & (-x); } class BIT { public: int dat[200010]; void modify(int p, int v) { for (; p <= n; p += lowbit(p)) dat[p] += v; } int query(int p) { int ret = 0; for (; p; p -= lowbit(p)) ret += dat[p]; return ret; } } T[3]; inline int calc() { int cnt = 0; for (int i = 0; i < 3; i++) cnt += (!st[i].empty()); if (cnt == 1) return n; else if (cnt == 2) { for (int i = 0; i < 3; i++) { if (!st[i].empty()) for (int j = i + 1; j < 3; j++) { if (!st[j].empty()) { if (beat[i][j] == 1) return st[i].size(); return st[j].size(); } } } return 114514; } int ans = 0; for (int i = 0; i < 3; i++) { int id = -1, id2; for (int j = 0; j < 3; j++) { if (beat[i][j] == 1) { id = j; break; } } id2 = 3 - i - id; int R1 = *st[id].rbegin(), L1 = *st[id].begin(); int R2 = *st[id2].rbegin(), L2 = *st[id2].begin(); ans += T[i].query(R1) - T[i].query(L1 - 1); ans += T[i].query(n) - T[i].query(max(R2, R1)); ans += T[i].query(min(L1, L2)); } return ans; } int main() { scanf("%d%d", &n, &q); scanf("%s", s + 1); for (int i = 1; i <= n; i++) a[i] = getid(s[i]); for (int i = 1; i <= n; i++) { st[a[i]].insert(i); T[a[i]].modify(i, 1); } printf("%d\n", calc()); while (q--) { char opt[5]; int p; scanf("%d%s", &p, opt); int x = getid(opt[0]); T[a[p]].modify(p, -1); st[a[p]].erase(p); a[p] = x; st[a[p]].insert(p); T[a[p]].modify(p, 1); printf("%d\n", calc()); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int getid(char c) { if (c == 'R') return 0; if (c == 'P') return 1; return 2; } const int beat[3][3] = {{0, -1, 1}, {1, 0, -1}, {-1, 1, 0}}; int n, q, a[200010]; char s[200010]; set<int> st[3]; inline int lowbit(int x) { return x & (-x); } class BIT { public: int dat[200010]; void modify(int p, int v) { for (; p <= n; p += lowbit(p)) dat[p] += v; } int query(int p) { int ret = 0; for (; p; p -= lowbit(p)) ret += dat[p]; return ret; } } T[3]; inline int calc() { int cnt = 0; for (int i = 0; i < 3; i++) cnt += (!st[i].empty()); if (cnt == 1) return n; else if (cnt == 2) { for (int i = 0; i < 3; i++) { if (!st[i].empty()) for (int j = i + 1; j < 3; j++) { if (!st[j].empty()) { if (beat[i][j] == 1) return st[i].size(); return st[j].size(); } } } return 114514; } int ans = 0; for (int i = 0; i < 3; i++) { int id = -1, id2; for (int j = 0; j < 3; j++) { if (beat[i][j] == 1) { id = j; break; } } id2 = 3 - i - id; int R1 = *st[id].rbegin(), L1 = *st[id].begin(); int R2 = *st[id2].rbegin(), L2 = *st[id2].begin(); ans += T[i].query(R1) - T[i].query(L1 - 1); ans += T[i].query(n) - T[i].query(max(R2, R1)); ans += T[i].query(min(L1, L2)); } return ans; } int main() { scanf("%d%d", &n, &q); scanf("%s", s + 1); for (int i = 1; i <= n; i++) a[i] = getid(s[i]); for (int i = 1; i <= n; i++) { st[a[i]].insert(i); T[a[i]].modify(i, 1); } printf("%d\n", calc()); while (q--) { char opt[5]; int p; scanf("%d%s", &p, opt); int x = getid(opt[0]); T[a[p]].modify(p, -1); st[a[p]].erase(p); a[p] = x; st[a[p]].insert(p); T[a[p]].modify(p, 1); printf("%d\n", calc()); } return 0; } ```
#include <bits/stdc++.h> char s[200005]; int bit[3][200005]; void change(int p, int v, int n, int bit[]) { while (p <= n) { bit[p] += v; p += p & -p; } } int ask(int p, int bit[]) { int res = 0; while (p != 0) { res += bit[p]; p -= p & -p; } return res; } int getId(char c) { if (c == 'R') return 0; else if (c == 'S') return 1; else return 2; } void exchange(int p, char c, int n) { int id = getId(s[p]); s[p] = c; change(p, -1, n, bit[id]); id = getId(s[p]); change(p, 1, n, bit[id]); } int getFirst(int tag, int n) { int L = 1, R = n; int res = -1; while (L <= R) { int M = (L + R) / 2; if (ask(M, bit[tag]) != 0) { res = M; R = M - 1; } else L = M + 1; } return res; } int getLast(int tag, int n) { int all = ask(n, bit[tag]); int L = 1, R = n; int res = -1; while (L <= R) { int M = (L + R) / 2; if (ask(M, bit[tag]) == all) { res = M; R = M - 1; } else L = M + 1; } return res; } int getSegment(int l, int r, int tag) { if (l > r) return 0; else return ask(r, bit[tag]) - ask(l - 1, bit[tag]); } int solve(int n) { int find = -1; for (int i = 0; i < 3; i++) { int total = ask(n, bit[i]); if (total == n) return n; if (total == 0) find = i; } if (find == -1) { int first[3] = {-1, -1, -1}; int last[3] = {-1, -1, -1}; for (int i = 0; i < 3; i++) { first[i] = getFirst(i, n), last[i] = getLast(i, n); } int ban = 0; for (int i = 0; i < 3; i++) { int weak = (i + 1) % 3, strong = (i + 2) % 3; ban += getSegment(first[strong], first[weak], i); ban += getSegment(last[weak], last[strong], i); } return n - ban; } else { if (find == 0) return ask(n, bit[1]); else if (find == 1) return ask(n, bit[2]); else return ask(n, bit[0]); } } int main() { int n, q; scanf("%d%d%s", &n, &q, s + 1); for (int i = 1; i <= n; i++) { if (s[i] == 'R') change(i, 1, n, bit[0]); else if (s[i] == 'S') change(i, 1, n, bit[1]); else change(i, 1, n, bit[2]); } printf("%d\n", solve(n)); for (int rnd = 1; rnd <= q; rnd++) { int p; char play[5]; scanf("%d%s", &p, play + 1); exchange(p, play[1], n); printf("%d\n", solve(n)); } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> char s[200005]; int bit[3][200005]; void change(int p, int v, int n, int bit[]) { while (p <= n) { bit[p] += v; p += p & -p; } } int ask(int p, int bit[]) { int res = 0; while (p != 0) { res += bit[p]; p -= p & -p; } return res; } int getId(char c) { if (c == 'R') return 0; else if (c == 'S') return 1; else return 2; } void exchange(int p, char c, int n) { int id = getId(s[p]); s[p] = c; change(p, -1, n, bit[id]); id = getId(s[p]); change(p, 1, n, bit[id]); } int getFirst(int tag, int n) { int L = 1, R = n; int res = -1; while (L <= R) { int M = (L + R) / 2; if (ask(M, bit[tag]) != 0) { res = M; R = M - 1; } else L = M + 1; } return res; } int getLast(int tag, int n) { int all = ask(n, bit[tag]); int L = 1, R = n; int res = -1; while (L <= R) { int M = (L + R) / 2; if (ask(M, bit[tag]) == all) { res = M; R = M - 1; } else L = M + 1; } return res; } int getSegment(int l, int r, int tag) { if (l > r) return 0; else return ask(r, bit[tag]) - ask(l - 1, bit[tag]); } int solve(int n) { int find = -1; for (int i = 0; i < 3; i++) { int total = ask(n, bit[i]); if (total == n) return n; if (total == 0) find = i; } if (find == -1) { int first[3] = {-1, -1, -1}; int last[3] = {-1, -1, -1}; for (int i = 0; i < 3; i++) { first[i] = getFirst(i, n), last[i] = getLast(i, n); } int ban = 0; for (int i = 0; i < 3; i++) { int weak = (i + 1) % 3, strong = (i + 2) % 3; ban += getSegment(first[strong], first[weak], i); ban += getSegment(last[weak], last[strong], i); } return n - ban; } else { if (find == 0) return ask(n, bit[1]); else if (find == 1) return ask(n, bit[2]); else return ask(n, bit[0]); } } int main() { int n, q; scanf("%d%d%s", &n, &q, s + 1); for (int i = 1; i <= n; i++) { if (s[i] == 'R') change(i, 1, n, bit[0]); else if (s[i] == 'S') change(i, 1, n, bit[1]); else change(i, 1, n, bit[2]); } printf("%d\n", solve(n)); for (int rnd = 1; rnd <= q; rnd++) { int p; char play[5]; scanf("%d%s", &p, play + 1); exchange(p, play[1], n); printf("%d\n", solve(n)); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct Update { long long i; string value; }; const long long N = (long long)2e5 + 7; long long n, q, a[N], ret[N], aib[N]; string s; vector<Update> updates; long long mod_pos[N], mod_val[N]; void add(long long i, long long x) { while (i <= n) { aib[i] += x; i += i & (-i); } } long long pre(long long i) { long long ret = 0; while (i) { ret += aib[i]; i -= i & (-i); } return ret; } void clr() { for (long long i = 1; i <= n; i++) { aib[i] = 0; } } long long sum(long long l, long long r) { if (l < 1) { l = 1; } if (r > n) { r = n; } if (l > r) { return 0; } return pre(r) - pre(l - 1); } void add(string zero, string poz, string neg) { clr(); set<long long> mic, mare; for (long long i = 1; i <= n; i++) { a[i] = 3; if (s[i - 1] == zero[0]) a[i] = 0; if (s[i - 1] == poz[0]) a[i] = +1; if (s[i - 1] == neg[0]) a[i] = -1; if (a[i] == -1) { mic.insert(i); } if (a[i] == +1) { mare.insert(i); } assert(a[i] != 3); if (a[i] == 0) { add(i, +1); } } for (long long i = 1; i <= q; i++) { mod_pos[i] = updates[i - 1].i; mod_val[i] = 3; if (updates[i - 1].value == zero) mod_val[i] = 0; if (updates[i - 1].value == poz) mod_val[i] = +1; if (updates[i - 1].value == neg) mod_val[i] = -1; assert(mod_val[i] != 3); } for (long long it = 0; it <= q; it++) { if (it) { if (a[mod_pos[it]] == 0) add(mod_pos[it], -1); if (a[mod_pos[it]] == -1) { mic.erase(mod_pos[it]); } if (a[mod_pos[it]] == +1) { mare.erase(mod_pos[it]); } a[mod_pos[it]] = mod_val[it]; if (a[mod_pos[it]] == -1) { mic.insert(mod_pos[it]); } if (a[mod_pos[it]] == +1) { mare.insert(mod_pos[it]); } if (a[mod_pos[it]] == 0) { add(mod_pos[it], +1); } } long long pre1 = 0, suf1 = 0, pre2 = 0, suf2 = 0; long long first_mic = -1, first_mare = -1; if (!mic.empty()) { first_mic = *mic.begin(); } if (!mare.empty()) { first_mare = *mare.begin(); } if (first_mic == -1) { pre1 = n; suf1 = n + 1; } else { if (first_mare == -1) { pre1 = first_mic - 1; suf1 = n + 1; } else { if (first_mare <= first_mic) { pre1 = n; suf1 = n + 1; } else { pre1 = first_mic - 1; suf1 = first_mare; } } } long long last_mic = -1, last_mare = -1; if (!mic.empty()) { auto it = mic.end(); it--; last_mic = *it; } if (!mare.empty()) { auto it = mare.end(); it--; last_mare = *it; } if (last_mic == -1) { pre2 = 0; suf2 = 1; } else { if (last_mare == -1) { suf2 = last_mic + 1; pre2 = 0; } else { if (last_mic <= last_mare) { pre2 = 0; suf2 = 1; } else { suf2 = last_mic + 1; pre2 = last_mare; } } } assert(pre1 < suf1); assert(pre2 < suf2); ret[it] += sum(1, min(pre1, pre2)); ret[it] += sum(suf1, pre2); ret[it] += sum(max(suf1, suf2), n); ret[it] += sum(suf2, pre1); } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> q >> s; updates.resize(q); for (auto &it : updates) { cin >> it.i >> it.value; } add("R", "S", "P"); add("S", "P", "R"); add("P", "R", "S"); for (long long i = 0; i <= q; i++) { cout << ret[i] << "\n"; } }
### Prompt Your challenge is to write a cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Update { long long i; string value; }; const long long N = (long long)2e5 + 7; long long n, q, a[N], ret[N], aib[N]; string s; vector<Update> updates; long long mod_pos[N], mod_val[N]; void add(long long i, long long x) { while (i <= n) { aib[i] += x; i += i & (-i); } } long long pre(long long i) { long long ret = 0; while (i) { ret += aib[i]; i -= i & (-i); } return ret; } void clr() { for (long long i = 1; i <= n; i++) { aib[i] = 0; } } long long sum(long long l, long long r) { if (l < 1) { l = 1; } if (r > n) { r = n; } if (l > r) { return 0; } return pre(r) - pre(l - 1); } void add(string zero, string poz, string neg) { clr(); set<long long> mic, mare; for (long long i = 1; i <= n; i++) { a[i] = 3; if (s[i - 1] == zero[0]) a[i] = 0; if (s[i - 1] == poz[0]) a[i] = +1; if (s[i - 1] == neg[0]) a[i] = -1; if (a[i] == -1) { mic.insert(i); } if (a[i] == +1) { mare.insert(i); } assert(a[i] != 3); if (a[i] == 0) { add(i, +1); } } for (long long i = 1; i <= q; i++) { mod_pos[i] = updates[i - 1].i; mod_val[i] = 3; if (updates[i - 1].value == zero) mod_val[i] = 0; if (updates[i - 1].value == poz) mod_val[i] = +1; if (updates[i - 1].value == neg) mod_val[i] = -1; assert(mod_val[i] != 3); } for (long long it = 0; it <= q; it++) { if (it) { if (a[mod_pos[it]] == 0) add(mod_pos[it], -1); if (a[mod_pos[it]] == -1) { mic.erase(mod_pos[it]); } if (a[mod_pos[it]] == +1) { mare.erase(mod_pos[it]); } a[mod_pos[it]] = mod_val[it]; if (a[mod_pos[it]] == -1) { mic.insert(mod_pos[it]); } if (a[mod_pos[it]] == +1) { mare.insert(mod_pos[it]); } if (a[mod_pos[it]] == 0) { add(mod_pos[it], +1); } } long long pre1 = 0, suf1 = 0, pre2 = 0, suf2 = 0; long long first_mic = -1, first_mare = -1; if (!mic.empty()) { first_mic = *mic.begin(); } if (!mare.empty()) { first_mare = *mare.begin(); } if (first_mic == -1) { pre1 = n; suf1 = n + 1; } else { if (first_mare == -1) { pre1 = first_mic - 1; suf1 = n + 1; } else { if (first_mare <= first_mic) { pre1 = n; suf1 = n + 1; } else { pre1 = first_mic - 1; suf1 = first_mare; } } } long long last_mic = -1, last_mare = -1; if (!mic.empty()) { auto it = mic.end(); it--; last_mic = *it; } if (!mare.empty()) { auto it = mare.end(); it--; last_mare = *it; } if (last_mic == -1) { pre2 = 0; suf2 = 1; } else { if (last_mare == -1) { suf2 = last_mic + 1; pre2 = 0; } else { if (last_mic <= last_mare) { pre2 = 0; suf2 = 1; } else { suf2 = last_mic + 1; pre2 = last_mare; } } } assert(pre1 < suf1); assert(pre2 < suf2); ret[it] += sum(1, min(pre1, pre2)); ret[it] += sum(suf1, pre2); ret[it] += sum(max(suf1, suf2), n); ret[it] += sum(suf2, pre1); } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> q >> s; updates.resize(q); for (auto &it : updates) { cin >> it.i >> it.value; } add("R", "S", "P"); add("S", "P", "R"); add("P", "R", "S"); for (long long i = 0; i <= q; i++) { cout << ret[i] << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; template <typename T> void out(T x) { cout << x << endl; exit(0); } using ll = long long; const int maxn = 2e5 + 10; int n, q; int conv(char c) { string s = "RPS"; for (int i = 0; i < 3; i++) { if (s[i] == c) return i; } assert(false); return -1; } int a[maxn]; set<int> ind[3]; int bit[3][maxn]; void upd(int t, int i, int dx) { for (; i < maxn; i += i & -i) { bit[t][i] += dx; } } int qry(int t, int i) { int res = 0; for (; i > 0; i -= i & -i) { res += bit[t][i]; } return res; } int qry(int t, int l, int r) { if (l > r) return 0; return qry(t, r) - qry(t, l - 1); } int solve() { int res = 0; for (int t = 0; t < 3; t++) { int pal = (t + 2) % 3; int foe = (t + 1) % 3; int cur = ind[t].size(); if (ind[pal].size() && ind[foe].size()) { int pal0 = *ind[pal].begin(); int paln = *ind[pal].rbegin(); int foe0 = *ind[foe].begin(); int foen = *ind[foe].rbegin(); cur -= qry(t, foe0, pal0); cur -= qry(t, paln, foen); } else if (ind[foe].size()) { res -= ind[t].size(); } res += cur; } return res; } void print() { for (int t = 0; t < 3; t++) { cout << ("t") << " is " << (t) << endl; for (int i : ind[t]) cout << i; cout << endl; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> q; string s; cin >> s; s = "*" + s; for (int i = 1; i <= n; i++) { a[i] = conv(s[i]); ind[a[i]].insert(i); upd(a[i], i, 1); } cout << solve() << "\n"; while (q--) { int i; char c; cin >> i >> c; int t = a[i]; upd(t, i, -1); ind[t].erase(i); a[i] = t = conv(c); upd(t, i, +1); ind[t].insert(i); cout << solve() << "\n"; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> void out(T x) { cout << x << endl; exit(0); } using ll = long long; const int maxn = 2e5 + 10; int n, q; int conv(char c) { string s = "RPS"; for (int i = 0; i < 3; i++) { if (s[i] == c) return i; } assert(false); return -1; } int a[maxn]; set<int> ind[3]; int bit[3][maxn]; void upd(int t, int i, int dx) { for (; i < maxn; i += i & -i) { bit[t][i] += dx; } } int qry(int t, int i) { int res = 0; for (; i > 0; i -= i & -i) { res += bit[t][i]; } return res; } int qry(int t, int l, int r) { if (l > r) return 0; return qry(t, r) - qry(t, l - 1); } int solve() { int res = 0; for (int t = 0; t < 3; t++) { int pal = (t + 2) % 3; int foe = (t + 1) % 3; int cur = ind[t].size(); if (ind[pal].size() && ind[foe].size()) { int pal0 = *ind[pal].begin(); int paln = *ind[pal].rbegin(); int foe0 = *ind[foe].begin(); int foen = *ind[foe].rbegin(); cur -= qry(t, foe0, pal0); cur -= qry(t, paln, foen); } else if (ind[foe].size()) { res -= ind[t].size(); } res += cur; } return res; } void print() { for (int t = 0; t < 3; t++) { cout << ("t") << " is " << (t) << endl; for (int i : ind[t]) cout << i; cout << endl; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> q; string s; cin >> s; s = "*" + s; for (int i = 1; i <= n; i++) { a[i] = conv(s[i]); ind[a[i]].insert(i); upd(a[i], i, 1); } cout << solve() << "\n"; while (q--) { int i; char c; cin >> i >> c; int t = a[i]; upd(t, i, -1); ind[t].erase(i); a[i] = t = conv(c); upd(t, i, +1); ind[t].insert(i); cout << solve() << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> res; class segtree { public: struct node { int r = 0, s = 0, p = 0; void apply(char u) { r = 0, s = 0, p = 0; if (u == 'R') r = 1; if (u == 'P') p = 1; if (u == 'S') s = 1; } }; node unite(const node &a, const node &b) { node res; res.r = a.r + b.r; res.s = a.s + b.s; res.p = a.p + b.p; return res; } inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); } int n; vector<node> tree; template <typename M> void build(int x, int l, int r, const vector<M> &v) { if (l == r) { tree[x].apply(v[l]); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); build(x + 1, l, y, v); build(z, y + 1, r, v); pull(x, z); } node get(int x, int l, int r, int ll, int rr) { if (l >= ll && rr >= r) { return tree[x]; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); node res{}; if (rr <= y) { res = get(x + 1, l, y, ll, rr); } else { if (ll > y) { res = get(z, y + 1, r, ll, rr); } else { res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr)); } } return res; } node get(int l, int r) { assert(0 <= l && l <= r && r <= n - 1); return get(0, 0, n - 1, l, r); } void modify(int x, int l, int r, int p, char u) { if (l == r) { tree[x].apply(u); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); if (p <= y) { modify(x + 1, l, y, p, u); } else { modify(z, y + 1, r, p, u); } pull(x, z); } template <typename M> segtree(const vector<M> &v) { n = v.size(); assert(n > 0); tree.resize(2 * n - 1); build(0, 0, n - 1, v); } }; set<int> rock, paper, scissor; int n, q; string tour; vector<char> har; char in[200005]; int main() { scanf("%d%d", &n, &q); scanf("%s", &in); tour = in; for (int i = 0; i < n; i++) har.push_back(tour[i]); for (int i = 0; i < n; i++) { if (tour[i] == 'R') rock.insert(i); if (tour[i] == 'P') paper.insert(i); if (tour[i] == 'S') scissor.insert(i); } segtree rev(har); auto calc = [&]() { int res = 0; { if (!scissor.size() && !paper.size()) res += n; if (scissor.size() > 0) { if (paper.size() > 0) { int u = *scissor.begin(), v = *scissor.rbegin(), s = *paper.begin(), t = *paper.rbegin(); res += rev.get(0, min(u, s)).r + rev.get(max(v, t), n - 1).r + rev.get(u, v).r; } else res += n - scissor.size(); } } { if (!paper.size() && !rock.size()) res += n; if (paper.size() > 0) { if (rock.size() > 0) { int u = *paper.begin(), v = *paper.rbegin(), s = *rock.begin(), t = *rock.rbegin(); res += rev.get(0, min(u, s)).s + rev.get(max(v, t), n - 1).s + rev.get(u, v).s; } else res += n - paper.size(); } } { if (!rock.size() && !scissor.size()) res += n; if (rock.size() > 0) { if (scissor.size() > 0) { int u = *rock.begin(), v = *rock.rbegin(), s = *scissor.begin(), t = *scissor.rbegin(); res += rev.get(0, min(u, s)).p + rev.get(max(v, t), n - 1).p + rev.get(u, v).p; } else res += n - rock.size(); } } return res; }; res.push_back(calc()); while (q--) { int i; char u; scanf("%d", &i); scanf(" %c", &u); i--; if (tour[i] == 'R') rock.erase(i); if (tour[i] == 'P') paper.erase(i); if (tour[i] == 'S') scissor.erase(i); tour[i] = u; rev.modify(0, 0, n - 1, i, u); if (u == 'R') rock.insert(i); if (u == 'P') paper.insert(i); if (u == 'S') scissor.insert(i); res.push_back(calc()); } for (auto i : res) cout << i << endl; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> res; class segtree { public: struct node { int r = 0, s = 0, p = 0; void apply(char u) { r = 0, s = 0, p = 0; if (u == 'R') r = 1; if (u == 'P') p = 1; if (u == 'S') s = 1; } }; node unite(const node &a, const node &b) { node res; res.r = a.r + b.r; res.s = a.s + b.s; res.p = a.p + b.p; return res; } inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); } int n; vector<node> tree; template <typename M> void build(int x, int l, int r, const vector<M> &v) { if (l == r) { tree[x].apply(v[l]); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); build(x + 1, l, y, v); build(z, y + 1, r, v); pull(x, z); } node get(int x, int l, int r, int ll, int rr) { if (l >= ll && rr >= r) { return tree[x]; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); node res{}; if (rr <= y) { res = get(x + 1, l, y, ll, rr); } else { if (ll > y) { res = get(z, y + 1, r, ll, rr); } else { res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr)); } } return res; } node get(int l, int r) { assert(0 <= l && l <= r && r <= n - 1); return get(0, 0, n - 1, l, r); } void modify(int x, int l, int r, int p, char u) { if (l == r) { tree[x].apply(u); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); if (p <= y) { modify(x + 1, l, y, p, u); } else { modify(z, y + 1, r, p, u); } pull(x, z); } template <typename M> segtree(const vector<M> &v) { n = v.size(); assert(n > 0); tree.resize(2 * n - 1); build(0, 0, n - 1, v); } }; set<int> rock, paper, scissor; int n, q; string tour; vector<char> har; char in[200005]; int main() { scanf("%d%d", &n, &q); scanf("%s", &in); tour = in; for (int i = 0; i < n; i++) har.push_back(tour[i]); for (int i = 0; i < n; i++) { if (tour[i] == 'R') rock.insert(i); if (tour[i] == 'P') paper.insert(i); if (tour[i] == 'S') scissor.insert(i); } segtree rev(har); auto calc = [&]() { int res = 0; { if (!scissor.size() && !paper.size()) res += n; if (scissor.size() > 0) { if (paper.size() > 0) { int u = *scissor.begin(), v = *scissor.rbegin(), s = *paper.begin(), t = *paper.rbegin(); res += rev.get(0, min(u, s)).r + rev.get(max(v, t), n - 1).r + rev.get(u, v).r; } else res += n - scissor.size(); } } { if (!paper.size() && !rock.size()) res += n; if (paper.size() > 0) { if (rock.size() > 0) { int u = *paper.begin(), v = *paper.rbegin(), s = *rock.begin(), t = *rock.rbegin(); res += rev.get(0, min(u, s)).s + rev.get(max(v, t), n - 1).s + rev.get(u, v).s; } else res += n - paper.size(); } } { if (!rock.size() && !scissor.size()) res += n; if (rock.size() > 0) { if (scissor.size() > 0) { int u = *rock.begin(), v = *rock.rbegin(), s = *scissor.begin(), t = *scissor.rbegin(); res += rev.get(0, min(u, s)).p + rev.get(max(v, t), n - 1).p + rev.get(u, v).p; } else res += n - rock.size(); } } return res; }; res.push_back(calc()); while (q--) { int i; char u; scanf("%d", &i); scanf(" %c", &u); i--; if (tour[i] == 'R') rock.erase(i); if (tour[i] == 'P') paper.erase(i); if (tour[i] == 'S') scissor.erase(i); tour[i] = u; rev.modify(0, 0, n - 1, i, u); if (u == 'R') rock.insert(i); if (u == 'P') paper.insert(i); if (u == 'S') scissor.insert(i); res.push_back(calc()); } for (auto i : res) cout << i << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; char s[200005], to[128], ch; set<int> p[3]; set<int>::iterator it; int c[3][200005], a, n, q, i; void add(int x, int y, int v) { while (y < 200005) c[x][y] += v, y += (y & (-y)); } int sum(int x, int y) { int ret = 0; while (y) ret += c[x][y], y -= (y & (-y)); return ret; } int F(int x, int y, int z) { if (!p[x].size()) return 0; if (!p[y].size()) return sum(x, n); if (!p[z].size()) return 0; int ret = sum(x, n), f1 = *p[y].begin(), f2 = *p[z].begin(); if (f2 > f1) ret -= sum(x, f2) - sum(x, f1); it = p[y].end(), f1 = *--it; it = p[z].end(), f2 = *--it; if (f1 > f2) ret -= sum(x, f1) - sum(x, f2); return ret; } int cal() { return F(0, 2, 1) + F(1, 0, 2) + F(2, 1, 0); } int main() { scanf("%d%d%s", &n, &q, s + 1); to['R'] = 1; to['S'] = 2; for (i = 1; i <= n; ++i) p[to[s[i]]].insert(i), add(to[s[i]], i, 1); cout << cal() << "\n"; while (q--) { cin >> a >> ch; add(to[s[a]], a, -1); p[to[s[a]]].erase(a); s[a] = ch; add(to[s[a]], a, 1); p[to[s[a]]].insert(a); cout << cal() << "\n"; } }
### Prompt Generate a cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[200005], to[128], ch; set<int> p[3]; set<int>::iterator it; int c[3][200005], a, n, q, i; void add(int x, int y, int v) { while (y < 200005) c[x][y] += v, y += (y & (-y)); } int sum(int x, int y) { int ret = 0; while (y) ret += c[x][y], y -= (y & (-y)); return ret; } int F(int x, int y, int z) { if (!p[x].size()) return 0; if (!p[y].size()) return sum(x, n); if (!p[z].size()) return 0; int ret = sum(x, n), f1 = *p[y].begin(), f2 = *p[z].begin(); if (f2 > f1) ret -= sum(x, f2) - sum(x, f1); it = p[y].end(), f1 = *--it; it = p[z].end(), f2 = *--it; if (f1 > f2) ret -= sum(x, f1) - sum(x, f2); return ret; } int cal() { return F(0, 2, 1) + F(1, 0, 2) + F(2, 1, 0); } int main() { scanf("%d%d%s", &n, &q, s + 1); to['R'] = 1; to['S'] = 2; for (i = 1; i <= n; ++i) p[to[s[i]]].insert(i), add(to[s[i]], i, 1); cout << cal() << "\n"; while (q--) { cin >> a >> ch; add(to[s[a]], a, -1); p[to[s[a]]].erase(a); s[a] = ch; add(to[s[a]], a, 1); p[to[s[a]]].insert(a); cout << cal() << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; char c[maxn]; set<int> s[3]; int sum[3][maxn], N; int id(char p) { if (p == 'R') return 0; if (p == 'S') return 1; return 2; } void add(int opt, int x, int val) { for (; x <= N; x += (-x) & x) sum[opt][x] += val; } int query(int opt, int x) { int res = 0; for (; x; x -= (-x) & x) res += sum[opt][x]; return res; } int cal(int p) { int lat = (p + 2) % 3, pre = (p + 1) % 3; if (s[lat].empty()) return query(p, N); if (s[pre].empty()) return 0; return query(p, N) - query(p, max(*s[pre].rbegin(), *s[lat].rbegin())) + query(p, min(*s[pre].begin(), *s[lat].begin())) + query(p, *s[pre].rbegin()) - query(p, *s[pre].begin()); } int main() { int M, pos, ans, p; char cc[3]; scanf("%d%d%s", &N, &M, c + 1); for (int i = 1; i <= N; i++) { p = id(c[i]); s[p].insert(i); add(p, i, 1); } ans = cal(0) + cal(1) + cal(2); printf("%d\n", ans); for (int i = 1; i <= M; i++) { scanf("%d%s", &pos, cc + 1); if (c[pos] == cc[1]) { printf("%d\n", ans); continue; } p = id(c[pos]); s[p].erase(pos); add(p, pos, -1); c[pos] = cc[1]; p = id(c[pos]); s[p].insert(pos); add(p, pos, 1); ans = cal(0) + cal(1) + cal(2); printf("%d\n", ans); } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 200010; char c[maxn]; set<int> s[3]; int sum[3][maxn], N; int id(char p) { if (p == 'R') return 0; if (p == 'S') return 1; return 2; } void add(int opt, int x, int val) { for (; x <= N; x += (-x) & x) sum[opt][x] += val; } int query(int opt, int x) { int res = 0; for (; x; x -= (-x) & x) res += sum[opt][x]; return res; } int cal(int p) { int lat = (p + 2) % 3, pre = (p + 1) % 3; if (s[lat].empty()) return query(p, N); if (s[pre].empty()) return 0; return query(p, N) - query(p, max(*s[pre].rbegin(), *s[lat].rbegin())) + query(p, min(*s[pre].begin(), *s[lat].begin())) + query(p, *s[pre].rbegin()) - query(p, *s[pre].begin()); } int main() { int M, pos, ans, p; char cc[3]; scanf("%d%d%s", &N, &M, c + 1); for (int i = 1; i <= N; i++) { p = id(c[i]); s[p].insert(i); add(p, i, 1); } ans = cal(0) + cal(1) + cal(2); printf("%d\n", ans); for (int i = 1; i <= M; i++) { scanf("%d%s", &pos, cc + 1); if (c[pos] == cc[1]) { printf("%d\n", ans); continue; } p = id(c[pos]); s[p].erase(pos); add(p, pos, -1); c[pos] = cc[1]; p = id(c[pos]); s[p].insert(pos); add(p, pos, 1); ans = cal(0) + cal(1) + cal(2); printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; char s[200005], to[128], ch; set<int> p[3]; set<int>::iterator it; int c[3][200005], a, n, q, i; void add(int x, int y, int v) { while (y < 200005) c[x][y] += v, y += (y & (-y)); } int sum(int x, int y) { int ret = 0; while (y) ret += c[x][y], y -= (y & (-y)); return ret; } int F(int x, int y, int z) { if (!p[x].size()) return 0; if (!p[y].size()) return sum(x, n); if (!p[z].size()) return 0; int ret = sum(x, n), f1 = *p[y].begin(), f2 = *p[z].begin(); if (f2 > f1) ret -= sum(x, f2) - sum(x, f1); it = p[y].end(), f1 = *--it; it = p[z].end(), f2 = *--it; if (f1 > f2) ret -= sum(x, f1) - sum(x, f2); return ret; } int cal() { return F(0, 2, 1) + F(1, 0, 2) + F(2, 1, 0); } int main() { scanf("%d%d%s", &n, &q, s + 1); to['R'] = 1; to['S'] = 2; for (i = 1; i <= n; ++i) p[to[s[i]]].insert(i), add(to[s[i]], i, 1); cout << cal() << "\n"; while (q--) { cin >> a >> ch; add(to[s[a]], a, -1); p[to[s[a]]].erase(a); s[a] = ch; add(to[s[a]], a, 1); p[to[s[a]]].insert(a); cout << cal() << "\n"; } }
### Prompt Please provide a Cpp coded solution to the problem described below: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[200005], to[128], ch; set<int> p[3]; set<int>::iterator it; int c[3][200005], a, n, q, i; void add(int x, int y, int v) { while (y < 200005) c[x][y] += v, y += (y & (-y)); } int sum(int x, int y) { int ret = 0; while (y) ret += c[x][y], y -= (y & (-y)); return ret; } int F(int x, int y, int z) { if (!p[x].size()) return 0; if (!p[y].size()) return sum(x, n); if (!p[z].size()) return 0; int ret = sum(x, n), f1 = *p[y].begin(), f2 = *p[z].begin(); if (f2 > f1) ret -= sum(x, f2) - sum(x, f1); it = p[y].end(), f1 = *--it; it = p[z].end(), f2 = *--it; if (f1 > f2) ret -= sum(x, f1) - sum(x, f2); return ret; } int cal() { return F(0, 2, 1) + F(1, 0, 2) + F(2, 1, 0); } int main() { scanf("%d%d%s", &n, &q, s + 1); to['R'] = 1; to['S'] = 2; for (i = 1; i <= n; ++i) p[to[s[i]]].insert(i), add(to[s[i]], i, 1); cout << cal() << "\n"; while (q--) { cin >> a >> ch; add(to[s[a]], a, -1); p[to[s[a]]].erase(a); s[a] = ch; add(to[s[a]], a, 1); p[to[s[a]]].insert(a); cout << cal() << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "URDL"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 998244353) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 998244353) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } string S = "RPS"; int gid(char x) { for (int(i) = 0; (i) < (int)(3); (i)++) if (S[i] == x) return i; return -1; } struct fw { int b[200135]; void update(int x, int v) { for (int i = x; i <= n; i += i & -i) { b[i] += v; } } int query(int x) { int ret = 0; for (; x > 0; ret += b[x], x -= x & -x) ; return ret; } int qy(int l, int r) { return query(r) - query(l - 1); } } t[3]; char s[200135]; set<int> sp[3]; int ppt() { for (int(i) = 0; (i) < (int)(3); (i)++) if (sp[i].size() == n) return n; if (sp[0].empty()) return sp[2].size(); if (sp[1].empty()) return sp[0].size(); if (sp[2].empty()) return sp[1].size(); int r0 = *sp[0].begin(), r1 = *sp[0].rbegin(); int p0 = *sp[1].begin(), p1 = *sp[1].rbegin(); int s0 = *sp[2].begin(), s1 = *sp[2].rbegin(); int ans = n; if (p0 < s0) ans -= t[0].qy(p0, s0); if (s1 < p1) ans -= t[0].qy(s1, p1); if (s0 < r0) ans -= t[1].qy(s0, r0); if (r1 < s1) ans -= t[1].qy(r1, s1); if (r0 < p0) ans -= t[2].qy(r0, p0); if (p1 < r1) ans -= t[2].qy(p1, r1); return ans; } void fmain(int tid) { scanf("%d%d", &n, &m); scanf("%s", s + 1); for (int(i) = 1; (i) <= (int)(n); (i)++) { int j = gid(s[i]); t[j].update(i, 1); sp[j].insert(i); } printf("%d\n", ppt()); char ope[3]; for (int(i) = 1; (i) <= (int)(m); (i)++) { scanf("%d%s", &k, ope); int j = gid(s[k]); t[j].update(k, -1); sp[j].erase(k); s[k] = ope[0]; j = gid(s[k]); t[j].update(k, 1); sp[j].insert(k); printf("%d\n", ppt()); } } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
### Prompt Create a solution in Cpp for the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "URDL"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 998244353) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 998244353) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } string S = "RPS"; int gid(char x) { for (int(i) = 0; (i) < (int)(3); (i)++) if (S[i] == x) return i; return -1; } struct fw { int b[200135]; void update(int x, int v) { for (int i = x; i <= n; i += i & -i) { b[i] += v; } } int query(int x) { int ret = 0; for (; x > 0; ret += b[x], x -= x & -x) ; return ret; } int qy(int l, int r) { return query(r) - query(l - 1); } } t[3]; char s[200135]; set<int> sp[3]; int ppt() { for (int(i) = 0; (i) < (int)(3); (i)++) if (sp[i].size() == n) return n; if (sp[0].empty()) return sp[2].size(); if (sp[1].empty()) return sp[0].size(); if (sp[2].empty()) return sp[1].size(); int r0 = *sp[0].begin(), r1 = *sp[0].rbegin(); int p0 = *sp[1].begin(), p1 = *sp[1].rbegin(); int s0 = *sp[2].begin(), s1 = *sp[2].rbegin(); int ans = n; if (p0 < s0) ans -= t[0].qy(p0, s0); if (s1 < p1) ans -= t[0].qy(s1, p1); if (s0 < r0) ans -= t[1].qy(s0, r0); if (r1 < s1) ans -= t[1].qy(r1, s1); if (r0 < p0) ans -= t[2].qy(r0, p0); if (p1 < r1) ans -= t[2].qy(p1, r1); return ans; } void fmain(int tid) { scanf("%d%d", &n, &m); scanf("%s", s + 1); for (int(i) = 1; (i) <= (int)(n); (i)++) { int j = gid(s[i]); t[j].update(i, 1); sp[j].insert(i); } printf("%d\n", ppt()); char ope[3]; for (int(i) = 1; (i) <= (int)(m); (i)++) { scanf("%d%s", &k, ope); int j = gid(s[k]); t[j].update(k, -1); sp[j].erase(k); s[k] = ope[0]; j = gid(s[k]); t[j].update(k, 1); sp[j].insert(k); printf("%d\n", ppt()); } } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename TYPE> inline void chkmax(TYPE &first, TYPE second) { first < second ? first = second : 0; } template <typename TYPE> inline void chkmin(TYPE &first, TYPE second) { second < first ? first = second : 0; } template <typename TYPE> void readint(TYPE &first) { first = 0; int f = 1; char c; for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) first = first * 10 + c - '0'; first *= f; } const int MAXN = 200005; int n; char str[MAXN]; int toggle(char c) { if (c == 'R') return 0; if (c == 'S') return 1; return 2; } int a[MAXN]; set<int> st[3], no[3]; int d[3][MAXN]; void add(int o, int first, int second) { for (; first <= n; first += (first & -first)) d[o][first] += second; } int query(int o, int first) { int s = 0; for (; first; first -= (first & -first)) s += d[o][first]; return s; } int work() { int ans = 0; for (int j = 0; j < 3; ++j) { if (st[j].empty()) continue; if ((int)st[j].size() == n) { ans = n; break; } int k = (j + 1) % 3; if (st[k].empty()) continue; int l = *st[k].begin(); set<int>::iterator it = st[k].end(); --it; int r = *it; ans += query(j, r) - query(j, l - 1); ans += *no[j].begin() - 1; it = no[j].end(); --it, --it; ans += n - *it; } return ans; } int main() { int Q, first, c; readint(n), readint(Q); scanf("%s", str + 1); for (int j = 0; j < 3; ++j) for (int i = 1; i <= n + 1; ++i) no[j].insert(i); for (int i = 1; i <= n; ++i) { a[i] = toggle(str[i]); st[a[i]].insert(i); no[a[i]].erase(i); add(a[i], i, 1); } printf("%d\n", work()); while (Q--) { readint(first); scanf("%s", str); c = toggle(str[0]); st[a[first]].erase(first); no[a[first]].insert(first); add(a[first], first, -1); st[c].insert(first); no[c].erase(first); add(c, first, 1); a[first] = c; printf("%d\n", work()); } return 0; }
### Prompt Please create a solution in Cpp to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename TYPE> inline void chkmax(TYPE &first, TYPE second) { first < second ? first = second : 0; } template <typename TYPE> inline void chkmin(TYPE &first, TYPE second) { second < first ? first = second : 0; } template <typename TYPE> void readint(TYPE &first) { first = 0; int f = 1; char c; for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) first = first * 10 + c - '0'; first *= f; } const int MAXN = 200005; int n; char str[MAXN]; int toggle(char c) { if (c == 'R') return 0; if (c == 'S') return 1; return 2; } int a[MAXN]; set<int> st[3], no[3]; int d[3][MAXN]; void add(int o, int first, int second) { for (; first <= n; first += (first & -first)) d[o][first] += second; } int query(int o, int first) { int s = 0; for (; first; first -= (first & -first)) s += d[o][first]; return s; } int work() { int ans = 0; for (int j = 0; j < 3; ++j) { if (st[j].empty()) continue; if ((int)st[j].size() == n) { ans = n; break; } int k = (j + 1) % 3; if (st[k].empty()) continue; int l = *st[k].begin(); set<int>::iterator it = st[k].end(); --it; int r = *it; ans += query(j, r) - query(j, l - 1); ans += *no[j].begin() - 1; it = no[j].end(); --it, --it; ans += n - *it; } return ans; } int main() { int Q, first, c; readint(n), readint(Q); scanf("%s", str + 1); for (int j = 0; j < 3; ++j) for (int i = 1; i <= n + 1; ++i) no[j].insert(i); for (int i = 1; i <= n; ++i) { a[i] = toggle(str[i]); st[a[i]].insert(i); no[a[i]].erase(i); add(a[i], i, 1); } printf("%d\n", work()); while (Q--) { readint(first); scanf("%s", str); c = toggle(str[0]); st[a[first]].erase(first); no[a[first]].insert(first); add(a[first], first, -1); st[c].insert(first); no[c].erase(first); add(c, first, 1); a[first] = c; printf("%d\n", work()); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int bit[200001][4], cd, q, code[300], k[4]; string s; multiset<int> ruler[4]; void update(int type, int pos, int x) { while (pos <= cd) bit[pos][type] += x, pos += pos & -pos; } int sum(int type, int pos) { int kq = 0; while (pos > 0) kq += bit[pos][type], pos -= pos & -pos; return kq; } int getsum(int type, int l, int r) { return sum(type, r) - sum(type, l - 1); } int get(int a) { int need = k[a]; if (ruler[need].size() == 0) { if (ruler[a].size() == cd) return cd; return 0; } int l = *ruler[need].begin(), r = *ruler[need].rbegin(); int kq = getsum(a, l, r); if (code[s[1]] == a) { int i = 1, e = cd, pos; while (e >= i) { int mid = (i + e) / 2; if (getsum(a, 1, mid) == mid) pos = mid, i = mid + 1; else e = mid - 1; } kq += pos; } if (code[s[cd]] == a) { int i = 1, e = cd, pos; while (e >= i) { int mid = (i + e) / 2; if (getsum(a, mid, cd) == cd - mid + 1) pos = mid, e = mid - 1; else i = mid + 1; } kq += (cd - pos + 1); } return kq; } void inp() { cin >> cd >> q; cin >> s; s = '0' + s; code['R'] = 1; code['S'] = 2; code['P'] = 3; k[1] = 2, k[2] = 3, k[3] = 1; for (int i = 1; i <= cd; i++) ruler[code[s[i]]].insert(i), update(code[s[i]], i, 1); } void do_it() { int a; char c; cout << get(1) + get(2) + get(3) << '\n'; for (int i = 1; i <= q; i++) { cin >> a >> c; ruler[code[s[a]]].erase(a); update(code[s[a]], a, -1); ruler[code[c]].insert(a); update(code[c], a, 1); s[a] = c; cout << get(1) + get(2) + get(3) << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); inp(); do_it(); }
### Prompt Please create a solution in cpp to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int bit[200001][4], cd, q, code[300], k[4]; string s; multiset<int> ruler[4]; void update(int type, int pos, int x) { while (pos <= cd) bit[pos][type] += x, pos += pos & -pos; } int sum(int type, int pos) { int kq = 0; while (pos > 0) kq += bit[pos][type], pos -= pos & -pos; return kq; } int getsum(int type, int l, int r) { return sum(type, r) - sum(type, l - 1); } int get(int a) { int need = k[a]; if (ruler[need].size() == 0) { if (ruler[a].size() == cd) return cd; return 0; } int l = *ruler[need].begin(), r = *ruler[need].rbegin(); int kq = getsum(a, l, r); if (code[s[1]] == a) { int i = 1, e = cd, pos; while (e >= i) { int mid = (i + e) / 2; if (getsum(a, 1, mid) == mid) pos = mid, i = mid + 1; else e = mid - 1; } kq += pos; } if (code[s[cd]] == a) { int i = 1, e = cd, pos; while (e >= i) { int mid = (i + e) / 2; if (getsum(a, mid, cd) == cd - mid + 1) pos = mid, e = mid - 1; else i = mid + 1; } kq += (cd - pos + 1); } return kq; } void inp() { cin >> cd >> q; cin >> s; s = '0' + s; code['R'] = 1; code['S'] = 2; code['P'] = 3; k[1] = 2, k[2] = 3, k[3] = 1; for (int i = 1; i <= cd; i++) ruler[code[s[i]]].insert(i), update(code[s[i]], i, 1); } void do_it() { int a; char c; cout << get(1) + get(2) + get(3) << '\n'; for (int i = 1; i <= q; i++) { cin >> a >> c; ruler[code[s[a]]].erase(a); update(code[s[a]], a, -1); ruler[code[c]].insert(a); update(code[c], a, 1); s[a] = c; cout << get(1) + get(2) + get(3) << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); inp(); do_it(); } ```
#include <bits/stdc++.h> const long long N = 216333, e7 = 1e7 + 7, inf = 1e17; typedef long long aN[N]; long long readll() { long long x = 0, w = 1; int c = getchar(); for (; c < '0' || c > '9'; c - '-' || (w = -w), c = getchar()) ; for (; c >= '0' && c <= '9'; x = x * 10 + (c ^ 48), c = getchar()) ; return x * w; } int readchar(int l = '0', int r = 'z') { int c = getchar(); for (; c < l || c > r; c = getchar()) ; return c; } struct qj { long long l, r; }; qj operator&(const qj& a, const qj& b) { return (qj){std::max(a.l, b.l), std::min(a.r, b.r)}; } struct bit { aN f; long long sum, n; void clear(long long x) { n = x, sum = 0, memset(f, 0, sizeof f); } void update(long long u, long long c) { sum += c; while (u <= n) f[u] += c, u += u & -u; } long long query(long long u) const { long long res = 0; while (u) res += f[u], u &= ~-u; return res; } long long query(qj b) const { b = b & (qj){1, n}; return b.l <= b.r ? query(b.r) - query(b.l - 1) : 0; } long long min() const { long long u = 0; for (register long long i = 1 << 20; i; i >>= 1) if (i + u <= n && !f[i + u]) u += i; return u + 1; } long long max() const { long long u = 0, s = sum; for (register long long i = 1 << 20; i; i >>= 1) if (i + u <= n && f[i + u] < s) s -= f[u += i]; return u + !!sum; } } s[3]; long long t[1145], a[1145141], n, m; void solve() { long long sum = 0; qj q, h; for (register long long i = 0; i <= 2; i++) sum += s[i].query(q = (qj){s[(i + 1) % 3].min(), s[(i + 2) % 3].min() - 1}) + s[i].query(h = (qj){s[(i + 2) % 3].max() + 1, s[(i + 1) % 3].max()}), sum -= s[i].query(q & h); printf("%lld\n", n - sum); }; int main() { t['P'] = 1, t['R'] = 0, t['S'] = 2, n = readll(), m = readll(); s[0].clear(n), s[1].clear(n), s[2].clear(n); long long x, c; for (register long long i = 1; i <= n; i++) s[a[i] = t[readchar()]].update(i, 1); solve(); for (register long long i = 1; i <= m; i++) x = readll(), c = readchar(), s[a[x]].update(x, -1), s[a[x] = t[c]].update(x, 1), solve(); return 0; }
### Prompt In cpp, your task is to solve the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> const long long N = 216333, e7 = 1e7 + 7, inf = 1e17; typedef long long aN[N]; long long readll() { long long x = 0, w = 1; int c = getchar(); for (; c < '0' || c > '9'; c - '-' || (w = -w), c = getchar()) ; for (; c >= '0' && c <= '9'; x = x * 10 + (c ^ 48), c = getchar()) ; return x * w; } int readchar(int l = '0', int r = 'z') { int c = getchar(); for (; c < l || c > r; c = getchar()) ; return c; } struct qj { long long l, r; }; qj operator&(const qj& a, const qj& b) { return (qj){std::max(a.l, b.l), std::min(a.r, b.r)}; } struct bit { aN f; long long sum, n; void clear(long long x) { n = x, sum = 0, memset(f, 0, sizeof f); } void update(long long u, long long c) { sum += c; while (u <= n) f[u] += c, u += u & -u; } long long query(long long u) const { long long res = 0; while (u) res += f[u], u &= ~-u; return res; } long long query(qj b) const { b = b & (qj){1, n}; return b.l <= b.r ? query(b.r) - query(b.l - 1) : 0; } long long min() const { long long u = 0; for (register long long i = 1 << 20; i; i >>= 1) if (i + u <= n && !f[i + u]) u += i; return u + 1; } long long max() const { long long u = 0, s = sum; for (register long long i = 1 << 20; i; i >>= 1) if (i + u <= n && f[i + u] < s) s -= f[u += i]; return u + !!sum; } } s[3]; long long t[1145], a[1145141], n, m; void solve() { long long sum = 0; qj q, h; for (register long long i = 0; i <= 2; i++) sum += s[i].query(q = (qj){s[(i + 1) % 3].min(), s[(i + 2) % 3].min() - 1}) + s[i].query(h = (qj){s[(i + 2) % 3].max() + 1, s[(i + 1) % 3].max()}), sum -= s[i].query(q & h); printf("%lld\n", n - sum); }; int main() { t['P'] = 1, t['R'] = 0, t['S'] = 2, n = readll(), m = readll(); s[0].clear(n), s[1].clear(n), s[2].clear(n); long long x, c; for (register long long i = 1; i <= n; i++) s[a[i] = t[readchar()]].update(i, 1); solve(); for (register long long i = 1; i <= m; i++) x = readll(), c = readchar(), s[a[x]].update(x, -1), s[a[x] = t[c]].update(x, 1), solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using vvi = vector<vector<int>>; using vi = vector<int>; using vvll = vector<vector<long long>>; using vll = vector<long long>; using vd = vector<double>; using vvd = vector<vector<double>>; using pii = pair<int, int>; using vpii = vector<pair<int, int>>; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } int wins_to(int type) { return (type + 2) % 3; } int loses_to(int type) { return (type + 1) % 3; } void st_set(vi &st, int tl, int tr, int pos, int idx, int val) { if (tl == tr) { st[pos] = val; return; } if (idx <= tl + (tr - tl) / 2) { st_set(st, tl, tl + (tr - tl) / 2, pos * 2, idx, val); } else { st_set(st, tl + (tr - tl) / 2 + 1, tr, pos * 2 + 1, idx, val); } st[pos] = st[pos * 2] + st[pos * 2 + 1]; } int st_sum(vi &st, int tl, int tr, int pos, int left, int right) { if (right < tl || tr < left) { return 0; } if (left <= tl && tr <= right) { return st[pos]; } return st_sum(st, tl, tl + (tr - tl) / 2, pos * 2, left, right) + st_sum(st, tl + (tr - tl) / 2 + 1, tr, pos * 2 + 1, left, right); } void solve_query(int n, vi &type, vector<set<int>> &positions, vvi &st) { int ans = 0; for (int t = 0; t < 3; t++) { if (positions[t].empty()) { continue; } if (positions[loses_to(t)].empty()) { ans += positions[t].size(); continue; } if (positions[wins_to(t)].empty()) { continue; } int impossible = 0; int first_loss = *positions[loses_to(t)].begin(); int first_win = *positions[wins_to(t)].begin(); if (first_loss < first_win) { impossible += st_sum(st[t], 0, n - 1, 1, first_loss, first_win); } int last_win = *prev(positions[wins_to(t)].end()); int last_loss = *prev(positions[loses_to(t)].end()); if (last_win < last_loss) { impossible += st_sum(st[t], 0, n - 1, 1, last_win, last_loss); } ans += positions[t].size() - impossible; } cout << ans << endl; } void solve() { int n, q; cin >> n >> q; string s; cin >> s; vi type(n); vector<set<int>> positions(3); vvi st(3, vi(4 * n)); for (int i = 0; i < n; i++) { type[i] = (s[i] == 'R' ? 0 : (s[i] == 'P' ? 1 : 2)); positions[type[i]].insert(i); st_set(st[type[i]], 0, n - 1, 1, i, 1); } solve_query(n, type, positions, st); while (q--) { int idx; char c; cin >> idx >> c; idx--; int new_type = (c == 'R' ? 0 : (c == 'P' ? 1 : 2)); positions[type[idx]].erase(idx); st_set(st[type[idx]], 0, n - 1, 1, idx, 0); type[idx] = new_type; positions[type[idx]].insert(idx); st_set(st[type[idx]], 0, n - 1, 1, idx, 1); solve_query(n, type, positions, st); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
### Prompt Please formulate a cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using vvi = vector<vector<int>>; using vi = vector<int>; using vvll = vector<vector<long long>>; using vll = vector<long long>; using vd = vector<double>; using vvd = vector<vector<double>>; using pii = pair<int, int>; using vpii = vector<pair<int, int>>; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } int wins_to(int type) { return (type + 2) % 3; } int loses_to(int type) { return (type + 1) % 3; } void st_set(vi &st, int tl, int tr, int pos, int idx, int val) { if (tl == tr) { st[pos] = val; return; } if (idx <= tl + (tr - tl) / 2) { st_set(st, tl, tl + (tr - tl) / 2, pos * 2, idx, val); } else { st_set(st, tl + (tr - tl) / 2 + 1, tr, pos * 2 + 1, idx, val); } st[pos] = st[pos * 2] + st[pos * 2 + 1]; } int st_sum(vi &st, int tl, int tr, int pos, int left, int right) { if (right < tl || tr < left) { return 0; } if (left <= tl && tr <= right) { return st[pos]; } return st_sum(st, tl, tl + (tr - tl) / 2, pos * 2, left, right) + st_sum(st, tl + (tr - tl) / 2 + 1, tr, pos * 2 + 1, left, right); } void solve_query(int n, vi &type, vector<set<int>> &positions, vvi &st) { int ans = 0; for (int t = 0; t < 3; t++) { if (positions[t].empty()) { continue; } if (positions[loses_to(t)].empty()) { ans += positions[t].size(); continue; } if (positions[wins_to(t)].empty()) { continue; } int impossible = 0; int first_loss = *positions[loses_to(t)].begin(); int first_win = *positions[wins_to(t)].begin(); if (first_loss < first_win) { impossible += st_sum(st[t], 0, n - 1, 1, first_loss, first_win); } int last_win = *prev(positions[wins_to(t)].end()); int last_loss = *prev(positions[loses_to(t)].end()); if (last_win < last_loss) { impossible += st_sum(st[t], 0, n - 1, 1, last_win, last_loss); } ans += positions[t].size() - impossible; } cout << ans << endl; } void solve() { int n, q; cin >> n >> q; string s; cin >> s; vi type(n); vector<set<int>> positions(3); vvi st(3, vi(4 * n)); for (int i = 0; i < n; i++) { type[i] = (s[i] == 'R' ? 0 : (s[i] == 'P' ? 1 : 2)); positions[type[i]].insert(i); st_set(st[type[i]], 0, n - 1, 1, i, 1); } solve_query(n, type, positions, st); while (q--) { int idx; char c; cin >> idx >> c; idx--; int new_type = (c == 'R' ? 0 : (c == 'P' ? 1 : 2)); positions[type[idx]].erase(idx); st_set(st[type[idx]], 0, n - 1, 1, idx, 0); type[idx] = new_type; positions[type[idx]].insert(idx); st_set(st[type[idx]], 0, n - 1, 1, idx, 1); solve_query(n, type, positions, st); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100, M = 3; int n, q, dex, ans, f[N], good[M], bad[M], fen[M][N]; string s; set<int> st[M]; inline void add(int dex, int type, int val) { for (; dex < N; dex += (dex & -dex)) fen[type][dex] += val; } inline int get(int dex, int type) { return (dex > 0 ? fen[type][dex] + get(dex ^ (dex & -dex), type) : 0); } inline void check(int l1, int r1, int l2, int r2, int type) { int l = max(l1, l2), r = min(r1, r2); if (r >= l) ans += get(r, type) - get(l - 1, type); } inline int solve() { ans = 0; for (int i = 0; i < M; i++) { if (st[i].empty()) continue; else if (st[i].size() == n) { ans = n; break; } if (st[bad[i]].empty()) { ans += st[i].size(); continue; } else if (st[good[i]].empty()) continue; int a, b, A, B; a = (*st[bad[i]].begin()) - 1; auto x = st[good[i]].begin(); b = (st[good[i]].empty() ? n + 1 : max(a + 1, *x)); B = (*st[bad[i]].rbegin()) + 1; x = --st[good[i]].end(); A = (st[good[i]].empty() ? 0 : min(*x, B - 1)); check(0, a, 0, A, i); check(0, a, B, n, i); check(b, n, 0, A, i); check(b, n, B, n, i); } return ans; } inline void add_element(int dex) { add(dex, f[s[dex]], 1); st[f[s[dex]]].insert(dex); } inline void del_element(int dex) { add(dex, f[s[dex]], -1); st[f[s[dex]]].erase(dex); } int main() { ios::sync_with_stdio(false), cin.tie(0); f['R'] = 0, f['P'] = 1, f['S'] = 2; for (int i = 0; i < M; i++) bad[i] = (i + 1) % M, good[i] = (i - 1 + M) % M; cin >> n >> q >> s; s = "#" + s; for (int i = 1; i <= n; i++) add_element(i); cout << solve() << '\n'; while (q--) { cin >> dex; del_element(dex); cin >> s[dex]; add_element(dex); cout << solve() << '\n'; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100, M = 3; int n, q, dex, ans, f[N], good[M], bad[M], fen[M][N]; string s; set<int> st[M]; inline void add(int dex, int type, int val) { for (; dex < N; dex += (dex & -dex)) fen[type][dex] += val; } inline int get(int dex, int type) { return (dex > 0 ? fen[type][dex] + get(dex ^ (dex & -dex), type) : 0); } inline void check(int l1, int r1, int l2, int r2, int type) { int l = max(l1, l2), r = min(r1, r2); if (r >= l) ans += get(r, type) - get(l - 1, type); } inline int solve() { ans = 0; for (int i = 0; i < M; i++) { if (st[i].empty()) continue; else if (st[i].size() == n) { ans = n; break; } if (st[bad[i]].empty()) { ans += st[i].size(); continue; } else if (st[good[i]].empty()) continue; int a, b, A, B; a = (*st[bad[i]].begin()) - 1; auto x = st[good[i]].begin(); b = (st[good[i]].empty() ? n + 1 : max(a + 1, *x)); B = (*st[bad[i]].rbegin()) + 1; x = --st[good[i]].end(); A = (st[good[i]].empty() ? 0 : min(*x, B - 1)); check(0, a, 0, A, i); check(0, a, B, n, i); check(b, n, 0, A, i); check(b, n, B, n, i); } return ans; } inline void add_element(int dex) { add(dex, f[s[dex]], 1); st[f[s[dex]]].insert(dex); } inline void del_element(int dex) { add(dex, f[s[dex]], -1); st[f[s[dex]]].erase(dex); } int main() { ios::sync_with_stdio(false), cin.tie(0); f['R'] = 0, f['P'] = 1, f['S'] = 2; for (int i = 0; i < M; i++) bad[i] = (i + 1) % M, good[i] = (i - 1 + M) % M; cin >> n >> q >> s; s = "#" + s; for (int i = 1; i <= n; i++) add_element(i); cout << solve() << '\n'; while (q--) { cin >> dex; del_element(dex); cin >> s[dex]; add_element(dex); cout << solve() << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename _T> inline void read(_T &f) { f = 0; _T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fu = -1; c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } f *= fu; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } template <typename T> void print(T x, char t) { print(x); putchar(t); } const int N = 2e5 + 5; struct SookeTree { int sum[N << 2]; SookeTree() { memset(sum, 0, sizeof(sum)); } void change(int u, int l, int r, int x, int y) { sum[u] += y; if (l == r) return; int mid = (l + r) >> 1; if (mid >= x) change(u << 1, l, mid, x, y); else change(u << 1 | 1, mid + 1, r, x, y); } int query(int u, int L, int R, int l, int r) { if (l > r) return 0; if (l <= L && R <= r) return sum[u]; int mid = (L + R) >> 1, ans = 0; if (mid >= l) ans += query(u << 1, L, mid, l, r); if (mid + 1 <= r) ans += query(u << 1 | 1, mid + 1, R, l, r); return ans; } } t[3]; char c[N]; int a[N]; int n, m; int calc(char c) { if (c == 'P') return 0; if (c == 'R') return 1; return 2; } int solve() { int ans = 0; int L[3] = {-1, -1, -1}, R[3] = {-1, -1, -1}; int l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[0].query(1, 1, n, 1, mid)) res = mid, r = mid - 1; else l = mid + 1; } L[0] = res; l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[0].query(1, 1, n, mid, n)) res = mid, l = mid + 1; else r = mid - 1; } R[0] = res; l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[1].query(1, 1, n, 1, mid)) res = mid, r = mid - 1; else l = mid + 1; } L[1] = res; l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[1].query(1, 1, n, mid, n)) res = mid, l = mid + 1; else r = mid - 1; } R[1] = res; l = 1, r = n, res = -1; l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[2].query(1, 1, n, 1, mid)) res = mid, r = mid - 1; else l = mid + 1; } L[2] = res; l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[2].query(1, 1, n, mid, n)) res = mid, l = mid + 1; else r = mid - 1; } R[2] = res; l = 1, r = n, res = -1; for (register int i = 0; i <= 2; i++) if (t[i].query(1, 1, n, 1, n) == n) return n; if (L[1] == -1) ans += t[2].query(1, 1, n, 1, n); else if (~L[0]) ans += t[2].query(1, 1, n, L[0], R[0]); if (L[2] == -1) ans += t[0].query(1, 1, n, 1, n); else if (~L[1]) ans += t[0].query(1, 1, n, L[1], R[1]); if (L[0] == -1) ans += t[1].query(1, 1, n, 1, n); else if (~L[2]) ans += t[1].query(1, 1, n, L[2], R[2]); if (~L[0] && ~L[1]) ans += t[2].query(1, 1, n, 2, min(L[0], L[1])); if (~L[0] && ~L[2]) ans += t[1].query(1, 1, n, 2, min(L[2], L[0])); if (~L[1] && ~L[2]) ans += t[0].query(1, 1, n, 2, min(L[1], L[2])); if (~R[0] && ~R[1]) ans += t[2].query(1, 1, n, max(R[1], R[0]), n - 1); if (~R[0] && ~R[2]) ans += t[1].query(1, 1, n, max(R[0], R[2]), n - 1); if (~R[1] && ~R[2]) ans += t[0].query(1, 1, n, max(R[2], R[1]), n - 1); if (a[1] == 0 && ~L[1] && ~L[2]) ans++; if (a[1] == 1 && ~L[0] && ~L[2]) ans++; if (a[1] == 2 && ~L[1] && ~L[0]) ans++; if (a[n] == 0 && ~L[1] && ~L[2]) ans++; if (a[n] == 1 && ~L[0] && ~L[2]) ans++; if (a[n] == 2 && ~L[1] && ~L[0]) ans++; return ans; } int main() { read(n); read(m); scanf("%s", c + 1); for (register int i = 1; i <= n; i++) { a[i] = calc(c[i]); t[a[i]].change(1, 1, n, i, 1); } print(solve(), '\n'); for (register int i = 1; i <= m; i++) { int tt; char c; read(tt); c = getchar(); while (c < 'A' || c > 'Z') c = getchar(); int now = calc(c); t[a[tt]].change(1, 1, n, tt, -1); a[tt] = now; t[a[tt]].change(1, 1, n, tt, 1); print(solve(), '\n'); } return 0; }
### Prompt Generate a Cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename _T> inline void read(_T &f) { f = 0; _T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fu = -1; c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } f *= fu; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } template <typename T> void print(T x, char t) { print(x); putchar(t); } const int N = 2e5 + 5; struct SookeTree { int sum[N << 2]; SookeTree() { memset(sum, 0, sizeof(sum)); } void change(int u, int l, int r, int x, int y) { sum[u] += y; if (l == r) return; int mid = (l + r) >> 1; if (mid >= x) change(u << 1, l, mid, x, y); else change(u << 1 | 1, mid + 1, r, x, y); } int query(int u, int L, int R, int l, int r) { if (l > r) return 0; if (l <= L && R <= r) return sum[u]; int mid = (L + R) >> 1, ans = 0; if (mid >= l) ans += query(u << 1, L, mid, l, r); if (mid + 1 <= r) ans += query(u << 1 | 1, mid + 1, R, l, r); return ans; } } t[3]; char c[N]; int a[N]; int n, m; int calc(char c) { if (c == 'P') return 0; if (c == 'R') return 1; return 2; } int solve() { int ans = 0; int L[3] = {-1, -1, -1}, R[3] = {-1, -1, -1}; int l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[0].query(1, 1, n, 1, mid)) res = mid, r = mid - 1; else l = mid + 1; } L[0] = res; l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[0].query(1, 1, n, mid, n)) res = mid, l = mid + 1; else r = mid - 1; } R[0] = res; l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[1].query(1, 1, n, 1, mid)) res = mid, r = mid - 1; else l = mid + 1; } L[1] = res; l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[1].query(1, 1, n, mid, n)) res = mid, l = mid + 1; else r = mid - 1; } R[1] = res; l = 1, r = n, res = -1; l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[2].query(1, 1, n, 1, mid)) res = mid, r = mid - 1; else l = mid + 1; } L[2] = res; l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) >> 1; if (t[2].query(1, 1, n, mid, n)) res = mid, l = mid + 1; else r = mid - 1; } R[2] = res; l = 1, r = n, res = -1; for (register int i = 0; i <= 2; i++) if (t[i].query(1, 1, n, 1, n) == n) return n; if (L[1] == -1) ans += t[2].query(1, 1, n, 1, n); else if (~L[0]) ans += t[2].query(1, 1, n, L[0], R[0]); if (L[2] == -1) ans += t[0].query(1, 1, n, 1, n); else if (~L[1]) ans += t[0].query(1, 1, n, L[1], R[1]); if (L[0] == -1) ans += t[1].query(1, 1, n, 1, n); else if (~L[2]) ans += t[1].query(1, 1, n, L[2], R[2]); if (~L[0] && ~L[1]) ans += t[2].query(1, 1, n, 2, min(L[0], L[1])); if (~L[0] && ~L[2]) ans += t[1].query(1, 1, n, 2, min(L[2], L[0])); if (~L[1] && ~L[2]) ans += t[0].query(1, 1, n, 2, min(L[1], L[2])); if (~R[0] && ~R[1]) ans += t[2].query(1, 1, n, max(R[1], R[0]), n - 1); if (~R[0] && ~R[2]) ans += t[1].query(1, 1, n, max(R[0], R[2]), n - 1); if (~R[1] && ~R[2]) ans += t[0].query(1, 1, n, max(R[2], R[1]), n - 1); if (a[1] == 0 && ~L[1] && ~L[2]) ans++; if (a[1] == 1 && ~L[0] && ~L[2]) ans++; if (a[1] == 2 && ~L[1] && ~L[0]) ans++; if (a[n] == 0 && ~L[1] && ~L[2]) ans++; if (a[n] == 1 && ~L[0] && ~L[2]) ans++; if (a[n] == 2 && ~L[1] && ~L[0]) ans++; return ans; } int main() { read(n); read(m); scanf("%s", c + 1); for (register int i = 1; i <= n; i++) { a[i] = calc(c[i]); t[a[i]].change(1, 1, n, i, 1); } print(solve(), '\n'); for (register int i = 1; i <= m; i++) { int tt; char c; read(tt); c = getchar(); while (c < 'A' || c > 'Z') c = getchar(); int now = calc(c); t[a[tt]].change(1, 1, n, tt, -1); a[tt] = now; t[a[tt]].change(1, 1, n, tt, 1); print(solve(), '\n'); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 200005; int t[3][N]; inline int lowbit(int x) { return x & (-x); } inline void add(int id, int k, int x) { for (int i = k; i < N; i += lowbit(i)) t[id][i] += x; } inline int query(int id, int k) { int res = 0; for (int i = k; i; i -= lowbit(i)) res += t[id][i]; return res; } int to[300]; int n, q; int a[N]; char s[N]; set<int> S[3]; inline int solve() { int cnt = 0; for (int i = 0; i < 3; i++) if (!S[i].empty()) cnt++; if (cnt == 1) return n; if (cnt == 2) for (int i = 0; i < 3; i++) if (S[i].empty()) return S[(i + 2) % 3].size(); int ans = 0, L, R; for (int i = 0; i < 3; i++) { L = *S[(i + 1) % 3].begin(), R = *S[(i + 2) % 3].begin(); if (R >= L) ans += query(i, R) - query(i, L - 1); L = *S[(i + 2) % 3].rbegin(), R = *S[(i + 1) % 3].rbegin(); if (R >= L) ans += query(i, R) - query(i, L - 1); } return n - ans; } int main() { to['R'] = 0, to['P'] = 1, to['S'] = 2; scanf("%d%d", &n, &q); scanf("%s", s + 1); for (int i = 1; i <= n; i++) { a[i] = to[s[i]]; add(a[i], i, 1); S[a[i]].insert(i); } printf("%d\n", solve()); while (q--) { int x; char ch[5]; scanf("%d%s", &x, ch + 1); add(a[x], x, -1); S[a[x]].erase(x); a[x] = to[ch[1]]; add(a[x], x, 1); S[a[x]].insert(x); printf("%d\n", solve()); } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 200005; int t[3][N]; inline int lowbit(int x) { return x & (-x); } inline void add(int id, int k, int x) { for (int i = k; i < N; i += lowbit(i)) t[id][i] += x; } inline int query(int id, int k) { int res = 0; for (int i = k; i; i -= lowbit(i)) res += t[id][i]; return res; } int to[300]; int n, q; int a[N]; char s[N]; set<int> S[3]; inline int solve() { int cnt = 0; for (int i = 0; i < 3; i++) if (!S[i].empty()) cnt++; if (cnt == 1) return n; if (cnt == 2) for (int i = 0; i < 3; i++) if (S[i].empty()) return S[(i + 2) % 3].size(); int ans = 0, L, R; for (int i = 0; i < 3; i++) { L = *S[(i + 1) % 3].begin(), R = *S[(i + 2) % 3].begin(); if (R >= L) ans += query(i, R) - query(i, L - 1); L = *S[(i + 2) % 3].rbegin(), R = *S[(i + 1) % 3].rbegin(); if (R >= L) ans += query(i, R) - query(i, L - 1); } return n - ans; } int main() { to['R'] = 0, to['P'] = 1, to['S'] = 2; scanf("%d%d", &n, &q); scanf("%s", s + 1); for (int i = 1; i <= n; i++) { a[i] = to[s[i]]; add(a[i], i, 1); S[a[i]].insert(i); } printf("%d\n", solve()); while (q--) { int x; char ch[5]; scanf("%d%s", &x, ch + 1); add(a[x], x, -1); S[a[x]].erase(x); a[x] = to[ch[1]]; add(a[x], x, 1); S[a[x]].insert(x); printf("%d\n", solve()); } return 0; } ```
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long maxn = 2e5 + 5, mod = 1e9 + 7, inf = mod; long long n; set<long long> s[3]; long long q; struct { vector<long long> sum = vector<long long>(maxn, 0); void add(long long i, long long v) { for (; i < maxn; i += i & -i) { sum[i] += v; } } long long get(long long i) { long long r = 0; for (; i > 0; i -= i & -i) { r += sum[i]; } return r; } long long getran(long long l, long long r) { return get(r) - get(l - 1); } } bit[3]; long long arr[maxn]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; map<long long, long long> m; m['R'] = 0; m['P'] = 1, m['S'] = 2; for (long long i = (long long)1; i <= (long long)n; i++) { char c; cin >> c; arr[i] = m[c]; s[arr[i]].insert(i); bit[arr[i]].add(i, 1); } for (long long i = (long long)0; i <= (long long)q; i++) { if (i) { char c; long long p; cin >> p >> c; s[arr[p]].erase(p); bit[arr[p]].add(p, -1); arr[p] = m[c]; s[arr[p]].insert(p); bit[arr[p]].add(p, 1); } long long ans = 0; for (long long t = (long long)0; t <= (long long)2; t++) { long long nxt = (t + 1) % 3, prv = (t + 2) % 3; if (s[nxt].empty()) ans += s[t].size(); else { long long l1 = (s[prv].empty() ? 1 : *s[prv].rbegin() + 1); long long r1 = *s[nxt].rbegin(); long long r2 = (s[prv].empty() ? n : *s[prv].begin() - 1); long long l2 = *s[nxt].begin(); if (l1 > r1) l1 = r1 = 0; if (l2 > r2) l2 = r2 = 0; if (max(l1, l2) <= min(r1, r2)) { l1 = min(l1, l2); r1 = max(r1, r2); l2 = r2 = 0; } ans += bit[t].getran(1, n) - bit[t].getran(l1, r1) - bit[t].getran(l2, r2); } } cout << ans << '\n'; } }
### Prompt In CPP, your task is to solve the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long maxn = 2e5 + 5, mod = 1e9 + 7, inf = mod; long long n; set<long long> s[3]; long long q; struct { vector<long long> sum = vector<long long>(maxn, 0); void add(long long i, long long v) { for (; i < maxn; i += i & -i) { sum[i] += v; } } long long get(long long i) { long long r = 0; for (; i > 0; i -= i & -i) { r += sum[i]; } return r; } long long getran(long long l, long long r) { return get(r) - get(l - 1); } } bit[3]; long long arr[maxn]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; map<long long, long long> m; m['R'] = 0; m['P'] = 1, m['S'] = 2; for (long long i = (long long)1; i <= (long long)n; i++) { char c; cin >> c; arr[i] = m[c]; s[arr[i]].insert(i); bit[arr[i]].add(i, 1); } for (long long i = (long long)0; i <= (long long)q; i++) { if (i) { char c; long long p; cin >> p >> c; s[arr[p]].erase(p); bit[arr[p]].add(p, -1); arr[p] = m[c]; s[arr[p]].insert(p); bit[arr[p]].add(p, 1); } long long ans = 0; for (long long t = (long long)0; t <= (long long)2; t++) { long long nxt = (t + 1) % 3, prv = (t + 2) % 3; if (s[nxt].empty()) ans += s[t].size(); else { long long l1 = (s[prv].empty() ? 1 : *s[prv].rbegin() + 1); long long r1 = *s[nxt].rbegin(); long long r2 = (s[prv].empty() ? n : *s[prv].begin() - 1); long long l2 = *s[nxt].begin(); if (l1 > r1) l1 = r1 = 0; if (l2 > r2) l2 = r2 = 0; if (max(l1, l2) <= min(r1, r2)) { l1 = min(l1, l2); r1 = max(r1, r2); l2 = r2 = 0; } ans += bit[t].getran(1, n) - bit[t].getran(l1, r1) - bit[t].getran(l2, r2); } } cout << ans << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int f[3][200100]; char a[200100]; set<int> in[3]; int sum(int s, int x) { int r = 0; while (x) { r += f[s][x]; x -= x & -x; } return r; } void upd(int s, int x, int v) { while (x < 200100) { f[s][x] += v; x += x & -x; } } int solve(int n) { int l[3], r[3], ans = 0; for (int j = 0; j < 3; j++) { if (in[j].empty()) { l[j] = n + 1; r[j] = 0; } else { l[j] = *(in[j].begin()); r[j] = *(in[j].rbegin()); } } for (int j = 0; j < 3; j++) { int l1 = min(l[(j + 1) % 3], l[(j + 2) % 3]) - 1, l2 = r[(j + 1) % 3]; int r1 = l[(j + 1) % 3], r2 = max(r[(j + 1) % 3], r[(j + 2) % 3]) + 1; ans += sum(j, min(l1, l2)); ans += sum(j, n) - sum(j, max(r1, r2) - 1); if (l1 >= r2) ans += sum(j, l1) - sum(j, r2 - 1); if (l2 >= r1) ans += sum(j, l2) - sum(j, r1 - 1); } return ans; } int main() { char c[10] = "RSP"; int n, q; scanf("%d%d", &n, &q); scanf("%s", a + 1); for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { if (a[i] == c[j]) { in[j].insert(i); upd(j, i, 1); } } } printf("%d\n", solve(n)); while (q--) { int x; char tmp; scanf("%d %c", &x, &tmp); for (int j = 0; j < 3; j++) { if (a[x] == c[j]) { in[j].erase(x); upd(j, x, -1); } } for (int j = 0; j < 3; j++) { if (tmp == c[j]) { in[j].insert(x); upd(j, x, 1); } } a[x] = tmp; printf("%d\n", solve(n)); } }
### Prompt Please formulate a cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int f[3][200100]; char a[200100]; set<int> in[3]; int sum(int s, int x) { int r = 0; while (x) { r += f[s][x]; x -= x & -x; } return r; } void upd(int s, int x, int v) { while (x < 200100) { f[s][x] += v; x += x & -x; } } int solve(int n) { int l[3], r[3], ans = 0; for (int j = 0; j < 3; j++) { if (in[j].empty()) { l[j] = n + 1; r[j] = 0; } else { l[j] = *(in[j].begin()); r[j] = *(in[j].rbegin()); } } for (int j = 0; j < 3; j++) { int l1 = min(l[(j + 1) % 3], l[(j + 2) % 3]) - 1, l2 = r[(j + 1) % 3]; int r1 = l[(j + 1) % 3], r2 = max(r[(j + 1) % 3], r[(j + 2) % 3]) + 1; ans += sum(j, min(l1, l2)); ans += sum(j, n) - sum(j, max(r1, r2) - 1); if (l1 >= r2) ans += sum(j, l1) - sum(j, r2 - 1); if (l2 >= r1) ans += sum(j, l2) - sum(j, r1 - 1); } return ans; } int main() { char c[10] = "RSP"; int n, q; scanf("%d%d", &n, &q); scanf("%s", a + 1); for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { if (a[i] == c[j]) { in[j].insert(i); upd(j, i, 1); } } } printf("%d\n", solve(n)); while (q--) { int x; char tmp; scanf("%d %c", &x, &tmp); for (int j = 0; j < 3; j++) { if (a[x] == c[j]) { in[j].erase(x); upd(j, x, -1); } } for (int j = 0; j < 3; j++) { if (tmp == c[j]) { in[j].insert(x); upd(j, x, 1); } } a[x] = tmp; printf("%d\n", solve(n)); } } ```
#include <bits/stdc++.h> using namespace std; struct Node { int l, r; int cnt[3]; }; int N, M; string s; int arr[200005]; set<int> st[3]; Node seg[1000000]; void pu(int idx) { for (int k = 0; k < 3; k++) { seg[idx].cnt[k] = seg[2 * idx].cnt[k] + seg[2 * idx + 1].cnt[k]; } } void build(int l, int r, int idx) { seg[idx].l = l; seg[idx].r = r; if (l == r) { seg[idx].cnt[arr[l]]++; return; } int mid = (l + r) / 2; build(l, mid, 2 * idx); build(mid + 1, r, 2 * idx + 1); pu(idx); } void upd(int p, int t, int v, int idx) { if (seg[idx].l == seg[idx].r) { seg[idx].cnt[t] += v; return; } int mid = (seg[idx].l + seg[idx].r) / 2; if (p <= mid) { upd(p, t, v, 2 * idx); } else { upd(p, t, v, 2 * idx + 1); } pu(idx); } int query(int l, int r, int t, int idx) { if (seg[idx].l == l && seg[idx].r == r) { return seg[idx].cnt[t]; } int mid = (seg[idx].l + seg[idx].r) / 2; if (r <= mid) { return query(l, r, t, 2 * idx); } else if (l > mid) { return query(l, r, t, 2 * idx + 1); } else { return query(l, mid, t, 2 * idx) + query(mid + 1, r, t, 2 * idx + 1); } } int solve() { int ret = N; for (int k = 0; k < 3; k++) { if (st[(k + 1) % 3].size() && st[(k + 2) % 3].empty()) { ret -= query(1, N, k, 1); } else { int ll = st[(k + 1) % 3].size() ? (*st[(k + 1) % 3].begin()) : N + 2; int lw = st[(k + 2) % 3].size() ? (*st[(k + 2) % 3].begin()) : N + 1; if (ll < lw) { ret -= query(ll, lw - 1, k, 1); } int rl = st[(k + 1) % 3].size() ? (*st[(k + 1) % 3].rbegin()) : -2; int rw = st[(k + 2) % 3].size() ? (*st[(k + 2) % 3].rbegin()) : -1; if (rl > rw) { ret -= query(rw + 1, rl, k, 1); } } } return ret; } int typ(char c) { if (c == 'R') { return 0; } else if (c == 'P') { return 1; } else { return 2; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> M >> s; for (int i = 1; i <= N; i++) { arr[i] = typ(s[i - 1]); st[arr[i]].insert(i); } build(1, N, 1); cout << solve() << "\n"; while (M--) { int n; char c; cin >> n >> c; upd(n, arr[n], -1, 1); st[arr[n]].erase(n); arr[n] = typ(c); upd(n, arr[n], 1, 1); st[arr[n]].insert(n); cout << solve() << "\n"; } }
### Prompt Please formulate a Cpp solution to the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Node { int l, r; int cnt[3]; }; int N, M; string s; int arr[200005]; set<int> st[3]; Node seg[1000000]; void pu(int idx) { for (int k = 0; k < 3; k++) { seg[idx].cnt[k] = seg[2 * idx].cnt[k] + seg[2 * idx + 1].cnt[k]; } } void build(int l, int r, int idx) { seg[idx].l = l; seg[idx].r = r; if (l == r) { seg[idx].cnt[arr[l]]++; return; } int mid = (l + r) / 2; build(l, mid, 2 * idx); build(mid + 1, r, 2 * idx + 1); pu(idx); } void upd(int p, int t, int v, int idx) { if (seg[idx].l == seg[idx].r) { seg[idx].cnt[t] += v; return; } int mid = (seg[idx].l + seg[idx].r) / 2; if (p <= mid) { upd(p, t, v, 2 * idx); } else { upd(p, t, v, 2 * idx + 1); } pu(idx); } int query(int l, int r, int t, int idx) { if (seg[idx].l == l && seg[idx].r == r) { return seg[idx].cnt[t]; } int mid = (seg[idx].l + seg[idx].r) / 2; if (r <= mid) { return query(l, r, t, 2 * idx); } else if (l > mid) { return query(l, r, t, 2 * idx + 1); } else { return query(l, mid, t, 2 * idx) + query(mid + 1, r, t, 2 * idx + 1); } } int solve() { int ret = N; for (int k = 0; k < 3; k++) { if (st[(k + 1) % 3].size() && st[(k + 2) % 3].empty()) { ret -= query(1, N, k, 1); } else { int ll = st[(k + 1) % 3].size() ? (*st[(k + 1) % 3].begin()) : N + 2; int lw = st[(k + 2) % 3].size() ? (*st[(k + 2) % 3].begin()) : N + 1; if (ll < lw) { ret -= query(ll, lw - 1, k, 1); } int rl = st[(k + 1) % 3].size() ? (*st[(k + 1) % 3].rbegin()) : -2; int rw = st[(k + 2) % 3].size() ? (*st[(k + 2) % 3].rbegin()) : -1; if (rl > rw) { ret -= query(rw + 1, rl, k, 1); } } } return ret; } int typ(char c) { if (c == 'R') { return 0; } else if (c == 'P') { return 1; } else { return 2; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> M >> s; for (int i = 1; i <= N; i++) { arr[i] = typ(s[i - 1]); st[arr[i]].insert(i); } build(1, N, 1); cout << solve() << "\n"; while (M--) { int n; char c; cin >> n >> c; upd(n, arr[n], -1, 1); st[arr[n]].erase(n); arr[n] = typ(c); upd(n, arr[n], 1, 1); st[arr[n]].insert(n); cout << solve() << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; const int mod = 1e9 + 7; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); int n, q, cnt[3]; string s, t = "RPS"; set<int> posl[3]; set<int, greater<int> > posr[3]; int f[3][maxn]; void upd(int w, int pos, int delta) { for (; pos <= n; pos += pos & -pos) f[w][pos] += delta; } int get(int w, int pos) { int ret = 0; for (; pos > 0; pos -= pos & -pos) ret += f[w][pos]; return ret; } int get(int w, int l, int r) { if (l > r) return 0; return get(w, r) - get(w, l - 1); } void answer() { if (cnt[0] == n || cnt[1] == n || cnt[2] == n) { cout << n << "\n"; return; } if (cnt[0] == 0) { cout << cnt[2] << "\n"; return; } if (cnt[1] == 0) { cout << cnt[0] << "\n"; return; } if (cnt[2] == 0) { cout << cnt[1] << "\n"; return; } int sub = 0; sub += get(0, *posl[1].begin(), *posl[2].begin()); sub += get(0, *posr[2].begin(), *posr[1].begin()); sub += get(1, *posl[2].begin(), *posl[0].begin()); sub += get(1, *posr[0].begin(), *posr[2].begin()); sub += get(2, *posl[0].begin(), *posl[1].begin()); sub += get(2, *posr[1].begin(), *posr[0].begin()); cout << n - sub << "\n"; } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> n >> q; cin >> s; s = 'x' + s; for (int i = 1; i <= n; i++) { int w = t.find(s[i]); posl[w].insert(i); posr[w].insert(i); cnt[w]++; upd(w, i, 1); } answer(); while (q--) { int p, w, pr; char c; cin >> p >> c; w = t.find(c), pr = t.find(s[p]); cnt[pr]--; posl[pr].erase(p); posr[pr].erase(p); upd(pr, p, -1); s[p] = c; cnt[w]++; posl[w].insert(p); posr[w].insert(p); upd(w, p, 1); answer(); } return 0; }
### Prompt In CPP, your task is to solve the following problem: n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one match of rock-paper-scissors, two players choose their shapes independently. The outcome is then determined depending on the chosen shapes: "paper" beats "rock", "rock" beats "scissors", "scissors" beat "paper", and two equal shapes result in a draw. At the start of the tournament all players will stand in a row, with their numbers increasing from 1 for the leftmost player, to n for the rightmost player. Each player has a pre-chosen shape that they will use in every game throughout the tournament. Here's how the tournament is conducted: * If there is only one player left, he is declared the champion. * Otherwise, two adjacent players in the row are chosen arbitrarily, and they play the next match. The losing player is eliminated from the tournament and leaves his place in the row (with his former neighbours becoming adjacent). If the game is a draw, the losing player is determined by a coin toss. The organizers are informed about all players' favoured shapes. They wish to find out the total number of players who have a chance of becoming the tournament champion (that is, there is a suitable way to choose the order of the games and manipulate the coin tosses). However, some players are still optimizing their strategy, and can inform the organizers about their new shapes. Can you find the number of possible champions after each such request? Input The first line contains two integers n and q β€” the number of players and requests respectively (1 ≀ n ≀ 2 β‹… 10^5, 0 ≀ q ≀ 2 β‹… 10^5). The second line contains a string of n characters. The i-th of these characters is "R", "P", or "S" if the player i was going to play "rock", "paper", or "scissors" before all requests respectively. The following q lines describe the requests. The j-th of these lines contain an integer p_j and a character c_j meaning that the player p_j is going to use the shape described by the character c_j from this moment (1 ≀ p_j ≀ n). Output Print q + 1 integers r_0, …, r_q, where r_k is the number of possible champions after processing k requests. Example Input 3 5 RPS 1 S 2 R 3 P 1 P 2 P Output 2 2 1 2 2 3 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; const int mod = 1e9 + 7; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); int n, q, cnt[3]; string s, t = "RPS"; set<int> posl[3]; set<int, greater<int> > posr[3]; int f[3][maxn]; void upd(int w, int pos, int delta) { for (; pos <= n; pos += pos & -pos) f[w][pos] += delta; } int get(int w, int pos) { int ret = 0; for (; pos > 0; pos -= pos & -pos) ret += f[w][pos]; return ret; } int get(int w, int l, int r) { if (l > r) return 0; return get(w, r) - get(w, l - 1); } void answer() { if (cnt[0] == n || cnt[1] == n || cnt[2] == n) { cout << n << "\n"; return; } if (cnt[0] == 0) { cout << cnt[2] << "\n"; return; } if (cnt[1] == 0) { cout << cnt[0] << "\n"; return; } if (cnt[2] == 0) { cout << cnt[1] << "\n"; return; } int sub = 0; sub += get(0, *posl[1].begin(), *posl[2].begin()); sub += get(0, *posr[2].begin(), *posr[1].begin()); sub += get(1, *posl[2].begin(), *posl[0].begin()); sub += get(1, *posr[0].begin(), *posr[2].begin()); sub += get(2, *posl[0].begin(), *posl[1].begin()); sub += get(2, *posr[1].begin(), *posr[0].begin()); cout << n - sub << "\n"; } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> n >> q; cin >> s; s = 'x' + s; for (int i = 1; i <= n; i++) { int w = t.find(s[i]); posl[w].insert(i); posr[w].insert(i); cnt[w]++; upd(w, i, 1); } answer(); while (q--) { int p, w, pr; char c; cin >> p >> c; w = t.find(c), pr = t.find(s[p]); cnt[pr]--; posl[pr].erase(p); posr[pr].erase(p); upd(pr, p, -1); s[p] = c; cnt[w]++; posl[w].insert(p); posr[w].insert(p); upd(w, p, 1); answer(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; const int K = 10; int a[N][N]; int speed[10]; int n, m; bool isValid(int x, int y) { return (x >= 0 && y >= 0 && x < n && y < m); } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int p; cin >> n >> m >> p; for (int i = 1; i <= p; i++) cin >> speed[i]; queue<pair<int, int> > q[K]; char ch; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> ch; if (ch >= '1' && ch <= '9') { int id = (ch - '0'); a[i][j] = id; q[id].push({i, j}); } else if (ch == '#') { a[i][j] = 10; } else { a[i][j] = 0; } } } vector<int> dx = {-1, 1, 0, 0}; vector<int> dy = {0, 0, 1, -1}; while (1) { bool leftCells = 0; for (int playerId = 1; playerId <= p; playerId++) { for (int i = 0; i < speed[playerId]; i++) { if (q[playerId].empty()) break; queue<pair<int, int> > tempQ; while (q[playerId].size()) { pair<int, int> p = q[playerId].front(); q[playerId].pop(); for (int k = 0; k < 4; k++) { int newX = p.first + dx[k], newY = p.second + dy[k]; if (isValid(newX, newY) && a[newX][newY] == 0) { tempQ.push({newX, newY}); a[newX][newY] = playerId; leftCells = 1; } } } swap(q[playerId], tempQ); } } if (leftCells == 0) break; } vector<int> ans(11, 0); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[a[i][j]]++; } } for (int i = 1; i <= p; i++) cout << ans[i] << " "; return 0; }
### Prompt In cpp, your task is to solve the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; const int K = 10; int a[N][N]; int speed[10]; int n, m; bool isValid(int x, int y) { return (x >= 0 && y >= 0 && x < n && y < m); } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int p; cin >> n >> m >> p; for (int i = 1; i <= p; i++) cin >> speed[i]; queue<pair<int, int> > q[K]; char ch; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> ch; if (ch >= '1' && ch <= '9') { int id = (ch - '0'); a[i][j] = id; q[id].push({i, j}); } else if (ch == '#') { a[i][j] = 10; } else { a[i][j] = 0; } } } vector<int> dx = {-1, 1, 0, 0}; vector<int> dy = {0, 0, 1, -1}; while (1) { bool leftCells = 0; for (int playerId = 1; playerId <= p; playerId++) { for (int i = 0; i < speed[playerId]; i++) { if (q[playerId].empty()) break; queue<pair<int, int> > tempQ; while (q[playerId].size()) { pair<int, int> p = q[playerId].front(); q[playerId].pop(); for (int k = 0; k < 4; k++) { int newX = p.first + dx[k], newY = p.second + dy[k]; if (isValid(newX, newY) && a[newX][newY] == 0) { tempQ.push({newX, newY}); a[newX][newY] = playerId; leftCells = 1; } } } swap(q[playerId], tempQ); } } if (leftCells == 0) break; } vector<int> ans(11, 0); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[a[i][j]]++; } } for (int i = 1; i <= p; i++) cout << ans[i] << " "; return 0; } ```
#include <bits/stdc++.h> const int NMAX = 1e3 + 100; const int PMAX = 10; struct pair { int x, y; pair(int x, int y) : x(x), y(y) {} }; char field[NMAX][NMAX]; std::queue<pair> qs[PMAX]; std::vector<int> players; int speed[PMAX], answer[PMAX]; int n, m, p; bool way; void preparetion() { std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); } void init() { memset(field, '#', sizeof field); } void get_data() { std::cin >> n >> m >> p; for (int i = 0; i < p; i++) { std::cin >> speed[i]; players.push_back(i); } init(); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { std::cin >> field[i][j]; if (field[i][j] >= '1' && field[i][j] <= '9') { answer[field[i][j] - '0' - 1]++; qs[field[i][j] - '0' - 1].push(pair(i, j)); } } } int d_x[] = {0, 0, -1, 1}; int d_y[] = {-1, 1, 0, 0}; void bfs(int cur_player) { for (int i = 0; !qs[cur_player].empty() && i < speed[cur_player]; i++) { std::queue<pair> tmp_q; while (!qs[cur_player].empty()) { pair cur = qs[cur_player].front(); qs[cur_player].pop(); for (int i = 0; i < 4; i++) { int to_x = cur.x + d_x[i], to_y = cur.y + d_y[i]; if (field[to_x][to_y] == '.') { way = true; answer[cur_player]++; tmp_q.push(pair(to_x, to_y)); field[to_x][to_y] = '0' + cur_player + 1; } } } qs[cur_player] = tmp_q; } } void print() { for (int i = 0; i < p; i++) std::cout << answer[i] << ' '; } void find_solution() { way = false; do { way = false; for (int i = 0; i < p; i++) bfs(i); } while (way); } int main() { preparetion(); get_data(); find_solution(); print(); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> const int NMAX = 1e3 + 100; const int PMAX = 10; struct pair { int x, y; pair(int x, int y) : x(x), y(y) {} }; char field[NMAX][NMAX]; std::queue<pair> qs[PMAX]; std::vector<int> players; int speed[PMAX], answer[PMAX]; int n, m, p; bool way; void preparetion() { std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); } void init() { memset(field, '#', sizeof field); } void get_data() { std::cin >> n >> m >> p; for (int i = 0; i < p; i++) { std::cin >> speed[i]; players.push_back(i); } init(); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { std::cin >> field[i][j]; if (field[i][j] >= '1' && field[i][j] <= '9') { answer[field[i][j] - '0' - 1]++; qs[field[i][j] - '0' - 1].push(pair(i, j)); } } } int d_x[] = {0, 0, -1, 1}; int d_y[] = {-1, 1, 0, 0}; void bfs(int cur_player) { for (int i = 0; !qs[cur_player].empty() && i < speed[cur_player]; i++) { std::queue<pair> tmp_q; while (!qs[cur_player].empty()) { pair cur = qs[cur_player].front(); qs[cur_player].pop(); for (int i = 0; i < 4; i++) { int to_x = cur.x + d_x[i], to_y = cur.y + d_y[i]; if (field[to_x][to_y] == '.') { way = true; answer[cur_player]++; tmp_q.push(pair(to_x, to_y)); field[to_x][to_y] = '0' + cur_player + 1; } } } qs[cur_player] = tmp_q; } } void print() { for (int i = 0; i < p; i++) std::cout << answer[i] << ' '; } void find_solution() { way = false; do { way = false; for (int i = 0; i < p; i++) bfs(i); } while (way); } int main() { preparetion(); get_data(); find_solution(); print(); return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int MAXN = 1e3 + 5, inf = 1e9; const ll INF = 1e18; const ld PI = 3.1415926535897932384626433832795; int mv[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int rmv[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}; int ok[MAXN][MAXN][4], OK[MAXN][MAXN], grid[MAXN][MAXN], s[MAXN]; set<pair<int, int>> av[11]; vector<int> G[MAXN], castles[10]; vector<ll> V; set<ll> S; map<ll, int> M; stack<ll> St; queue<ll> Q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << setprecision(13) << fixed; int n, m, p; cin >> n >> m >> p; for (int i = 1; i <= p; i++) { cin >> s[i]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char c; cin >> c; if (c != '.') { if (c == '#') { grid[i][j] = 10; } else { grid[i][j] = c - '0'; } } } } for (int i = 0; i <= max(n, m) + 1; i++) { grid[0][i] = grid[i][0] = grid[n + 1][i] = grid[i][m + 1] = 10; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { for (int k = 0; k < 4; k++) { if (not grid[i + mv[k][0]][j + mv[k][1]]) { ok[i][j][k] = 1; OK[i][j] = 1; } } if (grid[i][j] and OK[i][j]) { av[grid[i][j]].insert({i, j}); } } } while (true) { for (int t = 1; t <= p; t++) { queue<pair<pair<int, int>, int>> Q; for (auto &i : av[t]) { Q.push({i, 0}); } while (!Q.empty()) { auto pole = Q.front().first; int w = Q.front().second; Q.pop(); av[t].erase(pole); int i = pole.first, j = pole.second; for (int k = 0; k < 4; k++) { int x = i + mv[k][0], y = j + mv[k][1]; if (not grid[x][y]) { grid[x][y] = t; if (w + 1 < s[t]) { Q.push({{x, y}, w + 1}); } av[t].insert({x, y}); } } } } bool Ok = 0; for (int t = 1; t <= p; t++) { if (not av[t].empty()) { Ok = 1; break; } } if (not Ok) { break; } } for (int t = 1; t <= p; t++) { int ctr = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { ctr += (grid[i][j] == t); } } cout << ctr << " "; } cout << "\n"; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int MAXN = 1e3 + 5, inf = 1e9; const ll INF = 1e18; const ld PI = 3.1415926535897932384626433832795; int mv[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int rmv[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}; int ok[MAXN][MAXN][4], OK[MAXN][MAXN], grid[MAXN][MAXN], s[MAXN]; set<pair<int, int>> av[11]; vector<int> G[MAXN], castles[10]; vector<ll> V; set<ll> S; map<ll, int> M; stack<ll> St; queue<ll> Q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << setprecision(13) << fixed; int n, m, p; cin >> n >> m >> p; for (int i = 1; i <= p; i++) { cin >> s[i]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char c; cin >> c; if (c != '.') { if (c == '#') { grid[i][j] = 10; } else { grid[i][j] = c - '0'; } } } } for (int i = 0; i <= max(n, m) + 1; i++) { grid[0][i] = grid[i][0] = grid[n + 1][i] = grid[i][m + 1] = 10; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { for (int k = 0; k < 4; k++) { if (not grid[i + mv[k][0]][j + mv[k][1]]) { ok[i][j][k] = 1; OK[i][j] = 1; } } if (grid[i][j] and OK[i][j]) { av[grid[i][j]].insert({i, j}); } } } while (true) { for (int t = 1; t <= p; t++) { queue<pair<pair<int, int>, int>> Q; for (auto &i : av[t]) { Q.push({i, 0}); } while (!Q.empty()) { auto pole = Q.front().first; int w = Q.front().second; Q.pop(); av[t].erase(pole); int i = pole.first, j = pole.second; for (int k = 0; k < 4; k++) { int x = i + mv[k][0], y = j + mv[k][1]; if (not grid[x][y]) { grid[x][y] = t; if (w + 1 < s[t]) { Q.push({{x, y}, w + 1}); } av[t].insert({x, y}); } } } } bool Ok = 0; for (int t = 1; t <= p; t++) { if (not av[t].empty()) { Ok = 1; break; } } if (not Ok) { break; } } for (int t = 1; t <= p; t++) { int ctr = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { ctr += (grid[i][j] == t); } } cout << ctr << " "; } cout << "\n"; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)1e3 + 5; const int INF = (int)1e9; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; queue<pair<int, int> > q[10]; int d[10][MAXN][MAXN]; int col[MAXN][MAXN]; char s[MAXN][MAXN]; int speed[MAXN]; int ans[MAXN]; int n, m, p; void bfs() { for (int x = 0; x < n; ++x) { for (int y = 0; y < m; ++y) { col[x][y] = -1; } } for (int i = 1; i <= p; ++i) { for (int x = 0; x < n; ++x) { for (int y = 0; y < m; ++y) { d[i][x][y] = -1; if (s[x][y] == i + '0') { q[i].push(make_pair(x, y)); d[i][x][y] = 0; col[x][y] = i; } } } } vector<pair<int, int> > colored, mem; while (1) { bool leave = 1; for (int i = 1; i <= p; ++i) { colored.clear(); mem.clear(); leave &= q[i].empty(); while (!q[i].empty()) { int cx = q[i].front().first; int cy = q[i].front().second; q[i].pop(); if (d[i][cx][cy] == speed[i]) { mem.push_back(make_pair(cx, cy)); continue; } for (int dir = 0; dir < 4; ++dir) { int nx = cx + dx[dir]; int ny = cy + dy[dir]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) { continue; } if (s[nx][ny] == '#' || col[nx][ny] != -1) { continue; } if (d[i][nx][ny] == -1) { d[i][nx][ny] = d[i][cx][cy] + 1; col[nx][ny] = col[cx][cy]; colored.push_back(make_pair(nx, ny)); q[i].push(make_pair(nx, ny)); } } } for (auto it : colored) { d[i][it.first][it.second] = 0; } for (auto it : mem) { q[i].push(it); } } if (leave) { break; } } } void solve() { scanf("%d %d %d", &n, &m, &p); for (int i = 1; i <= p; ++i) { scanf("%d", &speed[i]); } for (int i = 0; i < n; ++i) { scanf("%s", s[i]); } bfs(); for (int x = 0; x < n; ++x) { for (int y = 0; y < m; ++y) { if (col[x][y] != -1) { ++ans[col[x][y]]; } } } for (int i = 1; i <= p; ++i) { printf("%d ", ans[i]); } } int main() { int tt = 1; while (tt--) { solve(); } return 0; }
### Prompt Please create a solution in cpp to the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = (int)1e3 + 5; const int INF = (int)1e9; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; queue<pair<int, int> > q[10]; int d[10][MAXN][MAXN]; int col[MAXN][MAXN]; char s[MAXN][MAXN]; int speed[MAXN]; int ans[MAXN]; int n, m, p; void bfs() { for (int x = 0; x < n; ++x) { for (int y = 0; y < m; ++y) { col[x][y] = -1; } } for (int i = 1; i <= p; ++i) { for (int x = 0; x < n; ++x) { for (int y = 0; y < m; ++y) { d[i][x][y] = -1; if (s[x][y] == i + '0') { q[i].push(make_pair(x, y)); d[i][x][y] = 0; col[x][y] = i; } } } } vector<pair<int, int> > colored, mem; while (1) { bool leave = 1; for (int i = 1; i <= p; ++i) { colored.clear(); mem.clear(); leave &= q[i].empty(); while (!q[i].empty()) { int cx = q[i].front().first; int cy = q[i].front().second; q[i].pop(); if (d[i][cx][cy] == speed[i]) { mem.push_back(make_pair(cx, cy)); continue; } for (int dir = 0; dir < 4; ++dir) { int nx = cx + dx[dir]; int ny = cy + dy[dir]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) { continue; } if (s[nx][ny] == '#' || col[nx][ny] != -1) { continue; } if (d[i][nx][ny] == -1) { d[i][nx][ny] = d[i][cx][cy] + 1; col[nx][ny] = col[cx][cy]; colored.push_back(make_pair(nx, ny)); q[i].push(make_pair(nx, ny)); } } } for (auto it : colored) { d[i][it.first][it.second] = 0; } for (auto it : mem) { q[i].push(it); } } if (leave) { break; } } } void solve() { scanf("%d %d %d", &n, &m, &p); for (int i = 1; i <= p; ++i) { scanf("%d", &speed[i]); } for (int i = 0; i < n; ++i) { scanf("%s", s[i]); } bfs(); for (int x = 0; x < n; ++x) { for (int y = 0; y < m; ++y) { if (col[x][y] != -1) { ++ans[col[x][y]]; } } } for (int i = 1; i <= p; ++i) { printf("%d ", ans[i]); } } int main() { int tt = 1; while (tt--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int t, n, m, x, y, c, k, p, newx, newy, xx, yy, mov; int f[N], a[N], b[N], sp[4]; char s[1004][1004]; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; bool vis[1003][1003]; bool valid(int i, int j) { return (i >= 0 && i < n && j >= 0 && j < m && s[i][j] == '.'); } vector<queue<pair<int, int>>> q(10); pair<int, int> pt; void bfs(int pl) { while (t < p) { mov = a[pl]; while (!q[pl].empty() && mov) { k = q[pl].size(); bool bb = 0; for (int j = 0; j < (int)k; ++j) { pt = q[pl].front(); q[pl].pop(); for (int i = 0; i < (int)4; ++i) { newx = pt.first + dx[i]; newy = pt.second + dy[i]; if (valid(newx, newy)) { bb = 1; q[pl].push({newx, newy}); if (vis[newx][newy] == 0) ++f[pl], s[newx][newy] = pl + '0'; } } } if (bb) --mov; } if (q[pl].empty() && !b[pl]) ++t, b[pl] = 1; ++pl; if (pl > p) pl = 1; } } int main() { scanf("%d%d%d", &n, &m, &p); for (int i = 1; i <= (int)p; ++i) scanf("%d", &a[i]); for (int i = 0; i < (int)n; ++i) scanf("%s", s[i]); for (int i = 0; i < (int)n; ++i) for (int j = 0; j < (int)m; ++j) if (s[i][j] != '.' && s[i][j] != '#') q[s[i][j] - '0'].push({i, j}), ++f[s[i][j] - '0']; bfs(1); for (int i = 1; i <= (int)p; ++i) printf("%d ", f[i]); return 0; }
### Prompt Generate a Cpp solution to the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int t, n, m, x, y, c, k, p, newx, newy, xx, yy, mov; int f[N], a[N], b[N], sp[4]; char s[1004][1004]; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; bool vis[1003][1003]; bool valid(int i, int j) { return (i >= 0 && i < n && j >= 0 && j < m && s[i][j] == '.'); } vector<queue<pair<int, int>>> q(10); pair<int, int> pt; void bfs(int pl) { while (t < p) { mov = a[pl]; while (!q[pl].empty() && mov) { k = q[pl].size(); bool bb = 0; for (int j = 0; j < (int)k; ++j) { pt = q[pl].front(); q[pl].pop(); for (int i = 0; i < (int)4; ++i) { newx = pt.first + dx[i]; newy = pt.second + dy[i]; if (valid(newx, newy)) { bb = 1; q[pl].push({newx, newy}); if (vis[newx][newy] == 0) ++f[pl], s[newx][newy] = pl + '0'; } } } if (bb) --mov; } if (q[pl].empty() && !b[pl]) ++t, b[pl] = 1; ++pl; if (pl > p) pl = 1; } } int main() { scanf("%d%d%d", &n, &m, &p); for (int i = 1; i <= (int)p; ++i) scanf("%d", &a[i]); for (int i = 0; i < (int)n; ++i) scanf("%s", s[i]); for (int i = 0; i < (int)n; ++i) for (int j = 0; j < (int)m; ++j) if (s[i][j] != '.' && s[i][j] != '#') q[s[i][j] - '0'].push({i, j}), ++f[s[i][j] - '0']; bfs(1); for (int i = 1; i <= (int)p; ++i) printf("%d ", f[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXX = 1e3 + 10, N = 12, inf = 1e7 + 10; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int n, m, p, a[N], dis[MAXX][MAXX], ans[N]; string s[MAXX]; bool vis[MAXX][MAXX]; deque<pair<int, int>> dq[N]; bool Ok() { for (int i = 1; i <= p; i++) if (dq[i].size()) return 1; return 0; } bool isValid(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m && s[x][y] == '.' && !vis[x][y]); } void bfs(int v) { deque<pair<int, int>> dq2 = dq[v]; while (dq2.size()) dis[dq2.front().first][dq2.front().second] = 0, dq2.pop_front(); while (dq[v].size()) { int x = dq[v].front().first, y = dq[v].front().second; s[x][y] = v + '0'; if (dis[x][y] == a[v]) return; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (isValid(nx, ny)) dq[v].push_back({nx, ny}), s[nx][ny] = s[x][y], dis[nx][ny] = dis[x][y] + 1; } dq[v].pop_front(); } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m >> p; for (int i = 1; i <= p; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] >= '1' && s[i][j] <= '9') dq[(s[i][j] - '0')].push_back({i, j}); while (Ok()) for (int i = 1; i <= p; i++) bfs(i); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] >= '1' && s[i][j] <= '9') ans[s[i][j] - '0']++; for (int i = 1; i <= p; i++) cout << ans[i] << " "; return cout << endl, 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXX = 1e3 + 10, N = 12, inf = 1e7 + 10; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int n, m, p, a[N], dis[MAXX][MAXX], ans[N]; string s[MAXX]; bool vis[MAXX][MAXX]; deque<pair<int, int>> dq[N]; bool Ok() { for (int i = 1; i <= p; i++) if (dq[i].size()) return 1; return 0; } bool isValid(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m && s[x][y] == '.' && !vis[x][y]); } void bfs(int v) { deque<pair<int, int>> dq2 = dq[v]; while (dq2.size()) dis[dq2.front().first][dq2.front().second] = 0, dq2.pop_front(); while (dq[v].size()) { int x = dq[v].front().first, y = dq[v].front().second; s[x][y] = v + '0'; if (dis[x][y] == a[v]) return; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (isValid(nx, ny)) dq[v].push_back({nx, ny}), s[nx][ny] = s[x][y], dis[nx][ny] = dis[x][y] + 1; } dq[v].pop_front(); } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m >> p; for (int i = 1; i <= p; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] >= '1' && s[i][j] <= '9') dq[(s[i][j] - '0')].push_back({i, j}); while (Ok()) for (int i = 1; i <= p; i++) bfs(i); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] >= '1' && s[i][j] <= '9') ans[s[i][j] - '0']++; for (int i = 1; i <= p; i++) cout << ans[i] << " "; return cout << endl, 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 100; char G[maxn][maxn]; bool vis[maxn][maxn]; int dist1[4] = {0, 0, -1, 1}; int dist2[4] = {1, -1, 0, 0}; int N; int M; int num; int NM; int s[15]; int ans[15] = {0}; queue<pair<int, int> > q[12]; void bfs(int cd) { int sz = q[cd].size(); while (sz--) { pair<int, int> top = q[cd].front(); q[cd].pop(); int fir = top.first; int sec = top.second; for (int i = 0; i < 4; i++) { int x = fir + dist1[i]; int y = sec + dist2[i]; if (x < 1 || x > N) continue; if (y < 1 || y > M) continue; if (vis[x][y] == true) continue; vis[x][y] = true; q[cd].push(pair<int, int>(x, y)); ans[cd] += 1; NM -= 1; } } } int main() { cin >> N >> M >> num; for (int i = 1; i <= num; i++) cin >> s[i]; for (int i = 1; i <= N; i++) cin >> G[i] + 1; NM = N * M; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { if (G[i][j] == '#') { NM -= 1; vis[i][j] = true; } else if (G[i][j] == '.') continue; else { int c = G[i][j] - '0'; vis[i][j] = true; q[c].push(pair<int, int>(i, j)); NM -= 1; ans[c] += 1; } } } int cd = 1; while (NM != 0) { int last = NM; for (int cd = 1; cd <= num; cd++) { for (int i = 1; i <= s[cd] && NM > 0; i++) { if (!q[cd].empty()) bfs(cd); else break; } } if (last == NM) break; } for (int i = 1; i <= num; i++) cout << ans[i] << ' '; return 0; }
### Prompt Generate a cpp solution to the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 100; char G[maxn][maxn]; bool vis[maxn][maxn]; int dist1[4] = {0, 0, -1, 1}; int dist2[4] = {1, -1, 0, 0}; int N; int M; int num; int NM; int s[15]; int ans[15] = {0}; queue<pair<int, int> > q[12]; void bfs(int cd) { int sz = q[cd].size(); while (sz--) { pair<int, int> top = q[cd].front(); q[cd].pop(); int fir = top.first; int sec = top.second; for (int i = 0; i < 4; i++) { int x = fir + dist1[i]; int y = sec + dist2[i]; if (x < 1 || x > N) continue; if (y < 1 || y > M) continue; if (vis[x][y] == true) continue; vis[x][y] = true; q[cd].push(pair<int, int>(x, y)); ans[cd] += 1; NM -= 1; } } } int main() { cin >> N >> M >> num; for (int i = 1; i <= num; i++) cin >> s[i]; for (int i = 1; i <= N; i++) cin >> G[i] + 1; NM = N * M; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { if (G[i][j] == '#') { NM -= 1; vis[i][j] = true; } else if (G[i][j] == '.') continue; else { int c = G[i][j] - '0'; vis[i][j] = true; q[c].push(pair<int, int>(i, j)); NM -= 1; ans[c] += 1; } } } int cd = 1; while (NM != 0) { int last = NM; for (int cd = 1; cd <= num; cd++) { for (int i = 1; i <= s[cd] && NM > 0; i++) { if (!q[cd].empty()) bfs(cd); else break; } } if (last == NM) break; } for (int i = 1; i <= num; i++) cout << ans[i] << ' '; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, p; cin >> n >> m >> p; vector<int> s(p + 1); for (int i = 1; i <= p; ++i) { cin >> s[i]; } struct Pos { int x, y; }; vector<queue<Pos>> q(p + 1); vector<vector<char>> g(n + 2, vector<char>(m + 2, '#')); vector<int> resp(p + 1, 0); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> g[i][j]; if (g[i][j] > '0' && g[i][j] <= '9') { q[g[i][j] - '0'].push({i, j}); resp[g[i][j] - '0']++; } } } int roff[]{0, 1, 0, -1}; int coff[]{-1, 0, 1, 0}; bool hasTurn = true; int time = 0; while (hasTurn) { hasTurn = false; for (int i = 1; i <= p; ++i) { int j = s[i]; while (j-- && !q[i].empty()) { int sz = q[i].size(); while (sz-- > 0) { auto u = q[i].front(); q[i].pop(); for (int k = 0; k < 4; ++k) { int r = u.x + roff[k]; int c = u.y + coff[k]; if (g[r][c] == '.') { g[r][c] = '0' + i; resp[i]++; hasTurn = true; q[i].push({r, c}); } } } } } } for (int i = 1; i <= p; ++i) cout << resp[i] << " "; return 0; }
### Prompt Please create a solution in cpp to the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, p; cin >> n >> m >> p; vector<int> s(p + 1); for (int i = 1; i <= p; ++i) { cin >> s[i]; } struct Pos { int x, y; }; vector<queue<Pos>> q(p + 1); vector<vector<char>> g(n + 2, vector<char>(m + 2, '#')); vector<int> resp(p + 1, 0); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> g[i][j]; if (g[i][j] > '0' && g[i][j] <= '9') { q[g[i][j] - '0'].push({i, j}); resp[g[i][j] - '0']++; } } } int roff[]{0, 1, 0, -1}; int coff[]{-1, 0, 1, 0}; bool hasTurn = true; int time = 0; while (hasTurn) { hasTurn = false; for (int i = 1; i <= p; ++i) { int j = s[i]; while (j-- && !q[i].empty()) { int sz = q[i].size(); while (sz-- > 0) { auto u = q[i].front(); q[i].pop(); for (int k = 0; k < 4; ++k) { int r = u.x + roff[k]; int c = u.y + coff[k]; if (g[r][c] == '.') { g[r][c] = '0' + i; resp[i]++; hasTurn = true; q[i].push({r, c}); } } } } } } for (int i = 1; i <= p; ++i) cout << resp[i] << " "; return 0; } ```
#include <bits/stdc++.h> using namespace std; int vis[1005][1005], sp[1005], cnt[1005]; char s[1005][1005]; int dx[] = {0, 0, 1, -1}; int dy[] = {-1, 1, 0, 0}; struct node { int x, y, steps; }; int main() { int n, m, p; cin >> n >> m >> p; for (int i = 1; i <= p; i++) scanf("%d", &sp[i]); queue<node> q[10]; for (int i = 1; i <= n; i++) { scanf("%s", s[i] + 1); for (int j = 1; j <= m; j++) if (s[i][j] >= '1' && s[i][j] <= '9') { vis[i][j] = i; cnt[s[i][j] - '0']++; q[s[i][j] - '0'].push(node{i, j, 0}); } else if (s[i][j] == '#') vis[i][j] = 10; } while (1) { int ok = 0; for (int i = 1; i <= p; i++) if (!q[i].empty()) { ok = 1; break; } if (!ok) break; for (int i = 1; i <= p; i++) { queue<node> q2; while (!q[i].empty()) { q2.push(q[i].front()); q[i].pop(); } while (!q2.empty()) { node tmp = q2.front(); q2.pop(); int xx = tmp.x, yy = tmp.y; for (int j = 0; j < 4; j++) { int newx = xx + dx[j], newy = yy + dy[j], st = tmp.steps + 1; if (!vis[newx][newy] && newx >= 1 && newx <= n && newy >= 1 && newy <= m && st <= sp[i]) { q[i].push(node{newx, newy, 0}); q2.push(node{newx, newy, st}); vis[newx][newy] = 1; cnt[i]++; } } } } } for (int i = 1; i <= p; i++) printf("%d ", cnt[i]); return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int vis[1005][1005], sp[1005], cnt[1005]; char s[1005][1005]; int dx[] = {0, 0, 1, -1}; int dy[] = {-1, 1, 0, 0}; struct node { int x, y, steps; }; int main() { int n, m, p; cin >> n >> m >> p; for (int i = 1; i <= p; i++) scanf("%d", &sp[i]); queue<node> q[10]; for (int i = 1; i <= n; i++) { scanf("%s", s[i] + 1); for (int j = 1; j <= m; j++) if (s[i][j] >= '1' && s[i][j] <= '9') { vis[i][j] = i; cnt[s[i][j] - '0']++; q[s[i][j] - '0'].push(node{i, j, 0}); } else if (s[i][j] == '#') vis[i][j] = 10; } while (1) { int ok = 0; for (int i = 1; i <= p; i++) if (!q[i].empty()) { ok = 1; break; } if (!ok) break; for (int i = 1; i <= p; i++) { queue<node> q2; while (!q[i].empty()) { q2.push(q[i].front()); q[i].pop(); } while (!q2.empty()) { node tmp = q2.front(); q2.pop(); int xx = tmp.x, yy = tmp.y; for (int j = 0; j < 4; j++) { int newx = xx + dx[j], newy = yy + dy[j], st = tmp.steps + 1; if (!vis[newx][newy] && newx >= 1 && newx <= n && newy >= 1 && newy <= m && st <= sp[i]) { q[i].push(node{newx, newy, 0}); q2.push(node{newx, newy, st}); vis[newx][newy] = 1; cnt[i]++; } } } } } for (int i = 1; i <= p; i++) printf("%d ", cnt[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; inline void solve() { long long int n, m, p; cin >> n >> m >> p; long long int val[p + 2]; for (long long int i = 1; i < p + 1; i++) { cin >> val[i]; } char a[n + 2][m + 2]; long long int req = n * m; queue<pair<long long int, long long int> > q[p + 2]; for (long long int i = 1; i < n + 1; i++) { for (long long int j = 1; j < m + 1; j++) { cin >> a[i][j]; if (a[i][j] >= '1' && a[i][j] <= '9') { q[a[i][j] - '0'].push({i, j}); } if (a[i][j] == '#') { req--; } } } long long int dist[n + 2][m + 2]; bool vis[n + 2][m + 2]; for (long long int i = 0; i < n + 2; i++) { for (long long int j = 0; j < m + 2; j++) { dist[i][j] = 0; vis[i][j] = false; } } long long int cnt = 1; while (cnt >= 1) { cnt = 0; for (long long int i = 1; i < p + 1; i++) { queue<pair<long long int, long long int> > cmp; while (!q[i].empty()) { pair<long long int, long long int> p1 = q[i].front(); q[i].pop(); long long int x = p1.first, y = p1.second; if (vis[x][y] == true) { continue; } if (a[x][y] == '#') { continue; } if (a[x][y] != '.' && a[x][y] != ('0' + i)) { continue; } a[x][y] = '0' + i; if (dist[x][y] == val[i]) { cnt++; cmp.push({x, y}); continue; } vis[x][y] = true; if (vis[x + 1][y] == false && x + 1 <= n && (a[x + 1][y] == '.')) { dist[x + 1][y] = dist[x][y] + 1; q[i].push({x + 1, y}); } if (x - 1 >= 1 && vis[x - 1][y] == false && (a[x - 1][y] == '.')) { dist[x - 1][y] = dist[x][y] + 1; q[i].push({x - 1, y}); } if (y + 1 <= m && vis[x][y + 1] == false && (a[x][y + 1] == '.')) { dist[x][y + 1] = dist[x][y] + 1; q[i].push({x, y + 1}); } if (y - 1 >= 1 && vis[x][y - 1] == false && (a[x][y - 1] == '.')) { dist[x][y - 1] = dist[x][y] + 1; q[i].push({x, y - 1}); } } while (!cmp.empty()) { pair<long long int, long long int> p1 = cmp.front(); dist[p1.first][p1.second] = 0; q[i].push(p1); cmp.pop(); } } } long long int ans[p + 2]; for (long long int i = 1; i < p + 2; i++) { ans[i] = 0; } for (long long int i = 1; i < n + 1; i++) { for (long long int j = 1; j < m + 1; j++) { if (a[i][j] >= '1' && a[i][j] <= '9') { ans[a[i][j] - '0']++; } } } for (long long int i = 1; i < p + 1; i++) { cout << ans[i] << " "; ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tests = 1; while (tests--) { solve(); } }
### Prompt Please provide a Cpp coded solution to the problem described below: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline void solve() { long long int n, m, p; cin >> n >> m >> p; long long int val[p + 2]; for (long long int i = 1; i < p + 1; i++) { cin >> val[i]; } char a[n + 2][m + 2]; long long int req = n * m; queue<pair<long long int, long long int> > q[p + 2]; for (long long int i = 1; i < n + 1; i++) { for (long long int j = 1; j < m + 1; j++) { cin >> a[i][j]; if (a[i][j] >= '1' && a[i][j] <= '9') { q[a[i][j] - '0'].push({i, j}); } if (a[i][j] == '#') { req--; } } } long long int dist[n + 2][m + 2]; bool vis[n + 2][m + 2]; for (long long int i = 0; i < n + 2; i++) { for (long long int j = 0; j < m + 2; j++) { dist[i][j] = 0; vis[i][j] = false; } } long long int cnt = 1; while (cnt >= 1) { cnt = 0; for (long long int i = 1; i < p + 1; i++) { queue<pair<long long int, long long int> > cmp; while (!q[i].empty()) { pair<long long int, long long int> p1 = q[i].front(); q[i].pop(); long long int x = p1.first, y = p1.second; if (vis[x][y] == true) { continue; } if (a[x][y] == '#') { continue; } if (a[x][y] != '.' && a[x][y] != ('0' + i)) { continue; } a[x][y] = '0' + i; if (dist[x][y] == val[i]) { cnt++; cmp.push({x, y}); continue; } vis[x][y] = true; if (vis[x + 1][y] == false && x + 1 <= n && (a[x + 1][y] == '.')) { dist[x + 1][y] = dist[x][y] + 1; q[i].push({x + 1, y}); } if (x - 1 >= 1 && vis[x - 1][y] == false && (a[x - 1][y] == '.')) { dist[x - 1][y] = dist[x][y] + 1; q[i].push({x - 1, y}); } if (y + 1 <= m && vis[x][y + 1] == false && (a[x][y + 1] == '.')) { dist[x][y + 1] = dist[x][y] + 1; q[i].push({x, y + 1}); } if (y - 1 >= 1 && vis[x][y - 1] == false && (a[x][y - 1] == '.')) { dist[x][y - 1] = dist[x][y] + 1; q[i].push({x, y - 1}); } } while (!cmp.empty()) { pair<long long int, long long int> p1 = cmp.front(); dist[p1.first][p1.second] = 0; q[i].push(p1); cmp.pop(); } } } long long int ans[p + 2]; for (long long int i = 1; i < p + 2; i++) { ans[i] = 0; } for (long long int i = 1; i < n + 1; i++) { for (long long int j = 1; j < m + 1; j++) { if (a[i][j] >= '1' && a[i][j] <= '9') { ans[a[i][j] - '0']++; } } } for (long long int i = 1; i < p + 1; i++) { cout << ans[i] << " "; ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tests = 1; while (tests--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; void setupIO(const string &PROB) { ios::sync_with_stdio(false); cin.tie(nullptr); ifstream infile(PROB + ".in"); if (infile.good()) { freopen((PROB + ".in").c_str(), "r", stdin); freopen((PROB + ".out").c_str(), "w", stdout); } } int s[1000]; char grid[1000][1000]; int dist[1000][1000]; int n, m, p; int main() { cin >> n >> m >> p; for (int i = 0; i < (p); i++) cin >> s[i]; int ans[p]; for (int i = 0; i < p; i++) ans[i] = 0; ; for (int i = 0; i < (1000); i++) for (int j = 0; j < (1000); j++) dist[i][j] = 0; ; queue<pair<int, int> > players[p]; for (int i = 0; i < (n); i++) { for (int j = 0; j < (m); j++) { cin >> grid[i][j]; if ('1' <= grid[i][j] && grid[i][j] <= '9') { players[grid[i][j] - '1'].push(make_pair(i, j)); ans[grid[i][j] - '1']++; } } } bool change = true; int iteration = 1; while (change) { change = false; for (int i = 0; i < (p); i++) { while (!players[i].empty()) { pair<int, int> next = players[i].front(); if (dist[next.first][next.second] >= iteration * s[i]) break; players[i].pop(); int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; for (int j = 0; j < (4); j++) { pair<int, int> x = {next.first + dx[j], next.second + dy[j]}; if (x.first < 0 || x.first >= n || x.second < 0 || x.second >= m || grid[x.first][x.second] != '.') continue; grid[x.first][x.second] = '1' + i; ans[i]++; players[i].push(x); dist[x.first][x.second] = dist[next.first][next.second] + 1; change = true; } } } iteration++; } cout << ans[0]; for (int i = 1; i < (p); i++) cout << " " << ans[i]; cout << endl; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void setupIO(const string &PROB) { ios::sync_with_stdio(false); cin.tie(nullptr); ifstream infile(PROB + ".in"); if (infile.good()) { freopen((PROB + ".in").c_str(), "r", stdin); freopen((PROB + ".out").c_str(), "w", stdout); } } int s[1000]; char grid[1000][1000]; int dist[1000][1000]; int n, m, p; int main() { cin >> n >> m >> p; for (int i = 0; i < (p); i++) cin >> s[i]; int ans[p]; for (int i = 0; i < p; i++) ans[i] = 0; ; for (int i = 0; i < (1000); i++) for (int j = 0; j < (1000); j++) dist[i][j] = 0; ; queue<pair<int, int> > players[p]; for (int i = 0; i < (n); i++) { for (int j = 0; j < (m); j++) { cin >> grid[i][j]; if ('1' <= grid[i][j] && grid[i][j] <= '9') { players[grid[i][j] - '1'].push(make_pair(i, j)); ans[grid[i][j] - '1']++; } } } bool change = true; int iteration = 1; while (change) { change = false; for (int i = 0; i < (p); i++) { while (!players[i].empty()) { pair<int, int> next = players[i].front(); if (dist[next.first][next.second] >= iteration * s[i]) break; players[i].pop(); int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; for (int j = 0; j < (4); j++) { pair<int, int> x = {next.first + dx[j], next.second + dy[j]}; if (x.first < 0 || x.first >= n || x.second < 0 || x.second >= m || grid[x.first][x.second] != '.') continue; grid[x.first][x.second] = '1' + i; ans[i]++; players[i].push(x); dist[x.first][x.second] = dist[next.first][next.second] + 1; change = true; } } } iteration++; } cout << ans[0]; for (int i = 1; i < (p); i++) cout << " " << ans[i]; cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f; char ma[1005][1005]; vector<pair<pair<int, int>, int> > v[20]; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}, s[20], vis[1005][1005], ans[20], n, m, p; bool bfs(int id) { queue<pair<pair<int, int>, int> > q; for (auto& i : v[id]) q.push(i); v[id].clear(); while (!q.empty()) { pair<pair<int, int>, int> tmp = q.front(); q.pop(); int d = tmp.second, x = tmp.first.first, y = tmp.first.second; if (d == s[id]) { v[id].push_back(make_pair(make_pair(x, y), 0)); continue; } for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (0 <= nx && nx < n && 0 <= ny && ny < m && ma[nx][ny] != '#' && !vis[nx][ny]) q.push(make_pair(make_pair(nx, ny), d + 1)), vis[nx][ny] = id; } } return int(v[id].size()); } int main() { cin >> n >> m >> p; for (int i = 1; i <= p; ++i) cin >> s[i]; for (int i = 0; i < n; ++i) scanf("%s", ma[i]); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (isdigit(ma[i][j])) v[ma[i][j] - '0'].push_back(make_pair(make_pair(i, j), 0)), vis[i][j] = ma[i][j] - '0'; } } bool f = 1; while (f) { f = 0; for (int i = 1; i <= p; ++i) if (bfs(i)) f = 1; } for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) ans[vis[i][j]]++; for (int i = 1; i <= p; ++i) cout << ans[i] << " "; return 0; }
### Prompt Generate a cpp solution to the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f; char ma[1005][1005]; vector<pair<pair<int, int>, int> > v[20]; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}, s[20], vis[1005][1005], ans[20], n, m, p; bool bfs(int id) { queue<pair<pair<int, int>, int> > q; for (auto& i : v[id]) q.push(i); v[id].clear(); while (!q.empty()) { pair<pair<int, int>, int> tmp = q.front(); q.pop(); int d = tmp.second, x = tmp.first.first, y = tmp.first.second; if (d == s[id]) { v[id].push_back(make_pair(make_pair(x, y), 0)); continue; } for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (0 <= nx && nx < n && 0 <= ny && ny < m && ma[nx][ny] != '#' && !vis[nx][ny]) q.push(make_pair(make_pair(nx, ny), d + 1)), vis[nx][ny] = id; } } return int(v[id].size()); } int main() { cin >> n >> m >> p; for (int i = 1; i <= p; ++i) cin >> s[i]; for (int i = 0; i < n; ++i) scanf("%s", ma[i]); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (isdigit(ma[i][j])) v[ma[i][j] - '0'].push_back(make_pair(make_pair(i, j), 0)), vis[i][j] = ma[i][j] - '0'; } } bool f = 1; while (f) { f = 0; for (int i = 1; i <= p; ++i) if (bfs(i)) f = 1; } for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) ans[vis[i][j]]++; for (int i = 1; i <= p; ++i) cout << ans[i] << " "; return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using P = pair<ll, ll>; const ll INF = 5e15; vector<vector<bool>> grid(1000, vector<bool>(1000, 0)); vector<ll> ans; vector<ll> S; ll H, W; ll dh[] = {1, 0, -1, 0}; ll dw[] = {0, 1, 0, -1}; vector<P> bfs(ll p, const vector<P> &starts) { queue<pair<P, ll>> que; for (const P &e : starts) que.push(make_pair(e, 0)); vector<P> ret; while (que.size()) { P now; ll d; tie(now, d) = que.front(); que.pop(); for (ll i = 0; i < 4; i++) { ll h = now.first + dh[i]; ll w = now.second + dw[i]; if (!(0 <= h && h < H && 0 <= w && w < W)) continue; if (grid[h][w]) continue; grid[h][w] = 1; ans[p]++; ret.push_back(P(h, w)); if (d + 1 < S[p]) que.push(make_pair(P(h, w), d + 1)); } } return move(ret); } int main() { ll PL; cin >> H >> W >> PL; ans.resize(PL); S.resize(PL); for (ll &e : S) cin >> e; vector<vector<P>> qv(PL); for (ll i = 0; i < H; i++) for (ll j = 0; j < W; j++) { char c; cin >> c; if (c == '.') continue; grid[i][j] = 1; if (c == '#') continue; ll n = c - '1'; qv[n].push_back(P(i, j)); ans[n]++; } while (1) { for (ll p = 0; p < PL; p++) qv[p] = bfs(p, qv[p]); bool b = 1; for (ll p = 0; p < PL; p++) if (qv[p].size()) b = 0; if (b) break; } for (ll i = 0; i < PL; i++) cout << ans[i] << " \n"[i == PL - 1]; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = int64_t; using P = pair<ll, ll>; const ll INF = 5e15; vector<vector<bool>> grid(1000, vector<bool>(1000, 0)); vector<ll> ans; vector<ll> S; ll H, W; ll dh[] = {1, 0, -1, 0}; ll dw[] = {0, 1, 0, -1}; vector<P> bfs(ll p, const vector<P> &starts) { queue<pair<P, ll>> que; for (const P &e : starts) que.push(make_pair(e, 0)); vector<P> ret; while (que.size()) { P now; ll d; tie(now, d) = que.front(); que.pop(); for (ll i = 0; i < 4; i++) { ll h = now.first + dh[i]; ll w = now.second + dw[i]; if (!(0 <= h && h < H && 0 <= w && w < W)) continue; if (grid[h][w]) continue; grid[h][w] = 1; ans[p]++; ret.push_back(P(h, w)); if (d + 1 < S[p]) que.push(make_pair(P(h, w), d + 1)); } } return move(ret); } int main() { ll PL; cin >> H >> W >> PL; ans.resize(PL); S.resize(PL); for (ll &e : S) cin >> e; vector<vector<P>> qv(PL); for (ll i = 0; i < H; i++) for (ll j = 0; j < W; j++) { char c; cin >> c; if (c == '.') continue; grid[i][j] = 1; if (c == '#') continue; ll n = c - '1'; qv[n].push_back(P(i, j)); ans[n]++; } while (1) { for (ll p = 0; p < PL; p++) qv[p] = bfs(p, qv[p]); bool b = 1; for (ll p = 0; p < PL; p++) if (qv[p].size()) b = 0; if (b) break; } for (ll i = 0; i < PL; i++) cout << ans[i] << " \n"[i == PL - 1]; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, m, p, s[15], ans[15]; char mdo[1010][1010]; queue<pair<long long, long long> > c; queue<pair<pair<long long, long long>, long long> > busq; pair<pair<long long, long long>, long long> A, B; pair<long long, long long> padre, h; long long dx[] = {1, 0, -1, 0}; long long dy[] = {0, 1, 0, -1}; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> p; for (long long i = 1; i <= p; i++) cin >> s[i]; for (long long i = 0; i < n; i++) for (long long j = 0; j < m; j++) cin >> mdo[i][j]; for (long long t = 1; t <= p; t++) { for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { if (mdo[i][j] - '0' == t) { ans[t]++; c.push({i, j}); } } } } while (!c.empty()) { padre = c.front(); c.pop(); while (!busq.empty()) busq.pop(); busq.push({padre, 0}); while (!busq.empty()) { A = busq.front(); A.second++; busq.pop(); for (long long i = 0; i < 4; i++) { B = A; B.first.first += dy[i]; B.first.second += dx[i]; if (B.first.first >= 0 && B.first.first < n && B.first.second >= 0 && B.first.second < m && mdo[B.first.first][B.first.second] == '.') { mdo[B.first.first][B.first.second] = mdo[padre.first][padre.second]; ans[mdo[padre.first][padre.second] - '0']++; if (B.second < s[mdo[padre.first][padre.second] - '0']) busq.push(B); if (B.second == s[mdo[padre.first][padre.second] - '0']) c.push(B.first); } } } } for (long long i = 1; i <= p; i++) cout << ans[i] << " "; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, m, p, s[15], ans[15]; char mdo[1010][1010]; queue<pair<long long, long long> > c; queue<pair<pair<long long, long long>, long long> > busq; pair<pair<long long, long long>, long long> A, B; pair<long long, long long> padre, h; long long dx[] = {1, 0, -1, 0}; long long dy[] = {0, 1, 0, -1}; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> p; for (long long i = 1; i <= p; i++) cin >> s[i]; for (long long i = 0; i < n; i++) for (long long j = 0; j < m; j++) cin >> mdo[i][j]; for (long long t = 1; t <= p; t++) { for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { if (mdo[i][j] - '0' == t) { ans[t]++; c.push({i, j}); } } } } while (!c.empty()) { padre = c.front(); c.pop(); while (!busq.empty()) busq.pop(); busq.push({padre, 0}); while (!busq.empty()) { A = busq.front(); A.second++; busq.pop(); for (long long i = 0; i < 4; i++) { B = A; B.first.first += dy[i]; B.first.second += dx[i]; if (B.first.first >= 0 && B.first.first < n && B.first.second >= 0 && B.first.second < m && mdo[B.first.first][B.first.second] == '.') { mdo[B.first.first][B.first.second] = mdo[padre.first][padre.second]; ans[mdo[padre.first][padre.second] - '0']++; if (B.second < s[mdo[padre.first][padre.second] - '0']) busq.push(B); if (B.second == s[mdo[padre.first][padre.second] - '0']) c.push(B.first); } } } } for (long long i = 1; i <= p; i++) cout << ans[i] << " "; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, m, p, x, y, z, now(1), sum, ans[10], a[10], dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0}; char c[1005][1005]; bitset<1005> vis[1005]; vector<pair<long long, long long>> v[10]; queue<pair<pair<long long, long long>, long long>> q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> p; for (int i = 1; i <= p; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> c[i][j]; if ('1' <= c[i][j] && c[i][j] <= '9') { v[c[i][j] - '0'].push_back(make_pair(i, j)); ans[c[i][j] - '0']++; vis[i][j] = true; } } } while (sum <= 9000000) { for (int i = 0; i < v[now].size(); i++) { q.push(make_pair(make_pair(v[now][i].first, v[now][i].second), a[now])); } v[now].clear(); while (q.size()) { x = q.front().first.first; y = q.front().first.second; z = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { if (!vis[x + dx[i]][y + dy[i]] && c[x + dx[i]][y + dy[i]] == '.') { ans[now]++; vis[x + dx[i]][y + dy[i]] = true; if (z == 1) { v[now].push_back(make_pair(x + dx[i], y + dy[i])); } else { q.push(make_pair(make_pair(x + dx[i], y + dy[i]), z - 1)); } } } } sum++, now++; if (now == p + 1) { now = 1; } } for (int i = 1; i <= p; i++) { cout << ans[i] << " "; } cout << '\n'; }
### Prompt Create a solution in CPP for the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, m, p, x, y, z, now(1), sum, ans[10], a[10], dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0}; char c[1005][1005]; bitset<1005> vis[1005]; vector<pair<long long, long long>> v[10]; queue<pair<pair<long long, long long>, long long>> q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> p; for (int i = 1; i <= p; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> c[i][j]; if ('1' <= c[i][j] && c[i][j] <= '9') { v[c[i][j] - '0'].push_back(make_pair(i, j)); ans[c[i][j] - '0']++; vis[i][j] = true; } } } while (sum <= 9000000) { for (int i = 0; i < v[now].size(); i++) { q.push(make_pair(make_pair(v[now][i].first, v[now][i].second), a[now])); } v[now].clear(); while (q.size()) { x = q.front().first.first; y = q.front().first.second; z = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { if (!vis[x + dx[i]][y + dy[i]] && c[x + dx[i]][y + dy[i]] == '.') { ans[now]++; vis[x + dx[i]][y + dy[i]] = true; if (z == 1) { v[now].push_back(make_pair(x + dx[i], y + dy[i])); } else { q.push(make_pair(make_pair(x + dx[i], y + dy[i]), z - 1)); } } } } sum++, now++; if (now == p + 1) { now = 1; } } for (int i = 1; i <= p; i++) { cout << ans[i] << " "; } cout << '\n'; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1010; const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0}; queue<pair<int, int> > q[10]; int n, m, p, d[N][N], s[10], ans[N]; char a[N]; bool chk(int x, int y) { return x && y && x <= n && y <= m && d[x][y] == -1; } int main() { scanf("%d%d%d", &n, &m, &p); for (int i = (1); i <= (p); i++) scanf("%d", s + i); for (int i = (1); i <= (n); i++) { scanf("%s", a + 1); for (int j = (1); j <= (m); j++) { if (a[j] == '.') d[i][j] = -1; else if (a[j] == '#') d[i][j] = 1; else q[a[j] - '0'].push(make_pair(i, j)), ++ans[a[j] - '0']; } } for (long long turn = 1;; ++turn) { bool f = 0; for (int i = (1); i <= (p); i++) { while (!q[i].empty()) { int x = q[i].front().first, y = q[i].front().second; if (d[x][y] >= turn * s[i]) break; for (int j = (0); j <= (3); j++) { int x1 = x + dx[j], y1 = y + dy[j]; if (chk(x1, y1)) d[x1][y1] = d[x][y] + 1, q[i].push(make_pair(x1, y1)), ++ans[i]; } q[i].pop(); } if (!q[i].empty()) f = 1; } if (!f) break; } for (int i = (1); i <= (p); i++) printf("%d ", ans[i]); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1010; const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0}; queue<pair<int, int> > q[10]; int n, m, p, d[N][N], s[10], ans[N]; char a[N]; bool chk(int x, int y) { return x && y && x <= n && y <= m && d[x][y] == -1; } int main() { scanf("%d%d%d", &n, &m, &p); for (int i = (1); i <= (p); i++) scanf("%d", s + i); for (int i = (1); i <= (n); i++) { scanf("%s", a + 1); for (int j = (1); j <= (m); j++) { if (a[j] == '.') d[i][j] = -1; else if (a[j] == '#') d[i][j] = 1; else q[a[j] - '0'].push(make_pair(i, j)), ++ans[a[j] - '0']; } } for (long long turn = 1;; ++turn) { bool f = 0; for (int i = (1); i <= (p); i++) { while (!q[i].empty()) { int x = q[i].front().first, y = q[i].front().second; if (d[x][y] >= turn * s[i]) break; for (int j = (0); j <= (3); j++) { int x1 = x + dx[j], y1 = y + dy[j]; if (chk(x1, y1)) d[x1][y1] = d[x][y] + 1, q[i].push(make_pair(x1, y1)), ++ans[i]; } q[i].pop(); } if (!q[i].empty()) f = 1; } if (!f) break; } for (int i = (1); i <= (p); i++) printf("%d ", ans[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m, p, arr[1005][1005], ans[1005], s[1005]; char a[1005][1005]; struct node { int xx, yy, zz; }; queue<node> ylh[10], Q; int dx[5] = {0, -1, 1, 0, 0}, dy[5] = {0, 0, 0, -1, 1}; void bfs(int who) { node tmp; int x, y, i; while (!Q.empty()) { tmp = Q.front(); Q.pop(); if (tmp.zz == 0) ylh[who].push(tmp); else for (i = 1; i <= 4; i++) { x = tmp.xx + dx[i]; y = tmp.yy + dy[i]; if (x < 1 || x > n || y < 1 || y > m || a[x][y] != '.' || arr[x][y] != 0) continue; arr[x][y] = who; Q.push(node{x, y, tmp.zz - 1}); } } return; } bool pand(int who) { node tmp; while (!ylh[who].empty()) { tmp = ylh[who].front(); ylh[who].pop(); tmp.zz = s[who]; Q.push(tmp); } bfs(who); return !ylh[who].empty(); } int main() { scanf("%d%d%d", &n, &m, &p); int i, j, www; for (i = 1; i <= p; i++) scanf("%d", &s[i]); for (i = 1; i <= n; i++) scanf("%s", a[i] + 1); for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { if (a[i][j] >= '0' && a[i][j] <= '0' + p) { www = a[i][j] - '0'; ylh[www].push(node{i, j, s[www]}); arr[i][j] = www; } } while (1) { bool tt1 = false; for (i = 1; i <= p; i++) tt1 |= pand(i); if (!tt1) break; } for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) ans[arr[i][j]]++; for (i = 1; i <= p; i++) printf("%d ", ans[i]); return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, p, arr[1005][1005], ans[1005], s[1005]; char a[1005][1005]; struct node { int xx, yy, zz; }; queue<node> ylh[10], Q; int dx[5] = {0, -1, 1, 0, 0}, dy[5] = {0, 0, 0, -1, 1}; void bfs(int who) { node tmp; int x, y, i; while (!Q.empty()) { tmp = Q.front(); Q.pop(); if (tmp.zz == 0) ylh[who].push(tmp); else for (i = 1; i <= 4; i++) { x = tmp.xx + dx[i]; y = tmp.yy + dy[i]; if (x < 1 || x > n || y < 1 || y > m || a[x][y] != '.' || arr[x][y] != 0) continue; arr[x][y] = who; Q.push(node{x, y, tmp.zz - 1}); } } return; } bool pand(int who) { node tmp; while (!ylh[who].empty()) { tmp = ylh[who].front(); ylh[who].pop(); tmp.zz = s[who]; Q.push(tmp); } bfs(who); return !ylh[who].empty(); } int main() { scanf("%d%d%d", &n, &m, &p); int i, j, www; for (i = 1; i <= p; i++) scanf("%d", &s[i]); for (i = 1; i <= n; i++) scanf("%s", a[i] + 1); for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { if (a[i][j] >= '0' && a[i][j] <= '0' + p) { www = a[i][j] - '0'; ylh[www].push(node{i, j, s[www]}); arr[i][j] = www; } } while (1) { bool tt1 = false; for (i = 1; i <= p; i++) tt1 |= pand(i); if (!tt1) break; } for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) ans[arr[i][j]]++; for (i = 1; i <= p; i++) printf("%d ", ans[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; double pi = acos(-1); long long int modexp(long long int a, int b) { long long int t = 1; while (b != 0) { if (b % 2 == 1) t = (t * a) % 1000000007; a = (a * a) % 1000000007; b /= 2; } return t % 1000000007; } int n = 1, m = 1, p = 1; int ps[10]; long long int f = 1; vector<vector<long long int>> v(10); vector<vector<long long int>> ori(10); vector<long long int> ne; int ma[2000][2000]; int flag[1500][1500]; int ti[1500][1500]; int be[1500][1500]; int change(int num, int error) { int i, j; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { flag[i][j] = 0; if (be[i][j] == num) { be[i][j] = -1; ti[i][j] = 1000000000; } } for (i = 0; i < v[num].size(); i++) { int x = v[num][i] / 1000; int y = v[num][i] % 1000 + 1; be[x][y] = num; ti[x][y] = 0; } while (v[num].size() != 0) { for (i = 0; i < v[num].size(); i++) { int x = v[num][i] / 1000; int y = v[num][i] % 1000 + 1; int now = flag[x][y] + 1; int nt; nt = now / ps[num]; if (now % ps[num] != 0) nt++; if (ma[x - 1][y] == 0 && x != 1) { if (flag[x - 1][y] == 0) { flag[x - 1][y] = flag[x][y] + 1; if (nt < ti[x - 1][y]) { if (be[x - 1][y] != -1) error = 1; ti[x - 1][y] = nt; be[x - 1][y] = num; ne.push_back((x - 1) * 1000 + y - 1); } } } if (ma[x + 1][y] == 0 && x != n) { if (flag[x + 1][y] == 0) { flag[x + 1][y] = flag[x][y] + 1; if (nt < ti[x + 1][y]) { if (be[x + 1][y] != -1) error = 1; ti[x + 1][y] = nt; be[x + 1][y] = num; ne.push_back((x + 1) * 1000 + y - 1); } } } if (ma[x][y - 1] == 0 && y != 1) { if (flag[x][y - 1] == 0) { flag[x][y - 1] = flag[x][y] + 1; if (nt < ti[x][y - 1]) { if (be[x][y - 1] != -1) error = 1; ti[x][y - 1] = nt; be[x][y - 1] = num; ne.push_back((x)*1000 + y - 2); } } } if (ma[x][y + 1] == 0 && y != m) { if (flag[x][y + 1] == 0) { if (nt < ti[x][y + 1]) { if (be[x][y + 1] != -1) error = 1; ti[x][y + 1] = nt; be[x][y + 1] = num; ne.push_back((x)*1000 + y); } flag[x][y + 1] = flag[x][y] + 1; } } } swap(v[num], ne); ne.clear(); } return error; } int main() { scanf("%d %d %d", &n, &m, &p); int i, j; char s[2000]; for (i = 1; i <= p; i++) { scanf("%d", &ps[i]); } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { be[i][j] = -1; ti[i][j] = 1000000000; } } for (i = 1; i <= n; i++) { scanf("%s", s + 1); for (j = 1; j <= m; j++) { if (s[j] == '.') ma[i][j] = 0; else if (s[j] == '#') ma[i][j] = -1; else { ma[i][j] = s[j] - '0'; v[ma[i][j]].push_back(i * 1000 + j - f); ori[ma[i][j]].push_back(i * 1000 + j - f); } } } for (i = 1; i <= p; i++) { v[i].clear(); for (j = 0; j < ori[i].size(); j++) v[i].push_back(ori[i][j]); int error = change(i, 0); if (error == 1) i = 0; } int sum[10] = {0}; for (int i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (be[i][j] != -1) sum[be[i][j]]++; } } for (i = 1; i <= p; i++) printf("%d ", sum[i]); printf("\n"); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; double pi = acos(-1); long long int modexp(long long int a, int b) { long long int t = 1; while (b != 0) { if (b % 2 == 1) t = (t * a) % 1000000007; a = (a * a) % 1000000007; b /= 2; } return t % 1000000007; } int n = 1, m = 1, p = 1; int ps[10]; long long int f = 1; vector<vector<long long int>> v(10); vector<vector<long long int>> ori(10); vector<long long int> ne; int ma[2000][2000]; int flag[1500][1500]; int ti[1500][1500]; int be[1500][1500]; int change(int num, int error) { int i, j; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { flag[i][j] = 0; if (be[i][j] == num) { be[i][j] = -1; ti[i][j] = 1000000000; } } for (i = 0; i < v[num].size(); i++) { int x = v[num][i] / 1000; int y = v[num][i] % 1000 + 1; be[x][y] = num; ti[x][y] = 0; } while (v[num].size() != 0) { for (i = 0; i < v[num].size(); i++) { int x = v[num][i] / 1000; int y = v[num][i] % 1000 + 1; int now = flag[x][y] + 1; int nt; nt = now / ps[num]; if (now % ps[num] != 0) nt++; if (ma[x - 1][y] == 0 && x != 1) { if (flag[x - 1][y] == 0) { flag[x - 1][y] = flag[x][y] + 1; if (nt < ti[x - 1][y]) { if (be[x - 1][y] != -1) error = 1; ti[x - 1][y] = nt; be[x - 1][y] = num; ne.push_back((x - 1) * 1000 + y - 1); } } } if (ma[x + 1][y] == 0 && x != n) { if (flag[x + 1][y] == 0) { flag[x + 1][y] = flag[x][y] + 1; if (nt < ti[x + 1][y]) { if (be[x + 1][y] != -1) error = 1; ti[x + 1][y] = nt; be[x + 1][y] = num; ne.push_back((x + 1) * 1000 + y - 1); } } } if (ma[x][y - 1] == 0 && y != 1) { if (flag[x][y - 1] == 0) { flag[x][y - 1] = flag[x][y] + 1; if (nt < ti[x][y - 1]) { if (be[x][y - 1] != -1) error = 1; ti[x][y - 1] = nt; be[x][y - 1] = num; ne.push_back((x)*1000 + y - 2); } } } if (ma[x][y + 1] == 0 && y != m) { if (flag[x][y + 1] == 0) { if (nt < ti[x][y + 1]) { if (be[x][y + 1] != -1) error = 1; ti[x][y + 1] = nt; be[x][y + 1] = num; ne.push_back((x)*1000 + y); } flag[x][y + 1] = flag[x][y] + 1; } } } swap(v[num], ne); ne.clear(); } return error; } int main() { scanf("%d %d %d", &n, &m, &p); int i, j; char s[2000]; for (i = 1; i <= p; i++) { scanf("%d", &ps[i]); } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { be[i][j] = -1; ti[i][j] = 1000000000; } } for (i = 1; i <= n; i++) { scanf("%s", s + 1); for (j = 1; j <= m; j++) { if (s[j] == '.') ma[i][j] = 0; else if (s[j] == '#') ma[i][j] = -1; else { ma[i][j] = s[j] - '0'; v[ma[i][j]].push_back(i * 1000 + j - f); ori[ma[i][j]].push_back(i * 1000 + j - f); } } } for (i = 1; i <= p; i++) { v[i].clear(); for (j = 0; j < ori[i].size(); j++) v[i].push_back(ori[i][j]); int error = change(i, 0); if (error == 1) i = 0; } int sum[10] = {0}; for (int i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (be[i][j] != -1) sum[be[i][j]]++; } } for (i = 1; i <= p; i++) printf("%d ", sum[i]); printf("\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long INFLL = 1e18; const int MOD = 1e9 + 7; const int MAXN = 1e3 + 5; int n, m, p; int s[10]; char g[MAXN][MAXN]; queue<pair<int, int> > nxt[10]; vector<pair<int, int> > temp; int ans[10]; int d[MAXN][MAXN]; int dr[4] = {0, 0, 1, -1}; int dc[4] = {1, -1, 0, 0}; bool valid(int r, int c) { return 0 <= r && r < n && 0 <= c && c < m; } void bfs() { memset(d, -1, sizeof(d)); memset(ans, 0, sizeof(ans)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { char c = g[i][j]; if (c != '.') { d[i][j] = 0; if (isdigit(c)) { nxt[c - '1'].push({i, j}); ans[c - '1']++; } } } bool f = 1; while (f) { for (int k = 0; k < p; k++) { temp.clear(); while (!nxt[k].empty()) { pair<int, int> pp = nxt[k].front(); nxt[k].pop(); int cr = pp.first; int cc = pp.second; for (int i = 0; i < 4; i++) { int r = cr + dr[i]; int c = cc + dc[i]; if (valid(r, c) && d[r][c] == -1) { ans[k]++; if (d[cr][cc] + 1 == s[k]) { d[r][c] = 0; temp.push_back({r, c}); continue; } d[r][c] = d[cr][cc] + 1; nxt[k].push({r, c}); } } } for (pair<int, int> pp : temp) nxt[k].push(pp); } f = 0; for (int i = 0; i < p; i++) f |= !nxt[i].empty(); } } int main() { scanf("%d %d %d", &n, &m, &p); for (int i = 0; i < p; i++) scanf("%d", &s[i]); for (int i = 0; i < n; i++) scanf("%s", g[i]); bfs(); for (int i = 0; i < p; i++) printf("%d ", ans[i]); printf("\n"); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long INFLL = 1e18; const int MOD = 1e9 + 7; const int MAXN = 1e3 + 5; int n, m, p; int s[10]; char g[MAXN][MAXN]; queue<pair<int, int> > nxt[10]; vector<pair<int, int> > temp; int ans[10]; int d[MAXN][MAXN]; int dr[4] = {0, 0, 1, -1}; int dc[4] = {1, -1, 0, 0}; bool valid(int r, int c) { return 0 <= r && r < n && 0 <= c && c < m; } void bfs() { memset(d, -1, sizeof(d)); memset(ans, 0, sizeof(ans)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { char c = g[i][j]; if (c != '.') { d[i][j] = 0; if (isdigit(c)) { nxt[c - '1'].push({i, j}); ans[c - '1']++; } } } bool f = 1; while (f) { for (int k = 0; k < p; k++) { temp.clear(); while (!nxt[k].empty()) { pair<int, int> pp = nxt[k].front(); nxt[k].pop(); int cr = pp.first; int cc = pp.second; for (int i = 0; i < 4; i++) { int r = cr + dr[i]; int c = cc + dc[i]; if (valid(r, c) && d[r][c] == -1) { ans[k]++; if (d[cr][cc] + 1 == s[k]) { d[r][c] = 0; temp.push_back({r, c}); continue; } d[r][c] = d[cr][cc] + 1; nxt[k].push({r, c}); } } } for (pair<int, int> pp : temp) nxt[k].push(pp); } f = 0; for (int i = 0; i < p; i++) f |= !nxt[i].empty(); } } int main() { scanf("%d %d %d", &n, &m, &p); for (int i = 0; i < p; i++) scanf("%d", &s[i]); for (int i = 0; i < n; i++) scanf("%s", g[i]); bfs(); for (int i = 0; i < p; i++) printf("%d ", ans[i]); printf("\n"); return 0; } ```
#include <bits/stdc++.h> const int N = 1010; const int Q = 250200; const long long mod = 1e9 + 7; using namespace std; int n; int m; int k; int s[N]; int p[10]; int res[10]; int d[N][N]; char c[N][N]; vector<pair<int, int> > v[10]; int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; for (int i = 1; i <= k; i++) { cin >> s[i]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> c[i][j]; if (c[i][j] != '.' && c[i][j] != '#') { v[c[i][j] - '0'].push_back({i, j}); } } } queue<pair<int, int> > q; while (true) { bool ok = false; for (int i = 1; i <= k; i++) { if (p[i] == v[i].size()) { continue; } ok = true; while (p[i] < v[i].size()) { d[v[i][p[i]].first][v[i][p[i]].second] = 0; q.push(v[i][p[i]]); p[i] += 1; } while (!q.empty()) { int x = q.front().first, y = q.front().second; q.pop(); if (d[x][y] == s[i]) { continue; } for (int j = 0; j < 4; j++) { int nx = x + dx[j], ny = y + dy[j]; if (c[nx][ny] != '.') { continue; } d[nx][ny] = d[x][y] + 1; c[nx][ny] = c[x][y]; q.push({nx, ny}); v[i].push_back({nx, ny}); } } } if (!ok) { break; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (c[i][j] != '.' && c[i][j] != '#') { res[c[i][j] - '0'] += 1; } } } for (int i = 1; i <= k; i++) { cout << res[i] << " "; } }
### Prompt Create a solution in Cpp for the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> const int N = 1010; const int Q = 250200; const long long mod = 1e9 + 7; using namespace std; int n; int m; int k; int s[N]; int p[10]; int res[10]; int d[N][N]; char c[N][N]; vector<pair<int, int> > v[10]; int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; for (int i = 1; i <= k; i++) { cin >> s[i]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> c[i][j]; if (c[i][j] != '.' && c[i][j] != '#') { v[c[i][j] - '0'].push_back({i, j}); } } } queue<pair<int, int> > q; while (true) { bool ok = false; for (int i = 1; i <= k; i++) { if (p[i] == v[i].size()) { continue; } ok = true; while (p[i] < v[i].size()) { d[v[i][p[i]].first][v[i][p[i]].second] = 0; q.push(v[i][p[i]]); p[i] += 1; } while (!q.empty()) { int x = q.front().first, y = q.front().second; q.pop(); if (d[x][y] == s[i]) { continue; } for (int j = 0; j < 4; j++) { int nx = x + dx[j], ny = y + dy[j]; if (c[nx][ny] != '.') { continue; } d[nx][ny] = d[x][y] + 1; c[nx][ny] = c[x][y]; q.push({nx, ny}); v[i].push_back({nx, ny}); } } } if (!ok) { break; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (c[i][j] != '.' && c[i][j] != '#') { res[c[i][j] - '0'] += 1; } } } for (int i = 1; i <= k; i++) { cout << res[i] << " "; } } ```
#include <bits/stdc++.h> using namespace std; int n, m, k; char t[1005][1005]; vector<pair<int, int>> pos[10]; int res[10]; int speed[100]; int dx[] = {1, 0, 0, -1}; int dy[] = {0, 1, -1, 0}; queue<pair<int, pair<int, int>>> q; bool ingrid(int x, int y) { return x < n && y < m && x >= 0 && y >= 0 && t[x][y] == '.'; } void f() { while (!q.empty()) { int ch = q.front().first; int x = q.front().second.first; int y = q.front().second.second; q.pop(); queue<pair<int, int>> q2; q2.push({x, y}); for (int j = 0; j < speed[ch] && !q2.empty(); j++) { int h = q2.size(); for (int p = 0; p < h; p++) { int x = q2.front().first; int y = q2.front().second; q2.pop(); for (int i = 0; i < 4; i++) { if (ingrid(x + dx[i], y + dy[i])) { t[x + dx[i]][y + dy[i]] = ('0' + ch); q2.push({x + dx[i], y + dy[i]}); } } } } while (!q2.empty()) { q.push({ch, q2.front()}); q2.pop(); } } } int main() { cin >> n >> m >> k; for (int i = 0; i < k; i++) { cin >> speed[i + 1]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> t[i][j]; if (t[i][j] <= '9' && t[i][j] >= '0') { pos[t[i][j] - '0' - 1].push_back({i, j}); } } } for (int i = 0; i < k; i++) { for (int j = 0; j < pos[i].size(); j++) { q.push({i + 1, pos[i][j]}); } } f(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (t[i][j] <= '9' && t[i][j] >= '0') { res[t[i][j] - '0' - 1]++; } } } for (int i = 0; i < k; i++) { cout << res[i] << " "; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, k; char t[1005][1005]; vector<pair<int, int>> pos[10]; int res[10]; int speed[100]; int dx[] = {1, 0, 0, -1}; int dy[] = {0, 1, -1, 0}; queue<pair<int, pair<int, int>>> q; bool ingrid(int x, int y) { return x < n && y < m && x >= 0 && y >= 0 && t[x][y] == '.'; } void f() { while (!q.empty()) { int ch = q.front().first; int x = q.front().second.first; int y = q.front().second.second; q.pop(); queue<pair<int, int>> q2; q2.push({x, y}); for (int j = 0; j < speed[ch] && !q2.empty(); j++) { int h = q2.size(); for (int p = 0; p < h; p++) { int x = q2.front().first; int y = q2.front().second; q2.pop(); for (int i = 0; i < 4; i++) { if (ingrid(x + dx[i], y + dy[i])) { t[x + dx[i]][y + dy[i]] = ('0' + ch); q2.push({x + dx[i], y + dy[i]}); } } } } while (!q2.empty()) { q.push({ch, q2.front()}); q2.pop(); } } } int main() { cin >> n >> m >> k; for (int i = 0; i < k; i++) { cin >> speed[i + 1]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> t[i][j]; if (t[i][j] <= '9' && t[i][j] >= '0') { pos[t[i][j] - '0' - 1].push_back({i, j}); } } } for (int i = 0; i < k; i++) { for (int j = 0; j < pos[i].size(); j++) { q.push({i + 1, pos[i][j]}); } } f(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (t[i][j] <= '9' && t[i][j] >= '0') { res[t[i][j] - '0' - 1]++; } } } for (int i = 0; i < k; i++) { cout << res[i] << " "; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MX = 3 * 1e5 + 50; const int dx[] = {0, 0, 1, -1}; const int dy[] = {1, -1, 0, 0}; struct Position { int x, y, id, v; }; queue<Position> q; vector<Position> pos[15]; int v[MX], ans[15]; int h, w, n, len = 0; char mp[1050][1050]; bool dge(int x, int y) { if (x >= 1 && x <= w && y >= 1 && y <= h && mp[y][x] == '.') return true; return false; } void bfs() { queue<Position> qq; Position qt = q.front(); while (q.front().id == qt.id) { qt = q.front(); q.pop(); qq.push(qt); } while (!qq.empty()) { qt = qq.front(); qq.pop(); for (int i = 0; i < 4; i++) { int x = qt.x + dx[i]; int y = qt.y + dy[i]; if (dge(x, y)) { mp[y][x] = qt.id; if (qt.v + 1 == v[qt.id]) { q.push(Position{x, y, qt.id, 0}); } else qq.push(Position{x, y, qt.id, qt.v + 1}); ans[qt.id]++; } } if (qq.empty() && !q.empty()) { qt.id = -1; while (qt.id == -1 || q.front().id == qt.id) { qt = q.front(); q.pop(); qq.push(qt); if (q.empty()) break; } } } } int main() { while (scanf("%d%d%d", &h, &w, &n) != EOF) { while (!q.empty()) q.pop(); memset(ans, 0, sizeof(ans)); for (int i = 1; i <= n; i++) scanf("%d", v + i); for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { scanf(" %c", &mp[i][j]); if (mp[i][j] >= '1' && mp[i][j] <= '9') { Position temp = {j, i, mp[i][j] - '0'}; temp.v = 0; pos[mp[i][j] - '0'].push_back(temp); ans[mp[i][j] - '0']++; } } } for (int i = 1; i <= n; i++) { for (int j = 0; j < pos[i].size(); j++) { q.push(pos[i][j]); } } bfs(); for (int i = 1; i <= n; i++) if (i == n) printf("%d\n", ans[i]); else printf("%d ", ans[i]); } }
### Prompt Develop a solution in Cpp to the problem described below: Kilani is playing a game with his friends. This game can be represented as a grid of size n Γ— m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≀ n, m ≀ 1000, 1 ≀ p ≀ 9) β€” the size of the grid and the number of players. The second line contains p integers s_i (1 ≀ s ≀ 10^9) β€” the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≀ x ≀ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers β€” the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MX = 3 * 1e5 + 50; const int dx[] = {0, 0, 1, -1}; const int dy[] = {1, -1, 0, 0}; struct Position { int x, y, id, v; }; queue<Position> q; vector<Position> pos[15]; int v[MX], ans[15]; int h, w, n, len = 0; char mp[1050][1050]; bool dge(int x, int y) { if (x >= 1 && x <= w && y >= 1 && y <= h && mp[y][x] == '.') return true; return false; } void bfs() { queue<Position> qq; Position qt = q.front(); while (q.front().id == qt.id) { qt = q.front(); q.pop(); qq.push(qt); } while (!qq.empty()) { qt = qq.front(); qq.pop(); for (int i = 0; i < 4; i++) { int x = qt.x + dx[i]; int y = qt.y + dy[i]; if (dge(x, y)) { mp[y][x] = qt.id; if (qt.v + 1 == v[qt.id]) { q.push(Position{x, y, qt.id, 0}); } else qq.push(Position{x, y, qt.id, qt.v + 1}); ans[qt.id]++; } } if (qq.empty() && !q.empty()) { qt.id = -1; while (qt.id == -1 || q.front().id == qt.id) { qt = q.front(); q.pop(); qq.push(qt); if (q.empty()) break; } } } } int main() { while (scanf("%d%d%d", &h, &w, &n) != EOF) { while (!q.empty()) q.pop(); memset(ans, 0, sizeof(ans)); for (int i = 1; i <= n; i++) scanf("%d", v + i); for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { scanf(" %c", &mp[i][j]); if (mp[i][j] >= '1' && mp[i][j] <= '9') { Position temp = {j, i, mp[i][j] - '0'}; temp.v = 0; pos[mp[i][j] - '0'].push_back(temp); ans[mp[i][j] - '0']++; } } } for (int i = 1; i <= n; i++) { for (int j = 0; j < pos[i].size(); j++) { q.push(pos[i][j]); } } bfs(); for (int i = 1; i <= n; i++) if (i == n) printf("%d\n", ans[i]); else printf("%d ", ans[i]); } } ```