Cod sursa(job #531553)

Utilizator marcelcodreaCodrea Marcel marcelcodrea Data 9 februarie 2011 21:12:42
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 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 mij;
int cod,x,y;
using namespace std;

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

int getMax(int nod, int lim1, int lim2, int st, int dr) {
    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,st,dr),getMax(2*nod+1,(lim1+lim2)/2+1,lim2,st,dr));


}
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, int poz, int newval) {
    if (st == dr) arb[nod] = newval;
    else {
        int mij = (st + dr) / 2;
        if (poz <= mij)
            update(2*nod,st,mij,poz,newval);
        else
            update(2*nod+1,mij+1,dr,poz,newval);
        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);
    for(int i = 1; i <= m; i++) {
     scanf("%d %d %d",&cod,&x,&y);
     if (cod == 0)
        printf("%d\n",getMax(1,1,n,x,y));
     else
      update(1,1,n,x,y);
    }
    return 0;
}