Cod sursa(job #531567)

Utilizator marcelcodreaCodrea Marcel marcelcodrea Data 9 februarie 2011 21:24:01
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 kb
#include <iostream>
#include <math.h>
#define MIN -1
#define N 100005
int sir[N];
int arb[4*N];
int n,m;
int val;
int st;
int poz;
int newval;
int dr;
using namespace std;

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

int getMax(int nod, int lim1, int lim2) {
    if (lim2 < st || lim1 > dr) return MIN;
     else
    if (lim1 >= st && lim2 <= dr) return arb[nod];
     else
      return maxim(getMax(2*nod,lim1,(lim1+lim2)/2),getMax(2*nod+1,(lim1+lim2)/2+1,lim2));


}
void buildTree(int nod, int st, int dr) {
    if (st == dr)
     arb[nod] = sir[st];
    else {
        buildTree(2*nod, st, (st+dr) / 2);
        buildTree(2*nod+1, (st + dr) / 2 + 1, dr);
        arb[nod] = maxim(arb[2 * nod], arb[2 * nod + 1]);
    }
}
void update(int nod, int st, int dr) {
    if (st == dr) arb[nod] = newval;
    else {
        int mij = (st + dr) / 2;
        if (poz <= mij)
            update(2*nod,st,mij);
        else
            update(2*nod+1,mij+1,dr);
        arb[nod] = maxim(arb[2*nod],arb[2*nod+1]);
    }
}
int main() {
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(int i = 1; i <= n; i++) {
        scanf("%d",&sir[i]);
    }
    buildTree(1,1,n);
    int cod,x,y;
    for(int i = 1; i <= m; i++) {
     scanf("%d %d %d",&cod,&x,&y);
     if (cod == 0) {
        st = x;
        dr = y;
        printf("%d\n",getMax(1,1,n));
     }
     else {
        poz = x;
        newval = y;
        update(1,1,n);
     }
    }
    return 0;
}