Cod sursa(job #2437970)

Utilizator Senth30Denis-Florin Cringanu Senth30 Data 10 iulie 2019 20:38:52
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.81 kb
#include <bits/stdc++.h>

using namespace std;

const int MAX = 131072;
const int NMAX = 100005;

int qry, poz, val, x_1, x_2;
int N, M, xval;
int T[3*NMAX], v[NMAX];

int pos;
char f[MAX];

int Query(int node, int st, int nd, int A, int B){
    if(A <= st && nd <= B){
        return T[node];
    } else {
        int ans = -1;
        int mid = (st + nd) / 2;
        if(A <= mid)
            ans = max(ans, Query(2 * node, st, mid, A, B));
        if(mid + 1 <= B)
            ans = max(ans, Query(2 * node + 1, mid + 1, nd, A, B));
        return ans;
    }
}

void Update(int node, int st, int nd, int pos, int val){
    int mid;
    if(st == nd){
        T[node] = val;
    } else {
        mid = (st + nd) / 2;
        if(pos <= mid) Update(2 * node, st, mid, pos, val);
        else Update(2 * node + 1, mid + 1, nd, pos, val);
        T[node] = max(T[2 * node], T[2 * node + 1]);
    }
}

inline void Read(int &nr){
    nr = 0;
    while(f[pos] < '0' || f[pos] > '9'){
        pos++;
        if(pos == MAX)
            fread(f, MAX, 1, stdin), pos = 0;
    }
    while(f[pos] >= '0' && f[pos] <= '9'){
        nr = 10 * nr + f[pos++] - '0';
        if(pos == MAX)
            fread(f, MAX, 1, stdin), pos = 0;
    }
}

int read(){
    fread(f, 1, MAX, stdin);
    Read(N); Read(M);
    for(int i = 1; i <= N; i++)
        Read(xval), Update(1, 1, N, i, xval);
}

int main(){

    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);

    read();

    for(int i = 1; i <= M; i++){
        Read(qry);
        if(qry == 1){
            Read(poz); Read(val);
            Update(1, 1, N, poz, val);
        } else {
            Read(x_1); Read(x_2);
            printf("%d\n", Query(1, 1, N, x_1, x_2));
        }
    }

    return 0;
}