Cod sursa(job #3157138)

Utilizator proflaurianPanaete Adrian proflaurian Data 14 octombrie 2023 14:26:48
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.28 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("arbint.in");
ofstream g("arbint.out");

int n, p = 1, q , a, b, valMax(int, int, int );
void upd(int ,int );
vector<int> A;
int main()
{
    f >> n >> q;
    while(p < n)
        p *= 2;
    A = vector<int>(2*p, 0);
    for(int i = 1; i <= n; i++)
        f>>A[p+i-1];
    for(int i = p - 1; i >= 1; i--)
        A[i] = max(A[2*i], A[2*i + 1]);
    for(int i = 1; i <= q; i++)
    {
        int t;
        f >> t >> a >> b;
        if( t == 0)
            g << valMax( 1, 1, p) << '\n';
        else
            upd( a + p - 1 , b );
    }
    return 0;
}

int valMax(int nod, int x, int y)
{
    if(a <= x && y <= b) /// daca [x,y] inclus in [a,b]     [a   [x     y]    b]
        return A[nod]; /// returneaza maximul de pe intervalul [x,y]

    if( y < a || b < x) /// daca [x,y] nu se intersescteaza cu [a,b] 1: [x y] [a b] 2: [a b ] [x y]
        return 0; /// returneaza -oo
    int z= (x + y) / 2; ///
    /// [ x               y ]
    /// [ x   z  ] [ z+1  y ]
    /// de exemplu pentru x = 9 , y = 16 (interval cu 9 elemente)
    /// z=(9+16)/2=12 si cele doua intervale subordonate de lungime 4 se obtina astfel >
    /// [ 9              16 ]
    /// [ 9  12  ] [  13 16 ]

    return max ( valMax( 2*nod , x, z) , valMax( 2*nod + 1, z + 1, y ));

}

void upd( int poz, int val)
{
    A[poz]=val;
    poz /= 2;
    while( poz >  0)
    {
        A[poz] = max( A[2*poz], A[2*poz + 1]);
        poz /= 2;
    }
}

//                                                ()
//
//
//
//                                               (1)6[1,8]*
//
//
//
//
//                                      /                           \
//
//
//
//                      (2)6[1,4]*                                                  (3)1[5,8]
//
//             /                     \                                     /                         \
//
//      (4)4[1,2]                     (5)6[3,4]*                      (6)1[5,6]                       (7)0[8,7]
//
//      /      \                       /      \                       /       \                      /        \
//
//(8)4[1,1]     (9)3[2,2]      (10)5[3,3]*    (11)6[4,4]      (12)1[5,5]      (13)0[6,6]      (14)0[7,7]     (15)0[8,8]
//