Cod sursa(job #1667963)

Utilizator tudormaximTudor Maxim tudormaxim Data 29 martie 2016 13:45:02
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

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

const int nmax = 1e5+5;
const int buffmax = 1<<19;
int bmax, v[nmax], bucket[nmax], p=buffmax-1;
char buff[buffmax];

class scanner{
public:
    inline scanner& operator >> (int &val){
        while(!(buff[p]>='0' && buff[p]<='9')){
            if(++p==buffmax) f.read(buff, buffmax), p=0;
        }
        val=0;
        while(buff[p]>='0' && buff[p]<='9'){
            val=(val<<1)+(val<<3)+buff[p]-'0';
            if(++p==buffmax) f.read(buff, buffmax), p=0;
        }
        return *this;
    }
} fin;

void update(int poz, int val){
    int i=poz/bmax, j;
    if(v[poz]==bucket[i]){
        bucket[i]=0;
        v[poz]=0;
        for(j=i*bmax; j<(i+1)*bmax; j++){
            if(v[j] > bucket[i]) bucket[i]=v[j];
        }
    }
    v[poz]=val;
    if(val > bucket[i]) bucket[i]=val;

}

void query(int inc, int sf){
    int st=inc/bmax, dr=sf/bmax, i, sol=0;
    for(i=st+1; i<dr; i++){
        if(bucket[i] > sol) sol=bucket[i];
    }
    if(sol < bucket[st]){
        for(i=inc; i<=sf && i<(st+1)*bmax; i++){
            if(v[i] > sol) sol=v[i];
        }
    }
    if(sol < bucket[dr]){
        for(i=max(dr*bmax, inc); i<=sf; i++){
            if(v[i] > sol) sol=v[i];
        }
    }
    fout << sol << "\n";
}

int main(){
    ios_base::sync_with_stdio(false);
    int n, m, i, a, b, op;
    fin >> n >> m;
    bmax=sqrt(n);
    for(i=0; i<n; i++){
        fin >> v[i];
        if(v[i] > bucket[i/bmax]) bucket[i/bmax]=v[i];
    }
    for(i=0; i<m; i++){
        fin >> op >> a >> b;
        if(op==1) update(a-1, b);
        else query(a-1, b-1);
    }
    f.close();
    fout.close();
    return 0;
}