output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <algorithm> #include <iostream> #include <string> #include <utility> #include <vector> using namespace std; int main() { int H, W, ans = 0; cin >> H >> W; vector<pair<int, int>> a; for (int i = 0; i < H; ++i) { string c; cin >> c; for (int j = 0; j < W; ++j) { if ('B' == c[j]) a.emplace_back(i, j); } } for (int i = 0; i < a.size() - 1; ++i) { for (int j = 1 + i; j < a.size(); ++j) ans = max(abs(a[i].first - a[j].first) + abs(a[i].second - a[j].second), ans); } cout << ans << endl; }
### Prompt Your challenge is to write a Cpp solution to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <algorithm> #include <iostream> #include <string> #include <utility> #include <vector> using namespace std; int main() { int H, W, ans = 0; cin >> H >> W; vector<pair<int, int>> a; for (int i = 0; i < H; ++i) { string c; cin >> c; for (int j = 0; j < W; ++j) { if ('B' == c[j]) a.emplace_back(i, j); } } for (int i = 0; i < a.size() - 1; ++i) { for (int j = 1 + i; j < a.size(); ++j) ans = max(abs(a[i].first - a[j].first) + abs(a[i].second - a[j].second), ans); } cout << ans << endl; } ```
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- vector<string> rotateClockwise(vector<string> g) { int n = g.size(); int m = g[0].size(); vector<string> res(m, string(n, '#')); rep(i, 0, n) rep(j, 0, m) res[j][n - i - 1] = g[i][j]; return res; } /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int H, W; vector<string> B; //--------------------------------------------------------------------------------------------------- int solve() { int ma = -1, mi = inf; rep(y, 0, H) rep(x, 0, W) if (B[y][x] == 'B') { chmax(ma, y + x); chmin(mi, y + x); } return ma - mi; } //--------------------------------------------------------------------------------------------------- void _main() { cin >> H >> W; rep(y, 0, H) { string s; cin >> s; B.push_back(s); } int ans = 0; rep(d, 0, 4) { // rotate B = rotateClockwise(B); swap(H, W); chmax(ans, solve()); } cout << ans << endl; }
### Prompt Please provide a cpp coded solution to the problem described below: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- vector<string> rotateClockwise(vector<string> g) { int n = g.size(); int m = g[0].size(); vector<string> res(m, string(n, '#')); rep(i, 0, n) rep(j, 0, m) res[j][n - i - 1] = g[i][j]; return res; } /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int H, W; vector<string> B; //--------------------------------------------------------------------------------------------------- int solve() { int ma = -1, mi = inf; rep(y, 0, H) rep(x, 0, W) if (B[y][x] == 'B') { chmax(ma, y + x); chmin(mi, y + x); } return ma - mi; } //--------------------------------------------------------------------------------------------------- void _main() { cin >> H >> W; rep(y, 0, H) { string s; cin >> s; B.push_back(s); } int ans = 0; rep(d, 0, 4) { // rotate B = rotateClockwise(B); swap(H, W); chmax(ans, solve()); } cout << ans << endl; } ```
#include<bits/stdc++.h> using namespace std; using Int = long long; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} struct FastIO{ FastIO(){ cin.tie(0); ios::sync_with_stdio(0); } }fastio_beet; //INSERT ABOVE HERE signed main(){ Int h,w; cin>>h>>w; vector<string> st(h); for(Int i=0;i<h;i++) cin>>st[i]; vector<Int> us,vs; for(Int i=0;i<h;i++){ for(Int j=0;j<w;j++){ if(st[i][j]!='B') continue; us.emplace_back(i+j); vs.emplace_back(i-j); } } sort(us.begin(),us.end()); sort(vs.begin(),vs.end()); cout<<max(us.back()-us.front(),vs.back()-vs.front())<<endl; return 0; }
### Prompt Develop a solution in CPP to the problem described below: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include<bits/stdc++.h> using namespace std; using Int = long long; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} struct FastIO{ FastIO(){ cin.tie(0); ios::sync_with_stdio(0); } }fastio_beet; //INSERT ABOVE HERE signed main(){ Int h,w; cin>>h>>w; vector<string> st(h); for(Int i=0;i<h;i++) cin>>st[i]; vector<Int> us,vs; for(Int i=0;i<h;i++){ for(Int j=0;j<w;j++){ if(st[i][j]!='B') continue; us.emplace_back(i+j); vs.emplace_back(i-j); } } sort(us.begin(),us.end()); sort(vs.begin(),vs.end()); cout<<max(us.back()-us.front(),vs.back()-vs.front())<<endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int h, w; cin >> h >> w; vector<P> A; char C[h][w]; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cin >> C[i][j]; if(C[i][j] == 'B'){ A.push_back({i, j}); } } } int a = A.size(), ans = 0; for(int i = 0; i < a; i++){ for(int j = i + 1; j < a; j++){ ans = max(ans, abs(A[i].first - A[j].first) + abs(A[i].second - A[j].second)); } } cout << ans << endl; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int h, w; cin >> h >> w; vector<P> A; char C[h][w]; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cin >> C[i][j]; if(C[i][j] == 'B'){ A.push_back({i, j}); } } } int a = A.size(), ans = 0; for(int i = 0; i < a; i++){ for(int j = i + 1; j < a; j++){ ans = max(ans, abs(A[i].first - A[j].first) + abs(A[i].second - A[j].second)); } } cout << ans << endl; return 0; } ```
#include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> #define chmin(a, b) ((a) = min((a), (b))) #define chmax(a, b) ((a) = max((a), (b))) #define fs first #define sc second #define eb emplace_back using namespace std; typedef long long ll; typedef pair<int, int> P; typedef tuple<int, int, int> T; const ll MOD = 1e9 + 7; const ll INF = 1e18; const double pi = acos(-1); const double eps = 1e-10; int dx[] = {1, 0, -1, 0}; int dy[] = {0, -1, 0, 1}; int main(){ int h, w; cin>>h>>w; vector<string> c(h); for(int i=0; i<h; i++) cin>>c[i]; vector<int> plus; vector<int> minus; for(int i=0; i<h; i++){ for(int j=0; j<w; j++){ if(c[i][j] == 'B'){ plus.emplace_back(j + i); minus.emplace_back(j - i); } } } sort(plus.begin(), plus.end()); sort(minus.begin(), minus.end()); int ma1 = plus[plus.size() - 1] - plus[0]; int ma2 = minus[minus.size() - 1] - minus[0]; cout << max(ma1, ma2) << endl; }
### Prompt Generate a CPP solution to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> #define chmin(a, b) ((a) = min((a), (b))) #define chmax(a, b) ((a) = max((a), (b))) #define fs first #define sc second #define eb emplace_back using namespace std; typedef long long ll; typedef pair<int, int> P; typedef tuple<int, int, int> T; const ll MOD = 1e9 + 7; const ll INF = 1e18; const double pi = acos(-1); const double eps = 1e-10; int dx[] = {1, 0, -1, 0}; int dy[] = {0, -1, 0, 1}; int main(){ int h, w; cin>>h>>w; vector<string> c(h); for(int i=0; i<h; i++) cin>>c[i]; vector<int> plus; vector<int> minus; for(int i=0; i<h; i++){ for(int j=0; j<w; j++){ if(c[i][j] == 'B'){ plus.emplace_back(j + i); minus.emplace_back(j - i); } } } sort(plus.begin(), plus.end()); sort(minus.begin(), minus.end()); int ma1 = plus[plus.size() - 1] - plus[0]; int ma2 = minus[minus.size() - 1] - minus[0]; cout << max(ma1, ma2) << endl; } ```
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <algorithm> #include <utility> #include <functional> #include <cstring> #include <queue> #include <stack> #include <math.h> #include <iterator> #include <vector> #include <string> #include <set> #include <math.h> #include <iostream> #include <random> #include<map> #include <iomanip> #include <time.h> #include <stdlib.h> #include <list> #include <typeinfo> #include <list> #include <set> #include <cassert> #include<fstream> #include <unordered_map> #include <cstdlib> #include <complex> #include <cctype> using namespace std; typedef string::const_iterator State; #define Ma_PI 3.141592653589793 #define eps 0.00000001 #define LONG_INF 1e18 #define GOLD 1.61803398874989484820458 #define MAX_MOD 1000000007 #define MOD 998244353 #define seg_size 262144 #define REP(i,n) for(long long i = 0;i < n;++i) int main(){ int h, w; cin >> h >> w; vector<pair<int,int>> geko; REP(i, h) { string s; cin >> s; REP(q, w) { if (s[q] == 'B') { geko.push_back(make_pair(i, q)); } } } int ans = 0; REP(i, geko.size()) { REP(q, geko.size()) { ans = max(ans, abs(geko[i].first - geko[q].first) + abs(geko[i].second - geko[q].second)); } } cout << ans << endl; return 0; }
### Prompt In CPP, your task is to solve the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <algorithm> #include <utility> #include <functional> #include <cstring> #include <queue> #include <stack> #include <math.h> #include <iterator> #include <vector> #include <string> #include <set> #include <math.h> #include <iostream> #include <random> #include<map> #include <iomanip> #include <time.h> #include <stdlib.h> #include <list> #include <typeinfo> #include <list> #include <set> #include <cassert> #include<fstream> #include <unordered_map> #include <cstdlib> #include <complex> #include <cctype> using namespace std; typedef string::const_iterator State; #define Ma_PI 3.141592653589793 #define eps 0.00000001 #define LONG_INF 1e18 #define GOLD 1.61803398874989484820458 #define MAX_MOD 1000000007 #define MOD 998244353 #define seg_size 262144 #define REP(i,n) for(long long i = 0;i < n;++i) int main(){ int h, w; cin >> h >> w; vector<pair<int,int>> geko; REP(i, h) { string s; cin >> s; REP(q, w) { if (s[q] == 'B') { geko.push_back(make_pair(i, q)); } } } int ans = 0; REP(i, geko.size()) { REP(q, geko.size()) { ans = max(ans, abs(geko[i].first - geko[q].first) + abs(geko[i].second - geko[q].second)); } } cout << ans << endl; return 0; } ```
#include<bits/stdc++.h> using namespace std; using ll = long long; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } #define FOR(i,a,b) for(ll i=(a);i<(b);++i) #define ALL(v) (v).begin(), (v).end() #define p(s) cout<<(s)<<endl #define p2(s, t) cout << (s) << " " << (t) << endl #define br() p("") #define pn(s) cout << (#s) << " " << (s) << endl #define p_yes() p("YES") #define p_no() p("NO") const ll mod = 1e9 + 7; const ll inf = 1e18; void vprint(vector<ll> A){ ll L = A.size(); FOR(i, 0, L){ if(i) cout << ' '; cout << A[i]; } cout << endl; } struct Point{ ll x, y; Point(ll _x, ll _y){ x = _x; y = _y; } }; ll dist(Point a, Point b){ ll ans = abs(a.x - b.x) + abs(a.y - b.y); return ans; } int main(){ cin.tie(0); ios::sync_with_stdio(false); // input ll H, W; cin >> H >> W; vector<Point> V; FOR(i, 0, H){ string s; cin >> s; FOR(j, 0, W){ if(s[j]=='B'){ V.push_back(Point(j, i)); } } } ll L = V.size(); ll ma = 0; FOR(i, 0, L){ FOR(j, i+1, L){ ll d = dist(V[i], V[j]); chmax(ma, d); } } p(ma); return 0; }
### Prompt Generate a cpp solution to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include<bits/stdc++.h> using namespace std; using ll = long long; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } #define FOR(i,a,b) for(ll i=(a);i<(b);++i) #define ALL(v) (v).begin(), (v).end() #define p(s) cout<<(s)<<endl #define p2(s, t) cout << (s) << " " << (t) << endl #define br() p("") #define pn(s) cout << (#s) << " " << (s) << endl #define p_yes() p("YES") #define p_no() p("NO") const ll mod = 1e9 + 7; const ll inf = 1e18; void vprint(vector<ll> A){ ll L = A.size(); FOR(i, 0, L){ if(i) cout << ' '; cout << A[i]; } cout << endl; } struct Point{ ll x, y; Point(ll _x, ll _y){ x = _x; y = _y; } }; ll dist(Point a, Point b){ ll ans = abs(a.x - b.x) + abs(a.y - b.y); return ans; } int main(){ cin.tie(0); ios::sync_with_stdio(false); // input ll H, W; cin >> H >> W; vector<Point> V; FOR(i, 0, H){ string s; cin >> s; FOR(j, 0, W){ if(s[j]=='B'){ V.push_back(Point(j, i)); } } } ll L = V.size(); ll ma = 0; FOR(i, 0, L){ FOR(j, i+1, L){ ll d = dist(V[i], V[j]); chmax(ma, d); } } p(ma); return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template<class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) //const ll mod = 1000000007; int H, W; string field[1005]; int main() { //cout.precision(10); cin >> H >> W; for(int h = 0; h < H; h++) cin >> field[h]; vector<ll> plus, minus; for(int h = 0; h < H; h++) { for(int w = 0; w < W; w++) { if(field[h][w] == '.') continue; plus.push_back(h+w); minus.push_back(h-w); } } sort(plus.begin(), plus.end()); sort(minus.begin(), minus.end()); cout << max(abs(plus[0] - plus.back()), abs(minus[0] - minus.back())) << endl; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template<class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) //const ll mod = 1000000007; int H, W; string field[1005]; int main() { //cout.precision(10); cin >> H >> W; for(int h = 0; h < H; h++) cin >> field[h]; vector<ll> plus, minus; for(int h = 0; h < H; h++) { for(int w = 0; w < W; w++) { if(field[h][w] == '.') continue; plus.push_back(h+w); minus.push_back(h-w); } } sort(plus.begin(), plus.end()); sort(minus.begin(), minus.end()); cout << max(abs(plus[0] - plus.back()), abs(minus[0] - minus.back())) << endl; return 0; } ```
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> using namespace std; const int INF = 1e9; int main() { int h, w; cin >> h >> w; string s[h]; vector<pii> v; for (int i = 0; i < h; i++) cin >> s[i]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == 'B') { v.push_back(pii(i, j)); } } } int ans = -1; for (int i = 0; i < v.size(); i++) { for (int j = 0; j < v.size(); j++) { if (i == j) continue; int dx = abs(v[i].first - v[j].first); int dy = abs(v[i].second - v[j].second); ans = max(ans, dx + dy); } } cout << ans << endl; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <bits/stdc++.h> #define ll long long #define pii pair<int, int> using namespace std; const int INF = 1e9; int main() { int h, w; cin >> h >> w; string s[h]; vector<pii> v; for (int i = 0; i < h; i++) cin >> s[i]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == 'B') { v.push_back(pii(i, j)); } } } int ans = -1; for (int i = 0; i < v.size(); i++) { for (int j = 0; j < v.size(); j++) { if (i == j) continue; int dx = abs(v[i].first - v[j].first); int dy = abs(v[i].second - v[j].second); ans = max(ans, dx + dy); } } cout << ans << endl; return 0; } ```
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <string> #include <iomanip> #include <algorithm> #include <cmath> #include <stdio.h> using namespace std; #define int long long int MOD = 1000000007; signed main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; int res = 0; string S; int mx1 = -100000; int mn1 = 100000; int mx2 = -100000; int mn2 = 100000; for (int i = 0; i < H; i++) { cin >> S; for (int j = 0; j < W; j++) { if (S[j] == 'B') { mx1 = max(mx1, i + j); mn1 = min(mn1, i + j); mx2 = max(mx2, i - j); mn2 = min(mn2, i - j); } } } res = max(res, mx1 - mn1); res = max(res, mx2 - mn2); cout << res << endl; }
### Prompt Please formulate a CPP solution to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <string> #include <iomanip> #include <algorithm> #include <cmath> #include <stdio.h> using namespace std; #define int long long int MOD = 1000000007; signed main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; int res = 0; string S; int mx1 = -100000; int mn1 = 100000; int mx2 = -100000; int mn2 = 100000; for (int i = 0; i < H; i++) { cin >> S; for (int j = 0; j < W; j++) { if (S[j] == 'B') { mx1 = max(mx1, i + j); mn1 = min(mn1, i + j); mx2 = max(mx2, i - j); mn2 = min(mn2, i - j); } } } res = max(res, mx1 - mn1); res = max(res, mx2 - mn2); cout << res << endl; } ```
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<int, int> P; int main() { int h, w; cin>>h>>w; string s[1010]; for(int i=0; i<h; i++) cin>>s[i]; int mn1=h+w, mn2=h+w, mx1=-h-w, mx2=-h-w; for(int i=0; i<h; i++){ for(int j=0; j<w; j++){ if(s[i][j]=='B'){ mn1=min(mn1, i+j); mx1=max(mx1, i+j); mn2=min(mn2, i-j); mx2=max(mx2, i-j); } } } cout<<max(mx1-mn1, mx2-mn2)<<endl; return 0; }
### Prompt Develop a solution in cpp to the problem described below: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<int, int> P; int main() { int h, w; cin>>h>>w; string s[1010]; for(int i=0; i<h; i++) cin>>s[i]; int mn1=h+w, mn2=h+w, mx1=-h-w, mx2=-h-w; for(int i=0; i<h; i++){ for(int j=0; j<w; j++){ if(s[i][j]=='B'){ mn1=min(mn1, i+j); mx1=max(mx1, i+j); mn2=min(mn2, i-j); mx2=max(mx2, i-j); } } } cout<<max(mx1-mn1, mx2-mn2)<<endl; return 0; } ```
#include <bits/stdc++.h> #define mod 1000000007 #define mod998 998244353 #define sp ' ' #define intmax 2147483647 #define llmax 9223372036854775807 #define mkp make_pair typedef long long ll; using namespace std; int H, W, res; char c; vector<int>v[1000]; int main() { cin >> H >> W; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { cin >> c; if (c == 'B') { v[i].push_back(j); } } } for (int i = 0; i < H; ++i) { if (!v[i].empty()) { for (int j = 0; j < H; ++j) { if (!v[j].empty()) { res = max(res, abs(i - j) + max(abs(v[i].front() - v[j].back()), abs(v[i].back() - v[j].front()))); } } } } cout << res << endl; }
### Prompt Generate a CPP solution to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <bits/stdc++.h> #define mod 1000000007 #define mod998 998244353 #define sp ' ' #define intmax 2147483647 #define llmax 9223372036854775807 #define mkp make_pair typedef long long ll; using namespace std; int H, W, res; char c; vector<int>v[1000]; int main() { cin >> H >> W; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { cin >> c; if (c == 'B') { v[i].push_back(j); } } } for (int i = 0; i < H; ++i) { if (!v[i].empty()) { for (int j = 0; j < H; ++j) { if (!v[j].empty()) { res = max(res, abs(i - j) + max(abs(v[i].front() - v[j].back()), abs(v[i].back() - v[j].front()))); } } } } cout << res << endl; } ```
#include <bits/stdc++.h> const int INF = 1e9; const int MOD = 1e9+7; using LL = long long; const LL LINF = 1e18; const double EPS = 1e-10; using namespace std; int main(){ int H,W;cin >> H >> W; vector<pair<int,int>> vec_h,vec_w; for(int i = 0;i < H;i++){ string s; cin >> s; for(int j = 0;j < W;j++){ if(s.at(j) == 'B'){ vec_h.push_back({i, j}); vec_w.push_back({j, i}); } } } int ans = 0; for(int i = 0;i < vec_h.size()-1;i++){ for(int j = i+1;j < vec_h.size();j++){ ans = max(ans, abs(vec_h.at(i).first-vec_h.at(j).first) + abs(vec_h.at(i).second-vec_h.at(j).second)); } } cout<<ans<<endl; }
### Prompt Construct a Cpp code solution to the problem outlined: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <bits/stdc++.h> const int INF = 1e9; const int MOD = 1e9+7; using LL = long long; const LL LINF = 1e18; const double EPS = 1e-10; using namespace std; int main(){ int H,W;cin >> H >> W; vector<pair<int,int>> vec_h,vec_w; for(int i = 0;i < H;i++){ string s; cin >> s; for(int j = 0;j < W;j++){ if(s.at(j) == 'B'){ vec_h.push_back({i, j}); vec_w.push_back({j, i}); } } } int ans = 0; for(int i = 0;i < vec_h.size()-1;i++){ for(int j = i+1;j < vec_h.size();j++){ ans = max(ans, abs(vec_h.at(i).first-vec_h.at(j).first) + abs(vec_h.at(i).second-vec_h.at(j).second)); } } cout<<ans<<endl; } ```
//include //------------------------------------------ #include <vector> #include <list> #include <map> #include <unordered_map> #include <climits> #include <set> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <queue> #include <random> #include <complex> #include <regex> #include <locale> #include <random> #include <type_traits> using namespace std; #define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";} #define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}} using LL = long long; //~~~~~~~~~~~~~~~~~~~~~_(^~^ 」 ∠)_~~~~~~~~~~~~~~~~~~~~~ constexpr int INF = 1e8; typedef pair<int, int> PII; int dy[] = {0, 1, 0, -1}; int dx[] = {1, 0, -1, 0}; int main() { int H, W; cin >> H >> W; vector<string> S(H); for (int i = 0; i < H; i++) cin >> S[i]; int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (S[i][j] == 'B') { for (int k = 0; k < H; k++) { for (int l = 0; l < W; l++) { if (S[k][l] == 'B') { ans = max(ans, abs(i - k) + abs(j - l)); } } } } } } cout << ans << endl; }
### Prompt Develop a solution in Cpp to the problem described below: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp //include //------------------------------------------ #include <vector> #include <list> #include <map> #include <unordered_map> #include <climits> #include <set> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <queue> #include <random> #include <complex> #include <regex> #include <locale> #include <random> #include <type_traits> using namespace std; #define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";} #define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}} using LL = long long; //~~~~~~~~~~~~~~~~~~~~~_(^~^ 」 ∠)_~~~~~~~~~~~~~~~~~~~~~ constexpr int INF = 1e8; typedef pair<int, int> PII; int dy[] = {0, 1, 0, -1}; int dx[] = {1, 0, -1, 0}; int main() { int H, W; cin >> H >> W; vector<string> S(H); for (int i = 0; i < H; i++) cin >> S[i]; int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (S[i][j] == 'B') { for (int k = 0; k < H; k++) { for (int l = 0; l < W; l++) { if (S[k][l] == 'B') { ans = max(ans, abs(i - k) + abs(j - l)); } } } } } } cout << ans << endl; } ```
#include <bits/stdc++.h> using namespace std; int main(){ int H, W; cin >> H >> W; string S[1000]; for(int i=0; i<H; i++) cin >> S[i]; vector<pair<int, int>> cand; for(int i=0; i<H; i++){ int l = -1, r = -1; for(int j=0; j<W; j++) if(S[i][j] == 'B'){ if(l == -1) l = j; r = j; } if(l >= 0){ cand.emplace_back(i, l); cand.emplace_back(i, r); } } int ans = 0; for(auto& p : cand) for(auto& q : cand){ int res = abs(p.first-q.first) + abs(p.second-q.second); ans = max(ans, res); } cout << ans << endl; return 0; }
### Prompt In CPP, your task is to solve the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int H, W; cin >> H >> W; string S[1000]; for(int i=0; i<H; i++) cin >> S[i]; vector<pair<int, int>> cand; for(int i=0; i<H; i++){ int l = -1, r = -1; for(int j=0; j<W; j++) if(S[i][j] == 'B'){ if(l == -1) l = j; r = j; } if(l >= 0){ cand.emplace_back(i, l); cand.emplace_back(i, r); } } int ans = 0; for(auto& p : cand) for(auto& q : cand){ int res = abs(p.first-q.first) + abs(p.second-q.second); ans = max(ans, res); } cout << ans << endl; return 0; } ```
#include<iostream> #include<string> #include<iomanip> #include<cmath> #include<vector> #include<algorithm> using namespace std; #define int long long #define endl "\n" const long long INF = (long long)1e18; const long long MOD = (long long)1e9 + 7; string yn(bool f){return f?"Yes":"No";} string YN(bool f){return f?"YES":"NO";} #define MAX signed main(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed<<setprecision(10); int H, W; int ans = 0; vector<string> c; vector<pair<int,int>> in; in.resize(4); in[0] = make_pair(INF,INF); in[1] = make_pair(INF,0); in[2] = make_pair(0,INF); in[3] = make_pair(0,0); cin>>H>>W; c.resize(H); for(int i = 0; i < H; i++){ cin>>c[i]; } for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ if(c[i][j] != 'B') continue; if(abs(in[0].first) + in[0].second > i + j){ in[0] = make_pair(i,j); } if(abs(in[1].first) + abs(in[1].second-W+1) > abs(i) + abs(j-W+1)){ in[1] = make_pair(i,j); } if(abs(in[2].first -H+1) + abs(in[2].second) > abs(i-H+1) + abs(j)){ in[2] = make_pair(i,j); } if(abs(in[3].first -H+1) + abs(in[3].second-W+1) > abs(i -H+1) + abs(j-W+1)){ in[3] = make_pair(i,j); } } } for(int i = 0; i < 4; i++){ for(int j = i+1; j < 4; j++){ ans = max(ans, abs(in[i].first-in[j].first) + abs(in[i].second-in[j].second)); } } cout<<ans<<endl; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include<iostream> #include<string> #include<iomanip> #include<cmath> #include<vector> #include<algorithm> using namespace std; #define int long long #define endl "\n" const long long INF = (long long)1e18; const long long MOD = (long long)1e9 + 7; string yn(bool f){return f?"Yes":"No";} string YN(bool f){return f?"YES":"NO";} #define MAX signed main(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed<<setprecision(10); int H, W; int ans = 0; vector<string> c; vector<pair<int,int>> in; in.resize(4); in[0] = make_pair(INF,INF); in[1] = make_pair(INF,0); in[2] = make_pair(0,INF); in[3] = make_pair(0,0); cin>>H>>W; c.resize(H); for(int i = 0; i < H; i++){ cin>>c[i]; } for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ if(c[i][j] != 'B') continue; if(abs(in[0].first) + in[0].second > i + j){ in[0] = make_pair(i,j); } if(abs(in[1].first) + abs(in[1].second-W+1) > abs(i) + abs(j-W+1)){ in[1] = make_pair(i,j); } if(abs(in[2].first -H+1) + abs(in[2].second) > abs(i-H+1) + abs(j)){ in[2] = make_pair(i,j); } if(abs(in[3].first -H+1) + abs(in[3].second-W+1) > abs(i -H+1) + abs(j-W+1)){ in[3] = make_pair(i,j); } } } for(int i = 0; i < 4; i++){ for(int j = i+1; j < 4; j++){ ans = max(ans, abs(in[i].first-in[j].first) + abs(in[i].second-in[j].second)); } } cout<<ans<<endl; return 0; } ```
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; int main(){ int h,w; cin >> h >> w; vector<string> s(h); for(int i = 0; i < h; i++)cin >> s[i]; int ans = 0; for(int i = 0; i < h; i++)for(int j = 0; j < w; j++){ for(int k = 0; k < h; k++)for(int l = 0; l < w; l++){ if(s[i][j] == 'B' && s[k][l] == 'B'){ ans = max(ans, abs(i-k) + abs(j-l)); } } } cout << ans << endl; }
### Prompt Please formulate a CPP solution to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; int main(){ int h,w; cin >> h >> w; vector<string> s(h); for(int i = 0; i < h; i++)cin >> s[i]; int ans = 0; for(int i = 0; i < h; i++)for(int j = 0; j < w; j++){ for(int k = 0; k < h; k++)for(int l = 0; l < w; l++){ if(s[i][j] == 'B' && s[k][l] == 'B'){ ans = max(ans, abs(i-k) + abs(j-l)); } } } cout << ans << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { int h,w; cin >> h >> w; vector<pair<int,int> > bu; char c; for(int i=0;i<h;++i){ for(int j=0;j<w;++j){ cin >> c; if(c=='B') bu.push_back(make_pair(i,j)); } } int ans=0; for(int i=0;i<bu.size();++i) for(int j=i+1;j<bu.size();++j) ans=max(ans,abs(bu[i].first-bu[j].first)+abs(bu[i].second-bu[j].second)); cout << ans << endl; return 0; }
### Prompt Please create a solution in Cpp to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int h,w; cin >> h >> w; vector<pair<int,int> > bu; char c; for(int i=0;i<h;++i){ for(int j=0;j<w;++j){ cin >> c; if(c=='B') bu.push_back(make_pair(i,j)); } } int ans=0; for(int i=0;i<bu.size();++i) for(int j=i+1;j<bu.size();++j) ans=max(ans,abs(bu[i].first-bu[j].first)+abs(bu[i].second-bu[j].second)); cout << ans << endl; return 0; } ```
/* Aa^~ kokoro ga pyonpyon suru n jaa^~ // ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```?Wfpppbpbbpbbpbbbkbkk // ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` `` ` ` ` ` ` ` ` ` ` dppbbkkkkkkkkkkqkqkk // HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` `` ````(Wpbkkkkkkkkqkkkkkqk // mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` `` ```(yppbkkkkkkkkkkkkkkk // ggmgmmqqqH$ ``````````....````` ` ````````.`````` `` ``````.yfpppbbbbkkkkqqqqqH // gmmmmmqqqkW<```` `````...````` .,.` ````....````` ``````` (Wbqqmgmmgmggggggggg // qmmmqqqqkkWk.``````````````````` ;:<`` `````.`````````````-_<-?WHHqmmmmmmgmmgggg // @@@@@@@gggHH6- ``````````````` `` _ `` ```````````````` ._~~_.`-?Wkqmmmmmmmggg@g // @@@@g@gggHY~.-<_- `````````````````````````````````` ._~~(<-``.`.(WHqqqmmggggmmm // @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.`` OHHWUWHmqHWXW // gggggmqK1.``..~.. _<<+-(____.. ```````` ..__~~_((<<!.`.``` .``.`` j0C1XUHmHIdW // ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..``` jOuWHHqHIdH // gmmqkW!`(_ J>` `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__``` zXWHg@HkXH // gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. ` _ ?&._.I`_`````` .XyVfppppW // HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd} .`..````jqg@@@@@@ // XHWZ{..<Jk~!.`.. (<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH // bkWt~.-jCz(_..`.(+<.(;< ._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH // WUUO..(f.(c...__+z<-(+~` _-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk // kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW // @@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX // @@mR.(j:..~.._<z!`.(>~``` ~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW // @gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH // mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@ // qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g // HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~`` (jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg // 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ```` d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq // X0XWHKXXw$<(z<.( `` WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W // XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy // kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<`` (v!`._..(??_.(1._.._=dUOOzzzwX // @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.`` :.~+=?~.(;_(...jdQQQQQkkk // H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@ // @@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g // @H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm // @@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y // @@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_ // MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz // HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk // HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH // rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy // HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%`` (Ctwwtz_~.<(Wg@H // NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ ` _``-zwrt1~~_<(MNM // MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>` ~````.OXtt>~._<?MM */ #include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using LL = long long; using VL = vector<LL>; using VVL = vector<VL>; using PLL = pair<LL, LL>; using VS = vector<string>; #define ALL(a) begin((a)),end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define MT make_tuple #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FF first #define SS second template<class S, class T> istream& operator>>(istream& is, pair<S,T>& p){ return is >> p.FF >> p.SS; } template<class T> istream& operator>>(istream& is, vector<T>& xs){ for(auto& x: xs) is >> x; return is; } template<class S, class T> ostream& operator<<(ostream& os, const pair<S,T>& p){ return os << p.FF << " " << p.SS; } template<class T> ostream& operator<<(ostream& os, const vector<T>& xs){ for(unsigned int i=0;i<xs.size();++i) os << (i?" ":"") << xs[i]; return os; } template<class T> void maxi(T& x, T y){ if(x < y) x = y; } template<class T> void mini(T& x, T y){ if(x > y) x = y; } void debug(istringstream&){} template <char sep=',', class Head, class... Tail> void debug(istringstream& iss, Head&& head, Tail&&... tail) { string name; getline(iss, name, ','); cout << sep << name << "=" << head; debug(iss, forward<Tail>(tail)...); } #ifdef PYONPOI #define DEBUG(...) \ do{ \ istringstream ss(#__VA_ARGS__); \ debug<' '>(ss, __VA_ARGS__); \ cout<<endl; \ }while(0) #else #define DEBUG #endif const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9+7; const int INF = 1e9; int main(){ cin.tie(0); ios_base::sync_with_stdio(false); int H, W; cin >> H >> W; VS vs(H); cin >> vs; int x0 = INF, x1 = -INF; int y0 = INF, y1 = -INF; vector<PII> xs; REP(y,H) REP(x,W){ if(vs[y][x] == 'B'){ int xx = x+y; int yy = x - y; //xs.EB(x+y,x-y); mini(x0, xx); maxi(x1, xx); mini(y0, yy); maxi(y1, yy); } } int ans = max(x1 - x0, y1 - y0); cout << ans << endl; return 0; }
### Prompt Please create a solution in Cpp to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp /* Aa^~ kokoro ga pyonpyon suru n jaa^~ // ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```?Wfpppbpbbpbbpbbbkbkk // ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` `` ` ` ` ` ` ` ` ` ` dppbbkkkkkkkkkkqkqkk // HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` `` ````(Wpbkkkkkkkkqkkkkkqk // mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` `` ```(yppbkkkkkkkkkkkkkkk // ggmgmmqqqH$ ``````````....````` ` ````````.`````` `` ``````.yfpppbbbbkkkkqqqqqH // gmmmmmqqqkW<```` `````...````` .,.` ````....````` ``````` (Wbqqmgmmgmggggggggg // qmmmqqqqkkWk.``````````````````` ;:<`` `````.`````````````-_<-?WHHqmmmmmmgmmgggg // @@@@@@@gggHH6- ``````````````` `` _ `` ```````````````` ._~~_.`-?Wkqmmmmmmmggg@g // @@@@g@gggHY~.-<_- `````````````````````````````````` ._~~(<-``.`.(WHqqqmmggggmmm // @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.`` OHHWUWHmqHWXW // gggggmqK1.``..~.. _<<+-(____.. ```````` ..__~~_((<<!.`.``` .``.`` j0C1XUHmHIdW // ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..``` jOuWHHqHIdH // gmmqkW!`(_ J>` `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__``` zXWHg@HkXH // gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. ` _ ?&._.I`_`````` .XyVfppppW // HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd} .`..````jqg@@@@@@ // XHWZ{..<Jk~!.`.. (<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH // bkWt~.-jCz(_..`.(+<.(;< ._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH // WUUO..(f.(c...__+z<-(+~` _-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk // kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW // @@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX // @@mR.(j:..~.._<z!`.(>~``` ~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW // @gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH // mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@ // qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g // HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~`` (jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg // 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ```` d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq // X0XWHKXXw$<(z<.( `` WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W // XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy // kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<`` (v!`._..(??_.(1._.._=dUOOzzzwX // @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.`` :.~+=?~.(;_(...jdQQQQQkkk // H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@ // @@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g // @H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm // @@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y // @@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_ // MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz // HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk // HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH // rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy // HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%`` (Ctwwtz_~.<(Wg@H // NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ ` _``-zwrt1~~_<(MNM // MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>` ~````.OXtt>~._<?MM */ #include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using LL = long long; using VL = vector<LL>; using VVL = vector<VL>; using PLL = pair<LL, LL>; using VS = vector<string>; #define ALL(a) begin((a)),end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define MT make_tuple #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FF first #define SS second template<class S, class T> istream& operator>>(istream& is, pair<S,T>& p){ return is >> p.FF >> p.SS; } template<class T> istream& operator>>(istream& is, vector<T>& xs){ for(auto& x: xs) is >> x; return is; } template<class S, class T> ostream& operator<<(ostream& os, const pair<S,T>& p){ return os << p.FF << " " << p.SS; } template<class T> ostream& operator<<(ostream& os, const vector<T>& xs){ for(unsigned int i=0;i<xs.size();++i) os << (i?" ":"") << xs[i]; return os; } template<class T> void maxi(T& x, T y){ if(x < y) x = y; } template<class T> void mini(T& x, T y){ if(x > y) x = y; } void debug(istringstream&){} template <char sep=',', class Head, class... Tail> void debug(istringstream& iss, Head&& head, Tail&&... tail) { string name; getline(iss, name, ','); cout << sep << name << "=" << head; debug(iss, forward<Tail>(tail)...); } #ifdef PYONPOI #define DEBUG(...) \ do{ \ istringstream ss(#__VA_ARGS__); \ debug<' '>(ss, __VA_ARGS__); \ cout<<endl; \ }while(0) #else #define DEBUG #endif const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9+7; const int INF = 1e9; int main(){ cin.tie(0); ios_base::sync_with_stdio(false); int H, W; cin >> H >> W; VS vs(H); cin >> vs; int x0 = INF, x1 = -INF; int y0 = INF, y1 = -INF; vector<PII> xs; REP(y,H) REP(x,W){ if(vs[y][x] == 'B'){ int xx = x+y; int yy = x - y; //xs.EB(x+y,x-y); mini(x0, xx); maxi(x1, xx); mini(y0, yy); maxi(y1, yy); } } int ans = max(x1 - x0, y1 - y0); cout << ans << endl; return 0; } ```
// Undone #include <bits/stdc++.h> using namespace std; int H, W; char grid[2000][2000]; int main() { cin >> H >> W; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> grid[i][j]; } } int ans = 0, found = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (grid[i][j] != 'B') continue; for (int k = H-1; k >= 0; k--) { for (int l = W-1; l >= 0; l--) { if (grid[k][l] != 'B') continue; ans = max(ans, abs(i-k)+abs(j-l)); } } } } cout << ans << endl; }
### Prompt Your challenge is to write a cpp solution to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp // Undone #include <bits/stdc++.h> using namespace std; int H, W; char grid[2000][2000]; int main() { cin >> H >> W; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> grid[i][j]; } } int ans = 0, found = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (grid[i][j] != 'B') continue; for (int k = H-1; k >= 0; k--) { for (int l = W-1; l >= 0; l--) { if (grid[k][l] != 'B') continue; ans = max(ans, abs(i-k)+abs(j-l)); } } } } cout << ans << endl; } ```
#include <cstdio> #include <algorithm> using namespace std; int x[901]; int y[901]; char row[31]; int main() { int H, W, idx = 0, maxd = 0; scanf("%d %d", &H, &W); for (int i = 0; i < H; i++) { scanf("%s", row); for (int j = 0; j < W; j++) if (row[j] == 'B') { x[idx] = i; y[idx] = j; idx++; } } for (int i = 0; i < idx-1; i++) { for (int j = i + 1; j < idx; j++) { maxd = max(maxd, abs(x[j]-x[i]) + abs(y[j]-y[i])); } } printf("%d\n", maxd); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <cstdio> #include <algorithm> using namespace std; int x[901]; int y[901]; char row[31]; int main() { int H, W, idx = 0, maxd = 0; scanf("%d %d", &H, &W); for (int i = 0; i < H; i++) { scanf("%s", row); for (int j = 0; j < W; j++) if (row[j] == 'B') { x[idx] = i; y[idx] = j; idx++; } } for (int i = 0; i < idx-1; i++) { for (int j = i + 1; j < idx; j++) { maxd = max(maxd, abs(x[j]-x[i]) + abs(y[j]-y[i])); } } printf("%d\n", maxd); return 0; } ```
#include <bits/stdc++.h> #define int long long #define endl '\n' #define FOR(i, a, n) for (int i = (a); i < (n); ++i) #define REP(i, n) FOR(i, 0, n) using namespace std; void _main() { int H, W; cin >> H >> W; vector<string> c(H); vector<pair<int, int>> pos; REP (i, H) { cin >> c[i]; REP (j, W) if (c[i][j] == 'B') { pos.emplace_back(i, j); break; } for (int j = W - 1; j >= 0; --j) if (c[i][j] == 'B') { pos.emplace_back(i, j); break; } } sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin(), pos.end()), pos.end()); int ans = 0; for (int i = 0; i < pos.size(); ++i) { for (int j = 0; j < pos.size(); ++j) { ans = max(ans, abs(pos[i].first - pos[j].first) + abs(pos[i].second - pos[j].second)); } } cout << ans << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(10); _main(); return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <bits/stdc++.h> #define int long long #define endl '\n' #define FOR(i, a, n) for (int i = (a); i < (n); ++i) #define REP(i, n) FOR(i, 0, n) using namespace std; void _main() { int H, W; cin >> H >> W; vector<string> c(H); vector<pair<int, int>> pos; REP (i, H) { cin >> c[i]; REP (j, W) if (c[i][j] == 'B') { pos.emplace_back(i, j); break; } for (int j = W - 1; j >= 0; --j) if (c[i][j] == 'B') { pos.emplace_back(i, j); break; } } sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin(), pos.end()), pos.end()); int ans = 0; for (int i = 0; i < pos.size(); ++i) { for (int j = 0; j < pos.size(); ++j) { ans = max(ans, abs(pos[i].first - pos[j].first) + abs(pos[i].second - pos[j].second)); } } cout << ans << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(10); _main(); return 0; } ```
#include <iostream> #include <algorithm> using namespace std; int H, W, maxn; char c[33][33]; int main() { cin >> H >> W; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) cin >> c[i][j]; } for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { for (int k = 1; k <= H; k++) { for (int l = 1; l <= W; l++) { if (c[i][j] == 'B' && c[k][l] == 'B') maxn = max(maxn, abs(i - k) + abs(j - l)); } } } } cout << maxn << endl; return 0; }
### Prompt Your task is to create a cpp solution to the following problem: A: Hokkaido University Easy Note Please note that the problem settings are the same as problem B, except for the constraints. story Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ... "Eh ... I'm not in time for the next class ..." problem Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}. Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|. Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan. Input format H W c_ {11} c_ {12} ... c_ {1W} :: c_ {H1} c_ {H2} ... c_ {HW} Constraint * 2 \ leq H, W \ leq 30 * H and W are integers * c_ {i, j} is either'B'or'.'. * At least two of c_ {i, j} are'B'. Output format Print the integer that represents the answer on one line. Input example 1 3 3 B.B ..B .BB Output example 1 Four * The longest is between the two points (1,1) and (3,3). Input example 2 4 3 B .. B .. ... ... Output example 2 1 * Adjacent positions may be the longest. Input example 3 6 6 ... B .. B.B.B. .B.B.B ... B.B .B..B. ..B ... Output example 3 7 Example Input 3 3 B.B ..B .BB Output 4 ### Response ```cpp #include <iostream> #include <algorithm> using namespace std; int H, W, maxn; char c[33][33]; int main() { cin >> H >> W; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) cin >> c[i][j]; } for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { for (int k = 1; k <= H; k++) { for (int l = 1; l <= W; l++) { if (c[i][j] == 'B' && c[k][l] == 'B') maxn = max(maxn, abs(i - k) + abs(j - l)); } } } } cout << maxn << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int64_t MOD = 1e9+7; void add(int64_t& a, int64_t b){ a = (a+b) % MOD; } void mul(int64_t& a, int64_t b){ a = a*b % MOD; } vector<int64_t> fact, seq_inv, fact_inv; void create_fact_mod(int num){ fact[0] = fact[1] = 1; for(int i=2; i<=num; i++) fact[i] = fact[i-1] * i % MOD; } void create_seq_inv_mod(int num){ seq_inv[0] = seq_inv[1] = 1; for(int i=2; i<=num; i++) seq_inv[i] = (MOD - MOD/i) * seq_inv[MOD%i] % MOD; } void create_fact_inv_mod(int num){ fact_inv[0] = fact_inv[1] = 1; for(int i=2; i<=num; i++) fact_inv[i] = fact_inv[i-1] * seq_inv[i] % MOD; } void create_mod_tables(int num){ fact.resize(num+1); seq_inv.resize(num+1); fact_inv.resize(num+1); create_fact_mod(num); create_seq_inv_mod(num); create_fact_inv_mod(num); } int64_t comb_mod(int n, int k){ return fact[n] * fact_inv[n-k] % MOD * fact_inv[k] % MOD; } int64_t perm_mod(int n, int k){ return fact[n] * fact_inv[n-k] % MOD; } int64_t power_mod(int64_t num, int64_t power){ int64_t prod = 1; num %= MOD; while(power > 0){ if(power&1) prod = prod * num % MOD; num = num * num % MOD; power >>= 1; } return prod; } int64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y){ int64_t d = a; if(b != 0){ d = extgcd(b, a%b, y, x); y -= (a/b) * x; }else{ x = 1; y = 0; } return d; } int64_t inv_mod(int64_t a){ int64_t x, y; extgcd(a, MOD, x, y); return (MOD + x%MOD) % MOD; } int nth_bit(int64_t num, int n){ return (num >> n) & 1; } int pop_count(int bits){ bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff); } int main(){ int N; cin >> N; const int M = 1<<20; static int num[M]; for(int i=0; i<N; i++){ int a; cin >> a; num[a]++; } for(int k=0; k<20; k++) for(int i=M-1; i>=0; i--) if(!nth_bit(i, k)) num[i] += num[i+(1<<k)]; vector<int64_t> pw2(N+1, 1); for(int i=1; i<=N; i++) pw2[i] = pw2[i-1] * 2 % MOD; int64_t ans = 0; for(int i=0; i<M; i++){ int64_t res = pw2[num[i]] - 1; if(pop_count(i)%2) res *= -1; add(ans, MOD+res); } cout << ans << endl; return 0; }
### Prompt Please create a solution in cpp to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int64_t MOD = 1e9+7; void add(int64_t& a, int64_t b){ a = (a+b) % MOD; } void mul(int64_t& a, int64_t b){ a = a*b % MOD; } vector<int64_t> fact, seq_inv, fact_inv; void create_fact_mod(int num){ fact[0] = fact[1] = 1; for(int i=2; i<=num; i++) fact[i] = fact[i-1] * i % MOD; } void create_seq_inv_mod(int num){ seq_inv[0] = seq_inv[1] = 1; for(int i=2; i<=num; i++) seq_inv[i] = (MOD - MOD/i) * seq_inv[MOD%i] % MOD; } void create_fact_inv_mod(int num){ fact_inv[0] = fact_inv[1] = 1; for(int i=2; i<=num; i++) fact_inv[i] = fact_inv[i-1] * seq_inv[i] % MOD; } void create_mod_tables(int num){ fact.resize(num+1); seq_inv.resize(num+1); fact_inv.resize(num+1); create_fact_mod(num); create_seq_inv_mod(num); create_fact_inv_mod(num); } int64_t comb_mod(int n, int k){ return fact[n] * fact_inv[n-k] % MOD * fact_inv[k] % MOD; } int64_t perm_mod(int n, int k){ return fact[n] * fact_inv[n-k] % MOD; } int64_t power_mod(int64_t num, int64_t power){ int64_t prod = 1; num %= MOD; while(power > 0){ if(power&1) prod = prod * num % MOD; num = num * num % MOD; power >>= 1; } return prod; } int64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y){ int64_t d = a; if(b != 0){ d = extgcd(b, a%b, y, x); y -= (a/b) * x; }else{ x = 1; y = 0; } return d; } int64_t inv_mod(int64_t a){ int64_t x, y; extgcd(a, MOD, x, y); return (MOD + x%MOD) % MOD; } int nth_bit(int64_t num, int n){ return (num >> n) & 1; } int pop_count(int bits){ bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff); } int main(){ int N; cin >> N; const int M = 1<<20; static int num[M]; for(int i=0; i<N; i++){ int a; cin >> a; num[a]++; } for(int k=0; k<20; k++) for(int i=M-1; i>=0; i--) if(!nth_bit(i, k)) num[i] += num[i+(1<<k)]; vector<int64_t> pw2(N+1, 1); for(int i=1; i<=N; i++) pw2[i] = pw2[i-1] * 2 % MOD; int64_t ans = 0; for(int i=0; i<M; i++){ int64_t res = pw2[num[i]] - 1; if(pop_count(i)%2) res *= -1; add(ans, MOD+res); } cout << ans << endl; return 0; } ```
#include <iostream> #include <vector> #include <array> #include <list> #include <string> #include <stack> #include <queue> #include <deque> #include <unordered_map> #include <unordered_set> #include <tuple> #include <memory> #include <cmath> #include <algorithm> #include <functional> #include <iomanip> #include <numeric> #include <climits> #include <cfloat> #include <cassert> #include <random> constexpr long long int MOD = 1000000007; long long int power(long long int base, int exp) { switch (exp) { case 0: return 1; case 1: return base % MOD; default: return power(base * base % MOD, exp / 2) * power(base, exp % 2) % MOD; } } long long int power_set_size(int size) { static std::vector<long long int> memo(100001, -1); if (memo[size] >= 0) return memo[size]; return memo[size] = power(2, size); } long long int calc(const std::vector<int>& values, int size = 20) { if (size == 0) return (power_set_size(values.size()) + MOD - 1) % MOD; if (values.empty()) return 0; long long int sum{ 0 }; std::vector<int> sub; for (auto i = 0; i < size; ++i) { sub.clear(); for (const auto v : values) if ((v & (1 << i)) != 0) sub.push_back(v); sum += calc(sub, i); } return ((power_set_size(values.size()) + MOD - 1 - sum) % MOD + MOD) % MOD; } int count_bit(int n) { return (n == 0) ? 0 : (n & 1) + count_bit(n >> 1); } int main() { int n; std::cin >> n; std::vector<int> values(n); for (auto& a : values) std::cin >> a; std::vector<long long int> pattern(1 << 20, 0); for (const auto a : values) pattern[a]++; for (auto i = 0; i < 20; ++i) { for (auto j = 0; j < pattern.size(); ++j) if ((j & (1 << i)) == 0) { pattern[j] += pattern[j | (1 << i)]; pattern[j] %= MOD; } } long long int result = 0; for (auto i = 0; i < pattern.size(); ++i) { if (count_bit(i) % 2 == 0) { result += power(2, pattern[i]) - 1; } else { result -= power(2, pattern[i]) - 1; } result %= MOD; } std::cout << (result + MOD) % MOD << std::endl; }
### Prompt Your task is to create a CPP solution to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <iostream> #include <vector> #include <array> #include <list> #include <string> #include <stack> #include <queue> #include <deque> #include <unordered_map> #include <unordered_set> #include <tuple> #include <memory> #include <cmath> #include <algorithm> #include <functional> #include <iomanip> #include <numeric> #include <climits> #include <cfloat> #include <cassert> #include <random> constexpr long long int MOD = 1000000007; long long int power(long long int base, int exp) { switch (exp) { case 0: return 1; case 1: return base % MOD; default: return power(base * base % MOD, exp / 2) * power(base, exp % 2) % MOD; } } long long int power_set_size(int size) { static std::vector<long long int> memo(100001, -1); if (memo[size] >= 0) return memo[size]; return memo[size] = power(2, size); } long long int calc(const std::vector<int>& values, int size = 20) { if (size == 0) return (power_set_size(values.size()) + MOD - 1) % MOD; if (values.empty()) return 0; long long int sum{ 0 }; std::vector<int> sub; for (auto i = 0; i < size; ++i) { sub.clear(); for (const auto v : values) if ((v & (1 << i)) != 0) sub.push_back(v); sum += calc(sub, i); } return ((power_set_size(values.size()) + MOD - 1 - sum) % MOD + MOD) % MOD; } int count_bit(int n) { return (n == 0) ? 0 : (n & 1) + count_bit(n >> 1); } int main() { int n; std::cin >> n; std::vector<int> values(n); for (auto& a : values) std::cin >> a; std::vector<long long int> pattern(1 << 20, 0); for (const auto a : values) pattern[a]++; for (auto i = 0; i < 20; ++i) { for (auto j = 0; j < pattern.size(); ++j) if ((j & (1 << i)) == 0) { pattern[j] += pattern[j | (1 << i)]; pattern[j] %= MOD; } } long long int result = 0; for (auto i = 0; i < pattern.size(); ++i) { if (count_bit(i) % 2 == 0) { result += power(2, pattern[i]) - 1; } else { result -= power(2, pattern[i]) - 1; } result %= MOD; } std::cout << (result + MOD) % MOD << std::endl; } ```
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } #define MOD 1000000007 int main() { int n = ri(); int a[n]; for (auto &i : a) i = ri(); std::vector<int> cnt(1 << 20); for (auto i : a) cnt[i]++; for(int i = 0; i < 20; i++) for(int j = 0; j < 1 << 20; j++) if (!(j >> i & 1)) cnt[j] += cnt[j | 1 << i]; int beki2[n + 1]; beki2[0] = 1; for (int i = 1; i <= n; i++) beki2[i] = beki2[i - 1] * 2 % MOD; int all = 0; for (int i = 0; i < 1 << 20; i++) { if (__builtin_popcount(i) & 1) all -= beki2[cnt[i]]; else all += beki2[cnt[i]]; if (all < 0) all += MOD; if (all >= MOD) all -= MOD; } std::cout << all << std::endl; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } #define MOD 1000000007 int main() { int n = ri(); int a[n]; for (auto &i : a) i = ri(); std::vector<int> cnt(1 << 20); for (auto i : a) cnt[i]++; for(int i = 0; i < 20; i++) for(int j = 0; j < 1 << 20; j++) if (!(j >> i & 1)) cnt[j] += cnt[j | 1 << i]; int beki2[n + 1]; beki2[0] = 1; for (int i = 1; i <= n; i++) beki2[i] = beki2[i - 1] * 2 % MOD; int all = 0; for (int i = 0; i < 1 << 20; i++) { if (__builtin_popcount(i) & 1) all -= beki2[cnt[i]]; else all += beki2[cnt[i]]; if (all < 0) all += MOD; if (all >= MOD) all -= MOD; } std::cout << all << std::endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; using lint = long long; const lint mod = 1e9 + 7; #define all(x) (x).begin(), (x).end() #define bitcount(n) __builtin_popcountl((lint)(n)) #define fcout cout << fixed << setprecision(15) #define highest(x) (63 - __builtin_clzl(x)) template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; } template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; } template<class T = string, class U = char>int character_count(T text, U character){ int ans = 0; for(U i: text){ ans += (i == character); } return ans; } lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }} struct position{ int y, x; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; // double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); } template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); } string to_string(string x){ return x; } template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) ans += to_string(*i) + " "; if(!ans.empty()) ans.pop_back(); cout << ans << endl; } template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } } template<class T> T gcd(T a, T b){ if(a && b){ return gcd(min(a, b), max(a, b) % min(a, b)); }else{ return a; }} template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; } struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint C(int p, int q) const{ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } }; template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; } template<class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2){ itr now = last; itr2 now2 = last2; while(now != first){ now--, now2--; (*now)++; if((*now) == (*now2)){ (*now) = 0; }else{ return true; } } return false; } int dp[21][1 << 20]; int main(){ int N; cin >> N; int cnt[1 << 20]; fill(cnt, cnt + (1 << 20), 0); for(int i = 0; i < N; i++){ int a; cin >> a; cnt[a]++; } for(int i = 0; i < (1 << 20); i++){ dp[0][i] = cnt[i]; } for(int i = 0; i < 20; i++){ for(int j = 0; j < (1 << 20); j++){ if((~(j >> i)) & 1){ dp[i + 1][j] = dp[i][j] + dp[i][j | (1 << i)]; }else{ dp[i + 1][j] = dp[i][j]; } } } //array_output(dp[20], dp[20] + 32); lint ans = 0; for(int i = 0; i < (1 << 20); i++){ if(bitcount(i) % 2 == 0){ ans += power(2, dp[20][i], mod); }else{ ans -= power(2, dp[20][i], mod); } ans = (ans + mod) % mod; } cout << ans << endl; }
### Prompt Please provide a Cpp coded solution to the problem described below: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using lint = long long; const lint mod = 1e9 + 7; #define all(x) (x).begin(), (x).end() #define bitcount(n) __builtin_popcountl((lint)(n)) #define fcout cout << fixed << setprecision(15) #define highest(x) (63 - __builtin_clzl(x)) template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; } template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; } template<class T = string, class U = char>int character_count(T text, U character){ int ans = 0; for(U i: text){ ans += (i == character); } return ans; } lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }} struct position{ int y, x; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; // double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); } template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); } string to_string(string x){ return x; } template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) ans += to_string(*i) + " "; if(!ans.empty()) ans.pop_back(); cout << ans << endl; } template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } } template<class T> T gcd(T a, T b){ if(a && b){ return gcd(min(a, b), max(a, b) % min(a, b)); }else{ return a; }} template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; } struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint C(int p, int q) const{ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } }; template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; } template<class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2){ itr now = last; itr2 now2 = last2; while(now != first){ now--, now2--; (*now)++; if((*now) == (*now2)){ (*now) = 0; }else{ return true; } } return false; } int dp[21][1 << 20]; int main(){ int N; cin >> N; int cnt[1 << 20]; fill(cnt, cnt + (1 << 20), 0); for(int i = 0; i < N; i++){ int a; cin >> a; cnt[a]++; } for(int i = 0; i < (1 << 20); i++){ dp[0][i] = cnt[i]; } for(int i = 0; i < 20; i++){ for(int j = 0; j < (1 << 20); j++){ if((~(j >> i)) & 1){ dp[i + 1][j] = dp[i][j] + dp[i][j | (1 << i)]; }else{ dp[i + 1][j] = dp[i][j]; } } } //array_output(dp[20], dp[20] + 32); lint ans = 0; for(int i = 0; i < (1 << 20); i++){ if(bitcount(i) % 2 == 0){ ans += power(2, dp[20][i], mod); }else{ ans -= power(2, dp[20][i], mod); } ans = (ans + mod) % mod; } cout << ans << endl; } ```
#include<deque> #include<queue> #include<vector> #include<algorithm> #include<iostream> #include<set> #include<cmath> #include<tuple> #include<string> #include<chrono> #include<functional> #include<iterator> #include<random> #include<unordered_set> #include<array> #include<map> #include<iomanip> #include<assert.h> #include<list> #include<bitset> #include<stack> #include<memory> #include<numeric> using namespace std; using namespace std::chrono; typedef long long int llint; typedef long double lldo; #define mp make_pair #define mt make_tuple #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define fir first #define sec second #define res resize #define ins insert #define era erase /*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/ const int mod=1000000007; const llint big=2.19e15+1; const long double pai=3.141592653589793238462643383279502884197; const long double eps=1e-4; template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;} template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;} llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);} llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;} template<class T> void SO(T& ve){sort(ve.begin(),ve.end());} template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());} template<class T>int LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();} template<class T>int UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();} int main(void){ int i,n;cin>>n; static llint a[1<<20]={}; static llint ni[100001]={}; ni[0]=1; for(i=0;i<n;i++){ni[i+1]=ni[i]*2%mod;} while(n--){ int x;cin>>x; a[x]++; } for(i=0;i<20;i++){ for(int bi=0;bi<(1<<20);bi++){ if(bi&(1<<i)){continue;} a[bi]+=a[bi+(1<<i)]; } } llint ans=0; for(int bi=0;bi<(1<<20);bi++){ int fu=1; for(i=0;i<20;i++){if(bi&(1<<i)){fu*=-1;}} ans+=fu*ni[a[bi]];ans%=mod; //if(bi<16){cerr<<ni[a[bi]]<<endl;} } ans+=mod;ans%=mod; cout<<ans<<endl; return 0; } /* 3 0 1 2 3 1 1 0 + - - + 8-2-2+1 =5 */
### Prompt Generate a cpp solution to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include<deque> #include<queue> #include<vector> #include<algorithm> #include<iostream> #include<set> #include<cmath> #include<tuple> #include<string> #include<chrono> #include<functional> #include<iterator> #include<random> #include<unordered_set> #include<array> #include<map> #include<iomanip> #include<assert.h> #include<list> #include<bitset> #include<stack> #include<memory> #include<numeric> using namespace std; using namespace std::chrono; typedef long long int llint; typedef long double lldo; #define mp make_pair #define mt make_tuple #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define fir first #define sec second #define res resize #define ins insert #define era erase /*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/ const int mod=1000000007; const llint big=2.19e15+1; const long double pai=3.141592653589793238462643383279502884197; const long double eps=1e-4; template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;} template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;} llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);} llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;} template<class T> void SO(T& ve){sort(ve.begin(),ve.end());} template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());} template<class T>int LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();} template<class T>int UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();} int main(void){ int i,n;cin>>n; static llint a[1<<20]={}; static llint ni[100001]={}; ni[0]=1; for(i=0;i<n;i++){ni[i+1]=ni[i]*2%mod;} while(n--){ int x;cin>>x; a[x]++; } for(i=0;i<20;i++){ for(int bi=0;bi<(1<<20);bi++){ if(bi&(1<<i)){continue;} a[bi]+=a[bi+(1<<i)]; } } llint ans=0; for(int bi=0;bi<(1<<20);bi++){ int fu=1; for(i=0;i<20;i++){if(bi&(1<<i)){fu*=-1;}} ans+=fu*ni[a[bi]];ans%=mod; //if(bi<16){cerr<<ni[a[bi]]<<endl;} } ans+=mod;ans%=mod; cout<<ans<<endl; return 0; } /* 3 0 1 2 3 1 1 0 + - - + 8-2-2+1 =5 */ ```
#include<bits/stdc++.h> using namespace std; using Int = long long; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} struct FastIO{ FastIO(){ cin.tie(0); ios::sync_with_stdio(0); } }fastio_beet; template<typename T,T MOD = 1000000007> struct Mint{ static constexpr T mod = MOD; T v; Mint():v(0){} Mint(signed v):v(v){} Mint(long long t){v=t%MOD;if(v<0) v+=MOD;} Mint pow(long long k){ Mint res(1),tmp(v); while(k){ if(k&1) res*=tmp; tmp*=tmp; k>>=1; } return res; } static Mint add_identity(){return Mint(0);} static Mint mul_identity(){return Mint(1);} Mint inv(){return pow(MOD-2);} Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;} Mint& operator/=(Mint a){return (*this)*=a.inv();} Mint operator+(Mint a) const{return Mint(v)+=a;} Mint operator-(Mint a) const{return Mint(v)-=a;} Mint operator*(Mint a) const{return Mint(v)*=a;} Mint operator/(Mint a) const{return Mint(v)/=a;} Mint operator-() const{return v?Mint(MOD-v):Mint(v);} bool operator==(const Mint a)const{return v==a.v;} bool operator!=(const Mint a)const{return v!=a.v;} bool operator <(const Mint a)const{return v <a.v;} static Mint comb(long long n,Int k){ Mint num(1),dom(1); for(Int i=0;i<k;i++){ num*=Mint(n-i); dom*=Mint(i+1); } return num/dom; } }; template<typename T,T MOD> constexpr T Mint<T, MOD>::mod; template<typename T,T MOD> ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;} //INSERT ABOVE HERE signed main(){ Int n; cin>>n; vector<Int> as(n); for(Int i=0;i<n;i++) cin>>as[i]; const Int m=20; Int sz=1<<m; vector<Int> cnt(sz,0); for(Int a:as) cnt[a]++; for(Int i=0;i<m;i++) for(Int b=0;b<sz;b++) if((~b>>i)&1) cnt[b]+=cnt[b|(1<<i)]; using M = Mint<Int>; M ans{0}; for(Int b=0;b<sz;b++){ if(__builtin_parity(b)) ans-=M(2).pow(cnt[b]); else ans+=M(2).pow(cnt[b]); } cout<<ans<<endl; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include<bits/stdc++.h> using namespace std; using Int = long long; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} struct FastIO{ FastIO(){ cin.tie(0); ios::sync_with_stdio(0); } }fastio_beet; template<typename T,T MOD = 1000000007> struct Mint{ static constexpr T mod = MOD; T v; Mint():v(0){} Mint(signed v):v(v){} Mint(long long t){v=t%MOD;if(v<0) v+=MOD;} Mint pow(long long k){ Mint res(1),tmp(v); while(k){ if(k&1) res*=tmp; tmp*=tmp; k>>=1; } return res; } static Mint add_identity(){return Mint(0);} static Mint mul_identity(){return Mint(1);} Mint inv(){return pow(MOD-2);} Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;} Mint& operator/=(Mint a){return (*this)*=a.inv();} Mint operator+(Mint a) const{return Mint(v)+=a;} Mint operator-(Mint a) const{return Mint(v)-=a;} Mint operator*(Mint a) const{return Mint(v)*=a;} Mint operator/(Mint a) const{return Mint(v)/=a;} Mint operator-() const{return v?Mint(MOD-v):Mint(v);} bool operator==(const Mint a)const{return v==a.v;} bool operator!=(const Mint a)const{return v!=a.v;} bool operator <(const Mint a)const{return v <a.v;} static Mint comb(long long n,Int k){ Mint num(1),dom(1); for(Int i=0;i<k;i++){ num*=Mint(n-i); dom*=Mint(i+1); } return num/dom; } }; template<typename T,T MOD> constexpr T Mint<T, MOD>::mod; template<typename T,T MOD> ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;} //INSERT ABOVE HERE signed main(){ Int n; cin>>n; vector<Int> as(n); for(Int i=0;i<n;i++) cin>>as[i]; const Int m=20; Int sz=1<<m; vector<Int> cnt(sz,0); for(Int a:as) cnt[a]++; for(Int i=0;i<m;i++) for(Int b=0;b<sz;b++) if((~b>>i)&1) cnt[b]+=cnt[b|(1<<i)]; using M = Mint<Int>; M ans{0}; for(Int b=0;b<sz;b++){ if(__builtin_parity(b)) ans-=M(2).pow(cnt[b]); else ans+=M(2).pow(cnt[b]); } cout<<ans<<endl; return 0; } ```
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1000000007; const ll inf = 3e18; const char sp = ' '; int N, a[100000]; ll cnt[1 << 20], DP[1 << 20]; ll pw(ll x, int y) { ll a = 1; while (y) { if (y & 1)a = a * x%mod; x = x * x%mod; y /= 2; } return a; } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; cnt[a[i]]++; } for (int j = 0; j < 20; j++) { for (int i = (1 << 20) - 1; i >= 0; i--) { if (!(i&(1 << j))) { cnt[i] += cnt[i | (1 << j)]; } } } ll res = 0; for (int i = 0; i < (1 << 20); i++) { int c = 0; for (int j = 0; j < 20; j++) { c += ((i >> j) & 1); } if (c & 1) { res -= pw(2, cnt[i]); } else { res += pw(2, cnt[i]); } res = (res + mod) % mod; } cout << res << endl; }
### Prompt Your task is to create a CPP solution to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1000000007; const ll inf = 3e18; const char sp = ' '; int N, a[100000]; ll cnt[1 << 20], DP[1 << 20]; ll pw(ll x, int y) { ll a = 1; while (y) { if (y & 1)a = a * x%mod; x = x * x%mod; y /= 2; } return a; } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; cnt[a[i]]++; } for (int j = 0; j < 20; j++) { for (int i = (1 << 20) - 1; i >= 0; i--) { if (!(i&(1 << j))) { cnt[i] += cnt[i | (1 << j)]; } } } ll res = 0; for (int i = 0; i < (1 << 20); i++) { int c = 0; for (int j = 0; j < 20; j++) { c += ((i >> j) & 1); } if (c & 1) { res -= pw(2, cnt[i]); } else { res += pw(2, cnt[i]); } res = (res + mod) % mod; } cout << res << endl; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; template<class T,class U> using P = pair<T,U>; template<class T> using vec = vector<T>; template<class T> using vvec = vector<vec<T>>; constexpr ll mod = 1e9+7; struct mint { ll x; mint(ll x=0):x((x%mod+mod)%mod){} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res+=a; } mint operator-(const mint a) const { mint res(*this); return res-=a; } mint operator*(const mint a) const { mint res(*this); return res*=a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod-2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res/=a; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vec<int> g(1<<20,0); for(int i=0;i<N;i++){ int a; cin >> a; g[a]++; } for(int i=0;i<20;i++){ for(int S=(1<<20)-1;S>=0;S--) if(!(S&(1<<i))) g[S] += g[S^(1<<i)]; } mint two = 2; mint ans = 0; for(int S=0;S<(1<<20);S++){ int d = __builtin_popcount(S); if(d%2==0) ans += two.pow(g[S])-1; else ans -= two.pow(g[S])-1; } cout << ans.x << endl; }
### Prompt In CPP, your task is to solve the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; template<class T,class U> using P = pair<T,U>; template<class T> using vec = vector<T>; template<class T> using vvec = vector<vec<T>>; constexpr ll mod = 1e9+7; struct mint { ll x; mint(ll x=0):x((x%mod+mod)%mod){} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res+=a; } mint operator-(const mint a) const { mint res(*this); return res-=a; } mint operator*(const mint a) const { mint res(*this); return res*=a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod-2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res/=a; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vec<int> g(1<<20,0); for(int i=0;i<N;i++){ int a; cin >> a; g[a]++; } for(int i=0;i<20;i++){ for(int S=(1<<20)-1;S>=0;S--) if(!(S&(1<<i))) g[S] += g[S^(1<<i)]; } mint two = 2; mint ans = 0; for(int S=0;S<(1<<20);S++){ int d = __builtin_popcount(S); if(d%2==0) ans += two.pow(g[S])-1; else ans -= two.pow(g[S])-1; } cout << ans.x << endl; } ```
#include <bits/stdc++.h> using namespace std; template <unsigned Mod> struct ModularInt { using M = ModularInt; unsigned v; ModularInt(long long a = 0) : v((a %= Mod) < 0 ? a + Mod : a) {} M operator-() const { return M() -= *this; } M& operator*=(M r) { v = (uint64_t)v * r.v % Mod; return *this; } M& operator/=(M r) { return *this *= r.inv(); } M& operator+=(M r) { if ((v += r.v) >= Mod) v -= Mod; return *this; } M& operator-=(M r) { if ((v += Mod - r.v) >= Mod) v -= Mod; return *this; } friend M operator*(M l, M r) { return l *= r; } friend M operator/(M l, M r) { return l /= r; } friend M operator+(M l, M r) { return l += r; } friend M operator-(M l, M r) { return l -= r; } M pow(long long n) const { assert(n >= 0); M res = 1; for (M t = *this; n; t *= t, n >>= 1) if (n & 1) res *= t; return res; } M inv() const { assert(v != 0); return pow(Mod - 2); } friend string to_string(M a) { return to_string(a.v); } }; constexpr unsigned mod = 1e9 + 7; using Mint = ModularInt<mod>; template<class T> void fzt(vector<T>& a) { int n = a.size(); assert(__builtin_popcount(n) == 1); for (int i = 0; 1 << i < n; ++i) { for (int S = 0; S < n; ++S) if (~S & 1 << i) { a[S] += a[S | 1 << i]; } } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); vector<int> a(1 << 20); int n; cin >> n; while (n--) { int x; cin >> x; ++a[x]; } fzt(a); Mint res; for (int s = 0; s < 1 << 20; ++s) { if (__builtin_parity(s)) { res -= Mint(2).pow(a[s]); } else { res += Mint(2).pow(a[s]); } } cout << res.v << '\n'; }
### Prompt Your task is to create a cpp solution to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <unsigned Mod> struct ModularInt { using M = ModularInt; unsigned v; ModularInt(long long a = 0) : v((a %= Mod) < 0 ? a + Mod : a) {} M operator-() const { return M() -= *this; } M& operator*=(M r) { v = (uint64_t)v * r.v % Mod; return *this; } M& operator/=(M r) { return *this *= r.inv(); } M& operator+=(M r) { if ((v += r.v) >= Mod) v -= Mod; return *this; } M& operator-=(M r) { if ((v += Mod - r.v) >= Mod) v -= Mod; return *this; } friend M operator*(M l, M r) { return l *= r; } friend M operator/(M l, M r) { return l /= r; } friend M operator+(M l, M r) { return l += r; } friend M operator-(M l, M r) { return l -= r; } M pow(long long n) const { assert(n >= 0); M res = 1; for (M t = *this; n; t *= t, n >>= 1) if (n & 1) res *= t; return res; } M inv() const { assert(v != 0); return pow(Mod - 2); } friend string to_string(M a) { return to_string(a.v); } }; constexpr unsigned mod = 1e9 + 7; using Mint = ModularInt<mod>; template<class T> void fzt(vector<T>& a) { int n = a.size(); assert(__builtin_popcount(n) == 1); for (int i = 0; 1 << i < n; ++i) { for (int S = 0; S < n; ++S) if (~S & 1 << i) { a[S] += a[S | 1 << i]; } } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); vector<int> a(1 << 20); int n; cin >> n; while (n--) { int x; cin >> x; ++a[x]; } fzt(a); Mint res; for (int s = 0; s < 1 << 20; ++s) { if (__builtin_parity(s)) { res -= Mint(2).pow(a[s]); } else { res += Mint(2).pow(a[s]); } } cout << res.v << '\n'; } ```
#include <iostream> #include <algorithm> #include <iomanip> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208; template<u32 M = 1000000007> struct modint{ u32 val; modint(): val(0){} template<typename T> modint(T t){t %= (T)M; if(t < 0) t += (T)M; val = t;} modint pow(ll k) const { modint res(1), x(val); while(k){ if(k&1) res *= x; x *= x; k >>= 1; } return res; } template<typename T> modint& operator=(T t){t %= (T)M; if(t < 0) t += (T)M; val = t; return *this;} modint inv() const {return pow(M-2);} modint& operator+=(modint a){val += a.val; if(val >= M) val -= M; return *this;} modint& operator-=(modint a){if(val < a.val) val += M-a.val; else val -= a.val; return *this;} modint& operator*=(modint a){val = (u64)val*a.val%M; return *this;} modint& operator/=(modint a){return (*this) *= a.inv();} modint operator+(modint a) const {return modint(val) +=a;} modint operator-(modint a) const {return modint(val) -=a;} modint operator*(modint a) const {return modint(val) *=a;} modint operator/(modint a) const {return modint(val) /=a;} modint operator-(){return modint(M-val);} bool operator==(const modint a) const {return val == a.val;} bool operator!=(const modint a) const {return val != a.val;} bool operator<(const modint a) const {return val < a.val;} }; using mint = modint<MOD>; int main() { int n; cin >> n; vector<int> dp(1 << 20); for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); dp[x]++; } for (int i = 0; i < 20; ++i) { for (int j = 0; j < (1 << 20); ++j) { if(!(j & (1 << i))) dp[j] += dp[j^(1 << i)]; } } vector<mint> mul2(n+1); mul2[0] = 1; for (int i = 0; i < n; ++i) { mul2[i+1] = mul2[i]+mul2[i]; } mint ans = 0; for (int i = 0; i < (1 << 20); ++i) { if(__builtin_popcount(i)&1) ans -= mul2[dp[i]]; else ans += mul2[dp[i]]; } cout << ans.val << "\n"; return 0; }
### Prompt Generate a Cpp solution to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <iostream> #include <algorithm> #include <iomanip> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208; template<u32 M = 1000000007> struct modint{ u32 val; modint(): val(0){} template<typename T> modint(T t){t %= (T)M; if(t < 0) t += (T)M; val = t;} modint pow(ll k) const { modint res(1), x(val); while(k){ if(k&1) res *= x; x *= x; k >>= 1; } return res; } template<typename T> modint& operator=(T t){t %= (T)M; if(t < 0) t += (T)M; val = t; return *this;} modint inv() const {return pow(M-2);} modint& operator+=(modint a){val += a.val; if(val >= M) val -= M; return *this;} modint& operator-=(modint a){if(val < a.val) val += M-a.val; else val -= a.val; return *this;} modint& operator*=(modint a){val = (u64)val*a.val%M; return *this;} modint& operator/=(modint a){return (*this) *= a.inv();} modint operator+(modint a) const {return modint(val) +=a;} modint operator-(modint a) const {return modint(val) -=a;} modint operator*(modint a) const {return modint(val) *=a;} modint operator/(modint a) const {return modint(val) /=a;} modint operator-(){return modint(M-val);} bool operator==(const modint a) const {return val == a.val;} bool operator!=(const modint a) const {return val != a.val;} bool operator<(const modint a) const {return val < a.val;} }; using mint = modint<MOD>; int main() { int n; cin >> n; vector<int> dp(1 << 20); for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); dp[x]++; } for (int i = 0; i < 20; ++i) { for (int j = 0; j < (1 << 20); ++j) { if(!(j & (1 << i))) dp[j] += dp[j^(1 << i)]; } } vector<mint> mul2(n+1); mul2[0] = 1; for (int i = 0; i < n; ++i) { mul2[i+1] = mul2[i]+mul2[i]; } mint ans = 0; for (int i = 0; i < (1 << 20); ++i) { if(__builtin_popcount(i)&1) ans -= mul2[dp[i]]; else ans += mul2[dp[i]]; } cout << ans.val << "\n"; return 0; } ```
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll MOD=1e9+7; ll dp[(1<<20)]; ll mod_pow(ll x,ll n){ ll res=1; while(n>0){ if(n&1) res=res*x%MOD; x=x*x%MOD; n>>=1; } return res; } int main(){ int N; cin>>N; vector<int> A(N); rep(i,N) cin>>A[i]; rep(i,N) dp[A[i]]++; for(int k=0;k<20;k++){ for(int s=0;s<(1<<20);s++){ if(s&(1<<k)); else dp[s]+=dp[s^(1<<k)]; } } ll ans=0; for(int i=1;i<(1<<20);i++){ int n=0; for(int j=0;j<20;j++){ if(i&(1<<j)) n++; } if(n%2) ans+=(mod_pow(2,dp[i])-1); else ans-=(mod_pow(2,dp[i])-1); ans=(ans+MOD)%MOD; } ll result=mod_pow(2,N)-1-ans; result=(result+MOD)%MOD; cout<<result<<endl; return 0; }
### Prompt Develop a solution in cpp to the problem described below: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll MOD=1e9+7; ll dp[(1<<20)]; ll mod_pow(ll x,ll n){ ll res=1; while(n>0){ if(n&1) res=res*x%MOD; x=x*x%MOD; n>>=1; } return res; } int main(){ int N; cin>>N; vector<int> A(N); rep(i,N) cin>>A[i]; rep(i,N) dp[A[i]]++; for(int k=0;k<20;k++){ for(int s=0;s<(1<<20);s++){ if(s&(1<<k)); else dp[s]+=dp[s^(1<<k)]; } } ll ans=0; for(int i=1;i<(1<<20);i++){ int n=0; for(int j=0;j<20;j++){ if(i&(1<<j)) n++; } if(n%2) ans+=(mod_pow(2,dp[i])-1); else ans-=(mod_pow(2,dp[i])-1); ans=(ans+MOD)%MOD; } ll result=mod_pow(2,N)-1-ans; result=(result+MOD)%MOD; cout<<result<<endl; return 0; } ```
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair<int,int> P; #define yn {puts("Yes");}else{puts("No");} #define MAX_N 200005 int dp[1 << 20] = {}; const ll MOD = 1e9+7; ll fac[MAX_N]; int main() { int n; cin >> n; int a[n]; rep(i,n){ cin >> a[i]; dp[a[i]]++; } rep(i,20) rep(b, 1 << 20) if(0 == (b & (1 << i))) dp[b] += dp[b | (1 << i)]; rep(i,MAX_N){ if(i == 0){ fac[i] = 1; }else{ fac[i] = fac[i-1] * 2 % MOD; } } ll ans = 0; rep(i,1 << 20){ if(__builtin_popcount(i) % 2 == 1){ ans += MOD - (fac[dp[i]] - 1); }else{ ans += fac[dp[i]] - 1; } ans %= MOD; } cout << ans << endl; return 0; }
### Prompt Please formulate a CPP solution to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair<int,int> P; #define yn {puts("Yes");}else{puts("No");} #define MAX_N 200005 int dp[1 << 20] = {}; const ll MOD = 1e9+7; ll fac[MAX_N]; int main() { int n; cin >> n; int a[n]; rep(i,n){ cin >> a[i]; dp[a[i]]++; } rep(i,20) rep(b, 1 << 20) if(0 == (b & (1 << i))) dp[b] += dp[b | (1 << i)]; rep(i,MAX_N){ if(i == 0){ fac[i] = 1; }else{ fac[i] = fac[i-1] * 2 % MOD; } } ll ans = 0; rep(i,1 << 20){ if(__builtin_popcount(i) % 2 == 1){ ans += MOD - (fac[dp[i]] - 1); }else{ ans += fac[dp[i]] - 1; } ans %= MOD; } cout << ans << endl; return 0; } ```
// #define _GLIBCXX_DEBUG // for STL debug (optional) #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; using ll = long long int; using int64 = long long int; template<typename T> void chmax(T &a, T b) {a = max(a, b);} template<typename T> void chmin(T &a, T b) {a = min(a, b);} template<typename T> void chadd(T &a, T b) {a = a + b;} int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const int INF = 1LL << 29; const ll LONGINF = 1LL << 60; const ll MOD = 1000000007LL; // ModInt begin using ll = long long; template<ll mod> struct ModInt { ll v; ll mod_pow(ll x, ll n) const { return (!n) ? 1 : (mod_pow((x*x)%mod,n/2) * ((n&1)?x:1)) % mod; } ModInt(ll a = 0) : v((a %= mod) < 0 ? a + mod : a) {} ModInt operator+ ( const ModInt& b ) const { return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v)); } ModInt operator- () const { return ModInt(-v); } ModInt operator- ( const ModInt& b ) const { return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v)); } ModInt operator* ( const ModInt& b ) const {return (v * b.v) % mod;} ModInt operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, mod-2)) % mod;} bool operator== ( const ModInt &b ) const {return v == b.v;} bool operator!= ( const ModInt &b ) const {return !(*this == b); } ModInt& operator+= ( const ModInt &b ) { v += b.v; if(v >= mod) v -= mod; return *this; } ModInt& operator-= ( const ModInt &b ) { v -= b.v; if(v < 0) v += mod; return *this; } ModInt& operator*= ( const ModInt &b ) { (v *= b.v) %= mod; return *this; } ModInt& operator/= ( const ModInt &b ) { (v *= mod_pow(b.v, mod-2)) %= mod; return *this; } ModInt pow(ll x) { return ModInt(mod_pow(v, x)); } // operator int() const { return int(v); } // operator long long int() const { return v; } }; template<ll mod> ostream& operator<< (ostream& out, ModInt<mod> a) {return out << a.v;} template<ll mod> istream& operator>> (istream& in, ModInt<mod>& a) { in >> a.v; return in; } // ModInt end using mint = ModInt<MOD>; int dp[1 << 20]; int main() { int N; cin >> N; vector<int> A(N); for(int i=0; i<N; i++) cin >> A[i], dp[ A[i] ]++; const int B = 20; for(int i=0; i<B; i++) { for(int bit=0; bit<(1<<B); bit++) { if(bit >> i & 1) dp[bit ^ (1 << i)] += dp[bit]; } } mint ans = 0; for(int bit=0; bit<(1<<B); bit++) { mint p = mint(2).pow(dp[bit]) - mint(1); if(__builtin_parity(bit)) ans -= p; else ans += p; } cout << ans << endl; return 0; }
### Prompt Generate a cpp solution to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp // #define _GLIBCXX_DEBUG // for STL debug (optional) #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; using ll = long long int; using int64 = long long int; template<typename T> void chmax(T &a, T b) {a = max(a, b);} template<typename T> void chmin(T &a, T b) {a = min(a, b);} template<typename T> void chadd(T &a, T b) {a = a + b;} int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const int INF = 1LL << 29; const ll LONGINF = 1LL << 60; const ll MOD = 1000000007LL; // ModInt begin using ll = long long; template<ll mod> struct ModInt { ll v; ll mod_pow(ll x, ll n) const { return (!n) ? 1 : (mod_pow((x*x)%mod,n/2) * ((n&1)?x:1)) % mod; } ModInt(ll a = 0) : v((a %= mod) < 0 ? a + mod : a) {} ModInt operator+ ( const ModInt& b ) const { return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v)); } ModInt operator- () const { return ModInt(-v); } ModInt operator- ( const ModInt& b ) const { return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v)); } ModInt operator* ( const ModInt& b ) const {return (v * b.v) % mod;} ModInt operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, mod-2)) % mod;} bool operator== ( const ModInt &b ) const {return v == b.v;} bool operator!= ( const ModInt &b ) const {return !(*this == b); } ModInt& operator+= ( const ModInt &b ) { v += b.v; if(v >= mod) v -= mod; return *this; } ModInt& operator-= ( const ModInt &b ) { v -= b.v; if(v < 0) v += mod; return *this; } ModInt& operator*= ( const ModInt &b ) { (v *= b.v) %= mod; return *this; } ModInt& operator/= ( const ModInt &b ) { (v *= mod_pow(b.v, mod-2)) %= mod; return *this; } ModInt pow(ll x) { return ModInt(mod_pow(v, x)); } // operator int() const { return int(v); } // operator long long int() const { return v; } }; template<ll mod> ostream& operator<< (ostream& out, ModInt<mod> a) {return out << a.v;} template<ll mod> istream& operator>> (istream& in, ModInt<mod>& a) { in >> a.v; return in; } // ModInt end using mint = ModInt<MOD>; int dp[1 << 20]; int main() { int N; cin >> N; vector<int> A(N); for(int i=0; i<N; i++) cin >> A[i], dp[ A[i] ]++; const int B = 20; for(int i=0; i<B; i++) { for(int bit=0; bit<(1<<B); bit++) { if(bit >> i & 1) dp[bit ^ (1 << i)] += dp[bit]; } } mint ans = 0; for(int bit=0; bit<(1<<B); bit++) { mint p = mint(2).pow(dp[bit]) - mint(1); if(__builtin_parity(bit)) ans -= p; else ans += p; } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; #define SZ(x) (int)(x.size()) using ll = long long; using ld = long double; using P = pair<int, int>; using vi = vector<int>; using vvi = vector<vector<int>>; using vll = vector<ll>; using vvll = vector<vector<ll>>; const double eps = 1e-10; const int MOD = 1000000007; const int INF = 1000000000; const ll LINF = 1ll<<50; template<typename T> void printv(const vector<T>& s) { for(int i=0;i<(int)(s.size());++i) { cout << s[i]; if(i == (int)(s.size())-1) cout << endl; else cout << " "; } } int cnt(int n) { int res = 0; while(n > 0) { res += n % 2; n /= 2; } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); int n; cin >> n; vi v(1<<20); for(int i=0;i<n;++i) { int a; cin >> a; v[a]++; } for(int i=0;i<20;++i) { for(int j=0;j<(1<<20);++j) { if((j>>i)&1) { v[j-(1<<i)] += v[j]; } } } vll po(n); po[0] = 1; for(int i=0;i<n;++i) { po[i+1] = po[i] * 2 % MOD; } ll ans = 0; for(int i=0;i<(1<<20);++i) { if(cnt(i) % 2 == 0) { ans += po[v[i]]; ans %= MOD; } else { ans += MOD - po[v[i]]; ans %= MOD; } } cout << ans << endl; }
### Prompt In CPP, your task is to solve the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define SZ(x) (int)(x.size()) using ll = long long; using ld = long double; using P = pair<int, int>; using vi = vector<int>; using vvi = vector<vector<int>>; using vll = vector<ll>; using vvll = vector<vector<ll>>; const double eps = 1e-10; const int MOD = 1000000007; const int INF = 1000000000; const ll LINF = 1ll<<50; template<typename T> void printv(const vector<T>& s) { for(int i=0;i<(int)(s.size());++i) { cout << s[i]; if(i == (int)(s.size())-1) cout << endl; else cout << " "; } } int cnt(int n) { int res = 0; while(n > 0) { res += n % 2; n /= 2; } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); int n; cin >> n; vi v(1<<20); for(int i=0;i<n;++i) { int a; cin >> a; v[a]++; } for(int i=0;i<20;++i) { for(int j=0;j<(1<<20);++j) { if((j>>i)&1) { v[j-(1<<i)] += v[j]; } } } vll po(n); po[0] = 1; for(int i=0;i<n;++i) { po[i+1] = po[i] * 2 % MOD; } ll ans = 0; for(int i=0;i<(1<<20);++i) { if(cnt(i) % 2 == 0) { ans += po[v[i]]; ans %= MOD; } else { ans += MOD - po[v[i]]; ans %= MOD; } } cout << ans << endl; } ```
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; using ll = long long; using lpair = pair<ll, ll>; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) #define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];}; void print() {} template <class H,class... T> void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);} ll power(ll x, ll n){ if(n == 0) return 1LL; ll res = power(x * x % MOD, n/2); if(n % 2 == 1) res = res * x % MOD; return res; } ll bitcnt[1LL<<22] = {}; ll number[1LL<<22] = {}; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; ll a[100010]; rep(i,0,N) cin >> a[i]; ll ans = 0; vector<ll> uq; rep(i,0,N) uq.push_back(a[i]); sort(uq.begin(), uq.end()); uq.erase(unique(uq.begin(), uq.end()), uq.end()); rep(i,0,N) number[a[i]]++; for(auto &e: uq){ for(int bit = e; ; bit = (bit-1) & e) { bitcnt[bit] += number[e]; if (!bit) break; } } // rep(i,0,N){ // for(int bit = a[i]; ; bit = (bit-1) & a[i]) { // bitcnt[bit]++; // if (!bit) break; // } // } // ll num[22] = {}; // rep(p,0,22){ // rep(i,0,N){ // if((a[i]>>p)&1) num[p]++; // } // } rep(bit,1,(1LL<<21)){ ll cnt = __builtin_popcount(bit); ll nn = bitcnt[bit]; ll val = (power(2, nn) - 1 + MOD) % MOD; if(cnt % 2 == 1){ ans += val; }else{ ans -= val; } ans %= MOD; } ans = (power(2,N) - 1 - ans + MOD*2) % MOD; print(ans); }
### Prompt In Cpp, your task is to solve the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp /*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; using ll = long long; using lpair = pair<ll, ll>; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) #define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];}; void print() {} template <class H,class... T> void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);} ll power(ll x, ll n){ if(n == 0) return 1LL; ll res = power(x * x % MOD, n/2); if(n % 2 == 1) res = res * x % MOD; return res; } ll bitcnt[1LL<<22] = {}; ll number[1LL<<22] = {}; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; ll a[100010]; rep(i,0,N) cin >> a[i]; ll ans = 0; vector<ll> uq; rep(i,0,N) uq.push_back(a[i]); sort(uq.begin(), uq.end()); uq.erase(unique(uq.begin(), uq.end()), uq.end()); rep(i,0,N) number[a[i]]++; for(auto &e: uq){ for(int bit = e; ; bit = (bit-1) & e) { bitcnt[bit] += number[e]; if (!bit) break; } } // rep(i,0,N){ // for(int bit = a[i]; ; bit = (bit-1) & a[i]) { // bitcnt[bit]++; // if (!bit) break; // } // } // ll num[22] = {}; // rep(p,0,22){ // rep(i,0,N){ // if((a[i]>>p)&1) num[p]++; // } // } rep(bit,1,(1LL<<21)){ ll cnt = __builtin_popcount(bit); ll nn = bitcnt[bit]; ll val = (power(2, nn) - 1 + MOD) % MOD; if(cnt % 2 == 1){ ans += val; }else{ ans -= val; } ans %= MOD; } ans = (power(2,N) - 1 - ans + MOD*2) % MOD; print(ans); } ```
// // Created by yamunaku on 2019/12/29. // #include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (n); i++) #define repl(i, l, r) for(int i = (l); i < (r); i++) #define per(i, n) for(int i = ((n)-1); i >= 0; i--) #define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--) #define all(x) (x).begin(),(x).end() #define MOD9 998244353 #define MOD 1000000007 #define IINF 1000000000 #define LINF 1000000000000000000 #define SP <<" "<< #define CYES cout<<"Yes"<<endl #define CNO cout<<"No"<<endl #define CFS cin.tie(0);ios::sync_with_stdio(false) #define CST(x) cout<<fixed<<setprecision(x) using ll = long long; using ld = long double; using vi = vector<int>; using mti = vector<vector<int>>; using vl = vector<ll>; using mtl = vector<vector<ll>>; using pi = pair<int, int>; using pl = pair<ll, ll>; template<typename T> using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>; ll modpow(ll x, ll a){ ll ans = 1; while(a){ if(a & 1) ans = ans * x % MOD; a >>= 1; x = x * x % MOD; } return ans; } ll inv(ll x){ return modpow(x, MOD - 2); } int main(){ // CFS; int n; cin >> n; vi a(n); int b = 20; vi dp(1 << b, 0); rep(i, n){ cin >> a[i]; dp[a[i]]++; } rep(i, b){ rep(j, 1 << b){ if(j & (1 << i)) dp[j ^ (1 << i)] += dp[j]; } } ll ans = modpow(2, n) - 1; vl mp(n + 1, 1); repl(i, 1, n + 1) mp[i] = mp[i - 1] * 2 % MOD; repl(i, 1, 1 << b){ int c = 0; rep(j, b) if(i & (1 << j)) c++; ans = (ans + (mp[dp[i]] - 1 + MOD) % MOD * (1 - c % 2 * 2) + MOD) % MOD; } cout << ans << endl; return 0; }
### Prompt Create a solution in cpp for the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp // // Created by yamunaku on 2019/12/29. // #include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (n); i++) #define repl(i, l, r) for(int i = (l); i < (r); i++) #define per(i, n) for(int i = ((n)-1); i >= 0; i--) #define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--) #define all(x) (x).begin(),(x).end() #define MOD9 998244353 #define MOD 1000000007 #define IINF 1000000000 #define LINF 1000000000000000000 #define SP <<" "<< #define CYES cout<<"Yes"<<endl #define CNO cout<<"No"<<endl #define CFS cin.tie(0);ios::sync_with_stdio(false) #define CST(x) cout<<fixed<<setprecision(x) using ll = long long; using ld = long double; using vi = vector<int>; using mti = vector<vector<int>>; using vl = vector<ll>; using mtl = vector<vector<ll>>; using pi = pair<int, int>; using pl = pair<ll, ll>; template<typename T> using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>; ll modpow(ll x, ll a){ ll ans = 1; while(a){ if(a & 1) ans = ans * x % MOD; a >>= 1; x = x * x % MOD; } return ans; } ll inv(ll x){ return modpow(x, MOD - 2); } int main(){ // CFS; int n; cin >> n; vi a(n); int b = 20; vi dp(1 << b, 0); rep(i, n){ cin >> a[i]; dp[a[i]]++; } rep(i, b){ rep(j, 1 << b){ if(j & (1 << i)) dp[j ^ (1 << i)] += dp[j]; } } ll ans = modpow(2, n) - 1; vl mp(n + 1, 1); repl(i, 1, n + 1) mp[i] = mp[i - 1] * 2 % MOD; repl(i, 1, 1 << b){ int c = 0; rep(j, b) if(i & (1 << j)) c++; ans = (ans + (mp[dp[i]] - 1 + MOD) % MOD * (1 - c % 2 * 2) + MOD) % MOD; } cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> #define pb push_back #define eb emplace_back #define fi first #define se second #define rep(i,N) for(long long i = 0; i < (long long)(N); i++) #define repr(i,N) for(long long i = (long long)(N) - 1; i >= 0; i--) #define rep1(i,N) for(long long i = 1; i <= (long long)(N) ; i++) #define repr1(i,N) for(long long i = (N) ; (long long)(i) > 0 ; i--) #define each(x,v) for(auto& x : v) #define all(v) (v).begin(),(v).end() #define sz(v) ((int)(v).size()) #define ini(...) int __VA_ARGS__; in(__VA_ARGS__) #define inl(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ins(...) string __VA_ARGS__; in(__VA_ARGS__) using namespace std; void solve(); using ll = long long; template<class T = ll> using V = vector<T>; using vi = V<int>; using vl = V<>; using vvi = V< V<int> >; constexpr int inf = 1001001001; constexpr ll infLL = (1LL << 61) - 1; struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7);} } iosetupnya; template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { int s = (int)v.size(); rep(i,s) os << (i ? " " : "") << v[i]; return os; } template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &x : v) is >> x; return is; } void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);} void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);} template<typename T>void die(T x){out(x); exit(0);} #ifdef NyaanDebug #include "NyaanDebug.h" #define trc(...) do { cerr << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0) #define trca(v,N) do { cerr << #v << " = "; array_out(v , N);cout << endl;} while(0) #else #define trc(...) #define trca(...) int main(){solve();} #endif using P = pair<ll,ll>; using vp = V<P>; constexpr int MOD = /**/ 1000000007; //*/ 998244353; //////////////////////////////// template< int mod > struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int) (1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt< MOD >; using vm = vector<modint>; vector<int> f(1048576 + 10 , 0); void solve(){ ini(N); rep(i , N){ ini(x); f[x]++;} rep(i,20) rep(j,1<<20) if (!(j&(1<<i))) f[j]+=f[j|(1<<i)]; rep(i,1000) if(f[i]) trc(i , f[i]); modint ans = 0; rep(i , 1048576){ if(f[i] == 0) continue; modint cur = modint(2).pow(f[i]) - 1; if( (__builtin_popcount(i) & 1) == 0) ans += cur; else ans -= cur; } out(ans); }
### Prompt Your challenge is to write a cpp solution to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <bits/stdc++.h> #define pb push_back #define eb emplace_back #define fi first #define se second #define rep(i,N) for(long long i = 0; i < (long long)(N); i++) #define repr(i,N) for(long long i = (long long)(N) - 1; i >= 0; i--) #define rep1(i,N) for(long long i = 1; i <= (long long)(N) ; i++) #define repr1(i,N) for(long long i = (N) ; (long long)(i) > 0 ; i--) #define each(x,v) for(auto& x : v) #define all(v) (v).begin(),(v).end() #define sz(v) ((int)(v).size()) #define ini(...) int __VA_ARGS__; in(__VA_ARGS__) #define inl(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ins(...) string __VA_ARGS__; in(__VA_ARGS__) using namespace std; void solve(); using ll = long long; template<class T = ll> using V = vector<T>; using vi = V<int>; using vl = V<>; using vvi = V< V<int> >; constexpr int inf = 1001001001; constexpr ll infLL = (1LL << 61) - 1; struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7);} } iosetupnya; template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { int s = (int)v.size(); rep(i,s) os << (i ? " " : "") << v[i]; return os; } template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &x : v) is >> x; return is; } void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);} void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);} template<typename T>void die(T x){out(x); exit(0);} #ifdef NyaanDebug #include "NyaanDebug.h" #define trc(...) do { cerr << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0) #define trca(v,N) do { cerr << #v << " = "; array_out(v , N);cout << endl;} while(0) #else #define trc(...) #define trca(...) int main(){solve();} #endif using P = pair<ll,ll>; using vp = V<P>; constexpr int MOD = /**/ 1000000007; //*/ 998244353; //////////////////////////////// template< int mod > struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int) (1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt< MOD >; using vm = vector<modint>; vector<int> f(1048576 + 10 , 0); void solve(){ ini(N); rep(i , N){ ini(x); f[x]++;} rep(i,20) rep(j,1<<20) if (!(j&(1<<i))) f[j]+=f[j|(1<<i)]; rep(i,1000) if(f[i]) trc(i , f[i]); modint ans = 0; rep(i , 1048576){ if(f[i] == 0) continue; modint cur = modint(2).pow(f[i]) - 1; if( (__builtin_popcount(i) & 1) == 0) ans += cur; else ans -= cur; } out(ans); } ```
#include<iostream> using namespace std; long mod=1e9+7; long power(long a,long b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;} int N; int cnt[1<<20]; int main() { cin>>N; for(int i=0;i<N;i++) { int a;cin>>a;cnt[a]++; } for(int i=0;i<20;i++)for(int j=0;j<1<<20;j++)if(!(j>>i&1))cnt[j]+=cnt[j|1<<i]; long ans=0; for(int i=0;i<1<<20;i++) { long now=power(2,cnt[i]); ans+=__builtin_popcount(i)%2?-now:now; } cout<<(ans%mod+mod)%mod<<endl; }
### Prompt Please provide a CPP coded solution to the problem described below: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include<iostream> using namespace std; long mod=1e9+7; long power(long a,long b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;} int N; int cnt[1<<20]; int main() { cin>>N; for(int i=0;i<N;i++) { int a;cin>>a;cnt[a]++; } for(int i=0;i<20;i++)for(int j=0;j<1<<20;j++)if(!(j>>i&1))cnt[j]+=cnt[j|1<<i]; long ans=0; for(int i=0;i<1<<20;i++) { long now=power(2,cnt[i]); ans+=__builtin_popcount(i)%2?-now:now; } cout<<(ans%mod+mod)%mod<<endl; } ```
#ifndef CLASS_MODINT #define CLASS_MODINT #include <cstdint> template <std::uint32_t mod> class modint { private: std::uint32_t n; public: modint() : n(0) {}; modint(std::int64_t n_) : n((n_ >= 0 ? n_ : mod - (-n_) % mod) % mod) {}; static constexpr std::uint32_t get_mod() { return mod; } std::uint32_t get() const { return n; } bool operator==(const modint& m) const { return n == m.n; } bool operator!=(const modint& m) const { return n != m.n; } modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; } modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; } modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; } modint operator+(const modint& m) const { return modint(*this) += m; } modint operator-(const modint& m) const { return modint(*this) -= m; } modint operator*(const modint& m) const { return modint(*this) *= m; } modint inv() const { return (*this).pow(mod - 2); } modint pow(std::uint64_t b) const { modint ans = 1, m = modint(*this); while (b) { if (b & 1) ans *= m; m *= m; b >>= 1; } return ans; } }; #endif // CLASS_MODINT #include <vector> #include <iostream> using namespace std; using mint = modint<1000000007>; int main() { int N; cin >> N; int bits = 20; vector<int> c(1 << bits); for (int i = 0; i < N; ++i) { int x; cin >> x; c[x] += 1; } for (int i = 0; i < bits; ++i) { for (int j = 0; j < 1 << bits; ++j) { if ((j >> i) & 1) { c[j - (1 << i)] += c[j]; } } } mint ans = 0; for (int i = 0; i < 1 << bits; ++i) { int cnt = 0; for (int j = 0; j < bits; ++j) { if ((i >> j) & 1) ++cnt; } if (cnt % 2 == 0) { ans += mint(2).pow(c[i]); } else { ans -= mint(2).pow(c[i]); } } cout << ans.get() << endl; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #ifndef CLASS_MODINT #define CLASS_MODINT #include <cstdint> template <std::uint32_t mod> class modint { private: std::uint32_t n; public: modint() : n(0) {}; modint(std::int64_t n_) : n((n_ >= 0 ? n_ : mod - (-n_) % mod) % mod) {}; static constexpr std::uint32_t get_mod() { return mod; } std::uint32_t get() const { return n; } bool operator==(const modint& m) const { return n == m.n; } bool operator!=(const modint& m) const { return n != m.n; } modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; } modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; } modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; } modint operator+(const modint& m) const { return modint(*this) += m; } modint operator-(const modint& m) const { return modint(*this) -= m; } modint operator*(const modint& m) const { return modint(*this) *= m; } modint inv() const { return (*this).pow(mod - 2); } modint pow(std::uint64_t b) const { modint ans = 1, m = modint(*this); while (b) { if (b & 1) ans *= m; m *= m; b >>= 1; } return ans; } }; #endif // CLASS_MODINT #include <vector> #include <iostream> using namespace std; using mint = modint<1000000007>; int main() { int N; cin >> N; int bits = 20; vector<int> c(1 << bits); for (int i = 0; i < N; ++i) { int x; cin >> x; c[x] += 1; } for (int i = 0; i < bits; ++i) { for (int j = 0; j < 1 << bits; ++j) { if ((j >> i) & 1) { c[j - (1 << i)] += c[j]; } } } mint ans = 0; for (int i = 0; i < 1 << bits; ++i) { int cnt = 0; for (int j = 0; j < bits; ++j) { if ((i >> j) & 1) ++cnt; } if (cnt % 2 == 0) { ans += mint(2).pow(c[i]); } else { ans -= mint(2).pow(c[i]); } } cout << ans.get() << endl; return 0; } ```
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define HUGE_NUM 99999999999999999 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; #define SIZE 100005 #define MAX 20 int N; ll dp[1 << MAX]; ll POW[SIZE]; int main(){ POW[0] = 1; for(int i = 1; i < SIZE; i++){ POW[i] = POW[i-1]*2; POW[i] %= MOD; } scanf("%d",&N); for(int i = 0; i < POW[MAX]; i++){ dp[i] = 0; } int tmp; for(int loop = 0; loop < N; loop++){ scanf("%d",&tmp); dp[tmp] += 1; } for(int loop = 0; loop < MAX; loop++){ for(int state = POW[MAX]-1; state >= 0; state--){ if((state & (1 << loop)) == 0){ //自分を含む集合の個数を求める dp[state] += dp[state+POW[loop]]; } } } ll ans = POW[N]-1; ll minus = 0; //全体から、少なくとも1つ以上の桁が1である集合を除く for(int i = 1; i < POW[MAX]; i++){ int count = 0; for(int loop = 0; loop < MAX; loop++){ if(i & (1 << loop))count++; } if(count%2 == 1){ minus += (POW[dp[i]]-1); minus %= MOD; }else{ minus -= (POW[dp[i]]-1); if(minus < 0){ minus += MOD; } } } ans -= minus; if(ans < 0){ ans += MOD; } printf("%lld\n",ans); return 0; }
### Prompt In cpp, your task is to solve the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define HUGE_NUM 99999999999999999 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; #define SIZE 100005 #define MAX 20 int N; ll dp[1 << MAX]; ll POW[SIZE]; int main(){ POW[0] = 1; for(int i = 1; i < SIZE; i++){ POW[i] = POW[i-1]*2; POW[i] %= MOD; } scanf("%d",&N); for(int i = 0; i < POW[MAX]; i++){ dp[i] = 0; } int tmp; for(int loop = 0; loop < N; loop++){ scanf("%d",&tmp); dp[tmp] += 1; } for(int loop = 0; loop < MAX; loop++){ for(int state = POW[MAX]-1; state >= 0; state--){ if((state & (1 << loop)) == 0){ //自分を含む集合の個数を求める dp[state] += dp[state+POW[loop]]; } } } ll ans = POW[N]-1; ll minus = 0; //全体から、少なくとも1つ以上の桁が1である集合を除く for(int i = 1; i < POW[MAX]; i++){ int count = 0; for(int loop = 0; loop < MAX; loop++){ if(i & (1 << loop))count++; } if(count%2 == 1){ minus += (POW[dp[i]]-1); minus %= MOD; }else{ minus -= (POW[dp[i]]-1); if(minus < 0){ minus += MOD; } } } ans -= minus; if(ans < 0){ ans += MOD; } printf("%lld\n",ans); return 0; } ```
#include<iostream> #include<algorithm> #include<vector> #include<string> #include<set> #include<queue> #include<stack> #include<bitset> #include<functional> #include<map> #include<iomanip> #include<limits> #include<unordered_set> #include<cmath> using namespace std; //long long p = 998244353; long long p = 1000000007; #define int long long #define vel vector<long long> #define vvel vector<vel> #define rep(i,n) for(int i=0;i<n;i++) #define sor(v) sort(v.begin(),v.end()) #define mmax(a,b) a=max(a,b) #define mmin(a,b) a=min(a,b) #define mkp make_pair #define pin pair<int,int> #define qin pair<pin,int> #define V vector #define Endl endl #define veb vector<bool> #define fcout cout << fixed << setprecision(15) #define rev(s) reverse(s.begin(),s.end()) #define lower(h,val) lower_bound(h.begin(),h.end(),val)-h.begin() #define upper(h,val) upper_bound(h.begin(),h.end(),val)-h.begin() int max_kai = 150000; vel kai(max_kai, 1); vel inv_kai; int rui(int a, int n, int mod) { if (n == 0) { return 1 % mod; } int x = rui(a, n / 2, mod); x *= x; x %= mod; if (n % 2 == 1) { x *= a; x %= mod; } return x; } vel pa; int root(int x) { if (pa[x] == -1) { return x; } int ans = root(pa[x]); pa[x] = ans; return ans; } void marge(int x, int y) { x = root(x); y = root(y); if (x != y) { pa[x] = y; } } int gcd(int x, int y) { if (x < y) { return gcd(y, x); } if (y == 0) { return x; } return gcd(y, x % y); } long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } vel uni(vel x) { if (x.size() == 0) { return x; } sor(x); int n = x.size(); vel ans(1, x[0]); for (int j = 1; j < n; j++) { if (x[j - 1] != x[j]) { ans.push_back(x[j]); } } x = ans; return x; } void pr(vel& v) { int n = v.size(); if (n != 0) { cout << v[0]; rep(i, n - 1) { cout << " " << v[i + 1]; } cout << endl; } } int sol(int x, int k) { if (x == 0 || k == 0) { return 0; } return x + sol(x / 2, k - 1); } vel pri(int n) { veb ans(n, true); vel prime; for (int j = 2; j < n; j++) { if (ans[j]) { prime.push_back(j); for (int k = 2; j * k < n; k++) { ans[j * k] = false; } } } return prime; } int solve(int L, int R, vel& prime) {//[L,R) if (L == 0) { L++; } int diff = R - L; vel num(diff); rep(i, diff) { num[i] = L + i; } veb ans(diff, true); for (auto p : prime) { int qL = (L + p - 1) / p; qL *= p; for (int i = qL; i < R; i += p) { int qi = i - L; while (num[qi] % p == 0) { ans[qi] = !ans[qi]; num[qi] /= p; } } } int ret = 0; rep(i, diff) { if (num[i] != 1) { ans[i] = !ans[i]; } if (ans[i]) { ret++; } else { ret--; } } return ret; } vel dp; int m; vel rui2; vel a; void solve(int mod, int st,int to) { if (mod == 0) { return; } solve(mod/2, st,to); int mid1 = st + mod; int mid2 = mid1 + mod; int l = lower_bound(a.begin(), a.end(), mid1) - a.begin(); int r = lower_bound(a.begin(), a.end(), mid2) - a.begin(); dp[mid1] = rui2[r-l]; solve(mod/2,mid1,mid2); } signed main() { int n; cin >> n; a=vel(n); rep(i, n) { cin >> a[i]; } m = (1 << 20); dp=vel(m,0); rep(i, n) { dp[a[i]]++; } rep(j, 20) { rep(i, m) { if ((i & (1 << j)) != 0) { int back = i - (1 << j); dp[back] += dp[i]; } } } rui2 = vel(n + 1, 1); rep(i, n) { rui2[i + 1] = rui2[i] * 2; rui2[i + 1] %= p; } rep(i, n + 1) { rui2[i]--; } int ans = 0; for(int i=1;i<m;i++) { int sum = 0; rep(j, 20) { if ((i & (1 << j)) != 0) { sum++; } } if (sum % 2 == 1) { ans += rui2[dp[i]]; } else { ans += p - rui2[dp[i]]; } ans %= p; } ans = rui2[n] + p - ans; ans %= p; cout << ans << endl; return 0; }
### Prompt Please create a solution in CPP to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include<iostream> #include<algorithm> #include<vector> #include<string> #include<set> #include<queue> #include<stack> #include<bitset> #include<functional> #include<map> #include<iomanip> #include<limits> #include<unordered_set> #include<cmath> using namespace std; //long long p = 998244353; long long p = 1000000007; #define int long long #define vel vector<long long> #define vvel vector<vel> #define rep(i,n) for(int i=0;i<n;i++) #define sor(v) sort(v.begin(),v.end()) #define mmax(a,b) a=max(a,b) #define mmin(a,b) a=min(a,b) #define mkp make_pair #define pin pair<int,int> #define qin pair<pin,int> #define V vector #define Endl endl #define veb vector<bool> #define fcout cout << fixed << setprecision(15) #define rev(s) reverse(s.begin(),s.end()) #define lower(h,val) lower_bound(h.begin(),h.end(),val)-h.begin() #define upper(h,val) upper_bound(h.begin(),h.end(),val)-h.begin() int max_kai = 150000; vel kai(max_kai, 1); vel inv_kai; int rui(int a, int n, int mod) { if (n == 0) { return 1 % mod; } int x = rui(a, n / 2, mod); x *= x; x %= mod; if (n % 2 == 1) { x *= a; x %= mod; } return x; } vel pa; int root(int x) { if (pa[x] == -1) { return x; } int ans = root(pa[x]); pa[x] = ans; return ans; } void marge(int x, int y) { x = root(x); y = root(y); if (x != y) { pa[x] = y; } } int gcd(int x, int y) { if (x < y) { return gcd(y, x); } if (y == 0) { return x; } return gcd(y, x % y); } long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } vel uni(vel x) { if (x.size() == 0) { return x; } sor(x); int n = x.size(); vel ans(1, x[0]); for (int j = 1; j < n; j++) { if (x[j - 1] != x[j]) { ans.push_back(x[j]); } } x = ans; return x; } void pr(vel& v) { int n = v.size(); if (n != 0) { cout << v[0]; rep(i, n - 1) { cout << " " << v[i + 1]; } cout << endl; } } int sol(int x, int k) { if (x == 0 || k == 0) { return 0; } return x + sol(x / 2, k - 1); } vel pri(int n) { veb ans(n, true); vel prime; for (int j = 2; j < n; j++) { if (ans[j]) { prime.push_back(j); for (int k = 2; j * k < n; k++) { ans[j * k] = false; } } } return prime; } int solve(int L, int R, vel& prime) {//[L,R) if (L == 0) { L++; } int diff = R - L; vel num(diff); rep(i, diff) { num[i] = L + i; } veb ans(diff, true); for (auto p : prime) { int qL = (L + p - 1) / p; qL *= p; for (int i = qL; i < R; i += p) { int qi = i - L; while (num[qi] % p == 0) { ans[qi] = !ans[qi]; num[qi] /= p; } } } int ret = 0; rep(i, diff) { if (num[i] != 1) { ans[i] = !ans[i]; } if (ans[i]) { ret++; } else { ret--; } } return ret; } vel dp; int m; vel rui2; vel a; void solve(int mod, int st,int to) { if (mod == 0) { return; } solve(mod/2, st,to); int mid1 = st + mod; int mid2 = mid1 + mod; int l = lower_bound(a.begin(), a.end(), mid1) - a.begin(); int r = lower_bound(a.begin(), a.end(), mid2) - a.begin(); dp[mid1] = rui2[r-l]; solve(mod/2,mid1,mid2); } signed main() { int n; cin >> n; a=vel(n); rep(i, n) { cin >> a[i]; } m = (1 << 20); dp=vel(m,0); rep(i, n) { dp[a[i]]++; } rep(j, 20) { rep(i, m) { if ((i & (1 << j)) != 0) { int back = i - (1 << j); dp[back] += dp[i]; } } } rui2 = vel(n + 1, 1); rep(i, n) { rui2[i + 1] = rui2[i] * 2; rui2[i + 1] %= p; } rep(i, n + 1) { rui2[i]--; } int ans = 0; for(int i=1;i<m;i++) { int sum = 0; rep(j, 20) { if ((i & (1 << j)) != 0) { sum++; } } if (sum % 2 == 1) { ans += rui2[dp[i]]; } else { ans += p - rui2[dp[i]]; } ans %= p; } ans = rui2[n] + p - ans; ans %= p; cout << ans << endl; return 0; } ```
#include <iostream> #include <set> #include <utility> #include <vector> #include <algorithm> #define llint long long #define inf 1e18 #define mod 1000000007 using namespace std; typedef pair<llint, llint> P; llint n; llint a[100005]; llint cnt[1<<20]; llint pop[1<<20]; void zeta_transform(llint a[], int n) { int S = 1<<n; for(int i = 0; i < n; i++){ for(int j = 0; j < S; j++){ if(!(j&(1<<i))) a[j] += a[j^(1<<i)], a[j] %= mod; } } } llint modpow(llint a, llint n) { if(n == 0) return 1; if(n % 2){ return ((a%mod) * (modpow(a, n-1)%mod)) % mod; } else{ return modpow((a*a)%mod, n/2) % mod; } } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> a[i], cnt[a[i]]++; zeta_transform(cnt, 20); for(int i = 0; i < (1<<20); i++) cnt[i] = modpow(2, cnt[i]) + mod - 1, cnt[i] %= mod; for(int i = 1; i < (1<<20); i++) pop[i] = pop[i&(i-1)] + 1; llint ans = 0; for(int i = 0; i < (1<<20); i++){ if(pop[i]%2) ans += mod - cnt[i], ans %= mod; else ans += cnt[i], ans %= mod; } cout << ans << endl; return 0; }
### Prompt In CPP, your task is to solve the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <iostream> #include <set> #include <utility> #include <vector> #include <algorithm> #define llint long long #define inf 1e18 #define mod 1000000007 using namespace std; typedef pair<llint, llint> P; llint n; llint a[100005]; llint cnt[1<<20]; llint pop[1<<20]; void zeta_transform(llint a[], int n) { int S = 1<<n; for(int i = 0; i < n; i++){ for(int j = 0; j < S; j++){ if(!(j&(1<<i))) a[j] += a[j^(1<<i)], a[j] %= mod; } } } llint modpow(llint a, llint n) { if(n == 0) return 1; if(n % 2){ return ((a%mod) * (modpow(a, n-1)%mod)) % mod; } else{ return modpow((a*a)%mod, n/2) % mod; } } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> a[i], cnt[a[i]]++; zeta_transform(cnt, 20); for(int i = 0; i < (1<<20); i++) cnt[i] = modpow(2, cnt[i]) + mod - 1, cnt[i] %= mod; for(int i = 1; i < (1<<20); i++) pop[i] = pop[i&(i-1)] + 1; llint ans = 0; for(int i = 0; i < (1<<20); i++){ if(pop[i]%2) ans += mod - cnt[i], ans %= mod; else ans += cnt[i], ans %= mod; } cout << ans << endl; return 0; } ```
#include <iostream> using namespace std; #pragma warning (disable: 4996) long long mod = 1000000007; long long N, A[1 << 20], power[1 << 20]; long long dp[1 << 20]; int main() { scanf("%lld", &N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); dp[A[i]]++; } for (int i = 0; i < 20; i++) { for (int j = 0; j < (1 << 20); j++) { if ((j & (1 << i)) != 0) dp[j - (1 << i)] += dp[j]; } } power[0] = 1; for (int i = 1; i < (1 << 20); i++) power[i] = (2LL * power[i - 1]) % mod; long long sum = 0; for (int i = 0; i < (1 << 20); i++) { int cnt = 0; for (int j = 0; j < 20; j++) { if ((i / (1 << j)) % 2 == 1) cnt++; } if (cnt % 2 == 0) sum += power[dp[i]]; else sum -= power[dp[i]]; } sum += mod * mod; sum %= mod; cout << sum << endl; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include <iostream> using namespace std; #pragma warning (disable: 4996) long long mod = 1000000007; long long N, A[1 << 20], power[1 << 20]; long long dp[1 << 20]; int main() { scanf("%lld", &N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); dp[A[i]]++; } for (int i = 0; i < 20; i++) { for (int j = 0; j < (1 << 20); j++) { if ((j & (1 << i)) != 0) dp[j - (1 << i)] += dp[j]; } } power[0] = 1; for (int i = 1; i < (1 << 20); i++) power[i] = (2LL * power[i - 1]) % mod; long long sum = 0; for (int i = 0; i < (1 << 20); i++) { int cnt = 0; for (int j = 0; j < 20; j++) { if ((i / (1 << j)) % 2 == 1) cnt++; } if (cnt % 2 == 0) sum += power[dp[i]]; else sum -= power[dp[i]]; } sum += mod * mod; sum %= mod; cout << sum << endl; return 0; } ```
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; ll const mod = 1e9+7; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) ll pow_mod(ll a,ll b) { ll ret; if (b < 0) ret = pow_mod(a,mod+b-1); else if (b == 0) ret = 1; else if (b == 1) ret = a; else { ll c = pow_mod(a,b/2); if (b%2) ret = (c*c)%mod*a%mod; else ret = c*c%mod; } return ret; } int main() { int n; cin >> n; vector<ll> dp1(1<<20,0),dp2(1<<20,0); for (int i = 0;i < n;++i) { int a; cin >> a; dp1[a]++; } for (int i = 0;i < 20;++i) for (int j = 0;j < (1<<20);++j) if (!(j&(1<<i))) (dp1[j] += dp1[j|(1<<i)]) %= mod; for (int i = 0;i < (1<<20);++i) dp2[i] = pow_mod(2,dp1[i])-1; for (int i = 0;i < 20;++i) for (int j = 0;j < (1<<20);++j) if (!(j&(1<<i))) (dp2[j] -= dp2[j|(1<<i)]) %= mod; cout << (dp2[0]+mod)%mod << endl; // p_ary(dp1,0,1<<4); // p_ary(dp2,0,1<<4); }
### Prompt Please create a solution in CPP to the following problem: Zero AND Subsets Given a multiset of nonnegative integers a_1, a_2, .., a_N. How many non-empty subsets of this set have a value bitwiseAND of 0? Find the remainder of the answer divided by 10 ^ 9 + 7. input N a_1 a_2 ... a_N output Divide the answer by 10 ^ 9 + 7 and output the remainder. Constraint * 1 \ leq N \ leq 10 ^ 5 * 0 \ leq a_i \ leq 2 ^ {20} -1 Input example 6 8 6 9 1 2 1 Output example 51 Example Input 6 8 6 9 1 2 1 Output 51 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; ll const mod = 1e9+7; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) ll pow_mod(ll a,ll b) { ll ret; if (b < 0) ret = pow_mod(a,mod+b-1); else if (b == 0) ret = 1; else if (b == 1) ret = a; else { ll c = pow_mod(a,b/2); if (b%2) ret = (c*c)%mod*a%mod; else ret = c*c%mod; } return ret; } int main() { int n; cin >> n; vector<ll> dp1(1<<20,0),dp2(1<<20,0); for (int i = 0;i < n;++i) { int a; cin >> a; dp1[a]++; } for (int i = 0;i < 20;++i) for (int j = 0;j < (1<<20);++j) if (!(j&(1<<i))) (dp1[j] += dp1[j|(1<<i)]) %= mod; for (int i = 0;i < (1<<20);++i) dp2[i] = pow_mod(2,dp1[i])-1; for (int i = 0;i < 20;++i) for (int j = 0;j < (1<<20);++j) if (!(j&(1<<i))) (dp2[j] -= dp2[j|(1<<i)]) %= mod; cout << (dp2[0]+mod)%mod << endl; // p_ary(dp1,0,1<<4); // p_ary(dp2,0,1<<4); } ```
#include <iostream> #define INF (1 << 31) - 1 using namespace std; typedef pair<int,int> P; int n,q,cnt = 1; P dat[300000]; void init(){ int nn = 1; while(nn < n) nn *= 2; n = nn; for(int i = 0;i < n * 2 - 1;i++){ dat[i] = P(INF,0); } } void update(int a,int b,int x,int k,int l,int r){ if(r <= a || b <= l) return; if(a <= l && r <= b){ dat[k] = P(x,cnt); return; } update(a,b,x,k * 2 + 1,l,(l + r) / 2); update(a,b,x,k * 2 + 2,(l + r) / 2,r); } int get(int i){ i = i + n - 1; int last = dat[i].second,val = dat[i].first; while(i){ i = (i - 1) / 2; if(last < dat[i].second){ last = dat[i].second; val = dat[i].first; } } return val; } int main(){ cin >> n >> q; init(); for(int i = 0;i < q;i++){ int v; cin >> v; if(v == 0){ int s,t,x; cin >> s >> t >> x; update(s,t + 1,x,0,0,n); cnt++; } else{ int x; cin >> x; cout << get(x) << endl; } } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> #define INF (1 << 31) - 1 using namespace std; typedef pair<int,int> P; int n,q,cnt = 1; P dat[300000]; void init(){ int nn = 1; while(nn < n) nn *= 2; n = nn; for(int i = 0;i < n * 2 - 1;i++){ dat[i] = P(INF,0); } } void update(int a,int b,int x,int k,int l,int r){ if(r <= a || b <= l) return; if(a <= l && r <= b){ dat[k] = P(x,cnt); return; } update(a,b,x,k * 2 + 1,l,(l + r) / 2); update(a,b,x,k * 2 + 2,(l + r) / 2,r); } int get(int i){ i = i + n - 1; int last = dat[i].second,val = dat[i].first; while(i){ i = (i - 1) / 2; if(last < dat[i].second){ last = dat[i].second; val = dat[i].first; } } return val; } int main(){ cin >> n >> q; init(); for(int i = 0;i < q;i++){ int v; cin >> v; if(v == 0){ int s,t,x; cin >> s >> t >> x; update(s,t + 1,x,0,0,n); cnt++; } else{ int x; cin >> x; cout << get(x) << endl; } } return 0; } ```
#include <stdio.h> const int INF=2147483647; struct segment_tree{ int map[2000010]; void build(int now,int l,int r){ if(l==r){ map[now]=INF; return; } map[now]=-1; int mid=(l+r)/2; build(now*2+1,l,mid); build(now*2+2,mid+1,r); return ; } void change(int n,int now,int L,int R,int l,int r){ if(R<l||L>r)return; if(L>=l&&R<=r){ map[now]=n; return; } if(map[now]!=-1){ map[now*2+1]=map[now*2+2]=map[now]; map[now]=-1; } int mid=(L+R)/2; change(n,now*2+1,L,mid,l,r); change(n,now*2+2,mid+1,R,l,r); return ; } int find(int now,int L,int R,int n){ if(L==R)return map[now]; if(map[now]!=-1){map[now*2+1]=map[now*2+2]=map[now]; int temp=map[now]; map[now]=-1; return temp; } int mid=(L+R)/2; if(mid>=n)return find(now*2+1,L,mid,n); else return find(now*2+2,mid+1,R,n); } }; int main(){ int n,m,k,l,r; segment_tree arr; scanf("%d%d",&n,&m); arr.build(0,0,n-1); while(m--){ scanf("%d",&k); if(k==0){ scanf("%d%d%d",&l,&r,&k); arr.change(k,0,0,n-1,l,r); } else { scanf("%d",&k); printf("%d\n",arr.find(0,0,n-1,k)); } } }
### Prompt Your task is to create a CPP solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <stdio.h> const int INF=2147483647; struct segment_tree{ int map[2000010]; void build(int now,int l,int r){ if(l==r){ map[now]=INF; return; } map[now]=-1; int mid=(l+r)/2; build(now*2+1,l,mid); build(now*2+2,mid+1,r); return ; } void change(int n,int now,int L,int R,int l,int r){ if(R<l||L>r)return; if(L>=l&&R<=r){ map[now]=n; return; } if(map[now]!=-1){ map[now*2+1]=map[now*2+2]=map[now]; map[now]=-1; } int mid=(L+R)/2; change(n,now*2+1,L,mid,l,r); change(n,now*2+2,mid+1,R,l,r); return ; } int find(int now,int L,int R,int n){ if(L==R)return map[now]; if(map[now]!=-1){map[now*2+1]=map[now*2+2]=map[now]; int temp=map[now]; map[now]=-1; return temp; } int mid=(L+R)/2; if(mid>=n)return find(now*2+1,L,mid,n); else return find(now*2+2,mid+1,R,n); } }; int main(){ int n,m,k,l,r; segment_tree arr; scanf("%d%d",&n,&m); arr.build(0,0,n-1); while(m--){ scanf("%d",&k); if(k==0){ scanf("%d%d%d",&l,&r,&k); arr.change(k,0,0,n-1,l,r); } else { scanf("%d",&k); printf("%d\n",arr.find(0,0,n-1,k)); } } } ```
#include<bits/stdc++.h> using namespace std; #define int long long #define INF 3e18 #define rep(i,n) for(int i=0;i<n;i++) #define P pair<int,int> int n,N; P dat[444444]; void init(){ int x=1; while(x<n)x*=2; N=x; rep(i,N*2-1)dat[i]={((int)1<<31)-1,INF}; } void eval(int k,int l,int r){ if(dat[k].second!=INF){ dat[k].first=dat[k].second; if(r-l>1){ dat[2*k+1].second=dat[k].second; dat[2*k+2].second=dat[k].second; } dat[k].second=INF; } } void update(int a,int b,int x,int k,int l,int r){ eval(k,l,r); if(b<=l||r<=a)return; if(a<=l&&r<=b){ dat[k].second=x; eval(k,l,r); } else{ update(a,b,x,2*k+1,l,(l+r)/2); update(a,b,x,2*k+2,(l+r)/2,r); } } int query(int a,int k,int l,int r){ eval(k,l,r); if(r-l==1)return dat[k].first; if(a<(l+r)/2)return query(a,k*2+1,l,(l+r)/2); else return query(a,k*2+2,(l+r)/2,r); } signed main(){ int q; cin>>n>>q; init(); rep(i,q){ int Q; cin>>Q; if(Q==0){ int s,t,x; cin>>s>>t>>x; update(s,t+1,x,0,0,N); } else{ int x; cin>>x; cout<<query(x,0,0,N)<<endl; } } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define int long long #define INF 3e18 #define rep(i,n) for(int i=0;i<n;i++) #define P pair<int,int> int n,N; P dat[444444]; void init(){ int x=1; while(x<n)x*=2; N=x; rep(i,N*2-1)dat[i]={((int)1<<31)-1,INF}; } void eval(int k,int l,int r){ if(dat[k].second!=INF){ dat[k].first=dat[k].second; if(r-l>1){ dat[2*k+1].second=dat[k].second; dat[2*k+2].second=dat[k].second; } dat[k].second=INF; } } void update(int a,int b,int x,int k,int l,int r){ eval(k,l,r); if(b<=l||r<=a)return; if(a<=l&&r<=b){ dat[k].second=x; eval(k,l,r); } else{ update(a,b,x,2*k+1,l,(l+r)/2); update(a,b,x,2*k+2,(l+r)/2,r); } } int query(int a,int k,int l,int r){ eval(k,l,r); if(r-l==1)return dat[k].first; if(a<(l+r)/2)return query(a,k*2+1,l,(l+r)/2); else return query(a,k*2+2,(l+r)/2,r); } signed main(){ int q; cin>>n>>q; init(); rep(i,q){ int Q; cin>>Q; if(Q==0){ int s,t,x; cin>>s>>t>>x; update(s,t+1,x,0,0,N); } else{ int x; cin>>x; cout<<query(x,0,0,N)<<endl; } } return 0; } ```
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> #define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i)) #define rep(i,n) FOR(i,0,n) #define pb push_back #define all(v) begin(v), end(v) #define debug(x) cerr<< #x <<": "<<x<<endl #define debug2(x,y) cerr<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vector<int> > vvi; typedef vector<ll> vll; typedef vector<vector<ll> > vvll; template<class T> using vv=vector<vector< T > >; #define INF INT_MAX int n; vi seg; vi lazy; void update(int index, int x) { seg[n + index] += x; for (int i = n + index; i >= 2; i /= 2) { seg[i/2] = seg[i] + seg[i^1]; } } void propagate(int k) { if (lazy[k] == INF) { return; } seg[k] = lazy[k]; if (k < n) { lazy[k*2] = lazy[k]; lazy[k*2+1] = lazy[k]; } lazy[k] = INT_MAX; } // seg[k] : [l, r) void range_update(int a, int b, int k, int l, int r, int x) { if (r <= a || b <= l) { return; } if (a <= l && r <= b) { lazy[k] = x; propagate(k); return; } else { propagate(k); range_update(a, b, k*2, l, (l+r)/2, x); range_update(a, b, k*2+1, (l+r)/2, r, x); } } int range_get(int x, int k, int l, int r) { propagate(k); if (x < l || r <= x) { return INF; } if (l == x && r == x+1) { return seg[k]; } else { return min(range_get(x, k*2, l, (l+r)/2), range_get(x, k*2+1, (l+r)/2, r)); } } int get_query(int x) { return range_get(x, 1, 0, n); } int main() { int n_, q; scanf("%d %d", &n_, &q); n = (1 << (int)(ceil(log2(n_)))); seg.assign(2*n, INT_MAX); lazy.assign(2*n, INT_MAX); int op, s, t, x; rep (i, q) { scanf("%d", &op); if (op == 0) { scanf("%d %d %d", &s, &t, &x); t += 1; range_update(s, t, 1, 0, n, x); } else { scanf("%d", &x); printf("%d\n", get_query(x)); } } return 0; }
### Prompt Generate a CPP solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> #define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i)) #define rep(i,n) FOR(i,0,n) #define pb push_back #define all(v) begin(v), end(v) #define debug(x) cerr<< #x <<": "<<x<<endl #define debug2(x,y) cerr<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vector<int> > vvi; typedef vector<ll> vll; typedef vector<vector<ll> > vvll; template<class T> using vv=vector<vector< T > >; #define INF INT_MAX int n; vi seg; vi lazy; void update(int index, int x) { seg[n + index] += x; for (int i = n + index; i >= 2; i /= 2) { seg[i/2] = seg[i] + seg[i^1]; } } void propagate(int k) { if (lazy[k] == INF) { return; } seg[k] = lazy[k]; if (k < n) { lazy[k*2] = lazy[k]; lazy[k*2+1] = lazy[k]; } lazy[k] = INT_MAX; } // seg[k] : [l, r) void range_update(int a, int b, int k, int l, int r, int x) { if (r <= a || b <= l) { return; } if (a <= l && r <= b) { lazy[k] = x; propagate(k); return; } else { propagate(k); range_update(a, b, k*2, l, (l+r)/2, x); range_update(a, b, k*2+1, (l+r)/2, r, x); } } int range_get(int x, int k, int l, int r) { propagate(k); if (x < l || r <= x) { return INF; } if (l == x && r == x+1) { return seg[k]; } else { return min(range_get(x, k*2, l, (l+r)/2), range_get(x, k*2+1, (l+r)/2, r)); } } int get_query(int x) { return range_get(x, 1, 0, n); } int main() { int n_, q; scanf("%d %d", &n_, &q); n = (1 << (int)(ceil(log2(n_)))); seg.assign(2*n, INT_MAX); lazy.assign(2*n, INT_MAX); int op, s, t, x; rep (i, q) { scanf("%d", &op); if (op == 0) { scanf("%d %d %d", &s, &t, &x); t += 1; range_update(s, t, 1, 0, n, x); } else { scanf("%d", &x); printf("%d\n", get_query(x)); } } return 0; } ```
#include <iostream> #include <algorithm> using namespace std; typedef long long ll; ll seg[1<<19],n,q; void add(ll a,ll b,ll r,ll l,ll k,ll x){ if(a<=r&&l<=b){ seg[k]=x; return; } if(l<a||b<r)return; if(seg[k]>=0){ seg[k*2+1]=seg[k]; seg[k*2+2]=seg[k]; seg[k]=-1; } add(a,b,r,(r+l-1)/2,k*2+1,x); add(a,b,(r+l+1)/2,l,k*2+2,x); } ll find(ll x,ll r,ll l,ll k){ if(seg[k]>=0)return seg[k]; if((r+l-1)/2>=x)return find(x,r,(r+l-1)/2,k*2+1); else return find(x,(r+l+1)/2,l,k*2+2); } int main(void){ cin>>n>>q; for(ll i=0;i<(1<<19)-1;i++)seg[i]=-1; seg[0]=2147483647; for(ll i=0;i<q;i++){ ll a; cin>>a; if(a){ ll x; cin>>x; cout<<find(x,0,(1<<18)-1,0)<<endl; }else{ ll s,t,x; cin>>s>>t>>x; add(s,t,0,(1<<18)-1,0,x); } } }
### Prompt Please create a solution in Cpp to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> #include <algorithm> using namespace std; typedef long long ll; ll seg[1<<19],n,q; void add(ll a,ll b,ll r,ll l,ll k,ll x){ if(a<=r&&l<=b){ seg[k]=x; return; } if(l<a||b<r)return; if(seg[k]>=0){ seg[k*2+1]=seg[k]; seg[k*2+2]=seg[k]; seg[k]=-1; } add(a,b,r,(r+l-1)/2,k*2+1,x); add(a,b,(r+l+1)/2,l,k*2+2,x); } ll find(ll x,ll r,ll l,ll k){ if(seg[k]>=0)return seg[k]; if((r+l-1)/2>=x)return find(x,r,(r+l-1)/2,k*2+1); else return find(x,(r+l+1)/2,l,k*2+2); } int main(void){ cin>>n>>q; for(ll i=0;i<(1<<19)-1;i++)seg[i]=-1; seg[0]=2147483647; for(ll i=0;i<q;i++){ ll a; cin>>a; if(a){ ll x; cin>>x; cout<<find(x,0,(1<<18)-1,0)<<endl; }else{ ll s,t,x; cin>>s>>t>>x; add(s,t,0,(1<<18)-1,0,x); } } } ```
#include<iostream> using namespace std; #include<vector> #include<functional> template<typename T> struct lazysegtree{ function<T(T,T)>calcfn,lazycalcfn; function<T(T,T,unsigned int)>updatefn; int n; T defvalue,lazydefvalue; vector<T>dat,lazy; vector<bool>lazyflag; lazysegtree(int n_=0,T defvalue_=0, function<T(T,T)>calcfn_=[](T a,T b){return a+b;}, function<T(T,T)>lazycalcfn_=[](T a,T b){return a+b;}, function<T(T,T,unsigned int)>updatefn_=[](T a,T b,unsigned int width){return a+b*width;}, T lazydefvalue_=0 ):defvalue(defvalue_),lazydefvalue(lazydefvalue_), calcfn(calcfn_),lazycalcfn(lazycalcfn_),updatefn(updatefn_) { n=1; while(n<n_)n<<=1; dat.assign(2*n-1,defvalue); lazy.assign(2*n-1,lazydefvalue); lazyflag.assign(2*n-1,false); } void copy(vector<T>v) { for(int i=0;i<v.size();i++)dat[i+n-1]=v[i]; for(int i=n-2;i>=0;i--)dat[i]=calcfn(dat[2*i+1],dat[2*i+2]); } void eval(int i,int l,int r) { if(lazyflag[i]) { dat[i]=updatefn(dat[i],lazy[i],r-l); if(r-l>1) { lazy[2*i+1]=lazycalcfn(lazy[2*i+1],lazy[i]); lazy[2*i+2]=lazycalcfn(lazy[2*i+2],lazy[i]); lazyflag[2*i+1]=lazyflag[2*i+2]=true; } lazy[i]=lazydefvalue; lazyflag[i]=false; } } void update(int a,int b,T x,int k=0,int l=0,int r=-1) { if(r<0)r=n; eval(k,l,r); if(b<=l||r<=a)return; else if(a<=l&&r<=b) { lazy[k]=lazycalcfn(lazy[k],x); lazyflag[k]=true; eval(k,l,r); } else { update(a,b,x,2*k+1,l,(l+r)/2); update(a,b,x,2*k+2,(l+r)/2,r); dat[k]=calcfn(dat[2*k+1],dat[2*k+2]); } } T query(int a,int b,int k=0,int l=0,int r=-1) { if(r<0)r=n; eval(k,l,r); if(b<=l||r<=a)return defvalue; else if(a<=l&&r<=b)return dat[k]; else return calcfn( query(a,b,2*k+1,l,(l+r)/2), query(a,b,2*k+2,(l+r)/2,r) ); } }; int main() { int n,q;cin>>n>>q; lazysegtree<int>S(n,2147483647,[](int a,int b){return a==2147483647?b:a;},[](int a,int b){return b;},[](int a,int b,unsigned int c){return b;}); for(int i=0;i<q;i++) { int c;cin>>c; if(c) { int s;cin>>s; cout<<S.query(s,s+1)<<endl; } else { int s,t,x;cin>>s>>t>>x; S.update(s,t+1,x); } } }
### Prompt Generate a Cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<iostream> using namespace std; #include<vector> #include<functional> template<typename T> struct lazysegtree{ function<T(T,T)>calcfn,lazycalcfn; function<T(T,T,unsigned int)>updatefn; int n; T defvalue,lazydefvalue; vector<T>dat,lazy; vector<bool>lazyflag; lazysegtree(int n_=0,T defvalue_=0, function<T(T,T)>calcfn_=[](T a,T b){return a+b;}, function<T(T,T)>lazycalcfn_=[](T a,T b){return a+b;}, function<T(T,T,unsigned int)>updatefn_=[](T a,T b,unsigned int width){return a+b*width;}, T lazydefvalue_=0 ):defvalue(defvalue_),lazydefvalue(lazydefvalue_), calcfn(calcfn_),lazycalcfn(lazycalcfn_),updatefn(updatefn_) { n=1; while(n<n_)n<<=1; dat.assign(2*n-1,defvalue); lazy.assign(2*n-1,lazydefvalue); lazyflag.assign(2*n-1,false); } void copy(vector<T>v) { for(int i=0;i<v.size();i++)dat[i+n-1]=v[i]; for(int i=n-2;i>=0;i--)dat[i]=calcfn(dat[2*i+1],dat[2*i+2]); } void eval(int i,int l,int r) { if(lazyflag[i]) { dat[i]=updatefn(dat[i],lazy[i],r-l); if(r-l>1) { lazy[2*i+1]=lazycalcfn(lazy[2*i+1],lazy[i]); lazy[2*i+2]=lazycalcfn(lazy[2*i+2],lazy[i]); lazyflag[2*i+1]=lazyflag[2*i+2]=true; } lazy[i]=lazydefvalue; lazyflag[i]=false; } } void update(int a,int b,T x,int k=0,int l=0,int r=-1) { if(r<0)r=n; eval(k,l,r); if(b<=l||r<=a)return; else if(a<=l&&r<=b) { lazy[k]=lazycalcfn(lazy[k],x); lazyflag[k]=true; eval(k,l,r); } else { update(a,b,x,2*k+1,l,(l+r)/2); update(a,b,x,2*k+2,(l+r)/2,r); dat[k]=calcfn(dat[2*k+1],dat[2*k+2]); } } T query(int a,int b,int k=0,int l=0,int r=-1) { if(r<0)r=n; eval(k,l,r); if(b<=l||r<=a)return defvalue; else if(a<=l&&r<=b)return dat[k]; else return calcfn( query(a,b,2*k+1,l,(l+r)/2), query(a,b,2*k+2,(l+r)/2,r) ); } }; int main() { int n,q;cin>>n>>q; lazysegtree<int>S(n,2147483647,[](int a,int b){return a==2147483647?b:a;},[](int a,int b){return b;},[](int a,int b,unsigned int c){return b;}); for(int i=0;i<q;i++) { int c;cin>>c; if(c) { int s;cin>>s; cout<<S.query(s,s+1)<<endl; } else { int s,t,x;cin>>s>>t>>x; S.update(s,t+1,x); } } } ```
#include<iostream> #include<algorithm> #include<vector> using namespace std; typedef long long ll; #define rep(i,n) for(int i=0;i<n;i++) #define chmax(a,b) a=max(a,b); #define chmin(a,b) a=min(a,b); class Segtree{ private: int V; typedef struct st{ ll a; }st; st st0=(st){(ll)1e18}; typedef struct op{ ll a; }op; op op0=(op){-1}; st f(st p,st q){ return (st){min(p.a,q.a)}; } void g(st &p,op q){ if(q.a==-1)return; p.a=q.a; } void h(op &p,op q){ if(q.a==-1)return; p.a=q.a; } st *dat; op *laz; st propagate(ll k){ if(k<V){ h(laz[k*2],laz[k]); h(laz[k*2+1],laz[k]); } g(dat[k],laz[k]); laz[k]=op0; return dat[k]; } void upd(ll l,ll r,op p){ l+=V,r+=V+1; for(ll d=20;d>=0;d--){ propagate(l>>d); propagate(r>>d); } for(ll a=l,b=r;a<b;a>>=1,b>>=1){ if(a&1){h(laz[a],p);a++;} if(b&1){b--;h(laz[b],p);} } for(ll a=l,b=r;a+b;a>>=1,b>>=1){ dat[a/2]=f(propagate(a/2*2),propagate(a/2*2+1)); dat[b/2]=f(propagate(b/2*2),propagate(b/2*2+1)); } } st qry(ll l,ll r){ l+=V,r+=V+1; for(ll d=20;d>=0;d--){ propagate(l>>d); propagate(r>>d); } st L=st0,R=st0; for(ll a=l,b=r;a<b;a>>=1,b>>=1){ if(a&1){L=f(L,propagate(a));a++;} if(b&1){b--;R=f(propagate(b),R);} } st res=f(L,R); return res; } public: void init(int ns){ V=1; while(V<=ns)V*=2; dat=new st[2*V]; laz=new op[2*V]; for(int i=0;i<2*V;i++){ dat[i]=st0; laz[i]=op0; } } void update(ll l,ll r,ll a){ upd(l,r,(op){a}); } ll query(ll l,ll r){ st res=qry(l,r); return res.a; } }; Segtree seg; int main(){ int n,q; cin>>n>>q; seg.init(n); rep(i,n)seg.update(i,i,2147483647); while(q--){ ll p,s,t,x; cin>>p; if(p==0){ cin>>s>>t>>x; seg.update(s,t,x); } if(p==1){ cin>>s; cout<<seg.query(s,s)<<"\n"; } } }
### Prompt Please formulate a Cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<iostream> #include<algorithm> #include<vector> using namespace std; typedef long long ll; #define rep(i,n) for(int i=0;i<n;i++) #define chmax(a,b) a=max(a,b); #define chmin(a,b) a=min(a,b); class Segtree{ private: int V; typedef struct st{ ll a; }st; st st0=(st){(ll)1e18}; typedef struct op{ ll a; }op; op op0=(op){-1}; st f(st p,st q){ return (st){min(p.a,q.a)}; } void g(st &p,op q){ if(q.a==-1)return; p.a=q.a; } void h(op &p,op q){ if(q.a==-1)return; p.a=q.a; } st *dat; op *laz; st propagate(ll k){ if(k<V){ h(laz[k*2],laz[k]); h(laz[k*2+1],laz[k]); } g(dat[k],laz[k]); laz[k]=op0; return dat[k]; } void upd(ll l,ll r,op p){ l+=V,r+=V+1; for(ll d=20;d>=0;d--){ propagate(l>>d); propagate(r>>d); } for(ll a=l,b=r;a<b;a>>=1,b>>=1){ if(a&1){h(laz[a],p);a++;} if(b&1){b--;h(laz[b],p);} } for(ll a=l,b=r;a+b;a>>=1,b>>=1){ dat[a/2]=f(propagate(a/2*2),propagate(a/2*2+1)); dat[b/2]=f(propagate(b/2*2),propagate(b/2*2+1)); } } st qry(ll l,ll r){ l+=V,r+=V+1; for(ll d=20;d>=0;d--){ propagate(l>>d); propagate(r>>d); } st L=st0,R=st0; for(ll a=l,b=r;a<b;a>>=1,b>>=1){ if(a&1){L=f(L,propagate(a));a++;} if(b&1){b--;R=f(propagate(b),R);} } st res=f(L,R); return res; } public: void init(int ns){ V=1; while(V<=ns)V*=2; dat=new st[2*V]; laz=new op[2*V]; for(int i=0;i<2*V;i++){ dat[i]=st0; laz[i]=op0; } } void update(ll l,ll r,ll a){ upd(l,r,(op){a}); } ll query(ll l,ll r){ st res=qry(l,r); return res.a; } }; Segtree seg; int main(){ int n,q; cin>>n>>q; seg.init(n); rep(i,n)seg.update(i,i,2147483647); while(q--){ ll p,s,t,x; cin>>p; if(p==0){ cin>>s>>t>>x; seg.update(s,t,x); } if(p==1){ cin>>s; cout<<seg.query(s,s)<<"\n"; } } } ```
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using namespace std; using ll = int64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template<class T> using PQ = priority_queue<T>; template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >; const int INF = 100010001; const ll LINF = (ll)INF*INF*10; template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;} template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;} int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vi a(n, INT_MAX); int m = sqrt(n); vi b((n+m-1)/m, -1); auto call = [&](int i) { for(int x = i/m*m; x != (i/m+1)*m and x != n; x++) { a[x] = b[i/m]; } b[i/m] = -1; }; auto get = [&](int i) { if(b[i/m] != -1) { call(i); } return a[i]; }; auto update = [&](int l, int r, int x) { if(l/m == r/m) { if(b[l/m] != -1) call(l); for(; l != r; l++) { a[l] = x; } } else { if(b[l/m] != -1) call(l); if(b[r/m] != -1) call(r); for(; l%m; a[l++] = x); for(; r%m; a[--r] = x); for(l /= m, r /= m; l != r; b[l++] = x); } }; while(q--) { int t; cin >> t; if(t) { int i; cin >> i; cout << get(i) << '\n'; } else { int l, r, x; cin >> l >> r >> x; update(l, r+1, x); } } }
### Prompt Your task is to create a cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp //#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using namespace std; using ll = int64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template<class T> using PQ = priority_queue<T>; template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >; const int INF = 100010001; const ll LINF = (ll)INF*INF*10; template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;} template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;} int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vi a(n, INT_MAX); int m = sqrt(n); vi b((n+m-1)/m, -1); auto call = [&](int i) { for(int x = i/m*m; x != (i/m+1)*m and x != n; x++) { a[x] = b[i/m]; } b[i/m] = -1; }; auto get = [&](int i) { if(b[i/m] != -1) { call(i); } return a[i]; }; auto update = [&](int l, int r, int x) { if(l/m == r/m) { if(b[l/m] != -1) call(l); for(; l != r; l++) { a[l] = x; } } else { if(b[l/m] != -1) call(l); if(b[r/m] != -1) call(r); for(; l%m; a[l++] = x); for(; r%m; a[--r] = x); for(l /= m, r /= m; l != r; b[l++] = x); } }; while(q--) { int t; cin >> t; if(t) { int i; cin >> i; cout << get(i) << '\n'; } else { int l, r, x; cin >> l >> r >> x; update(l, r+1, x); } } } ```
#include <bits/stdc++.h> using namespace std; #define FOR(I,X,Y) for(long long (I)=(X);(I)<(Y);(I)++) #define REP(I,X,Y) for(long long (I)=(Y)-1;(I)>=(X);(I)--) #define ALL(X) (X).begin(),(X).end() #define pb push_back #define COUNT(V,X) upper_bound((V).begin(),(V).end(),X)-lower_bound((V).begin(),(V).end(),X) #define debug(x) cerr<<#x<<':'<<x<<endl; #define DEBUG(v) cerr<<#v<<':';for(auto x:v)cerr<<x<<' ';cerr<<endl; #define INF 1000000007 #define LINF 1000000000000000007 #define EPS 1e-9 typedef long long ll; struct RangeUpdateQuery{ vector<long long> data; long long sq; vector<long long> lazy; void boot(long long n){ data.resize(n); sq = round(sqrt((double)n)); lazy.resize(n/sq+1); for(long long i = 0;i < lazy.size();i++)lazy[i] = -1000000000000000007; } void update(long long s,long long t,long long num){ if(lazy[s/sq] != -1000000000000000007){ for(long long i = s/sq*sq;i < min((s/sq+1)*sq,(long long)data.size());i++)data[i] = lazy[s/sq]; lazy[s/sq] = -1000000000000000007; } if(lazy[t/sq] != -1000000000000000007){ for(long long i = t/sq*sq;i < min((t/sq+1)*sq,(long long)data.size());i++)data[i] = lazy[t/sq]; lazy[t/sq] = -1000000000000000007; } while(s%sq && s<=t){ data[s] = num; s++; } while((t-s+1)/sq){ lazy[s/sq] = num; s += sq; } while(s <= t){ data[s] = num; s++; } return; } long long find(long long s){ if(lazy[s/sq] != -1000000000000000007){ for(long long i = s/sq*sq;i < min((s/sq+1)*sq,(long long)data.size());i++)data[i] = lazy[s/sq]; lazy[s/sq] = -1000000000000000007; } return data[s]; } }; signed main(){ RangeUpdateQuery RUQ; int n,q; cin >> n >> q; int query,s,t,x; RUQ.boot(n); RUQ.update(0,n-1,(ll)2147483647); vector<long long> ans; FOR(i,0,q){ cin >> query; if(query == 0){ cin >> s >> t >> x; RUQ.update(s,t,x); } else{ cin >> s; ans.pb(RUQ.find(s)); } } for(auto x:ans)cout << x << endl; }
### Prompt Generate a cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define FOR(I,X,Y) for(long long (I)=(X);(I)<(Y);(I)++) #define REP(I,X,Y) for(long long (I)=(Y)-1;(I)>=(X);(I)--) #define ALL(X) (X).begin(),(X).end() #define pb push_back #define COUNT(V,X) upper_bound((V).begin(),(V).end(),X)-lower_bound((V).begin(),(V).end(),X) #define debug(x) cerr<<#x<<':'<<x<<endl; #define DEBUG(v) cerr<<#v<<':';for(auto x:v)cerr<<x<<' ';cerr<<endl; #define INF 1000000007 #define LINF 1000000000000000007 #define EPS 1e-9 typedef long long ll; struct RangeUpdateQuery{ vector<long long> data; long long sq; vector<long long> lazy; void boot(long long n){ data.resize(n); sq = round(sqrt((double)n)); lazy.resize(n/sq+1); for(long long i = 0;i < lazy.size();i++)lazy[i] = -1000000000000000007; } void update(long long s,long long t,long long num){ if(lazy[s/sq] != -1000000000000000007){ for(long long i = s/sq*sq;i < min((s/sq+1)*sq,(long long)data.size());i++)data[i] = lazy[s/sq]; lazy[s/sq] = -1000000000000000007; } if(lazy[t/sq] != -1000000000000000007){ for(long long i = t/sq*sq;i < min((t/sq+1)*sq,(long long)data.size());i++)data[i] = lazy[t/sq]; lazy[t/sq] = -1000000000000000007; } while(s%sq && s<=t){ data[s] = num; s++; } while((t-s+1)/sq){ lazy[s/sq] = num; s += sq; } while(s <= t){ data[s] = num; s++; } return; } long long find(long long s){ if(lazy[s/sq] != -1000000000000000007){ for(long long i = s/sq*sq;i < min((s/sq+1)*sq,(long long)data.size());i++)data[i] = lazy[s/sq]; lazy[s/sq] = -1000000000000000007; } return data[s]; } }; signed main(){ RangeUpdateQuery RUQ; int n,q; cin >> n >> q; int query,s,t,x; RUQ.boot(n); RUQ.update(0,n-1,(ll)2147483647); vector<long long> ans; FOR(i,0,q){ cin >> query; if(query == 0){ cin >> s >> t >> x; RUQ.update(s,t,x); } else{ cin >> s; ans.pb(RUQ.find(s)); } } for(auto x:ans)cout << x << endl; } ```
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define For(i, a, b) for(int (i)=(a); (i)<(b); ++(i)) #define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, int> pli; typedef pair<lint, lint> pll; typedef complex<double> xy_t; template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;} template<class T>bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;} constexpr lint mod = 1e9+7; constexpr lint INF = mod*mod; constexpr int MAX = 200010; template<typename T> struct SegTree_dual{ using F=function<T(T, T)>; int sz=1; T et; F f, g; vector<T> node; SegTree_dual(int sz_, T et_, F f_, F g_): et(et_), f(f_), g(g_){ while(sz<sz_) sz<<=1; node.resize(sz<<1, et); } void update(int l, int r, T x){ for(l+=sz, r+=sz; l<r; l>>=1, r>>=1){ if(l&1){ node[l]=g(node[l], x); l++; } if(r&1){ --r; node[r]=g(node[r], x); } } } T query(int i){ i+=sz; T val=node[i]; i>>=1; while(i){ val=f(val, node[i]); i>>=1; } return val; } }; int main(){ int n, q; scanf("%d%d", &n, &q); auto f=[](pii a, pii b){return max(a, b);}; auto g=[](pii a, pii b){return b;}; SegTree_dual<pii> st(n, {-1, INT_MAX}, f, g); rep(time, q){ int c; scanf("%d", &c); if(c==0){ int s, t, x; scanf("%d%d%d", &s, &t, &x); st.update(s, t+1, {time, x}); } else{ int i; scanf("%d", &i); printf("%d\n", st.query(i).se); } } }
### Prompt Your challenge is to write a Cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define For(i, a, b) for(int (i)=(a); (i)<(b); ++(i)) #define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, int> pli; typedef pair<lint, lint> pll; typedef complex<double> xy_t; template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;} template<class T>bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;} constexpr lint mod = 1e9+7; constexpr lint INF = mod*mod; constexpr int MAX = 200010; template<typename T> struct SegTree_dual{ using F=function<T(T, T)>; int sz=1; T et; F f, g; vector<T> node; SegTree_dual(int sz_, T et_, F f_, F g_): et(et_), f(f_), g(g_){ while(sz<sz_) sz<<=1; node.resize(sz<<1, et); } void update(int l, int r, T x){ for(l+=sz, r+=sz; l<r; l>>=1, r>>=1){ if(l&1){ node[l]=g(node[l], x); l++; } if(r&1){ --r; node[r]=g(node[r], x); } } } T query(int i){ i+=sz; T val=node[i]; i>>=1; while(i){ val=f(val, node[i]); i>>=1; } return val; } }; int main(){ int n, q; scanf("%d%d", &n, &q); auto f=[](pii a, pii b){return max(a, b);}; auto g=[](pii a, pii b){return b;}; SegTree_dual<pii> st(n, {-1, INT_MAX}, f, g); rep(time, q){ int c; scanf("%d", &c); if(c==0){ int s, t, x; scanf("%d%d%d", &s, &t, &x); st.update(s, t+1, {time, x}); } else{ int i; scanf("%d", &i); printf("%d\n", st.query(i).se); } } } ```
#include <iostream> #include <iomanip> #include <string> #include <vector> #include <algorithm> #include <numeric> #include <map> #include <set> #include <cstdio> #include <cstring> #include <cmath> #include <bitset> #include <climits> #define REP(i,n) for (int i=0;i<(n);i++) #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--) #define ll long long #define ull unsigned long long int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const int INF = 1e9; const int MOD = 1e9 + 7; using namespace std; const int MAX = (1LL << 31) - 1LL; const int MAX_N = (1 << 17)+1; struct RUQ{ int n; int datx[MAX_N*2 - 1]; int datc[MAX_N*2 - 1]; //n?????????????????¨?????¨???????????? void init(int n_){ n = 1; while(n<n_)n*=2;//n????????????????????§????????¨update????????°??? REP(i,2*n-1)datx[i] = MAX; REP(i,2*n-1)datc[i] = -1; } //[a,b)?????????x????????´(c) void update(int a,int b,int x,int c,int k,int l,int r){ if(r <= a || b <= l)return; if(a <= l && b >= r){ datx[k] = x; datc[k] = c; }else{ update(a,b,x,c,k*2+1,l,(l+r)/2); update(a,b,x,c,k*2+2,(l+r)/2,r); } } //a[i]????????? int query(int i){ i += n-1; int c = -1; int x = MAX; while(true){ if(datc[i] > c){ x = datx[i]; c = datc[i]; } if(i == 0)break;//?????????i??¨i==0????§??????????????????? i = (i-1)/2; } return x; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n,q; cin >> n >> q; RUQ ruq;ruq.init(n); REP(i,q){ int q; cin >> q; if(q){ int j;cin >>j; cout << ruq.query(j) << endl; }else{ int a,b,x; cin >> a>>b>>x; b++; ruq.update(a,b,x,i,0,0,ruq.n); } } return 0; }
### Prompt Generate a cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> #include <iomanip> #include <string> #include <vector> #include <algorithm> #include <numeric> #include <map> #include <set> #include <cstdio> #include <cstring> #include <cmath> #include <bitset> #include <climits> #define REP(i,n) for (int i=0;i<(n);i++) #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--) #define ll long long #define ull unsigned long long int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const int INF = 1e9; const int MOD = 1e9 + 7; using namespace std; const int MAX = (1LL << 31) - 1LL; const int MAX_N = (1 << 17)+1; struct RUQ{ int n; int datx[MAX_N*2 - 1]; int datc[MAX_N*2 - 1]; //n?????????????????¨?????¨???????????? void init(int n_){ n = 1; while(n<n_)n*=2;//n????????????????????§????????¨update????????°??? REP(i,2*n-1)datx[i] = MAX; REP(i,2*n-1)datc[i] = -1; } //[a,b)?????????x????????´(c) void update(int a,int b,int x,int c,int k,int l,int r){ if(r <= a || b <= l)return; if(a <= l && b >= r){ datx[k] = x; datc[k] = c; }else{ update(a,b,x,c,k*2+1,l,(l+r)/2); update(a,b,x,c,k*2+2,(l+r)/2,r); } } //a[i]????????? int query(int i){ i += n-1; int c = -1; int x = MAX; while(true){ if(datc[i] > c){ x = datx[i]; c = datc[i]; } if(i == 0)break;//?????????i??¨i==0????§??????????????????? i = (i-1)/2; } return x; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n,q; cin >> n >> q; RUQ ruq;ruq.init(n); REP(i,q){ int q; cin >> q; if(q){ int j;cin >>j; cout << ruq.query(j) << endl; }else{ int a,b,x; cin >> a>>b>>x; b++; ruq.update(a,b,x,i,0,0,ruq.n); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct RLUQ{ struct query{ int type;//0=empty, 1=set int value; query(int a=0,int b=0):type(a),value(b) {} }; query s[(1<<18)]; int t[(1<<18)]; RLUQ(){ fill(t,t+(1<<18),INT_MAX); } void compute(int k,int l,int r){ query q=s[k]; s[k].type=0; if(q.type==0||r-l==1)return; s[k*2+1]=s[k*2+2]=q; t[k*2+1]=t[k*2+2]=q.value; } void Set(int a,int b,int x,int k=0,int l=0,int r=(1<<17)){ if(b<=l || r<=a)return; compute(k,l,r); if(a<=l && r<=b){ s[k]=query(1,x); t[k]=x; }else{ int m=(l+r)/2; Set(a,b,x,k*2+1,l,m); Set(a,b,x,k*2+2,m,r); t[k]=min(t[k*2+1],t[k*2+2]); } } int Get(int a,int b,int k=0,int l=0,int r=(1<<17)){ if(b<=l || r<=a)return INT_MAX; compute(k,l,r); if(a<=l && r<=b){ return t[k]; }else{ int m=(l+r)/2; int lv=Get(a,b,k*2+1,l,m); int rv=Get(a,b,k*2+2,m,r); return min(lv,rv); } } }; int main(){ RLUQ a; int n,q; cin>>n>>q; int c,s,t,x; for(int i=0;i<q;i++){ cin>>c; if(c){ cin>>s; cout<<a.Get(s,s+1)<<endl; } else{ cin>>s>>t>>x; a.Set(s,t+1,x); } } }
### Prompt Your task is to create a CPP solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct RLUQ{ struct query{ int type;//0=empty, 1=set int value; query(int a=0,int b=0):type(a),value(b) {} }; query s[(1<<18)]; int t[(1<<18)]; RLUQ(){ fill(t,t+(1<<18),INT_MAX); } void compute(int k,int l,int r){ query q=s[k]; s[k].type=0; if(q.type==0||r-l==1)return; s[k*2+1]=s[k*2+2]=q; t[k*2+1]=t[k*2+2]=q.value; } void Set(int a,int b,int x,int k=0,int l=0,int r=(1<<17)){ if(b<=l || r<=a)return; compute(k,l,r); if(a<=l && r<=b){ s[k]=query(1,x); t[k]=x; }else{ int m=(l+r)/2; Set(a,b,x,k*2+1,l,m); Set(a,b,x,k*2+2,m,r); t[k]=min(t[k*2+1],t[k*2+2]); } } int Get(int a,int b,int k=0,int l=0,int r=(1<<17)){ if(b<=l || r<=a)return INT_MAX; compute(k,l,r); if(a<=l && r<=b){ return t[k]; }else{ int m=(l+r)/2; int lv=Get(a,b,k*2+1,l,m); int rv=Get(a,b,k*2+2,m,r); return min(lv,rv); } } }; int main(){ RLUQ a; int n,q; cin>>n>>q; int c,s,t,x; for(int i=0;i<q;i++){ cin>>c; if(c){ cin>>s; cout<<a.Get(s,s+1)<<endl; } else{ cin>>s>>t>>x; a.Set(s,t+1,x); } } } ```
#include <iostream> #include <iomanip> // << fixed << setprecision(xxx) #include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ; #include <vector> #include <string> // to_string(nnn) // substr(m, n) // stoi(nnn) #include <complex> #include <tuple> // get<n>(xxx) #include <queue> #include <stack> #include <map> // if (M.find(key) != M.end()) { } #include <set> // S.insert(M); // if (S.find(key) != S.end()) { } // for (auto it=S.begin(); it != S.end(); it++) { } // auto it = S.lower_bound(M); #include <random> // random_device rd; mt19937 mt(rd()); #include <cctype> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> // atoi(xxx) using namespace std; #define DEBUG 0 // change 0 -> 1 if we need debug. // insert #if<tab> by my emacs. #if DEBUG == 1 ... #end typedef long long ll; // const int dx[4] = {1, 0, -1, 0}; // const int dy[4] = {0, 1, 0, -1}; // const int C = 1e6+10; // const ll M = 1000000007; class SegTree { // index starts at 0. public: int N; int* dat; SegTree(int n) { N = 1; while (N < n) N *= 2; dat = new int[2 * N - 1]; for (auto i = 0; i < 2 * N - 1; ++i) { dat[i] = -1; } } ~SegTree() { delete[] dat; } int find(int k) { k += N - 1; int ans = dat[k]; while (k > 0) { k = (k - 1)/2; if (dat[k] != -1) ans = dat[k]; } return ans; } void update(int v, int a, int b, int k, int l, int r) { if (r <= a || b <= l) { return; } if (a <= l && r <= b) { dat[k] = v; return; } if (dat[k] != -1) { dat[2 * k + 1] = dat[k]; dat[2 * k + 2] = dat[k]; dat[k] = -1; } update(v, a, b, k * 2 + 1, l, (l + r) / 2); update(v, a, b, k * 2 + 2, (l + r) / 2, r); } void update(int v, int a, int b) { update(v, a, b, 0, 0, N); } }; int main () { int n, q; cin >> n >> q; SegTree st(n); for (auto i = 0; i < n; ++i) { st.update(2147483647, i, i+1); } for (auto i = 0; i < q; ++i) { int com; cin >> com; if (com == 0) { int s, t, x; cin >> s >> t >> x; st.update(x, s, t+1); } else { int k; cin >> k; cout << st.find(k) << endl; } } }
### Prompt Generate a Cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> #include <iomanip> // << fixed << setprecision(xxx) #include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ; #include <vector> #include <string> // to_string(nnn) // substr(m, n) // stoi(nnn) #include <complex> #include <tuple> // get<n>(xxx) #include <queue> #include <stack> #include <map> // if (M.find(key) != M.end()) { } #include <set> // S.insert(M); // if (S.find(key) != S.end()) { } // for (auto it=S.begin(); it != S.end(); it++) { } // auto it = S.lower_bound(M); #include <random> // random_device rd; mt19937 mt(rd()); #include <cctype> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> // atoi(xxx) using namespace std; #define DEBUG 0 // change 0 -> 1 if we need debug. // insert #if<tab> by my emacs. #if DEBUG == 1 ... #end typedef long long ll; // const int dx[4] = {1, 0, -1, 0}; // const int dy[4] = {0, 1, 0, -1}; // const int C = 1e6+10; // const ll M = 1000000007; class SegTree { // index starts at 0. public: int N; int* dat; SegTree(int n) { N = 1; while (N < n) N *= 2; dat = new int[2 * N - 1]; for (auto i = 0; i < 2 * N - 1; ++i) { dat[i] = -1; } } ~SegTree() { delete[] dat; } int find(int k) { k += N - 1; int ans = dat[k]; while (k > 0) { k = (k - 1)/2; if (dat[k] != -1) ans = dat[k]; } return ans; } void update(int v, int a, int b, int k, int l, int r) { if (r <= a || b <= l) { return; } if (a <= l && r <= b) { dat[k] = v; return; } if (dat[k] != -1) { dat[2 * k + 1] = dat[k]; dat[2 * k + 2] = dat[k]; dat[k] = -1; } update(v, a, b, k * 2 + 1, l, (l + r) / 2); update(v, a, b, k * 2 + 2, (l + r) / 2, r); } void update(int v, int a, int b) { update(v, a, b, 0, 0, N); } }; int main () { int n, q; cin >> n >> q; SegTree st(n); for (auto i = 0; i < n; ++i) { st.update(2147483647, i, i+1); } for (auto i = 0; i < q; ++i) { int com; cin >> com; if (com == 0) { int s, t, x; cin >> s >> t >> x; st.update(x, s, t+1); } else { int k; cin >> k; cout << st.find(k) << endl; } } } ```
#include <bits/stdc++.h> using namespace std; struct RU { using type1 = int; using type2 = int; static type1 id1() { return INT_MAX; } static type2 id2() { return -1; } static type1 op1(const type1& l, const type1& r) { return min(l, r); } static type1 op2(const type1& l, const type2& r) { return r == -1 ? l : r; } static type2 op3(const type2& l, const type2& r) { return r == -1 ? l : r; } }; template <typename M> class LazySegmentTree { using T1 = typename M::type1; using T2 = typename M::type2; const int n; vector<T1> data; vector<T2> lazy; int size(int n) { int res = 1; while (res < n) res <<= 1; return res; } void push(int node) { if (lazy[node] == M::id2()) return; if (node < n) { lazy[node * 2] = M::op3(lazy[node * 2], lazy[node]); lazy[node * 2 + 1] = M::op3(lazy[node * 2 + 1], lazy[node]); } data[node] = M::op2(data[node], lazy[node]); lazy[node] = M::id2(); } void suc(int l, int r, int node, int lb, int ub, T2 val) { if (ub <= l || r <= lb) return; if (l <= lb && ub <= r) { lazy[node] = M::op3(lazy[node], val); return; } push(node); int c = (lb + ub) / 2; suc(l, r, node * 2, lb, c, val); suc(l, r, node * 2 + 1, c, ub, val); data[node] = M::op1(M::op2(data[node * 2], lazy[node * 2]), M::op2(data[node * 2 + 1], lazy[node * 2 + 1])); } T1 sub(int l, int r, int node, int lb, int ub) { if (ub <= l || r <= lb) return M::id1(); if (l <= lb && ub <= r) return M::op2(data[node], lazy[node]); push(node); int c = (lb + ub) / 2; return M::op1(sub(l, r, node * 2, lb, c), sub(l, r, node * 2 + 1, c, ub)); } public: LazySegmentTree(int n_) : n(size(n_)), data(n * 2, M::id1()), lazy(n * 2, M::id2()) {} void update(int l, int r, T2 val) { suc(l, r + 1, 1, 0, n, val); } T1 find(int l, int r) { return sub(l, r + 1, 1, 0, n); } }; int main() { ios::sync_with_stdio(false), cin.tie(0); int n, q, type, s, t, x; cin >> n >> q; LazySegmentTree<RU> lst(n); while (q--) { cin >> type; if (type) { cin >> x; printf("%d\n", lst.find(x, x)); } else { cin >> s >> t >> x; lst.update(s, t, x); } } return 0; }
### Prompt In cpp, your task is to solve the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct RU { using type1 = int; using type2 = int; static type1 id1() { return INT_MAX; } static type2 id2() { return -1; } static type1 op1(const type1& l, const type1& r) { return min(l, r); } static type1 op2(const type1& l, const type2& r) { return r == -1 ? l : r; } static type2 op3(const type2& l, const type2& r) { return r == -1 ? l : r; } }; template <typename M> class LazySegmentTree { using T1 = typename M::type1; using T2 = typename M::type2; const int n; vector<T1> data; vector<T2> lazy; int size(int n) { int res = 1; while (res < n) res <<= 1; return res; } void push(int node) { if (lazy[node] == M::id2()) return; if (node < n) { lazy[node * 2] = M::op3(lazy[node * 2], lazy[node]); lazy[node * 2 + 1] = M::op3(lazy[node * 2 + 1], lazy[node]); } data[node] = M::op2(data[node], lazy[node]); lazy[node] = M::id2(); } void suc(int l, int r, int node, int lb, int ub, T2 val) { if (ub <= l || r <= lb) return; if (l <= lb && ub <= r) { lazy[node] = M::op3(lazy[node], val); return; } push(node); int c = (lb + ub) / 2; suc(l, r, node * 2, lb, c, val); suc(l, r, node * 2 + 1, c, ub, val); data[node] = M::op1(M::op2(data[node * 2], lazy[node * 2]), M::op2(data[node * 2 + 1], lazy[node * 2 + 1])); } T1 sub(int l, int r, int node, int lb, int ub) { if (ub <= l || r <= lb) return M::id1(); if (l <= lb && ub <= r) return M::op2(data[node], lazy[node]); push(node); int c = (lb + ub) / 2; return M::op1(sub(l, r, node * 2, lb, c), sub(l, r, node * 2 + 1, c, ub)); } public: LazySegmentTree(int n_) : n(size(n_)), data(n * 2, M::id1()), lazy(n * 2, M::id2()) {} void update(int l, int r, T2 val) { suc(l, r + 1, 1, 0, n, val); } T1 find(int l, int r) { return sub(l, r + 1, 1, 0, n); } }; int main() { ios::sync_with_stdio(false), cin.tie(0); int n, q, type, s, t, x; cin >> n >> q; LazySegmentTree<RU> lst(n); while (q--) { cin >> type; if (type) { cin >> x; printf("%d\n", lst.find(x, x)); } else { cin >> s >> t >> x; lst.update(s, t, x); } } return 0; } ```
#include <cstdio> #include <cstdlib> #include <cstring> #define siz 10000000 char buf[siz], *bit = buf; inline int nextInt(void) { register int ret = 0; register int neg = false; for (; *bit < '0'; ++bit) if (*bit == '-')neg ^= true; for (; *bit >= '0'; ++bit) ret = ret * 10 + *bit - '0'; return neg ? -ret : ret; } #define inf 2147483647 int n, m; int tag[400005]; int find(int t, int l, int r, int p) { if (~tag[t]) return tag[t]; int mid = (l + r) >> 1; if (p <= mid) return find(t << 1, l, mid, p); else return find(t << 1 | 1, mid + 1, r, p); } void update(int t, int l, int r, int x, int y, int k) { if (l == x && r == y) tag[t] = k; else { int mid = (l + r) >> 1; if (~tag[t]) tag[t << 1] = tag[t << 1 | 1] = tag[t], tag[t] = -1; if (y <= mid) update(t << 1, l, mid, x, y, k); else if (x > mid) update(t << 1 | 1, mid + 1, r, x, y, k); else { update(t << 1, l, mid, x, mid, k); update(t << 1 | 1, mid + 1, r, mid + 1, y, k); } } } signed main(void) { fread(buf, 1, siz, stdin); n = nextInt(); m = nextInt(); for (int i = 0; i < (n << 2); ++i) tag[i] = inf; for (int i = 1; i <= m; ++i) { int c = nextInt(); if (c) // find(x) printf("%d\n", find(1, 1, n, nextInt() + 1)); else { int x = nextInt(); int y = nextInt(); int k = nextInt(); update(1, 1, n, x + 1, y + 1, k); } } //system("pause"); }
### Prompt Your challenge is to write a CPP solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <cstdio> #include <cstdlib> #include <cstring> #define siz 10000000 char buf[siz], *bit = buf; inline int nextInt(void) { register int ret = 0; register int neg = false; for (; *bit < '0'; ++bit) if (*bit == '-')neg ^= true; for (; *bit >= '0'; ++bit) ret = ret * 10 + *bit - '0'; return neg ? -ret : ret; } #define inf 2147483647 int n, m; int tag[400005]; int find(int t, int l, int r, int p) { if (~tag[t]) return tag[t]; int mid = (l + r) >> 1; if (p <= mid) return find(t << 1, l, mid, p); else return find(t << 1 | 1, mid + 1, r, p); } void update(int t, int l, int r, int x, int y, int k) { if (l == x && r == y) tag[t] = k; else { int mid = (l + r) >> 1; if (~tag[t]) tag[t << 1] = tag[t << 1 | 1] = tag[t], tag[t] = -1; if (y <= mid) update(t << 1, l, mid, x, y, k); else if (x > mid) update(t << 1 | 1, mid + 1, r, x, y, k); else { update(t << 1, l, mid, x, mid, k); update(t << 1 | 1, mid + 1, r, mid + 1, y, k); } } } signed main(void) { fread(buf, 1, siz, stdin); n = nextInt(); m = nextInt(); for (int i = 0; i < (n << 2); ++i) tag[i] = inf; for (int i = 1; i <= m; ++i) { int c = nextInt(); if (c) // find(x) printf("%d\n", find(1, 1, n, nextInt() + 1)); else { int x = nextInt(); int y = nextInt(); int k = nextInt(); update(1, 1, n, x + 1, y + 1, k); } } //system("pause"); } ```
#include <bits/stdc++.h> using namespace std; int sid = 1; struct segtree { int l, r, sl, sr, v; } s[1000005]; int build(int l, int r) { int u = sid++; int m = l + r >> 1; s[u].l = l; s[u].r = r; s[u].v = 0x7fffffff; // from problem statement if (l != r) { s[u].sl = build(l, m); s[u].sr = build(m + 1, r); } return u; } void update(int u, int l, int r, int v) { if (l == s[u].l && r == s[u].r) { s[u].v = v; return; } if (s[u].v != -1) { s[s[u].sl].v = s[u].v; s[s[u].sr].v = s[u].v; s[u].v = -1; } int m = s[u].l + s[u].r >> 1; if (r <= m) { update(s[u].sl, l, r, v); } else if (l > m) { update(s[u].sr, l, r, v); } else { update(s[u].sl, l, m, v); update(s[u].sr, m + 1, r, v); } } int query(int u, int k) { if (s[u].v != -1) return s[u].v; int m = s[u].l + s[u].r >> 1; if (k <= m) return query(s[u].sl, k); else return query(s[u].sr, k); } int main() { int n, q; cin >> n >> q; int T = build(0, n - 1); for (int i = 0; i < q; i++) { int t; cin >> t; if (t == 0) { int a, b, c; cin >> a >> b >> c; update(T, a, b, c); } else { int k; cin >> k; cout << query(T, k) << endl; } } }
### Prompt Develop a solution in CPP to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int sid = 1; struct segtree { int l, r, sl, sr, v; } s[1000005]; int build(int l, int r) { int u = sid++; int m = l + r >> 1; s[u].l = l; s[u].r = r; s[u].v = 0x7fffffff; // from problem statement if (l != r) { s[u].sl = build(l, m); s[u].sr = build(m + 1, r); } return u; } void update(int u, int l, int r, int v) { if (l == s[u].l && r == s[u].r) { s[u].v = v; return; } if (s[u].v != -1) { s[s[u].sl].v = s[u].v; s[s[u].sr].v = s[u].v; s[u].v = -1; } int m = s[u].l + s[u].r >> 1; if (r <= m) { update(s[u].sl, l, r, v); } else if (l > m) { update(s[u].sr, l, r, v); } else { update(s[u].sl, l, m, v); update(s[u].sr, m + 1, r, v); } } int query(int u, int k) { if (s[u].v != -1) return s[u].v; int m = s[u].l + s[u].r >> 1; if (k <= m) return query(s[u].sl, k); else return query(s[u].sr, k); } int main() { int n, q; cin >> n >> q; int T = build(0, n - 1); for (int i = 0; i < q; i++) { int t; cin >> t; if (t == 0) { int a, b, c; cin >> a >> b >> c; update(T, a, b, c); } else { int k; cin >> k; cout << query(T, k) << endl; } } } ```
#include <iostream> #include <climits> typedef std::pair<int,int> TV; // Time and Value void update(TV* A, int s, int t, int index, int l, int r, int time, int val) { if (t<l || r<=s) return; // no intersection else if (s<=l && r<=t+1) { // [l,r) is contained in [s,t] A[index].first = time; A[index].second = val; return; } else { // at least one end exceeds the range int mid = (l+r)/2; update(A, s, t, 2*index+1, l, mid, time, val); update(A, s, t, 2*index+2, mid, r, time, val); } } TV find(TV* A, int k, int index, int l, int r) { if (l+1==r) return A[index]; int mid = (l+r)/2; TV res; if (k<mid) res = find(A, k, 2*index+1, l, mid); else res = find(A, k, 2*index+2, mid, r); return (res.first > A[index].first) ? res : A[index]; } int main () { int n, q; std::cin >> n >> q; int a_size = 1; for(; a_size<n; a_size<<=1); TV* A = new TV[2*a_size-1]; for (int i=0; i<2*a_size-1; i++) { A[i].first = -1; A[i].second = INT_MAX; } for (int i=0; i<q; i++) { int op, s, t, x, k; std::cin >> op; if (op==0) { std::cin >> s >> t >> x; update(A, s, t, 0, 0, n, i, x); } else { std::cin >> k; std::cout << find(A, k, 0, 0, n).second << std::endl; } } delete[] A; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> #include <climits> typedef std::pair<int,int> TV; // Time and Value void update(TV* A, int s, int t, int index, int l, int r, int time, int val) { if (t<l || r<=s) return; // no intersection else if (s<=l && r<=t+1) { // [l,r) is contained in [s,t] A[index].first = time; A[index].second = val; return; } else { // at least one end exceeds the range int mid = (l+r)/2; update(A, s, t, 2*index+1, l, mid, time, val); update(A, s, t, 2*index+2, mid, r, time, val); } } TV find(TV* A, int k, int index, int l, int r) { if (l+1==r) return A[index]; int mid = (l+r)/2; TV res; if (k<mid) res = find(A, k, 2*index+1, l, mid); else res = find(A, k, 2*index+2, mid, r); return (res.first > A[index].first) ? res : A[index]; } int main () { int n, q; std::cin >> n >> q; int a_size = 1; for(; a_size<n; a_size<<=1); TV* A = new TV[2*a_size-1]; for (int i=0; i<2*a_size-1; i++) { A[i].first = -1; A[i].second = INT_MAX; } for (int i=0; i<q; i++) { int op, s, t, x, k; std::cin >> op; if (op==0) { std::cin >> s >> t >> x; update(A, s, t, 0, 0, n, i, x); } else { std::cin >> k; std::cout << find(A, k, 0, 0, n).second << std::endl; } } delete[] A; return 0; } ```
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<set> #include<map> #include<string> #include<functional> #include<queue> #include<stack> #include<math.h> #define INF ((1<<30)-1+(1<<30)) #define EPS 1.0e-6 using namespace std; typedef long long ll; int n,q; int m[420000]; int init(int n) { int k = 1; while (k < n) { k *= 2; } for (int i = 0; i < 2 * k; i++) { m[i] = INF; } return k; } //[l,r)???x?????´??°??????.[bottom,top),node?????????????????? void update(int x, int l, int r, int bottom = 0, int top = n, int node = 1) { if (l <= bottom&&top <= r) { m[node] = x; return; } if (top <= l || r <= bottom)return; int mid = (bottom + top) / 2; //-INF???????????????????????¨?????????????????????????????¨?????¨??? if (m[node] != -INF) { m[2 * node] = m[node]; m[2 * node + 1] = m[node]; m[node] = -INF; } update(x, l, r, bottom, mid, 2 * node); update(x, l, r, mid, top, 2 * node + 1); } int find(int i, int bottom = 0,int top=n,int node=1) { if (i < bottom || top <= i)return -INF; if (m[node] != -INF)return m[node]; int mid = (bottom + top) / 2; int l = find(i, bottom, mid, 2 * node); int r = find(i, mid, top, 2 * node + 1); return max(l, r); } int main() { cin >> n >> q; n=init(n); int a, s, t, x, k; for (int i = 0; i < q; i++) { cin >> a; if (a == 0) { cin >> s >> t >> x; update(x, s, t + 1); } if (a == 1) { cin >> k; cout << find(k) << endl; } } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<set> #include<map> #include<string> #include<functional> #include<queue> #include<stack> #include<math.h> #define INF ((1<<30)-1+(1<<30)) #define EPS 1.0e-6 using namespace std; typedef long long ll; int n,q; int m[420000]; int init(int n) { int k = 1; while (k < n) { k *= 2; } for (int i = 0; i < 2 * k; i++) { m[i] = INF; } return k; } //[l,r)???x?????´??°??????.[bottom,top),node?????????????????? void update(int x, int l, int r, int bottom = 0, int top = n, int node = 1) { if (l <= bottom&&top <= r) { m[node] = x; return; } if (top <= l || r <= bottom)return; int mid = (bottom + top) / 2; //-INF???????????????????????¨?????????????????????????????¨?????¨??? if (m[node] != -INF) { m[2 * node] = m[node]; m[2 * node + 1] = m[node]; m[node] = -INF; } update(x, l, r, bottom, mid, 2 * node); update(x, l, r, mid, top, 2 * node + 1); } int find(int i, int bottom = 0,int top=n,int node=1) { if (i < bottom || top <= i)return -INF; if (m[node] != -INF)return m[node]; int mid = (bottom + top) / 2; int l = find(i, bottom, mid, 2 * node); int r = find(i, mid, top, 2 * node + 1); return max(l, r); } int main() { cin >> n >> q; n=init(n); int a, s, t, x, k; for (int i = 0; i < q; i++) { cin >> a; if (a == 0) { cin >> s >> t >> x; update(x, s, t + 1); } if (a == 1) { cin >> k; cout << find(k) << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) FOR(i,0,n) #define pb emplace_back typedef long long ll; typedef pair<int,int> pint; const ll INF=(1ll<<31)-1; int a[100001]; int b[1005]; int main(){ int n,q; cin>>n>>q; int sqn=sqrt(n); int bn=(n+sqn-1)/sqn; rep(i,n) a[i]=INF; rep(i,bn) b[i]=INF; int com; rep(i,q){ cin>>com; if(com==0){ int s,t,x; //[s,t) cin>>s>>t>>x; ++t; rep(j,bn+1){ int l=j*sqn,r=(j+1)*sqn; if(r<=s||t<=l) continue; if(s<=l&&r<=t) b[j]=x; else{ FOR(k,l,r){ if(k>=max(s,l)&&k<min(t,r)) a[k]=x; else if(b[j]!=INF) a[k]=b[j]; } b[j]=INF; } } } if(com==1){ int k; cin>>k; int cur=k/sqn; if(b[cur]!=INF){ FOR(j,cur*sqn,(cur+1)*sqn){ a[j]=b[cur]; } b[cur]=INF; } cout<<a[k]<<endl; } } return 0; }
### Prompt Generate a cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) FOR(i,0,n) #define pb emplace_back typedef long long ll; typedef pair<int,int> pint; const ll INF=(1ll<<31)-1; int a[100001]; int b[1005]; int main(){ int n,q; cin>>n>>q; int sqn=sqrt(n); int bn=(n+sqn-1)/sqn; rep(i,n) a[i]=INF; rep(i,bn) b[i]=INF; int com; rep(i,q){ cin>>com; if(com==0){ int s,t,x; //[s,t) cin>>s>>t>>x; ++t; rep(j,bn+1){ int l=j*sqn,r=(j+1)*sqn; if(r<=s||t<=l) continue; if(s<=l&&r<=t) b[j]=x; else{ FOR(k,l,r){ if(k>=max(s,l)&&k<min(t,r)) a[k]=x; else if(b[j]!=INF) a[k]=b[j]; } b[j]=INF; } } } if(com==1){ int k; cin>>k; int cur=k/sqn; if(b[cur]!=INF){ FOR(j,cur*sqn,(cur+1)*sqn){ a[j]=b[cur]; } b[cur]=INF; } cout<<a[k]<<endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; } template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, pii> pip; typedef pair<pll, pll> P; const ll INF = 1ll<<29; const ll MOD = 1000000007; const double EPS = 1e-9; const bool debug = 0; //---------------------------------// template<typename T> struct LazySegTree { vector<T> node, lazy; vector<bool> done; int n; T init_val; LazySegTree(int _n, T init_val) : init_val(init_val) { n = 1; while (n < _n) n *= 2; node.resize(n * 2 - 1, init_val); lazy.resize(n * 2 - 1); done.resize(n * 2 - 1, true); } void eval(int k, int l, int r) { if (done[k]) return; node[k] = lazy[k]; if (r - l > 1) { lazy[k * 2 + 1] = lazy[k]; done[k * 2 + 1] = false; lazy[k * 2 + 2] = lazy[k]; done[k * 2 + 2] = false; } done[k] = true; } void update(int a, int b, T x, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (a >= r || b <= l) return; if (a <= l && r <= b) { lazy[k] = x; done[k] = false; eval(k, l, r); } else { update(a, b, x, k * 2 + 1, l, (l + r) / 2); update(a, b, x, k * 2 + 2, (l + r) / 2, r); node[k] = min(node[k * 2 + 1], node[k * 2 + 2]); } } T query(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (a >= r || b <= l) return init_val; if (a <= l && r <= b) return node[k]; ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2); ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return min(vl, vr); } }; int main() { int n, q; cin >> n >> q; LazySegTree<ll> st(n, (1ll<<31) - 1); while (q--) { int a, b, c, d; cin >> a >> b; if (a == 0) { cin >> c >> d; st.update(b, c + 1, d); } else { cout << st.query(b, b + 1) << endl; } } return 0; }
### Prompt Generate a Cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; } template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, pii> pip; typedef pair<pll, pll> P; const ll INF = 1ll<<29; const ll MOD = 1000000007; const double EPS = 1e-9; const bool debug = 0; //---------------------------------// template<typename T> struct LazySegTree { vector<T> node, lazy; vector<bool> done; int n; T init_val; LazySegTree(int _n, T init_val) : init_val(init_val) { n = 1; while (n < _n) n *= 2; node.resize(n * 2 - 1, init_val); lazy.resize(n * 2 - 1); done.resize(n * 2 - 1, true); } void eval(int k, int l, int r) { if (done[k]) return; node[k] = lazy[k]; if (r - l > 1) { lazy[k * 2 + 1] = lazy[k]; done[k * 2 + 1] = false; lazy[k * 2 + 2] = lazy[k]; done[k * 2 + 2] = false; } done[k] = true; } void update(int a, int b, T x, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (a >= r || b <= l) return; if (a <= l && r <= b) { lazy[k] = x; done[k] = false; eval(k, l, r); } else { update(a, b, x, k * 2 + 1, l, (l + r) / 2); update(a, b, x, k * 2 + 2, (l + r) / 2, r); node[k] = min(node[k * 2 + 1], node[k * 2 + 2]); } } T query(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (a >= r || b <= l) return init_val; if (a <= l && r <= b) return node[k]; ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2); ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return min(vl, vr); } }; int main() { int n, q; cin >> n >> q; LazySegTree<ll> st(n, (1ll<<31) - 1); while (q--) { int a, b, c, d; cin >> a >> b; if (a == 0) { cin >> c >> d; st.update(b, c + 1, d); } else { cout << st.query(b, b + 1) << endl; } } return 0; } ```
#include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <cstring> #include <vector> #include <queue> #include <set> #include <map> #include <cmath> #include <iomanip> #include <cassert> #include <bitset> using namespace std; typedef pair<int, int> P; #define rep(i, n) for (int i=0; i<(n); i++) #define all(c) (c).begin(), (c).end() #define uniq(c) c.erase(unique(all(c)), (c).end()) #define _1 first #define _2 second #define pb push_back //#define INF 1145141919 #define INF 2147483647 #define MOD 1000000007 #define MAX_N (1<<17) // 10^5 struct SegTree { int seg[MAX_N*2-1], lazy[MAX_N*2-1]; SegTree() { for (int i=0; i<MAX_N*2-1; i++) seg[i] = INF, lazy[i] = INF; } inline void setLazy(int k, int v) { lazy[k] = v; seg[k] = v; } inline void fix(int k) { seg[k] = max(seg[k*2+1], seg[k*2+2]); } inline void push(int k) { if (lazy[k] == INF) return; setLazy(k*2+1, lazy[k]); setLazy(k*2+2, lazy[k]); lazy[k] = INF; } void fill(int a, int b, int x, int k=0, int l=0, int r=MAX_N) { if (r <= a || b <= l) return; if (a <= l && r <= b) { setLazy(k, x); return; } push(k); fill(a, b, x, k*2+1, l, (l+r)/2); fill(a, b, x, k*2+2, (l+r)/2, r); fix(k); } int query(int a, int b, int k=0, int l=0, int r=MAX_N) { if (r <= a || b <= l) return 0; if (a <= l && r <= b) return seg[k]; push(k); return max( query(a, b, k*2+1, l, (l+r)/2), query(a, b, k*2+2, (l+r)/2, r) ); } }; int N, Q; SegTree seg; signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> Q; rep(i, Q) { int t; cin >> t; if (t == 0) { int l, r, x; cin >> l >> r >> x; seg.fill(l, r+1, x); } else { int i; cin >> i; cout << seg.query(i, i+1) << "\n"; } } return 0; }
### Prompt In cpp, your task is to solve the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <cstring> #include <vector> #include <queue> #include <set> #include <map> #include <cmath> #include <iomanip> #include <cassert> #include <bitset> using namespace std; typedef pair<int, int> P; #define rep(i, n) for (int i=0; i<(n); i++) #define all(c) (c).begin(), (c).end() #define uniq(c) c.erase(unique(all(c)), (c).end()) #define _1 first #define _2 second #define pb push_back //#define INF 1145141919 #define INF 2147483647 #define MOD 1000000007 #define MAX_N (1<<17) // 10^5 struct SegTree { int seg[MAX_N*2-1], lazy[MAX_N*2-1]; SegTree() { for (int i=0; i<MAX_N*2-1; i++) seg[i] = INF, lazy[i] = INF; } inline void setLazy(int k, int v) { lazy[k] = v; seg[k] = v; } inline void fix(int k) { seg[k] = max(seg[k*2+1], seg[k*2+2]); } inline void push(int k) { if (lazy[k] == INF) return; setLazy(k*2+1, lazy[k]); setLazy(k*2+2, lazy[k]); lazy[k] = INF; } void fill(int a, int b, int x, int k=0, int l=0, int r=MAX_N) { if (r <= a || b <= l) return; if (a <= l && r <= b) { setLazy(k, x); return; } push(k); fill(a, b, x, k*2+1, l, (l+r)/2); fill(a, b, x, k*2+2, (l+r)/2, r); fix(k); } int query(int a, int b, int k=0, int l=0, int r=MAX_N) { if (r <= a || b <= l) return 0; if (a <= l && r <= b) return seg[k]; push(k); return max( query(a, b, k*2+1, l, (l+r)/2), query(a, b, k*2+2, (l+r)/2, r) ); } }; int N, Q; SegTree seg; signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> Q; rep(i, Q) { int t; cin >> t; if (t == 0) { int l, r, x; cin >> l >> r >> x; seg.fill(l, r+1, x); } else { int i; cin >> i; cout << seg.query(i, i+1) << "\n"; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAX_N=1<<18; typedef pair<int,int> P; int n_; P dat[2*MAX_N-1]; void init(int n) { n_=1; while (n>n_) { n_*=2; } for (int i=0; i<2*n_-1; i++) { dat[i].first=-1; dat[i].second=INT_MAX; } } int find(int i) { i+=n_-1; P p=dat[i]; while (i>0) { i=(i-1)/2; p=max(p,dat[i]); } return p.second; } void update(int a,int b,P p,int k=0,int l=0,int r=n_) { if (r<=a || b<=l) { return; } if (a<=l && r<=b) { dat[k]=p; } else { update(a,b,p,k*2+1,l,(l+r)/2); update(a,b,p,k*2+2,(l+r)/2,r); } } int main() { int n, q; cin >> n >> q; init(n); for (int k=0; k<q; k++) { int f; cin >> f; if (!f) { int s, t, x; cin >> s >> t >> x; update(s,t+1,P(k,x)); } else { int i; cin >> i; printf("%d\n",find(i)); } } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX_N=1<<18; typedef pair<int,int> P; int n_; P dat[2*MAX_N-1]; void init(int n) { n_=1; while (n>n_) { n_*=2; } for (int i=0; i<2*n_-1; i++) { dat[i].first=-1; dat[i].second=INT_MAX; } } int find(int i) { i+=n_-1; P p=dat[i]; while (i>0) { i=(i-1)/2; p=max(p,dat[i]); } return p.second; } void update(int a,int b,P p,int k=0,int l=0,int r=n_) { if (r<=a || b<=l) { return; } if (a<=l && r<=b) { dat[k]=p; } else { update(a,b,p,k*2+1,l,(l+r)/2); update(a,b,p,k*2+2,(l+r)/2,r); } } int main() { int n, q; cin >> n >> q; init(n); for (int k=0; k<q; k++) { int f; cin >> f; if (!f) { int s, t, x; cin >> s >> t >> x; update(s,t+1,P(k,x)); } else { int i; cin >> i; printf("%d\n",find(i)); } } return 0; } ```
#include <iostream> #include <sstream> #include <cstdio> #include <stdlib.h> #include <string> #include <vector> #include <algorithm> #include <climits> #include <cmath> using namespace std; #define MAX make_pair(-1,INT_MAX) vector<pair<int,int> > ary; int n; int right(int k){ return 2*k+2; } int left(int k){ return 2*k+1; } int parent(int k){ return (k-1)/2; } void update(int s,int t,int p,pair<int,int> x,int l,int r){ if(r<=s||t<=l)return; if(s<=l&&r<=t)ary[p]=x; else{ update(s,t,left(p),x,l,(l+r)/2); update(s,t,right(p),x,(l+r)/2,r); } } int query(int x){ x+=n-1; pair<int,int> res=ary[x]; while(x>0){ x=parent(x); res=(res<ary[x])?ary[x]:res; } return res.second; } void find(int x){ cout<<query(x)<<endl; } int main(){ int m,q,com,s,t,x,i=0; cin>>n>>q; while(n>pow(2,i))i++; n = pow(2,i); m = n*2-1; ary.resize(m*2); for(int i=0;i<m*2;i++)ary[i] = MAX; for(int i=0;i<q;i++){ cin>>com; if(!com){ cin>>s>>t>>x; update(s,t+1,0,make_pair(i,x),0,n); } else{ cin>>x; find(x); } } return 0; }
### Prompt Please formulate a cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> #include <sstream> #include <cstdio> #include <stdlib.h> #include <string> #include <vector> #include <algorithm> #include <climits> #include <cmath> using namespace std; #define MAX make_pair(-1,INT_MAX) vector<pair<int,int> > ary; int n; int right(int k){ return 2*k+2; } int left(int k){ return 2*k+1; } int parent(int k){ return (k-1)/2; } void update(int s,int t,int p,pair<int,int> x,int l,int r){ if(r<=s||t<=l)return; if(s<=l&&r<=t)ary[p]=x; else{ update(s,t,left(p),x,l,(l+r)/2); update(s,t,right(p),x,(l+r)/2,r); } } int query(int x){ x+=n-1; pair<int,int> res=ary[x]; while(x>0){ x=parent(x); res=(res<ary[x])?ary[x]:res; } return res.second; } void find(int x){ cout<<query(x)<<endl; } int main(){ int m,q,com,s,t,x,i=0; cin>>n>>q; while(n>pow(2,i))i++; n = pow(2,i); m = n*2-1; ary.resize(m*2); for(int i=0;i<m*2;i++)ary[i] = MAX; for(int i=0;i<q;i++){ cin>>com; if(!com){ cin>>s>>t>>x; update(s,t+1,0,make_pair(i,x),0,n); } else{ cin>>x; find(x); } } return 0; } ```
#include <iostream> using namespace std; #define INF 2147483647 #define ASTART (1<<17) int a[1<<18][2]; int ti=1; int find(int i){ int nv=INF,t=0; i+=ASTART-1; nv=a[i][0]; t=a[i][1]; while (i>0){ i=(i-1)/2; if (t<a[i][1])nv=a[i][0],t=a[i][1]; //cout << i << " = " << a[i][0]<< " = " << a[i][1]<< endl; } return nv; } void update(int s,int t,int x,int k,int l,int r){ if (l>=s&&r<=t){ a[k][0]=x; a[k][1]=ti; return; } if (r<=s||l>=t)return; int m=(l+r)/2; update(s,t,x,k*2+1,l,m); update(s,t,x,k*2+2,m,r); } int main(){ int n,q,com,s,t,x; for (int i=0;i<(1<<18);i++)a[i][0]=INF,a[i][1]=0; cin >> n>>q; for (int i=0;i<q;i++){ cin >> com; if (com==0) { cin >> s >> t >> x; update(s,t+1,x,0,0,ASTART); ti++; }else { cin >> s; cout << find(s)<< endl; } //for (int i=0;i<5;i++)cout << a[i] << endl; //cout << a[ASTART]; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> using namespace std; #define INF 2147483647 #define ASTART (1<<17) int a[1<<18][2]; int ti=1; int find(int i){ int nv=INF,t=0; i+=ASTART-1; nv=a[i][0]; t=a[i][1]; while (i>0){ i=(i-1)/2; if (t<a[i][1])nv=a[i][0],t=a[i][1]; //cout << i << " = " << a[i][0]<< " = " << a[i][1]<< endl; } return nv; } void update(int s,int t,int x,int k,int l,int r){ if (l>=s&&r<=t){ a[k][0]=x; a[k][1]=ti; return; } if (r<=s||l>=t)return; int m=(l+r)/2; update(s,t,x,k*2+1,l,m); update(s,t,x,k*2+2,m,r); } int main(){ int n,q,com,s,t,x; for (int i=0;i<(1<<18);i++)a[i][0]=INF,a[i][1]=0; cin >> n>>q; for (int i=0;i<q;i++){ cin >> com; if (com==0) { cin >> s >> t >> x; update(s,t+1,x,0,0,ASTART); ti++; }else { cin >> s; cout << find(s)<< endl; } //for (int i=0;i<5;i++)cout << a[i] << endl; //cout << a[ASTART]; } return 0; } ```
#include<iostream> #include<cstdio> #include<vector> #include<queue> #include<map> #include<string> #include<algorithm> #include<functional> #define ll long long #define pa pair<int,int> #define int long long using namespace std; int inf=(1ll<<40); struct seg_rangeupd_getpoint{ // 1 // 2 3 // 4 5 6 7 private: public: // (1<<15)=32768 // 1<<16 = 65536 // 1<<17 = 131072 // 1<<18 = 262144 int cor=(1<<17); vector<int> vec; void shoki1(){ vec.resize(2*cor+3, inf); } void shoki2(){ vec[1]=(1ll<<31)-1; //初期値 } void chien(int k,int l,int r){ if(l+1<r)if(vec[k]!=inf){ vec[2*k]=vec[k]; vec[2*k+1]=vec[k]; vec[k]=inf; } } void rangeupd(int a,int b,int w, int k=1,int l=0,int r=-3){ //[a,b)を wに変更 if(r<0) r=cor; if(a<=l && r<=b){ vec[k]=w; return ; } if(r<=a || b<=l){ return ; } chien(k,l,r); // cout<<k<<" "<<l<<" "<<r<<endl; rangeupd(a,b,w,k*2,l,(l+r)/2); rangeupd(a,b,w,k*2+1,(l+r)/2,r); return ; } // [a,b) // k-th node // k no kukanha [l,r) int getpoint(int x){ x+=cor; int ans=0; while(1){ if(x==0) break; if(vec[x]!=inf) ans=vec[x]; x/=2; } return ans; } void pre(){ for(int i=1;i<2*cor;i++){ cout<<vec[i]<<" "; if(((i+1)&(-(i+1)))==i+1) cout<<endl; } } }; //[a,b)にxを一様加算 rangeadd(a,b,x) //[a,b)の和 getsum(a,b) signed main(){ seg_rangeupd_getpoint SE; SE.shoki1(); // for(int i=0;i<SE.cor;i++){ // SE.vec[i+SE.cor]=i; // } SE.shoki2(); int n,q; cin>>n>>q; for(int i=0;i<q;i++){ int d,a,b,c; cin>>a; if(a==0){ cin>>b>>c>>d; SE.rangeupd(b,c+1,d); } else{ cin>>b; cout<<SE.getpoint(b)<<endl; } //SE.pre(); } return 0; }
### Prompt Generate a cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<iostream> #include<cstdio> #include<vector> #include<queue> #include<map> #include<string> #include<algorithm> #include<functional> #define ll long long #define pa pair<int,int> #define int long long using namespace std; int inf=(1ll<<40); struct seg_rangeupd_getpoint{ // 1 // 2 3 // 4 5 6 7 private: public: // (1<<15)=32768 // 1<<16 = 65536 // 1<<17 = 131072 // 1<<18 = 262144 int cor=(1<<17); vector<int> vec; void shoki1(){ vec.resize(2*cor+3, inf); } void shoki2(){ vec[1]=(1ll<<31)-1; //初期値 } void chien(int k,int l,int r){ if(l+1<r)if(vec[k]!=inf){ vec[2*k]=vec[k]; vec[2*k+1]=vec[k]; vec[k]=inf; } } void rangeupd(int a,int b,int w, int k=1,int l=0,int r=-3){ //[a,b)を wに変更 if(r<0) r=cor; if(a<=l && r<=b){ vec[k]=w; return ; } if(r<=a || b<=l){ return ; } chien(k,l,r); // cout<<k<<" "<<l<<" "<<r<<endl; rangeupd(a,b,w,k*2,l,(l+r)/2); rangeupd(a,b,w,k*2+1,(l+r)/2,r); return ; } // [a,b) // k-th node // k no kukanha [l,r) int getpoint(int x){ x+=cor; int ans=0; while(1){ if(x==0) break; if(vec[x]!=inf) ans=vec[x]; x/=2; } return ans; } void pre(){ for(int i=1;i<2*cor;i++){ cout<<vec[i]<<" "; if(((i+1)&(-(i+1)))==i+1) cout<<endl; } } }; //[a,b)にxを一様加算 rangeadd(a,b,x) //[a,b)の和 getsum(a,b) signed main(){ seg_rangeupd_getpoint SE; SE.shoki1(); // for(int i=0;i<SE.cor;i++){ // SE.vec[i+SE.cor]=i; // } SE.shoki2(); int n,q; cin>>n>>q; for(int i=0;i<q;i++){ int d,a,b,c; cin>>a; if(a==0){ cin>>b>>c>>d; SE.rangeupd(b,c+1,d); } else{ cin>>b; cout<<SE.getpoint(b)<<endl; } //SE.pre(); } return 0; } ```
#include "stdio.h" #include "math.h" #include <algorithm> using namespace std; #define SUQARE_SIZE 100000 int block[SUQARE_SIZE]; int blockSize; int n; void updateArr(int arr[],int left,int num){ for(int i = left;i < left + blockSize;++i){ if(i >= n){ break; } arr[i] = num; } } void update(int arr[],int l, int r,int value) { // 左端 if(l % blockSize != 0 && l != 0 && l < r){ int blockNumber = l / blockSize; if(block[blockNumber] != __INT_MAX__){ updateArr(arr,blockNumber * blockSize,block[blockNumber]); block[blockNumber] = __INT_MAX__; } do{ arr[l] = value; l++; }while(l % blockSize != 0 && l < r); } // blockに該当する部分 while ((l + blockSize) <= r) { block[l / blockSize] = value; l += blockSize; } if(l <= r){ int blockNumber = r / blockSize; if(block[blockNumber] != __INT_MAX__){ updateArr(arr,blockNumber * blockSize,block[blockNumber]); block[blockNumber] = __INT_MAX__; } // 右端 do{ arr[l] = value; l++; }while(l <= r); } } int query(int arr[],int index) { int blockNumber = index / blockSize; if(block[blockNumber] != __INT_MAX__){ return block[blockNumber]; } else{ return arr[index]; } } int main(){ int q,command; scanf("%d %d",&n,&q); int a[n]; blockSize = sqrt(n); for(int i = 0;i < n;++i){ a[i] = block[i] = __INT_MAX__; } for(int i = 0;i < q;++i){ scanf("%d",&command); if(command == 0){ int l,r,value; scanf("%d %d %d",&l,&r,&value); update(a,l,r,value); }else{ int index; scanf("%d",&index); printf("%d\n",query(a, index)); } } }
### Prompt Please create a solution in cpp to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include "stdio.h" #include "math.h" #include <algorithm> using namespace std; #define SUQARE_SIZE 100000 int block[SUQARE_SIZE]; int blockSize; int n; void updateArr(int arr[],int left,int num){ for(int i = left;i < left + blockSize;++i){ if(i >= n){ break; } arr[i] = num; } } void update(int arr[],int l, int r,int value) { // 左端 if(l % blockSize != 0 && l != 0 && l < r){ int blockNumber = l / blockSize; if(block[blockNumber] != __INT_MAX__){ updateArr(arr,blockNumber * blockSize,block[blockNumber]); block[blockNumber] = __INT_MAX__; } do{ arr[l] = value; l++; }while(l % blockSize != 0 && l < r); } // blockに該当する部分 while ((l + blockSize) <= r) { block[l / blockSize] = value; l += blockSize; } if(l <= r){ int blockNumber = r / blockSize; if(block[blockNumber] != __INT_MAX__){ updateArr(arr,blockNumber * blockSize,block[blockNumber]); block[blockNumber] = __INT_MAX__; } // 右端 do{ arr[l] = value; l++; }while(l <= r); } } int query(int arr[],int index) { int blockNumber = index / blockSize; if(block[blockNumber] != __INT_MAX__){ return block[blockNumber]; } else{ return arr[index]; } } int main(){ int q,command; scanf("%d %d",&n,&q); int a[n]; blockSize = sqrt(n); for(int i = 0;i < n;++i){ a[i] = block[i] = __INT_MAX__; } for(int i = 0;i < q;++i){ scanf("%d",&command); if(command == 0){ int l,r,value; scanf("%d %d %d",&l,&r,&value); update(a,l,r,value); }else{ int index; scanf("%d",&index); printf("%d\n",query(a, index)); } } } ```
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF=INT_MAX; struct segtree { vector<int> node; vector<int> lazy; vector<bool> lflg; int n; segtree(int sz) { n=1;while(n<sz) n*=2; node.resize(2*n,INF); lazy.resize(2*n,0); lflg.resize(2*n,false); } void lazyupdate(int k, int l, int r) { if(lflg[k]) { node[k]=lazy[k]; push(lazy[k],k,l,r); lazy[k]=0; lflg[k]=false; } } void push(int v,int k,int l,int r) { if(r-l>0) { lazy[2*k]=lazy[2*k+1]=v; lflg[2*k]=lflg[2*k+1]=true; } } void update(int qa, int qb, int v, int k=1, int l=1,int r=-1) { if(r<0) r=n; lazyupdate(k, l, r); if(qb<l||r<qa) return; if(qa<=l&&r<=qb) { node[k]=v; push(v,k,l,r); } else { int m=(l+r)/2; update(qa,qb,v,2*k,l,m); update(qa,qb,v,2*k+1,m+1,r); node[k]=node[2*k]+node[2*k+1]; } } int get(int qa, int qb, int k=1, int l=1, int r=-1) { if(r<0) r=n; lazyupdate(k ,l, r); if(qb<l||r<qa) return INF; if(qa<=l&&r<=qb) return node[k]; else { int m=(l+r)/2; int lv=get(qa, qb, 2*k, l, m); int rv=get(qa, qb, 2*k+1, m+1, r); return min(lv,rv); } } }; int main() { int n,q; scanf("%d %d",&n,&q); segtree tree(n); for(int i=0;i<q;i++){ int c; scanf("%d",&c); if(c){ int x; scanf("%d",&x); cout<<tree.get(x+1,x+1)<<endl; }else{ int s,t,v; scanf("%d %d %d",&s,&t,&v); tree.update(s+1,t+1,v); } } }
### Prompt Develop a solution in Cpp to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF=INT_MAX; struct segtree { vector<int> node; vector<int> lazy; vector<bool> lflg; int n; segtree(int sz) { n=1;while(n<sz) n*=2; node.resize(2*n,INF); lazy.resize(2*n,0); lflg.resize(2*n,false); } void lazyupdate(int k, int l, int r) { if(lflg[k]) { node[k]=lazy[k]; push(lazy[k],k,l,r); lazy[k]=0; lflg[k]=false; } } void push(int v,int k,int l,int r) { if(r-l>0) { lazy[2*k]=lazy[2*k+1]=v; lflg[2*k]=lflg[2*k+1]=true; } } void update(int qa, int qb, int v, int k=1, int l=1,int r=-1) { if(r<0) r=n; lazyupdate(k, l, r); if(qb<l||r<qa) return; if(qa<=l&&r<=qb) { node[k]=v; push(v,k,l,r); } else { int m=(l+r)/2; update(qa,qb,v,2*k,l,m); update(qa,qb,v,2*k+1,m+1,r); node[k]=node[2*k]+node[2*k+1]; } } int get(int qa, int qb, int k=1, int l=1, int r=-1) { if(r<0) r=n; lazyupdate(k ,l, r); if(qb<l||r<qa) return INF; if(qa<=l&&r<=qb) return node[k]; else { int m=(l+r)/2; int lv=get(qa, qb, 2*k, l, m); int rv=get(qa, qb, 2*k+1, m+1, r); return min(lv,rv); } } }; int main() { int n,q; scanf("%d %d",&n,&q); segtree tree(n); for(int i=0;i<q;i++){ int c; scanf("%d",&c); if(c){ int x; scanf("%d",&x); cout<<tree.get(x+1,x+1)<<endl; }else{ int s,t,v; scanf("%d %d %d",&s,&t,&v); tree.update(s+1,t+1,v); } } } ```
#include <bits/stdc++.h> using namespace std; struct RU { using t1 = int; using t2 = int; static t2 id2() { return -1; } static t1 op2(const t1& l, const t2& r) { return r == id2() ? l : r; } static t2 op3(const t2& l, const t2& r) { return r == id2() ? l : r; } }; template <typename M> class lazy_segment_tree { using T1 = typename M::t1; using T2 = typename M::t2; const int h, n; vector<T1> data; vector<T2> lazy; void push(int node) { if (lazy[node] == M::id2()) return; if (node < n) { lazy[node << 1] = M::op3(lazy[node << 1], lazy[node]); lazy[(node << 1) + 1] = M::op3(lazy[(node << 1) + 1], lazy[node]); } lazy[node] = M::id2(); } public: lazy_segment_tree(int n_, T1 v1) : h(ceil(log2(n_))), n(1 << h), data(n, v1), lazy(n << 1, M::id2()) {} lazy_segment_tree(const vector<T1>& data_) : h(ceil(log2(data_.size()))), n(1 << h), data(data_), lazy(n << 1, M::id2()) {} void update(int l, int r, T2 val) { l += n, r += n; for (int i = h; i > 0; i--) push(l >> i), push(r >> i); r++; while (l < r) { if (l & 1) lazy[l] = M::op3(lazy[l], val), l++; if (r & 1) r--, lazy[r] = M::op3(lazy[r], val); l >>= 1; r >>= 1; } } T1 find(int p) { T1 res = data[p]; p += n; while (p) res = M::op2(res, lazy[p]), p >>= 1; return res; } }; int main() { ios::sync_with_stdio(false), cin.tie(0); int n, q; cin >> n >> q; lazy_segment_tree<RU> lst(n, INT_MAX); while (q--) { int com; cin >> com; if (com == 0) { int s, t, x; cin >> s >> t >> x; lst.update(s, t, x); } else { int p; cin >> p; printf("%d\n", lst.find(p)); } } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct RU { using t1 = int; using t2 = int; static t2 id2() { return -1; } static t1 op2(const t1& l, const t2& r) { return r == id2() ? l : r; } static t2 op3(const t2& l, const t2& r) { return r == id2() ? l : r; } }; template <typename M> class lazy_segment_tree { using T1 = typename M::t1; using T2 = typename M::t2; const int h, n; vector<T1> data; vector<T2> lazy; void push(int node) { if (lazy[node] == M::id2()) return; if (node < n) { lazy[node << 1] = M::op3(lazy[node << 1], lazy[node]); lazy[(node << 1) + 1] = M::op3(lazy[(node << 1) + 1], lazy[node]); } lazy[node] = M::id2(); } public: lazy_segment_tree(int n_, T1 v1) : h(ceil(log2(n_))), n(1 << h), data(n, v1), lazy(n << 1, M::id2()) {} lazy_segment_tree(const vector<T1>& data_) : h(ceil(log2(data_.size()))), n(1 << h), data(data_), lazy(n << 1, M::id2()) {} void update(int l, int r, T2 val) { l += n, r += n; for (int i = h; i > 0; i--) push(l >> i), push(r >> i); r++; while (l < r) { if (l & 1) lazy[l] = M::op3(lazy[l], val), l++; if (r & 1) r--, lazy[r] = M::op3(lazy[r], val); l >>= 1; r >>= 1; } } T1 find(int p) { T1 res = data[p]; p += n; while (p) res = M::op2(res, lazy[p]), p >>= 1; return res; } }; int main() { ios::sync_with_stdio(false), cin.tie(0); int n, q; cin >> n >> q; lazy_segment_tree<RU> lst(n, INT_MAX); while (q--) { int com; cin >> com; if (com == 0) { int s, t, x; cin >> s >> t >> x; lst.update(s, t, x); } else { int p; cin >> p; printf("%d\n", lst.find(p)); } } return 0; } ```
#include <iostream> using namespace std; #define INF (1 << 30) - 1 + (1 << 30) int a[262144], n2, lazy[262144]; void lazyeval(int i) { if (lazy[i] == INF) return; a[i] = lazy[i]; if (i < n2 - 1) { lazy[2 * i + 1] = lazy[2 * i + 2] = lazy[i]; } lazy[i] = INF; } int findsingle(int i) { int j = i + n2 - 1; int val = a[i + n2 - 1]; if (lazy[j] != INF) val = lazy[j]; while (j > 0) { j = (j - 1) / 2; if (lazy[j] != INF) val = lazy[j]; } return val; } void updaterange(int s, int t, int i, int l, int r, int x) { lazyeval(i); // cout << s << " " << t << " " << i << " " << l << " " << r << endl; if (s <= l && r <= t) { a[i] = x; if (i < n2 - 1) { lazy[2 * i + 1] = lazy[2 * i + 2] = a[i]; } return; } if (t <= l || r <= s) return; updaterange(s, t, i * 2 + 1, l, (l + r) / 2, x); updaterange(s, t, i * 2 + 2, (l + r) / 2, r, x); a[i] = min(a[i * 2 + 1], a[i * 2 + 2]); } int main() { int n, q, com, s, t, x; cin >> n >> q; n2 = 1; while (n > n2) n2 *= 2; for (int i = 0; i < n2 * 2; i++) a[i] = lazy[i] = INF; for (int i = 0; i < q; i++) { cin >> com >> s; if (com == 0) { cin >> t >> x; updaterange(s, t + 1, 0, 0, n2, x); } else cout << findsingle(s) << endl; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> using namespace std; #define INF (1 << 30) - 1 + (1 << 30) int a[262144], n2, lazy[262144]; void lazyeval(int i) { if (lazy[i] == INF) return; a[i] = lazy[i]; if (i < n2 - 1) { lazy[2 * i + 1] = lazy[2 * i + 2] = lazy[i]; } lazy[i] = INF; } int findsingle(int i) { int j = i + n2 - 1; int val = a[i + n2 - 1]; if (lazy[j] != INF) val = lazy[j]; while (j > 0) { j = (j - 1) / 2; if (lazy[j] != INF) val = lazy[j]; } return val; } void updaterange(int s, int t, int i, int l, int r, int x) { lazyeval(i); // cout << s << " " << t << " " << i << " " << l << " " << r << endl; if (s <= l && r <= t) { a[i] = x; if (i < n2 - 1) { lazy[2 * i + 1] = lazy[2 * i + 2] = a[i]; } return; } if (t <= l || r <= s) return; updaterange(s, t, i * 2 + 1, l, (l + r) / 2, x); updaterange(s, t, i * 2 + 2, (l + r) / 2, r, x); a[i] = min(a[i * 2 + 1], a[i * 2 + 2]); } int main() { int n, q, com, s, t, x; cin >> n >> q; n2 = 1; while (n > n2) n2 *= 2; for (int i = 0; i < n2 * 2; i++) a[i] = lazy[i] = INF; for (int i = 0; i < q; i++) { cin >> com >> s; if (com == 0) { cin >> t >> x; updaterange(s, t + 1, 0, 0, n2, x); } else cout << findsingle(s) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(c) begin(c), end(c) #define dump(x) cerr << __LINE__ << ":\t" #x " = " << (x) << endl struct segtree { vector<int> lazy, dat; int n; segtree(int n_, int init){ n = 1; while(n < n_) n *= 2; dat.assign(n*2, init); lazy.assign(n*2, -1); } void set(int l, int r, int x){ static queue<tuple<int,int,int>> q; q.emplace(0, n, 1); while(q.size()){ int segl, segr, n; tie(segl, segr, n) = q.front(); q.pop(); push(segl, segr, n); // if(segr <= l || r <= segl); if(l <= segl && segr <= r) lazy[n] = x; else { int segm = (segl + segr) / 2; if(l < segm && segl < r) q.emplace(segl, segm, n*2); if(l < segr && segm < r) q.emplace(segm, segr, n*2+1); } } } int get(int k){ return get(k, 0, n, 1); } int get(int k, int segl, int segr, int n){ push(segl, segr, n); if(segl + 1 == segr) return dat[n]; int segm = (segl + segr) / 2; if(k < segm) return get(k, segl, segm, n*2); else return get(k, segm, segr, n*2+1); } void push(int segl, int segr, int node){ if(lazy[node] != -1){ dat[node] = lazy[node]; if(segl + 1 != segr){ lazy[node*2] = lazy[node]; lazy[node*2+1] = lazy[node]; } lazy[node] = -1; } } }; int main(){ cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; segtree st(n, 2147483647); for(int iq = 0; iq < q; ++iq){ int t; cin >> t; // dump(t); if(t == 0){ int s, t, x; cin >> s >> t >> x; ++t; st.set(s, t, x); } else { int i; cin >> i; cout << st.get(i) << '\n'; } } }
### Prompt Generate a CPP solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(c) begin(c), end(c) #define dump(x) cerr << __LINE__ << ":\t" #x " = " << (x) << endl struct segtree { vector<int> lazy, dat; int n; segtree(int n_, int init){ n = 1; while(n < n_) n *= 2; dat.assign(n*2, init); lazy.assign(n*2, -1); } void set(int l, int r, int x){ static queue<tuple<int,int,int>> q; q.emplace(0, n, 1); while(q.size()){ int segl, segr, n; tie(segl, segr, n) = q.front(); q.pop(); push(segl, segr, n); // if(segr <= l || r <= segl); if(l <= segl && segr <= r) lazy[n] = x; else { int segm = (segl + segr) / 2; if(l < segm && segl < r) q.emplace(segl, segm, n*2); if(l < segr && segm < r) q.emplace(segm, segr, n*2+1); } } } int get(int k){ return get(k, 0, n, 1); } int get(int k, int segl, int segr, int n){ push(segl, segr, n); if(segl + 1 == segr) return dat[n]; int segm = (segl + segr) / 2; if(k < segm) return get(k, segl, segm, n*2); else return get(k, segm, segr, n*2+1); } void push(int segl, int segr, int node){ if(lazy[node] != -1){ dat[node] = lazy[node]; if(segl + 1 != segr){ lazy[node*2] = lazy[node]; lazy[node*2+1] = lazy[node]; } lazy[node] = -1; } } }; int main(){ cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; segtree st(n, 2147483647); for(int iq = 0; iq < q; ++iq){ int t; cin >> t; // dump(t); if(t == 0){ int s, t, x; cin >> s >> t >> x; ++t; st.set(s, t, x); } else { int i; cin >> i; cout << st.get(i) << '\n'; } } } ```
#include<bits/stdc++.h> using namespace std; int n,q,d[1<<18],a,b,c,e[1<<17]; void m(int l,int r,int k){if(r<=a||b<=l)return;if(a<=l&&r<=b)d[k]=q;else{m(l,(l+r)/2,k*2+1);m((l+r)/2,r,k*2+2);}} int main(){ cin>>a>>q;n=2;while(n<a)n*=2;a=2*n;while(a--)d[a]=114514;e[114514]=INT_MAX; while(q--){cin>>a; if(a){cin>>a;a+=n-1,b=d[a];while(a)a=(a-1)/2,b=min(b,d[a]);cout<<e[b]<<endl;} else{cin>>a>>b>>c;b++,e[q]=c;m(0,n,0);} } return 0; }
### Prompt Create a solution in CPP for the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int n,q,d[1<<18],a,b,c,e[1<<17]; void m(int l,int r,int k){if(r<=a||b<=l)return;if(a<=l&&r<=b)d[k]=q;else{m(l,(l+r)/2,k*2+1);m((l+r)/2,r,k*2+2);}} int main(){ cin>>a>>q;n=2;while(n<a)n*=2;a=2*n;while(a--)d[a]=114514;e[114514]=INT_MAX; while(q--){cin>>a; if(a){cin>>a;a+=n-1,b=d[a];while(a)a=(a-1)/2,b=min(b,d[a]);cout<<e[b]<<endl;} else{cin>>a>>b>>c;b++,e[q]=c;m(0,n,0);} } return 0; } ```
#include<bits/stdc++.h> #define int long long #define for0(i, n) for(int i = 0; i < (n); i++) #define mp make_pair using namespace std; int t, n, q, l = 0, r = 123456, x = 2147483647, q1; pair<int, int>t1[123456], t2[12345], t3[1234], t4[123], t5[12]; void f1() { r++; while (l < r) { if (l % 10000 == 0 && l + 10000 <= r) { t5[l / 10000] = mp(t, x); l += 10000; } else if (l % 1000 == 0 && l + 1000 <= r) { t4[l / 1000] = mp(t, x); l += 1000; } else if (l % 100 == 0 && l + 100 <= r) { t3[l / 100] = mp(t, x); l += 100; } else if (l % 10 == 0 && l + 10 <= r) { t2[l / 10] = mp(t, x); l += 10; } else { t1[l] = mp(t, x); l++; } } } int f2(int i) { int t = t1[i].first, x = t1[i].second; i /= 10; if (t < t2[i].first) { t = t2[i].first; x = t2[i].second; } i /= 10; if (t < t3[i].first) { t = t3[i].first; x = t3[i].second; } i /= 10; if (t < t4[i].first) { t = t4[i].first; x = t4[i].second; } i /= 10; if (t < t5[i].first) { t = t5[i].first; x = t5[i].second; } return x; } signed main() { cin >> n >> q; for0(i, n)t1[i] = mp(0, x); for (t = 1; t <= q; t++) { cin >> q1; if (q1) { int i; cin >> i; cout << f2(i) << endl; } else { cin >> l >> r >> x; f1(); } } }
### Prompt Please create a solution in Cpp to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<bits/stdc++.h> #define int long long #define for0(i, n) for(int i = 0; i < (n); i++) #define mp make_pair using namespace std; int t, n, q, l = 0, r = 123456, x = 2147483647, q1; pair<int, int>t1[123456], t2[12345], t3[1234], t4[123], t5[12]; void f1() { r++; while (l < r) { if (l % 10000 == 0 && l + 10000 <= r) { t5[l / 10000] = mp(t, x); l += 10000; } else if (l % 1000 == 0 && l + 1000 <= r) { t4[l / 1000] = mp(t, x); l += 1000; } else if (l % 100 == 0 && l + 100 <= r) { t3[l / 100] = mp(t, x); l += 100; } else if (l % 10 == 0 && l + 10 <= r) { t2[l / 10] = mp(t, x); l += 10; } else { t1[l] = mp(t, x); l++; } } } int f2(int i) { int t = t1[i].first, x = t1[i].second; i /= 10; if (t < t2[i].first) { t = t2[i].first; x = t2[i].second; } i /= 10; if (t < t3[i].first) { t = t3[i].first; x = t3[i].second; } i /= 10; if (t < t4[i].first) { t = t4[i].first; x = t4[i].second; } i /= 10; if (t < t5[i].first) { t = t5[i].first; x = t5[i].second; } return x; } signed main() { cin >> n >> q; for0(i, n)t1[i] = mp(0, x); for (t = 1; t <= q; t++) { cin >> q1; if (q1) { int i; cin >> i; cout << f2(i) << endl; } else { cin >> l >> r >> x; f1(); } } } ```
#include<iostream> #include<cstdio> #include<vector> #include<algorithm> #include<cstring> #include<stdio.h> #include<fstream> #include<stdlib.h> #include<math.h> #include<queue> #include<string.h> #include<stack> using namespace std; #define INF 2147483647 int n=1; int A[300000]; bool flag[300000]; int lazy[300000]; void init(int size){ while(n<size){ n*=2; } for(int i=0;i<2*n-1;i++){ A[i]=INF; flag[i]=false; } } void eval(int k,int l,int r){ if(flag[k]){ A[k]=lazy[k]; if(l+1<r){ lazy[2*k+1]=lazy[2*k+2]=lazy[k]; flag[2*k+1]=flag[2*k+2]=true; } flag[k]=false; } } void update(int x,int y,int a,int k=0,int l=0,int r=n){ eval(k,l,r); if(r<=x||y<=l){ return; } if(x<=l&&r<=y){ flag[k]=true; lazy[k]=a; eval(k,l,r); } else{ update(x,y,a,2*k+1,l,(l+r)/2); update(x,y,a,2*k+2,(l+r)/2,r); A[k]=min(A[2*k+1],A[2*k+2]); } } int query2(int x,int y,int k=0,int l=0,int r=n){ eval(k,l,r); if(r<=x||y<=l){ return INF; } if(x<=l&&r<=y){ return A[k]; } int vl=query2(x,y,2*k+1,l,(l+r)/2); int vr=query2(x,y,2*k+2,(l+r)/2,r); return min(vl,vr); } int main(){ int k; int q; cin>>k>>q; init(k); int qu; int x,s,t; for(int i=0;i<q;i++){ cin>>qu; if(qu==0){ cin>>s>>t>>x; update(s,t+1,x); } else if(qu==1){ cin>>x; cout<<query2(x,x+1)<<endl; } } return 0; }
### Prompt Generate a Cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<iostream> #include<cstdio> #include<vector> #include<algorithm> #include<cstring> #include<stdio.h> #include<fstream> #include<stdlib.h> #include<math.h> #include<queue> #include<string.h> #include<stack> using namespace std; #define INF 2147483647 int n=1; int A[300000]; bool flag[300000]; int lazy[300000]; void init(int size){ while(n<size){ n*=2; } for(int i=0;i<2*n-1;i++){ A[i]=INF; flag[i]=false; } } void eval(int k,int l,int r){ if(flag[k]){ A[k]=lazy[k]; if(l+1<r){ lazy[2*k+1]=lazy[2*k+2]=lazy[k]; flag[2*k+1]=flag[2*k+2]=true; } flag[k]=false; } } void update(int x,int y,int a,int k=0,int l=0,int r=n){ eval(k,l,r); if(r<=x||y<=l){ return; } if(x<=l&&r<=y){ flag[k]=true; lazy[k]=a; eval(k,l,r); } else{ update(x,y,a,2*k+1,l,(l+r)/2); update(x,y,a,2*k+2,(l+r)/2,r); A[k]=min(A[2*k+1],A[2*k+2]); } } int query2(int x,int y,int k=0,int l=0,int r=n){ eval(k,l,r); if(r<=x||y<=l){ return INF; } if(x<=l&&r<=y){ return A[k]; } int vl=query2(x,y,2*k+1,l,(l+r)/2); int vr=query2(x,y,2*k+2,(l+r)/2,r); return min(vl,vr); } int main(){ int k; int q; cin>>k>>q; init(k); int qu; int x,s,t; for(int i=0;i<q;i++){ cin>>qu; if(qu==0){ cin>>s>>t>>x; update(s,t+1,x); } else if(qu==1){ cin>>x; cout<<query2(x,x+1)<<endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; #define int long long const int INF = (1LL << 31) - 1; const int sqrtN = 512; struct SqrtDecomposition { int N, K; vector<int> data; vector<bool> lazyFlag; vector<int> lazyUpdate; SqrtDecomposition(int n) : N(n) { K = (N + sqrtN - 1) / sqrtN; data.assign(K * sqrtN, INF); lazyFlag.assign(K, false); lazyUpdate.assign(K, 0); } void eval(int k) { if (lazyFlag[k]) { lazyFlag[k] = false; for (int i = k * sqrtN; i < (k + 1) * sqrtN; ++i) { data[i] = lazyUpdate[k]; } } } // [s, t) void update(int s, int t, int x) { for (int k = 0; k < K; ++k) { int l = k * sqrtN, r = (k + 1) * sqrtN; if (r <= s || t <= l) continue; if (s <= l && r <= t) { lazyFlag[k] = true; lazyUpdate[k] = x; } else { eval(k); for (int i = max(s, l); i < min(t, r); ++i) { data[i] = x; } } } } int find(int i) { int k = i / sqrtN; eval(k); return data[i]; } }; signed main() { ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; SqrtDecomposition decomp(N); while (Q--) { int c; cin >> c; if (c == 0) { int s, t, x; cin >> s >> t >> x; decomp.update(s, t + 1, x); } else { int i; cin >> i; cout << decomp.find(i) << endl; } } }
### Prompt Generate a cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define int long long const int INF = (1LL << 31) - 1; const int sqrtN = 512; struct SqrtDecomposition { int N, K; vector<int> data; vector<bool> lazyFlag; vector<int> lazyUpdate; SqrtDecomposition(int n) : N(n) { K = (N + sqrtN - 1) / sqrtN; data.assign(K * sqrtN, INF); lazyFlag.assign(K, false); lazyUpdate.assign(K, 0); } void eval(int k) { if (lazyFlag[k]) { lazyFlag[k] = false; for (int i = k * sqrtN; i < (k + 1) * sqrtN; ++i) { data[i] = lazyUpdate[k]; } } } // [s, t) void update(int s, int t, int x) { for (int k = 0; k < K; ++k) { int l = k * sqrtN, r = (k + 1) * sqrtN; if (r <= s || t <= l) continue; if (s <= l && r <= t) { lazyFlag[k] = true; lazyUpdate[k] = x; } else { eval(k); for (int i = max(s, l); i < min(t, r); ++i) { data[i] = x; } } } } int find(int i) { int k = i / sqrtN; eval(k); return data[i]; } }; signed main() { ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; SqrtDecomposition decomp(N); while (Q--) { int c; cin >> c; if (c == 0) { int s, t, x; cin >> s >> t >> x; decomp.update(s, t + 1, x); } else { int i; cin >> i; cout << decomp.find(i) << endl; } } } ```
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000001 #define NUM 2147483647 int N = 1; int* value; void init(int first_N){ while(N < first_N)N *= 2; } //update(s, t, value, 0, 0, N-1)でas,as+1,...,atの値をxに変更する void update(int update_left,int update_right,int new_value,int node_id,int node_left,int node_right){ if(update_right < node_left || update_left > node_right)return; else if(update_left <= node_left && update_right >= node_right){ value[node_id] = new_value; }else{ if(value[node_id] >= 0){ value[2*node_id+1] = value[node_id]; value[2*node_id+2] = value[node_id]; value[node_id] = -1; } update(update_left,update_right,new_value,2*node_id+1,node_left,(node_left+node_right)/2); update(update_left,update_right,new_value,2*node_id+2,(node_left+node_right)/2+1,node_right); } } //query(loc, loc, 0, 0, N-1)でalocの値を出す(sumもあるかもしれないが要検討) int query(int search_left,int search_right,int node_id,int node_left,int node_right){ if(search_right < node_left || search_left > node_right){ return -1; }else if(node_left <= search_left && node_right >= search_right && value[node_id] >= 0){ return value[node_id]; }else{ int left = query(search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2); int right = query(search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right); return max(left,right); } } //問題は0-indexedで渡されている int main(){ int n, q; cin >> n >> q; init(n); value = new int[270000]; for(int i = 0; i <= 2*N-2; i++) value[i] = NUM; int command, left, right, value, loc; for(int i = 0; i < q; i++){ cin >> command; if(command == 0){ cin >> left >> right >> value; update(left,right,value,0,0,N-1); }else{ cin >> loc; cout << query(loc, loc, 0, 0, N-1) << endl; } } }
### Prompt In CPP, your task is to solve the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long int ll; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000001 #define NUM 2147483647 int N = 1; int* value; void init(int first_N){ while(N < first_N)N *= 2; } //update(s, t, value, 0, 0, N-1)でas,as+1,...,atの値をxに変更する void update(int update_left,int update_right,int new_value,int node_id,int node_left,int node_right){ if(update_right < node_left || update_left > node_right)return; else if(update_left <= node_left && update_right >= node_right){ value[node_id] = new_value; }else{ if(value[node_id] >= 0){ value[2*node_id+1] = value[node_id]; value[2*node_id+2] = value[node_id]; value[node_id] = -1; } update(update_left,update_right,new_value,2*node_id+1,node_left,(node_left+node_right)/2); update(update_left,update_right,new_value,2*node_id+2,(node_left+node_right)/2+1,node_right); } } //query(loc, loc, 0, 0, N-1)でalocの値を出す(sumもあるかもしれないが要検討) int query(int search_left,int search_right,int node_id,int node_left,int node_right){ if(search_right < node_left || search_left > node_right){ return -1; }else if(node_left <= search_left && node_right >= search_right && value[node_id] >= 0){ return value[node_id]; }else{ int left = query(search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2); int right = query(search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right); return max(left,right); } } //問題は0-indexedで渡されている int main(){ int n, q; cin >> n >> q; init(n); value = new int[270000]; for(int i = 0; i <= 2*N-2; i++) value[i] = NUM; int command, left, right, value, loc; for(int i = 0; i < q; i++){ cin >> command; if(command == 0){ cin >> left >> right >> value; update(left,right,value,0,0,N-1); }else{ cin >> loc; cout << query(loc, loc, 0, 0, N-1) << endl; } } } ```
#include <iostream> #include <vector> using namespace std; template<typename T> class RangeUpdateQuery { public: explicit RangeUpdateQuery(int n, T def) : N(calcN_(n)) { mVal.assign(2*N+1, make_pair(-1, def)); } void update(int l, int r, T value, int ts){ updateImpl_(l, r, make_pair(ts, value), 0, 0, N); } T get(int idx){ int i = N + idx - 1; auto res = mVal[i]; while(i > 0){ i = (i-1)/2; res = max(mVal[i], res); } return res.second; } void debug() { for(auto t: mVal) cout << t.first<<","<<t.second << " "; cout << endl; } private: int calcN_(int n){ int res = 1; while(res < n) res *= 2; return res; } void updateImpl_(int l, int r, pair<int, T> value, int idx, int rangeL, int rangeR){ if(r <= rangeL || rangeR <= l) return; if(l <= rangeL && rangeR <= r){ mVal[idx] = value; } else { int rangeM = (rangeL+rangeR)/2; updateImpl_(l, r, value, 2*idx+1, rangeL, rangeM); updateImpl_(l, r, value, 2*idx+2, rangeM, rangeR); } } const int N; vector<pair<int,T>> mVal; }; int main(){ int n, q; while(cin >> n >> q){ const int INF = 0x7FFFFFFF; RangeUpdateQuery<int> ruq(n, INF); for(int i=0;i<q;i++){ int c; cin >> c; if(c == 0){ int s, t, x; cin >> s >> t >> x; ruq.update(s, t+1, x, i); } else { int p; cin >> p; cout << ruq.get(p) << endl; } } } }
### Prompt Please provide a CPP coded solution to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> #include <vector> using namespace std; template<typename T> class RangeUpdateQuery { public: explicit RangeUpdateQuery(int n, T def) : N(calcN_(n)) { mVal.assign(2*N+1, make_pair(-1, def)); } void update(int l, int r, T value, int ts){ updateImpl_(l, r, make_pair(ts, value), 0, 0, N); } T get(int idx){ int i = N + idx - 1; auto res = mVal[i]; while(i > 0){ i = (i-1)/2; res = max(mVal[i], res); } return res.second; } void debug() { for(auto t: mVal) cout << t.first<<","<<t.second << " "; cout << endl; } private: int calcN_(int n){ int res = 1; while(res < n) res *= 2; return res; } void updateImpl_(int l, int r, pair<int, T> value, int idx, int rangeL, int rangeR){ if(r <= rangeL || rangeR <= l) return; if(l <= rangeL && rangeR <= r){ mVal[idx] = value; } else { int rangeM = (rangeL+rangeR)/2; updateImpl_(l, r, value, 2*idx+1, rangeL, rangeM); updateImpl_(l, r, value, 2*idx+2, rangeM, rangeR); } } const int N; vector<pair<int,T>> mVal; }; int main(){ int n, q; while(cin >> n >> q){ const int INF = 0x7FFFFFFF; RangeUpdateQuery<int> ruq(n, INF); for(int i=0;i<q;i++){ int c; cin >> c; if(c == 0){ int s, t, x; cin >> s >> t >> x; ruq.update(s, t+1, x, i); } else { int p; cin >> p; cout << ruq.get(p) << endl; } } } } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(c) begin(c), end(c) #define dump(x) cerr << __LINE__ << ":\t" #x " = " << (x) << endl struct segtree { vector<int> lazy, dat; int n; segtree(int n_, int init){ n = 1; while(n < n_) n *= 2; dat.assign(n*2, init); lazy.assign(n*2, -1); } void set(int l, int r, int x){ static queue<tuple<int,int,int>> q; q.emplace(0, n, 1); while(q.size()){ int segl, segr, n; tie(segl, segr, n) = q.front(); q.pop(); push(segl, segr, n); if(segr <= l || r <= segl); else if(l <= segl && segr <= r) lazy[n] = x; else { int segm = (segl + segr) / 2; q.emplace(segl, segm, n*2); q.emplace(segm, segr, n*2+1); } } } int get(int k){ return get(k, 0, n, 1); } int get(int k, int segl, int segr, int n){ push(segl, segr, n); if(segl + 1 == segr) return dat[n]; int segm = (segl + segr) / 2; if(k < segm) return get(k, segl, segm, n*2); else return get(k, segm, segr, n*2+1); } void push(int segl, int segr, int node){ if(lazy[node] != -1){ dat[node] = lazy[node]; if(segl + 1 != segr){ lazy[node*2] = lazy[node]; lazy[node*2+1] = lazy[node]; } lazy[node] = -1; } } }; int main(){ cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; segtree st(n, 2147483647); for(int iq = 0; iq < q; ++iq){ int t; cin >> t; // dump(t); if(t == 0){ int s, t, x; cin >> s >> t >> x; ++t; st.set(s, t, x); } else { int i; cin >> i; cout << st.get(i) << '\n'; } } }
### Prompt Develop a solution in Cpp to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(c) begin(c), end(c) #define dump(x) cerr << __LINE__ << ":\t" #x " = " << (x) << endl struct segtree { vector<int> lazy, dat; int n; segtree(int n_, int init){ n = 1; while(n < n_) n *= 2; dat.assign(n*2, init); lazy.assign(n*2, -1); } void set(int l, int r, int x){ static queue<tuple<int,int,int>> q; q.emplace(0, n, 1); while(q.size()){ int segl, segr, n; tie(segl, segr, n) = q.front(); q.pop(); push(segl, segr, n); if(segr <= l || r <= segl); else if(l <= segl && segr <= r) lazy[n] = x; else { int segm = (segl + segr) / 2; q.emplace(segl, segm, n*2); q.emplace(segm, segr, n*2+1); } } } int get(int k){ return get(k, 0, n, 1); } int get(int k, int segl, int segr, int n){ push(segl, segr, n); if(segl + 1 == segr) return dat[n]; int segm = (segl + segr) / 2; if(k < segm) return get(k, segl, segm, n*2); else return get(k, segm, segr, n*2+1); } void push(int segl, int segr, int node){ if(lazy[node] != -1){ dat[node] = lazy[node]; if(segl + 1 != segr){ lazy[node*2] = lazy[node]; lazy[node*2+1] = lazy[node]; } lazy[node] = -1; } } }; int main(){ cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; segtree st(n, 2147483647); for(int iq = 0; iq < q; ++iq){ int t; cin >> t; // dump(t); if(t == 0){ int s, t, x; cin >> s >> t >> x; ++t; st.set(s, t, x); } else { int i; cin >> i; cout << st.get(i) << '\n'; } } } ```
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) #define rrep(i,n) for(int i=1;i<=n;i++) #define drep(i,n) for(int i=n;i>=0;i--) #define MAX 100000 #define ll long long #define dmp make_pair #define dpb push_back #define P pair<int,int> #define fi first #define se second using namespace std; //__gcd(a,b), __builtin_popcount(a); const int INF = 2147483647; #define B 100 int bucket[MAX/B][B]; int data[MAX/B]; void update(int a, int b, int x){ int f1 = 1, f2 = 1; while(a <= b && a%B != 0){ if(f1 && data[a/B] != -1){ for(int i = 0;i < B;i++)bucket[a/B][i] = data[a/B]; f1 = 0;data[a/B] = -1; } bucket[a/B][a%B] = x; a++; } while(a <= b && b%B != B-1){ if(f2 && data[b/B] != -1){ for(int i = 0;i < B;i++)bucket[b/B][i] = data[b/B]; f2 = 0;data[b/B] = -1; } bucket[b/B][b%B] = x; b--; } while(a < b){ data[a/B] = x; a += B; } } int find(int x){ if(data[x/B] != -1){ for(int i = 0;i < B;i++)bucket[x/B][i] = data[x/B]; data[x/B] = -1; } return bucket[x/B][x%B]; } int main(){ int n, q, c, s, t, x, ans; scanf("%d%d", &n, &q); fill(data, data+MAX/B, -1); fill((int*)bucket, (int*)(bucket+MAX/B), INF); while(q--){ scanf("%d", &c); if(!c){ scanf("%d%d%d", &s, &t, &x); update(s, t, x); }else{ scanf("%d", &x); ans = find(x); printf("%d\n", ans); } } //rep(i,n)printf("%d ", bucket[0][i]); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) #define rrep(i,n) for(int i=1;i<=n;i++) #define drep(i,n) for(int i=n;i>=0;i--) #define MAX 100000 #define ll long long #define dmp make_pair #define dpb push_back #define P pair<int,int> #define fi first #define se second using namespace std; //__gcd(a,b), __builtin_popcount(a); const int INF = 2147483647; #define B 100 int bucket[MAX/B][B]; int data[MAX/B]; void update(int a, int b, int x){ int f1 = 1, f2 = 1; while(a <= b && a%B != 0){ if(f1 && data[a/B] != -1){ for(int i = 0;i < B;i++)bucket[a/B][i] = data[a/B]; f1 = 0;data[a/B] = -1; } bucket[a/B][a%B] = x; a++; } while(a <= b && b%B != B-1){ if(f2 && data[b/B] != -1){ for(int i = 0;i < B;i++)bucket[b/B][i] = data[b/B]; f2 = 0;data[b/B] = -1; } bucket[b/B][b%B] = x; b--; } while(a < b){ data[a/B] = x; a += B; } } int find(int x){ if(data[x/B] != -1){ for(int i = 0;i < B;i++)bucket[x/B][i] = data[x/B]; data[x/B] = -1; } return bucket[x/B][x%B]; } int main(){ int n, q, c, s, t, x, ans; scanf("%d%d", &n, &q); fill(data, data+MAX/B, -1); fill((int*)bucket, (int*)(bucket+MAX/B), INF); while(q--){ scanf("%d", &c); if(!c){ scanf("%d%d%d", &s, &t, &x); update(s, t, x); }else{ scanf("%d", &x); ans = find(x); printf("%d\n", ans); } } //rep(i,n)printf("%d ", bucket[0][i]); return 0; } ```
#include<iostream> using namespace std; typedef long long Int; #define SEG_LEN (1 << 18) int seg[SEG_LEN * 2]; Int update_val[1100000]; Int get(int ind){ ind += SEG_LEN; int last_update = seg[ind]; while(true){ ind /= 2; if(ind == 0)break; last_update = max(last_update, seg[ind]); } return update_val[last_update]; } void update(int l, int r, int time, Int val){ update_val[time] = val; l += SEG_LEN; r += SEG_LEN; int ans = 0; while(l < r){ if(l % 2 == 1){ seg[l] = time; l++; } l /= 2; if(r % 2 == 1){ seg[r-1] = time; ans += seg[r-1]; r--; } r /= 2; } } int main(){ int n, q, s, t, x, ind, com; cin >> n >> q; update(0, n, 0, (1LL<<31) - 1LL); for(int i = 0;i < q;i++){ cin >> com; if(com == 0){//add cin >> s >> t >> x; update(s, t+1, i+1, x); } if(com == 1){//get sum cin >> ind; cout << get(ind) << endl; } } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<iostream> using namespace std; typedef long long Int; #define SEG_LEN (1 << 18) int seg[SEG_LEN * 2]; Int update_val[1100000]; Int get(int ind){ ind += SEG_LEN; int last_update = seg[ind]; while(true){ ind /= 2; if(ind == 0)break; last_update = max(last_update, seg[ind]); } return update_val[last_update]; } void update(int l, int r, int time, Int val){ update_val[time] = val; l += SEG_LEN; r += SEG_LEN; int ans = 0; while(l < r){ if(l % 2 == 1){ seg[l] = time; l++; } l /= 2; if(r % 2 == 1){ seg[r-1] = time; ans += seg[r-1]; r--; } r /= 2; } } int main(){ int n, q, s, t, x, ind, com; cin >> n >> q; update(0, n, 0, (1LL<<31) - 1LL); for(int i = 0;i < q;i++){ cin >> com; if(com == 0){//add cin >> s >> t >> x; update(s, t+1, i+1, x); } if(com == 1){//get sum cin >> ind; cout << get(ind) << endl; } } return 0; } ```
#include <bits/stdc++.h> #define int long long #define endl '\n' #define FOR(i, a, n) for (int i = (a); i < (n); ++i) #define REP(i, n) FOR(i, 0, n) using namespace std; // Dual Segment Tree template <class OM> struct DST { int n, lev; vector<OM> lz; virtual OM h(OM, OM) = 0; const OM e0; DST(int n_, const OM& e0) : e0(e0) { for (n = 1, lev = 0; n < n_; n <<= 1, ++lev); lz.assign(n << 1, e0); } inline void push(int k) { for (int i = lev; i > 0; --i) { int j = k >> i; if (lz[j] == e0) continue; lz[(j << 1) | 0] = h(lz[(j << 1) | 0], lz[j]); lz[(j << 1) | 1] = h(lz[(j << 1) | 1], lz[j]); lz[j] = e0; } } // [l, r) // l, r: 0-indexed void update(int l, int r, const OM& x) { push(l += n); push(r += n - 1); for (++r; l < r; l >>= 1, r >>= 1) { if (l & 1) lz[l] = h(lz[l], x), l++; if (r & 1) --r, lz[r] = h(lz[r], x); } } // k: 0-indexed OM query(int k) { push(k += n); return lz[k]; } }; // Range Update Query (RUQ) template <class OM> struct RUQ : DST<OM> { RUQ(int n, OM e0 = numeric_limits<OM>::max()) : DST<OM>(n, e0) {} OM h(OM a, OM b) override { return b; } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; RUQ<int> ruq(n, (1LL << 31) - 1); REP (i, q) { int c; cin >> c; if (c == 0) { int s, t, x; cin >> s >> t >> x; ruq.update(s, t + 1, x); } else { int j; cin >> j; cout << ruq.query(j) << endl; } } }
### Prompt Please provide a cpp coded solution to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> #define int long long #define endl '\n' #define FOR(i, a, n) for (int i = (a); i < (n); ++i) #define REP(i, n) FOR(i, 0, n) using namespace std; // Dual Segment Tree template <class OM> struct DST { int n, lev; vector<OM> lz; virtual OM h(OM, OM) = 0; const OM e0; DST(int n_, const OM& e0) : e0(e0) { for (n = 1, lev = 0; n < n_; n <<= 1, ++lev); lz.assign(n << 1, e0); } inline void push(int k) { for (int i = lev; i > 0; --i) { int j = k >> i; if (lz[j] == e0) continue; lz[(j << 1) | 0] = h(lz[(j << 1) | 0], lz[j]); lz[(j << 1) | 1] = h(lz[(j << 1) | 1], lz[j]); lz[j] = e0; } } // [l, r) // l, r: 0-indexed void update(int l, int r, const OM& x) { push(l += n); push(r += n - 1); for (++r; l < r; l >>= 1, r >>= 1) { if (l & 1) lz[l] = h(lz[l], x), l++; if (r & 1) --r, lz[r] = h(lz[r], x); } } // k: 0-indexed OM query(int k) { push(k += n); return lz[k]; } }; // Range Update Query (RUQ) template <class OM> struct RUQ : DST<OM> { RUQ(int n, OM e0 = numeric_limits<OM>::max()) : DST<OM>(n, e0) {} OM h(OM a, OM b) override { return b; } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; RUQ<int> ruq(n, (1LL << 31) - 1); REP (i, q) { int c; cin >> c; if (c == 0) { int s, t, x; cin >> s >> t >> x; ruq.update(s, t + 1, x); } else { int j; cin >> j; cout << ruq.query(j) << endl; } } } ```
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<vector> #include<climits> #define MAX_N 1000001 using namespace std; const int INF = (1LL<<31)-1; int sqrtN; int N,K; vector<int> data; vector<bool> lazyflag; vector<int> lazyupdate; void init(){ K=(N+sqrtN-1)/sqrtN; data.assign(K*sqrtN,INF); lazyflag.assign(K,false); lazyupdate.assign(K,0); } void eval(int k){ if(lazyflag[k]){ lazyflag[k]=false; for(int i=k*sqrtN;i<(k+1)*sqrtN;i++){ data[i]=lazyupdate[k]; } } } void update(int s, int t, int x){ for(int k=0;k<K;k++){ int l=k*sqrtN; int r=(k+1)*sqrtN; if(r<=s || t<=l) continue; if(s<=l && r<=t){ lazyflag[k]=true; lazyupdate[k]=x; } else{ eval(k); for(int i=max(s,l);i<min(t,r);i++){ data[i]=x; } } } } void find_i(int i){ int k=i/sqrtN; eval(k); printf("%d\n",data[i]); } int main(){ int q,com; int s,t,x; scanf("%d %d",&N,&q); sqrtN=(int)sqrt(N); init(); for(int i=0;i<q;i++){ scanf("%d",&com); if(com==0){ scanf("%d %d %d",&s,&t,&x); update(s,t+1,x);//[s,t) } else{ scanf("%d",&x); find_i(x); } } }
### Prompt In CPP, your task is to solve the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<vector> #include<climits> #define MAX_N 1000001 using namespace std; const int INF = (1LL<<31)-1; int sqrtN; int N,K; vector<int> data; vector<bool> lazyflag; vector<int> lazyupdate; void init(){ K=(N+sqrtN-1)/sqrtN; data.assign(K*sqrtN,INF); lazyflag.assign(K,false); lazyupdate.assign(K,0); } void eval(int k){ if(lazyflag[k]){ lazyflag[k]=false; for(int i=k*sqrtN;i<(k+1)*sqrtN;i++){ data[i]=lazyupdate[k]; } } } void update(int s, int t, int x){ for(int k=0;k<K;k++){ int l=k*sqrtN; int r=(k+1)*sqrtN; if(r<=s || t<=l) continue; if(s<=l && r<=t){ lazyflag[k]=true; lazyupdate[k]=x; } else{ eval(k); for(int i=max(s,l);i<min(t,r);i++){ data[i]=x; } } } } void find_i(int i){ int k=i/sqrtN; eval(k); printf("%d\n",data[i]); } int main(){ int q,com; int s,t,x; scanf("%d %d",&N,&q); sqrtN=(int)sqrt(N); init(); for(int i=0;i<q;i++){ scanf("%d",&com); if(com==0){ scanf("%d %d %d",&s,&t,&x); update(s,t+1,x);//[s,t) } else{ scanf("%d",&x); find_i(x); } } } ```
#include <bits/stdc++.h> using namespace std; #define int long long #define all(V) (V).begin(),(V).end() #define umap unordered_map #define uset unordered_set #define br cout<<"\n"; const int MAXN=1e5+10, MOD=1e9+7; int n,m; int arr[MAXN]; int t[5*MAXN],lazy[5*MAXN]; int doit(int a,int b){ return a+b; } void build(int start,int end,int index=1){ if(start==end){ t[index]=INT_MAX; return ; } int mid = start+end >> 1; build(start,mid,index << 1); build(mid+1,end,index<<1|1); t[index] = doit(t[index<<1], t[index<<1|1]); } void propagate(int start,int end,int index){ t[index] = (end-start+1)*lazy[index]; // bcz leaf not hv childs if(start!=end){ lazy[index<<1] = lazy[index]; lazy[index<<1|1] = lazy[index]; } lazy[index]=-1; } int Query(int start,int end,int l,int r,int index=1){ if(lazy[index]!=-1){ propagate(start,end,index); } // out range if(r<start or end<l){ return 0; } // in Raange if( l<=start and end<=r){ return t[index]; } int mid = start+end >>1; return doit(Query(start,mid,l,r,index<<1), Query(mid+1,end,l,r,index<<1|1)); } void pUpdate(int start,int end,int point,int val,int index=1){ if(start==end){ t[index]=val; return ; } int mid = start+end>>1; if(point <= mid){ pUpdate(start,mid,point,val,index<<1); } else{ pUpdate(mid+1,end,point,val,index<<1|1); } t[index] = doit(t[index << 1] , t[index << 1 | 1]); } void rangeUpdate(int start,int end,int l,int r,int val,int index){ if(lazy[index]!= -1){ propagate(start,end,index); } if(l>end or r<start){ return; } // in range if(l<=start and end<=r){ lazy[index]=val; propagate(start,end,index); return; } int mid = start+end>>1; rangeUpdate(start,mid,l,r,val,index<<1); rangeUpdate(mid+1,end,l,r,val,index<<1|1); t[index] = doit(t[index<<1],t[index<<1|1]); } signed main() { // #ifndef ONLINE_JUDGE // freopen("./input.txt","r",stdin); // freopen("./output.txt","w",stdout); // #endif ios::sync_with_stdio(false); cin.tie(nullptr); cin>>n>>m; memset(lazy,-1,sizeof lazy); build(0,n-1); for(int i=1; i<=m; i++){ int q; cin>>q; if(q==1){ int t; cin>>t; cout<<Query(0,n-1,t,t,1); br; } else{ int s,t,x; cin>>s>>t>>x; rangeUpdate(0,n-1,s,t,x,1); } } }
### Prompt Your challenge is to write a CPP solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define int long long #define all(V) (V).begin(),(V).end() #define umap unordered_map #define uset unordered_set #define br cout<<"\n"; const int MAXN=1e5+10, MOD=1e9+7; int n,m; int arr[MAXN]; int t[5*MAXN],lazy[5*MAXN]; int doit(int a,int b){ return a+b; } void build(int start,int end,int index=1){ if(start==end){ t[index]=INT_MAX; return ; } int mid = start+end >> 1; build(start,mid,index << 1); build(mid+1,end,index<<1|1); t[index] = doit(t[index<<1], t[index<<1|1]); } void propagate(int start,int end,int index){ t[index] = (end-start+1)*lazy[index]; // bcz leaf not hv childs if(start!=end){ lazy[index<<1] = lazy[index]; lazy[index<<1|1] = lazy[index]; } lazy[index]=-1; } int Query(int start,int end,int l,int r,int index=1){ if(lazy[index]!=-1){ propagate(start,end,index); } // out range if(r<start or end<l){ return 0; } // in Raange if( l<=start and end<=r){ return t[index]; } int mid = start+end >>1; return doit(Query(start,mid,l,r,index<<1), Query(mid+1,end,l,r,index<<1|1)); } void pUpdate(int start,int end,int point,int val,int index=1){ if(start==end){ t[index]=val; return ; } int mid = start+end>>1; if(point <= mid){ pUpdate(start,mid,point,val,index<<1); } else{ pUpdate(mid+1,end,point,val,index<<1|1); } t[index] = doit(t[index << 1] , t[index << 1 | 1]); } void rangeUpdate(int start,int end,int l,int r,int val,int index){ if(lazy[index]!= -1){ propagate(start,end,index); } if(l>end or r<start){ return; } // in range if(l<=start and end<=r){ lazy[index]=val; propagate(start,end,index); return; } int mid = start+end>>1; rangeUpdate(start,mid,l,r,val,index<<1); rangeUpdate(mid+1,end,l,r,val,index<<1|1); t[index] = doit(t[index<<1],t[index<<1|1]); } signed main() { // #ifndef ONLINE_JUDGE // freopen("./input.txt","r",stdin); // freopen("./output.txt","w",stdout); // #endif ios::sync_with_stdio(false); cin.tie(nullptr); cin>>n>>m; memset(lazy,-1,sizeof lazy); build(0,n-1); for(int i=1; i<=m; i++){ int q; cin>>q; if(q==1){ int t; cin>>t; cout<<Query(0,n-1,t,t,1); br; } else{ int s,t,x; cin>>s>>t>>x; rangeUpdate(0,n-1,s,t,x,1); } } } ```
#include<iostream> #include<algorithm> #include<map> using namespace std; #define SEG_NUM (1<<20) pair<long, long> seg[SEG_NUM*2]; long find(int i){ i += SEG_NUM; long ans = 2147483647; int time = 0; while(i > 0){ if(time < seg[i].first) { ans = seg[i].second; time = seg[i].first; } i /= 2; } return ans; } void update(int s, int t, long x, long time){ s += SEG_NUM; t += SEG_NUM; while(s < t){ if(s % 2 == 1){ seg[s].first = time; seg[s].second = x; s++; } s /= 2; if(t % 2 == 1){ seg[t-1].first = time; seg[t-1].second = x; t--; } t /= 2; } } int main() { for(long i=0;i<2*SEG_NUM;++i){ seg[i] = make_pair(0, 2147483647); } int n, q; cin>>n>>q; long query, s, t, x, i; for(int time=0;time<q;++time){ cin>>query; if(query){ cin>>i; cout<<find(i)<<endl; } else { cin>>s>>t>>x; update(s, t+1, x, time+1); } } }
### Prompt Please provide a CPP coded solution to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<iostream> #include<algorithm> #include<map> using namespace std; #define SEG_NUM (1<<20) pair<long, long> seg[SEG_NUM*2]; long find(int i){ i += SEG_NUM; long ans = 2147483647; int time = 0; while(i > 0){ if(time < seg[i].first) { ans = seg[i].second; time = seg[i].first; } i /= 2; } return ans; } void update(int s, int t, long x, long time){ s += SEG_NUM; t += SEG_NUM; while(s < t){ if(s % 2 == 1){ seg[s].first = time; seg[s].second = x; s++; } s /= 2; if(t % 2 == 1){ seg[t-1].first = time; seg[t-1].second = x; t--; } t /= 2; } } int main() { for(long i=0;i<2*SEG_NUM;++i){ seg[i] = make_pair(0, 2147483647); } int n, q; cin>>n>>q; long query, s, t, x, i; for(int time=0;time<q;++time){ cin>>query; if(query){ cin>>i; cout<<find(i)<<endl; } else { cin>>s>>t>>x; update(s, t+1, x, time+1); } } } ```
#include <bits/stdc++.h> #define mod 1000000007 #define sp ' ' #define intmax 2147483647 #define llmax 9223372036854775807 #define nyan "(=^・ω・^=)" #define mkp make_pair #define mkt make_tuple #define lP pair<ll, ll> #define iP pair<int,int> typedef long long ll; using namespace std; int n, q, s, t, x, u; iP a[1<<18]; bool b; int update(int s, int t, int x, int k, int l, int r, int i) { if (t < l || r < s)return 0; else if (s <= l&&r <= t) { a[k] = mkp(x, i); return 0; } else { update(s, t, x, k * 2, l, (l + r) / 2, i); update(s, t, x, k * 2 + 1, (l + r) / 2 + 1, r, i); return 0; } } int find(int x) { int y, z = -2; x += u; while (x) { if (z < a[x].second) { y = a[x].first; z = a[x].second; } x /= 2; } return y; } int main(){ cin >> n >> q; for (int i = 1;; ++i) { if ((1 << i) / n >= 2) { u = (1 << i - 1); break; } } for (int i = 1; i != u * 2; ++i) a[i] = mkp(intmax, -1); for (int i = 0; i != q; ++i) { cin >> b >> s; if (b) { cout << find(s) << endl; } else { cin >> t >> x; update(s, t, x, 1, 0, u - 1, i); } } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> #define mod 1000000007 #define sp ' ' #define intmax 2147483647 #define llmax 9223372036854775807 #define nyan "(=^・ω・^=)" #define mkp make_pair #define mkt make_tuple #define lP pair<ll, ll> #define iP pair<int,int> typedef long long ll; using namespace std; int n, q, s, t, x, u; iP a[1<<18]; bool b; int update(int s, int t, int x, int k, int l, int r, int i) { if (t < l || r < s)return 0; else if (s <= l&&r <= t) { a[k] = mkp(x, i); return 0; } else { update(s, t, x, k * 2, l, (l + r) / 2, i); update(s, t, x, k * 2 + 1, (l + r) / 2 + 1, r, i); return 0; } } int find(int x) { int y, z = -2; x += u; while (x) { if (z < a[x].second) { y = a[x].first; z = a[x].second; } x /= 2; } return y; } int main(){ cin >> n >> q; for (int i = 1;; ++i) { if ((1 << i) / n >= 2) { u = (1 << i - 1); break; } } for (int i = 1; i != u * 2; ++i) a[i] = mkp(intmax, -1); for (int i = 0; i != q; ++i) { cin >> b >> s; if (b) { cout << find(s) << endl; } else { cin >> t >> x; update(s, t, x, 1, 0, u - 1, i); } } return 0; } ```
#include <algorithm> #include <cassert> #include <iostream> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; struct Lonoid { int64_t x; Lonoid() {} Lonoid(int64_t x) : x(x) {} static Lonoid ident() { return Lonoid(-1); } static Lonoid multiply(Lonoid f, Lonoid g) { if (f == Lonoid::ident()) return g; return f; } bool operator==(const Lonoid &rhs) const { return x == rhs.x; } }; template <typename L> struct SegmentTree { int n; L e; vector<L> laz; SegmentTree(int n0) { n = 1; e = L::ident(); while (n < n0) { n *= 2; } laz.resize(n * 2 - 1, e); } void _update(int i, L f) { laz[i] = L::multiply(f, laz[i]); } void update(int ql, int qr, L f) { update(ql, qr, f, 0, 0, n); } void update(int ql, int qr, L f, int i, int il, int ir) { if (qr <= il or ir <= ql) return; if (ql <= il and ir <= qr) { _update(i, f); } else { auto m = (il + ir) / 2, lch = i * 2 + 1, rch = i * 2 + 2; _update(lch, laz[i]); _update(rch, laz[i]); laz[i] = e; update(ql, qr, f, lch, il, m); update(ql, qr, f, rch, m, ir); } } L composite(int i) { auto f = L::ident(); for (int k = i + n - 1; k > 0; k = (k - 1) / 2) { f = L::multiply(laz[k], f); } return L::multiply(laz[0], f); } }; int main() { int n, q; cin >> n >> q; SegmentTree<Lonoid> seg(n); while (q--) { int o; cin >> o; if (o == 0) { int s, t; int64_t x; cin >> s >> t >> x; seg.update(s, t + 1, Lonoid(x)); } else { int i; cin >> i; auto ans = seg.composite(i).x; if (ans < 0) ans = (1LL << 31) - 1; cout << ans << endl; } } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <algorithm> #include <cassert> #include <iostream> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; struct Lonoid { int64_t x; Lonoid() {} Lonoid(int64_t x) : x(x) {} static Lonoid ident() { return Lonoid(-1); } static Lonoid multiply(Lonoid f, Lonoid g) { if (f == Lonoid::ident()) return g; return f; } bool operator==(const Lonoid &rhs) const { return x == rhs.x; } }; template <typename L> struct SegmentTree { int n; L e; vector<L> laz; SegmentTree(int n0) { n = 1; e = L::ident(); while (n < n0) { n *= 2; } laz.resize(n * 2 - 1, e); } void _update(int i, L f) { laz[i] = L::multiply(f, laz[i]); } void update(int ql, int qr, L f) { update(ql, qr, f, 0, 0, n); } void update(int ql, int qr, L f, int i, int il, int ir) { if (qr <= il or ir <= ql) return; if (ql <= il and ir <= qr) { _update(i, f); } else { auto m = (il + ir) / 2, lch = i * 2 + 1, rch = i * 2 + 2; _update(lch, laz[i]); _update(rch, laz[i]); laz[i] = e; update(ql, qr, f, lch, il, m); update(ql, qr, f, rch, m, ir); } } L composite(int i) { auto f = L::ident(); for (int k = i + n - 1; k > 0; k = (k - 1) / 2) { f = L::multiply(laz[k], f); } return L::multiply(laz[0], f); } }; int main() { int n, q; cin >> n >> q; SegmentTree<Lonoid> seg(n); while (q--) { int o; cin >> o; if (o == 0) { int s, t; int64_t x; cin >> s >> t >> x; seg.update(s, t + 1, Lonoid(x)); } else { int i; cin >> i; auto ans = seg.composite(i).x; if (ans < 0) ans = (1LL << 31) - 1; cout << ans << endl; } } return 0; } ```
#include "bits/stdc++.h" using namespace std; class SegmentTree { private: long long Size; pair<long long, long long> Unit; vector<pair<long long, long long> > Node; public: SegmentTree(long long N, pair<long long, long long> X) : Unit(X) { Size = 1; while (Size < N) Size *= 2; Node.assign(Size * 2, Unit); } void update(long long L, long long R, long long T, long long X) { long long NOWS = Size; while (L < R) { if (L & 1) { Node[L + NOWS] = { T, X }; L++; } if (R & 1) { Node[R + NOWS - 1] = { T, X }; R--; } L >>= 1, R >>= 1, NOWS >>= 1; } } long long query(long long X) { long long MAXT = Unit.first, ANS = Unit.second; long long NOWS = Size; while (NOWS > 0) { if (MAXT < Node[X + NOWS].first) { MAXT = Node[X + NOWS].first; ANS = Node[X + NOWS].second; } if (X & 1) X--; X >>= 1, NOWS >>= 1; } return ANS; } }; int main() { long long N, Q; cin >> N >> Q; SegmentTree Seg(N, { -1, (1LL << 31) - 1 }); for (int i = 0; i < Q; i++) { long long COM; cin >> COM; if (COM == 0) { long long S, T, X; cin >> S >> T >> X; Seg.update(S, T + 1, i, X); } else { long long X; cin >> X; cout << Seg.query(X) << endl; } } }
### Prompt Construct a cpp code solution to the problem outlined: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include "bits/stdc++.h" using namespace std; class SegmentTree { private: long long Size; pair<long long, long long> Unit; vector<pair<long long, long long> > Node; public: SegmentTree(long long N, pair<long long, long long> X) : Unit(X) { Size = 1; while (Size < N) Size *= 2; Node.assign(Size * 2, Unit); } void update(long long L, long long R, long long T, long long X) { long long NOWS = Size; while (L < R) { if (L & 1) { Node[L + NOWS] = { T, X }; L++; } if (R & 1) { Node[R + NOWS - 1] = { T, X }; R--; } L >>= 1, R >>= 1, NOWS >>= 1; } } long long query(long long X) { long long MAXT = Unit.first, ANS = Unit.second; long long NOWS = Size; while (NOWS > 0) { if (MAXT < Node[X + NOWS].first) { MAXT = Node[X + NOWS].first; ANS = Node[X + NOWS].second; } if (X & 1) X--; X >>= 1, NOWS >>= 1; } return ANS; } }; int main() { long long N, Q; cin >> N >> Q; SegmentTree Seg(N, { -1, (1LL << 31) - 1 }); for (int i = 0; i < Q; i++) { long long COM; cin >> COM; if (COM == 0) { long long S, T, X; cin >> S >> T >> X; Seg.update(S, T + 1, i, X); } else { long long X; cin >> X; cout << Seg.query(X) << endl; } } } ```
#include<iostream> #include<string> #include<algorithm> #include<cmath> #include<vector> #include<stack> #include<climits> #include<cstring> #include<queue> using namespace std; typedef unsigned long long ull; const ull INF = ULLONG_MAX/3; void latentEvaluation(int ss, vector<ull> &xList, vector<ull> &sxList){ int sn = sxList.size(); if(sxList[ss] != INF){ for(int i=0;i<sn;i++){ xList[sn*ss+i] = sxList[ss]; } sxList[ss] = INF; } } void update(int s, int t, ull x, vector<ull> &xList, vector<ull> &sxList){ int nn = xList.size(); int sn = sxList.size(); int ss = s/sn; int tt = t/sn; if(ss == tt){ latentEvaluation(ss, xList, sxList); for (int i=0;i<t-s+1;i++){ xList[s+i] = x; } }else{ latentEvaluation(ss, xList, sxList); for(int i=s;i<(ss+1)*sn;i++){ xList[i] = x; } latentEvaluation(tt, xList, sxList); for(int i=tt*sn;i<t+1;i++){ xList[i] = x; } for(int i=ss+1;i<tt;i++){ sxList[i] = x; } } } void find(int ind, vector<ull> &xList, vector<ull> &sxList){ int nn = xList.size(); int sn = sxList.size(); latentEvaluation(ind/sn, xList, sxList); cout << xList[ind] << endl; } int main(){ int n, q, qType, s, t,ind; ull x; cin >> n >> q; int sn = sqrt(n)+1; vector<ull> xList(sn*sn, (1ull << 31) -1); vector<ull> sxList(sn, INF); for (int i=0;i<q;i++){ cin >> qType; if (qType == 0){ cin >> s >> t >> x; update(s,t,x,xList,sxList); }else{ cin >> ind; find(ind, xList, sxList); } /* for (int i=0;i<sn*sn;i++){ cout << xList[i] << " "; } cout << endl; for (int i=0;i<sn;i++){ cout << sxList[i] << " "; } cout << endl; */ } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<iostream> #include<string> #include<algorithm> #include<cmath> #include<vector> #include<stack> #include<climits> #include<cstring> #include<queue> using namespace std; typedef unsigned long long ull; const ull INF = ULLONG_MAX/3; void latentEvaluation(int ss, vector<ull> &xList, vector<ull> &sxList){ int sn = sxList.size(); if(sxList[ss] != INF){ for(int i=0;i<sn;i++){ xList[sn*ss+i] = sxList[ss]; } sxList[ss] = INF; } } void update(int s, int t, ull x, vector<ull> &xList, vector<ull> &sxList){ int nn = xList.size(); int sn = sxList.size(); int ss = s/sn; int tt = t/sn; if(ss == tt){ latentEvaluation(ss, xList, sxList); for (int i=0;i<t-s+1;i++){ xList[s+i] = x; } }else{ latentEvaluation(ss, xList, sxList); for(int i=s;i<(ss+1)*sn;i++){ xList[i] = x; } latentEvaluation(tt, xList, sxList); for(int i=tt*sn;i<t+1;i++){ xList[i] = x; } for(int i=ss+1;i<tt;i++){ sxList[i] = x; } } } void find(int ind, vector<ull> &xList, vector<ull> &sxList){ int nn = xList.size(); int sn = sxList.size(); latentEvaluation(ind/sn, xList, sxList); cout << xList[ind] << endl; } int main(){ int n, q, qType, s, t,ind; ull x; cin >> n >> q; int sn = sqrt(n)+1; vector<ull> xList(sn*sn, (1ull << 31) -1); vector<ull> sxList(sn, INF); for (int i=0;i<q;i++){ cin >> qType; if (qType == 0){ cin >> s >> t >> x; update(s,t,x,xList,sxList); }else{ cin >> ind; find(ind, xList, sxList); } /* for (int i=0;i<sn*sn;i++){ cout << xList[i] << " "; } cout << endl; for (int i=0;i<sn;i++){ cout << sxList[i] << " "; } cout << endl; */ } return 0; } ```
#include <iostream> #include <vector> using namespace std; template<typename T> class RangeUpdateQuery { public: explicit RangeUpdateQuery(int n, T def) : N(calcN_(n)) { mVal.assign(2*N+1, make_pair(-1, def)); } void update(int l, int r, T value, int ts){ l = max(0, l); r = min(N, r); int offset = N; while(offset > 0){ if(l >= r) break; if(l&1){ mVal[offset+l-1].first = ts; mVal[offset+l-1].second = value; l++; } if(r&1){ mVal[offset+r-2].first = ts; mVal[offset+r-2].second = value; } l /= 2; r /= 2; offset /= 2; } } T get(int idx){ int i = N + idx - 1; auto res = mVal[i]; while(i > 0){ i = (i-1)/2; res = max(mVal[i], res); } return res.second; } private: int calcN_(int n){ int res = 1; while(res < n) res *= 2; return res; } const int N; vector<pair<int,T>> mVal; }; int main(){ int n, q; while(cin >> n >> q){ const int INF = 0x7FFFFFFF; RangeUpdateQuery<int> ruq(n, INF); for(int i=0;i<q;i++){ int c; cin >> c; if(c == 0){ int s, t, x; cin >> s >> t >> x; ruq.update(s, t+1, x, i); } else { int p; cin >> p; cout << ruq.get(p) << endl; } } } }
### Prompt Develop a solution in CPP to the problem described below: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> #include <vector> using namespace std; template<typename T> class RangeUpdateQuery { public: explicit RangeUpdateQuery(int n, T def) : N(calcN_(n)) { mVal.assign(2*N+1, make_pair(-1, def)); } void update(int l, int r, T value, int ts){ l = max(0, l); r = min(N, r); int offset = N; while(offset > 0){ if(l >= r) break; if(l&1){ mVal[offset+l-1].first = ts; mVal[offset+l-1].second = value; l++; } if(r&1){ mVal[offset+r-2].first = ts; mVal[offset+r-2].second = value; } l /= 2; r /= 2; offset /= 2; } } T get(int idx){ int i = N + idx - 1; auto res = mVal[i]; while(i > 0){ i = (i-1)/2; res = max(mVal[i], res); } return res.second; } private: int calcN_(int n){ int res = 1; while(res < n) res *= 2; return res; } const int N; vector<pair<int,T>> mVal; }; int main(){ int n, q; while(cin >> n >> q){ const int INF = 0x7FFFFFFF; RangeUpdateQuery<int> ruq(n, INF); for(int i=0;i<q;i++){ int c; cin >> c; if(c == 0){ int s, t, x; cin >> s >> t >> x; ruq.update(s, t+1, x, i); } else { int p; cin >> p; cout << ruq.get(p) << endl; } } } } ```
#include <bits/stdc++.h> using namespace std; #define int long long #define all(v) begin(v), end(v) #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++) #define min(...) min({__VA_ARGS__}) #define max(...) max({__VA_ARGS__}) template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;} template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;} using pint = pair<int, int>; using tint = tuple<int, int, int>; using vint = vector<int>; const int inf = (1LL<<31)-1; const int mod = 1e9 + 7; struct SegmentTree { vector<int> data, lazy; int sz; SegmentTree(int n) { sz = 1; while(sz < n) sz <<= 1; data.resize(2*sz-1, inf); lazy.resize(2*sz-1, -1); } void lazy_evaluate(int k, int l, int r) { if(~lazy[k]) { data[k] = lazy[k]; if(r - l > 1) { lazy[2*k+1] = lazy[k]; lazy[2*k+2] = lazy[k]; } lazy[k] = -1; } } void update(int a, int b, int x, int k, int l, int r) { lazy_evaluate(k, l, r); if(r <= a || b <= l) return; if(a <= l && r <= b) { lazy[k] = x; lazy_evaluate(k, l, r); return; } update(a, b, x, 2*k+1, l, (l+r)/2); update(a, b, x, 2*k+2, (l+r)/2, r); } void update(int a, int b, int x) { update(a, b, x, 0, 0, sz); } int query(int i, int k, int l, int r) { lazy_evaluate(k, l, r); if(r - l == 1) return data[k]; if(l <= i && i <= r) { if(i < (l+r)/2) return query(i, 2*k+1, l, (l+r)/2); else return query(i, 2*k+2, (l+r)/2, r); } return inf; } int query(int i) { return query(i, 0, 0, sz); } }; signed main() { cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); int n, q; cin >> n >> q; SegmentTree seg(n); rep(i, q) { int com, s, t, x; cin >> com >> s; if(com == 0) cin >> t >> x, seg.update(s, t+1, x); else cout << seg.query(s) << endl; } return 0; }
### Prompt In cpp, your task is to solve the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define int long long #define all(v) begin(v), end(v) #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++) #define min(...) min({__VA_ARGS__}) #define max(...) max({__VA_ARGS__}) template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;} template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;} using pint = pair<int, int>; using tint = tuple<int, int, int>; using vint = vector<int>; const int inf = (1LL<<31)-1; const int mod = 1e9 + 7; struct SegmentTree { vector<int> data, lazy; int sz; SegmentTree(int n) { sz = 1; while(sz < n) sz <<= 1; data.resize(2*sz-1, inf); lazy.resize(2*sz-1, -1); } void lazy_evaluate(int k, int l, int r) { if(~lazy[k]) { data[k] = lazy[k]; if(r - l > 1) { lazy[2*k+1] = lazy[k]; lazy[2*k+2] = lazy[k]; } lazy[k] = -1; } } void update(int a, int b, int x, int k, int l, int r) { lazy_evaluate(k, l, r); if(r <= a || b <= l) return; if(a <= l && r <= b) { lazy[k] = x; lazy_evaluate(k, l, r); return; } update(a, b, x, 2*k+1, l, (l+r)/2); update(a, b, x, 2*k+2, (l+r)/2, r); } void update(int a, int b, int x) { update(a, b, x, 0, 0, sz); } int query(int i, int k, int l, int r) { lazy_evaluate(k, l, r); if(r - l == 1) return data[k]; if(l <= i && i <= r) { if(i < (l+r)/2) return query(i, 2*k+1, l, (l+r)/2); else return query(i, 2*k+2, (l+r)/2, r); } return inf; } int query(int i) { return query(i, 0, 0, sz); } }; signed main() { cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); int n, q; cin >> n >> q; SegmentTree seg(n); rep(i, q) { int com, s, t, x; cin >> com >> s; if(com == 0) cin >> t >> x, seg.update(s, t+1, x); else cout << seg.query(s) << endl; } return 0; } ```
#include <iostream> #include <vector> const int MAX = 2147483647; int min(int a, int b) { return a < b ? a : b; } int getSegTreeSize(int len) { if (len == 0) return 0; int ret, cnt = 0, memo = 1; while (memo < len) { memo *= 2; cnt++; } ret = 1 << (cnt + 1); return ret; } class RUQ { private: int size, length; std::vector<int> node; RUQ() {} int find(int return_index, int index, int left, int right) { if (node[index] != -1) return node[index]; if ((left + right) / 2 < return_index) return find(return_index, index * 2 + 1, (left + right) / 2 + 1, right); return find(return_index, index * 2, left, (left + right) / 2); } void update(int left, int right, int element, int index, int i_left, int i_right) { if (element == node[index]) return; if (left <= i_left && i_right <= right) { node[index] = element; return; } if (node[index] != -1) { node[index * 2] = node[index]; node[index * 2 + 1] = node[index]; node[index] = -1; } int middle = (i_left + i_right) / 2; if (middle >= left) update(left, right, element, index * 2, i_left, middle); if (middle < right) update(left, right, element, index * 2 + 1, middle + 1, i_right); } public: RUQ(int size) { this->size = getSegTreeSize(size) / 2; this->length = getSegTreeSize(size); node.resize(this->length); fill(node.begin(), node.end(), MAX); } void update(int left, int right, int element) { update(left, right, element, 1, 1, size); } int find(int index) { return find(index, 1, 1, size); } }; int main() { int n, q; std::cin >> n >> q; RUQ segTree(n); for (int i = 0; i < q; i++) { int com; std::cin >> com; if (com == 0) { int l, r, e; std::cin >> l >> r >> e; segTree.update(l + 1, r + 1, e); } else { int index; std::cin >> index; std::cout << segTree.find(index + 1) << std::endl; } } }
### Prompt Your task is to create a cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> #include <vector> const int MAX = 2147483647; int min(int a, int b) { return a < b ? a : b; } int getSegTreeSize(int len) { if (len == 0) return 0; int ret, cnt = 0, memo = 1; while (memo < len) { memo *= 2; cnt++; } ret = 1 << (cnt + 1); return ret; } class RUQ { private: int size, length; std::vector<int> node; RUQ() {} int find(int return_index, int index, int left, int right) { if (node[index] != -1) return node[index]; if ((left + right) / 2 < return_index) return find(return_index, index * 2 + 1, (left + right) / 2 + 1, right); return find(return_index, index * 2, left, (left + right) / 2); } void update(int left, int right, int element, int index, int i_left, int i_right) { if (element == node[index]) return; if (left <= i_left && i_right <= right) { node[index] = element; return; } if (node[index] != -1) { node[index * 2] = node[index]; node[index * 2 + 1] = node[index]; node[index] = -1; } int middle = (i_left + i_right) / 2; if (middle >= left) update(left, right, element, index * 2, i_left, middle); if (middle < right) update(left, right, element, index * 2 + 1, middle + 1, i_right); } public: RUQ(int size) { this->size = getSegTreeSize(size) / 2; this->length = getSegTreeSize(size); node.resize(this->length); fill(node.begin(), node.end(), MAX); } void update(int left, int right, int element) { update(left, right, element, 1, 1, size); } int find(int index) { return find(index, 1, 1, size); } }; int main() { int n, q; std::cin >> n >> q; RUQ segTree(n); for (int i = 0; i < q; i++) { int com; std::cin >> com; if (com == 0) { int l, r, e; std::cin >> l >> r >> e; segTree.update(l + 1, r + 1, e); } else { int index; std::cin >> index; std::cout << segTree.find(index + 1) << std::endl; } } } ```
#include<bits/stdc++.h> using namespace std; typedef long long int ll; #define INF (1LL<<31)-1 typedef struct RUQ{ ll n; vector<ll> a; //vector<bool> f; void init(int size){ n = 1; while(n<size) n*=2; a = vector<ll> (2*n-1,INF); //f = vector<bool> (2*n-1,true); } void init(vector<ll> &b){ int size = b.size(); n = 1; while(n<size)n*=2; a = vector<ll> (2*n-1,INF); //f = vector<bool> (n,true); for(int i=0;i<size;i++) a[i+n-1] = b[i]; } // void update(ll aa,ll b ,ll k,ll l,ll r ,ll x){ if(r<= aa|| b<= l)return ; if(aa<=l && r<=b ) a[k] = x; else{ if(a[k] != INF){ a[k*2+1] = a[k]; a[k*2+2] = a[k]; a[k] = INF; } update(aa,b,k*2+1,l,(l+r)/2,x); update(aa,b,k*2+2,(l+r)/2,r,x); } return ; } ll find(ll aa,ll b,ll k,ll l,ll r){ if(r<=aa|| b<=l) return INF; if(aa<=l && r<= b) return a[k]; if(a[k]!=INF){ a[k*2+1] = a[k]; a[k*2+2] = a[k]; a[k] = INF; } return min(find(aa,b,k*2+1,l,(l+r)/2),find(aa,b,k*2+2,(l+r)/2,r) ); } //[s,t] void update(ll s,ll t,ll x){ update(s,t+1,0,0,n,x); } ll find(ll s){ return find(s,s+1,0,0,n); } }RUQ; int main(){ ll n,q; cin>>n>>q; RUQ seg; seg.init(n); //cout<<seg.a.size()<<endl; for(ll i=0;i<q;i++){ ll d,s,t,x; cin>>d; if(d==0){ cin>>s>>t>>x; seg.update(s,t,x); }else{ cin>>s; cout<<seg.find(s)<<endl; } } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long int ll; #define INF (1LL<<31)-1 typedef struct RUQ{ ll n; vector<ll> a; //vector<bool> f; void init(int size){ n = 1; while(n<size) n*=2; a = vector<ll> (2*n-1,INF); //f = vector<bool> (2*n-1,true); } void init(vector<ll> &b){ int size = b.size(); n = 1; while(n<size)n*=2; a = vector<ll> (2*n-1,INF); //f = vector<bool> (n,true); for(int i=0;i<size;i++) a[i+n-1] = b[i]; } // void update(ll aa,ll b ,ll k,ll l,ll r ,ll x){ if(r<= aa|| b<= l)return ; if(aa<=l && r<=b ) a[k] = x; else{ if(a[k] != INF){ a[k*2+1] = a[k]; a[k*2+2] = a[k]; a[k] = INF; } update(aa,b,k*2+1,l,(l+r)/2,x); update(aa,b,k*2+2,(l+r)/2,r,x); } return ; } ll find(ll aa,ll b,ll k,ll l,ll r){ if(r<=aa|| b<=l) return INF; if(aa<=l && r<= b) return a[k]; if(a[k]!=INF){ a[k*2+1] = a[k]; a[k*2+2] = a[k]; a[k] = INF; } return min(find(aa,b,k*2+1,l,(l+r)/2),find(aa,b,k*2+2,(l+r)/2,r) ); } //[s,t] void update(ll s,ll t,ll x){ update(s,t+1,0,0,n,x); } ll find(ll s){ return find(s,s+1,0,0,n); } }RUQ; int main(){ ll n,q; cin>>n>>q; RUQ seg; seg.init(n); //cout<<seg.a.size()<<endl; for(ll i=0;i<q;i++){ ll d,s,t,x; cin>>d; if(d==0){ cin>>s>>t>>x; seg.update(s,t,x); }else{ cin>>s; cout<<seg.find(s)<<endl; } } return 0; } ```
#include <iostream> #include <vector> #include <string> #include <stack> #include <map> #include <algorithm> #include <iomanip> #include <cmath> #include <queue> #include <climits> using namespace std; #define MOD 1000000007 using ll = long long; bool operator<(const pair<int, int>& a, const pair<int, int>& b) { if (a.first == b.first) { return a.second < b.second; } return a.first < b.first; } const int sqrtN = 512; struct SqrtDecomposition { int N, K; vector<pair<int,int>> data; //内部の値, どのタイミングで変更したか vector<pair<int, int>> bucketUpdate; SqrtDecomposition(int n) : N(n) { K = (N + sqrtN - 1) / sqrtN; data.assign(K*sqrtN, make_pair(INT_MAX, 0)); bucketUpdate.assign(K, make_pair(INT_MAX, 0)); } // [s, t) void update(int s, int t, int x, int num) { for (int k = 0; k < K; ++k) { int l = k * sqrtN, r = (k + 1) *sqrtN; if (r <= s || t <= l) continue; if (s <= l && r <= t) { bucketUpdate[k] = make_pair(x, num); } else { for (int i = max(s, l); i < min(t, r); ++i) { data[i] = make_pair(x, num); } } } } //[x, y) int find(int i) { int k = i / sqrtN; if (data[i].second > bucketUpdate[k].second) return data[i].first; else return bucketUpdate[k].first; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; SqrtDecomposition sd(n); for (int i = 0; i < q; ++i) { int com; cin >> com; if (com == 0) { int s, t, x; cin >> s >> t >> x; sd.update(s, t+1, x, i+1); } else { int t; cin >> t; cout << sd.find(t) << endl; } } int a; cin >> a; return 0; }
### Prompt Please create a solution in Cpp to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <iostream> #include <vector> #include <string> #include <stack> #include <map> #include <algorithm> #include <iomanip> #include <cmath> #include <queue> #include <climits> using namespace std; #define MOD 1000000007 using ll = long long; bool operator<(const pair<int, int>& a, const pair<int, int>& b) { if (a.first == b.first) { return a.second < b.second; } return a.first < b.first; } const int sqrtN = 512; struct SqrtDecomposition { int N, K; vector<pair<int,int>> data; //内部の値, どのタイミングで変更したか vector<pair<int, int>> bucketUpdate; SqrtDecomposition(int n) : N(n) { K = (N + sqrtN - 1) / sqrtN; data.assign(K*sqrtN, make_pair(INT_MAX, 0)); bucketUpdate.assign(K, make_pair(INT_MAX, 0)); } // [s, t) void update(int s, int t, int x, int num) { for (int k = 0; k < K; ++k) { int l = k * sqrtN, r = (k + 1) *sqrtN; if (r <= s || t <= l) continue; if (s <= l && r <= t) { bucketUpdate[k] = make_pair(x, num); } else { for (int i = max(s, l); i < min(t, r); ++i) { data[i] = make_pair(x, num); } } } } //[x, y) int find(int i) { int k = i / sqrtN; if (data[i].second > bucketUpdate[k].second) return data[i].first; else return bucketUpdate[k].first; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; SqrtDecomposition sd(n); for (int i = 0; i < q; ++i) { int com; cin >> com; if (com == 0) { int s, t, x; cin >> s >> t >> x; sd.update(s, t+1, x, i+1); } else { int t; cin >> t; cout << sd.find(t) << endl; } } int a; cin >> a; return 0; } ```
#include <cmath> #include <cstdio> #include <vector> #include <algorithm> #pragma warning(disable : 4996) using namespace std; int N, Q; int main() { scanf("%d%d", &N, &Q); vector<int> tp(Q), s(Q), t(Q), x(Q); for (int i = 0; i < Q; i++) { scanf("%d%d", &tp[i], &s[i]); if (tp[i] == 0) scanf("%d%d", &t[i], &x[i]), t[i]++; } vector<int> d(N, 2147483647); int B = sqrt(Q); for (int i = 0; i < B; i++) { int l = 1LL * i * Q / B, r = 1LL * (i + 1) * Q / B; for (int j = l; j < r; j++) { if (tp[j] == 1) { bool flag = false; for (int k = j - 1; k >= l; k--) { if (tp[k] == 0 && s[k] <= s[j] && s[j] < t[k]) { printf("%d\n", x[k]); flag = true; break; } } if (!flag) printf("%d\n", d[s[j]]); } } vector<int> cp = { 0, N }; for (int j = l; j < r; j++) { if (tp[j] == 0) { cp.push_back(s[j]); cp.push_back(t[j]); } } sort(cp.begin(), cp.end()); cp.erase(unique(cp.begin(), cp.end()), cp.end()); vector<int> f(cp.size() - 1, -1); for (int j = l; j < r; j++) { if (tp[j] == 0) { int pl = lower_bound(cp.begin(), cp.end(), s[j]) - cp.begin(); int pr = lower_bound(cp.begin(), cp.end(), t[j]) - cp.begin(); for (int k = pl; k < pr; k++) f[k] = x[j]; } } for (int j = 0; j < cp.size() - 1; j++) { if (f[j] != -1) { for (int k = cp[j]; k < cp[j + 1]; k++) d[k] = f[j]; } } } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Write a program which manipulates a sequence A = {a0, a1, . . . , an−1} with the following operations: * update(s, t, x): change as, as+1, ..., at to x. * find(i): output the value of ai. Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1. Constraints * 1 ≤ n ≤ 100000 * 1 ≤ q ≤ 100000 * 0 ≤ s ≤ t < n * 0 ≤ i < n * 0 ≤ x < 231−1 Input n q query1 query2 : queryq In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format: 0 s t x or 1 i The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(i). Output For each find operation, print the value. Examples Input 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 1 1 Output 1 3 Input 1 3 1 0 0 0 0 5 1 0 Output 2147483647 5 ### Response ```cpp #include <cmath> #include <cstdio> #include <vector> #include <algorithm> #pragma warning(disable : 4996) using namespace std; int N, Q; int main() { scanf("%d%d", &N, &Q); vector<int> tp(Q), s(Q), t(Q), x(Q); for (int i = 0; i < Q; i++) { scanf("%d%d", &tp[i], &s[i]); if (tp[i] == 0) scanf("%d%d", &t[i], &x[i]), t[i]++; } vector<int> d(N, 2147483647); int B = sqrt(Q); for (int i = 0; i < B; i++) { int l = 1LL * i * Q / B, r = 1LL * (i + 1) * Q / B; for (int j = l; j < r; j++) { if (tp[j] == 1) { bool flag = false; for (int k = j - 1; k >= l; k--) { if (tp[k] == 0 && s[k] <= s[j] && s[j] < t[k]) { printf("%d\n", x[k]); flag = true; break; } } if (!flag) printf("%d\n", d[s[j]]); } } vector<int> cp = { 0, N }; for (int j = l; j < r; j++) { if (tp[j] == 0) { cp.push_back(s[j]); cp.push_back(t[j]); } } sort(cp.begin(), cp.end()); cp.erase(unique(cp.begin(), cp.end()), cp.end()); vector<int> f(cp.size() - 1, -1); for (int j = l; j < r; j++) { if (tp[j] == 0) { int pl = lower_bound(cp.begin(), cp.end(), s[j]) - cp.begin(); int pr = lower_bound(cp.begin(), cp.end(), t[j]) - cp.begin(); for (int k = pl; k < pr; k++) f[k] = x[j]; } } for (int j = 0; j < cp.size() - 1; j++) { if (f[j] != -1) { for (int k = cp[j]; k < cp[j + 1]; k++) d[k] = f[j]; } } } return 0; } ```