Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define nl "\n"
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define RV return void
- #define sz(x) int(x.size())
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define fixed(n) fixed << setprecision(n)
- #define cin(v) for(auto&x:v) cin >> x;
- #define cout(v) for(auto&x:v) cout << x << " ";
- void files(){
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
- struct Node{
- int mx;
- Node(){
- mx=-1;
- }
- Node(int x){
- mx=x;
- }
- };
- struct SegTree{
- int tree_size;
- vector<Node> tree;
- SegTree(int n){
- tree_size = 1;
- while (tree_size < n) tree_size *= 2;
- tree.assign(2 * tree_size, Node());
- }
- Node merge(Node &left, Node &right){
- Node ans = Node();
- ans.mx = max(left.mx , right.mx);
- return ans;
- }
- void build(vector<int> & v, int node, int lx, int rx){
- if(rx - lx == 1){
- if(lx < sz(v) )
- tree[node] = Node(v[lx]);
- return;
- }
- int md = lx + (rx - lx) / 2;
- build(v, 2 * node + 1, lx, md);
- build(v, 2 * node + 2, md, rx);
- tree[node] = merge(tree[2 * node + 1], tree[2 * node + 2]);
- }
- void build(vector<int> & v){
- build(v, 0, 0, tree_size);
- }
- void set(int idx,int val , int node, int lx, int rx){
- if(rx - lx == 1){
- tree[node] = Node(val);
- return;
- }
- int md = lx + (rx - lx) / 2;
- if(idx < md)
- set(idx,val, 2 * node + 1, lx, md);
- else
- set(idx,val, 2 * node + 2, md, rx);
- tree[node] = merge(tree[2 * node + 1], tree[2 * node + 2]);
- }
- void set(int idx , int val){
- set(idx, val, 0, 0, tree_size);
- }
- int get(int x , int lst , int node, int lx, int rx){
- if(rx - lx == 1){
- // return (tree[node].mx >= x and lx >= lst ? lx : -1);
- // cout << "node : " << node << " -> " << lx << " " << rx << nl;
- return lx;
- }
- Node left = tree[node*2+1];
- int md = lx + (rx - lx) / 2;
- if(left.mx >= x and lst < md )
- return get(x , lst , 2 * node + 1, lx , md);
- else if(tree[node*2+2].mx >= x and lst < rx )
- return get( x , lst , 2 * node +2, md , rx);
- return -1;
- }
- int get(int x , int lst){
- return get(x , lst ,0, 0, tree_size);
- }
- };
- void solve(){
- int n , idx ,m , x , op , val , lst;
- cin >> n >> m;
- vector<int>v(n);
- cin(v);
- SegTree seg(n);
- seg.build(v);
- while(m--){
- cin >> op;
- if(op==1){
- cin >> idx >> val;
- v[idx]=val;
- seg.set(idx , val);
- }
- else{
- cin >> x >> lst;
- int ans =seg.get(x , lst);
- cout << ans << nl;
- }
- }
- }
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- // files();
- int testCase=1;
- // cin >> testCase ;
- for(int i=1 ; i <= testCase ; i++){
- // cout << "Case "<< i <<": ";
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement