Cod sursa(job #631232)

Utilizator sunt_emoSunt emo sunt_emo Data 7 noiembrie 2011 14:59:13
Problema Arbori de intervale Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 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,Max;

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,ph<<1);
    int m2=comp ((l+r)>>1,r,(ph<<1)+1);
    return h[ph]=max (m1,m2);
}

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

void op0 (int l,int r,int l0,int r0,int ph) {
    printf ("%d %d %d %d\n",l,r,l0,r0);
    if (l0<=l&&r0>=r) {
        Max=max(h[ph],Max);
        return;
    }
    int m=(l+r)>>1;
    if (l0<m) op0 (l,m,l0,r0,(ph<<1));
    if (r0>m) op0 (m,r,l0,r0,(ph<<1)+1);
}

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