Cod sursa(job #1477943)

Utilizator lflorin29Florin Laiu lflorin29 Data 27 august 2015 13:55:11
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>
using namespace std;

const int kMax = (int) 2 * 1e5 + 4;
const int lowInf = ~(1 << 30);
const int blockSize = 512;

int n, m;
int v[kMax];
int go[blockSize >> 1];

ifstream fin ("arbint.in");
ofstream fout ("arbint.out");

void read(){
    fin >> n >> m;
    for (int i = 0; i < n; ++i)
        fin >> v[i];
}

void build(){
    for (int i = 0 ; i < n ; ++ i)
        go[i / blockSize] = max(go[i / blockSize], v[i]);
}

inline void update (int i, int val){
    int where = i / blockSize;
    go[where] = 0;
    v[i] = val;
    for (int j = where * blockSize; j < (where + 1) * blockSize; ++j)
          go[where] = max(go[where], v[j]);
}

inline int query (int a, int b){
    int ret = lowInf ;
    for (; a % blockSize and a <= b; a++)
        ret = max(ret, v[a]);
    for (; a + blockSize <= b; a += blockSize)
        ret = max(ret, go[a / blockSize ]);
    for (; a <= b; ++a)
        ret = max(v[a], ret);
    return ret;
}

inline void solve(){
    for (int q = 0; q < m; ++q){
        int now, a, b;
        fin >> now >> a >> b;
        if (!now)
            fout << query(a - 1, b - 1) << "\n";
        else
            update(a - 1, b);
    }
}

int main(){
    read();
    build();
    solve();
}