Cod sursa(job #1558047)

Utilizator TonyFrumTony Frum TonyFrum Data 28 decembrie 2015 17:15:41
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <iostream>
#include<fstream>
using namespace std;
int v[10000001],n,x,y,maxi;
ifstream f("arbint.in");
ofstream g("arbint.out");
void m(int t, int a, int b)
{
    if(a>=x && y>=b)
    {
        //cout<<a<<" "<<b<<" "<<x<<endl;
        if(maxi<v[t])
            maxi=v[t];
    }
    else
    {
        if(x<=(a+b)/2)
            m(2*t,a,(a+b)/2);
        if(y>=(a+b)/2+1){
            m(2*t+1,(a+b)/2+1,b);}
    }
}
void build(int t,int a, int b)
{
    if(a==b)
    {
        int x;
        f>>x;
        v[t]=x;
    }
    else
    {
        build(2*t,a,(a+b)/2);
        build(2*t+1,(a+b)/2+1,b);
        if(v[2*t]>v[2*t+1])
            v[t]=v[2*t];
        else
            v[t]=v[2*t+1];
    }
}
void update(int t,int a,int b)
{
    if(a==b && b==x){
        v[t]=y;}
    else
    {
        if(x<=(a+b)/2)
            update(2*t,a,(a+b)/2);
        else
            update(2*t+1,(a+b)/2+1,b);
        if(v[2*t]>v[2*t+1])
            v[t]=v[2*t];
        else
            v[t]=v[2*t+1];
    }
}
int main()
{
    int p,i,l;
    f>>n>>p;
    maxi=0;
    build(1,1,n);
    for(i=1;i<=p;i++)
    {
        f>>l>>x>>y;
        maxi=0;
        if(l==0)
        {
            m(1,1,n);
            g<<maxi<<'\n';
        }
        else
            update(1,1,n);
            int k=1;
            /*while(v[k]!=0)
            {
                cout<<v[k]<<" ";k++;
            }
            cout<<endl;*/
    }
    return 0;
}