output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
using P = pair<int,int>;
struct RUQ{
int n,seg,init,now;
vector<P> dat;
RUQ(int n,int init) : n(n),init(init),seg(1),now(0){
while(seg < n) seg *= 2;
dat.resize(seg * 2 - 1);
for(int i = 0;i < seg * 2 - 1;i++) dat[i] = P(-1,init);
}
void update(int a,int b,int x,int tim,int k = 0,int l = 0,int r = -1){
if(r == -1) r = seg;
if(r <= a || b <= l) return;
if(a <= l && r <= b){
dat[k] = P(tim,x);
return;
}
update(a,b,x,tim,k * 2 + 1,l,(l + r) / 2);
update(a,b,x,tim,k * 2 + 2,(l + r) / 2,r);
}
void update(int a,int b,int x){
update(a,b,x,now++);
}
P get(int i){
i += seg - 1;
P ret = dat[i];
while(i){
i = (i - 1) / 2;
ret = max(ret,dat[i]);
}
return ret;
}
};
signed main(){
int n,q;
cin >> n >> q;
RUQ ruq(n,(1ll << 31) - 1);
for(int i = 0;i < q;i++){
int t;
cin >> t;
if(!t){
int a,b,x;
cin >> a >> b >> x;
ruq.update(a,b + 1,x);
}
else{
int x;
cin >> x;
cout << ruq.get(x).second << 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;
using P = pair<int,int>;
struct RUQ{
int n,seg,init,now;
vector<P> dat;
RUQ(int n,int init) : n(n),init(init),seg(1),now(0){
while(seg < n) seg *= 2;
dat.resize(seg * 2 - 1);
for(int i = 0;i < seg * 2 - 1;i++) dat[i] = P(-1,init);
}
void update(int a,int b,int x,int tim,int k = 0,int l = 0,int r = -1){
if(r == -1) r = seg;
if(r <= a || b <= l) return;
if(a <= l && r <= b){
dat[k] = P(tim,x);
return;
}
update(a,b,x,tim,k * 2 + 1,l,(l + r) / 2);
update(a,b,x,tim,k * 2 + 2,(l + r) / 2,r);
}
void update(int a,int b,int x){
update(a,b,x,now++);
}
P get(int i){
i += seg - 1;
P ret = dat[i];
while(i){
i = (i - 1) / 2;
ret = max(ret,dat[i]);
}
return ret;
}
};
signed main(){
int n,q;
cin >> n >> q;
RUQ ruq(n,(1ll << 31) - 1);
for(int i = 0;i < q;i++){
int t;
cin >> t;
if(!t){
int a,b,x;
cin >> a >> b >> x;
ruq.update(a,b + 1,x);
}
else{
int x;
cin >> x;
cout << ruq.get(x).second << endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
class RUQSegmentTree {
const int n;
const int flag = -1;
const int id = 1;
vector<int> data, data2;
int size(int n) {
int res = 1;
while (res < n) res <<= 1;
return res;
}
void sub(int l, int r, int node, int lb, int ub, int val) {
if (ub <= l || r <= lb) return;
if (data[node] == val) return;
if (l <= lb && ub <= r) {
data[node] = val;
return;
}
int left = node * 2, right = node * 2 + 1;
if (data[node] != flag) {
data[left] = data[node];
data[right] = data[node];
}
data[node] = flag;
sub(l, r, left, lb, (lb + ub) / 2, val);
sub(l, r, right, (lb + ub) / 2, ub, val);
data2[node] = min(data[left] == flag ? data2[left] : data[left], data[right] == flag ? data2[right] : data[right]);
}
public:
RUQSegmentTree(int n_) :
n(size(n_)), data(n * 2, INT_MAX), data2(n * 2, INT_MAX) {}
void update(int l, int r, int val) {
sub(l, r + 1, 1, 0, n, val);
}
int find(int i) {
i += n;
int res = data[i];
while (i >>= 1) {
if (data[i] != flag) res = data[i];
}
return res;
}
};
int main()
{
int n, q, b, i, s, t, x;
cin >> n >> q;
RUQSegmentTree ruq(n);
while (q--) {
cin >> b;
if (b) {
cin >> i;
cout << ruq.find(i) << endl;
}
else {
cin >> s >> t >> x;
ruq.update(s, t, 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 <bits/stdc++.h>
using namespace std;
class RUQSegmentTree {
const int n;
const int flag = -1;
const int id = 1;
vector<int> data, data2;
int size(int n) {
int res = 1;
while (res < n) res <<= 1;
return res;
}
void sub(int l, int r, int node, int lb, int ub, int val) {
if (ub <= l || r <= lb) return;
if (data[node] == val) return;
if (l <= lb && ub <= r) {
data[node] = val;
return;
}
int left = node * 2, right = node * 2 + 1;
if (data[node] != flag) {
data[left] = data[node];
data[right] = data[node];
}
data[node] = flag;
sub(l, r, left, lb, (lb + ub) / 2, val);
sub(l, r, right, (lb + ub) / 2, ub, val);
data2[node] = min(data[left] == flag ? data2[left] : data[left], data[right] == flag ? data2[right] : data[right]);
}
public:
RUQSegmentTree(int n_) :
n(size(n_)), data(n * 2, INT_MAX), data2(n * 2, INT_MAX) {}
void update(int l, int r, int val) {
sub(l, r + 1, 1, 0, n, val);
}
int find(int i) {
i += n;
int res = data[i];
while (i >>= 1) {
if (data[i] != flag) res = data[i];
}
return res;
}
};
int main()
{
int n, q, b, i, s, t, x;
cin >> n >> q;
RUQSegmentTree ruq(n);
while (q--) {
cin >> b;
if (b) {
cin >> i;
cout << ruq.find(i) << endl;
}
else {
cin >> s >> t >> x;
ruq.update(s, t, x);
}
}
return 0;
}
``` |
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int MOD = (int)(1e9) + 7;
const int INF = (1ll << 31) - 1;
const double PI = acos(-1);
const double EPS = 1e-9;
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; }
struct Segtree {
int n;
vector<int>d;
Segtree(int m) {
for (n = 1; n < m; n <<= 1);
d.assign(2 * n, INF);
}
void update(int a, int b, int x = -1, int k = 1, int l = 0, int r = -1) {
if (r == -1)r = n;
if (r <= a || b <= l)return;
if (a <= l&&r <= b) {
if (x >= 0)d[k] = x;
return;
}
else {
if (d[k] != INF) {
d[2 * k] = d[2 * k + 1] = d[k];
d[k] = INF;
}
update(a, b, x, 2 * k, l, (l + r) / 2);
update(a, b, x, 2 * k + 1, (l + r) / 2, r);
}
}
int find(int i) {
update(i, i + 1);
return d[n + i];
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, Q; cin >> N >> Q;
Segtree seg(N);
rep(i, 0, Q) {
int com; cin >> com;
if (com) {
int x; cin >> x;
cout << seg.find(x) << endl;
}
else {
int s, t, x; cin >> s >> t >> x;
seg.update(s, t + 1, 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 "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int MOD = (int)(1e9) + 7;
const int INF = (1ll << 31) - 1;
const double PI = acos(-1);
const double EPS = 1e-9;
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; }
struct Segtree {
int n;
vector<int>d;
Segtree(int m) {
for (n = 1; n < m; n <<= 1);
d.assign(2 * n, INF);
}
void update(int a, int b, int x = -1, int k = 1, int l = 0, int r = -1) {
if (r == -1)r = n;
if (r <= a || b <= l)return;
if (a <= l&&r <= b) {
if (x >= 0)d[k] = x;
return;
}
else {
if (d[k] != INF) {
d[2 * k] = d[2 * k + 1] = d[k];
d[k] = INF;
}
update(a, b, x, 2 * k, l, (l + r) / 2);
update(a, b, x, 2 * k + 1, (l + r) / 2, r);
}
}
int find(int i) {
update(i, i + 1);
return d[n + i];
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, Q; cin >> N >> Q;
Segtree seg(N);
rep(i, 0, Q) {
int com; cin >> com;
if (com) {
int x; cin >> x;
cout << seg.find(x) << endl;
}
else {
int s, t, x; cin >> s >> t >> x;
seg.update(s, t + 1, x);
}
}
return 0;
}
``` |
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<20)
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 k) {
k += n-1;
P p = dat[k];
while (k > 0) {
k = (k-1)/2;
p = max(p, dat[k]);
}
return p.second;
}
void update(int a, int b, int k, P p, int l, int r) {
if (r <= a || b <= l) return;
if (a <= l && r <= b) {
dat[k] = p;
}
else {
update(a,b,k*2+1,p,l,(l+r)/2);
update(a,b,k*2+2,p,(l+r)/2,r);
}
}
int main() {
int _n, q;
cin >> _n >> q;
init(_n);
for (int i = 0; i < q; i++) {
int j; cin >> j;
if (j) {
int u;
cin >> u;
cout << find(u) << endl;
}
else {
int s, t, x;
cin >> s >> t >> x;
update(s,t+1,0,P(i,x),0,n);
}
}
}
| ### 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"
using namespace std;
typedef long long ll;
#define INF (1<<20)
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 k) {
k += n-1;
P p = dat[k];
while (k > 0) {
k = (k-1)/2;
p = max(p, dat[k]);
}
return p.second;
}
void update(int a, int b, int k, P p, int l, int r) {
if (r <= a || b <= l) return;
if (a <= l && r <= b) {
dat[k] = p;
}
else {
update(a,b,k*2+1,p,l,(l+r)/2);
update(a,b,k*2+2,p,(l+r)/2,r);
}
}
int main() {
int _n, q;
cin >> _n >> q;
init(_n);
for (int i = 0; i < q; i++) {
int j; cin >> j;
if (j) {
int u;
cin >> u;
cout << find(u) << endl;
}
else {
int s, t, x;
cin >> s >> t >> x;
update(s,t+1,0,P(i,x),0,n);
}
}
}
``` |
#include <iostream>
#include <vector>
using namespace std;
const int INF = 2147483647;
struct SegmentTree {
int n;
vector<int> heap;
SegmentTree(int n): n(n) {
heap.assign(4 * n, INF);
}
void update(int nodeId, int l, int r, int ql, int qr, int v) {
if (qr < l || ql > r) {
return;
}
else if (ql <= l && r <= qr) {
heap[nodeId] = v;
}
else {
pushdown(nodeId);
int m = l + (r - l)/2;
update(nodeId * 2, l, m, ql, qr, v);
update(nodeId * 2 + 1, m + 1, r, ql, qr, v);
}
}
int query(int nodeId, int l, int r, int q) {
if (q < l || q > r) {
return -1;
}
else if (heap[nodeId] != -1) {
return heap[nodeId];
}
else {
int m = l + (r - l)/2;
return max(query(nodeId * 2, l, m, q), query(nodeId * 2 + 1, m + 1, r, q));
}
}
void pushdown(int nodeId) {
//cout << "push: " << nodeId << endl;
if (heap[nodeId] != -1) {
heap[nodeId * 2] = heap[nodeId];
heap[nodeId * 2 + 1] = heap[nodeId];
heap[nodeId] = -1;
}
}
};
int main() {
int n, q;
cin >> n >> q;
SegmentTree st(n);
for (int i = 0; i < q; i++) {
int cmd;
cin >> cmd;
if (cmd == 0) {
int ql, qr, v;
cin >> ql >> qr >> v;
st.update(1, 0, n -1, ql, qr, v);
// for (int i = 0; i < 4 * n; i++) {
// cout << i << ":" << st.heap[i] << endl;
// }
}
else {
int pos;
cin >> pos;
cout << st.query(1, 0, n-1, pos) << endl;
}
}
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 <iostream>
#include <vector>
using namespace std;
const int INF = 2147483647;
struct SegmentTree {
int n;
vector<int> heap;
SegmentTree(int n): n(n) {
heap.assign(4 * n, INF);
}
void update(int nodeId, int l, int r, int ql, int qr, int v) {
if (qr < l || ql > r) {
return;
}
else if (ql <= l && r <= qr) {
heap[nodeId] = v;
}
else {
pushdown(nodeId);
int m = l + (r - l)/2;
update(nodeId * 2, l, m, ql, qr, v);
update(nodeId * 2 + 1, m + 1, r, ql, qr, v);
}
}
int query(int nodeId, int l, int r, int q) {
if (q < l || q > r) {
return -1;
}
else if (heap[nodeId] != -1) {
return heap[nodeId];
}
else {
int m = l + (r - l)/2;
return max(query(nodeId * 2, l, m, q), query(nodeId * 2 + 1, m + 1, r, q));
}
}
void pushdown(int nodeId) {
//cout << "push: " << nodeId << endl;
if (heap[nodeId] != -1) {
heap[nodeId * 2] = heap[nodeId];
heap[nodeId * 2 + 1] = heap[nodeId];
heap[nodeId] = -1;
}
}
};
int main() {
int n, q;
cin >> n >> q;
SegmentTree st(n);
for (int i = 0; i < q; i++) {
int cmd;
cin >> cmd;
if (cmd == 0) {
int ql, qr, v;
cin >> ql >> qr >> v;
st.update(1, 0, n -1, ql, qr, v);
// for (int i = 0; i < 4 * n; i++) {
// cout << i << ":" << st.heap[i] << endl;
// }
}
else {
int pos;
cin >> pos;
cout << st.query(1, 0, n-1, pos) << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = (1LL<<31)-1;
class RUQ{
public:
int N;
vector<int> data;
vector<int> times;
void update(int time, int a, int b, int x, int k, int l, int r){
if(a <= l && r <= b){
if(times[k] < time){
data[k] = x;
times[k] = time;
}
return;
}
if(b <= l || r <= a)
return;
update(time, a, b, x, 2*k+1, l, (l+r)/2);
update(time, a, b, x, 2*k+2, (l+r)/2, r);
}
public:
RUQ(int N_){
N = 1;
while(N < N_)
N <<= 1;
data.assign(2*N-1, INF);
times.assign(2*N-1, -1);
}
void update(int time, int s, int t, int x){
update(time, s, t, x, 0, 0, N);
}
int find(int x){
x += N-1;
int res = INF;
int time = -1;
while(true){
if(time < times[x]){
time = times[x];
res = data[x];
}
if(x == 0)
break;
x = (x-1)/2;
}
return res;
}
};
int main(){
int N, Q;
cin >> N >> Q;
RUQ ruq(N);
for(int i=0; i<Q; i++){
int com; cin >> com;
if(com == 0){
int s, t, x;
cin >> s >> t >> x;
ruq.update(i, s, t+1, x);
}
if(com == 1){
int i; cin >> i;
cout << ruq.find(i) << endl;
}
}
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 <bits/stdc++.h>
using namespace std;
const int INF = (1LL<<31)-1;
class RUQ{
public:
int N;
vector<int> data;
vector<int> times;
void update(int time, int a, int b, int x, int k, int l, int r){
if(a <= l && r <= b){
if(times[k] < time){
data[k] = x;
times[k] = time;
}
return;
}
if(b <= l || r <= a)
return;
update(time, a, b, x, 2*k+1, l, (l+r)/2);
update(time, a, b, x, 2*k+2, (l+r)/2, r);
}
public:
RUQ(int N_){
N = 1;
while(N < N_)
N <<= 1;
data.assign(2*N-1, INF);
times.assign(2*N-1, -1);
}
void update(int time, int s, int t, int x){
update(time, s, t, x, 0, 0, N);
}
int find(int x){
x += N-1;
int res = INF;
int time = -1;
while(true){
if(time < times[x]){
time = times[x];
res = data[x];
}
if(x == 0)
break;
x = (x-1)/2;
}
return res;
}
};
int main(){
int N, Q;
cin >> N >> Q;
RUQ ruq(N);
for(int i=0; i<Q; i++){
int com; cin >> com;
if(com == 0){
int s, t, x;
cin >> s >> t >> x;
ruq.update(i, s, t+1, x);
}
if(com == 1){
int i; cin >> i;
cout << ruq.find(i) << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, q;
const long long inf = (1ll << 31) - 1;
const int max_n = (1 << 17);
long long seg[max_n * 2 - 1];
long long lazy[max_n * 2 - 1];
void init()
{
int _n = n;
n = 1;
while (n < _n) n *= 2;
fill(seg, seg + 2 * n - 1, inf);
fill(lazy, lazy + 2 * n - 1, inf + 1);
}
void update(int a, int b, int x, int k = 0, int l = 0, int r = n)
{
if (a <= l && r <= b){
lazy[k] = x;
return;
}
if (r <= a || b <= l){
return;
}
if (lazy[k] != inf + 1) {
lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
}
lazy[k] = inf + 1;
update(a, b, x, k * 2 + 1, l, (l + r) / 2);
update(a, b, x, k * 2 + 2, (l + r) / 2, r);
}
void find(int a, int b, int k = 0, int l = 0, int r = n)
{
if (a <= l && r <= b){
if (lazy[k] != inf + 1){
seg[k] = lazy[k];
}
lazy[k] = inf + 1;
return;
}
else if (r <= a || b <= l){
return;
}
else {
if (lazy[k] != inf + 1){
lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
lazy[k] = inf + 1;
}
find(a, b, k * 2 + 1, l, (l + r) / 2);
find(a, b, k * 2 + 2, (l + r) / 2, r);
}
}
int main()
{
cin >> n >> q;
init();
for (int i = 0; i < q; i++){
int f;
cin >> f;
if (f == 0){
int s, t, x;
cin >> s >> t >> x;
update(s, t + 1, x);
/*
for (int i = 0; i < n * 2 - 1; i++){
cout << lazy[i] << " ";
}
puts("");
*/
} else {
int i;
cin >> i;
find(i, i + 1);
/*
for (int i = 0; i < n * 2 - 1; i++){
cout << seg[i] << " ";
}
puts("");
*/
cout << seg[i + n - 1] << endl;
}
}
} | ### 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 <bits/stdc++.h>
using namespace std;
int n, q;
const long long inf = (1ll << 31) - 1;
const int max_n = (1 << 17);
long long seg[max_n * 2 - 1];
long long lazy[max_n * 2 - 1];
void init()
{
int _n = n;
n = 1;
while (n < _n) n *= 2;
fill(seg, seg + 2 * n - 1, inf);
fill(lazy, lazy + 2 * n - 1, inf + 1);
}
void update(int a, int b, int x, int k = 0, int l = 0, int r = n)
{
if (a <= l && r <= b){
lazy[k] = x;
return;
}
if (r <= a || b <= l){
return;
}
if (lazy[k] != inf + 1) {
lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
}
lazy[k] = inf + 1;
update(a, b, x, k * 2 + 1, l, (l + r) / 2);
update(a, b, x, k * 2 + 2, (l + r) / 2, r);
}
void find(int a, int b, int k = 0, int l = 0, int r = n)
{
if (a <= l && r <= b){
if (lazy[k] != inf + 1){
seg[k] = lazy[k];
}
lazy[k] = inf + 1;
return;
}
else if (r <= a || b <= l){
return;
}
else {
if (lazy[k] != inf + 1){
lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
lazy[k] = inf + 1;
}
find(a, b, k * 2 + 1, l, (l + r) / 2);
find(a, b, k * 2 + 2, (l + r) / 2, r);
}
}
int main()
{
cin >> n >> q;
init();
for (int i = 0; i < q; i++){
int f;
cin >> f;
if (f == 0){
int s, t, x;
cin >> s >> t >> x;
update(s, t + 1, x);
/*
for (int i = 0; i < n * 2 - 1; i++){
cout << lazy[i] << " ";
}
puts("");
*/
} else {
int i;
cin >> i;
find(i, i + 1);
/*
for (int i = 0; i < n * 2 - 1; i++){
cout << seg[i] << " ";
}
puts("");
*/
cout << seg[i + n - 1] << endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct LazySegmentTree {
int size;
vector<int> node, lazy;
LazySegmentTree(int n) {
size = 1;
while (size < n) size <<= 1;
node.resize(2 * size, (unsigned int) (1 << 31) - 1);
lazy.resize(2 * size, -1);
}
void eval(int k) {
if (lazy[k] == -1) return;
if (k >= size) {
node[k] = lazy[k];
} else {
lazy[2 * k] = lazy[2 * k + 1] = lazy[k];
}
lazy[k] = -1;
}
void update(int a, int b, int x, int k = 1, int l = 0, int r = -1) {
if (r == -1) r = size;
eval(k);
if (r <= a || b <= l) return;
if (a <= l && r <= b) {
lazy[k] = x;
eval(k);
} else {
int m = (l + r) / 2;
update(a, b, x, 2 * k, l, m);
update(a, b, x, 2 * k + 1, m, r);
}
}
int query(int i, int k = 1, int l = 0, int r = -1) {
if (r == -1) r = size;
eval(k);
int m = (l + r) / 2;
if (k >= size && l == i) return node[k];
if (l <= i && i < m) return query(i, 2 * k, l, m);
else return query(i, 2 * k + 1, m, r);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
LazySegmentTree st(n);
for (int j = 0; j < q; j++) {
int type;
cin >> type;
if (type == 0) {
int s, t, x;
cin >> s >> t >> x;
st.update(s, t + 1, x);
} else {
int i;
cin >> i;
cout << st.query(i) << '\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 <bits/stdc++.h>
using namespace std;
using ll = long long;
struct LazySegmentTree {
int size;
vector<int> node, lazy;
LazySegmentTree(int n) {
size = 1;
while (size < n) size <<= 1;
node.resize(2 * size, (unsigned int) (1 << 31) - 1);
lazy.resize(2 * size, -1);
}
void eval(int k) {
if (lazy[k] == -1) return;
if (k >= size) {
node[k] = lazy[k];
} else {
lazy[2 * k] = lazy[2 * k + 1] = lazy[k];
}
lazy[k] = -1;
}
void update(int a, int b, int x, int k = 1, int l = 0, int r = -1) {
if (r == -1) r = size;
eval(k);
if (r <= a || b <= l) return;
if (a <= l && r <= b) {
lazy[k] = x;
eval(k);
} else {
int m = (l + r) / 2;
update(a, b, x, 2 * k, l, m);
update(a, b, x, 2 * k + 1, m, r);
}
}
int query(int i, int k = 1, int l = 0, int r = -1) {
if (r == -1) r = size;
eval(k);
int m = (l + r) / 2;
if (k >= size && l == i) return node[k];
if (l <= i && i < m) return query(i, 2 * k, l, m);
else return query(i, 2 * k + 1, m, r);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
LazySegmentTree st(n);
for (int j = 0; j < q; j++) {
int type;
cin >> type;
if (type == 0) {
int s, t, x;
cin >> s >> t >> x;
st.update(s, t + 1, x);
} else {
int i;
cin >> i;
cout << st.query(i) << '\n';
}
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1<<17;
int n,dat[2*MAX_N-1];
//?????????
void init(int n_){
//????´???°n???2???????????????
n=1;
while(n<n_)n*=2;
for(int i=0;i<2*n-1;i++)dat[i]=INT_MAX;
}
void update(int a,int b,int k,int l,int r,int x){
if(r<=a||b<=l)return;
if(a<=l&&r<=b)dat[k]=x;
else{
if(dat[k]!=INT_MAX){
dat[k*2+1]=dat[k];
dat[k*2+2]=dat[k];
dat[k]=INT_MAX;
}
update(a,b,k*2+1,l,(l+r)/2,x);
update(a,b,k*2+2,(l+r)/2,r,x);
}
}
int find(int a,int b,int k,int l,int r){
if(r<=a||b<=l)return INT_MAX;
if(a<=l&&r<=b)return dat[k];
else{
if(dat[k]!=INT_MAX){
dat[k*2+1]=dat[k];
dat[k*2+2]=dat[k];
dat[k]=INT_MAX;
}
return min(find(a,b,k*2+1,l,(l+r)/2),find(a,b,k*2+2,(l+r)/2,r));
}
}
int main(){
int q;
cin>>n>>q;
init(n);
int u,s,t,x;
for(int i=0;i<q;i++){
cin>>u;
if(!u){
cin>>s>>t>>x;
update(s,t+1,0,0,n,x);
}else{
cin>>s;
cout<<find(s,s+1,0,0,n)<<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<bits/stdc++.h>
using namespace std;
const int MAX_N = 1<<17;
int n,dat[2*MAX_N-1];
//?????????
void init(int n_){
//????´???°n???2???????????????
n=1;
while(n<n_)n*=2;
for(int i=0;i<2*n-1;i++)dat[i]=INT_MAX;
}
void update(int a,int b,int k,int l,int r,int x){
if(r<=a||b<=l)return;
if(a<=l&&r<=b)dat[k]=x;
else{
if(dat[k]!=INT_MAX){
dat[k*2+1]=dat[k];
dat[k*2+2]=dat[k];
dat[k]=INT_MAX;
}
update(a,b,k*2+1,l,(l+r)/2,x);
update(a,b,k*2+2,(l+r)/2,r,x);
}
}
int find(int a,int b,int k,int l,int r){
if(r<=a||b<=l)return INT_MAX;
if(a<=l&&r<=b)return dat[k];
else{
if(dat[k]!=INT_MAX){
dat[k*2+1]=dat[k];
dat[k*2+2]=dat[k];
dat[k]=INT_MAX;
}
return min(find(a,b,k*2+1,l,(l+r)/2),find(a,b,k*2+2,(l+r)/2,r));
}
}
int main(){
int q;
cin>>n>>q;
init(n);
int u,s,t,x;
for(int i=0;i<q;i++){
cin>>u;
if(!u){
cin>>s>>t>>x;
update(s,t+1,0,0,n,x);
}else{
cin>>s;
cout<<find(s,s+1,0,0,n)<<endl;
}
}
return 0;
}
``` |
#include<cstdio>
#include<algorithm>
#define N 100000
#define MAX (1<<31)-1
using namespace std;
int A[N], T[4*N], lazy[4*N];
void build(int l, int r, int k) {
if (l == r) {
T[k] = A[l];
return;
}
int mid = (l + r) / 2;
build(l, mid, k * 2);
build(mid + 1, r, k * 2 + 1);
T[k] = min(T[2 * k], T[2 * k + 1]);
}
void pushdown(int k) {
if (lazy[k] != -1) {
lazy[2*k] = lazy[k];
lazy[2*k+1] = lazy[k];
T[2*k] = lazy[k];
T[2*k+1] = lazy[k];
lazy[k] = -1;
}
}
void updata(int L, int R, int data ,int l, int r ,int k) {
if (L <= l && r <= R) {
T[k] = data;
lazy[k] = data;
return;
}
pushdown(k);
int mid = (l + r) / 2;
if (mid >= L) updata(L, R, data, l, mid, 2*k);
if (mid < R) updata(L, R, data, mid+1, r, 2*k+1);
T[k] = min(T[2 * k], T[2 * k + 1]);
}
int find(int L, int R, int l, int r, int k) {
if (L <= l && r <= R) return T[k];
pushdown(k);
int mid = (l + r) / 2;
int ans = MAX;
if (mid >= L) ans = min(ans, find(L, R, l, mid, k * 2));
if (mid < R) ans = min(ans, find(L, R, mid + 1, r, k * 2 + 1));
return ans;
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++)
A[i] = MAX;
for (int i = 0; i < 4*n; i++)
lazy[i] = -1;
build(0, n - 1, 1);
int com, s, t, x, y;
while (q--) {
scanf("%d", &com);
if (com == 0) {
scanf("%d%d%d", &s, &t, &x);
updata(s, t, x, 0, n - 1, 1);
}
else {
scanf("%d", &x);
printf("%d\n", find(x, x, 0, n - 1, 1));
}
}
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<cstdio>
#include<algorithm>
#define N 100000
#define MAX (1<<31)-1
using namespace std;
int A[N], T[4*N], lazy[4*N];
void build(int l, int r, int k) {
if (l == r) {
T[k] = A[l];
return;
}
int mid = (l + r) / 2;
build(l, mid, k * 2);
build(mid + 1, r, k * 2 + 1);
T[k] = min(T[2 * k], T[2 * k + 1]);
}
void pushdown(int k) {
if (lazy[k] != -1) {
lazy[2*k] = lazy[k];
lazy[2*k+1] = lazy[k];
T[2*k] = lazy[k];
T[2*k+1] = lazy[k];
lazy[k] = -1;
}
}
void updata(int L, int R, int data ,int l, int r ,int k) {
if (L <= l && r <= R) {
T[k] = data;
lazy[k] = data;
return;
}
pushdown(k);
int mid = (l + r) / 2;
if (mid >= L) updata(L, R, data, l, mid, 2*k);
if (mid < R) updata(L, R, data, mid+1, r, 2*k+1);
T[k] = min(T[2 * k], T[2 * k + 1]);
}
int find(int L, int R, int l, int r, int k) {
if (L <= l && r <= R) return T[k];
pushdown(k);
int mid = (l + r) / 2;
int ans = MAX;
if (mid >= L) ans = min(ans, find(L, R, l, mid, k * 2));
if (mid < R) ans = min(ans, find(L, R, mid + 1, r, k * 2 + 1));
return ans;
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++)
A[i] = MAX;
for (int i = 0; i < 4*n; i++)
lazy[i] = -1;
build(0, n - 1, 1);
int com, s, t, x, y;
while (q--) {
scanf("%d", &com);
if (com == 0) {
scanf("%d%d%d", &s, &t, &x);
updata(s, t, x, 0, n - 1, 1);
}
else {
scanf("%d", &x);
printf("%d\n", find(x, x, 0, n - 1, 1));
}
}
return 0;
}
``` |
#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;
const vector<T1> data;
vector<T2> lazy;
void push(int node) {
if (lazy[node] == M::id2()) return;
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) const {
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
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 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;
const vector<T1> data;
vector<T2> lazy;
void push(int node) {
if (lazy[node] == M::id2()) return;
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) const {
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 <algorithm>
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
template<typename OperatorMonoid>
struct SegmentTree {
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
const H h;
const OperatorMonoid OM0;
int sz, height;
vector<OperatorMonoid> lazy;
SegmentTree(int n, const H h, const OperatorMonoid &OM0)
: h(h), OM0(OM0), sz(1), height(0)
{
while (sz < n) sz <<= 1, height++;
lazy.assign(sz * 2, OM0);
}
inline void propagate(int k) {
if (lazy[k] == OM0) return ;
lazy[k << 1 | 0] = h(lazy[k << 1 | 0], lazy[k]);
lazy[k << 1 | 1] = h(lazy[k << 1 | 1], lazy[k]);
lazy[k] = OM0;
}
inline void thrust(int k) {
for (int i = height; i > 0; i--) propagate(k >> i);
}
void update(int a, int b, const OperatorMonoid &x) {
thrust(a += sz);
thrust(b += sz - 1);
for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) lazy[l] = h(lazy[l], x), ++l;
if (r & 1) --r, lazy[r] = h(lazy[r], x);
}
}
void set(int k, const OperatorMonoid &x) { thrust(k += sz); lazy[k] = x; }
OperatorMonoid get(int k) { thrust(k += sz); return lazy[k]; }
OperatorMonoid operator[](const int &k) { return get(k); }
};
const int INF = numeric_limits<int>::max();
int main() {
int n, q; cin >> n >> q;
SegmentTree<int> seg(n, [](int a, int b) { return b; }, INF);
while (q--) {
int com; cin >> com;
if (com == 0) {
int s, t, x; cin >> s >> t >> x; t++;
seg.update(s, t, x);
} else {
int i; cin >> i;
cout << seg[i] << endl;
}
}
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 <algorithm>
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
template<typename OperatorMonoid>
struct SegmentTree {
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
const H h;
const OperatorMonoid OM0;
int sz, height;
vector<OperatorMonoid> lazy;
SegmentTree(int n, const H h, const OperatorMonoid &OM0)
: h(h), OM0(OM0), sz(1), height(0)
{
while (sz < n) sz <<= 1, height++;
lazy.assign(sz * 2, OM0);
}
inline void propagate(int k) {
if (lazy[k] == OM0) return ;
lazy[k << 1 | 0] = h(lazy[k << 1 | 0], lazy[k]);
lazy[k << 1 | 1] = h(lazy[k << 1 | 1], lazy[k]);
lazy[k] = OM0;
}
inline void thrust(int k) {
for (int i = height; i > 0; i--) propagate(k >> i);
}
void update(int a, int b, const OperatorMonoid &x) {
thrust(a += sz);
thrust(b += sz - 1);
for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) lazy[l] = h(lazy[l], x), ++l;
if (r & 1) --r, lazy[r] = h(lazy[r], x);
}
}
void set(int k, const OperatorMonoid &x) { thrust(k += sz); lazy[k] = x; }
OperatorMonoid get(int k) { thrust(k += sz); return lazy[k]; }
OperatorMonoid operator[](const int &k) { return get(k); }
};
const int INF = numeric_limits<int>::max();
int main() {
int n, q; cin >> n >> q;
SegmentTree<int> seg(n, [](int a, int b) { return b; }, INF);
while (q--) {
int com; cin >> com;
if (com == 0) {
int s, t, x; cin >> s >> t >> x; t++;
seg.update(s, t, x);
} else {
int i; cin >> i;
cout << seg[i] << endl;
}
}
return 0;
}
``` |
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const long long INF = (1ll<<31)-1;
template<typename T>
class lazy_segment_tree{
int n;
T fval;
vector<T> dat, lazy;
vector<bool> lazyFlag;
public:
lazy_segment_tree(){
}
lazy_segment_tree(int n_, T val){
init(n_, val);
}
~lazy_segment_tree(){
}
void init(int n_, T val){
fval = val;
n = 1;
while(n < n_) n *= 2;
dat.resize(2 * n - 1);
lazy.resize(2 * n - 1, fval);
lazyFlag.resize(2 * n - 1, false);
for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
}
void eval(int k, int l, int r){
if(lazyFlag[k]){
dat[k] = lazy[k];
if(r - l > 1) {
lazy[2*k+1] = lazy[2*k+2] = lazy[k];
lazyFlag[2*k+1] = lazyFlag[2*k+2] = lazyFlag[k];
}
lazyFlag[k] = 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 ;}
if(a <= l && r <= b){
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] = min(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(r <= a || b <= l){
return INF;
}
if(a <= l && r <= b){
return dat[k];
}else {
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2 );
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
};
signed main(){
lazy_segment_tree<long long> lseg;
int n, q;
cin>>n>>q;
lseg.init(n+10, INF);
for(int i = 0; i < q; i++){
int com, s, t, x;
cin>>com;
s++, t++;
if(com){
cin>>s;
cout<<lseg.query(s,s+1)<<endl;
} else {
cin>>s>>t>>x;
lseg.update(s, t+1, x);
}
}
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<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const long long INF = (1ll<<31)-1;
template<typename T>
class lazy_segment_tree{
int n;
T fval;
vector<T> dat, lazy;
vector<bool> lazyFlag;
public:
lazy_segment_tree(){
}
lazy_segment_tree(int n_, T val){
init(n_, val);
}
~lazy_segment_tree(){
}
void init(int n_, T val){
fval = val;
n = 1;
while(n < n_) n *= 2;
dat.resize(2 * n - 1);
lazy.resize(2 * n - 1, fval);
lazyFlag.resize(2 * n - 1, false);
for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
}
void eval(int k, int l, int r){
if(lazyFlag[k]){
dat[k] = lazy[k];
if(r - l > 1) {
lazy[2*k+1] = lazy[2*k+2] = lazy[k];
lazyFlag[2*k+1] = lazyFlag[2*k+2] = lazyFlag[k];
}
lazyFlag[k] = 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 ;}
if(a <= l && r <= b){
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] = min(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(r <= a || b <= l){
return INF;
}
if(a <= l && r <= b){
return dat[k];
}else {
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2 );
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
};
signed main(){
lazy_segment_tree<long long> lseg;
int n, q;
cin>>n>>q;
lseg.init(n+10, INF);
for(int i = 0; i < q; i++){
int com, s, t, x;
cin>>com;
s++, t++;
if(com){
cin>>s;
cout<<lseg.query(s,s+1)<<endl;
} else {
cin>>s>>t>>x;
lseg.update(s, t+1, x);
}
}
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 push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pint;
const int MAX_N=1<<17;
const ll INF=(1ll<<31)-1;
ll dat[2*MAX_N-1];
pint dat2[2*MAX_N-1];
int N;
//RMQ
void init(int n_){
N=1;
while(N<n_) N*=2;
//rep(i,2*N-1) dat[i]=INF;
rep(i,2*N-1) dat2[i]=mp(-1,INF);
}
void update(int k,ll a){
k+=N-1;
dat[k]=a;
while(k>0){
k=(k-1)/2;
dat[k]=min(dat[k*2+1],dat[k*2+2]);
}
}
//range [a,b)
ll query(int a,int b,int k,int l,int r){
if(r<=a||b<=l) return INF;
if(a<=l&&r<=b) return dat[k];
else{
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);
}
}
//RUQ
//range [a,b)
void r_update(int a,int b,pint x,int k,int l,int r){
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
dat2[k]=x;return;
}
else{
r_update(a,b,x,k*2+1,l,(l+r)/2);
r_update(a,b,x,k*2+2,(l+r)/2,r);
}
}
ll r_query(int i){
i+=N-1;
pint res=dat2[i];
while(i>0){
i=(i-1)/2;
res=max(res,dat2[i]);
}
return res.second;
}
int main(){
//int n,q,c,x,y;
int n,q,c,s,t,x;
cin>>n>>q;
init(n);
rep(i,q){
cin>>c;
//if(c==0) update(x,y);
if(c==0){
cin>>s>>t>>x;
r_update(s,t+1,mp(i,x),0,0,N);
}
else{
ll ans;
cin>>x;
//ans=query(x,y+1,0,0,N);
ans=r_query(x);
cout<<ans<<endl;
}
}
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;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pint;
const int MAX_N=1<<17;
const ll INF=(1ll<<31)-1;
ll dat[2*MAX_N-1];
pint dat2[2*MAX_N-1];
int N;
//RMQ
void init(int n_){
N=1;
while(N<n_) N*=2;
//rep(i,2*N-1) dat[i]=INF;
rep(i,2*N-1) dat2[i]=mp(-1,INF);
}
void update(int k,ll a){
k+=N-1;
dat[k]=a;
while(k>0){
k=(k-1)/2;
dat[k]=min(dat[k*2+1],dat[k*2+2]);
}
}
//range [a,b)
ll query(int a,int b,int k,int l,int r){
if(r<=a||b<=l) return INF;
if(a<=l&&r<=b) return dat[k];
else{
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);
}
}
//RUQ
//range [a,b)
void r_update(int a,int b,pint x,int k,int l,int r){
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
dat2[k]=x;return;
}
else{
r_update(a,b,x,k*2+1,l,(l+r)/2);
r_update(a,b,x,k*2+2,(l+r)/2,r);
}
}
ll r_query(int i){
i+=N-1;
pint res=dat2[i];
while(i>0){
i=(i-1)/2;
res=max(res,dat2[i]);
}
return res.second;
}
int main(){
//int n,q,c,x,y;
int n,q,c,s,t,x;
cin>>n>>q;
init(n);
rep(i,q){
cin>>c;
//if(c==0) update(x,y);
if(c==0){
cin>>s>>t>>x;
r_update(s,t+1,mp(i,x),0,0,N);
}
else{
ll ans;
cin>>x;
//ans=query(x,y+1,0,0,N);
ans=r_query(x);
cout<<ans<<endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
#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=(1LL<<31)-1;
const double pi=acos(-1);
const double eps=1e-10;
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
class SquareDivision{
private:
const int BUCKET_SIZE=256;
ll N, B;
vector<ll> data;
vector<bool> lazy_flag;
vector<ll> lazy_update;
public:
void prepare(int n){
B=(n+BUCKET_SIZE-1)/BUCKET_SIZE;
N=B*BUCKET_SIZE;
data.assign(N, INF);
lazy_flag.assign(B, false);
lazy_update.assign(B, 0);
}
void eval(int k){
if(lazy_flag[k]){
lazy_flag[k]=false;
for(int i=k*BUCKET_SIZE; i<(k+1)*BUCKET_SIZE; i++){
data[i]=lazy_update[k];
}
}
}
// [s, t)
void update(int s, int t, ll x){
for(int k=0; k<B; k++){
int l=k*BUCKET_SIZE, r=(k+1)*BUCKET_SIZE;
if(r<=s || t<=l) continue;
if(s<=l && r<=t){
lazy_flag[k]=true;
lazy_update[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/BUCKET_SIZE;
eval(k);
return data[i];
}
};
signed main(){
int n, q; cin>>n>>q;
SquareDivision ans;
ans.prepare(n);
while(q--){
int c; cin>>c;
if(c==0){
ll s, t, x; cin>>s>>t>>x;
ans.update(s, t+1, x);
}
else{
ll x; cin>>x;
cout << ans.find(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>
#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=(1LL<<31)-1;
const double pi=acos(-1);
const double eps=1e-10;
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
class SquareDivision{
private:
const int BUCKET_SIZE=256;
ll N, B;
vector<ll> data;
vector<bool> lazy_flag;
vector<ll> lazy_update;
public:
void prepare(int n){
B=(n+BUCKET_SIZE-1)/BUCKET_SIZE;
N=B*BUCKET_SIZE;
data.assign(N, INF);
lazy_flag.assign(B, false);
lazy_update.assign(B, 0);
}
void eval(int k){
if(lazy_flag[k]){
lazy_flag[k]=false;
for(int i=k*BUCKET_SIZE; i<(k+1)*BUCKET_SIZE; i++){
data[i]=lazy_update[k];
}
}
}
// [s, t)
void update(int s, int t, ll x){
for(int k=0; k<B; k++){
int l=k*BUCKET_SIZE, r=(k+1)*BUCKET_SIZE;
if(r<=s || t<=l) continue;
if(s<=l && r<=t){
lazy_flag[k]=true;
lazy_update[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/BUCKET_SIZE;
eval(k);
return data[i];
}
};
signed main(){
int n, q; cin>>n>>q;
SquareDivision ans;
ans.prepare(n);
while(q--){
int c; cin>>c;
if(c==0){
ll s, t, x; cin>>s>>t>>x;
ans.update(s, t+1, x);
}
else{
ll x; cin>>x;
cout << ans.find(x) << endl;
}
}
}
``` |
#include <iostream>
#include <vector>
#define int long long
using namespace std;
typedef pair<int,int> P;
class RUQ{
int n,seg,init;
vector<P> dat;
public:
RUQ(int siz,int def) : n(siz),init(def),seg(1){
while(seg < n) seg *= 2;
dat.resize(seg * 2 - 1);
for(int i = 0;i < seg * 2 - 1;i++) dat[i] = P(-1,init);
}
void update(int a,int b,int x,int tim,int k = 0,int l = 0,int r = -1){
if(r == -1) r = seg;
if(r <= a || b <= l) return;
if(a <= l && r <= b){
dat[k] = P(tim,x);
return;
}
update(a,b,x,tim,k * 2 + 1,l,(l + r) / 2);
update(a,b,x,tim,k * 2 + 2,(l + r) / 2,r);
}
P get(int i){
i += seg - 1;
P ret = dat[i];
while(i){
i = (i - 1) / 2;
ret = max(ret,dat[i]);
}
return ret;
}
};
signed main() {
int n,q;
cin >> n >> q;
RUQ ruq(n,(1 << 31) - 1);
for(int i = 0;i < q;i++){
int t;
cin >> t;
if(t == 0){
int s,t,x;
cin >> s >> t >> x;
ruq.update(s,t + 1,x,i);
}else{
int x;
cin >> x;
cout << ruq.get(x).second << 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 <iostream>
#include <vector>
#define int long long
using namespace std;
typedef pair<int,int> P;
class RUQ{
int n,seg,init;
vector<P> dat;
public:
RUQ(int siz,int def) : n(siz),init(def),seg(1){
while(seg < n) seg *= 2;
dat.resize(seg * 2 - 1);
for(int i = 0;i < seg * 2 - 1;i++) dat[i] = P(-1,init);
}
void update(int a,int b,int x,int tim,int k = 0,int l = 0,int r = -1){
if(r == -1) r = seg;
if(r <= a || b <= l) return;
if(a <= l && r <= b){
dat[k] = P(tim,x);
return;
}
update(a,b,x,tim,k * 2 + 1,l,(l + r) / 2);
update(a,b,x,tim,k * 2 + 2,(l + r) / 2,r);
}
P get(int i){
i += seg - 1;
P ret = dat[i];
while(i){
i = (i - 1) / 2;
ret = max(ret,dat[i]);
}
return ret;
}
};
signed main() {
int n,q;
cin >> n >> q;
RUQ ruq(n,(1 << 31) - 1);
for(int i = 0;i < q;i++){
int t;
cin >> t;
if(t == 0){
int s,t,x;
cin >> s >> t >> x;
ruq.update(s,t + 1,x,i);
}else{
int x;
cin >> x;
cout << ruq.get(x).second << endl;
}
}
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];
//cout<<"x:"<<x<<endl;
while(x>0){
//cout<<"query: "<<x<<" xf:"<<ary[x].first<<" x s:"<<ary[x].second<<endl;
x=parent(x);
res=(res<ary[x])?ary[x]:res;
}
//cout<<"query: "<<x<<" xf:"<<ary[x].first<<" xs:"<<ary[x].second<<endl;
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){
//cout<<"in-"<<i<<endl;
cin>>s>>t>>x;
update(s,t+1,0,make_pair(i,x),0,n);
}
else{
cin>>x;
find(x);
//cout<<"in-"<<i<<endl;
}
}
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 <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];
//cout<<"x:"<<x<<endl;
while(x>0){
//cout<<"query: "<<x<<" xf:"<<ary[x].first<<" x s:"<<ary[x].second<<endl;
x=parent(x);
res=(res<ary[x])?ary[x]:res;
}
//cout<<"query: "<<x<<" xf:"<<ary[x].first<<" xs:"<<ary[x].second<<endl;
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){
//cout<<"in-"<<i<<endl;
cin>>s>>t>>x;
update(s,t+1,0,make_pair(i,x),0,n);
}
else{
cin>>x;
find(x);
//cout<<"in-"<<i<<endl;
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
//BEGIN CUT HERE
template <typename T,typename E>
struct SegmentTree{
typedef function<T(T,E)> G;
typedef function<T(E,E)> H;
int n;
G g;
H h;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(){};
SegmentTree(int n_,G g,H h,T d1,E d0,
vector<T> v=vector<T>()):
g(g),h(h),d1(d1),d0(d0){
init(n_);
if(n_==(int)v.size()) build(n_,v);
}
void init(int n_){
n=1;
while(n<n_) n*=2;
dat.clear();
dat.resize(n,d1);
laz.clear();
laz.resize(2*n-1,d0);
}
void build(int n_, vector<T> v){
for(int i=0;i<n_;i++) dat[i]=v[i];
}
void eval(int k){
if(k*2+1<2*n-1){
laz[k*2+1]=h(laz[k*2+1],laz[k]);
laz[k*2+2]=h(laz[k*2+2],laz[k]);
laz[k]=d0;
}
}
void update(int a,int b,E x,int k,int l,int r){
eval(k);
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
laz[k]=h(laz[k],x);
return;
}
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
void update(int a,int b,E x){
update(a,b,x,0,0,n);
}
void eval2(int k){
if(k) eval2((k-1)/2);
eval(k);
}
T query(int k){
T c=dat[k];
k+=n-1;
eval2(k);
return g(c,laz[k]);
}
};
//END CUT HERE
signed main(){
int n,q;
cin>>n>>q;
SegmentTree<int,int> beet(n,
[&](int a,int b){ return b<0?a:b;},
[&](int a,int b){ return b<0?a:b;},
INT_MAX,-1);
for(int i=0;i<q;i++){
int c;
cin>>c;
if(c){
int x;
cin>>x;
cout<<beet.query(x)<<endl;
}else{
int s,t,x;
cin>>s>>t>>x;
t++;
beet.update(s,t,x);
}
}
return 0;
}
/*
verified on 2017/10/15
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_D&lang=jp
*/ | ### 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;
//BEGIN CUT HERE
template <typename T,typename E>
struct SegmentTree{
typedef function<T(T,E)> G;
typedef function<T(E,E)> H;
int n;
G g;
H h;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(){};
SegmentTree(int n_,G g,H h,T d1,E d0,
vector<T> v=vector<T>()):
g(g),h(h),d1(d1),d0(d0){
init(n_);
if(n_==(int)v.size()) build(n_,v);
}
void init(int n_){
n=1;
while(n<n_) n*=2;
dat.clear();
dat.resize(n,d1);
laz.clear();
laz.resize(2*n-1,d0);
}
void build(int n_, vector<T> v){
for(int i=0;i<n_;i++) dat[i]=v[i];
}
void eval(int k){
if(k*2+1<2*n-1){
laz[k*2+1]=h(laz[k*2+1],laz[k]);
laz[k*2+2]=h(laz[k*2+2],laz[k]);
laz[k]=d0;
}
}
void update(int a,int b,E x,int k,int l,int r){
eval(k);
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
laz[k]=h(laz[k],x);
return;
}
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
void update(int a,int b,E x){
update(a,b,x,0,0,n);
}
void eval2(int k){
if(k) eval2((k-1)/2);
eval(k);
}
T query(int k){
T c=dat[k];
k+=n-1;
eval2(k);
return g(c,laz[k]);
}
};
//END CUT HERE
signed main(){
int n,q;
cin>>n>>q;
SegmentTree<int,int> beet(n,
[&](int a,int b){ return b<0?a:b;},
[&](int a,int b){ return b<0?a:b;},
INT_MAX,-1);
for(int i=0;i<q;i++){
int c;
cin>>c;
if(c){
int x;
cin>>x;
cout<<beet.query(x)<<endl;
}else{
int s,t,x;
cin>>s>>t>>x;
t++;
beet.update(s,t,x);
}
}
return 0;
}
/*
verified on 2017/10/15
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_D&lang=jp
*/
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) FOR(i,0,n)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define PB push_back
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
const ll INF = (1ll << 60);
typedef pair<ll,ll> pii;
struct RUQ{
ll size;
ll *tree;
vi history;
RUQ(ll sz){
ll logsize; for(logsize=0;(1ll<<logsize)<=sz;logsize++);
size=(1ll<<logsize);
tree=new ll[size*2-1]; REP(i, size*2-1) tree[i]=0;
}
void update(ll s,ll t,ll x){
ll id=history.size();
updatesub(s,t,0,size,0,id);
history.push_back(x);
}
void updatesub(ll s,ll t,ll l,ll r,ll k,ll id){
if(r<=s||t<=l||l>=r) return;
if(l+1==r) {tree[k]=id; return;}
if(s<=l&&r<=t) {tree[k]=id; return;}
ll mid=(l+r+1)/2;
updatesub(s,t,l,mid,k*2+1,id);
updatesub(s,t,mid,r,k*2+2,id);
}
ll find(ll i){
ll k=size+i-1;
ll recent=tree[k];
while(k>0){
k=(k-1)/2;
recent=max(recent,tree[k]);
}
return history[recent];
}
};
int main(){
ll n,q; cin>>n>>q;
RUQ ruq(n);
ruq.update(0,n,(1ll<<31)-1);
REP(i,q){
ll com;cin>>com;
if(com==0) {ll s,t,x; cin>>s>>t>>x; ruq.update(s,t+1,x);}
else {ll i; cin>>i; cout<<ruq.find(i)<<endl;}
}
} | ### 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;
typedef long long ll;
#define REP(i,n) FOR(i,0,n)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define PB push_back
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
const ll INF = (1ll << 60);
typedef pair<ll,ll> pii;
struct RUQ{
ll size;
ll *tree;
vi history;
RUQ(ll sz){
ll logsize; for(logsize=0;(1ll<<logsize)<=sz;logsize++);
size=(1ll<<logsize);
tree=new ll[size*2-1]; REP(i, size*2-1) tree[i]=0;
}
void update(ll s,ll t,ll x){
ll id=history.size();
updatesub(s,t,0,size,0,id);
history.push_back(x);
}
void updatesub(ll s,ll t,ll l,ll r,ll k,ll id){
if(r<=s||t<=l||l>=r) return;
if(l+1==r) {tree[k]=id; return;}
if(s<=l&&r<=t) {tree[k]=id; return;}
ll mid=(l+r+1)/2;
updatesub(s,t,l,mid,k*2+1,id);
updatesub(s,t,mid,r,k*2+2,id);
}
ll find(ll i){
ll k=size+i-1;
ll recent=tree[k];
while(k>0){
k=(k-1)/2;
recent=max(recent,tree[k]);
}
return history[recent];
}
};
int main(){
ll n,q; cin>>n>>q;
RUQ ruq(n);
ruq.update(0,n,(1ll<<31)-1);
REP(i,q){
ll com;cin>>com;
if(com==0) {ll s,t,x; cin>>s>>t>>x; ruq.update(s,t+1,x);}
else {ll i; cin>>i; cout<<ruq.find(i)<<endl;}
}
}
``` |
#include <iostream>
using namespace std;
const int DEPTH = 17;
const int INTMAX = (1LL << 31) - 1;
struct RUQ {
int d[1 << (DEPTH + 1)];
int t[1 << (DEPTH + 1)];
RUQ() {
int i;
for (i = 0; i < (1 << (DEPTH + 1)); i++) {
d[i] = INTMAX;
t[i] = -1;
}
}
//[l, r)
void update(int l, int r, int x, int Time, int a = 0, int b = (1 << DEPTH), int id = 0) {
if (a >= r || b <= l) return;
if (l <= a && b <= r) {
d[id] = x;
t[id] = Time;
return;
}
update(l, r, x, Time, a, (a + b) / 2, id * 2 + 1);
update(l, r, x, Time, (a + b) / 2, b, id * 2 + 2);
}
int getValue(int pos) {
pos += (1 << DEPTH) - 1;
int retD = d[pos];
int retT = t[pos];
while (pos > 0) {
pos = (pos - 1) / 2;
if (retT < t[pos]) {
retT = t[pos];
retD = d[pos];
}
}
return retD;
}
};
RUQ ruq;
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < q; i++) {
int type;
cin >> type;
if (type == 0) {
int s, t, x;
cin >> s >> t >> x;
ruq.update(s, t + 1, x, i);
}
else {
int pos;
cin >> pos;
cout << ruq.getValue(pos) << 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;
const int DEPTH = 17;
const int INTMAX = (1LL << 31) - 1;
struct RUQ {
int d[1 << (DEPTH + 1)];
int t[1 << (DEPTH + 1)];
RUQ() {
int i;
for (i = 0; i < (1 << (DEPTH + 1)); i++) {
d[i] = INTMAX;
t[i] = -1;
}
}
//[l, r)
void update(int l, int r, int x, int Time, int a = 0, int b = (1 << DEPTH), int id = 0) {
if (a >= r || b <= l) return;
if (l <= a && b <= r) {
d[id] = x;
t[id] = Time;
return;
}
update(l, r, x, Time, a, (a + b) / 2, id * 2 + 1);
update(l, r, x, Time, (a + b) / 2, b, id * 2 + 2);
}
int getValue(int pos) {
pos += (1 << DEPTH) - 1;
int retD = d[pos];
int retT = t[pos];
while (pos > 0) {
pos = (pos - 1) / 2;
if (retT < t[pos]) {
retT = t[pos];
retD = d[pos];
}
}
return retD;
}
};
RUQ ruq;
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < q; i++) {
int type;
cin >> type;
if (type == 0) {
int s, t, x;
cin >> s >> t >> x;
ruq.update(s, t + 1, x, i);
}
else {
int pos;
cin >> pos;
cout << ruq.getValue(pos) << endl;
}
}
return 0;
}
``` |
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 2147483647
vector<pair<int,int> > a(300000,pair<int,int>(INF,0));
int n,si;
int find(int);
void update(int,int,int,int,int,int,int);
int main(){
int q,f;
si = 1;
cin >> n >> q;
for(int i = 0;;i++){
si*=2;
if(si >= n)break;
}
for(int i = 0;i<q;i++){
cin >> f;
if(f == 1){
int po,dat;
cin >> po;
dat = find(po);
cout << dat << endl;
}
else{
int st,go,d;
cin >> st >> go>>d;
update(st,go+1,0,0,si,d,i+1);
}
}
return 0;
}
int find(int p){
int ca = si-1+p;
pair<int,int> ma = a[ca];
while(ca>0){
ca=(ca-1)/2;
if(ma.second<a[ca].second){
ma=a[ca];
}
}
return ma.first;
}
void update (int s,int t,int k,int l,int r,int d,int ti){
int m=(l+r)/2;
if(s<=l&&r<=t){
a[k].first=d;
a[k].second=ti;
return;
}
else if(r<=s||t<=l)return;
else{
update(s,t,k*2+1,l,m,d,ti);
update(s,t,k*2+2,m,r,d,ti);
}
}
| ### 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>
#include<algorithm>
using namespace std;
#define INF 2147483647
vector<pair<int,int> > a(300000,pair<int,int>(INF,0));
int n,si;
int find(int);
void update(int,int,int,int,int,int,int);
int main(){
int q,f;
si = 1;
cin >> n >> q;
for(int i = 0;;i++){
si*=2;
if(si >= n)break;
}
for(int i = 0;i<q;i++){
cin >> f;
if(f == 1){
int po,dat;
cin >> po;
dat = find(po);
cout << dat << endl;
}
else{
int st,go,d;
cin >> st >> go>>d;
update(st,go+1,0,0,si,d,i+1);
}
}
return 0;
}
int find(int p){
int ca = si-1+p;
pair<int,int> ma = a[ca];
while(ca>0){
ca=(ca-1)/2;
if(ma.second<a[ca].second){
ma=a[ca];
}
}
return ma.first;
}
void update (int s,int t,int k,int l,int r,int d,int ti){
int m=(l+r)/2;
if(s<=l&&r<=t){
a[k].first=d;
a[k].second=ti;
return;
}
else if(r<=s||t<=l)return;
else{
update(s,t,k*2+1,l,m,d,ti);
update(s,t,k*2+2,m,r,d,ti);
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
// Segment tree. Assignment on interval, single element access.
template<typename T>
class SegTree{
size_t n;
int h;
vector<T> data;
vector<bool> used;
static T combine(T L, T R){
return max(L, R);
}
void apply(size_t i, T v){
data[i] = v;
used[i] = true;
}
void push(size_t p){ // propagates the changes from the root to node p.
for (int s = h; s > 0; --s){
size_t i = p >> s;
if (used[i]){
apply(i*2, data[i]);
apply(i*2+1, data[i]);
used[i] = false;
}
}
}
int calc_h(size_t nn){
int hh = 1;
for (; nn > 1; ++hh, nn >>= 1);
return hh;
}
public:
SegTree(size_t _n): n(_n), h(calc_h(n)), data(vector<T>(2 * n, numeric_limits<int>::max())), used(vector<bool>(2 * n, false)){used[1] = true;}
SegTree(const vector<T> &src): n(src.size()), h(calc_h(n)), data(vector<T>(2 * n, 0)), used(vector<bool>(2 * n, false)){init(src);}
void init(const vector<T> &src) {for (size_t i = 0; i != n; ++i) apply(n + i, src[i]);}
void modify(size_t L, size_t R, T v){ // assign v to range [L, R)
L += n;
R += n;
push(L);
push(R - 1);
for (; L < R; L >>= 1, R >>= 1){
if (L & 1) apply(L++, v);
if (R & 1) apply(--R, v);
}
}
T query(size_t i){
push(i + n);
return data[i + n];
}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
SegTree<int> seg(n);
while (q--){
int c;
cin >> c;
if (c == 0){
int s, t, x;
cin >> s >> t >> x;
seg.modify(s, t + 1, x);
}
else{
int i;
cin >> i;
cout << seg.query(i) << '\n';
}
}
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;
// Segment tree. Assignment on interval, single element access.
template<typename T>
class SegTree{
size_t n;
int h;
vector<T> data;
vector<bool> used;
static T combine(T L, T R){
return max(L, R);
}
void apply(size_t i, T v){
data[i] = v;
used[i] = true;
}
void push(size_t p){ // propagates the changes from the root to node p.
for (int s = h; s > 0; --s){
size_t i = p >> s;
if (used[i]){
apply(i*2, data[i]);
apply(i*2+1, data[i]);
used[i] = false;
}
}
}
int calc_h(size_t nn){
int hh = 1;
for (; nn > 1; ++hh, nn >>= 1);
return hh;
}
public:
SegTree(size_t _n): n(_n), h(calc_h(n)), data(vector<T>(2 * n, numeric_limits<int>::max())), used(vector<bool>(2 * n, false)){used[1] = true;}
SegTree(const vector<T> &src): n(src.size()), h(calc_h(n)), data(vector<T>(2 * n, 0)), used(vector<bool>(2 * n, false)){init(src);}
void init(const vector<T> &src) {for (size_t i = 0; i != n; ++i) apply(n + i, src[i]);}
void modify(size_t L, size_t R, T v){ // assign v to range [L, R)
L += n;
R += n;
push(L);
push(R - 1);
for (; L < R; L >>= 1, R >>= 1){
if (L & 1) apply(L++, v);
if (R & 1) apply(--R, v);
}
}
T query(size_t i){
push(i + n);
return data[i + n];
}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
SegTree<int> seg(n);
while (q--){
int c;
cin >> c;
if (c == 0){
int s, t, x;
cin >> s >> t >> x;
seg.modify(s, t + 1, x);
}
else{
int i;
cin >> i;
cout << seg.query(i) << '\n';
}
}
return 0;
}
``` |
#include <iostream>
#include <vector>
class SegmentTree {
std::vector<std::vector<int>> _array;
std::vector<int> _size;
static constexpr int undefined = -1;
void make_upper_undefined(int pos, int depth = 1) {
if (depth == _array.size()) return;
make_upper_undefined(pos / 2, depth + 1);
switch (_array[depth][pos]) {
case undefined: return;
default:
_array[depth - 1][pos * 2] = _array[depth][pos];
if (pos * 2 + 1 < _array[depth - 1].size()) _array[depth - 1][pos * 2 + 1] = _array[depth][pos];
_array[depth][pos] = undefined;
}
}
public:
SegmentTree(int size) :_array{}, _size{} {
for (auto i = size; i > 1; i = (i + 1) / 2) {
_size.push_back(i);
}
_size.push_back(1);
_array = std::vector<std::vector<int>>(_size.size());
for (auto i = 0; i < _array.size(); ++i) {
_array[i] = std::vector<int>(_size[i], undefined);
}
_array[_array.size() - 1][0] = 2147483647;
}
void update(int from, int to, int new_value) {
auto depth = 0;
while (from < to) {
if (from % 2 == 1) {
make_upper_undefined(from / 2, depth + 1);
_array[depth][from] = new_value;
++from;
}
if (to % 2 == 0) {
make_upper_undefined(to / 2, depth + 1);
_array[depth][to] = new_value;
--to;
}
from >>= 1; to >>= 1; ++depth;
}
if (from == to) {
make_upper_undefined(from / 2, depth + 1);
_array[depth][from] = new_value;
}
}
int find(int pos) const {
for (auto i = _array.size() - 1; i >= 0; --i) {
switch (_array[i][pos >> i]) {
case undefined: continue;
default: return _array[i][pos >> i];
}
}
}
};
int main() {
int n, q;
std::cin >> n >> q;
auto stree = SegmentTree(n);
int query, s, t, x, i;
for (auto count = 0; count < q; ++count) {
std::cin >> query;
switch (query) {
case 0:
std::cin >> s >> t >> x;
stree.update(s, t, x);
break;
default:
std::cin >> i;
std::cout << stree.find(i) << std::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>
class SegmentTree {
std::vector<std::vector<int>> _array;
std::vector<int> _size;
static constexpr int undefined = -1;
void make_upper_undefined(int pos, int depth = 1) {
if (depth == _array.size()) return;
make_upper_undefined(pos / 2, depth + 1);
switch (_array[depth][pos]) {
case undefined: return;
default:
_array[depth - 1][pos * 2] = _array[depth][pos];
if (pos * 2 + 1 < _array[depth - 1].size()) _array[depth - 1][pos * 2 + 1] = _array[depth][pos];
_array[depth][pos] = undefined;
}
}
public:
SegmentTree(int size) :_array{}, _size{} {
for (auto i = size; i > 1; i = (i + 1) / 2) {
_size.push_back(i);
}
_size.push_back(1);
_array = std::vector<std::vector<int>>(_size.size());
for (auto i = 0; i < _array.size(); ++i) {
_array[i] = std::vector<int>(_size[i], undefined);
}
_array[_array.size() - 1][0] = 2147483647;
}
void update(int from, int to, int new_value) {
auto depth = 0;
while (from < to) {
if (from % 2 == 1) {
make_upper_undefined(from / 2, depth + 1);
_array[depth][from] = new_value;
++from;
}
if (to % 2 == 0) {
make_upper_undefined(to / 2, depth + 1);
_array[depth][to] = new_value;
--to;
}
from >>= 1; to >>= 1; ++depth;
}
if (from == to) {
make_upper_undefined(from / 2, depth + 1);
_array[depth][from] = new_value;
}
}
int find(int pos) const {
for (auto i = _array.size() - 1; i >= 0; --i) {
switch (_array[i][pos >> i]) {
case undefined: continue;
default: return _array[i][pos >> i];
}
}
}
};
int main() {
int n, q;
std::cin >> n >> q;
auto stree = SegmentTree(n);
int query, s, t, x, i;
for (auto count = 0; count < q; ++count) {
std::cin >> query;
switch (query) {
case 0:
std::cin >> s >> t >> x;
stree.update(s, t, x);
break;
default:
std::cin >> i;
std::cout << stree.find(i) << std::endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct segtree_lazy {
int n;
vector<T> tree, lazy;
T def = numeric_limits<T>::max(), none = -1;
segtree_lazy(int n_) {
for (n = 1; n < n_; n *= 2) {
}
tree.assign(2 * n, def);
lazy.assign(2 * n, none);
}
T merge(T a, T b) {
return max(a, b);
}
void set_lazy(int index, T x) {
tree[index] = x;
lazy[index] = x;
}
void push(int index) {
if (lazy[index] == none) {
return;
}
set_lazy(2 * index, tree[index]);
set_lazy(2 * index + 1, tree[index]);
lazy[index] = none;
}
void fix(int index) {
tree[index] = merge(tree[2 * index], tree[2 * index + 1]);
}
void update(int a, int b, int index, int l, int r, T x) {
if (r <= a || b <= l) {
return;
}
if (a <= l && r <= b) {
set_lazy(index, x);
return;
}
push(index);
update(a, b, 2 * index, l, (l + r) / 2, x);
update(a, b, 2 * index + 1, (l + r) / 2, r, x);
fix(index);
}
void update(int l, int r, T x) {
update(l, r, 1, 0, n, x);
}
T query(int a, int b, int index, int l, int r) {
if (r <= a || b <= l) {
return none;
}
if (a <= l && r <= b) {
return tree[index];
}
push(index);
T puni = query(a, b, 2 * index, l, (l + r) / 2);
T muni = query(a, b, 2 * index + 1, (l + r) / 2, r);
return merge(puni, muni);
}
T query(int l, int r) {
return query(l, r, 1, 0, n);
}
};
int main() {
int n, q;
cin >> n >> q;
segtree_lazy<int> seg(n);
for (int i = 0; i < q; i++) {
bool b;
cin >> b;
if (b) {
int x;
cin >> x;
cout << seg.query(x, x + 1) << endl;
} else {
int s, t, x;
cin >> s >> t >> x;
seg.update(s, t + 1, x);
}
}
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;
template <typename T>
struct segtree_lazy {
int n;
vector<T> tree, lazy;
T def = numeric_limits<T>::max(), none = -1;
segtree_lazy(int n_) {
for (n = 1; n < n_; n *= 2) {
}
tree.assign(2 * n, def);
lazy.assign(2 * n, none);
}
T merge(T a, T b) {
return max(a, b);
}
void set_lazy(int index, T x) {
tree[index] = x;
lazy[index] = x;
}
void push(int index) {
if (lazy[index] == none) {
return;
}
set_lazy(2 * index, tree[index]);
set_lazy(2 * index + 1, tree[index]);
lazy[index] = none;
}
void fix(int index) {
tree[index] = merge(tree[2 * index], tree[2 * index + 1]);
}
void update(int a, int b, int index, int l, int r, T x) {
if (r <= a || b <= l) {
return;
}
if (a <= l && r <= b) {
set_lazy(index, x);
return;
}
push(index);
update(a, b, 2 * index, l, (l + r) / 2, x);
update(a, b, 2 * index + 1, (l + r) / 2, r, x);
fix(index);
}
void update(int l, int r, T x) {
update(l, r, 1, 0, n, x);
}
T query(int a, int b, int index, int l, int r) {
if (r <= a || b <= l) {
return none;
}
if (a <= l && r <= b) {
return tree[index];
}
push(index);
T puni = query(a, b, 2 * index, l, (l + r) / 2);
T muni = query(a, b, 2 * index + 1, (l + r) / 2, r);
return merge(puni, muni);
}
T query(int l, int r) {
return query(l, r, 1, 0, n);
}
};
int main() {
int n, q;
cin >> n >> q;
segtree_lazy<int> seg(n);
for (int i = 0; i < q; i++) {
bool b;
cin >> b;
if (b) {
int x;
cin >> x;
cout << seg.query(x, x + 1) << endl;
} else {
int s, t, x;
cin >> s >> t >> x;
seg.update(s, t + 1, x);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct range_update_point_value{
int N;
vector<pair<T, int>> ST;
int t;
range_update_point_value(int n){
N = 1;
while (N < n){
N *= 2;
}
ST = vector<pair<T, int>>(N * 2 - 1, make_pair(0, 0));
t = 1;
}
range_update_point_value(int n, T x){
N = 1;
while (N < n){
N *= 2;
}
ST = vector<pair<T, int>>(N * 2 - 1, make_pair(0, 0));
for (int i = 0; i < n; i++){
ST[N - 1 + i] = make_pair(x, 1);
}
t = 2;
}
range_update_point_value(vector<T> A){
int n = A.size();
N = 1;
while (N < n){
N *= 2;
}
ST = vector<pair<T, int>>(N * 2 - 1, make_pair(0, 0));
for (int i = 0; i < n; i++){
ST[N - 1 + i] = make_pair(A[i], 1);
}
t = 2;
}
T operator [](int k){
k += N - 1;
T ans = ST[k].first;
T time = ST[k].second;
while (k > 0){
k = (k - 1) / 2;
if (ST[k].second > time){
ans = ST[k].first;
time = ST[k].second;
}
}
return ans;
}
void update(int L, int R, T x, int i, int l, int r){
if (R <= l || r <= L){
return;
} else if (L <= l && r <= R){
ST[i] = make_pair(x, t);
} else {
int m = (l + r) / 2;
update(L, R, x, i * 2 + 1, l, m);
update(L, R, x, i * 2 + 2, m, r);
}
}
void range_update(int L, int R, T x){
update(L, R, x, 0, 0, N);
t++;
}
};
int main(){
int n, q;
cin >> n >> q;
range_update_point_value<int> a(n, 2147483647);
for (int c = 0; c < q; c++){
int T;
cin >> T;
if (T == 0){
int s, t, x;
cin >> s >> t >> x;
a.range_update(s, t + 1, x);
}
if (T == 1){
int i;
cin >> i;
cout << a[i] << endl;
}
}
}
| ### 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>
using namespace std;
template <typename T>
struct range_update_point_value{
int N;
vector<pair<T, int>> ST;
int t;
range_update_point_value(int n){
N = 1;
while (N < n){
N *= 2;
}
ST = vector<pair<T, int>>(N * 2 - 1, make_pair(0, 0));
t = 1;
}
range_update_point_value(int n, T x){
N = 1;
while (N < n){
N *= 2;
}
ST = vector<pair<T, int>>(N * 2 - 1, make_pair(0, 0));
for (int i = 0; i < n; i++){
ST[N - 1 + i] = make_pair(x, 1);
}
t = 2;
}
range_update_point_value(vector<T> A){
int n = A.size();
N = 1;
while (N < n){
N *= 2;
}
ST = vector<pair<T, int>>(N * 2 - 1, make_pair(0, 0));
for (int i = 0; i < n; i++){
ST[N - 1 + i] = make_pair(A[i], 1);
}
t = 2;
}
T operator [](int k){
k += N - 1;
T ans = ST[k].first;
T time = ST[k].second;
while (k > 0){
k = (k - 1) / 2;
if (ST[k].second > time){
ans = ST[k].first;
time = ST[k].second;
}
}
return ans;
}
void update(int L, int R, T x, int i, int l, int r){
if (R <= l || r <= L){
return;
} else if (L <= l && r <= R){
ST[i] = make_pair(x, t);
} else {
int m = (l + r) / 2;
update(L, R, x, i * 2 + 1, l, m);
update(L, R, x, i * 2 + 2, m, r);
}
}
void range_update(int L, int R, T x){
update(L, R, x, 0, 0, N);
t++;
}
};
int main(){
int n, q;
cin >> n >> q;
range_update_point_value<int> a(n, 2147483647);
for (int c = 0; c < q; c++){
int T;
cin >> T;
if (T == 0){
int s, t, x;
cin >> s >> t >> x;
a.range_update(s, t + 1, x);
}
if (T == 1){
int i;
cin >> i;
cout << a[i] << endl;
}
}
}
``` |
#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef pair<int, int>P;
int dat[400000], lazy[400000], N;
inline void push(int k) {
if (lazy[k] == -1)return;
dat[k] = lazy[k];
if (k < N - 1) {
lazy[2 * k + 1] = lazy[k];
lazy[2 * k + 2] = lazy[k];
}
lazy[k] = -1;
}
inline void update_node(int k) {
dat[k] = min(dat[2 * k + 1], dat[2 * k + 2]);
}
inline void update(int a, int b, int x, int k, int l, int r) {
push(k);
if (r <= a || b <= l)return;
if (a <= l&&r <= b) {
lazy[k] = x; push(k); return;
}
update(a, b, x, k * 2 + 1, l, (l + r) / 2); update(a, b, x, k * 2 + 2, (l + r) / 2, r);
update_node(k);
}
inline int query(int a, int b, int k, int l, int r) {
push(k);
if (r <= a || b <= l)return INT_MAX;
if (a <= l&&r <= b)return dat[k];
int lb = query(a, b, k * 2 + 1, l, (l + r) / 2);
int rb = query(a, b, k * 2 + 2, (l + r) / 2, r);
update_node(k);
return min(lb, rb);
}
int main() {
int n, q; scanf("%d%d", &n, &q);
N = 1; while (N < n)N <<= 1;
rep(i, 2 * N - 1)dat[i] = INT_MAX, lazy[i] = -1;
rep(i, q) {
int t; scanf("%d", &t);
if (t == 0) {
int a, b, x; scanf("%d%d%d", &a, &b, &x);
update(a, b + 1, x, 0, 0, N);
}
else {
int a; scanf("%d", &a);
printf("%d\n", query(a, a + 1, 0, 0, N));
}
}
} | ### 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 rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef pair<int, int>P;
int dat[400000], lazy[400000], N;
inline void push(int k) {
if (lazy[k] == -1)return;
dat[k] = lazy[k];
if (k < N - 1) {
lazy[2 * k + 1] = lazy[k];
lazy[2 * k + 2] = lazy[k];
}
lazy[k] = -1;
}
inline void update_node(int k) {
dat[k] = min(dat[2 * k + 1], dat[2 * k + 2]);
}
inline void update(int a, int b, int x, int k, int l, int r) {
push(k);
if (r <= a || b <= l)return;
if (a <= l&&r <= b) {
lazy[k] = x; push(k); return;
}
update(a, b, x, k * 2 + 1, l, (l + r) / 2); update(a, b, x, k * 2 + 2, (l + r) / 2, r);
update_node(k);
}
inline int query(int a, int b, int k, int l, int r) {
push(k);
if (r <= a || b <= l)return INT_MAX;
if (a <= l&&r <= b)return dat[k];
int lb = query(a, b, k * 2 + 1, l, (l + r) / 2);
int rb = query(a, b, k * 2 + 2, (l + r) / 2, r);
update_node(k);
return min(lb, rb);
}
int main() {
int n, q; scanf("%d%d", &n, &q);
N = 1; while (N < n)N <<= 1;
rep(i, 2 * N - 1)dat[i] = INT_MAX, lazy[i] = -1;
rep(i, q) {
int t; scanf("%d", &t);
if (t == 0) {
int a, b, x; scanf("%d%d%d", &a, &b, &x);
update(a, b + 1, x, 0, 0, N);
}
else {
int a; scanf("%d", &a);
printf("%d\n", query(a, a + 1, 0, 0, N));
}
}
}
``` |
#include <cstdio>
#include <utility>
#include <algorithm>
using namespace std;
using uint=unsigned int;
using update_t=pair<int,uint>;//time value
const int MAXN=1e5+10;
struct Node{
int lbound,rbound;
update_t update;
Node *left,*right;
};
int A[MAXN];
Node segtree[MAXN*2];
Node* next_pos=segtree;
int n,q;
Node* build(int left,int right){
auto* cur=next_pos;
next_pos++;
cur->lbound=left;
cur->rbound=right;
cur->update=update_t(0,(1u<<31)-1);
int mid=(left+right)/2;
if (right-left==1) return cur;
cur->left=build(left,mid);
cur->right=build(mid,right);
return cur;
}
void update(Node& self,int l,int r,update_t u){
// puts("s");
int mid=(self.lbound+self.rbound)/2;
if (l==self.lbound&&r==self.rbound){
self.update=u;
}else if (l>=mid){
update(*self.right,l,r,u);
}else if (r<=mid){
update(*self.left,l,r,u);
}else{
update(*self.left,l,mid,u);
update(*self.right,mid,r,u);
}
}
update_t query(Node& self,int i){
// puts("q");
if (self.rbound-self.lbound==1&&self.lbound==i) return self.update;
int mid=(self.lbound+self.rbound)/2;
if (i>=mid) return max(query(*self.right,i),self.update);
else return max(query(*self.left,i),self.update);
}
int main(){
scanf("%d%d",&n,&q);
build(0,n);
// puts("yes");
for(int time=1;time<=q;time++){
int t;
scanf("%d",&t);
if (t==0) {
int s,t,x;
scanf("%d%d%d",&s,&t,&x);
update(segtree[0],s,t+1,update_t(time,x));
// puts("updated");
}else{
int i;
// puts("ok");
scanf("%d",&i);
auto v=query(segtree[0],i).second;
printf("%u\n",v);
}
}
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 <cstdio>
#include <utility>
#include <algorithm>
using namespace std;
using uint=unsigned int;
using update_t=pair<int,uint>;//time value
const int MAXN=1e5+10;
struct Node{
int lbound,rbound;
update_t update;
Node *left,*right;
};
int A[MAXN];
Node segtree[MAXN*2];
Node* next_pos=segtree;
int n,q;
Node* build(int left,int right){
auto* cur=next_pos;
next_pos++;
cur->lbound=left;
cur->rbound=right;
cur->update=update_t(0,(1u<<31)-1);
int mid=(left+right)/2;
if (right-left==1) return cur;
cur->left=build(left,mid);
cur->right=build(mid,right);
return cur;
}
void update(Node& self,int l,int r,update_t u){
// puts("s");
int mid=(self.lbound+self.rbound)/2;
if (l==self.lbound&&r==self.rbound){
self.update=u;
}else if (l>=mid){
update(*self.right,l,r,u);
}else if (r<=mid){
update(*self.left,l,r,u);
}else{
update(*self.left,l,mid,u);
update(*self.right,mid,r,u);
}
}
update_t query(Node& self,int i){
// puts("q");
if (self.rbound-self.lbound==1&&self.lbound==i) return self.update;
int mid=(self.lbound+self.rbound)/2;
if (i>=mid) return max(query(*self.right,i),self.update);
else return max(query(*self.left,i),self.update);
}
int main(){
scanf("%d%d",&n,&q);
build(0,n);
// puts("yes");
for(int time=1;time<=q;time++){
int t;
scanf("%d",&t);
if (t==0) {
int s,t,x;
scanf("%d%d%d",&s,&t,&x);
update(segtree[0],s,t+1,update_t(time,x));
// puts("updated");
}else{
int i;
// puts("ok");
scanf("%d",&i);
auto v=query(segtree[0],i).second;
printf("%u\n",v);
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define INF (1 << 30) - 1
#define LLINF (1LL << 61) - 1
// #define int ll
using namespace std;
const int MOD = 1000000007;
const int MAX = 510000;
static const int MAX_N = 1<<17;
pii dat[2 * MAX_N -1];
int n;
struct SegmentTree{
SegmentTree(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=(1LL<<31)-1;
}
}
int query(int k){
k += n-1;
pii value=dat[k];
while(k>0){
k=(k-1)/2;
value=max(value,dat[k]);
}
return value.second;
}
void update(int a,int b,int k,pii P,int l,int r){
if(r<=a || b<=l) return;
if(a<=l && r<=b){
dat[k]=P;
}
else{
update(a,b,k*2+1,P,l,(l+r)/2);
update(a,b,k*2+2,P,(l+r)/2,r);
}
}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int _n,q;
cin>>_n>>q;
SegmentTree sg(_n);
rep(i,q){
int c;
cin>>c;
if(!c){
int s,t,x;
cin>>s>>t>>x;
sg.update(s,t+1,0,pii(i,x),0,n);
} else{
int x;
cin>>x;
cout<<sg.query(x)<<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>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define INF (1 << 30) - 1
#define LLINF (1LL << 61) - 1
// #define int ll
using namespace std;
const int MOD = 1000000007;
const int MAX = 510000;
static const int MAX_N = 1<<17;
pii dat[2 * MAX_N -1];
int n;
struct SegmentTree{
SegmentTree(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=(1LL<<31)-1;
}
}
int query(int k){
k += n-1;
pii value=dat[k];
while(k>0){
k=(k-1)/2;
value=max(value,dat[k]);
}
return value.second;
}
void update(int a,int b,int k,pii P,int l,int r){
if(r<=a || b<=l) return;
if(a<=l && r<=b){
dat[k]=P;
}
else{
update(a,b,k*2+1,P,l,(l+r)/2);
update(a,b,k*2+2,P,(l+r)/2,r);
}
}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int _n,q;
cin>>_n>>q;
SegmentTree sg(_n);
rep(i,q){
int c;
cin>>c;
if(!c){
int s,t,x;
cin>>s>>t>>x;
sg.update(s,t+1,0,pii(i,x),0,n);
} else{
int x;
cin>>x;
cout<<sg.query(x)<<endl;
}
}
return 0;
}
``` |
#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)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef pair<int,int> P;
typedef long long int ll;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MAX_N = 1 << 19;
#define NUM 2147483647
int nn = 1;
int data[MAX_N];
void init(int first_N){
nn = 1;
if(first_N < 10) nn = 100;
while(nn < first_N)nn *= 2;
}
// [left,right]の区間の値をvalueに更新する
// update_(left,right,value,0,0,nn-1)のように呼ぶ
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){
data[node_id] = new_value;
}else{
if(data[node_id] >= 0){
data[2*node_id+1] = data[node_id];
data[2*node_id+2] = data[node_id];
data[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_(ite,ite,0,0,nn-1)のように呼ぶ
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 && data[node_id] >= 0){
return data[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);
}
}
int main(){
int n,Q;
cin >> n >> Q;
init(n);
for(int i = 0; i <= 2*nn-2; i++)data[i] = NUM;
rep(i,Q){
int command;
cin >> command;
if(command == 0){
int left,right,value;
cin >> left >> right >> value;
update_(left,right,value,0,0,nn-1);
}else{
int loc;
cin >> loc;
cout << query_(loc,loc,0,0,nn-1) << endl;
}
}
}
| ### 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 <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)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef pair<int,int> P;
typedef long long int ll;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MAX_N = 1 << 19;
#define NUM 2147483647
int nn = 1;
int data[MAX_N];
void init(int first_N){
nn = 1;
if(first_N < 10) nn = 100;
while(nn < first_N)nn *= 2;
}
// [left,right]の区間の値をvalueに更新する
// update_(left,right,value,0,0,nn-1)のように呼ぶ
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){
data[node_id] = new_value;
}else{
if(data[node_id] >= 0){
data[2*node_id+1] = data[node_id];
data[2*node_id+2] = data[node_id];
data[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_(ite,ite,0,0,nn-1)のように呼ぶ
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 && data[node_id] >= 0){
return data[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);
}
}
int main(){
int n,Q;
cin >> n >> Q;
init(n);
for(int i = 0; i <= 2*nn-2; i++)data[i] = NUM;
rep(i,Q){
int command;
cin >> command;
if(command == 0){
int left,right,value;
cin >> left >> right >> value;
update_(left,right,value,0,0,nn-1);
}else{
int loc;
cin >> loc;
cout << query_(loc,loc,0,0,nn-1) << endl;
}
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define MAX 500005
//#define INF 1001001001
template <typename T>
struct RMQ {
const T INF = numeric_limits<T>::max();
int n;
vector<T> dat, lazy;
RMQ(int _n) : n(), dat(_n*4,INF), lazy(_n*4,INF) {
int x = 1;
while (x < _n) x <<= 1;
n = x;
}
void update(int i, T x) {
i += n-1;
dat[i] = x;
while (i > 0) {
i = (i-1)/2;
dat[i] = min(dat[i*2+1],dat[i*2+2]);
}
}
T query(int a, int b, int k, int l, int r) {
eval(k);
if (r <= a || b <= l) return INF;
if (a <= l && r <= b) return dat[k];
T mid = (l+r)>>1;
T vl = query(a,b,k*2+1,l,mid);
T vr = query(a,b,k*2+2,mid,r);
return min(vl,vr);
}
T query(int a, int b) { return query(a,b,0,0,n); }
void eval(int k) {
if (lazy[k] == INF) return;
if (k < n-1) lazy[k*2+1] = lazy[k*2+2] = lazy[k];
dat[k] = lazy[k];
lazy[k] = INF;
}
void update(int a, int b, int x, int k, int l, int r) {
eval(k);
if (r <= a || b <= l) return;
if (a <= l && r <= b) {
lazy[k] = x;
eval(k);
} else {
int mid = (l+r)>>1;
update(a,b,x,k*2+1,l,mid);
update(a,b,x,k*2+2,mid,r);
dat[k] = min(dat[k*2+1],dat[k*2+2]);
}
}
void update(int a, int b, int x) { update(a,b,x,0,0,n); }
inline T operator[](int a) { return query(a,a+1); }
};
int main(int, char**)
{
int n, q;
cin >> n >> q;
RMQ<int> rmq(n);
vector<int> ans;
rep(i,q) {
int command; cin >> command;
if (command == 0) {
int s, t, x;
cin >> s >> t >> x;
rmq.update(s,t+1,x);
} else if (command == 1) {
int i; cin >> i;
ans.push_back(rmq[i]);
}
}
for (auto a : ans) cout << a << 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<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define MAX 500005
//#define INF 1001001001
template <typename T>
struct RMQ {
const T INF = numeric_limits<T>::max();
int n;
vector<T> dat, lazy;
RMQ(int _n) : n(), dat(_n*4,INF), lazy(_n*4,INF) {
int x = 1;
while (x < _n) x <<= 1;
n = x;
}
void update(int i, T x) {
i += n-1;
dat[i] = x;
while (i > 0) {
i = (i-1)/2;
dat[i] = min(dat[i*2+1],dat[i*2+2]);
}
}
T query(int a, int b, int k, int l, int r) {
eval(k);
if (r <= a || b <= l) return INF;
if (a <= l && r <= b) return dat[k];
T mid = (l+r)>>1;
T vl = query(a,b,k*2+1,l,mid);
T vr = query(a,b,k*2+2,mid,r);
return min(vl,vr);
}
T query(int a, int b) { return query(a,b,0,0,n); }
void eval(int k) {
if (lazy[k] == INF) return;
if (k < n-1) lazy[k*2+1] = lazy[k*2+2] = lazy[k];
dat[k] = lazy[k];
lazy[k] = INF;
}
void update(int a, int b, int x, int k, int l, int r) {
eval(k);
if (r <= a || b <= l) return;
if (a <= l && r <= b) {
lazy[k] = x;
eval(k);
} else {
int mid = (l+r)>>1;
update(a,b,x,k*2+1,l,mid);
update(a,b,x,k*2+2,mid,r);
dat[k] = min(dat[k*2+1],dat[k*2+2]);
}
}
void update(int a, int b, int x) { update(a,b,x,0,0,n); }
inline T operator[](int a) { return query(a,a+1); }
};
int main(int, char**)
{
int n, q;
cin >> n >> q;
RMQ<int> rmq(n);
vector<int> ans;
rep(i,q) {
int command; cin >> command;
if (command == 0) {
int s, t, x;
cin >> s >> t >> x;
rmq.update(s,t+1,x);
} else if (command == 1) {
int i; cin >> i;
ans.push_back(rmq[i]);
}
}
for (auto a : ans) cout << a << endl;
return 0;
}
``` |
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#define REP(i,n) for(int i = 0;i < (n);i++)
#define pb push_back
using namespace std;
const int INF = 1e9;
typedef long long ll;
int main(){
int n,q;
cin >> n >> q;
vector <int> res;
int a[n];
int sq = sqrt(n);
int nsq = n/sq+1;
int lazy[nsq];
fill(a,a+n,2147483647);
fill(lazy,lazy+nsq,-1);
REP(i,q){
int z;
cin >> z;
if(z == 0){
int s,t,x;
cin >> s >> t >> x;
/*if(s == t){
a[s] = x;
}*/
if(t/sq == s/sq){
if(lazy[t/sq] >= 0){
fill_n(a+(t/sq)*sq,sq,lazy[t/sq]);
lazy[t/sq] = -1;
}
if(t-s+1 == sq){
lazy[t/sq] = x;
}
else{
fill_n(a+s,t-s+1,x);
}
//cout << "debug1" <<endl;
}
else{
//cout << "debug3" << endl;
int f = s,to = t;
while(to%sq != (sq - 1)){
to--;
}
while(f%sq != 0){
f++;
}
if(lazy[t/sq] >= 0){
fill_n(a+(t/sq)*sq,sq,lazy[t/sq]);
}
if(lazy[s/sq] >= 0){
fill_n(a+(s/sq)*sq,sq,lazy[s/sq]);
}
fill_n(a+to+1,t-to,x);
lazy[t/sq] = -1;
fill_n(a+s,f-s,x);
lazy[s/sq] = -1;
f = f/sq;
to = to/sq;
if(f <= to){
fill_n(lazy+f,to-f+1,x);
}
}
/*REP(i,n)
cout << a[i] << " ";
cout << endl;*/
}
else{
int x;
cin >> x;
int te = x / sq;
if(lazy[te] < 0){
//cout << a[x] << endl;
res.pb(a[x]);
}
else{
fill_n(a+te*sq,sq,lazy[te]);
lazy[te] = -1;
//cout << a[x] << endl;
res.pb(a[x]);
}
}
}
for(int i = 0;i < res.size();i++){
cout << res[i] << endl;
}
//cout << res.size() << endl;
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<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#define REP(i,n) for(int i = 0;i < (n);i++)
#define pb push_back
using namespace std;
const int INF = 1e9;
typedef long long ll;
int main(){
int n,q;
cin >> n >> q;
vector <int> res;
int a[n];
int sq = sqrt(n);
int nsq = n/sq+1;
int lazy[nsq];
fill(a,a+n,2147483647);
fill(lazy,lazy+nsq,-1);
REP(i,q){
int z;
cin >> z;
if(z == 0){
int s,t,x;
cin >> s >> t >> x;
/*if(s == t){
a[s] = x;
}*/
if(t/sq == s/sq){
if(lazy[t/sq] >= 0){
fill_n(a+(t/sq)*sq,sq,lazy[t/sq]);
lazy[t/sq] = -1;
}
if(t-s+1 == sq){
lazy[t/sq] = x;
}
else{
fill_n(a+s,t-s+1,x);
}
//cout << "debug1" <<endl;
}
else{
//cout << "debug3" << endl;
int f = s,to = t;
while(to%sq != (sq - 1)){
to--;
}
while(f%sq != 0){
f++;
}
if(lazy[t/sq] >= 0){
fill_n(a+(t/sq)*sq,sq,lazy[t/sq]);
}
if(lazy[s/sq] >= 0){
fill_n(a+(s/sq)*sq,sq,lazy[s/sq]);
}
fill_n(a+to+1,t-to,x);
lazy[t/sq] = -1;
fill_n(a+s,f-s,x);
lazy[s/sq] = -1;
f = f/sq;
to = to/sq;
if(f <= to){
fill_n(lazy+f,to-f+1,x);
}
}
/*REP(i,n)
cout << a[i] << " ";
cout << endl;*/
}
else{
int x;
cin >> x;
int te = x / sq;
if(lazy[te] < 0){
//cout << a[x] << endl;
res.pb(a[x]);
}
else{
fill_n(a+te*sq,sq,lazy[te]);
lazy[te] = -1;
//cout << a[x] << endl;
res.pb(a[x]);
}
}
}
for(int i = 0;i < res.size();i++){
cout << res[i] << endl;
}
//cout << res.size() << endl;
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
//BEGIN CUT HERE
template <typename T,typename E>
struct SegmentTree{
typedef function<T(T,E)> G;
typedef function<T(E,E)> H;
int n;
G g;
H h;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(){};
SegmentTree(int n_,G g,H h,T d1,E d0,
vector<T> v=vector<T>()):
g(g),h(h),d1(d1),d0(d0){
init(n_);
if(n_==(int)v.size()) build(n_,v);
}
void init(int n_){
n=1;
while(n<n_) n*=2;
dat.clear();
dat.resize(n,d1);
laz.clear();
laz.resize(2*n-1,d0);
}
void build(int n_, vector<T> v){
for(int i=0;i<n_;i++) dat[i]=v[i];
}
void eval(int k){
if(k*2+1<2*n-1){
laz[k*2+1]=h(laz[k*2+1],laz[k]);
laz[k*2+2]=h(laz[k*2+2],laz[k]);
laz[k]=d0;
}
}
void update(int a,int b,E x,int k,int l,int r){
eval(k);
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
laz[k]=h(laz[k],x);
return;
}
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
void update(int a,int b,E x){
update(a,b,x,0,0,n);
}
void eval2(int k){
if(k) eval2((k-1)/2);
eval(k);
}
T query(int k){
T c=dat[k];
k+=n-1;
eval2(k);
return g(c,laz[k]);
}
};
//END CUT HERE
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n,q;
cin>>n>>q;
SegmentTree<int,int> beet(n,
[&](int a,int b){ return b<0?a:b;},
[&](int a,int b){ return b<0?a:b;},
INT_MAX,-1);
for(int i=0;i<q;i++){
int c;
cin>>c;
if(c){
int x;
cin>>x;
cout<<beet.query(x)<<endl;
}else{
int s,t,x;
cin>>s>>t>>x;
t++;
beet.update(s,t,x);
}
}
return 0;
}
/*
verified on 2017/11/05
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_D&lang=jp
*/ | ### 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;
//BEGIN CUT HERE
template <typename T,typename E>
struct SegmentTree{
typedef function<T(T,E)> G;
typedef function<T(E,E)> H;
int n;
G g;
H h;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(){};
SegmentTree(int n_,G g,H h,T d1,E d0,
vector<T> v=vector<T>()):
g(g),h(h),d1(d1),d0(d0){
init(n_);
if(n_==(int)v.size()) build(n_,v);
}
void init(int n_){
n=1;
while(n<n_) n*=2;
dat.clear();
dat.resize(n,d1);
laz.clear();
laz.resize(2*n-1,d0);
}
void build(int n_, vector<T> v){
for(int i=0;i<n_;i++) dat[i]=v[i];
}
void eval(int k){
if(k*2+1<2*n-1){
laz[k*2+1]=h(laz[k*2+1],laz[k]);
laz[k*2+2]=h(laz[k*2+2],laz[k]);
laz[k]=d0;
}
}
void update(int a,int b,E x,int k,int l,int r){
eval(k);
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
laz[k]=h(laz[k],x);
return;
}
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
void update(int a,int b,E x){
update(a,b,x,0,0,n);
}
void eval2(int k){
if(k) eval2((k-1)/2);
eval(k);
}
T query(int k){
T c=dat[k];
k+=n-1;
eval2(k);
return g(c,laz[k]);
}
};
//END CUT HERE
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n,q;
cin>>n>>q;
SegmentTree<int,int> beet(n,
[&](int a,int b){ return b<0?a:b;},
[&](int a,int b){ return b<0?a:b;},
INT_MAX,-1);
for(int i=0;i<q;i++){
int c;
cin>>c;
if(c){
int x;
cin>>x;
cout<<beet.query(x)<<endl;
}else{
int s,t,x;
cin>>s>>t>>x;
t++;
beet.update(s,t,x);
}
}
return 0;
}
/*
verified on 2017/11/05
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_D&lang=jp
*/
``` |
//Range Update Query
//reference https://kujira16.hateblo.jp/entry/2016/12/15/000000
//https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_D
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
template <typename T>
struct RUQ {
T n;
T k;//バケットの数
T sqrt_n;//バケットの大きさ
vector<T> data;
vector<T> lazy_update;
T INF;
RUQ() = delete;
RUQ(T n_,T sqrt_n_ = 1000,T INF_ = 1e9) {
initialize(n_,sqrt_n_,INF_);
}
void initialize(T n_,T sqrt_n_ = 1000,T INF_ = 1e9) {
n = n_;
sqrt_n = sqrt_n_;
k = (n + sqrt_n - 1) / sqrt_n;//切り上げ割り算
data.clear();lazy_update.clear();
data.resize(k * sqrt_n,INF_);
lazy_update.resize(k,INF_ + 1);
INF = INF_;
}
void delay_propagation(T bucket_id) {
if (lazy_update[bucket_id] <= INF) {
for (int i = bucket_id * sqrt_n;i < (bucket_id + 1) * sqrt_n;i++) {
data[i] = lazy_update[bucket_id];
}
lazy_update[bucket_id] = INF + 1;
}
}
void update(T a,T b,T x) {
if (a >= sqrt_n * k || b > sqrt_n * k) {
cerr << "error index k" << endl;
return;
}
if (a >= n || b > n) {
cerr << "warning index k" << endl;
}
rep (i,k) {
T l = i * sqrt_n,r = (i + 1) * sqrt_n;
if (b <= l|| r <= a) continue;
if (l < a || b < r) {
delay_propagation(i);
for (int i = max(l,a);i < min(b,r);i++) {
data[i] = x;
}
} else {
lazy_update[i] = x;
}
}
}
T get(T i) { // i番目(0-indexed)の値を取得する
T bucket_id = i / sqrt_n;
if (lazy_update[bucket_id] > INF)return data[i];
return lazy_update[bucket_id];
}
};
using ll = long long;
signed main() {
ll n,q;
cin >> n >> q;
RUQ<ll> ruq(n,1000,(1ll << 31) - 1ll);
rep(i,q) {
ll c;
cin >> c;
if(c) {
ll i;
cin >> i;
cout << ruq.get(i) << endl;
} else {
ll s,t,x;
cin >> s >> t >> x;
ruq.update(s,t+1,x);
}
}
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
//Range Update Query
//reference https://kujira16.hateblo.jp/entry/2016/12/15/000000
//https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_D
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
template <typename T>
struct RUQ {
T n;
T k;//バケットの数
T sqrt_n;//バケットの大きさ
vector<T> data;
vector<T> lazy_update;
T INF;
RUQ() = delete;
RUQ(T n_,T sqrt_n_ = 1000,T INF_ = 1e9) {
initialize(n_,sqrt_n_,INF_);
}
void initialize(T n_,T sqrt_n_ = 1000,T INF_ = 1e9) {
n = n_;
sqrt_n = sqrt_n_;
k = (n + sqrt_n - 1) / sqrt_n;//切り上げ割り算
data.clear();lazy_update.clear();
data.resize(k * sqrt_n,INF_);
lazy_update.resize(k,INF_ + 1);
INF = INF_;
}
void delay_propagation(T bucket_id) {
if (lazy_update[bucket_id] <= INF) {
for (int i = bucket_id * sqrt_n;i < (bucket_id + 1) * sqrt_n;i++) {
data[i] = lazy_update[bucket_id];
}
lazy_update[bucket_id] = INF + 1;
}
}
void update(T a,T b,T x) {
if (a >= sqrt_n * k || b > sqrt_n * k) {
cerr << "error index k" << endl;
return;
}
if (a >= n || b > n) {
cerr << "warning index k" << endl;
}
rep (i,k) {
T l = i * sqrt_n,r = (i + 1) * sqrt_n;
if (b <= l|| r <= a) continue;
if (l < a || b < r) {
delay_propagation(i);
for (int i = max(l,a);i < min(b,r);i++) {
data[i] = x;
}
} else {
lazy_update[i] = x;
}
}
}
T get(T i) { // i番目(0-indexed)の値を取得する
T bucket_id = i / sqrt_n;
if (lazy_update[bucket_id] > INF)return data[i];
return lazy_update[bucket_id];
}
};
using ll = long long;
signed main() {
ll n,q;
cin >> n >> q;
RUQ<ll> ruq(n,1000,(1ll << 31) - 1ll);
rep(i,q) {
ll c;
cin >> c;
if(c) {
ll i;
cin >> i;
cout << ruq.get(i) << endl;
} else {
ll s,t,x;
cin >> s >> t >> x;
ruq.update(s,t+1,x);
}
}
return 0;
}
``` |
#include <iostream>
#include <vector>
using namespace std;
#define int long long
const int INF = (1LL << 31) - 1;
const int MAX = 1<<9;
struct LazySegmentTree {
int N, K;
vector<int> D;
vector<bool> lazyFlag;
vector<int> lazy;
LazySegmentTree(int n) : N(n) {
K = (N + MAX - 1) / MAX;
D.assign(K * MAX, INF);
lazyFlag.assign(K, false);
lazy.assign(K, 0);
}
void eval(int k) {
if (lazyFlag[k]) {
lazyFlag[k] = false;
for (int i = k * MAX; i < (k + 1) * MAX; ++i) {
D[i] = lazy[k];
}
}
}
void update(int s, int t, int x) {
for (int k = 0; k < K; ++k) {
int l = k * MAX, r = (k + 1) * MAX;
if (r <= s || t <= l)
continue;
if (s <= l && r <= t) {
lazyFlag[k] = true;
lazy[k] = x;
} else {
eval(k);
for (int i = max(s, l); i < min(t, r); ++i) {
D[i] = x;
}
}
}
}
int find(int i) {
int k = i / MAX;
eval(k);
return D[i];
}
};
signed main() {
int p, q;
cin >> p >> q;
LazySegmentTree segtree(p);
for(int i=0; i<q; i++) {
int com;
cin >> com;
if (com == 0) {
int s, t, x;
cin >> s >> t >> x;
segtree.update(s, t + 1, x);
} else {
int i;
cin >> i;
cout << segtree.find(i) << 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;
#define int long long
const int INF = (1LL << 31) - 1;
const int MAX = 1<<9;
struct LazySegmentTree {
int N, K;
vector<int> D;
vector<bool> lazyFlag;
vector<int> lazy;
LazySegmentTree(int n) : N(n) {
K = (N + MAX - 1) / MAX;
D.assign(K * MAX, INF);
lazyFlag.assign(K, false);
lazy.assign(K, 0);
}
void eval(int k) {
if (lazyFlag[k]) {
lazyFlag[k] = false;
for (int i = k * MAX; i < (k + 1) * MAX; ++i) {
D[i] = lazy[k];
}
}
}
void update(int s, int t, int x) {
for (int k = 0; k < K; ++k) {
int l = k * MAX, r = (k + 1) * MAX;
if (r <= s || t <= l)
continue;
if (s <= l && r <= t) {
lazyFlag[k] = true;
lazy[k] = x;
} else {
eval(k);
for (int i = max(s, l); i < min(t, r); ++i) {
D[i] = x;
}
}
}
}
int find(int i) {
int k = i / MAX;
eval(k);
return D[i];
}
};
signed main() {
int p, q;
cin >> p >> q;
LazySegmentTree segtree(p);
for(int i=0; i<q; i++) {
int com;
cin >> com;
if (com == 0) {
int s, t, x;
cin >> s >> t >> x;
segtree.update(s, t + 1, x);
} else {
int i;
cin >> i;
cout << segtree.find(i) << endl;
}
}
}
``` |
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
//const int INF = 1e8;
using namespace std;
struct RUQ{
using T = int;
T operator()(const T &a, const T &b) { return min(a,b); };
static constexpr T identity() { return INT_MAX; }
};
template<class Monoid>
class rangeUpdateQuery{
private:
using T = typename Monoid::T;
Monoid op;
const int n;
vector<T> dat, lazy;
int query(int a, int b, int k, int l, int r){
evaluate(k);
if(b <= l || r <= a) return INT_MAX;
else if(a <= l && r <= b) return dat[k];
else{
int vl = query(a, b, k * 2, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 1, (l + r) / 2, r);
return min(vl, vr);
}
}
inline void evaluate(int k){
if(lazy[k] == op.identity()) return;
dat[k] = lazy[k];
if(k < n){
lazy[2 * k] = lazy[k];
lazy[2 * k + 1] = lazy[k];
}
lazy[k] = op.identity();
}
void update(int a, int b, int k, int l, int r, int x){
evaluate(k);
if(r <= a || b <= l) return;
if(a <= l && r <= b){
lazy[k] = x;
}else if(l < b && a < r){
evaluate(k);
update(a, b, k * 2, l, (l + r) / 2, x);
update(a, b, k * 2 + 1, (l + r) / 2, r, x);
}
}
int power(int n){
int res = 1;
while(n >= res) res*=2;
return res;
}
public:
rangeUpdateQuery(int n) : n(power(n)), dat(4 * n, op.identity()), lazy(4 * n, op.identity()) {}
rangeUpdateQuery(const vector<T> &v) : n(v.size()), dat(4 * n), lazy(4 * n, op.identity()){
copy(begin(v), end(v), begin(dat) + n);
for(int i = n - 1; i > 0; i--) dat[i] = op(dat[2 * i], dat[2 * i + 1]);
}
int query(int a, int b){ return query(a,b,1,0,n); }
void update(int s, int t, int x){ update(s, t, 1, 0, n, x); }
int get(int a){ return query(a, a + 1); };
};
int main(){
int n, q;
cin >> n >> q;
rangeUpdateQuery<RUQ> seg(n);
rep(i,q){
int com;
cin >> com;
if(not com){
int s, t, x;
cin >> s >> t >> x;
//cout << s << ' ' << t << endl;
seg.update(s + 1, t + 2, x);
}else{
int p;
cin >> p;
cout << seg.get(p + 1) << endl;
}
}
}
| ### 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>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
//const int INF = 1e8;
using namespace std;
struct RUQ{
using T = int;
T operator()(const T &a, const T &b) { return min(a,b); };
static constexpr T identity() { return INT_MAX; }
};
template<class Monoid>
class rangeUpdateQuery{
private:
using T = typename Monoid::T;
Monoid op;
const int n;
vector<T> dat, lazy;
int query(int a, int b, int k, int l, int r){
evaluate(k);
if(b <= l || r <= a) return INT_MAX;
else if(a <= l && r <= b) return dat[k];
else{
int vl = query(a, b, k * 2, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 1, (l + r) / 2, r);
return min(vl, vr);
}
}
inline void evaluate(int k){
if(lazy[k] == op.identity()) return;
dat[k] = lazy[k];
if(k < n){
lazy[2 * k] = lazy[k];
lazy[2 * k + 1] = lazy[k];
}
lazy[k] = op.identity();
}
void update(int a, int b, int k, int l, int r, int x){
evaluate(k);
if(r <= a || b <= l) return;
if(a <= l && r <= b){
lazy[k] = x;
}else if(l < b && a < r){
evaluate(k);
update(a, b, k * 2, l, (l + r) / 2, x);
update(a, b, k * 2 + 1, (l + r) / 2, r, x);
}
}
int power(int n){
int res = 1;
while(n >= res) res*=2;
return res;
}
public:
rangeUpdateQuery(int n) : n(power(n)), dat(4 * n, op.identity()), lazy(4 * n, op.identity()) {}
rangeUpdateQuery(const vector<T> &v) : n(v.size()), dat(4 * n), lazy(4 * n, op.identity()){
copy(begin(v), end(v), begin(dat) + n);
for(int i = n - 1; i > 0; i--) dat[i] = op(dat[2 * i], dat[2 * i + 1]);
}
int query(int a, int b){ return query(a,b,1,0,n); }
void update(int s, int t, int x){ update(s, t, 1, 0, n, x); }
int get(int a){ return query(a, a + 1); };
};
int main(){
int n, q;
cin >> n >> q;
rangeUpdateQuery<RUQ> seg(n);
rep(i,q){
int com;
cin >> com;
if(not com){
int s, t, x;
cin >> s >> t >> x;
//cout << s << ' ' << t << endl;
seg.update(s + 1, t + 2, x);
}else{
int p;
cin >> p;
cout << seg.get(p + 1) << endl;
}
}
}
``` |
#include<iostream>
#include<cstdio>
#include<climits>
using namespace std;
// 完成版。
int tree[(1 << 18)];
void Update(int s, int t, int m, int left = 0, int right = (1 << 17), int key = 0){
if(t < left || right <= s) return;
if(s <= left && right - 1 <= t){ tree[key] = max(tree[key], m); return; }
Update(s, t, m, left, (left + right) / 2, 2 * key + 1);
Update(s, t, m, (left + right) / 2, right, 2 * key + 2);
}
int Find(int i){
i += (1 << 17) - 1;
int m = tree[i];
while(i){
i = (i - 1) / 2;
m = max(tree[i], m);
};
return m;
}
int main(){
int i;
for(i = 0; i < (1 << 18); i++) tree[i] = 0;
int n, q;
int x[100000];
for(i = 0; i < 100000; i++) x[i] = INT_MAX;
int s, t, value, m;
m = 1;
cin >> n >> q;
int query;
while(q){
q--;
cin >> query;
if(query){
cin >> i;
//cout << x[Find(i)] << endl;
printf("%d\n", x[Find(i)]);
}else{
cin >> s >> t >> value;
x[m] = value;
Update(s, t, m);
m++;
}
};
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<iostream>
#include<cstdio>
#include<climits>
using namespace std;
// 完成版。
int tree[(1 << 18)];
void Update(int s, int t, int m, int left = 0, int right = (1 << 17), int key = 0){
if(t < left || right <= s) return;
if(s <= left && right - 1 <= t){ tree[key] = max(tree[key], m); return; }
Update(s, t, m, left, (left + right) / 2, 2 * key + 1);
Update(s, t, m, (left + right) / 2, right, 2 * key + 2);
}
int Find(int i){
i += (1 << 17) - 1;
int m = tree[i];
while(i){
i = (i - 1) / 2;
m = max(tree[i], m);
};
return m;
}
int main(){
int i;
for(i = 0; i < (1 << 18); i++) tree[i] = 0;
int n, q;
int x[100000];
for(i = 0; i < 100000; i++) x[i] = INT_MAX;
int s, t, value, m;
m = 1;
cin >> n >> q;
int query;
while(q){
q--;
cin >> query;
if(query){
cin >> i;
//cout << x[Find(i)] << endl;
printf("%d\n", x[Find(i)]);
}else{
cin >> s >> t >> value;
x[m] = value;
Update(s, t, m);
m++;
}
};
return 0;
}
``` |
#include "bits/stdc++.h"
#include <array>
using namespace std;
struct SgTree{
private:
vector<pair<long long, bool>> node;
long long defval;
int N;
int n;
public:
void init(int n, long long defval = INT64_MAX){
N = 1;
while(N < n)N<<=1;
this->n = n;
this->defval = defval;
node.assign(N*2-1, make_pair(defval, false));
}
long long query(int a, int b, int l = 0, int r = -1, int i = 0){
if(r == -1) r = N;
if(b <= l || r <= a) return defval;
if(a <= l && r <= b) return node[i].first;
if(node[i].second) return node[i].first;
return min(query(a,b,l,(l+r)/2,i*2+1),query(a,b,(l+r)/2,r,i*2+2));
}
void update2(int a, int b, long long v, int l, int r, int i, long long defv){
if(b <= l || r <= a) { node[i] = make_pair(defv, true); return; }
if(a <= l && r <= b) { node[i] = make_pair(v, true); return; }
update2(a,b,v,l,(l+r)/2,i*2+1,defv);
update2(a,b,v,(l+r)/2,r,i*2+2,defv);
node[i].second = false;
node[i].first = min(node[i*2+1].first, node[i*2+2].first);
}
void update(int a, int b, long long v, int l = 0, int r = -1, int i = 0){
if(r == -1) r = N;
if(b <= l || r <= a) return;
if(a <= l && r <= b) { node[i] = make_pair(v, true); return; }
if(node[i].second) return update2(a,b,v,l,r,i,node[i].first);
update(a,b,v,l,(l+r)/2,i*2+1);
update(a,b,v,(l+r)/2,r,i*2+2);
node[i].first = min(node[i*2+1].first, node[i*2+2].first);
}
};
int main() {
int n, q; cin >> n >> q;
SgTree tr; tr.init(n, INT_MAX);
vector<long long> ans;
for(int i = 0; i < q; i++){
int c; cin >> c;
if(c == 0){
int s,t,x; cin >> s >> t >> x;
tr.update(s, t+1, x);
}
else {
int i; cin >> i;
ans.push_back(tr.query(i, i+1));
}
}
for(int i = 0; i < ans.size(); i++){
cout << ans[i] << endl;
}
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 "bits/stdc++.h"
#include <array>
using namespace std;
struct SgTree{
private:
vector<pair<long long, bool>> node;
long long defval;
int N;
int n;
public:
void init(int n, long long defval = INT64_MAX){
N = 1;
while(N < n)N<<=1;
this->n = n;
this->defval = defval;
node.assign(N*2-1, make_pair(defval, false));
}
long long query(int a, int b, int l = 0, int r = -1, int i = 0){
if(r == -1) r = N;
if(b <= l || r <= a) return defval;
if(a <= l && r <= b) return node[i].first;
if(node[i].second) return node[i].first;
return min(query(a,b,l,(l+r)/2,i*2+1),query(a,b,(l+r)/2,r,i*2+2));
}
void update2(int a, int b, long long v, int l, int r, int i, long long defv){
if(b <= l || r <= a) { node[i] = make_pair(defv, true); return; }
if(a <= l && r <= b) { node[i] = make_pair(v, true); return; }
update2(a,b,v,l,(l+r)/2,i*2+1,defv);
update2(a,b,v,(l+r)/2,r,i*2+2,defv);
node[i].second = false;
node[i].first = min(node[i*2+1].first, node[i*2+2].first);
}
void update(int a, int b, long long v, int l = 0, int r = -1, int i = 0){
if(r == -1) r = N;
if(b <= l || r <= a) return;
if(a <= l && r <= b) { node[i] = make_pair(v, true); return; }
if(node[i].second) return update2(a,b,v,l,r,i,node[i].first);
update(a,b,v,l,(l+r)/2,i*2+1);
update(a,b,v,(l+r)/2,r,i*2+2);
node[i].first = min(node[i*2+1].first, node[i*2+2].first);
}
};
int main() {
int n, q; cin >> n >> q;
SgTree tr; tr.init(n, INT_MAX);
vector<long long> ans;
for(int i = 0; i < q; i++){
int c; cin >> c;
if(c == 0){
int s,t,x; cin >> s >> t >> x;
tr.update(s, t+1, x);
}
else {
int i; cin >> i;
ans.push_back(tr.query(i, i+1));
}
}
for(int i = 0; i < ans.size(); i++){
cout << ans[i] << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pairInt;
typedef tuple<int, int, int> tuple3Int;
#define FOR(i, n) for (int i = 0; i < int(n); i++)
#define FOR1(i, m, n) for (int i = int(m); i < int(n); i++)
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
ll pow(ll base, uint exp) {
if (exp == 0)
return 1;
if (exp % 2 == 0) {
return pow(base * base, exp / 2);
} else {
return base * pow(base * base, exp / 2);
}
}
class SegTree {
private:
int num;
int exp;
int seq;
vector<pair<int, ll>> vec;
public:
SegTree(int n) : num(n), seq(0){
exp = 0;
int x = 1;
while (x < n) {
exp ++;
x <<= 1;
}
vec.resize(2 * pow(2, exp) - 1, make_pair(-1, pow(2, 31) - 1));
}
void update(int s, int t, ll x) {
_update(s, t, x, 0, 0, pow(2, exp) - 1);
seq ++;
}
ll get(int i) {
return _get(i, 0, 0, pow(2, exp) - 1).second;
}
private:
void _update(int s, int t, ll x, int index, int from, int to) {
if (s <= from && to <= t) {
vec[index].first = seq;
vec[index].second = x;
} else {
if (s <= (from + to) / 2)
_update(s, t, x, 2 * index + 1, from, (from + to) / 2);
if ((from + to) / 2 < t)
_update(s, t, x, 2 * index + 2, (from + to) / 2 + 1, to);
}
}
pair<int, ll> _get(int i, int index, int from, int to) {
pair<int, ll> p;
if (from == to) {
return vec[index];
}
if (i <= (from + to) / 2)
p = _get(i, 2 * index + 1, from, (from + to) / 2);
else
p = _get(i, 2 * index + 2, (from + to) / 2 + 1, to);
if (p.first < vec[index].first)
return vec[index];
else
return p;
}
};
int main(int argc, char *argv[])
{
int n, q;
scanf("%d%d", &n, &q);
SegTree st(n);
FOR(i, q) {
int a;
scanf("%d", &a);
if (a == 0) {
int s, t, x;
scanf("%d%d%d", &s, &t, &x);
st.update(s, t, x);
} else {
int b;
scanf("%d", &b);
printf("%lld\n", st.get(b));
}
}
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>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pairInt;
typedef tuple<int, int, int> tuple3Int;
#define FOR(i, n) for (int i = 0; i < int(n); i++)
#define FOR1(i, m, n) for (int i = int(m); i < int(n); i++)
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
ll pow(ll base, uint exp) {
if (exp == 0)
return 1;
if (exp % 2 == 0) {
return pow(base * base, exp / 2);
} else {
return base * pow(base * base, exp / 2);
}
}
class SegTree {
private:
int num;
int exp;
int seq;
vector<pair<int, ll>> vec;
public:
SegTree(int n) : num(n), seq(0){
exp = 0;
int x = 1;
while (x < n) {
exp ++;
x <<= 1;
}
vec.resize(2 * pow(2, exp) - 1, make_pair(-1, pow(2, 31) - 1));
}
void update(int s, int t, ll x) {
_update(s, t, x, 0, 0, pow(2, exp) - 1);
seq ++;
}
ll get(int i) {
return _get(i, 0, 0, pow(2, exp) - 1).second;
}
private:
void _update(int s, int t, ll x, int index, int from, int to) {
if (s <= from && to <= t) {
vec[index].first = seq;
vec[index].second = x;
} else {
if (s <= (from + to) / 2)
_update(s, t, x, 2 * index + 1, from, (from + to) / 2);
if ((from + to) / 2 < t)
_update(s, t, x, 2 * index + 2, (from + to) / 2 + 1, to);
}
}
pair<int, ll> _get(int i, int index, int from, int to) {
pair<int, ll> p;
if (from == to) {
return vec[index];
}
if (i <= (from + to) / 2)
p = _get(i, 2 * index + 1, from, (from + to) / 2);
else
p = _get(i, 2 * index + 2, (from + to) / 2 + 1, to);
if (p.first < vec[index].first)
return vec[index];
else
return p;
}
};
int main(int argc, char *argv[])
{
int n, q;
scanf("%d%d", &n, &q);
SegTree st(n);
FOR(i, q) {
int a;
scanf("%d", &a);
if (a == 0) {
int s, t, x;
scanf("%d%d%d", &s, &t, &x);
st.update(s, t, x);
} else {
int b;
scanf("%d", &b);
printf("%lld\n", st.get(b));
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
struct node
{
int num,l,r,ls,rs,lazy;
node(int _l,int _r)
{
num=INT_MAX;
l=_l;
r=_r;
}
};
vector<node> T;
int make(int l,int r)
{
int id=T.size();
T.push_back(node(l,r));
T[id].ls=(l+1==r?-1:make(l,(l+r)/2));
T[id].rs=(l+1==r?-1:make((l+r)/2,r));
return id;
}
void update(int id,int l,int r,int t)
{
if(l==r||T[id].l>=r||T[id].r<=l)
return ;
if(T[id].l>=l&&T[id].r<=r)
{
T[id].num=t;
return ;
}
int mid=(T[id].l+T[id].r)>>1;
if(T[id].num>=0)
T[T[id].ls].num=T[T[id].rs].num=T[id].num;
if(l<mid)
update(T[id].ls,l,r,t);
if(r>mid)
update(T[id].rs,l,r,t);
T[id].num=-1;
}
int getmin(int id,int l,int r)
{
if(l==r||T[id].l>=r||T[id].r<=l)
return INT_MAX;
if(T[id].num>=0)
return T[id].num;
int mid=(T[id].l+T[id].r)>>1,mi=INT_MAX;
if(l<mid)
mi=min(mi,getmin(T[id].ls,l,r));
if(r>mid)
mi=min(mi,getmin(T[id].rs,l,r));
return mi;
}
int main()
{
T.reserve(200100);
int n,q;
scanf("%d%d",&n,&q);
make(0,n);
for(int i=0;i<q;i++)
{
int com;
scanf("%d",&com);
if(com==0)
{
int s,t,x;
scanf("%d%d%d",&s,&t,&x);
update(0,s,t+1,x);
}
else
{
int s;
scanf("%d",&s);
printf("%d\n",getmin(0,s,s+1));
}
}
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<bits/stdc++.h>
using namespace std;
struct node
{
int num,l,r,ls,rs,lazy;
node(int _l,int _r)
{
num=INT_MAX;
l=_l;
r=_r;
}
};
vector<node> T;
int make(int l,int r)
{
int id=T.size();
T.push_back(node(l,r));
T[id].ls=(l+1==r?-1:make(l,(l+r)/2));
T[id].rs=(l+1==r?-1:make((l+r)/2,r));
return id;
}
void update(int id,int l,int r,int t)
{
if(l==r||T[id].l>=r||T[id].r<=l)
return ;
if(T[id].l>=l&&T[id].r<=r)
{
T[id].num=t;
return ;
}
int mid=(T[id].l+T[id].r)>>1;
if(T[id].num>=0)
T[T[id].ls].num=T[T[id].rs].num=T[id].num;
if(l<mid)
update(T[id].ls,l,r,t);
if(r>mid)
update(T[id].rs,l,r,t);
T[id].num=-1;
}
int getmin(int id,int l,int r)
{
if(l==r||T[id].l>=r||T[id].r<=l)
return INT_MAX;
if(T[id].num>=0)
return T[id].num;
int mid=(T[id].l+T[id].r)>>1,mi=INT_MAX;
if(l<mid)
mi=min(mi,getmin(T[id].ls,l,r));
if(r>mid)
mi=min(mi,getmin(T[id].rs,l,r));
return mi;
}
int main()
{
T.reserve(200100);
int n,q;
scanf("%d%d",&n,&q);
make(0,n);
for(int i=0;i<q;i++)
{
int com;
scanf("%d",&com);
if(com==0)
{
int s,t,x;
scanf("%d%d%d",&s,&t,&x);
update(0,s,t+1,x);
}
else
{
int s;
scanf("%d",&s);
printf("%d\n",getmin(0,s,s+1));
}
}
return 0;
}
``` |
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = (1ll << 31) - 1;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
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; }
template<typename T>
struct RangeUpdateQuery {
int n;
vector<T>d;
RangeUpdateQuery(int m) {
for (n = 1; n < m; n <<= 1);
d.assign(2 * n, INF);
}
int get(int i) {
update(i, i + 1, -1);
return d[n + i];
}
void update(int a, int b, T x) {
return update(a, b, x, 1, 0, n);
}
void update(int a, int b, T x, int k, int l, int r) {
// [a,b) [l,r)
if (r <= a || b <= l)return;
else if (a <= l&&r <= b) {
if (x >= 0)d[k] = x;
return;
}
else {
if (d[k] != INF)d[2 * k] = d[2 * k + 1] = d[k], d[k] = INF;
update(a, b, x, 2 * k, l, (l + r) / 2);
update(a, b, x, 2 * k + 1, (l + r) / 2, r);
}
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q; cin >> n >> q;
RangeUpdateQuery<int> ruq(n);
rep(i, 0, q) {
int com; cin >> com;
if (com) {
int i; cin >> i;
cout << ruq.get(i) << endl;
}
else {
int s, t, x; cin >> s >> t >> x;
ruq.update(s, t + 1, 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;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = (1ll << 31) - 1;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
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; }
template<typename T>
struct RangeUpdateQuery {
int n;
vector<T>d;
RangeUpdateQuery(int m) {
for (n = 1; n < m; n <<= 1);
d.assign(2 * n, INF);
}
int get(int i) {
update(i, i + 1, -1);
return d[n + i];
}
void update(int a, int b, T x) {
return update(a, b, x, 1, 0, n);
}
void update(int a, int b, T x, int k, int l, int r) {
// [a,b) [l,r)
if (r <= a || b <= l)return;
else if (a <= l&&r <= b) {
if (x >= 0)d[k] = x;
return;
}
else {
if (d[k] != INF)d[2 * k] = d[2 * k + 1] = d[k], d[k] = INF;
update(a, b, x, 2 * k, l, (l + r) / 2);
update(a, b, x, 2 * k + 1, (l + r) / 2, r);
}
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q; cin >> n >> q;
RangeUpdateQuery<int> ruq(n);
rep(i, 0, q) {
int com; cin >> com;
if (com) {
int i; cin >> i;
cout << ruq.get(i) << endl;
}
else {
int s, t, x; cin >> s >> t >> x;
ruq.update(s, t + 1, x);
}
}
return 0;
}
``` |
#include<iostream>
#include<algorithm>
#pragma warning(disable:4996)
using namespace std;
long long seg[262149], n, v, a, b, c, d, INF = (1LL << 31) - 1, size_ = 1;
void update(long long p, long long q, long long r, long long s, long long t, long long u, long long v) {
if (s <= p || q <= r) { if (v != INF)seg[u] = v; return; }
if (p <= r && s <= q) { seg[u] = t; return; }
long long w = v; if (w == INF)w = seg[u]; seg[u] = INF;
update(p, q, r, (r + s) / 2, t, u * 2, w);
update(p, q, (r + s) / 2, s, t, u * 2 + 1, w);
}
long long find(long long p, long long q, long long r, long long s, long long u) {
if (s <= p || q <= r)return INF;
if ((r <= p && q <= s) && (seg[u] != INF || s - r == 1)) { return seg[u]; }
long long a1 = find(p, q, r, (r + s) / 2, u * 2);
long long a2 = find(p, q, (r + s) / 2, s, u * 2 + 1);
return min(a1, a2);
}
int main() {
cin >> n >> v; while (size_ < n)size_ *= 2;
for (int i = 1; i <= size_ * 2; i++)seg[i] = INF;
for (int i = 1; i <= v; i++) {
scanf("%d", &a);
if (a == 0) { scanf("%lld%lld%lld", &b, &c, &d); c++; update(b, c, 0, size_, d, 1, INF); }
if (a == 1) { scanf("%lld", &b); printf("%lld\n", find(b, b + 1, 0, size_, 1)); }
}
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<iostream>
#include<algorithm>
#pragma warning(disable:4996)
using namespace std;
long long seg[262149], n, v, a, b, c, d, INF = (1LL << 31) - 1, size_ = 1;
void update(long long p, long long q, long long r, long long s, long long t, long long u, long long v) {
if (s <= p || q <= r) { if (v != INF)seg[u] = v; return; }
if (p <= r && s <= q) { seg[u] = t; return; }
long long w = v; if (w == INF)w = seg[u]; seg[u] = INF;
update(p, q, r, (r + s) / 2, t, u * 2, w);
update(p, q, (r + s) / 2, s, t, u * 2 + 1, w);
}
long long find(long long p, long long q, long long r, long long s, long long u) {
if (s <= p || q <= r)return INF;
if ((r <= p && q <= s) && (seg[u] != INF || s - r == 1)) { return seg[u]; }
long long a1 = find(p, q, r, (r + s) / 2, u * 2);
long long a2 = find(p, q, (r + s) / 2, s, u * 2 + 1);
return min(a1, a2);
}
int main() {
cin >> n >> v; while (size_ < n)size_ *= 2;
for (int i = 1; i <= size_ * 2; i++)seg[i] = INF;
for (int i = 1; i <= v; i++) {
scanf("%d", &a);
if (a == 0) { scanf("%lld%lld%lld", &b, &c, &d); c++; update(b, c, 0, size_, d, 1, INF); }
if (a == 1) { scanf("%lld", &b); printf("%lld\n", find(b, b + 1, 0, size_, 1)); }
}
return 0;
}
``` |
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define max0(a,b,c) max(max(a,b),c)
#define min0(a,b,c) min(min(a,b),c)
#define ft first
#define sc second
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define mod 1000000007
using namespace std;
using Graph = vector<vector<int>>;
typedef long long lint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldouble;
typedef vector<int> vec;
typedef vector<ll> lvec;
typedef vector<ull> ulvec;
typedef vector<double> dvec;
typedef vector<pair<int,int>> pvec;
typedef vector<pair<ll,ll>> plvec;
typedef vector<tuple<ll,ll,ll>> tvec;
typedef vector<string> svec;
const int INF=(1LL<<31)-1,MAX_N=1000000;
int n;
int d[MAX_N*2];
void init(int _n){
n=1;
while(n<_n) n*=2;
for(int i=0;i<2*n-1;i++) d[i]=INF;
}
void update(int a,int b,int k,int l,int r,int x){
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
d[k]=x;
}else{
if(d[k]!=INF){
d[2*k+1]=d[k];
d[2*k+2]=d[k];
d[k]=INF;
}
int m=(l+r)/2;
if(m>=b) update(a,b,2*k+1,l,m,x);
else if(m<=a) update(a,b,2*k+2,m,r,x);
else{
update(a,b,2*k+1,l,(l+r)/2,x);
update(a,b,2*k+2,(l+r)/2,r,x);
}
}
}
int find(int i,int k,int l,int r){
if(d[k]!=INF) return d[k];
if(r-l==1) return d[k];
int m=(l+r)/2;
if(i<m) return find(i,2*k+1,l,m);
return find(i,2*k+2,m,r);
}
int main(){
int N,q;
cin>>N>>q;
init(N);
rep(i,q){
int a;
cin>>a;
if(a==0){
int s,t,x;
cin>>s>>t>>x;
update(s,t+1,0,0,n,x);
}else{
int x;
cin>>x;
cout<<find(x,0,0,n)<<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
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define max0(a,b,c) max(max(a,b),c)
#define min0(a,b,c) min(min(a,b),c)
#define ft first
#define sc second
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define mod 1000000007
using namespace std;
using Graph = vector<vector<int>>;
typedef long long lint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldouble;
typedef vector<int> vec;
typedef vector<ll> lvec;
typedef vector<ull> ulvec;
typedef vector<double> dvec;
typedef vector<pair<int,int>> pvec;
typedef vector<pair<ll,ll>> plvec;
typedef vector<tuple<ll,ll,ll>> tvec;
typedef vector<string> svec;
const int INF=(1LL<<31)-1,MAX_N=1000000;
int n;
int d[MAX_N*2];
void init(int _n){
n=1;
while(n<_n) n*=2;
for(int i=0;i<2*n-1;i++) d[i]=INF;
}
void update(int a,int b,int k,int l,int r,int x){
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
d[k]=x;
}else{
if(d[k]!=INF){
d[2*k+1]=d[k];
d[2*k+2]=d[k];
d[k]=INF;
}
int m=(l+r)/2;
if(m>=b) update(a,b,2*k+1,l,m,x);
else if(m<=a) update(a,b,2*k+2,m,r,x);
else{
update(a,b,2*k+1,l,(l+r)/2,x);
update(a,b,2*k+2,(l+r)/2,r,x);
}
}
}
int find(int i,int k,int l,int r){
if(d[k]!=INF) return d[k];
if(r-l==1) return d[k];
int m=(l+r)/2;
if(i<m) return find(i,2*k+1,l,m);
return find(i,2*k+2,m,r);
}
int main(){
int N,q;
cin>>N>>q;
init(N);
rep(i,q){
int a;
cin>>a;
if(a==0){
int s,t,x;
cin>>s>>t>>x;
update(s,t+1,0,0,n,x);
}else{
int x;
cin>>x;
cout<<find(x,0,0,n)<<endl;
}
}
}
``` |
#include <cstdio>
#include <functional>
#include <vector>
using namespace std;
#define int long long
#define dotimes(i, n) for (int i = 0, i##max__ = (n); i < i##max__; i++)
#define whole(f, x, ...) ([&](decltype((x)) c) { return (f)(begin(c), end(c), ## __VA_ARGS__); })(x)
int rint() {
int x;
scanf("%lld", &x);
return x;
}
void wint(int x) {
printf("%lld\n", x);
}
template<typename T> int size(T const& c) { return static_cast<int>(c.size()); }
template <typename T> bool maxs(T& a, T const& b) { return a < b ? a = b, true : false; }
template <typename T> bool mins(T& a, T const& b) { return a > b ? a = b, true : false; }
inline int lg(int x) { return 63 - __builtin_clzll(static_cast<unsigned int>(x)); }
class segment_tree {
// Negative nodes delegate to their children
vector<int> xs;
public:
segment_tree(int n) : xs(1 << (lg(2 * n - 1) + 1), (1LL << 31) - 1) {}
void update(int i, int j, int x) {
function<void(int, int, int)> rec;
rec =
[&](int k, int a, int b) {
if (i <= a && b <= j)
xs[k] = x;
else {
if (xs[k] >= 0)
xs[2*k] = xs[2*k+1] = xs[k];
int c = a + (b - a) / 2;
if (j <= c)
rec(2*k, a, c);
else if (c <= i)
rec(2*k+1, c, b);
else {
rec(2*k, a, c);
rec(2*k+1, c, b);
}
xs[k] = -1;
}
};
rec(1, 0, ::size(xs)/2);
}
int query(const int i) {
function<int(int, int, int)> rec;
rec =
[&](int k, int a, int b) {
if (xs[k] >= 0)
return xs[k];
int c = a + (b - a) / 2;
return i < c ? rec(2*k, a, c) : rec(2*k+1, c, b);
};
return rec(1, 0, ::size(xs)/2);
}
};
signed main() {
const int n = rint();
const int q = rint();
segment_tree st(n);
dotimes(i, q)
if (rint()) {
int i = rint();
wint(st.query(i));
} else {
int s = rint();
int t = rint();
int x = rint();
st.update(s, t+1, x);
}
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 <cstdio>
#include <functional>
#include <vector>
using namespace std;
#define int long long
#define dotimes(i, n) for (int i = 0, i##max__ = (n); i < i##max__; i++)
#define whole(f, x, ...) ([&](decltype((x)) c) { return (f)(begin(c), end(c), ## __VA_ARGS__); })(x)
int rint() {
int x;
scanf("%lld", &x);
return x;
}
void wint(int x) {
printf("%lld\n", x);
}
template<typename T> int size(T const& c) { return static_cast<int>(c.size()); }
template <typename T> bool maxs(T& a, T const& b) { return a < b ? a = b, true : false; }
template <typename T> bool mins(T& a, T const& b) { return a > b ? a = b, true : false; }
inline int lg(int x) { return 63 - __builtin_clzll(static_cast<unsigned int>(x)); }
class segment_tree {
// Negative nodes delegate to their children
vector<int> xs;
public:
segment_tree(int n) : xs(1 << (lg(2 * n - 1) + 1), (1LL << 31) - 1) {}
void update(int i, int j, int x) {
function<void(int, int, int)> rec;
rec =
[&](int k, int a, int b) {
if (i <= a && b <= j)
xs[k] = x;
else {
if (xs[k] >= 0)
xs[2*k] = xs[2*k+1] = xs[k];
int c = a + (b - a) / 2;
if (j <= c)
rec(2*k, a, c);
else if (c <= i)
rec(2*k+1, c, b);
else {
rec(2*k, a, c);
rec(2*k+1, c, b);
}
xs[k] = -1;
}
};
rec(1, 0, ::size(xs)/2);
}
int query(const int i) {
function<int(int, int, int)> rec;
rec =
[&](int k, int a, int b) {
if (xs[k] >= 0)
return xs[k];
int c = a + (b - a) / 2;
return i < c ? rec(2*k, a, c) : rec(2*k+1, c, b);
};
return rec(1, 0, ::size(xs)/2);
}
};
signed main() {
const int n = rint();
const int q = rint();
segment_tree st(n);
dotimes(i, q)
if (rint()) {
int i = rint();
wint(st.query(i));
} else {
int s = rint();
int t = rint();
int x = rint();
st.update(s, t+1, x);
}
return 0;
}
``` |
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const int INF = 2147483647;
using namespace std;
const int sqrtN = 512;
class sqdiv{
public:
int N, K;
vector<int> data;
vector<bool> lazy_flag;
vector<int> lazy_update;
sqdiv(int n){ //?????±???????????????????????´
N = n;
K = (N + sqrtN - 1) / sqrtN;
data.assign(K * sqrtN, INF);
lazy_flag.assign(K, false);
lazy_update.assign(K, 0);
}
void eval(int k){
if(lazy_flag[k]){
lazy_flag[k] = false;
for(int i = k * sqrtN; i < (k + 1) * sqrtN; i++){
data[i] = lazy_update[k];
}
}
}
void update(int x, int y, int a){
rep(k,K){
int l = k * sqrtN, r = (k + 1) * sqrtN;
if(r <= x || y <= l) continue;
if(x <= l && r <= y){
lazy_flag[k] = true;
lazy_update[k] = a;
}else{
eval(k);
for(int i = max(x, l); i < min(y, r); i++){
data[i] = a;
}
}
}
}
int find(int a){
int k = a / sqrtN;
eval(k);
return data[a];
}
};
int main(){
int n,q;
cin >> n >> q;
sqdiv ob(n);
rep(i,q){
bool com;
int a, b, c;
cin >> com;
if(com){
cin >> a;
cout << ob.find(a) << endl;
}else{
cin >> a >> b >> c;
ob.update(a,b + 1,c);
}
}
} | ### 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>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const int INF = 2147483647;
using namespace std;
const int sqrtN = 512;
class sqdiv{
public:
int N, K;
vector<int> data;
vector<bool> lazy_flag;
vector<int> lazy_update;
sqdiv(int n){ //?????±???????????????????????´
N = n;
K = (N + sqrtN - 1) / sqrtN;
data.assign(K * sqrtN, INF);
lazy_flag.assign(K, false);
lazy_update.assign(K, 0);
}
void eval(int k){
if(lazy_flag[k]){
lazy_flag[k] = false;
for(int i = k * sqrtN; i < (k + 1) * sqrtN; i++){
data[i] = lazy_update[k];
}
}
}
void update(int x, int y, int a){
rep(k,K){
int l = k * sqrtN, r = (k + 1) * sqrtN;
if(r <= x || y <= l) continue;
if(x <= l && r <= y){
lazy_flag[k] = true;
lazy_update[k] = a;
}else{
eval(k);
for(int i = max(x, l); i < min(y, r); i++){
data[i] = a;
}
}
}
}
int find(int a){
int k = a / sqrtN;
eval(k);
return data[a];
}
};
int main(){
int n,q;
cin >> n >> q;
sqdiv ob(n);
rep(i,q){
bool com;
int a, b, c;
cin >> com;
if(com){
cin >> a;
cout << ob.find(a) << endl;
}else{
cin >> a >> b >> c;
ob.update(a,b + 1,c);
}
}
}
``` |
#include <iostream>
using namespace std;
const int maxn = 0x7fffffff;
const int maxnode = 1 << 19;
const int INF = 1000000000;
int y1, y2, x, v;
struct IntervalTree
{
int sumv[maxnode], setv[maxnode];
// 维护信息
void maintain(int o, int L, int R)
{
int lc = o * 2, rc = o * 2 + 1;
if(R > L)
{
sumv[o] = sumv[lc] + sumv[rc];
}
if(setv[o] >= 0)
{
sumv[o] = setv[o] * (R - L + 1);
}
}
// 标记传递
void pushdown(int o)
{
int lc = o * 2, rc = o * 2 + 1;
if(setv[o] >= 0)
{
setv[lc] = setv[rc] = setv[o];
setv[o] = -1; // 清除本结点标记
}
}
void update(int o, int L, int R)
{
int lc = o * 2, rc = o * 2 + 1;
if(y1 <= L && y2 >= R) // 标记修改
{
setv[o] = v;
}
else
{
pushdown(o);
int M = L + (R - L) / 2;
if(y1 <= M) update(lc, L, M);
else maintain(lc, L, M);
if(y2 > M) update(rc, M + 1, R);
else maintain(rc, M + 1, R);
}
maintain(o, L, R);
}
void query(int o, int L, int R, int &ssum)
{
int lc = o * 2, rc = o * 2 + 1;
maintain(o, L, R); // 处理被pushdown下来的标记
if(y1 <= L && y2 >= R)
{
ssum = sumv[o];
}
else
{
pushdown(o);
int M = L + (R - L) / 2;
int lsum = 0;
int rsum = 0;
if(y1 <= M) query(lc, L, M, lsum);
else maintain(lc, L, M);
if(y2 > M) query(rc, M + 1, R, rsum);
else maintain(rc, M + 1, R);
ssum = lsum + rsum;
}
}
};
/*
3 5
0 0 1 1
0 1 2 3
0 2 2 2
1 0
1 1
*/
int n, q;
IntervalTree tree;
int main()
{
scanf("%d %d", &n, &q);
tree.setv[1] = maxn;
for (int i = 0; i < q; ++i)
{
int op;
scanf("%d", &op);
if (!op)
{
scanf("%d %d %d", &y1, &y2, &v);
++y1;
++y2;
tree.update(1, 1, n);
}
else
{
int loc;
scanf("%d", &loc);
++loc;
y1 = y2 = loc;
int gsum = 0;
int ssum;
tree.query(1, 1, n, ssum);
gsum += ssum;
printf("%d\n", gsum);
}
}
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>
using namespace std;
const int maxn = 0x7fffffff;
const int maxnode = 1 << 19;
const int INF = 1000000000;
int y1, y2, x, v;
struct IntervalTree
{
int sumv[maxnode], setv[maxnode];
// 维护信息
void maintain(int o, int L, int R)
{
int lc = o * 2, rc = o * 2 + 1;
if(R > L)
{
sumv[o] = sumv[lc] + sumv[rc];
}
if(setv[o] >= 0)
{
sumv[o] = setv[o] * (R - L + 1);
}
}
// 标记传递
void pushdown(int o)
{
int lc = o * 2, rc = o * 2 + 1;
if(setv[o] >= 0)
{
setv[lc] = setv[rc] = setv[o];
setv[o] = -1; // 清除本结点标记
}
}
void update(int o, int L, int R)
{
int lc = o * 2, rc = o * 2 + 1;
if(y1 <= L && y2 >= R) // 标记修改
{
setv[o] = v;
}
else
{
pushdown(o);
int M = L + (R - L) / 2;
if(y1 <= M) update(lc, L, M);
else maintain(lc, L, M);
if(y2 > M) update(rc, M + 1, R);
else maintain(rc, M + 1, R);
}
maintain(o, L, R);
}
void query(int o, int L, int R, int &ssum)
{
int lc = o * 2, rc = o * 2 + 1;
maintain(o, L, R); // 处理被pushdown下来的标记
if(y1 <= L && y2 >= R)
{
ssum = sumv[o];
}
else
{
pushdown(o);
int M = L + (R - L) / 2;
int lsum = 0;
int rsum = 0;
if(y1 <= M) query(lc, L, M, lsum);
else maintain(lc, L, M);
if(y2 > M) query(rc, M + 1, R, rsum);
else maintain(rc, M + 1, R);
ssum = lsum + rsum;
}
}
};
/*
3 5
0 0 1 1
0 1 2 3
0 2 2 2
1 0
1 1
*/
int n, q;
IntervalTree tree;
int main()
{
scanf("%d %d", &n, &q);
tree.setv[1] = maxn;
for (int i = 0; i < q; ++i)
{
int op;
scanf("%d", &op);
if (!op)
{
scanf("%d %d %d", &y1, &y2, &v);
++y1;
++y2;
tree.update(1, 1, n);
}
else
{
int loc;
scanf("%d", &loc);
++loc;
y1 = y2 = loc;
int gsum = 0;
int ssum;
tree.query(1, 1, n, ssum);
gsum += ssum;
printf("%d\n", gsum);
}
}
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){
set(l, r, x, 0, n, 1);
}
void set(int l, int r, int x, int segl, int segr, int n){
// cout << l << ' ' << r << ' ' << x << ' ' << segl << ' ' << segr << ' ' << n << endl;
push(segl, segr, n);
if(segr <= l || r <= segl);
else if(l <= segl && segr <= r) lazy[n] = x;
else {
int segm = (segl + segr) / 2;
set(l, r, x, segl, segm, n*2);
set(l, r, x, 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(){
int n, q;
while(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';
}
// for(int i = 1; i < (int)st.dat.size(); ++i){
// cout << '[' << st.dat[i] << ' ' << st.lazy[i] << ']';
// if(i == 1 || i == 3 || i == 7 || i == 15) cout << 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 <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){
set(l, r, x, 0, n, 1);
}
void set(int l, int r, int x, int segl, int segr, int n){
// cout << l << ' ' << r << ' ' << x << ' ' << segl << ' ' << segr << ' ' << n << endl;
push(segl, segr, n);
if(segr <= l || r <= segl);
else if(l <= segl && segr <= r) lazy[n] = x;
else {
int segm = (segl + segr) / 2;
set(l, r, x, segl, segm, n*2);
set(l, r, x, 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(){
int n, q;
while(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';
}
// for(int i = 1; i < (int)st.dat.size(); ++i){
// cout << '[' << st.dat[i] << ' ' << st.lazy[i] << ']';
// if(i == 1 || i == 3 || i == 7 || i == 15) cout << endl;
// }
}
}
}
``` |
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr ll MOD = 1e9 + 7;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> struct Segtree {
inline T merge(T a, T b) { return a.first > b.first ? a : b; }
inline void act(T &a, T b) { a = b; }
vector<T> dat;
int n;
T e;
Segtree(int n_, T e) : e(e) {
n = 1;
while (n < n_) {
n <<= 1;
}
dat.resize(2 * n, e);
}
void set(const int &a, const int &b, const T &x, int k, int l, int r) {
if (b <= l || r <= a) {
return;
}
if (a <= l && r <= b) {
act(dat[k], x);
return;
}
set(a, b, x, k << 1, l, (l + r) >> 1);
set(a, b, x, k << 1 | 1, (l + r) >> 1, r);
}
inline void set(const int &a, const int &b, const T &x) {
if (a >= b) {
return;
}
set(a, b, x, 1, 0, n);
}
T get(int k) {
k += n;
T res = dat[k];
k >>= 1;
while (k > 0) {
res = merge(res, dat[k]);
k >>= 1;
}
return res;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
Segtree<P> seg(n, P(-1, (1LL << 31) - 1LL));
for (int k = 0; k < q; k++) {
int t;
cin >> t;
if (t == 0) {
int l, r, x;
cin >> l >> r >> x;
++r;
seg.set(l, r, P(k, x));
} else {
int i;
cin >> i;
cout << seg.get(i).second << "\n";
}
}
}
| ### 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 all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr ll MOD = 1e9 + 7;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> struct Segtree {
inline T merge(T a, T b) { return a.first > b.first ? a : b; }
inline void act(T &a, T b) { a = b; }
vector<T> dat;
int n;
T e;
Segtree(int n_, T e) : e(e) {
n = 1;
while (n < n_) {
n <<= 1;
}
dat.resize(2 * n, e);
}
void set(const int &a, const int &b, const T &x, int k, int l, int r) {
if (b <= l || r <= a) {
return;
}
if (a <= l && r <= b) {
act(dat[k], x);
return;
}
set(a, b, x, k << 1, l, (l + r) >> 1);
set(a, b, x, k << 1 | 1, (l + r) >> 1, r);
}
inline void set(const int &a, const int &b, const T &x) {
if (a >= b) {
return;
}
set(a, b, x, 1, 0, n);
}
T get(int k) {
k += n;
T res = dat[k];
k >>= 1;
while (k > 0) {
res = merge(res, dat[k]);
k >>= 1;
}
return res;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
Segtree<P> seg(n, P(-1, (1LL << 31) - 1LL));
for (int k = 0; k < q; k++) {
int t;
cin >> t;
if (t == 0) {
int l, r, x;
cin >> l >> r >> x;
++r;
seg.set(l, r, P(k, x));
} else {
int i;
cin >> i;
cout << seg.get(i).second << "\n";
}
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
struct SegmentTree
{
vector< pair< int, int > > lazy;
int sz, timestamp;
SegmentTree(int n)
{
sz = 1;
while(sz < n) sz <<= 1;
lazy.assign(2 * sz - 1, {-1, 2147483647});
timestamp = 0;
}
void update(int a, int b, int x, int k, int l, int r)
{
if(a >= r || b <= l) {
} else if(a <= l && r <= b) {
lazy[k] = {timestamp, x};
} else {
update(a, b, x, 2 * k + 1, l, (l + r) >> 1);
update(a, b, x, 2 * k + 2, (l + r) >> 1, r);
}
}
void update(int a, int b, int x)
{
update(a, b, x, 0, 0, sz);
++timestamp;
}
int query(int k)
{
k += sz - 1;
auto ret = lazy[k];
while(k > 0) {
k = (k - 1) >> 1;
ret = max(ret, lazy[k]);
}
return (ret.second);
}
};
int main()
{
int N, Q;
scanf("%d %d", &N, &Q);
SegmentTree tree(N);
while(Q--) {
int t;
scanf("%d", &t);
if(t == 0) {
int s, t, x;
scanf("%d %d %d", &s, &t, &x);
tree.update(s, ++t, x);
} else {
int i;
scanf("%d", &i);
printf("%d\n", tree.query(i));
}
}
} | ### 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;
struct SegmentTree
{
vector< pair< int, int > > lazy;
int sz, timestamp;
SegmentTree(int n)
{
sz = 1;
while(sz < n) sz <<= 1;
lazy.assign(2 * sz - 1, {-1, 2147483647});
timestamp = 0;
}
void update(int a, int b, int x, int k, int l, int r)
{
if(a >= r || b <= l) {
} else if(a <= l && r <= b) {
lazy[k] = {timestamp, x};
} else {
update(a, b, x, 2 * k + 1, l, (l + r) >> 1);
update(a, b, x, 2 * k + 2, (l + r) >> 1, r);
}
}
void update(int a, int b, int x)
{
update(a, b, x, 0, 0, sz);
++timestamp;
}
int query(int k)
{
k += sz - 1;
auto ret = lazy[k];
while(k > 0) {
k = (k - 1) >> 1;
ret = max(ret, lazy[k]);
}
return (ret.second);
}
};
int main()
{
int N, Q;
scanf("%d %d", &N, &Q);
SegmentTree tree(N);
while(Q--) {
int t;
scanf("%d", &t);
if(t == 0) {
int s, t, x;
scanf("%d %d %d", &s, &t, &x);
tree.update(s, ++t, x);
} else {
int i;
scanf("%d", &i);
printf("%d\n", tree.query(i));
}
}
}
``` |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,x,n) for(int i=(x);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define int long long
using namespace std;
const int MOD=1e9+7;
const int INF=1e9;
template<typename T> void cmax(T &a, T b) {a = max(a, b);}
template<typename T> void cmin(T &a, T b) {a = min(a, b);}
int inf=2147483647;
int size=1;
vector<int> tree;
void init(int n){
while(size<n)size*=2;
tree.assign(size*2,inf);
}
//Range add Query [ )
void update(int l,int r,int x){
l+=size,r+=size;
int nl=l,nr=r;
stack<int> s;
for(l/=2;1<=l;l/=2)s.emplace(l);
while(!s.empty()){
int tmp=s.top();s.pop();
if(tree[tmp]==-1)continue;
tree[2*tmp]=tree[2*tmp+1]=tree[tmp];
tree[tmp]=-1;
}
for(r=(r-1)/2;1<=r;r/=2)s.emplace(r);
while(!s.empty()){
int tmp=s.top();s.pop();
if(tree[tmp]==-1)continue;
tree[2*tmp]=tree[2*tmp+1]=tree[tmp];
tree[tmp]=-1;
}
for(;nl<nr;nl/=2,nr/=2){
if(nl%2==1)tree[nl++]=x;
if(nr%2==1)tree[--nr]=x;
}
return ;
}
int getvalue(int i){
i+=size;
int res=tree[i];
while(i/=2){
if(tree[i]!=-1){
res=tree[i];
}
}
return res;
}
signed main(){
int n,q;
cin>>n>>q;
init(n);
rep(i,q){
int a;
cin>>a;
if(a){
int x;
cin>>x;
cout<<getvalue(x)<<endl;
}else{
int s,t,x;
cin>>s>>t>>x;
t++;
update(s,t,x);
}
}
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>
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,x,n) for(int i=(x);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define int long long
using namespace std;
const int MOD=1e9+7;
const int INF=1e9;
template<typename T> void cmax(T &a, T b) {a = max(a, b);}
template<typename T> void cmin(T &a, T b) {a = min(a, b);}
int inf=2147483647;
int size=1;
vector<int> tree;
void init(int n){
while(size<n)size*=2;
tree.assign(size*2,inf);
}
//Range add Query [ )
void update(int l,int r,int x){
l+=size,r+=size;
int nl=l,nr=r;
stack<int> s;
for(l/=2;1<=l;l/=2)s.emplace(l);
while(!s.empty()){
int tmp=s.top();s.pop();
if(tree[tmp]==-1)continue;
tree[2*tmp]=tree[2*tmp+1]=tree[tmp];
tree[tmp]=-1;
}
for(r=(r-1)/2;1<=r;r/=2)s.emplace(r);
while(!s.empty()){
int tmp=s.top();s.pop();
if(tree[tmp]==-1)continue;
tree[2*tmp]=tree[2*tmp+1]=tree[tmp];
tree[tmp]=-1;
}
for(;nl<nr;nl/=2,nr/=2){
if(nl%2==1)tree[nl++]=x;
if(nr%2==1)tree[--nr]=x;
}
return ;
}
int getvalue(int i){
i+=size;
int res=tree[i];
while(i/=2){
if(tree[i]!=-1){
res=tree[i];
}
}
return res;
}
signed main(){
int n,q;
cin>>n>>q;
init(n);
rep(i,q){
int a;
cin>>a;
if(a){
int x;
cin>>x;
cout<<getvalue(x)<<endl;
}else{
int s,t,x;
cin>>s>>t>>x;
t++;
update(s,t,x);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pi M_PI
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;
class RUQ{
public:
int t[555555],v[555555],c;
void init(){
c=0;
memset(t,0,sizeof(t));
memset(v,0,sizeof(v));
}
void update(int a,int b,int x) {
c++;
v[c]=x;
update2(a,b,c);
}
void update2(int a,int b,int x,int k=0,int l=0,int r=1<<18){
if(b<=l||r<=a)return;
if(a<=l&&r<=b){t[k]=max(t[k],x);return;}
int m=(l+r)/2;
update2(a,b,x,k*2+1,l,m);
update2(a,b,x,k*2+2,m,r);
}
int query(int i){
i+=(1<<18)-1;
int res=t[i];
while(i){
i=(i-1)/2;
res=max(res,t[i]);
}
return v[res];
}
};
void Main() {
int n,m;
cin >> n >> m;
RUQ r;
r.init();
r.v[0]=INT_MAX;
while(m--) {
int z,x;
cin >> z >> x;
if(!z) {
int y,w;
cin >> y >> w;
r.update(x,y+1,w);
} else pr(r.query(x));
}
}
int main(){ios::sync_with_stdio(0);cin.tie(0);Main();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 <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pi M_PI
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;
class RUQ{
public:
int t[555555],v[555555],c;
void init(){
c=0;
memset(t,0,sizeof(t));
memset(v,0,sizeof(v));
}
void update(int a,int b,int x) {
c++;
v[c]=x;
update2(a,b,c);
}
void update2(int a,int b,int x,int k=0,int l=0,int r=1<<18){
if(b<=l||r<=a)return;
if(a<=l&&r<=b){t[k]=max(t[k],x);return;}
int m=(l+r)/2;
update2(a,b,x,k*2+1,l,m);
update2(a,b,x,k*2+2,m,r);
}
int query(int i){
i+=(1<<18)-1;
int res=t[i];
while(i){
i=(i-1)/2;
res=max(res,t[i]);
}
return v[res];
}
};
void Main() {
int n,m;
cin >> n >> m;
RUQ r;
r.init();
r.v[0]=INT_MAX;
while(m--) {
int z,x;
cin >> z >> x;
if(!z) {
int y,w;
cin >> y >> w;
r.update(x,y+1,w);
} else pr(r.query(x));
}
}
int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
``` |
#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;}
//BEGIN CUT HERE
template <typename E>
struct SegmentTree{
using H = function<E(E,E)>;
int n,height;
H h;
E ei;
vector<E> laz;
SegmentTree(H h,E ei):h(h),ei(ei){}
void init(int n_){
n=1;height=0;
while(n<n_) n<<=1,height++;
laz.assign(2*n,ei);
}
inline void eval(int k){
if(laz[k]==ei) return;
laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
laz[k]=ei;
}
inline void thrust(int k){
for(int i=height;i;i--) eval(k>>i);
}
void update(int a,int b,E x){
thrust(a+=n);
thrust(b+=n-1);
for(int l=a,r=b+1;l<r;l>>=1,r>>=1){
if(l&1) laz[l]=h(laz[l],x),l++;
if(r&1) --r,laz[r]=h(laz[r],x);
}
}
E get_val(int a){
thrust(a+=n);
return laz[a];
}
void set_val(int a,E x){
thrust(a+=n);
laz[a]=x;
}
};
//END CUT HERE
struct FastIO{
FastIO(){
cin.tie(0);
ios::sync_with_stdio(0);
}
}fastio_beet;
//INSERT ABOVE HERE
signed AOJ_DSL_2_D(){
int n,q;
cin>>n>>q;
auto h=[](int a,int b){(void)a;return b;};
int ei=INT_MAX;
SegmentTree<int> seg(h,ei);
seg.init(n);
for(int i=0;i<q;i++){
int tp;
cin>>tp;
if(tp==0){
int s,t,x;
cin>>s>>t>>x;
seg.update(s,t+1,x);
}
if(tp==1){
int s;
cin>>s;
cout<<seg.get_val(s)<<"\n";
}
}
cout<<flush;
return 0;
}
signed main(){
AOJ_DSL_2_D();
//AOJ_DSL_2_E();
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;
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;}
//BEGIN CUT HERE
template <typename E>
struct SegmentTree{
using H = function<E(E,E)>;
int n,height;
H h;
E ei;
vector<E> laz;
SegmentTree(H h,E ei):h(h),ei(ei){}
void init(int n_){
n=1;height=0;
while(n<n_) n<<=1,height++;
laz.assign(2*n,ei);
}
inline void eval(int k){
if(laz[k]==ei) return;
laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
laz[k]=ei;
}
inline void thrust(int k){
for(int i=height;i;i--) eval(k>>i);
}
void update(int a,int b,E x){
thrust(a+=n);
thrust(b+=n-1);
for(int l=a,r=b+1;l<r;l>>=1,r>>=1){
if(l&1) laz[l]=h(laz[l],x),l++;
if(r&1) --r,laz[r]=h(laz[r],x);
}
}
E get_val(int a){
thrust(a+=n);
return laz[a];
}
void set_val(int a,E x){
thrust(a+=n);
laz[a]=x;
}
};
//END CUT HERE
struct FastIO{
FastIO(){
cin.tie(0);
ios::sync_with_stdio(0);
}
}fastio_beet;
//INSERT ABOVE HERE
signed AOJ_DSL_2_D(){
int n,q;
cin>>n>>q;
auto h=[](int a,int b){(void)a;return b;};
int ei=INT_MAX;
SegmentTree<int> seg(h,ei);
seg.init(n);
for(int i=0;i<q;i++){
int tp;
cin>>tp;
if(tp==0){
int s,t,x;
cin>>s>>t>>x;
seg.update(s,t+1,x);
}
if(tp==1){
int s;
cin>>s;
cout<<seg.get_val(s)<<"\n";
}
}
cout<<flush;
return 0;
}
signed main(){
AOJ_DSL_2_D();
//AOJ_DSL_2_E();
return 0;
}
``` |
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>
#define INF_LL 9000000000000000000
#define INF 2000000000
#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
class RangeSeg{
int n;
vector<ll> data, lazy;
public:
RangeSeg(int n_){
n = 1;
while(n < n_) n <<= 1;
REP(i, n*2-1){
data.push_back(-1);
lazy.push_back(-1);
}
}
void push(int l, int r, int k){
if(lazy[k] != -1){
data[k] = lazy[k];
if(r - l > 1){
lazy[k*2+1] = lazy[k];
lazy[k*2+2] = lazy[k];
}
lazy[k] = -1;
}
}
void update(int s, int t, int l, int r, int k, int x){
push(l, r, k);
if(s <= l && r <= t){
lazy[k] = x;
push(l, r, k);
}else if(l < t && s < r){
update(s, t, (l+r)/2, r, k*2+2, x);
update(s, t, l, (l+r)/2, k*2+1, x);
}
}
void update(int s, int t, int x){
update(s, t, 0, n, 0, x);
}
void show(){
int i = 0;
int en = 1;
while(i < n*2-1){
cout << "(" << data[i] << ", " << lazy[i] << ") ";
if(i == en*2-2){
cout << endl;
en *= 2;
}
i++;
}
}
ll find(int l, int r, int k, int i){
push(l, r, k);
if(r-l == 1){
return data[k];
}
if(l <= i && i <= r){
if((l+r)/2 <= i){
return find((l+r)/2, r, k*2+2, i);
}else{
return find(l, (l+r)/2, k*2+1, i);
}
}else{
return -1;
}
}
ll find(int i){
return find(0, n, 0, i);
}
};
int main(void){
int n, q;
cin >> n >> q;
RangeSeg rs(n);
REP(i, q){
int com;
cin >> com;
if(com == 0){
int s, t, x;
cin >> s >> t >> x;
rs.update(s, t+1, x);
}else{
int x;
cin >> x;
x = rs.find(x);
if(x == -1)
cout << ((ll)1 << 31)-1 << endl;
else
cout << x << endl;
}
}
} | ### 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 <iomanip>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>
#define INF_LL 9000000000000000000
#define INF 2000000000
#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
class RangeSeg{
int n;
vector<ll> data, lazy;
public:
RangeSeg(int n_){
n = 1;
while(n < n_) n <<= 1;
REP(i, n*2-1){
data.push_back(-1);
lazy.push_back(-1);
}
}
void push(int l, int r, int k){
if(lazy[k] != -1){
data[k] = lazy[k];
if(r - l > 1){
lazy[k*2+1] = lazy[k];
lazy[k*2+2] = lazy[k];
}
lazy[k] = -1;
}
}
void update(int s, int t, int l, int r, int k, int x){
push(l, r, k);
if(s <= l && r <= t){
lazy[k] = x;
push(l, r, k);
}else if(l < t && s < r){
update(s, t, (l+r)/2, r, k*2+2, x);
update(s, t, l, (l+r)/2, k*2+1, x);
}
}
void update(int s, int t, int x){
update(s, t, 0, n, 0, x);
}
void show(){
int i = 0;
int en = 1;
while(i < n*2-1){
cout << "(" << data[i] << ", " << lazy[i] << ") ";
if(i == en*2-2){
cout << endl;
en *= 2;
}
i++;
}
}
ll find(int l, int r, int k, int i){
push(l, r, k);
if(r-l == 1){
return data[k];
}
if(l <= i && i <= r){
if((l+r)/2 <= i){
return find((l+r)/2, r, k*2+2, i);
}else{
return find(l, (l+r)/2, k*2+1, i);
}
}else{
return -1;
}
}
ll find(int i){
return find(0, n, 0, i);
}
};
int main(void){
int n, q;
cin >> n >> q;
RangeSeg rs(n);
REP(i, q){
int com;
cin >> com;
if(com == 0){
int s, t, x;
cin >> s >> t >> x;
rs.update(s, t+1, x);
}else{
int x;
cin >> x;
x = rs.find(x);
if(x == -1)
cout << ((ll)1 << 31)-1 << endl;
else
cout << x << endl;
}
}
}
``` |
#include<iostream>
using namespace std;
#include<vector>
#include<functional>
template<typename T>
struct dualsegtree{
function<T(T,T)>lazycalcfn,updatefn;
int n;
T lazydefvalue;
vector<T>dat,lazy;
vector<bool>lazyflag;
dualsegtree(int n_=0,T defvalue=0,
function<T(T,T)>lazycalcfn_=[](T a,T b){return a+b;},
function<T(T,T)>updatefn_=[](T a,T b){return a+b;},
T lazydefvalue_=0
):lazydefvalue(lazydefvalue_),
lazycalcfn(lazycalcfn_),updatefn(updatefn_)
{
n=1;
while(n<n_)n<<=1;
dat.assign(n,defvalue);
lazy.assign(n-1,lazydefvalue);
lazyflag.assign(n-1,false);
}
void copy(const vector<T>&v)
{
for(int i=0;i<v.size();i++)dat[i]=v[i];
lazy.assign(n-1,lazydefvalue);
lazyflag.assign(n-1,false);
}
void update(int a,int b,T x,int k=0,int l=0,int r=-1)//[a,b)
{
if(r<0)r=n;
if(b<=l||r<=a)return;
else if(a<=l&&r<=b)
{
if(k<n-1)
{
lazy[k]=lazycalcfn(lazy[k],x);
lazyflag[k]=true;
}
else dat[k-n+1]=updatefn(dat[k-n+1],x);
}
else
{
if(lazyflag[k])
{
update(0,n,lazy[k],k*2+1,l,(l+r)/2);
update(0,n,lazy[k],k*2+2,(l+r)/2,r);
lazy[k]=lazydefvalue;
lazyflag[k]=false;
}
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
}
T query(int i)
{
T ret=dat[i];
i+=n-1;
while(i>0)
{
i=(i-1)/2;
if(lazyflag[i])ret=updatefn(ret,lazy[i]);
}
return ret;
}
};
int main()
{
int N,Q;
cin>>N>>Q;
dualsegtree<int>P(N,(int)((1LL<<31)-1),
[](int a,int b){return b;},[](int a,int b){return b;});
for(;Q--;)
{
int q;cin>>q;
if(q==0)
{
int s,t,x;cin>>s>>t>>x;
P.update(s,t+1,x);
}
else
{
int i;cin>>i;cout<<P.query(i)<<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<iostream>
using namespace std;
#include<vector>
#include<functional>
template<typename T>
struct dualsegtree{
function<T(T,T)>lazycalcfn,updatefn;
int n;
T lazydefvalue;
vector<T>dat,lazy;
vector<bool>lazyflag;
dualsegtree(int n_=0,T defvalue=0,
function<T(T,T)>lazycalcfn_=[](T a,T b){return a+b;},
function<T(T,T)>updatefn_=[](T a,T b){return a+b;},
T lazydefvalue_=0
):lazydefvalue(lazydefvalue_),
lazycalcfn(lazycalcfn_),updatefn(updatefn_)
{
n=1;
while(n<n_)n<<=1;
dat.assign(n,defvalue);
lazy.assign(n-1,lazydefvalue);
lazyflag.assign(n-1,false);
}
void copy(const vector<T>&v)
{
for(int i=0;i<v.size();i++)dat[i]=v[i];
lazy.assign(n-1,lazydefvalue);
lazyflag.assign(n-1,false);
}
void update(int a,int b,T x,int k=0,int l=0,int r=-1)//[a,b)
{
if(r<0)r=n;
if(b<=l||r<=a)return;
else if(a<=l&&r<=b)
{
if(k<n-1)
{
lazy[k]=lazycalcfn(lazy[k],x);
lazyflag[k]=true;
}
else dat[k-n+1]=updatefn(dat[k-n+1],x);
}
else
{
if(lazyflag[k])
{
update(0,n,lazy[k],k*2+1,l,(l+r)/2);
update(0,n,lazy[k],k*2+2,(l+r)/2,r);
lazy[k]=lazydefvalue;
lazyflag[k]=false;
}
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
}
T query(int i)
{
T ret=dat[i];
i+=n-1;
while(i>0)
{
i=(i-1)/2;
if(lazyflag[i])ret=updatefn(ret,lazy[i]);
}
return ret;
}
};
int main()
{
int N,Q;
cin>>N>>Q;
dualsegtree<int>P(N,(int)((1LL<<31)-1),
[](int a,int b){return b;},[](int a,int b){return b;});
for(;Q--;)
{
int q;cin>>q;
if(q==0)
{
int s,t,x;cin>>s>>t>>x;
P.update(s,t+1,x);
}
else
{
int i;cin>>i;cout<<P.query(i)<<endl;
}
}
}
``` |
#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 < 100;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 != 99){
if(f2 && data[b/B] != -1){
for(int i = 0;i < 100;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 < 100;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 < 100;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 != 99){
if(f2 && data[b/B] != -1){
for(int i = 0;i < 100;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 < 100;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>
#include <vector>
#include <functional>
#include <limits>
template <class Data, class Operator>
class CoSegmentTree {
using OperatorMerger = std::function<Operator(Operator, Operator)>;
using Applier = std::function<Data(Data, Operator)>;
public:
int length;
std::vector<Operator> ope;
Data dat_id;
Operator ope_id;
OperatorMerger opem;
Applier app;
explicit CoSegmentTree(int N, Data dat_id, Operator ope_id, OperatorMerger opem, Applier app)
: length(1), dat_id(dat_id), ope_id(ope_id), opem(opem), app(app) {
while (length < N) length *= 2;
ope.assign(length * 2, ope_id);
}
// half-open interval [ql, qr)
void update(int ql, int qr, Operator e) { return update(ql, qr, e, 1, 0, length); }
void update(int ql, int qr, Operator e, int nidx, int nl, int nr) {
if (nr <= ql || qr <= nl) return;
if (ql <= nl && nr <= qr) {
ope[nidx] = opem(ope[nidx], e);
return;
}
// 子に作用素を伝播
ope[nidx * 2] = opem(ope[nidx * 2], ope[nidx]);
ope[nidx * 2 + 1] = opem(ope[nidx * 2 + 1], ope[nidx]);
ope[nidx] = ope_id;
// 子を再帰的に更新
update(ql, qr, e, nidx * 2, nl, (nl + nr) / 2);
update(ql, qr, e, nidx * 2 + 1, (nl + nr) / 2, nr);
}
Data query(int idx) {
int nidx = idx + length;
Data ret = app(dat_id, ope[nidx]);
while (nidx > 0) {
nidx /= 2;
ret = app(ret, ope[nidx]);
}
return ret;
}
};
const int INF = std::numeric_limits<int>::max();
int main() {
int N, Q;
std::cin >> N >> Q;
CoSegmentTree<int, int> seg(N, INF, INF,
[](int a, int b) { return (b == INF ? a : b); },
[](int a, int e) { return (e == INF ? a : e); });
for (int q = 0; q < Q; ++q) {
int k;
std::cin >> k;
if (k == 0) {
int l, r, x;
std::cin >> l >> r >> x;
seg.update(l, r + 1, x);
} else {
int i;
std::cin >> i;
std::cout << seg.query(i) << std::endl;
}
}
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 <iostream>
#include <vector>
#include <functional>
#include <limits>
template <class Data, class Operator>
class CoSegmentTree {
using OperatorMerger = std::function<Operator(Operator, Operator)>;
using Applier = std::function<Data(Data, Operator)>;
public:
int length;
std::vector<Operator> ope;
Data dat_id;
Operator ope_id;
OperatorMerger opem;
Applier app;
explicit CoSegmentTree(int N, Data dat_id, Operator ope_id, OperatorMerger opem, Applier app)
: length(1), dat_id(dat_id), ope_id(ope_id), opem(opem), app(app) {
while (length < N) length *= 2;
ope.assign(length * 2, ope_id);
}
// half-open interval [ql, qr)
void update(int ql, int qr, Operator e) { return update(ql, qr, e, 1, 0, length); }
void update(int ql, int qr, Operator e, int nidx, int nl, int nr) {
if (nr <= ql || qr <= nl) return;
if (ql <= nl && nr <= qr) {
ope[nidx] = opem(ope[nidx], e);
return;
}
// 子に作用素を伝播
ope[nidx * 2] = opem(ope[nidx * 2], ope[nidx]);
ope[nidx * 2 + 1] = opem(ope[nidx * 2 + 1], ope[nidx]);
ope[nidx] = ope_id;
// 子を再帰的に更新
update(ql, qr, e, nidx * 2, nl, (nl + nr) / 2);
update(ql, qr, e, nidx * 2 + 1, (nl + nr) / 2, nr);
}
Data query(int idx) {
int nidx = idx + length;
Data ret = app(dat_id, ope[nidx]);
while (nidx > 0) {
nidx /= 2;
ret = app(ret, ope[nidx]);
}
return ret;
}
};
const int INF = std::numeric_limits<int>::max();
int main() {
int N, Q;
std::cin >> N >> Q;
CoSegmentTree<int, int> seg(N, INF, INF,
[](int a, int b) { return (b == INF ? a : b); },
[](int a, int e) { return (e == INF ? a : e); });
for (int q = 0; q < Q; ++q) {
int k;
std::cin >> k;
if (k == 0) {
int l, r, x;
std::cin >> l >> r >> x;
seg.update(l, r + 1, x);
} else {
int i;
std::cin >> i;
std::cout << seg.query(i) << std::endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template<typename OperatorMonoid>
struct DualSegmentTree{
typedef function<OperatorMonoid(OperatorMonoid,OperatorMonoid)> H;
int n,hi;
H h;
OperatorMonoid id1;
vector<OperatorMonoid> laz;
DualSegmentTree(int n_,H h,OperatorMonoid id1):h(h),id1(id1){init(n_);}
void init(int n_){
n=1,hi=0;
while(n<n_) n<<=1,++hi;
laz.assign(n<<1,id1);
}
inline void propagate(int k){
if (laz[k]==id1) return;
laz[k<<1|0]=h(laz[k<<1|0],laz[k]);
laz[k<<1|1]=h(laz[k<<1|1],laz[k]);
laz[k]=id1;
}
inline void thrust(int k){
for (int i=hi;i;--i) propagate(k>>i);
}
void update(int a,int b,OperatorMonoid x){
if (a>=b) return;
thrust(a+=n),thrust(b+=n-1);
for (int l=a,r=b+1;l<r;l>>=1,r>>=1){
if (l&1) laz[l]=h(laz[l],x),++l;
if (r&1) --r,laz[r]=h(laz[r],x);
}
}
void set_val(int k,OperatorMonoid x){
thrust(k+=n);
laz[k]=x;
}
OperatorMonoid operator[](int k){
thrust(k+=n);
return laz[k];
}
};
// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_D
void DSL_2_D(){
int n,q; cin >> n >> q;
auto h=[](int a,int b){return b;};
DualSegmentTree<int> seg(n,h,INT_MAX);
for (;q--;){
int c,s,t,x,i; cin >> c;
if (!c){
cin >> s >> t >> x;
seg.update(s,t+1,x);
} else {
cin >> i;
cout << seg[i] << '\n';
}
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
DSL_2_D();
}
| ### 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;
template<typename OperatorMonoid>
struct DualSegmentTree{
typedef function<OperatorMonoid(OperatorMonoid,OperatorMonoid)> H;
int n,hi;
H h;
OperatorMonoid id1;
vector<OperatorMonoid> laz;
DualSegmentTree(int n_,H h,OperatorMonoid id1):h(h),id1(id1){init(n_);}
void init(int n_){
n=1,hi=0;
while(n<n_) n<<=1,++hi;
laz.assign(n<<1,id1);
}
inline void propagate(int k){
if (laz[k]==id1) return;
laz[k<<1|0]=h(laz[k<<1|0],laz[k]);
laz[k<<1|1]=h(laz[k<<1|1],laz[k]);
laz[k]=id1;
}
inline void thrust(int k){
for (int i=hi;i;--i) propagate(k>>i);
}
void update(int a,int b,OperatorMonoid x){
if (a>=b) return;
thrust(a+=n),thrust(b+=n-1);
for (int l=a,r=b+1;l<r;l>>=1,r>>=1){
if (l&1) laz[l]=h(laz[l],x),++l;
if (r&1) --r,laz[r]=h(laz[r],x);
}
}
void set_val(int k,OperatorMonoid x){
thrust(k+=n);
laz[k]=x;
}
OperatorMonoid operator[](int k){
thrust(k+=n);
return laz[k];
}
};
// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_D
void DSL_2_D(){
int n,q; cin >> n >> q;
auto h=[](int a,int b){return b;};
DualSegmentTree<int> seg(n,h,INT_MAX);
for (;q--;){
int c,s,t,x,i; cin >> c;
if (!c){
cin >> s >> t >> x;
seg.update(s,t+1,x);
} else {
cin >> i;
cout << seg[i] << '\n';
}
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
DSL_2_D();
}
``` |
#include "bits/stdc++.h"
using namespace std;
//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<(o)<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = (1LL << 31) - 1;
const int MOD = (int)(1e9 + 7);
class RangeUpdateQuery {
public:
int n;
vector<int> d;
vector<int> timestamp;
int time;
RangeUpdateQuery(int m) {
for (n = 1; n < m; n *= 2);
d.assign(n * 2, INF);
time = 0;
timestamp.assign(n * 2, 0);
}
//[l, r)
void update(int l, int r, int x) {
time++;
for (; l && l + (l&-l) <= r; l += l&-l) {
d[(n + l) / (l&-l)] = x;
timestamp[(n + l) / (l&-l)] = time;
}
for (; l < r; r -= r&-r) {
d[(n + r) / (r&-r) - 1] = x;
timestamp[(n + r) / (r&-r) - 1] = time;
}
}
int get(int i) {
int ret = d[n + i];
int max_time = timestamp[n + i];
for (int j = (n + i) / 2; j > 0; j >>= 1) {
if (max_time > timestamp[j])continue;
max_time = timestamp[j];
ret = d[j];
}
return ret;
}
};
signed main() {
int n, q; cin >> n >> q;
RangeUpdateQuery RUQ(n);
rep(i, 0, q) {
int com; cin >> com;
if (com == 0) {
int s, t, x; cin >> s >> t >> x;
RUQ.update(s, t + 1, x);
}
else {
int i; cin >> i;
cout << RUQ.get(i) << 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 "bits/stdc++.h"
using namespace std;
//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<(o)<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = (1LL << 31) - 1;
const int MOD = (int)(1e9 + 7);
class RangeUpdateQuery {
public:
int n;
vector<int> d;
vector<int> timestamp;
int time;
RangeUpdateQuery(int m) {
for (n = 1; n < m; n *= 2);
d.assign(n * 2, INF);
time = 0;
timestamp.assign(n * 2, 0);
}
//[l, r)
void update(int l, int r, int x) {
time++;
for (; l && l + (l&-l) <= r; l += l&-l) {
d[(n + l) / (l&-l)] = x;
timestamp[(n + l) / (l&-l)] = time;
}
for (; l < r; r -= r&-r) {
d[(n + r) / (r&-r) - 1] = x;
timestamp[(n + r) / (r&-r) - 1] = time;
}
}
int get(int i) {
int ret = d[n + i];
int max_time = timestamp[n + i];
for (int j = (n + i) / 2; j > 0; j >>= 1) {
if (max_time > timestamp[j])continue;
max_time = timestamp[j];
ret = d[j];
}
return ret;
}
};
signed main() {
int n, q; cin >> n >> q;
RangeUpdateQuery RUQ(n);
rep(i, 0, q) {
int com; cin >> com;
if (com == 0) {
int s, t, x; cin >> s >> t >> x;
RUQ.update(s, t + 1, x);
}
else {
int i; cin >> i;
cout << RUQ.get(i) << endl;;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool upd[(1<<18)];
int val[(1<<18)];
void Set(int a,int b,int x,int k=0,int l=0,int r=(1<<17)){
if(b<=l || r<=a)return;
if( r-l>1 && upd[k] ){
upd[k*2+1] = upd[k*2+2] = 1;
val[k*2+1] = val[k*2+2] = val[k];
}
upd[k]=0;
if(a<=l && r<=b){
upd[k]=1;
val[k]=x;
}
else{
Set(a,b,x,k*2+1,l,(l+r)/2);
Set(a,b,x,k*2+2,(l+r)/2,r);
val[k]=min(val[k*2+1],val[k*2+2]);
}
}
int Find(int a,int b,int k=0,int l=0,int r=(1<<17)){
if(b<=l || r<=a)return INT_MAX;
if( r-l>1 && upd[k] ){
upd[k*2+1] = upd[k*2+2] = 1;
val[k*2+1] = val[k*2+2] = val[k];
}
upd[k]=0;
if(a<=l && r<=b) return val[k];
else{
int L = Find(a,b,k*2+1,l,(l+r)/2);
int R = Find(a,b,k*2+2,(l+r)/2,r);
return min(L,R);
}
}
int n,m,a,b,c,d;
int main(){
memset(upd,0,sizeof(upd));
fill(val,val+(1<<18),INT_MAX);
cin>>n>>m;
while(m--){
cin>>a>>b;
if(a){
cout<<Find(b,b+1)<<endl;
}
else{
cin>>c>>d;
Set(b,c+1,d);
}
}
}
| ### 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 <bits/stdc++.h>
using namespace std;
bool upd[(1<<18)];
int val[(1<<18)];
void Set(int a,int b,int x,int k=0,int l=0,int r=(1<<17)){
if(b<=l || r<=a)return;
if( r-l>1 && upd[k] ){
upd[k*2+1] = upd[k*2+2] = 1;
val[k*2+1] = val[k*2+2] = val[k];
}
upd[k]=0;
if(a<=l && r<=b){
upd[k]=1;
val[k]=x;
}
else{
Set(a,b,x,k*2+1,l,(l+r)/2);
Set(a,b,x,k*2+2,(l+r)/2,r);
val[k]=min(val[k*2+1],val[k*2+2]);
}
}
int Find(int a,int b,int k=0,int l=0,int r=(1<<17)){
if(b<=l || r<=a)return INT_MAX;
if( r-l>1 && upd[k] ){
upd[k*2+1] = upd[k*2+2] = 1;
val[k*2+1] = val[k*2+2] = val[k];
}
upd[k]=0;
if(a<=l && r<=b) return val[k];
else{
int L = Find(a,b,k*2+1,l,(l+r)/2);
int R = Find(a,b,k*2+2,(l+r)/2,r);
return min(L,R);
}
}
int n,m,a,b,c,d;
int main(){
memset(upd,0,sizeof(upd));
fill(val,val+(1<<18),INT_MAX);
cin>>n>>m;
while(m--){
cin>>a>>b;
if(a){
cout<<Find(b,b+1)<<endl;
}
else{
cin>>c>>d;
Set(b,c+1,d);
}
}
}
``` |
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
void Init();
void update(int index, int value, int searchLeft, int searchRight, int left, int right);
int find(int index, int searchLeft, int searchRight, int left, int right);
void lazy_propagate(int index);
const int INF = INT_MAX;
vector<int> tree;
vector<int> lazy;
int n, q;
int main() {
cin >> n >> q;
Init();
for (int i = 0; i < q; i++) {
int com, a, b, x;
cin >> com;
switch (com) {
case 0:
cin >> a >> b >> x;
update(0, x, 0, n, a, b + 1);
break;
case 1:
cin >> x;
cout << find(0, 0, n, x, x + 1) << endl;
break;
}
}
return 0;
}
void Init() {
int _n = 1;
while (_n < n)
_n *= 2;
n = _n;
tree = vector<int>(2 * n - 1, INF);
lazy = vector<int>(2 * n - 1, -1);
}
void lazy_propagate(int index) {
if (lazy[index] == -1)
return;
tree[index] = lazy[index];
if (index < n - 1) {
lazy[index * 2 + 1] = lazy[index];
lazy[index * 2 + 2] = lazy[index];
}
lazy[index] = -1;
}
void update(int index, int value, int searchLeft, int searchRight, int left, int right) {
if (right <= searchLeft || searchRight <= left) {
lazy_propagate(index);
}
else if (left <= searchLeft && searchRight <= right) {
lazy[index] = value;
lazy_propagate(index);
}
else {
lazy_propagate(index);
int mid = (searchLeft + searchRight) / 2;
update(index * 2 + 1, value, searchLeft, mid, left, right);
update(index * 2 + 2, value, mid, searchRight, left, right);
tree[index] = min(tree[index * 2 + 1], tree[index * 2 + 2]);
}
}
int find(int index, int searchLeft, int searchRight, int left, int right) {
if (right <= searchLeft || searchRight <= left) {
return INF;
}
else if (left <= searchLeft && searchRight <= right) {
lazy_propagate(index);
return tree[index];
}
else {
lazy_propagate(index);
int mid = (searchLeft + searchRight) / 2;
int lValue = find(index * 2 + 1, searchLeft, mid, left, right);
int rValue = find(index * 2 + 2, mid, searchRight, left, right);
return min(lValue, rValue);
}
}
| ### 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>
#include <climits>
using namespace std;
void Init();
void update(int index, int value, int searchLeft, int searchRight, int left, int right);
int find(int index, int searchLeft, int searchRight, int left, int right);
void lazy_propagate(int index);
const int INF = INT_MAX;
vector<int> tree;
vector<int> lazy;
int n, q;
int main() {
cin >> n >> q;
Init();
for (int i = 0; i < q; i++) {
int com, a, b, x;
cin >> com;
switch (com) {
case 0:
cin >> a >> b >> x;
update(0, x, 0, n, a, b + 1);
break;
case 1:
cin >> x;
cout << find(0, 0, n, x, x + 1) << endl;
break;
}
}
return 0;
}
void Init() {
int _n = 1;
while (_n < n)
_n *= 2;
n = _n;
tree = vector<int>(2 * n - 1, INF);
lazy = vector<int>(2 * n - 1, -1);
}
void lazy_propagate(int index) {
if (lazy[index] == -1)
return;
tree[index] = lazy[index];
if (index < n - 1) {
lazy[index * 2 + 1] = lazy[index];
lazy[index * 2 + 2] = lazy[index];
}
lazy[index] = -1;
}
void update(int index, int value, int searchLeft, int searchRight, int left, int right) {
if (right <= searchLeft || searchRight <= left) {
lazy_propagate(index);
}
else if (left <= searchLeft && searchRight <= right) {
lazy[index] = value;
lazy_propagate(index);
}
else {
lazy_propagate(index);
int mid = (searchLeft + searchRight) / 2;
update(index * 2 + 1, value, searchLeft, mid, left, right);
update(index * 2 + 2, value, mid, searchRight, left, right);
tree[index] = min(tree[index * 2 + 1], tree[index * 2 + 2]);
}
}
int find(int index, int searchLeft, int searchRight, int left, int right) {
if (right <= searchLeft || searchRight <= left) {
return INF;
}
else if (left <= searchLeft && searchRight <= right) {
lazy_propagate(index);
return tree[index];
}
else {
lazy_propagate(index);
int mid = (searchLeft + searchRight) / 2;
int lValue = find(index * 2 + 1, searchLeft, mid, left, right);
int rValue = find(index * 2 + 2, mid, searchRight, left, right);
return min(lValue, rValue);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF=INT_MAX;
struct lst
{
private:
int n;
vector<int> node;
vector<int> lazy;
vector<bool> lflg;
public:
lst(int sz)
{
n=1; while(n<sz) n*=2;
node.resize(2*n-1, INF);
lazy.resize(2*n-1, 0);
lflg.resize(2*n-1, false);
}
void lazy2node(int k, int l, int r)
{
if(lflg[k])
{
node[k]=lazy[k];
lflg[k]=false;
prop(k, l, r);
}
}
void prop(int k, int l, int r)
{
if (r-l>0)
{
lazy[2*k+1]=lazy[2*k+2]=node[k];
lflg[2*k+1]=lflg[2*k+2]=true;
}
}
void update(int qa, int qb, int v, int k=0, int l=0, int r=-1)
{
if(r<0) r=n-1;
lazy2node(k, l, r);
if(qb<l || r<qa) return;
if(qa<=l && r<=qb)
{
node[k] = v;
prop(k, l, r);
}
else
{
int m=(l+r)/2;
update(qa, qb, v, 2*k+1, l, m);
update(qa, qb, v, 2*k+2, m+1, r);
}
}
int get(int qa, int qb, int k=0, int l=0, int r=-1)
{
if(r<0) r=n-1;
lazy2node(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+1, l, m);
int lr = get(qa, qb, 2*k+2, m+1, r);
return min(lv, lr);
}
}
};
int main()
{
int n, q;
scanf("%d %d", &n, &q);
lst tree = lst(n);
for(int i=0; i<q; i++)
{
int t;
scanf("%d", &t);
if(t)
{
int s;
scanf("%d", &s);
printf("%d\n", tree.get(s,s));
}
else
{
int s, t, v;
scanf("%d %d %d", &s, &t, &v);
tree.update(s, t, v);
}
}
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>
using namespace std;
const int INF=INT_MAX;
struct lst
{
private:
int n;
vector<int> node;
vector<int> lazy;
vector<bool> lflg;
public:
lst(int sz)
{
n=1; while(n<sz) n*=2;
node.resize(2*n-1, INF);
lazy.resize(2*n-1, 0);
lflg.resize(2*n-1, false);
}
void lazy2node(int k, int l, int r)
{
if(lflg[k])
{
node[k]=lazy[k];
lflg[k]=false;
prop(k, l, r);
}
}
void prop(int k, int l, int r)
{
if (r-l>0)
{
lazy[2*k+1]=lazy[2*k+2]=node[k];
lflg[2*k+1]=lflg[2*k+2]=true;
}
}
void update(int qa, int qb, int v, int k=0, int l=0, int r=-1)
{
if(r<0) r=n-1;
lazy2node(k, l, r);
if(qb<l || r<qa) return;
if(qa<=l && r<=qb)
{
node[k] = v;
prop(k, l, r);
}
else
{
int m=(l+r)/2;
update(qa, qb, v, 2*k+1, l, m);
update(qa, qb, v, 2*k+2, m+1, r);
}
}
int get(int qa, int qb, int k=0, int l=0, int r=-1)
{
if(r<0) r=n-1;
lazy2node(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+1, l, m);
int lr = get(qa, qb, 2*k+2, m+1, r);
return min(lv, lr);
}
}
};
int main()
{
int n, q;
scanf("%d %d", &n, &q);
lst tree = lst(n);
for(int i=0; i<q; i++)
{
int t;
scanf("%d", &t);
if(t)
{
int s;
scanf("%d", &s);
printf("%d\n", tree.get(s,s));
}
else
{
int s, t, v;
scanf("%d %d %d", &s, &t, &v);
tree.update(s, t, v);
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
int t[(1<<18)];
//0 2 1 0 0
//0 2 1 0 0 65536
//0 2 1 0 0 2
void Set(int a,int b,int x,int k=0,int l=0,int r=(1<<17)){
if(b<=l || r<=a)return;
if(a<=l && r<=b){
t[k]=max(t[k],x);
return;
}
int m=(l+r)/2;
Set(a,b,x,k*2+1,l,m);
Set(a,b,x,k*2+2,m,r);
}
int Get(int i){
i+=(1<<17)-1;
int res=t[i];
while(i){
i=(i-1)/2;
res=max(res,t[i]);
}
return res;
}
int n,q;
int value[100005];
int main(){
value[0]=2147483647;
scanf("%d %d",&n,&q);
for(int i=1;i<=q;i++){
int type;
scanf("%d",&type);
if(type==0){
int l,r;
scanf("%d %d %d",&l,&r,&value[i]);
r++;
Set(l,r,i);
}else{
int target;
scanf("%d",&target);
int ans=Get(target);
printf("%d\n",value[ans]);
}
}
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>
using namespace std;
int t[(1<<18)];
//0 2 1 0 0
//0 2 1 0 0 65536
//0 2 1 0 0 2
void Set(int a,int b,int x,int k=0,int l=0,int r=(1<<17)){
if(b<=l || r<=a)return;
if(a<=l && r<=b){
t[k]=max(t[k],x);
return;
}
int m=(l+r)/2;
Set(a,b,x,k*2+1,l,m);
Set(a,b,x,k*2+2,m,r);
}
int Get(int i){
i+=(1<<17)-1;
int res=t[i];
while(i){
i=(i-1)/2;
res=max(res,t[i]);
}
return res;
}
int n,q;
int value[100005];
int main(){
value[0]=2147483647;
scanf("%d %d",&n,&q);
for(int i=1;i<=q;i++){
int type;
scanf("%d",&type);
if(type==0){
int l,r;
scanf("%d %d %d",&l,&r,&value[i]);
r++;
Set(l,r,i);
}else{
int target;
scanf("%d",&target);
int ans=Get(target);
printf("%d\n",value[ans]);
}
}
return 0;
}
``` |
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
struct RangeUpdateQuery{
int n, now;
std::vector<int> dat, time;
void init(int n_){
n = 1;
now = 0;
while(n < n_) n *= 2;
dat.resize(2 * n - 1, std::numeric_limits<int>::max());
time.resize(2 * n - 1, 0);
}
void update(int a, int b, int k, int l, int r, int x){
if(r <= a || b <= l) return;
if(a <= l && r <= b){
dat[k] = x;
time[k] = now;
}
else{
update(a, b, k * 2 + 1, l, (l + r) / 2, x);
update(a, b, k * 2 + 2, (l + r) / 2, r, x);
}
}
void update(int a, int b, int x){
now++;
update(a, b, 0, 0, n, x);
}
int query(int k){
k += n - 1;
int res = dat[k], recent = time[k];
while(k > 0) {
k = (k - 1) / 2;
if(recent < time[k]){
recent = time[k];
res = dat[k];
}
}
return res;
}
};
using namespace std;
int main(){
int n, q;
cin >> n >> q;
RangeUpdateQuery ruq;
ruq.init(n);
while(q--){
int com;
cin >> com;
if(com){
int i;
cin >> i;
cout << ruq.query(i) << "\n";
}else{
int s, t, x;
cin >> s >> t >> x;
ruq.update(s, t + 1, x);
}
}
}
| ### 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 <iostream>
#include <vector>
#include <limits>
#include <algorithm>
struct RangeUpdateQuery{
int n, now;
std::vector<int> dat, time;
void init(int n_){
n = 1;
now = 0;
while(n < n_) n *= 2;
dat.resize(2 * n - 1, std::numeric_limits<int>::max());
time.resize(2 * n - 1, 0);
}
void update(int a, int b, int k, int l, int r, int x){
if(r <= a || b <= l) return;
if(a <= l && r <= b){
dat[k] = x;
time[k] = now;
}
else{
update(a, b, k * 2 + 1, l, (l + r) / 2, x);
update(a, b, k * 2 + 2, (l + r) / 2, r, x);
}
}
void update(int a, int b, int x){
now++;
update(a, b, 0, 0, n, x);
}
int query(int k){
k += n - 1;
int res = dat[k], recent = time[k];
while(k > 0) {
k = (k - 1) / 2;
if(recent < time[k]){
recent = time[k];
res = dat[k];
}
}
return res;
}
};
using namespace std;
int main(){
int n, q;
cin >> n >> q;
RangeUpdateQuery ruq;
ruq.init(n);
while(q--){
int com;
cin >> com;
if(com){
int i;
cin >> i;
cout << ruq.query(i) << "\n";
}else{
int s, t, x;
cin >> s >> t >> x;
ruq.update(s, t + 1, x);
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;
template <class operator_monoid> class dual_segment_tree{
public:
typedef typename operator_monoid::underlying_type underlying_type;
typedef typename operator_monoid::value_type value_type;
size_t n;
vector<underlying_type> v;
vector<value_type> w;
underlying_type e = operator_monoid::unit();
dual_segment_tree(size_t size){
n = 1;
while(n < size) n <<= 1;
v.resize(2*n, e);
w.resize(size, operator_monoid::default_value());
}
dual_segment_tree(vector<value_type> _w){
size_t size = w.size();
n = 1;
while(n < size) n <<= 1;
v.resize(2*n, e);
w(_w);
}
void range_operate(underlying_type f, size_t a, size_t b, size_t k, size_t l, size_t r){
if(r <= a || b <= l) return;
if(a <= l && r <= b) v[k] = operator_monoid::compose(f, v[k]);
else{
if(2*k < v.size())
v[2*k] = operator_monoid::compose(v[k], v[2*k]);
if(2*k+1 < v.size())
v[2*k+1] = operator_monoid::compose(v[k], v[2*k+1]);
v[k] = operator_monoid::unit();
range_operate(f, a, b, 2*k, l, (l+r+1)/2);
range_operate(f, a, b, 2*k+1, (l+r+1)/2, r);
}
}
void range_operate(underlying_type f, size_t a, size_t b){
range_operate(f, a, b, 1, 0, n);
}
value_type point_get(size_t i){
range_operate(e, i, i+1, 1, 0, n);
return operator_monoid::operate(v[i+n], w[i]);
}
};
struct assign_operator_monoid{
using underlying_type = pair<bool, Int>;
using value_type = Int;
static underlying_type unit(){
return pair<bool, int>(true, 0);
}
static underlying_type compose(underlying_type l, underlying_type r){
if(l == unit()) return r;
return l;
}
static value_type default_value(){
return (1ll<<31)-1;
}
static value_type operate(underlying_type f, value_type x){
return f.first ? x : f.second;
}
};
int main(){
// cin.tie(0);
// ios::sync_with_stdio(false);
Int n, q; cin >> n >> q;
dual_segment_tree<assign_operator_monoid> dst(n);
while(q--){
Int p; cin >> p;
if(p == 0){
Int s, t, x; cin >> s >> t >> x;
dst.range_operate(pair<bool, Int>(false, x), s, t+1);
}else{
Int i; cin >> i;
cout << dst.point_get(i) << "\n";
// if(i == 5)
// for(Int j=0; j<64; ++j)
// cout << j << " " << (dst.v[j].first ? "id" : "no") << " " << dst.v[j].second << "\n";
}
}
}
| ### 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;
using Int = int_fast64_t;
template <class operator_monoid> class dual_segment_tree{
public:
typedef typename operator_monoid::underlying_type underlying_type;
typedef typename operator_monoid::value_type value_type;
size_t n;
vector<underlying_type> v;
vector<value_type> w;
underlying_type e = operator_monoid::unit();
dual_segment_tree(size_t size){
n = 1;
while(n < size) n <<= 1;
v.resize(2*n, e);
w.resize(size, operator_monoid::default_value());
}
dual_segment_tree(vector<value_type> _w){
size_t size = w.size();
n = 1;
while(n < size) n <<= 1;
v.resize(2*n, e);
w(_w);
}
void range_operate(underlying_type f, size_t a, size_t b, size_t k, size_t l, size_t r){
if(r <= a || b <= l) return;
if(a <= l && r <= b) v[k] = operator_monoid::compose(f, v[k]);
else{
if(2*k < v.size())
v[2*k] = operator_monoid::compose(v[k], v[2*k]);
if(2*k+1 < v.size())
v[2*k+1] = operator_monoid::compose(v[k], v[2*k+1]);
v[k] = operator_monoid::unit();
range_operate(f, a, b, 2*k, l, (l+r+1)/2);
range_operate(f, a, b, 2*k+1, (l+r+1)/2, r);
}
}
void range_operate(underlying_type f, size_t a, size_t b){
range_operate(f, a, b, 1, 0, n);
}
value_type point_get(size_t i){
range_operate(e, i, i+1, 1, 0, n);
return operator_monoid::operate(v[i+n], w[i]);
}
};
struct assign_operator_monoid{
using underlying_type = pair<bool, Int>;
using value_type = Int;
static underlying_type unit(){
return pair<bool, int>(true, 0);
}
static underlying_type compose(underlying_type l, underlying_type r){
if(l == unit()) return r;
return l;
}
static value_type default_value(){
return (1ll<<31)-1;
}
static value_type operate(underlying_type f, value_type x){
return f.first ? x : f.second;
}
};
int main(){
// cin.tie(0);
// ios::sync_with_stdio(false);
Int n, q; cin >> n >> q;
dual_segment_tree<assign_operator_monoid> dst(n);
while(q--){
Int p; cin >> p;
if(p == 0){
Int s, t, x; cin >> s >> t >> x;
dst.range_operate(pair<bool, Int>(false, x), s, t+1);
}else{
Int i; cin >> i;
cout << dst.point_get(i) << "\n";
// if(i == 5)
// for(Int j=0; j<64; ++j)
// cout << j << " " << (dst.v[j].first ? "id" : "no") << " " << dst.v[j].second << "\n";
}
}
}
``` |
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const long long INF = (1 << 31) - 1;
struct segtree{
int size;
vector<long long> updated;
segtree(int n){
size = 1;
while(n>size){
size *= 2;
}
updated.assign(2*size-1,INF);
}
void update(int a,int b,long long x,int k,int l,int r){
if(b<=l||r<=a) return;
if(a<=l&&r<=b) updated[k] = x;
else{
if(updated[k]>=0){
updated[2*k+1] = updated[k];
updated[2*k+2] = updated[k];
updated[k] = -1;
}
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,long long x){
update(a,b+1,x,0,0,size);
}
long long find(int i,int k,int l,int r){
if(i<l||r<=i) return -1;
if(updated[k]>=0) return updated[k];
else return max(find(i,2*k+1,l,(l+r)/2),find(i,2*k+2,(l+r)/2,r));
}
long long find(int i){
return find(i,0,0,size);
}
};
int main(){
int n,q;
scanf("%d %d",&n,&q);
segtree st(n);
for(int i=0;i<q;i++){
int cmd;
scanf("%d",&cmd);
if(cmd==0){
long long s,t,x;
scanf("%lld %lld %lld",&s,&t,&x);
st.update(s,t,x);
}else if(cmd==1){
int i;
scanf("%d",&i);
printf("%lld\n",st.find(i));
}
}
} | ### 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<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const long long INF = (1 << 31) - 1;
struct segtree{
int size;
vector<long long> updated;
segtree(int n){
size = 1;
while(n>size){
size *= 2;
}
updated.assign(2*size-1,INF);
}
void update(int a,int b,long long x,int k,int l,int r){
if(b<=l||r<=a) return;
if(a<=l&&r<=b) updated[k] = x;
else{
if(updated[k]>=0){
updated[2*k+1] = updated[k];
updated[2*k+2] = updated[k];
updated[k] = -1;
}
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,long long x){
update(a,b+1,x,0,0,size);
}
long long find(int i,int k,int l,int r){
if(i<l||r<=i) return -1;
if(updated[k]>=0) return updated[k];
else return max(find(i,2*k+1,l,(l+r)/2),find(i,2*k+2,(l+r)/2,r));
}
long long find(int i){
return find(i,0,0,size);
}
};
int main(){
int n,q;
scanf("%d %d",&n,&q);
segtree st(n);
for(int i=0;i<q;i++){
int cmd;
scanf("%d",&cmd);
if(cmd==0){
long long s,t,x;
scanf("%lld %lld %lld",&s,&t,&x);
st.update(s,t,x);
}else if(cmd==1){
int i;
scanf("%d",&i);
printf("%lld\n",st.find(i));
}
}
}
``` |
#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <list>
using namespace std;
typedef pair<long long int, long long int> P;
long long int INF = 2147483647;
int const TREE_SIZE = 1 << 20;
long long int seg_tree[TREE_SIZE];
long long int T[TREE_SIZE];
int find(int a){
a += TREE_SIZE / 2;
int ret = seg_tree[a];
int tmax = T[a];
for(int x = a / 2; x > 0; x /= 2){
if(T[x] > tmax){
tmax = T[x];
ret = seg_tree[x];
}
}
return ret;
}
void update(int a, int b, int index, int num, int time, int l, int r){
// 外からは query(a, b, 1, 0, TREE_SIZE / 2) のように呼ぶ
if(r <= a || b <= l){
return;
}
if(a <= l && r <= b){
seg_tree[index] = num;
T[index] = time;
return;
}
update(a, b, index * 2, num, time, l, (l + r) / 2);
update(a, b, index * 2 + 1, num, time, (l + r) / 2, r);
return;
}
int main(){
int n, q;
cin >> n >> q;
for(int i = 0; i < TREE_SIZE; i++){
seg_tree[i] = INF;
T[i] = -1;
}
for(int i = 0; i < q; i++){
long long int num, a, b, c;
cin >> num;
if(num == 0){
cin >> a >> b >> c;
update(a, b + 1, 1, c, i, 0, TREE_SIZE / 2);
}else{
cin >> a;
cout << find(a) << endl;
}
/*
for(int j = 0; j < n; j++){
cout << find(j) << " ";
}
cout << endl;
*/
}
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
#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <list>
using namespace std;
typedef pair<long long int, long long int> P;
long long int INF = 2147483647;
int const TREE_SIZE = 1 << 20;
long long int seg_tree[TREE_SIZE];
long long int T[TREE_SIZE];
int find(int a){
a += TREE_SIZE / 2;
int ret = seg_tree[a];
int tmax = T[a];
for(int x = a / 2; x > 0; x /= 2){
if(T[x] > tmax){
tmax = T[x];
ret = seg_tree[x];
}
}
return ret;
}
void update(int a, int b, int index, int num, int time, int l, int r){
// 外からは query(a, b, 1, 0, TREE_SIZE / 2) のように呼ぶ
if(r <= a || b <= l){
return;
}
if(a <= l && r <= b){
seg_tree[index] = num;
T[index] = time;
return;
}
update(a, b, index * 2, num, time, l, (l + r) / 2);
update(a, b, index * 2 + 1, num, time, (l + r) / 2, r);
return;
}
int main(){
int n, q;
cin >> n >> q;
for(int i = 0; i < TREE_SIZE; i++){
seg_tree[i] = INF;
T[i] = -1;
}
for(int i = 0; i < q; i++){
long long int num, a, b, c;
cin >> num;
if(num == 0){
cin >> a >> b >> c;
update(a, b + 1, 1, c, i, 0, TREE_SIZE / 2);
}else{
cin >> a;
cout << find(a) << endl;
}
/*
for(int j = 0; j < n; j++){
cout << find(j) << " ";
}
cout << endl;
*/
}
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];
}
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;
}
}
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 <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];
}
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;
}
}
return 0;
}
``` |
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<ctime>
using namespace std;
const int NIL = -1;
const int MAX = 2147483647;
clock_t START, END;
struct Node {
int l, r, key, lazy;
};
bool debug = false;
Node Tree[500010];
int mark[500010];
int n = 0, N = 1;
void update(int x, int y, int z, int k, int height)
{
if (!mark[k]) {
Tree[k].l = k * (1 << height) - N;
Tree[k].r = min(Tree[k].l + (1 << height) - 1, n - 1);
mark[k] = 1;
}
if (x > y)
return;
if (x <= Tree[k].l && Tree[k].r <= y) {
Tree[k].key = z;
Tree[k].lazy = 1;
return;
}
else if (Tree[k].r < x || Tree[k].l > y) {
return;
}
else {
if (Tree[k].lazy) {
Tree[k].lazy = 0;
Tree[2 * k].key = Tree[2 * k + 1].key = Tree[k].key;
Tree[2 * k].lazy = Tree[2 * k + 1].lazy = 1;
}
update(x, min(Tree[k].l + (1 << (height - 1)) - 1, y), z, 2 * k, height - 1);
update(max(Tree[k].l + (1 << (height - 1)), x), y, z, 2 * k + 1, height - 1);
}
}
int find(int k, int height, int z)
{
if (!mark[k]) {
Tree[k].l = k * (1 << height) - N;
Tree[k].r = min(Tree[k].l + (1 << height) - 1, n - 1);
mark[k] = 1;
}
if (Tree[k].lazy) {
Tree[2 * k].key = Tree[2 * k + 1].key = Tree[k].key;
Tree[2 * k].lazy = Tree[2 * k + 1].lazy = 1;
return Tree[k].key;
}
else {
if (z <= Tree[k].l + (1 << (height - 1)) - 1)
return find(2 * k, height - 1, z);
else
return find(2 * k + 1, height - 1, z);
}
}
int main(void)
{
if (debug) {
START = clock();
freopen("in15.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
int q, com, x, s, t, k, height = 0;
scanf("%d%d", &n, &q);
while (N < n) {
N *= 2;
height++;
}
for (int i = 1; i <= n + N - 1; i++) {
Tree[i].key = MAX;
Tree[i].lazy = 1;
}
for (int i = 0; i < q; i++) {
scanf("%d", &com);
if (com) {
scanf("%d", &k);
printf("%d\n", find(1, height, k));
}
else {
scanf("%d%d%d", &s, &t, &x);
update(s, t, x, 1, height);
}
}
if (debug) {
END = clock();
double endtime = (double)(END - START) / 1000;
printf("total time = %lf s", endtime);
}
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<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<ctime>
using namespace std;
const int NIL = -1;
const int MAX = 2147483647;
clock_t START, END;
struct Node {
int l, r, key, lazy;
};
bool debug = false;
Node Tree[500010];
int mark[500010];
int n = 0, N = 1;
void update(int x, int y, int z, int k, int height)
{
if (!mark[k]) {
Tree[k].l = k * (1 << height) - N;
Tree[k].r = min(Tree[k].l + (1 << height) - 1, n - 1);
mark[k] = 1;
}
if (x > y)
return;
if (x <= Tree[k].l && Tree[k].r <= y) {
Tree[k].key = z;
Tree[k].lazy = 1;
return;
}
else if (Tree[k].r < x || Tree[k].l > y) {
return;
}
else {
if (Tree[k].lazy) {
Tree[k].lazy = 0;
Tree[2 * k].key = Tree[2 * k + 1].key = Tree[k].key;
Tree[2 * k].lazy = Tree[2 * k + 1].lazy = 1;
}
update(x, min(Tree[k].l + (1 << (height - 1)) - 1, y), z, 2 * k, height - 1);
update(max(Tree[k].l + (1 << (height - 1)), x), y, z, 2 * k + 1, height - 1);
}
}
int find(int k, int height, int z)
{
if (!mark[k]) {
Tree[k].l = k * (1 << height) - N;
Tree[k].r = min(Tree[k].l + (1 << height) - 1, n - 1);
mark[k] = 1;
}
if (Tree[k].lazy) {
Tree[2 * k].key = Tree[2 * k + 1].key = Tree[k].key;
Tree[2 * k].lazy = Tree[2 * k + 1].lazy = 1;
return Tree[k].key;
}
else {
if (z <= Tree[k].l + (1 << (height - 1)) - 1)
return find(2 * k, height - 1, z);
else
return find(2 * k + 1, height - 1, z);
}
}
int main(void)
{
if (debug) {
START = clock();
freopen("in15.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
int q, com, x, s, t, k, height = 0;
scanf("%d%d", &n, &q);
while (N < n) {
N *= 2;
height++;
}
for (int i = 1; i <= n + N - 1; i++) {
Tree[i].key = MAX;
Tree[i].lazy = 1;
}
for (int i = 0; i < q; i++) {
scanf("%d", &com);
if (com) {
scanf("%d", &k);
printf("%d\n", find(1, height, k));
}
else {
scanf("%d%d%d", &s, &t, &x);
update(s, t, x, 1, height);
}
}
if (debug) {
END = clock();
double endtime = (double)(END - START) / 1000;
printf("total time = %lf s", endtime);
}
return 0;
}
``` |
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = (1ll << 31) - 1;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
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; }
template<typename T>
struct RangeUpdateQuery {
int n;
vector<int>d;
RangeUpdateQuery(int m) {
for (n = 1; n < m; n <<= 1);
d.assign(2 * n, INF);
}
int find(int i) {
update(i, i + 1);
return d[n + i];
}
void update(int a, int b, int x=-1, int k = 1, int l = 0, int r = -1) {
if (r < 0)r = n;
if (r <= a || b <= l)return;
else if (a <= l&&r <= b) {
if (x >= 0)d[k] = x;
return;
}
else {
if (d[k] != INF)d[2 * k] = d[2 * k + 1] = d[k], d[k] = INF;
update(a, b, x, 2 * k, l, (l + r) / 2);
update(a, b, x, 2 * k + 1, (l + r) / 2, r);
}
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q; cin >> n >> q;
RangeUpdateQuery<int>ruq(n);
rep(i, 0, q) {
int com; cin >> com;
if (com) {
int x; cin >> x;
cout << ruq.find(x) << endl;
}
else {
int s, t, x; cin >> s >> t >> x;
ruq.update(s, t + 1, x);
}
}
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;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = (1ll << 31) - 1;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
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; }
template<typename T>
struct RangeUpdateQuery {
int n;
vector<int>d;
RangeUpdateQuery(int m) {
for (n = 1; n < m; n <<= 1);
d.assign(2 * n, INF);
}
int find(int i) {
update(i, i + 1);
return d[n + i];
}
void update(int a, int b, int x=-1, int k = 1, int l = 0, int r = -1) {
if (r < 0)r = n;
if (r <= a || b <= l)return;
else if (a <= l&&r <= b) {
if (x >= 0)d[k] = x;
return;
}
else {
if (d[k] != INF)d[2 * k] = d[2 * k + 1] = d[k], d[k] = INF;
update(a, b, x, 2 * k, l, (l + r) / 2);
update(a, b, x, 2 * k + 1, (l + r) / 2, r);
}
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q; cin >> n >> q;
RangeUpdateQuery<int>ruq(n);
rep(i, 0, q) {
int com; cin >> com;
if (com) {
int x; cin >> x;
cout << ruq.find(x) << endl;
}
else {
int s, t, x; cin >> s >> t >> x;
ruq.update(s, t + 1, x);
}
}
return 0;
}
``` |
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
int arr[100005 << 2], value[100005], n, q;
void update(int a, int b, int x, int k = 0, int l = 0, int r = (1 << 17)) {
if (b <= l || r <= a)return;
if (a <= l && r <= b) {
arr[k] = max(arr[k], x);
return;
}
int m = (l + r) / 2;
update(a, b, x, k * 2 + 1, l, m);
update(a, b, x, k * 2 + 2, m, r);
}
int query(int x) {
x += (1 << 17) - 1;
int res = arr[x];
while (x) {
x = (x - 1) / 2;
res = max(res, arr[x]);
}
return res;
}
int main()
{
queue<int> r;
value[0] = 2147483647;
cin >> n >> q;
for (int i = 1; i <= q; ++i) {
int ch, a, b;
cin >> ch;
if (ch) {
cin >> a;
r.push(value[query(a)]);
}
else {
cin >> a >> b >> value[i];
++b;
update(a, b, i);
}
}
while (!r.empty())
{
cout << r.front() << endl;
r.pop();
}
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 <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
int arr[100005 << 2], value[100005], n, q;
void update(int a, int b, int x, int k = 0, int l = 0, int r = (1 << 17)) {
if (b <= l || r <= a)return;
if (a <= l && r <= b) {
arr[k] = max(arr[k], x);
return;
}
int m = (l + r) / 2;
update(a, b, x, k * 2 + 1, l, m);
update(a, b, x, k * 2 + 2, m, r);
}
int query(int x) {
x += (1 << 17) - 1;
int res = arr[x];
while (x) {
x = (x - 1) / 2;
res = max(res, arr[x]);
}
return res;
}
int main()
{
queue<int> r;
value[0] = 2147483647;
cin >> n >> q;
for (int i = 1; i <= q; ++i) {
int ch, a, b;
cin >> ch;
if (ch) {
cin >> a;
r.push(value[query(a)]);
}
else {
cin >> a >> b >> value[i];
++b;
update(a, b, i);
}
}
while (!r.empty())
{
cout << r.front() << endl;
r.pop();
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
//BEGIN CUT HERE
template <typename T,typename E>
struct SegmentTree{
using G = function<T(T,E)>;
using H = function<E(E,E)>;
int n;
G g;
H h;
T ti;
E ei;
vector<T> dat;
vector<E> laz;
SegmentTree(){};
SegmentTree(int n_,G g,H h,T ti,E ei):
g(g),h(h),ti(ti),ei(ei){
init(n_);
}
void init(int n_){
n=1;
while(n<n_) n*=2;
dat.clear();
dat.resize(n,ti);
laz.clear();
laz.resize(2*n-1,ei);
}
void build(int n_, vector<T> v){
for(int i=0;i<n_;i++) dat[i]=v[i];
}
void eval(int k){
if(k*2+1<2*n-1){
laz[k*2+1]=h(laz[k*2+1],laz[k]);
laz[k*2+2]=h(laz[k*2+2],laz[k]);
laz[k]=ei;
}
}
void update(int a,int b,E x,int k,int l,int r){
eval(k);
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
laz[k]=h(laz[k],x);
return;
}
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
void update(int a,int b,E x){
update(a,b,x,0,0,n);
}
void eval2(int k){
if(k) eval2((k-1)/2);
eval(k);
}
T query(int k){
T c=dat[k];
k+=n-1;
eval2(k);
return g(c,laz[k]);
}
};
//END CUT HERE
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n,q;
cin>>n>>q;
SegmentTree<int,int> beet(n,
[&](int a,int b){ return b<0?a:b;},
[&](int a,int b){ return b<0?a:b;},
INT_MAX,-1);
for(int i=0;i<q;i++){
int c;
cin>>c;
if(c){
int x;
cin>>x;
cout<<beet.query(x)<<endl;
}else{
int s,t,x;
cin>>s>>t>>x;
t++;
beet.update(s,t,x);
}
}
return 0;
}
/*
verified on 2018/03/04
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_D&lang=jp
*/
| ### 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;
//BEGIN CUT HERE
template <typename T,typename E>
struct SegmentTree{
using G = function<T(T,E)>;
using H = function<E(E,E)>;
int n;
G g;
H h;
T ti;
E ei;
vector<T> dat;
vector<E> laz;
SegmentTree(){};
SegmentTree(int n_,G g,H h,T ti,E ei):
g(g),h(h),ti(ti),ei(ei){
init(n_);
}
void init(int n_){
n=1;
while(n<n_) n*=2;
dat.clear();
dat.resize(n,ti);
laz.clear();
laz.resize(2*n-1,ei);
}
void build(int n_, vector<T> v){
for(int i=0;i<n_;i++) dat[i]=v[i];
}
void eval(int k){
if(k*2+1<2*n-1){
laz[k*2+1]=h(laz[k*2+1],laz[k]);
laz[k*2+2]=h(laz[k*2+2],laz[k]);
laz[k]=ei;
}
}
void update(int a,int b,E x,int k,int l,int r){
eval(k);
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
laz[k]=h(laz[k],x);
return;
}
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
void update(int a,int b,E x){
update(a,b,x,0,0,n);
}
void eval2(int k){
if(k) eval2((k-1)/2);
eval(k);
}
T query(int k){
T c=dat[k];
k+=n-1;
eval2(k);
return g(c,laz[k]);
}
};
//END CUT HERE
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n,q;
cin>>n>>q;
SegmentTree<int,int> beet(n,
[&](int a,int b){ return b<0?a:b;},
[&](int a,int b){ return b<0?a:b;},
INT_MAX,-1);
for(int i=0;i<q;i++){
int c;
cin>>c;
if(c){
int x;
cin>>x;
cout<<beet.query(x)<<endl;
}else{
int s,t,x;
cin>>s>>t>>x;
t++;
beet.update(s,t,x);
}
}
return 0;
}
/*
verified on 2018/03/04
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_D&lang=jp
*/
``` |
#include <stdio.h>
#include <utility>
typedef long long ll;
int main(void) {
ll i, j, k, n, q, c, s, t, x;
scanf("%lld%lld", &n, &q);
ll f = 1;
std::pair<ll, ll> seg[4 * n];
while(f < n) f *= 2;
for(i = f; i < f * 2; ++i) seg[i].first = 2147483647, seg[i].second = 0;
for(i = 0; i < f; ++i) seg[i].first = -1, seg[i].second = -1;
for(i = 1; i <= q; ++i) {
scanf("%lld", &c);
if(!c) {
scanf("%lld%lld%lld", &s, &t, &x);
ll r = s + f;
while(1) {
ll las, ra = 1;
while(!(r % 2)) r /= 2;
while(ra <= r) ra *= 2;
ra /= 2;
las = f / ra * (r + 1 - ra) - 1;
while(las > t) {
r *= 2;
ra *= 2;
las = f / ra * (r + 1 - ra) - 1;
}
seg[r].first = x;
seg[r].second = i;
if(las == t) break;
r++;
}
} else {
scanf("%d", &s);
int now = s + f, nowa, nown = -1;
while(now) {
if(seg[now].second > nown) nowa = seg[now].first, nown = seg[now].second;
now /= 2;
}
printf("%lld\n", nowa);
}
}
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 <stdio.h>
#include <utility>
typedef long long ll;
int main(void) {
ll i, j, k, n, q, c, s, t, x;
scanf("%lld%lld", &n, &q);
ll f = 1;
std::pair<ll, ll> seg[4 * n];
while(f < n) f *= 2;
for(i = f; i < f * 2; ++i) seg[i].first = 2147483647, seg[i].second = 0;
for(i = 0; i < f; ++i) seg[i].first = -1, seg[i].second = -1;
for(i = 1; i <= q; ++i) {
scanf("%lld", &c);
if(!c) {
scanf("%lld%lld%lld", &s, &t, &x);
ll r = s + f;
while(1) {
ll las, ra = 1;
while(!(r % 2)) r /= 2;
while(ra <= r) ra *= 2;
ra /= 2;
las = f / ra * (r + 1 - ra) - 1;
while(las > t) {
r *= 2;
ra *= 2;
las = f / ra * (r + 1 - ra) - 1;
}
seg[r].first = x;
seg[r].second = i;
if(las == t) break;
r++;
}
} else {
scanf("%d", &s);
int now = s + f, nowa, nown = -1;
while(now) {
if(seg[now].second > nown) nowa = seg[now].first, nown = seg[now].second;
now /= 2;
}
printf("%lld\n", nowa);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define FOR(i,x,n) for(ll i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define MOD (1000000007)
#define INF (1e9)
#define INFL (1e18)
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template<class T>using arr=vector<vector<T>>;
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} cout << a[a.size()-1] << endl;}
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 LEAF ((ll)pow(2, 17)-1)
#define NUM ((ll)pow(2, 18)-1)
using P=pair<ll, ll>;
vector<P> seg(NUM, {(ll)pow(2,31)-1, 0});
void update(ll s, ll t, ll x, ll n, ll l, ll r, ll k){
// prllf("%d, %d, %d\n", l ,r, k);
if(r < s || t < l) {
// pr("c");
return;}
if(l==r) {seg[k] = {x, n};
// pr("a");
return;}
if(s<=l && r<=t) {seg[k] = {x, n};
// pr("b");
return;}
ll mid = (l+r)/2;
ll chl = 2*k+1, chr = 2*k+2;
update(s, t, x, n, l, mid, chl);
update(s, t, x, n, mid+1, r, chr);
}
ll find(ll i, P p){
if(seg[i].second > p.second) p = seg[i];
if(i==0) return p.first;
return find((i-1)/2, p);
}
int main()
{
ll n, q; cin >> n >> q;
ll c, s, t, x;
rep(i, q){
cin >> c;
if(c==1){
cin >> x;
ll k = find(LEAF + x, {0, -1});
pr(k);
}else{
cin >> s >> t >> x;
update(s, t, x, i+1, 0, LEAF, 0);
}
}
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 rep(i,n) for(ll i=0; i<(n); i++)
#define FOR(i,x,n) for(ll i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define MOD (1000000007)
#define INF (1e9)
#define INFL (1e18)
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template<class T>using arr=vector<vector<T>>;
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} cout << a[a.size()-1] << endl;}
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 LEAF ((ll)pow(2, 17)-1)
#define NUM ((ll)pow(2, 18)-1)
using P=pair<ll, ll>;
vector<P> seg(NUM, {(ll)pow(2,31)-1, 0});
void update(ll s, ll t, ll x, ll n, ll l, ll r, ll k){
// prllf("%d, %d, %d\n", l ,r, k);
if(r < s || t < l) {
// pr("c");
return;}
if(l==r) {seg[k] = {x, n};
// pr("a");
return;}
if(s<=l && r<=t) {seg[k] = {x, n};
// pr("b");
return;}
ll mid = (l+r)/2;
ll chl = 2*k+1, chr = 2*k+2;
update(s, t, x, n, l, mid, chl);
update(s, t, x, n, mid+1, r, chr);
}
ll find(ll i, P p){
if(seg[i].second > p.second) p = seg[i];
if(i==0) return p.first;
return find((i-1)/2, p);
}
int main()
{
ll n, q; cin >> n >> q;
ll c, s, t, x;
rep(i, q){
cin >> c;
if(c==1){
cin >> x;
ll k = find(LEAF + x, {0, -1});
pr(k);
}else{
cin >> s >> t >> x;
update(s, t, x, i+1, 0, LEAF, 0);
}
}
return 0;}
``` |
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996)
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;
const long long INF2 = (((ll)1<<31)-1);
int main(int argc, char* argv[])
{
int n,q;
scanf("%d%d", &n, &q);
vector<pair<int, pair<int, pair<int,int> > > > z(q); // type, s, t, x;
int i;
int cnt=0;
for(i=0; i<q; i++) {
int type, s, t, x;
scanf("%d", &type);
if(type==0) {
scanf("%d%d%d", &s, &t, &x);
z[i]=make_pair(type, make_pair(s, make_pair(t,x)));
}
else {
scanf("%d", &s);
z[i]=make_pair(type, make_pair(s, make_pair(0,0)));
cnt++;
}
}
int ansnum=cnt;
set<pair<int,int> > zz; // val, ansid
vector<int> ans(ansnum, INF2);
cnt=ansnum-1;
for(i=q-1; i>=0; i--) {
if(z[i].first==0) {
int s=z[i].second.first;
int t=z[i].second.second.first;
int x=z[i].second.second.second;
auto it=zz.lower_bound(make_pair(s,-INF));
auto itend=zz.lower_bound(make_pair(t+1,-INF));
while(it!=itend) {
ans[it->second]=x;
auto itnext=it; itnext++;
zz.erase(*it);
it=itnext;
}
}
else {
zz.insert(make_pair(z[i].second.first, cnt));
cnt--;
}
}
for(i=0; i<ansnum; i++) {
printf("%d\n", ans[i]);
}
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 <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996)
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;
const long long INF2 = (((ll)1<<31)-1);
int main(int argc, char* argv[])
{
int n,q;
scanf("%d%d", &n, &q);
vector<pair<int, pair<int, pair<int,int> > > > z(q); // type, s, t, x;
int i;
int cnt=0;
for(i=0; i<q; i++) {
int type, s, t, x;
scanf("%d", &type);
if(type==0) {
scanf("%d%d%d", &s, &t, &x);
z[i]=make_pair(type, make_pair(s, make_pair(t,x)));
}
else {
scanf("%d", &s);
z[i]=make_pair(type, make_pair(s, make_pair(0,0)));
cnt++;
}
}
int ansnum=cnt;
set<pair<int,int> > zz; // val, ansid
vector<int> ans(ansnum, INF2);
cnt=ansnum-1;
for(i=q-1; i>=0; i--) {
if(z[i].first==0) {
int s=z[i].second.first;
int t=z[i].second.second.first;
int x=z[i].second.second.second;
auto it=zz.lower_bound(make_pair(s,-INF));
auto itend=zz.lower_bound(make_pair(t+1,-INF));
while(it!=itend) {
ans[it->second]=x;
auto itnext=it; itnext++;
zz.erase(*it);
it=itnext;
}
}
else {
zz.insert(make_pair(z[i].second.first, cnt));
cnt--;
}
}
for(i=0; i<ansnum; i++) {
printf("%d\n", ans[i]);
}
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){
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
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 <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){
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>
#define int long long
using namespace std;
typedef pair<int,int> P;
const int INF=pow(2,31)-1;
std::vector<P> v(1e6,P(INF,-1));
int N=1;
void update(int a,int b,int c,int d,int k,int l,int r){
if(r<=a||b<=l)return;
if(a<=l&&r<=b)v[k]=P(c,d);
else{
update(a,b,c,d,k*2+1,l,(l+r)/2);
update(a,b,c,d,k*2+2,(l+r)/2,r);
}
}
int ma(int a){
int k=a+N-1;
P res=v[k];
while(k){
k=(k-1)/2;
if(res.second<v[k].second)res=v[k];
}
return res.first;
}
signed main(){
int n,M;
cin>>n>>M;
while(n>N)N*=2;
std::vector<int> ans;
for(int i=0;i<M;i++){
int q;cin>>q;
if(q){
int a;cin>>a;
ans.push_back(ma(a));
}
else{
int s,t,x;cin>>s>>t>>x;
update(s,t+1,x,i,0,0,N);
}
}
for(int p:ans)cout<<p<<endl;
}
| ### 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>
#define int long long
using namespace std;
typedef pair<int,int> P;
const int INF=pow(2,31)-1;
std::vector<P> v(1e6,P(INF,-1));
int N=1;
void update(int a,int b,int c,int d,int k,int l,int r){
if(r<=a||b<=l)return;
if(a<=l&&r<=b)v[k]=P(c,d);
else{
update(a,b,c,d,k*2+1,l,(l+r)/2);
update(a,b,c,d,k*2+2,(l+r)/2,r);
}
}
int ma(int a){
int k=a+N-1;
P res=v[k];
while(k){
k=(k-1)/2;
if(res.second<v[k].second)res=v[k];
}
return res.first;
}
signed main(){
int n,M;
cin>>n>>M;
while(n>N)N*=2;
std::vector<int> ans;
for(int i=0;i<M;i++){
int q;cin>>q;
if(q){
int a;cin>>a;
ans.push_back(ma(a));
}
else{
int s,t,x;cin>>s>>t>>x;
update(s,t+1,x,i,0,0,N);
}
}
for(int p:ans)cout<<p<<endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,l,r) for(int i = (int) (l);i < (int) (r);i++)
#define ALL(x) x.begin(),x.end()
template<typename T> bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; }
template<typename T> bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; }
typedef long long ll;
const int BUCKET = 400,SIZE = 300;
int N,Q;
bool updateFlag [BUCKET];
ll lazyUpdate [BUCKET];
ll A [100000];
const ll INF = (1ll << 31) - 1;
void update(int l,int r,ll val)
{
FOR(i,0,BUCKET){
int bucketL = i * SIZE,bucketR = (i + 1) * SIZE;
if(r <= bucketL || bucketR <= l) continue;
if(l <= bucketL && bucketR <= r){
lazyUpdate [i] = val;
updateFlag [i] = true;
continue;
}
if(updateFlag [i]){
FOR(j,bucketL,bucketR){
A [j] = lazyUpdate [i];
}
updateFlag [i] = false;
}
FOR(j,bucketL,bucketR){
if(l <= j && j < r){
A [j] = val;
}
}
}
}
ll get(int idx)
{
if(updateFlag [idx / SIZE]){
int bucketL = (idx / SIZE) * SIZE,bucketR = (idx / SIZE + 1) * SIZE;
FOR(i,bucketL,bucketR){
A [i] = lazyUpdate [idx / SIZE];
}
updateFlag [idx / SIZE] = false;
}
return A [idx];
}
int main()
{
scanf("%d%d",&N,&Q);
fill(A,A + N,INF);
vector<ll> ans;
FOR(i,0,Q){
int com;
scanf("%d",&com);
if(com == 0){
int s,t,x;
scanf("%d%d%d",&s,&t,&x);
update(s,t + 1,x);
}
else{
int idx;
scanf("%d",&idx);
ans.push_back(get(idx));
}
}
FOR(i,0,ans.size()){
printf("%lld\n",ans [i]);
}
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;
#define FOR(i,l,r) for(int i = (int) (l);i < (int) (r);i++)
#define ALL(x) x.begin(),x.end()
template<typename T> bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; }
template<typename T> bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; }
typedef long long ll;
const int BUCKET = 400,SIZE = 300;
int N,Q;
bool updateFlag [BUCKET];
ll lazyUpdate [BUCKET];
ll A [100000];
const ll INF = (1ll << 31) - 1;
void update(int l,int r,ll val)
{
FOR(i,0,BUCKET){
int bucketL = i * SIZE,bucketR = (i + 1) * SIZE;
if(r <= bucketL || bucketR <= l) continue;
if(l <= bucketL && bucketR <= r){
lazyUpdate [i] = val;
updateFlag [i] = true;
continue;
}
if(updateFlag [i]){
FOR(j,bucketL,bucketR){
A [j] = lazyUpdate [i];
}
updateFlag [i] = false;
}
FOR(j,bucketL,bucketR){
if(l <= j && j < r){
A [j] = val;
}
}
}
}
ll get(int idx)
{
if(updateFlag [idx / SIZE]){
int bucketL = (idx / SIZE) * SIZE,bucketR = (idx / SIZE + 1) * SIZE;
FOR(i,bucketL,bucketR){
A [i] = lazyUpdate [idx / SIZE];
}
updateFlag [idx / SIZE] = false;
}
return A [idx];
}
int main()
{
scanf("%d%d",&N,&Q);
fill(A,A + N,INF);
vector<ll> ans;
FOR(i,0,Q){
int com;
scanf("%d",&com);
if(com == 0){
int s,t,x;
scanf("%d%d%d",&s,&t,&x);
update(s,t + 1,x);
}
else{
int idx;
scanf("%d",&idx);
ans.push_back(get(idx));
}
}
FOR(i,0,ans.size()){
printf("%lld\n",ans [i]);
}
return 0;
}
``` |
#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;
const vector<T1> data;
vector<T2> lazy;
void push(int node) {
if (lazy[node] == M::id2()) return;
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;
const vector<T1> data;
vector<T2> lazy;
void push(int node) {
if (lazy[node] == M::id2()) return;
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 <bits/stdc++.h>
using namespace std;
const int sqrtN = 512;
struct SqrtDecomposition {
int N, K;
vector<int> data;
vector<bool> lazyFlag;
vector<int> lazyUpdate;
explicit SqrtDecomposition(const vector<int> &v) : N(v.size()) {
K = (N + sqrtN - 1) / sqrtN;
data.assign(K * sqrtN, 0);
for (int i = 0; i < v.size(); ++i) {
data[i] = v[i];
}
lazyFlag.assign(K, false);
lazyUpdate.assign(K, 0);
}
// [s, t) = x
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];
}
private:
void eval(int k) {
if (lazyFlag[k]) {
lazyFlag[k] = false;
for (int i = k * sqrtN; i < (k + 1) * sqrtN; ++i) {
data[i] = lazyUpdate[k];
}
}
}
};
int main() {
ios::sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
vector<int> v(N, (1LL << 31) - 1);
SqrtDecomposition sd(v);
while (Q--) {
int c;
cin >> c;
if (c == 0) {
int s, t, x;
cin >> s >> t >> x;
sd.update(s, t + 1, x);
} else {
int i;
cin >> i;
cout << sd.find(i) << 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 <bits/stdc++.h>
using namespace std;
const int sqrtN = 512;
struct SqrtDecomposition {
int N, K;
vector<int> data;
vector<bool> lazyFlag;
vector<int> lazyUpdate;
explicit SqrtDecomposition(const vector<int> &v) : N(v.size()) {
K = (N + sqrtN - 1) / sqrtN;
data.assign(K * sqrtN, 0);
for (int i = 0; i < v.size(); ++i) {
data[i] = v[i];
}
lazyFlag.assign(K, false);
lazyUpdate.assign(K, 0);
}
// [s, t) = x
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];
}
private:
void eval(int k) {
if (lazyFlag[k]) {
lazyFlag[k] = false;
for (int i = k * sqrtN; i < (k + 1) * sqrtN; ++i) {
data[i] = lazyUpdate[k];
}
}
}
};
int main() {
ios::sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
vector<int> v(N, (1LL << 31) - 1);
SqrtDecomposition sd(v);
while (Q--) {
int c;
cin >> c;
if (c == 0) {
int s, t, x;
cin >> s >> t >> x;
sd.update(s, t + 1, x);
} else {
int i;
cin >> i;
cout << sd.find(i) << endl;
}
}
return 0;
}
``` |
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;
template<class T>
class sg_tree : public vector<T>{
private:
lli n;
T default_value;
void _query(lli a,lli b,T x,lli k,lli l,lli r){
if(r <= a || b <= l) return;
if(a <= l && r <= b) (*this)[k] = x;
else {
_query(a,b,x,k*2+1,l,(l+r)/2);
_query(a,b,x,k*2+2,(l+r)/2,r);
return;
}
}
public:
sg_tree():vector<T>(){
}
sg_tree(lli n,T d = -1):vector<T>(n*4,d){
this->n = 1;
while(n > this->n) this->n <<= 1;
this->default_value = d;
}
T update(lli i){
i += n - 1;
T ret = (*this)[i];
while(i > 0){
i = (i - 1) / 2;
ret = max(ret,(*this)[i]);
}
return ret;
}
void query(lli a,lli b,lli x){
_query(a,b,x,0,0,n);
}
T func(T a,T b){
return min(a,b);
}
};
lli n,q;
lli c,s,t,x;
lli k = 0;
vll b;
sg_tree<lli> sg;
int main(){
cin >> n >> q;
sg = sg_tree<lli> (n);
for(lli i = 0;i < q;i++){
cin >> c;
if(c){
cin >> x;
lli t;
if((t = sg.update(x)) < 0) cout << 2147483647 << endl;
else cout << b[t] << endl;
}else{
cin >> s >> t >> x;
b.push_back(x);
sg.query(s,t+1,k);
k++;
}
}
} | ### 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 <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;
template<class T>
class sg_tree : public vector<T>{
private:
lli n;
T default_value;
void _query(lli a,lli b,T x,lli k,lli l,lli r){
if(r <= a || b <= l) return;
if(a <= l && r <= b) (*this)[k] = x;
else {
_query(a,b,x,k*2+1,l,(l+r)/2);
_query(a,b,x,k*2+2,(l+r)/2,r);
return;
}
}
public:
sg_tree():vector<T>(){
}
sg_tree(lli n,T d = -1):vector<T>(n*4,d){
this->n = 1;
while(n > this->n) this->n <<= 1;
this->default_value = d;
}
T update(lli i){
i += n - 1;
T ret = (*this)[i];
while(i > 0){
i = (i - 1) / 2;
ret = max(ret,(*this)[i]);
}
return ret;
}
void query(lli a,lli b,lli x){
_query(a,b,x,0,0,n);
}
T func(T a,T b){
return min(a,b);
}
};
lli n,q;
lli c,s,t,x;
lli k = 0;
vll b;
sg_tree<lli> sg;
int main(){
cin >> n >> q;
sg = sg_tree<lli> (n);
for(lli i = 0;i < q;i++){
cin >> c;
if(c){
cin >> x;
lli t;
if((t = sg.update(x)) < 0) cout << 2147483647 << endl;
else cout << b[t] << endl;
}else{
cin >> s >> t >> x;
b.push_back(x);
sg.query(s,t+1,k);
k++;
}
}
}
``` |
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;
#define NUM 2147483647
int N = 1;
int* data;
void init(int first_N){
while(N < first_N)N *= 2;
}
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){
data[node_id] = new_value;
}else{
if(data[node_id] >= 0){
data[2*node_id+1] = data[node_id];
data[2*node_id+2] = data[node_id];
data[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);
}
}
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 && data[node_id] >= 0){
return data[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);
}
}
int main(){
int first_N,Q;
scanf("%d %d",&first_N,&Q);
init(first_N);
data = new int[270000];
for(int i = 0; i <= 2*N-2; i++)data[i] = NUM;
int command,left,right,value,loc;
for(int i = 0; i < Q; i++){
scanf("%d",&command);
if(command == 0){
scanf("%d %d %d",&left,&right,&value);
update(left,right,value,0,0,N-1);
}else{
scanf("%d",&loc);
printf("%d\n",query(loc,loc,0,0,N-1));
}
}
} | ### 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 <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;
#define NUM 2147483647
int N = 1;
int* data;
void init(int first_N){
while(N < first_N)N *= 2;
}
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){
data[node_id] = new_value;
}else{
if(data[node_id] >= 0){
data[2*node_id+1] = data[node_id];
data[2*node_id+2] = data[node_id];
data[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);
}
}
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 && data[node_id] >= 0){
return data[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);
}
}
int main(){
int first_N,Q;
scanf("%d %d",&first_N,&Q);
init(first_N);
data = new int[270000];
for(int i = 0; i <= 2*N-2; i++)data[i] = NUM;
int command,left,right,value,loc;
for(int i = 0; i < Q; i++){
scanf("%d",&command);
if(command == 0){
scanf("%d %d %d",&left,&right,&value);
update(left,right,value,0,0,N-1);
}else{
scanf("%d",&loc);
printf("%d\n",query(loc,loc,0,0,N-1));
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template <class Z> Z getint() {
char c = getchar();
bool neg = c == '-';
Z res = neg ? 0 : c - '0';
while (isdigit(c = getchar())) res = res * 10 + (c - '0');
return neg ? -res : res;
}
template <class Z> void putint(Z a, char c = '\n') {
if (a < 0) putchar('-'), a = -a;
int d[40], i = 0;
do d[i++] = a % 10; while (a /= 10);
while (i--) putchar('0' + d[i]);
putchar(c);
}
template <class T> struct assign_segtree {
const int n;
vector<pair<int, T>> t;
assign_segtree(int _n, T a) : n(_n), t(2 * n, {0, a}) {}
void assign(int l, int r, T a) {
static int time = 0;
++time;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) t[l++] = {time, a};
if (r & 1) t[--r] = {time, a};
}
}
T get(int i) const {
auto res = t[i += n];
while (i >>= 1) if (t[i].first > res.first) res = t[i];
return res.second;
}
};
int main() {
int n = getint<int>();
int q = getint<int>();
assign_segtree<int> st(n, 0x7fffffff);
while (q--) {
if (getint<int>() == 0) {
int l = getint<int>();
int r = getint<int>() + 1;
int a = getint<int>();
st.assign(l, r, a);
} else {
putint(st.get(getint<int>()));
}
}
}
| ### 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;
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template <class Z> Z getint() {
char c = getchar();
bool neg = c == '-';
Z res = neg ? 0 : c - '0';
while (isdigit(c = getchar())) res = res * 10 + (c - '0');
return neg ? -res : res;
}
template <class Z> void putint(Z a, char c = '\n') {
if (a < 0) putchar('-'), a = -a;
int d[40], i = 0;
do d[i++] = a % 10; while (a /= 10);
while (i--) putchar('0' + d[i]);
putchar(c);
}
template <class T> struct assign_segtree {
const int n;
vector<pair<int, T>> t;
assign_segtree(int _n, T a) : n(_n), t(2 * n, {0, a}) {}
void assign(int l, int r, T a) {
static int time = 0;
++time;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) t[l++] = {time, a};
if (r & 1) t[--r] = {time, a};
}
}
T get(int i) const {
auto res = t[i += n];
while (i >>= 1) if (t[i].first > res.first) res = t[i];
return res.second;
}
};
int main() {
int n = getint<int>();
int q = getint<int>();
assign_segtree<int> st(n, 0x7fffffff);
while (q--) {
if (getint<int>() == 0) {
int l = getint<int>();
int r = getint<int>() + 1;
int a = getint<int>();
st.assign(l, r, a);
} else {
putint(st.get(getint<int>()));
}
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define int long long
//BEGIN CUT HERE
struct RUP{
int n;
vector<int> dat,laz;
const int def=INT_MAX;
RUP(){}
RUP(int n_){init(n_);}
void init(int n_){
n=1;
while(n<n_) n*=2;
dat.clear();
dat.resize(2*n-1,def);
laz.clear();
laz.resize(2*n-1,-1);
}
inline void eval(int len,int k){
if(laz[k]<0) return;
if(k*2+1<n*2-1){
laz[k*2+1]=laz[k];
laz[k*2+2]=laz[k];
}
dat[k]=laz[k];
laz[k]=-1;
}
void update(int a,int b,int x,int k,int l,int r){
eval(r-l,k);
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
laz[k]=x;
return;
}
eval(r-l,k);
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
int query(int a,int b,int k,int l,int r){
eval(r-l,k);
if(r<=a||b<=l) return def;
if(a<=l&&r<=b) return dat[k];
int vl=query(a,b,k*2+1,l,(l+r)/2);
int vr=query(a,b,k*2+2,(l+r)/2,r);
return min(vl,vr);
}
void update(int a,int b,int x){
update(a,b,x,0,0,n);
}
int query(int a){
return query(a,a+1,0,0,n);
}
};
//END CUT HERE
signed main(){
int n,q;
cin>>n>>q;
RUP rup(n);
for(int i=0;i<q;i++){
int f;
cin>>f;
if(!f){
int s,t,x;
cin>>s>>t>>x;
rup.update(s,t+1,x);
}else{
int u;
cin>>u;
cout<<rup.query(u)<<endl;
}
}
return 0;
}
/*
verified on 2017/07/12
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_D
*/ | ### 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;
#define int long long
//BEGIN CUT HERE
struct RUP{
int n;
vector<int> dat,laz;
const int def=INT_MAX;
RUP(){}
RUP(int n_){init(n_);}
void init(int n_){
n=1;
while(n<n_) n*=2;
dat.clear();
dat.resize(2*n-1,def);
laz.clear();
laz.resize(2*n-1,-1);
}
inline void eval(int len,int k){
if(laz[k]<0) return;
if(k*2+1<n*2-1){
laz[k*2+1]=laz[k];
laz[k*2+2]=laz[k];
}
dat[k]=laz[k];
laz[k]=-1;
}
void update(int a,int b,int x,int k,int l,int r){
eval(r-l,k);
if(r<=a||b<=l) return;
if(a<=l&&r<=b){
laz[k]=x;
return;
}
eval(r-l,k);
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
int query(int a,int b,int k,int l,int r){
eval(r-l,k);
if(r<=a||b<=l) return def;
if(a<=l&&r<=b) return dat[k];
int vl=query(a,b,k*2+1,l,(l+r)/2);
int vr=query(a,b,k*2+2,(l+r)/2,r);
return min(vl,vr);
}
void update(int a,int b,int x){
update(a,b,x,0,0,n);
}
int query(int a){
return query(a,a+1,0,0,n);
}
};
//END CUT HERE
signed main(){
int n,q;
cin>>n>>q;
RUP rup(n);
for(int i=0;i<q;i++){
int f;
cin>>f;
if(!f){
int s,t,x;
cin>>s>>t>>x;
rup.update(s,t+1,x);
}else{
int u;
cin>>u;
cout<<rup.query(u)<<endl;
}
}
return 0;
}
/*
verified on 2017/07/12
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_D
*/
``` |
#include <bits/stdc++.h>
using namespace std;
/*RMQ update:O(logN) query:O(lonN)*/
const int MAX_N = 1<<17;
int n,dat[2*MAX_N-1];
//?????????
void init(int n_){
//????´???°n???2???????????????
n=1;
while(n<n_)n*=2;
for(int i=0;i<2*n-1;i++)dat[i]=INT_MAX;
}
//[a,b)???????°????????±???????
//query(a,b,0,0,n)
void update(int a,int b,int x=-1,int k=0,int l=0,int r=n){
if(r<=a||b<=l)return ;
if(a<=l&&r<=b){
if(x>=0)dat[k]=x;
return;
}
if(dat[k]!=INT_MAX)dat[k*2+1]=dat[k*2+2]=dat[k];
dat[k]=INT_MAX;
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
int main(){
int q;
cin>>n>>q;
init(n);
while(q--){
int cmd;
cin>>cmd;
if(cmd==0){
int s,t,x;
cin>>s>>t>>x;
update(s,t+1,x);
}
else {
int idx;
cin>>idx;
update(idx,idx+1);
cout <<dat[idx+n-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;
/*RMQ update:O(logN) query:O(lonN)*/
const int MAX_N = 1<<17;
int n,dat[2*MAX_N-1];
//?????????
void init(int n_){
//????´???°n???2???????????????
n=1;
while(n<n_)n*=2;
for(int i=0;i<2*n-1;i++)dat[i]=INT_MAX;
}
//[a,b)???????°????????±???????
//query(a,b,0,0,n)
void update(int a,int b,int x=-1,int k=0,int l=0,int r=n){
if(r<=a||b<=l)return ;
if(a<=l&&r<=b){
if(x>=0)dat[k]=x;
return;
}
if(dat[k]!=INT_MAX)dat[k*2+1]=dat[k*2+2]=dat[k];
dat[k]=INT_MAX;
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
}
int main(){
int q;
cin>>n>>q;
init(n);
while(q--){
int cmd;
cin>>cmd;
if(cmd==0){
int s,t,x;
cin>>s>>t>>x;
update(s,t+1,x);
}
else {
int idx;
cin>>idx;
update(idx,idx+1);
cout <<dat[idx+n-1]<<endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;
typedef pair<int,int>P;
const int N = 1 << 17;
//?????????????????????0?§???????
//????°????????±???????????????°??????(RMQ)
//?????°???????????¨???????????°??????????????????
int n,n_,q,ud[2*N-1],t[2*N-1];
//?????????
//n_???x??\??????????°????2????????????
void init(int x)
{
//?°????????????????????´???°???2???????????????
n_=1;
while(n_<x){
n_*=2;
}
rep(i,2*n_-1){
ud[i] = (int)((1LL<<31) -1LL);
t[i] = -1;
}
}
//k????????????????????§??????
int find(int k)
{
P res;
//????????????
k += n_-1;
res.first = ud[k];
res.second = t[k];
//?????????????????´??°
while(k>0){
k = (k-1)/2;
if(t[k] > res.second){
res.first = ud[k];
res.second = t[k];
}
}
return res.first;
}
//[a,b)???????°????????±??????????
//k??????????????????
//????????????query(a,b,0,0,n_)??¨???????????¶???(n_??¨???????????¨?????¨???)
void update(int a,int b,int k,int l,int r,int val,int id)
{
if(r <= a || b <= l){
return;
}
if(a <= l && r <= b){
ud[k] = val;
t[k] = id;
}else{
update(a,b,2*k+1,l,(l+r)/2,val,id);
update(a,b,2*k+2,(l+r)/2,r,val,id);
}
}
int main()
{
scanf("%d%d",&n,&q);
init(n);
rep(i,q){
int x;
scanf("%d",&x);
if(x==0){
int s,t,u;
scanf("%d%d%d",&s,&t,&u);
update(s,t+1,0,0,n_,u,i);
}else{
int s;
scanf("%d",&s);
printf("%d\n",find(s));
}
}
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 <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;
typedef pair<int,int>P;
const int N = 1 << 17;
//?????????????????????0?§???????
//????°????????±???????????????°??????(RMQ)
//?????°???????????¨???????????°??????????????????
int n,n_,q,ud[2*N-1],t[2*N-1];
//?????????
//n_???x??\??????????°????2????????????
void init(int x)
{
//?°????????????????????´???°???2???????????????
n_=1;
while(n_<x){
n_*=2;
}
rep(i,2*n_-1){
ud[i] = (int)((1LL<<31) -1LL);
t[i] = -1;
}
}
//k????????????????????§??????
int find(int k)
{
P res;
//????????????
k += n_-1;
res.first = ud[k];
res.second = t[k];
//?????????????????´??°
while(k>0){
k = (k-1)/2;
if(t[k] > res.second){
res.first = ud[k];
res.second = t[k];
}
}
return res.first;
}
//[a,b)???????°????????±??????????
//k??????????????????
//????????????query(a,b,0,0,n_)??¨???????????¶???(n_??¨???????????¨?????¨???)
void update(int a,int b,int k,int l,int r,int val,int id)
{
if(r <= a || b <= l){
return;
}
if(a <= l && r <= b){
ud[k] = val;
t[k] = id;
}else{
update(a,b,2*k+1,l,(l+r)/2,val,id);
update(a,b,2*k+2,(l+r)/2,r,val,id);
}
}
int main()
{
scanf("%d%d",&n,&q);
init(n);
rep(i,q){
int x;
scanf("%d",&x);
if(x==0){
int s,t,u;
scanf("%d%d%d",&s,&t,&u);
update(s,t+1,0,0,n_,u,i);
}else{
int s;
scanf("%d",&s);
printf("%d\n",find(s));
}
}
return 0;
}
``` |
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = (1ll << 31) - 1;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
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; }
template<typename T>
struct RangeUpdateQuery {
int n;
vector<int>d;
RangeUpdateQuery(int m) {
for (n = 1; n < m; n <<= 1);
d.assign(2 * n, INF);
}
int find(int i) {
update(i, i + 1);
return d[n + i];
}
void update(int a, int b, int x = -1, int k = 1, int l = 0, int r = -1) {
if (r == -1)r = n;
if (r <= a || b <= l)return;
else if (a <= l&&r <= b) {
if (x >= 0)d[k] = x;
return;
}
else {
if (d[k] != INF)d[2 * k] = d[2 * k + 1] = d[k], d[k] = INF;
update(a, b, x, 2 * k, l, (l + r) / 2);
update(a, b, x, 2 * k + 1, (l + r) / 2, r);
}
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q; cin >> n >> q;
RangeUpdateQuery<int> ruq(n);
rep(i, 0, q) {
int com; cin >> com;
if (com) {
int x; cin >> x;
cout << ruq.find(x) << endl;
}
else {
int s, t, x; cin >> s >> t >> x;
ruq.update(s, t + 1, x);
}
}
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 "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = (1ll << 31) - 1;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
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; }
template<typename T>
struct RangeUpdateQuery {
int n;
vector<int>d;
RangeUpdateQuery(int m) {
for (n = 1; n < m; n <<= 1);
d.assign(2 * n, INF);
}
int find(int i) {
update(i, i + 1);
return d[n + i];
}
void update(int a, int b, int x = -1, int k = 1, int l = 0, int r = -1) {
if (r == -1)r = n;
if (r <= a || b <= l)return;
else if (a <= l&&r <= b) {
if (x >= 0)d[k] = x;
return;
}
else {
if (d[k] != INF)d[2 * k] = d[2 * k + 1] = d[k], d[k] = INF;
update(a, b, x, 2 * k, l, (l + r) / 2);
update(a, b, x, 2 * k + 1, (l + r) / 2, r);
}
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q; cin >> n >> q;
RangeUpdateQuery<int> ruq(n);
rep(i, 0, q) {
int com; cin >> com;
if (com) {
int x; cin >> x;
cout << ruq.find(x) << endl;
}
else {
int s, t, x; cin >> s >> t >> x;
ruq.update(s, t + 1, x);
}
}
return 0;
}
``` |
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <tuple>
#include <cstring>
#include <map>
#include <iomanip>
#include <ctime>
#include <complex>
#include <cassert>
#include <climits>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define _ << " " <<
#define all(X) (X).begin(), (X).end()
#define len(X) (X).size()
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define Tiii tuple<int, int, int>
#define Tlll tuple<ll, ll, ll>
class SegTree_Delay {
public:
int f;
int INIT = INT_MAX;
vector<int> dat, lazy;
SegTree_Delay(int n) {
f = 1;
while (f < n) f *= 2;
dat.assign(2 * f, INIT);
lazy.assign(2 * f, INIT);
}
void eval(int k) {
if (lazy[k] == INIT) return;
if (k < f - 1) {
lazy[2 * k + 1] = lazy[k];
lazy[2 * k + 2] = lazy[k];
}
dat[k] = lazy[k];
lazy[k] = INIT;
}
void update(int a, int b, int x, int k, int l, int r) {
eval(k);
if (a <= l && r <= b) {
lazy[k] = x;
eval(k);
}
else if (a < r && l < b) {
update(a, b, x, k * 2 + 1, l, (l + r) / 2);
update(a, b, x, k * 2 + 2, (l + r) / 2, r);
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
void update(int a, int b, int x) {
update(a, b, x, 0, 0, f);
}
int query(int a, int b) {
return query(a, b, 0, 0, f);
}
int query(int a, int b, int k, int l, int r) {
eval(k);
if (r <= a || b <= l) return INIT;
if (a <= l && r <= b) return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
};
int main() {
int n, q;
cin >> n >> q;
SegTree_Delay st = SegTree_Delay(n);
while (q--) {
int m;
cin >> m;
if (m) {
int i; cin >> i;
cout << st.query(i, i + 1) << endl;
}
else {
int s, t, x;
cin >> s >> t >> x;
st.update(s, t + 1, x);
}
}
}
| ### 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 <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <tuple>
#include <cstring>
#include <map>
#include <iomanip>
#include <ctime>
#include <complex>
#include <cassert>
#include <climits>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define _ << " " <<
#define all(X) (X).begin(), (X).end()
#define len(X) (X).size()
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define Tiii tuple<int, int, int>
#define Tlll tuple<ll, ll, ll>
class SegTree_Delay {
public:
int f;
int INIT = INT_MAX;
vector<int> dat, lazy;
SegTree_Delay(int n) {
f = 1;
while (f < n) f *= 2;
dat.assign(2 * f, INIT);
lazy.assign(2 * f, INIT);
}
void eval(int k) {
if (lazy[k] == INIT) return;
if (k < f - 1) {
lazy[2 * k + 1] = lazy[k];
lazy[2 * k + 2] = lazy[k];
}
dat[k] = lazy[k];
lazy[k] = INIT;
}
void update(int a, int b, int x, int k, int l, int r) {
eval(k);
if (a <= l && r <= b) {
lazy[k] = x;
eval(k);
}
else if (a < r && l < b) {
update(a, b, x, k * 2 + 1, l, (l + r) / 2);
update(a, b, x, k * 2 + 2, (l + r) / 2, r);
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
void update(int a, int b, int x) {
update(a, b, x, 0, 0, f);
}
int query(int a, int b) {
return query(a, b, 0, 0, f);
}
int query(int a, int b, int k, int l, int r) {
eval(k);
if (r <= a || b <= l) return INIT;
if (a <= l && r <= b) return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
};
int main() {
int n, q;
cin >> n >> q;
SegTree_Delay st = SegTree_Delay(n);
while (q--) {
int m;
cin >> m;
if (m) {
int i; cin >> i;
cout << st.query(i, i + 1) << endl;
}
else {
int s, t, x;
cin >> s >> t >> x;
st.update(s, t + 1, x);
}
}
}
``` |
#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){
set(l, r, x, 0, n, 1);
}
void set(int l, int r, int x, int segl, int segr, int n){
push(segl, segr, n);
if(segr <= l || r <= segl);
else if(l <= segl && segr <= r) lazy[n] = x;
else {
int segm = (segl + segr) / 2;
set(l, r, x, segl, segm, n*2);
set(l, r, x, 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
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;
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){
set(l, r, x, 0, n, 1);
}
void set(int l, int r, int x, int segl, int segr, int n){
push(segl, segr, n);
if(segr <= l || r <= segl);
else if(l <= segl && segr <= r) lazy[n] = x;
else {
int segm = (segl + segr) / 2;
set(l, r, x, segl, segm, n*2);
set(l, r, x, 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 <iostream>
#include <vector>
using namespace std;
const int INF = 2147483647;
struct SegmentTree {
int n;
vector<int> heap;
vector<bool> isLazy;
SegmentTree(int n): n(n) {
heap.assign(4 * n, INF);
isLazy.assign(4 * n, true);
}
void update(int nodeId, int l, int r, int ql, int qr, int v) {
if (qr < l || ql > r) {
return;
}
else if (ql <= l && r <= qr) {
heap[nodeId] = v;
isLazy[nodeId] = true;
}
else {
pushdown(nodeId);
int m = l + (r - l)/2;
update(nodeId * 2, l, m, ql, qr, v);
update(nodeId * 2 + 1, m + 1, r, ql, qr, v);
}
}
int query(int nodeId, int l, int r, int q) {
if (q < l || q > r) {
return -INF;
}
else if (isLazy[nodeId]) {
return heap[nodeId];
}
else {
int m = l + (r - l)/2;
return max(query(nodeId * 2, l, m, q), query(nodeId * 2 + 1, m + 1, r, q));
}
}
void pushdown(int nodeId) {
//cout << "push: " << nodeId << endl;
if (isLazy[nodeId]) {
heap[nodeId * 2] = heap[nodeId];
isLazy[nodeId * 2] = true;
heap[nodeId * 2 + 1] = heap[nodeId];
isLazy[nodeId * 2 + 1] = true;
heap[nodeId] = -INF;
isLazy[nodeId] = false;
}
}
};
int main() {
int n, q;
cin >> n >> q;
SegmentTree st(n);
for (int i = 0; i < q; i++) {
int cmd;
cin >> cmd;
if (cmd == 0) {
int ql, qr, v;
cin >> ql >> qr >> v;
st.update(1, 0, n -1, ql, qr, v);
// for (int i = 0; i < 4 * n; i++) {
// cout << i << ":" << st.heap[i] << endl;
// }
}
else {
int pos;
cin >> pos;
cout << st.query(1, 0, n-1, pos) << 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 <iostream>
#include <vector>
using namespace std;
const int INF = 2147483647;
struct SegmentTree {
int n;
vector<int> heap;
vector<bool> isLazy;
SegmentTree(int n): n(n) {
heap.assign(4 * n, INF);
isLazy.assign(4 * n, true);
}
void update(int nodeId, int l, int r, int ql, int qr, int v) {
if (qr < l || ql > r) {
return;
}
else if (ql <= l && r <= qr) {
heap[nodeId] = v;
isLazy[nodeId] = true;
}
else {
pushdown(nodeId);
int m = l + (r - l)/2;
update(nodeId * 2, l, m, ql, qr, v);
update(nodeId * 2 + 1, m + 1, r, ql, qr, v);
}
}
int query(int nodeId, int l, int r, int q) {
if (q < l || q > r) {
return -INF;
}
else if (isLazy[nodeId]) {
return heap[nodeId];
}
else {
int m = l + (r - l)/2;
return max(query(nodeId * 2, l, m, q), query(nodeId * 2 + 1, m + 1, r, q));
}
}
void pushdown(int nodeId) {
//cout << "push: " << nodeId << endl;
if (isLazy[nodeId]) {
heap[nodeId * 2] = heap[nodeId];
isLazy[nodeId * 2] = true;
heap[nodeId * 2 + 1] = heap[nodeId];
isLazy[nodeId * 2 + 1] = true;
heap[nodeId] = -INF;
isLazy[nodeId] = false;
}
}
};
int main() {
int n, q;
cin >> n >> q;
SegmentTree st(n);
for (int i = 0; i < q; i++) {
int cmd;
cin >> cmd;
if (cmd == 0) {
int ql, qr, v;
cin >> ql >> qr >> v;
st.update(1, 0, n -1, ql, qr, v);
// for (int i = 0; i < 4 * n; i++) {
// cout << i << ":" << st.heap[i] << endl;
// }
}
else {
int pos;
cin >> pos;
cout << st.query(1, 0, n-1, pos) << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define dhoom ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
#define fi first
#define se second
#define sc scanf
#define pr printf
#define pb push_back
const ll inf = 1e18;
const int nax = 2e6 + 7;
ll mod;
int n , q;
using namespace std;
ll exp(ll a , ll b){
if(b == 0) return 1;
int x = b % 2 == 0 ? ((exp(a , b/2)%mod)*(exp(a , b / 2)%mod))%mod : (a%mod*exp(a , b - 1) % mod)%mod;
return x;
}
ll tree[nax];
ll arr[nax];
int marked[nax];
void build(ll tl , ll tr , ll pos){
if(tl == tr)
{
tree[pos] = arr[tl];
return ;
}
ll mid = tl + (tr - tl)/2;
build(tl , mid , 2 * pos);
build(mid + 1 , tr , 2 * pos + 1);
}
void update(ll l , ll r , ll val , ll tl , ll tr , ll pos){
if(l > r)
return ;
if(tl == l && tr == r){
tree[pos] = val;
marked[pos] = 1;
return ;
}
ll mid = tl + (tr - tl)/2;
if(marked[pos]){
tree[2 * pos] = tree[pos];
tree[2*pos + 1] = tree[pos];
marked[pos*2] = marked[pos*2 + 1] = 1;
marked[pos] = 0;
}
update(l , min(r , mid),val , tl , mid , 2 * pos );
update(max(l , mid + 1), r , val , mid + 1 , tr , 2 * pos + 1);
//tree[pos] = tree[2*pos] + tree[ 2* pos + 1];
}
ll query(ll ind, ll tl , ll tr , ll pos){
if(tl == tr)
return tree[pos];
ll mid = tl + (tr - tl)/2;
if(marked[pos]){
tree[2 * pos] = tree[pos];
tree[2*pos + 1] = tree[pos];
marked[pos*2] = marked[pos*2 + 1] = 1;
marked[pos] = 0;
}
if(ind <= mid)
return query(ind , tl , mid , 2*pos);
else
return query(ind , mid + 1 , tr , 2*pos + 1);
}
int main(int argc,char ** argv){
dhoom;
cin >> n >> q;
ll x = pow(2LL , 31LL) - 1;
// cout << "HEY" << endl;
//cout << x << endl;
for(int i = 1 ; i <= n ; i++)
arr[i] = x;
//cout << "building" << endl;
build(1 , n , 1);
//cout << " hi " << endl;
for(int i = 0 ; i < q ; i++){
ll a , b , c , d;
cin >> a;
if(a == 0){
cin >> b >> c >> d;
update(b+1 , c+1 , d , 1 , n , 1);
}
else
{
cin >> b;
cout << query(b + 1, 1 , n , 1) << 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 <bits/stdc++.h>
using namespace std;
#define dhoom ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
#define fi first
#define se second
#define sc scanf
#define pr printf
#define pb push_back
const ll inf = 1e18;
const int nax = 2e6 + 7;
ll mod;
int n , q;
using namespace std;
ll exp(ll a , ll b){
if(b == 0) return 1;
int x = b % 2 == 0 ? ((exp(a , b/2)%mod)*(exp(a , b / 2)%mod))%mod : (a%mod*exp(a , b - 1) % mod)%mod;
return x;
}
ll tree[nax];
ll arr[nax];
int marked[nax];
void build(ll tl , ll tr , ll pos){
if(tl == tr)
{
tree[pos] = arr[tl];
return ;
}
ll mid = tl + (tr - tl)/2;
build(tl , mid , 2 * pos);
build(mid + 1 , tr , 2 * pos + 1);
}
void update(ll l , ll r , ll val , ll tl , ll tr , ll pos){
if(l > r)
return ;
if(tl == l && tr == r){
tree[pos] = val;
marked[pos] = 1;
return ;
}
ll mid = tl + (tr - tl)/2;
if(marked[pos]){
tree[2 * pos] = tree[pos];
tree[2*pos + 1] = tree[pos];
marked[pos*2] = marked[pos*2 + 1] = 1;
marked[pos] = 0;
}
update(l , min(r , mid),val , tl , mid , 2 * pos );
update(max(l , mid + 1), r , val , mid + 1 , tr , 2 * pos + 1);
//tree[pos] = tree[2*pos] + tree[ 2* pos + 1];
}
ll query(ll ind, ll tl , ll tr , ll pos){
if(tl == tr)
return tree[pos];
ll mid = tl + (tr - tl)/2;
if(marked[pos]){
tree[2 * pos] = tree[pos];
tree[2*pos + 1] = tree[pos];
marked[pos*2] = marked[pos*2 + 1] = 1;
marked[pos] = 0;
}
if(ind <= mid)
return query(ind , tl , mid , 2*pos);
else
return query(ind , mid + 1 , tr , 2*pos + 1);
}
int main(int argc,char ** argv){
dhoom;
cin >> n >> q;
ll x = pow(2LL , 31LL) - 1;
// cout << "HEY" << endl;
//cout << x << endl;
for(int i = 1 ; i <= n ; i++)
arr[i] = x;
//cout << "building" << endl;
build(1 , n , 1);
//cout << " hi " << endl;
for(int i = 0 ; i < q ; i++){
ll a , b , c , d;
cin >> a;
if(a == 0){
cin >> b >> c >> d;
update(b+1 , c+1 , d , 1 , n , 1);
}
else
{
cin >> b;
cout << query(b + 1, 1 , n , 1) << endl;
}
}
return 0;
}
``` |
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 2147483647
typedef long long ll;
using namespace std;
#define N (1<<17)
class RUQ{
private:
int dat[2*N];
vector<int> hs;
void dfs(int l,int r,int a,int b,int k,int x){
if(l<=a&&b<=r){dat[k]=x;return;}
if(b<=l||r<=a)return;
int m=(a+b)/2;
dfs(l,r,a,m,k*2+1,x);
dfs(l,r,m,b,k*2+2,x);
}
public:
void Init(){
hs.push_back(mod);
lol(i,2*N)dat[i]=0;
}
void Update(int l,int r,int x){
dfs(l,r+1,0,N,0,hs.size());
hs.push_back(x);
}
int Query(int i){
i+=N-1;
int maxi=dat[i];
while(i){
i=(i-1)/2;
maxi=max(maxi,dat[i]);
}
return hs[maxi];
}
};
int main(){
int n,q;cin>>n>>q;
RUQ ruq;ruq.Init();
while(q--){
int c;cin>>c;
if(c==0){
int a,b,x;cin>>a>>b>>x;
ruq.Update(a,b,x);
}
if(c==1){
int x;cin>>x;
cout<<ruq.Query(x)<<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<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 2147483647
typedef long long ll;
using namespace std;
#define N (1<<17)
class RUQ{
private:
int dat[2*N];
vector<int> hs;
void dfs(int l,int r,int a,int b,int k,int x){
if(l<=a&&b<=r){dat[k]=x;return;}
if(b<=l||r<=a)return;
int m=(a+b)/2;
dfs(l,r,a,m,k*2+1,x);
dfs(l,r,m,b,k*2+2,x);
}
public:
void Init(){
hs.push_back(mod);
lol(i,2*N)dat[i]=0;
}
void Update(int l,int r,int x){
dfs(l,r+1,0,N,0,hs.size());
hs.push_back(x);
}
int Query(int i){
i+=N-1;
int maxi=dat[i];
while(i){
i=(i-1)/2;
maxi=max(maxi,dat[i]);
}
return hs[maxi];
}
};
int main(){
int n,q;cin>>n>>q;
RUQ ruq;ruq.Init();
while(q--){
int c;cin>>c;
if(c==0){
int a,b,x;cin>>a>>b>>x;
ruq.Update(a,b,x);
}
if(c==1){
int x;cin>>x;
cout<<ruq.Query(x)<<endl;
}
}
return 0;
}
``` |
#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 udata[MAX/B], data[MAX/B];
void update(int a, int b, int x){
int f1 = 1, f2 = 1;
while(a <= b && a%B != 0){
if(f1 && udata[a/B] != -1){
for(int i = 0;i < B;i++)bucket[a/B][i] = udata[a/B];
f1 = 0;udata[a/B] = -1;
}
bucket[a/B][a%B] = x;
a++;
}
while(a <= b && b%B != B-1){
if(f2 && udata[b/B] != -1){
for(int i = 0;i < B;i++)bucket[b/B][i] = udata[b/B];
f2 = 0;udata[b/B] = -1;
}
bucket[b/B][b%B] = x;
b--;
}
while(a < b){
udata[a/B] = x;
a += B;
}
}
int find(int x){
if(udata[x/B] != -1){
for(int i = 0;i < B;i++)bucket[x/B][i] = udata[x/B];
udata[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(udata, udata+MAX/B, -1);
fill(data, data+MAX/B, INF);
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 udata[MAX/B], data[MAX/B];
void update(int a, int b, int x){
int f1 = 1, f2 = 1;
while(a <= b && a%B != 0){
if(f1 && udata[a/B] != -1){
for(int i = 0;i < B;i++)bucket[a/B][i] = udata[a/B];
f1 = 0;udata[a/B] = -1;
}
bucket[a/B][a%B] = x;
a++;
}
while(a <= b && b%B != B-1){
if(f2 && udata[b/B] != -1){
for(int i = 0;i < B;i++)bucket[b/B][i] = udata[b/B];
f2 = 0;udata[b/B] = -1;
}
bucket[b/B][b%B] = x;
b--;
}
while(a < b){
udata[a/B] = x;
a += B;
}
}
int find(int x){
if(udata[x/B] != -1){
for(int i = 0;i < B;i++)bucket[x/B][i] = udata[x/B];
udata[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(udata, udata+MAX/B, -1);
fill(data, data+MAX/B, INF);
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 <bits/stdc++.h>
using namespace std;
typedef int64_t i64;
constexpr i64 llinf=4611686018427387903LL;
template <class T_>
class rupdq{
public:
i64 n;
i64 h;
T_ *lz;
void make(i64 N){
n=1;
h=0;
while(n<N){
n*=2;
h++;
}
lz=new i64[n*2]{};
for(i64 i=0;i<n*2;i++) lz[i]=(T_)llinf;
}
void update(i64 A,i64 B,T_ X){
thrust(A+=n);
thrust(B+=n-1);
for(i64 l=A,r=B+1;l<r;l/=2,r/=2){
if(l%2==1){
lz[l]=X;
l++;
}
if(r%2==1){
r--;
lz[r]=X;
}
}
}
T_ query(i64 K){
thrust(K+=n);
return lz[K];
}
void thrust(i64 K){
for(i64 i=h;i>0;i--){
i64 k=K>>i;
if(lz[k]==(T_)llinf) continue;
lz[k*2]=lz[k];
lz[k*2+1]=lz[k];
lz[k]=(T_)llinf;
}
}
};
int main(void){
i64 n,q;
scanf("%lli%lli",&n,&q);
rupdq<i64> r;
r.make(n);
for(i64 i=0;i<q;i++){
i64 c;
scanf("%lli",&c);
if(c==0){
i64 s,t,x;
scanf("%lli%lli%lli",&s,&t,&x);
r.update(s,t+1,x);
}
if(c==1){
i64 j;
scanf("%lli",&j);
j=r.query(j);
if(j==llinf) printf("2147483647\n");
else printf("%lli\n",j);
}
}
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;
typedef int64_t i64;
constexpr i64 llinf=4611686018427387903LL;
template <class T_>
class rupdq{
public:
i64 n;
i64 h;
T_ *lz;
void make(i64 N){
n=1;
h=0;
while(n<N){
n*=2;
h++;
}
lz=new i64[n*2]{};
for(i64 i=0;i<n*2;i++) lz[i]=(T_)llinf;
}
void update(i64 A,i64 B,T_ X){
thrust(A+=n);
thrust(B+=n-1);
for(i64 l=A,r=B+1;l<r;l/=2,r/=2){
if(l%2==1){
lz[l]=X;
l++;
}
if(r%2==1){
r--;
lz[r]=X;
}
}
}
T_ query(i64 K){
thrust(K+=n);
return lz[K];
}
void thrust(i64 K){
for(i64 i=h;i>0;i--){
i64 k=K>>i;
if(lz[k]==(T_)llinf) continue;
lz[k*2]=lz[k];
lz[k*2+1]=lz[k];
lz[k]=(T_)llinf;
}
}
};
int main(void){
i64 n,q;
scanf("%lli%lli",&n,&q);
rupdq<i64> r;
r.make(n);
for(i64 i=0;i<q;i++){
i64 c;
scanf("%lli",&c);
if(c==0){
i64 s,t,x;
scanf("%lli%lli%lli",&s,&t,&x);
r.update(s,t+1,x);
}
if(c==1){
i64 j;
scanf("%lli",&j);
j=r.query(j);
if(j==llinf) printf("2147483647\n");
else printf("%lli\n",j);
}
}
return 0;
}
``` |
#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 1000
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
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>
#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 1000
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>
#include<iomanip>
#include <cmath>
#include <climits>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <queue>
#include <tuple>
#include <map>
#include <list>
#include <string>
#include <numeric>
#include <utility>
#include <cfloat>
#include <set>
using namespace std;
int sqrtN = 512;
struct SqrtDecomposition{
int N, K;
vector <long long> data;
vector <long long> bucketUpdatedLazy;
SqrtDecomposition(int n){
N = n;
K = (N + sqrtN - 1) / sqrtN;
data.assign(N, ((long long) 1 << 31) - 1);
bucketUpdatedLazy.assign(K, -1);
}
void propagateLazy(int k){
if(bucketUpdatedLazy[k] < 0){
return;
}
for(int i = 0; i < sqrtN; i++){
data[k * sqrtN + i] = bucketUpdatedLazy[k];
}
bucketUpdatedLazy[k] = -1;
}
long long get(int x){
propagateLazy(x / sqrtN);
return data[x];
}
void update(int x, int y, int a){
if(y - x < sqrtN){
propagateLazy(x / sqrtN);
propagateLazy((y - 1) / sqrtN);
for(int i = x; i < y; i++){
data[i] = a;
}
return;
}
for(int i = x / sqrtN + 1; i < y / sqrtN; i++){
bucketUpdatedLazy[i] = a;
//if(bucketUpdatedLazy[i] < 0 && bucketUpdatedLazy[i] != -1){
// cerr << "Warning" << endl;
//}
}
propagateLazy(x / sqrtN);
for(int i = x; i < (x / sqrtN + 1) * sqrtN; i++){
data[i] = a;
}
propagateLazy((y - 1)/ sqrtN);
for(int i = y / sqrtN * sqrtN; i < y; i++){
data[i] = a;
}
}
};
int main(){
int n;
int q;
cin >> n >> q;
SqrtDecomposition sq(n);
for(int i = 0; i < q; i++){
int com;
int s;
int t;
int x;
cin >> com;
if(com == 0){
cin >> s >> t >> x;
sq.update(s, t + 1, x);
} else {
cin >> x;
cout << sq.get(x) << 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 <iostream>
#include<iomanip>
#include <cmath>
#include <climits>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <queue>
#include <tuple>
#include <map>
#include <list>
#include <string>
#include <numeric>
#include <utility>
#include <cfloat>
#include <set>
using namespace std;
int sqrtN = 512;
struct SqrtDecomposition{
int N, K;
vector <long long> data;
vector <long long> bucketUpdatedLazy;
SqrtDecomposition(int n){
N = n;
K = (N + sqrtN - 1) / sqrtN;
data.assign(N, ((long long) 1 << 31) - 1);
bucketUpdatedLazy.assign(K, -1);
}
void propagateLazy(int k){
if(bucketUpdatedLazy[k] < 0){
return;
}
for(int i = 0; i < sqrtN; i++){
data[k * sqrtN + i] = bucketUpdatedLazy[k];
}
bucketUpdatedLazy[k] = -1;
}
long long get(int x){
propagateLazy(x / sqrtN);
return data[x];
}
void update(int x, int y, int a){
if(y - x < sqrtN){
propagateLazy(x / sqrtN);
propagateLazy((y - 1) / sqrtN);
for(int i = x; i < y; i++){
data[i] = a;
}
return;
}
for(int i = x / sqrtN + 1; i < y / sqrtN; i++){
bucketUpdatedLazy[i] = a;
//if(bucketUpdatedLazy[i] < 0 && bucketUpdatedLazy[i] != -1){
// cerr << "Warning" << endl;
//}
}
propagateLazy(x / sqrtN);
for(int i = x; i < (x / sqrtN + 1) * sqrtN; i++){
data[i] = a;
}
propagateLazy((y - 1)/ sqrtN);
for(int i = y / sqrtN * sqrtN; i < y; i++){
data[i] = a;
}
}
};
int main(){
int n;
int q;
cin >> n >> q;
SqrtDecomposition sq(n);
for(int i = 0; i < q; i++){
int com;
int s;
int t;
int x;
cin >> com;
if(com == 0){
cin >> s >> t >> x;
sq.update(s, t + 1, x);
} else {
cin >> x;
cout << sq.get(x) << endl;
}
}
return 0;
}
``` |
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 2147483647
int tree[1000000];
void Update(int s, int t, int m, int left = 0, int right = (1 << 17), int key = 0){
if(t < left || right <= s) return;
if(s <= left && right - 1 <= t){ tree[key] = max(tree[key], m); return; }
Update(s, t, m, left, (left + right) / 2, 2 * key + 1);
Update(s, t, m, (left + right) / 2, right, 2 * key + 2);
}
int Query_find(int i){
i += (1 << 17) - 1;
int m = tree[i];
while(i){
i = (i - 1) / 2;
m = max(tree[i], m);
};
return m;
}
int main(){
int n, q, x[1000000],i;
for(i=0; i<1000000; i++) {tree[i]=0; x[i]=INF;}
cin >> n >> q;
int c, s, t, value, m=1;
for (int j=0; j<q; j++) {
cin >> c;
if(c){
cin >> i;
cout << x[Query_find(i)] << endl;
}else{
cin >> s >> t >> value;
x[m] = value;
Update(s, t, m);
m++;
}
}
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>
#include<algorithm>
using namespace std;
#define INF 2147483647
int tree[1000000];
void Update(int s, int t, int m, int left = 0, int right = (1 << 17), int key = 0){
if(t < left || right <= s) return;
if(s <= left && right - 1 <= t){ tree[key] = max(tree[key], m); return; }
Update(s, t, m, left, (left + right) / 2, 2 * key + 1);
Update(s, t, m, (left + right) / 2, right, 2 * key + 2);
}
int Query_find(int i){
i += (1 << 17) - 1;
int m = tree[i];
while(i){
i = (i - 1) / 2;
m = max(tree[i], m);
};
return m;
}
int main(){
int n, q, x[1000000],i;
for(i=0; i<1000000; i++) {tree[i]=0; x[i]=INF;}
cin >> n >> q;
int c, s, t, value, m=1;
for (int j=0; j<q; j++) {
cin >> c;
if(c){
cin >> i;
cout << x[Query_find(i)] << endl;
}else{
cin >> s >> t >> value;
x[m] = value;
Update(s, t, m);
m++;
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
pair<int,int> SegTree[1<<18];
void update(int l,int r,int x,int t)
{
while (l!=r)
{
if(l%2)
{
SegTree[l] = make_pair(x,t);
l++;
}
if(r%2)
{
SegTree[r-1] = make_pair(x,t);
r--;
}
l/=2;
r/=2;
}
//SegTree[l] = make_pair(x,t);
//cout<<l<<endl;
}
int get(int i)
{
pair<int,int> res(-1,-2);
while(i!=0)
{
if(SegTree[i].second>res.second)
{
res = SegTree[i];
}
i/=2;
}
return res.first;
}
int main()
{
//cout<<(1<<18)<<endl;
int n,q;
cin >> n >> q;
for(int i = 0;i<(1<<18);i++)
{
SegTree[i]=make_pair(INT_MAX,-1);
}
for(int i = 0;i<q;i++)
{
int op;
cin >> op;
if(op)
{
int I;
cin >> I;
I += (1<<17);
cout<<get(I)<<endl;
}
else
{
int s,t,x;
cin >> s >> t >> x;
s+=(1<<17);
t+=(1<<17);
t++;
update(s,t,x,i);
}
//cout<<SegTree[(1<<17)].first<<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;
using ll = long long;
pair<int,int> SegTree[1<<18];
void update(int l,int r,int x,int t)
{
while (l!=r)
{
if(l%2)
{
SegTree[l] = make_pair(x,t);
l++;
}
if(r%2)
{
SegTree[r-1] = make_pair(x,t);
r--;
}
l/=2;
r/=2;
}
//SegTree[l] = make_pair(x,t);
//cout<<l<<endl;
}
int get(int i)
{
pair<int,int> res(-1,-2);
while(i!=0)
{
if(SegTree[i].second>res.second)
{
res = SegTree[i];
}
i/=2;
}
return res.first;
}
int main()
{
//cout<<(1<<18)<<endl;
int n,q;
cin >> n >> q;
for(int i = 0;i<(1<<18);i++)
{
SegTree[i]=make_pair(INT_MAX,-1);
}
for(int i = 0;i<q;i++)
{
int op;
cin >> op;
if(op)
{
int I;
cin >> I;
I += (1<<17);
cout<<get(I)<<endl;
}
else
{
int s,t,x;
cin >> s >> t >> x;
s+=(1<<17);
t+=(1<<17);
t++;
update(s,t,x,i);
}
//cout<<SegTree[(1<<17)].first<<endl;
}
}
``` |
#include <bits/stdc++.h>
#define int long long
using namespace std;
class RUQ{
public:
typedef pair<int,int> P;
int n, cnt;
vector<P> A;
const int INF = (1LL<<31)-1;
RUQ(){}
RUQ(int n_){
n = 1;
while( n_ > n ) n *= 2;
A.resize( n * 2 - 1, P( 0, INF ) );
cnt = 0;
}
int find(int x){
P res = P( 0, INF );
x += n - 1;
res = max( res, A[x] );
while(x){
x = ( x + 1 ) / 2 - 1;
res = max( res, A[x] );
}
return res.second;
}
void update(int a, int b, int l, int r, int k, P x){
if( r <= a || b <= l ) return;
if( a <= l && r <= b ){
A[k] = x;
return;
}
update( a, b, l, (l+r)/2, k*2+1, x );
update( a, b, (l+r)/2, r, k*2+2, x );
}
void update(int a, int b, int x){
cnt++;
update(a,b,0,n,0,P(cnt,x));
}
};
signed main(){
int n, q;
cin>>n>>q;
RUQ ruq(n);
while(q--){
int com;
cin>>com;
if( com == 0 ){
int s, t, x;
cin>>s>>t>>x;
ruq.update(s,t+1,x);
}
else{
int x;
cin>>x;
cout<<ruq.find(x)<<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 <bits/stdc++.h>
#define int long long
using namespace std;
class RUQ{
public:
typedef pair<int,int> P;
int n, cnt;
vector<P> A;
const int INF = (1LL<<31)-1;
RUQ(){}
RUQ(int n_){
n = 1;
while( n_ > n ) n *= 2;
A.resize( n * 2 - 1, P( 0, INF ) );
cnt = 0;
}
int find(int x){
P res = P( 0, INF );
x += n - 1;
res = max( res, A[x] );
while(x){
x = ( x + 1 ) / 2 - 1;
res = max( res, A[x] );
}
return res.second;
}
void update(int a, int b, int l, int r, int k, P x){
if( r <= a || b <= l ) return;
if( a <= l && r <= b ){
A[k] = x;
return;
}
update( a, b, l, (l+r)/2, k*2+1, x );
update( a, b, (l+r)/2, r, k*2+2, x );
}
void update(int a, int b, int x){
cnt++;
update(a,b,0,n,0,P(cnt,x));
}
};
signed main(){
int n, q;
cin>>n>>q;
RUQ ruq(n);
while(q--){
int com;
cin>>com;
if( com == 0 ){
int s, t, x;
cin>>s>>t>>x;
ruq.update(s,t+1,x);
}
else{
int x;
cin>>x;
cout<<ruq.find(x)<<endl;
}
}
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
int t[(1<<18)];
void Set(int a,int b,int x,int k=0,int l=0,int r=(1<<17)){
if(b<=l || r<=a)return;
if(a<=l && r<=b){
t[k]=max(t[k],x);
return;
}
int m=(l+r)/2;
Set(a,b,x,k*2+1,l,m);
Set(a,b,x,k*2+2,m,r);
}
int Get(int i){
i+=(1<<17)-1;
int res=t[i];
while(i){
i=(i-1)/2;
res=max(res,t[i]);
}
return res;
}
int n,q;
int value[100005];
int main(){
value[0]=2147483647;
scanf("%d %d",&n,&q);
for(int i=1;i<=q;i++){
int type;
scanf("%d",&type);
if(type==0){
int l,r;
scanf("%d %d %d",&l,&r,&value[i]);
r++;
Set(l,r,i);
}else{
int target;
scanf("%d",&target);
int ans=Get(target);
printf("%d\n",value[ans]);
}
}
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;
int t[(1<<18)];
void Set(int a,int b,int x,int k=0,int l=0,int r=(1<<17)){
if(b<=l || r<=a)return;
if(a<=l && r<=b){
t[k]=max(t[k],x);
return;
}
int m=(l+r)/2;
Set(a,b,x,k*2+1,l,m);
Set(a,b,x,k*2+2,m,r);
}
int Get(int i){
i+=(1<<17)-1;
int res=t[i];
while(i){
i=(i-1)/2;
res=max(res,t[i]);
}
return res;
}
int n,q;
int value[100005];
int main(){
value[0]=2147483647;
scanf("%d %d",&n,&q);
for(int i=1;i<=q;i++){
int type;
scanf("%d",&type);
if(type==0){
int l,r;
scanf("%d %d %d",&l,&r,&value[i]);
r++;
Set(l,r,i);
}else{
int target;
scanf("%d",&target);
int ans=Get(target);
printf("%d\n",value[ans]);
}
}
return 0;
}
``` |
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using ll = long long;
template <typename T>
T min(T a, T b){
return (a < b) ? a : b;
}
template <typename T>
class SegTree{
static const int MAXN = 100010;
T INF;
int n;
T A[MAXN*4];
T time[MAXN*4];
public:
SegTree(int size, T INF): INF(INF){
n = 1;
while(n < size){
n *= 2;
}
n *= 2;
for(int i = 0; i < MAXN*4; i++) A[i] = INF;
for(int i = 0; i < MAXN*4; i++) time[i] = 0;
}
void update(int a, int b, T v, T t){
return _update(a, b, 0, 0, n, v, t);
}
void _update(int a, int b, int k, int l, int r, T v, T t){
if(a >= r || b <= l){
return;
}
if(l >= a && r <= b){
A[k] = v;
time[k] = t;
return;
}
_update(a, b, 2*k + 1, l, (l+r)/2, v, t);
_update(a, b, 2*k + 2, (l+r)/2, r, v, t);
}
T query(int _idx){
int offset = n - 1;
int idx = _idx + offset;
T t = 0;
T ans = INF;
while(idx > 0){
if(time[idx] >= t){
ans = A[idx];
t = time[idx];
}
idx = (idx-1)/2;
}
return ans;
}
};
int main(void){
int n, q; cin >> n >> q;
SegTree<int> rmq(n, 0x7FFFFFFF);
for(int loop = 0; loop < q; loop++){
int com;
cin >> com;
if(com == 0){
int x, y, v; cin >> x >> y >> v;
rmq.update(x, y+1, v, loop+1);
}else{
int i; cin >> i;
cout << rmq.query(i) << 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>
using std::cin;
using std::cout;
using std::endl;
using ll = long long;
template <typename T>
T min(T a, T b){
return (a < b) ? a : b;
}
template <typename T>
class SegTree{
static const int MAXN = 100010;
T INF;
int n;
T A[MAXN*4];
T time[MAXN*4];
public:
SegTree(int size, T INF): INF(INF){
n = 1;
while(n < size){
n *= 2;
}
n *= 2;
for(int i = 0; i < MAXN*4; i++) A[i] = INF;
for(int i = 0; i < MAXN*4; i++) time[i] = 0;
}
void update(int a, int b, T v, T t){
return _update(a, b, 0, 0, n, v, t);
}
void _update(int a, int b, int k, int l, int r, T v, T t){
if(a >= r || b <= l){
return;
}
if(l >= a && r <= b){
A[k] = v;
time[k] = t;
return;
}
_update(a, b, 2*k + 1, l, (l+r)/2, v, t);
_update(a, b, 2*k + 2, (l+r)/2, r, v, t);
}
T query(int _idx){
int offset = n - 1;
int idx = _idx + offset;
T t = 0;
T ans = INF;
while(idx > 0){
if(time[idx] >= t){
ans = A[idx];
t = time[idx];
}
idx = (idx-1)/2;
}
return ans;
}
};
int main(void){
int n, q; cin >> n >> q;
SegTree<int> rmq(n, 0x7FFFFFFF);
for(int loop = 0; loop < q; loop++){
int com;
cin >> com;
if(com == 0){
int x, y, v; cin >> x >> y >> v;
rmq.update(x, y+1, v, loop+1);
}else{
int i; cin >> i;
cout << rmq.query(i) << endl;
}
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1 << 18;
#define int long long
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 query(int k){
k+=n-1;
P p=dat[k];
//cout<<k<<"/"<<dat[k].second<<endl;
while(k>0){
k=(k-1)/2;
//cout<<k<<"/"<<dat[k].second<<endl;
p=max(p,dat[k]);
}
return p.second;
}
void update(int a,int b,int k,P p,int l,int r){
if(r<=a||b<=l) return;
if(a<=l&&r<=b) {
//cout<<a<<":"<<b<<":"<<k<<":"<<l<<":"<<r<<endl;
dat[k]=p;
}else{
update(a,b,k*2+1,p,l,(l+r)/2);
update(a,b,k*2+2,p,(l+r)/2,r);
}
}
signed main(){
int _n,q;
cin>>_n>>q;
init(_n+1);
for(int i=0;i<q;i++){
int f;
cin>>f;
if(!f){
int s,t,x;
cin>>s>>t>>x;
//cout<<i<<":"<<x<<endl;
update(s,t+1,0,P(i,x),0,n);
}else{
int u;
cin>>u;
cout<<query(u)<<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<bits/stdc++.h>
using namespace std;
const int MAX_N = 1 << 18;
#define int long long
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 query(int k){
k+=n-1;
P p=dat[k];
//cout<<k<<"/"<<dat[k].second<<endl;
while(k>0){
k=(k-1)/2;
//cout<<k<<"/"<<dat[k].second<<endl;
p=max(p,dat[k]);
}
return p.second;
}
void update(int a,int b,int k,P p,int l,int r){
if(r<=a||b<=l) return;
if(a<=l&&r<=b) {
//cout<<a<<":"<<b<<":"<<k<<":"<<l<<":"<<r<<endl;
dat[k]=p;
}else{
update(a,b,k*2+1,p,l,(l+r)/2);
update(a,b,k*2+2,p,(l+r)/2,r);
}
}
signed main(){
int _n,q;
cin>>_n>>q;
init(_n+1);
for(int i=0;i<q;i++){
int f;
cin>>f;
if(!f){
int s,t,x;
cin>>s>>t>>x;
//cout<<i<<":"<<x<<endl;
update(s,t+1,0,P(i,x),0,n);
}else{
int u;
cin>>u;
cout<<query(u)<<endl;
}
}
return 0;
}
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.