Cod sursa(job #626097)

Utilizator theodora_maneaManea Theodora Maria theodora_manea Data 26 octombrie 2011 13:23:13
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <stdio.h>
 
int arb[262144],n,m,i,poz,a,b,x,max;
 
int maxim(int a,int b) {
    if (a>b) return a;
   else return b;
}
 
void insert(int nod,int st,int dr) {
    int m;
	m=(st+dr)/2;
    if (st==poz && dr==poz) arb[nod]=x;
	else {
    if (poz<=m) insert(2*nod,st,m);
    else insert(2*nod+1,m+1,dr);
    //arb[nod]=maxim(arb[2*nod],arb[2*nod+1]);
	if (arb[2*nod]>arb[2*nod+1]) arb[nod]=arb[2*nod];
	else arb[nod]=arb[2*nod+1];
	}
}

int cautare(int nod, int st,int dr) {
    int m,x1,x2;
	x1=0;
	x2=0;
    if (a<=st && dr<=b) x1=arb[nod];
	else {
    m=(st+dr)/2;
    if (a<=m) x1=cautare(nod*2,st,m);
    if (b>m) x2=cautare(nod*2+1,m+1,dr);
	if (x2>x1) x1=x2;
	}
	return x1;
}
 
int main () {
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (i=1; i<=n; i++) {
        scanf("%d",&x);
        poz=i;
        insert(1,1,n);
    }
    for (i=1; i<=m; i++) {
        scanf("%d%d%d",&x,&a,&b);
        if (x==0) {
            printf("%d\n",cautare(1,1,n));
        }
        else {
            poz=a;
            x=b;
            insert(1,1,n);
        }
    }
    return 0;
}