Cod sursa(job #2029970)

Utilizator MarinPeptenaruMarin Vasile Peptenaru MarinPeptenaru Data 30 septembrie 2017 18:52:46
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.64 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("arbint.in");
ofstream out("arbint.out");
int arbore[400002], v[100002];
int n,m,q,x,y,sol;
void build (int nod , int st, int dr) /// nodul si intervalul pe care il atribui
{
    if(st==dr)
        arbore[nod]=v[st];
    else if(st<dr)
    {
        int m=(st+dr)/2;
        int nod1=nod*2;
        int nod2=nod*2 + 1;
        build (nod*2, st, m); /// fii unui nod n sunt mereu nodurile 2*n, 2*n+1 (intr-un arbore binar)
        build (nod*2 +1, m+1, dr);
        arbore[nod]=max(arbore[nod1],arbore[nod2]);
    }
}
void change (int poz, int val, int nod, int st, int dr)
{
    if(st==dr) arbore[nod]=val;
    else
    {
        int m=(st+dr)/2;
        int nod1=nod*2;
        int nod2=nod*2 +1;
        if(poz<=m) change(poz,val,nod1,st,m);
        else change(poz,val,nod2,m+1,dr);
        arbore[nod]=max(arbore[nod1],arbore[nod2]);
    }
}
void search (int nod, int x, int y, int st, int dr)
{
    if(x==st && y==dr) sol=max(sol,arbore[nod]);
    else
    {
        int m=(st+dr)/2;
        if(x<=m)
        {
            if(y<=m) search(nod*2,x,y,st,m);
            else
            {
                search(nod*2,x,m,st,m);
                search(nod*2+1,m+1,y,m+1,dr);
            }
        }
        else search(nod*2+1,x,y,m+1,dr);
    }
}
int main()
{
    in>>n>>m;
    for(int i=1; i<=n; i++)
        in>>v[i];
    build(1,1,n);
    for(;m;m--)
    {
        in>>q>>x>>y;
        if(q==0)
        {
            sol=0;
            search(1,x,y,1,n);
            out<<sol<<'\n';
        }
        else change(x,y,1,1,n);
    }
    return 0;
}