Cod sursa(job #2203806)

Utilizator unknownpersonBidasca Carina Georgiana unknownperson Data 13 mai 2018 11:14:51
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("arbint.in");
ofstream g("arbint.out");
const int N = 1<<18;
struct nod
{
    int M,L,R;
    nod *fs,*fd;
};
nod *root,*make_tree(int,int);
void upd(nod*);
int n,m,o,x,y,get_max(nod*);
int main()
{
    f>>n>>m;
    root = make_tree(1,n);
    for(;m;m--)
    {
        f>>o>>x>>y;
        if(o==0)
            g<<get_max(root)<<"\n";
        else
            upd(root);
    }

    return 0;
}

int get_max(nod *N)
{
    if(x>N->R||y<N->L)
        return 0;
    if(x<=N->L&&N->R<=y)
        return N->M;
    return max(get_max(N->fs),get_max(N->fd));
}
nod *make_tree(int st,int dr)
{
    nod *N=new nod;
    if(st==dr)
    {
        N->L=N->R=st;
        f>>N->M;
        N->fs=N->fd=NULL;
    }
    else
    {
        int mi=(st+dr)/2;
        N->L=N->R=st;
        N->fs=make_tree(st,mi);
        N->fd=make_tree(mi+1,dr);
        N->M=max(N->fs->M,N->fd->M);
    }
    return N;
}
void upd(nod *N)
{
    if(N->L==N->R)
    {
        N->M=y;
        return;
    }
    if(x<=N->fs->R)
        upd(N->fs);
    else
        upd(N->fd);
    N->M=max(N->fs->M,N->fd->M);
}