Cod sursa(job #2593357)

Utilizator VanillaSoltan Marian Vanilla Data 3 aprilie 2020 17:00:16
Problema Arbori de intervale Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.73 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <fstream>
#include <set>
#include <stack>
#include <queue>
#include <list>
using namespace std;
typedef long long int64;
typedef vector<int> vec;
typedef vector<int64> vec64;
#define db(x) cout << "> " << #x << ": " << x << "\n";
// // // // // // // // // // // // // // // // // // //
ifstream in ("arbint.in");
ofstream out ("arbint.out");
int q, n;
int partsize;
vec arr (100001);
vec part (100001);

void update(int indx, int val){
    int tm = -1;
    if (val >= arr[indx]){
        arr[indx] = val;
        part[indx / partsize] = val;
    }
    else{
        arr[indx] = val;
        int i = (indx / partsize) * partsize;
        while (i < (indx / partsize) * partsize + partsize && i < n){
            tm = max(tm, arr[i]);
            i++;
        }
        part[indx / partsize] = tm;
    }
}

int range(int l, int r){
    int rs = -1;
    while (l <= r && l % partsize !=0){
        rs = max(rs, arr[l]);
        l++;
    }
    while (l + partsize <= r){
        rs = max(rs, part[l / partsize]);
        l+= partsize;
    }
    while (l <= r){
        rs = max(rs, arr[l]);
        l++;
    }
    return rs;
}

int main(){
    in >> n >> q;
    for (int i = 0; i < n; i++){
        in >> arr[i];
    }
    int indx = -1;
    partsize = sqrt(n);
    for (int i = 0; i < n; i++){
        if (i % partsize == 0)
            indx++;
        part[indx] = max(part[indx], arr[i]);
    }
    while (q--){
        int a,x,y;
        in >> a >> x >> y;
        if (a == 0){
            out << range(x-1, y-1) <<"\n";
        }
        else
            update(x-1, y);
    }
    return 0;
}