Cod sursa(job #2968506)

Utilizator LucaT2Tasadan Luca LucaT2 Data 21 ianuarie 2023 13:04:09
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.81 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("arbint.in");
ofstream fout("arbint.out");

struct nod{
    int value,left_bound,right_bound;
    nod *left_child,*right_child;
};
int n,ar[1000005],m;
nod* create_nod(int *v,int left_bound,int right_bound)
{
    nod *q=new nod;
    q->left_bound=left_bound;
    q->right_bound=right_bound;
    if(right_bound==left_bound)
    {
        q->value=ar[left_bound];
        q->left_child=NULL;
        q->right_child=NULL;
        return q;
    }
    int mij=(left_bound+right_bound)/2;
    q->left_child=create_nod(v,left_bound,mij);
    q->right_child=create_nod(v,mij+1,right_bound);
    q->value=max(q->left_child->value,q->right_child->value);
    return q;
}

void free_nod(nod *r)
{
    if(r==NULL)
        return;
    free_nod(r->left_child);
    free_nod(r->right_child);
    delete r;
}

void update(nod *r,int pos,int val)
{
    if(pos<r->left_bound || pos>r->right_bound)
        return;
    if(r->left_bound==r->right_bound)
    {
        r->value=val;
        return;
    }
    update(r->left_child,pos,val);
    update(r->right_child,pos,val);
    r->value=max(r->left_child->value,r->right_child->value);
}

int query(nod *r,int a, int b)
{
    if(r->right_bound<a || r->left_bound>b)
        return INT_MIN;
    if(r->left_bound>=a && r->right_bound<=b)
        return r->value;
    int s1 = query(r->left_child,a,b);
    int s2 = query(r->right_child,a,b);
    return max(s1,s2);
}

int main()
{
    fin>>n>>m;
    for(int i=1;i<=n;i++)
        fin>>ar[i];
    int c,x,y;
    nod *q=create_nod(ar,1,n);
    for(int i=1;i<=m;i++)
    {
        fin>>c>>x>>y;
        if(c==0)
        {
        fout<<query(q,x,y)<<"\n";
        }
        else update(q,x,y);
    }
    free_nod(q);
    return 0;
}