Cod sursa(job #1437798)

Utilizator teoceltareconstantin teodor teoceltare Data 18 mai 2015 17:34:25
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.37 kb
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int v[100010],n,m1,a,b,maxx;
struct nod
{
    int max1,lst,ldr;
    nod *st, *dr;
} *k;
void citire()
{
    fin>>n>>m1;
    for(int a=1;a<=n;a++)
    {
        fin>>v[a];
    }
}
void prel(nod *&c,int str,int drr)
{
    int m=(str+drr)/2;
    if(str==drr)
    {
        c->st=0;
        c->dr=0;
        c->max1=v[str];
        c->lst=c->ldr=str;
    }
    else
    {
        c->lst=str;
        c->ldr=drr;
        nod *c1,*c2;
        c1=new(nod);
        c2=new(nod);
        c->st=c1;
        prel(c1,str,m);
        c->dr=c2;
        prel(c2,m+1,drr);
        if(c1->max1>c2->max1)
        {
            c->max1=c1->max1;
        }
        else
        {
            c->max1=c2->max1;
        }
    }
}
void inloc(int x,nod *&c)
{
    if(c->lst==c->ldr)
    {
        c->max1=v[x];
    }
    else
    {
        int m=(c->lst+c->ldr)/2;
        nod *c1;
        if(x<=m)
        {
            c1=c->st;
            inloc(x,c1);
            c->st=c1;
        }
        else
        {
            c1=c->dr;
            inloc(x,c1);
            c->dr=c1;
        }
        if(c->st->max1>c->dr->max1)
        {
            c->max1=c->st->max1;
        }
        else
        {
            c->max1=c->dr->max1;
        }
    }
}
void parc1(nod *c)
{
    int m;
    if(a<=c->lst and b>=c->ldr)
    {
        if(maxx<c->max1) maxx=c->max1;
    }
    else
    {
        m=(c->lst+c->ldr)/2;
        if(a<=m)
        {
            parc1(c->st);
        }
        if(b>m)
        {
            parc1(c->dr);
        }
    }
}
int main()
{
    int x,y,z;
    citire();
    nod *c1,*c2;
    c1=new(nod);
    c2=new(nod);
    k=new(nod);
    k->lst=1;
    k->ldr=n;
    int m=(1+n)/2,max1,max2;
    k->st=c1;
    prel(c1,1,m);
    k->dr=c2;
    prel(c2,m+1,n);
    if(c1->max1 >c2->max1)
    {
        k->max1=c1->max1;
    }
    else
    {
        k->max1=c2->max1;
    }
    for(int a1=1;a1<=m1;a1++)
    {
        fin>>z;
        if(z==1)
        {
            fin>>x>>y;
            v[x]=y;
            inloc(x,k);
        }
        else if(z==0)
        {
            fin>>a>>b;
            maxx=0;
            parc1(k);
            fout<<maxx<<'\n';
        }
    }
}