Cod sursa(job #2857576)

Utilizator PescaPescariu Matei Alexandru Pesca Data 25 februarie 2022 21:05:56
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.76 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
struct arbore
{
    int st,dr,val;
    arbore *vf,*f1,*f2;
}*VF;
int n=-69;
arbore *gen(int s,int d,arbore *va)
{
    if(s==d)
    {
        arbore *nou=new arbore;
        nou->st=nou->dr=s;
        nou->f1=nou->f2=NULL;
        nou->vf=va;
        fin>>nou->val;
        return nou;
    }
    arbore *nou=new arbore;
    nou->vf=va;
    nou->st=s;
    nou->dr=d;
    int m=(s+d)/2;
    nou->f1=gen(s,m,nou);
    nou->f2=gen(m+1,d,nou);
    nou->val=max(nou->f1->val,nou->f2->val);
    return nou;
}
void creare()
{
    VF=new arbore;
    VF->st=1;
    VF->dr=n;
    VF->vf=NULL;
    int s=1,d=n,m=(s+d)/2;
    VF->f1=gen(s,m,VF);
    VF->f2=gen(m+1,d,VF);
    VF->val=max(VF->f1->val,VF->f2->val);
}
int mx(int s,int d,arbore *a)
{
    if(!a)
        return 0;
    if(a->st>=s && a->dr<=d)
        return a->val;
    if(a->st>d || a->dr<s)
        return 0;
    return max(mx(s,d,a->f1),mx(s,d,a->f2));
}
void upd (int pz,int x,arbore *a,bool pp=true)
{
    if(pp)
    {
        if(a->st==a->dr)
        {
            a->val=x;
            return upd(pz,x,a,false);
        }
        int mj=(a->st+a->dr)/2; /// 1- mj , mj+1 - d
        if(mj<pz)
            upd(pz,x,a->f2);
        else
            upd(pz,x,a->f1);
    }
    else
    {
        if(a->vf)
        {
            if(x>a->vf->val)
                a->vf->val=x;
            else
            {
                if(a->vf->f1==a)
                    if(x>a->vf->f2->val)
                        a->vf->val=x;
                    else
                        a->vf->val=a->vf->f2->val;
                else if(x>a->vf->f1->val)
                    a->vf->val=x;
                else
                    a->vf->val=a->vf->f1->val;
            }
            return upd(pz,x,a->vf,pp);
        }
    }
}
void afs() /// afisare
{
    arbore *q[100];
    q[0]=VF;
    int s=0,d=1;
    while(s<d)
    {
        if(q[s])
        {
            q[d++]=q[s]->f1;
            q[d++]=q[s]->f2;
        }
        s++;
    }
    s=0;
    for(int i=0; i<d; i++)
    {
        for(int j=0; j<(1<<i); j++)
            if(q[s])
                cout<<q[s++]->val<<"  ";
            else
                break;
        if(q[s])
            cout<<"\n";
        else
            break;
    }///arbint.in
    cout<<'\n';
}
int main()
{
    int q;
    fin>>n>>q;
    cout<<n;
    creare();
    ///afs();
    while(q--)
    {
        int a,x,y;
        fin>>a>>x>>y;
        if(a==0)
        {
            if(x>y)
                swap(x,y);
            fout<<mx(x,y,VF)<<'\n';
        }
        else
            upd(x,y,VF);
        ///afs();
    }
}