Cod sursa(job #631216)

Utilizator sunt_emoSunt emo sunt_emo Data 7 noiembrie 2011 13:52:37
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>
#include <iostream>
#define N 100010

using namespace std;

ifstream in ("arbint.in");
ofstream out ("arbint.out");
int p[N],h[4*N],n,m,i,a,b;

int max (int a,int b) {
    if (a>b) return a; return b;
}

int comp (int l,int r,int ph) {
    if (r-l==1) {
        in>>h[ph];
        p[l]=ph;
        return h[ph];
    }
    int m1=comp (l,(l+r)>>1,2*ph);
    int m2=comp ((l+r)>>1,r,2*ph+1);
    return h[ph]=max (m1,m2);
}

void op1 (int poz,int val) {
    int ph;
    h[ph=p[poz-1]]=val;
    while ((ph=ph/2)) h[ph]=max (h[ph*2],h[ph*2+1]);
}

int op0 (int l,int r,int l0,int r0,int ph) {
    if (l==l0&&r==r0) return h[ph];
    int m=(l+r)>>1;
    if (l0<m)
        if (r0<=m) return op0 (l,m,l0,m,ph*2);
        else return max (op0 (l,m,l0,m,ph*2),op0 (m,r,m,r0,ph*2+1));
    else return op0 (m,r,l0,r0,2*ph+1);
}

int main () {
    in>>n>>m;
    comp (0,n,1);
    while (m--) {
        in>>i>>a>>b;
        if (i) op1 (a,b);
        else out<<op0 (0,n,a-1,b,1)<<"\n";
    }
    return 0;
}