Cod sursa(job #673018)

Utilizator SmarandaMaria Pandele Smaranda Data 3 februarie 2012 18:09:06
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include<cstdio>
#include<algorithm>
using namespace std;

struct nod_tree {
    nod_tree *tree_st,*tree_dr;
    long left,right,val;
};
nod_tree temp;
void create_tree (nod_tree *p , long st , long dr) {
    p->val=0;
    p->left=st;
    p->right=dr;
    if (st==dr)
        return;
    p->tree_st=new nod_tree;
    p->tree_dr=new nod_tree;
    create_tree(p->tree_st,st,(st+dr)/2);
    create_tree(p->tree_dr,(st+dr)/2+1,dr);
}

void update (nod_tree *p,long poz, long val1) {
    if (p->left==p->right) {
        p->val=val1;
        return ;
    }
    if (poz<=((p->left)+(p->right))/2)
        update (p->tree_st,poz,val1);
    else
        update (p->tree_dr,poz,val1);
    p->val=max(p->tree_dr->val,p->tree_st->val);
}

long query (nod_tree *p , long st , long dr) {
    if (st==p->left && dr==p->right)
        return p->val;
    long t=0,x=((p->right)+(p->left))/2;
    if (st<=x)
        t=query(p->tree_st,st,min(dr,x));
    if (dr>x)
        t=max(t,query(p->tree_dr,max(st,x+1),dr));
    return t;
}

void read () {
    long n,m,a,b,c,i;
    scanf("%ld%ld",&n,&m);
    create_tree(&temp,1,n);
    for (i=1;i<=n;i++) {
        scanf("%ld",&a);
        update(&temp,i,a);
    }
    for (i=1;i<=m;i++) {
        scanf("%ld%ld%ld",&a,&b,&c);
        if (a==1)
            update(&temp,b,c);
        else
            printf("%ld\n",query (&temp,b,c));
    }
}

int main () {
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);

    read ();
    return 0;
}