87572024-01-29 08:01:07nietzsche1Bináris fa magassága (50 pont)cpp17Time limit exceeded 20/50600ms14312 KiB
#include <iostream>
#include <math.h>
#include <vector>

struct Elem
{
    Elem() {}
    Elem( unsigned int _magassag ) : magassag( _magassag ) {}
    unsigned int magassag = 0;
    unsigned int tav = 1;
    int szulo = -1;
    int jobb = -1;
    int bal = -1;
    int MilyenMagas( int ujtav, std::vector<Elem*> elemek )
    {
        if ( ujtav != -1 ) [[unlikely]]
        {
            tav = ujtav;
            if ( szulo != -1 ) [[unlikely]]
                return elemek.at( szulo )->MilyenMagas( -1, elemek );
            else
                throw 42;
        }
        else
        {
            int balmagassag = elemek.at( bal )->magassag + elemek.at( bal )->tav;
            int jobbmagassag = elemek.at( jobb )->magassag + elemek.at( jobb )->tav;
            if ( balmagassag > jobbmagassag )
                magassag = balmagassag;
            else
                magassag = jobbmagassag;
            if ( szulo != -1 ) [[unlikely]]
                return elemek.at( szulo )->MilyenMagas( -1, elemek );
            else
                return magassag;
        }
    }
};

int main()
{
    int szintekSzama, muveletekSzama;
    std::cin >> szintekSzama >> muveletekSzama;

    std::vector<Elem*> elemek;
    for ( int i = 0; i < szintekSzama; i++ )
        for ( int j = 0; j < powl( 2, i ); j++ )
        {
            if ( ! elemek.empty() )
            {
                elemek.push_back( new Elem( szintekSzama - i - 1 ) );
                elemek.back()->szulo = powl( 2, i - 1 ) + ( j >> 1 ) - 1;
                if ( j & 1 == 1 )
                    elemek.at( elemek.back()->szulo )->jobb = elemek.size() - 1;
                else
                    elemek.at( elemek.back()->szulo )->bal = elemek.size() - 1;
            }
            else
            {
                elemek.push_back( new Elem( szintekSzama - i - 1 ) );
            }
        }
    
    int sorszam, ujtav;
    std::vector<int> megoldasok;
    for ( int i = 0; i < muveletekSzama; i++ )
    {
        std::cin >> sorszam >> ujtav;
        megoldasok.push_back( elemek.at( sorszam-1 )->MilyenMagas( ujtav, elemek ) );
    }

    for ( int i : megoldasok )
        std::cout << i << std::endl;
    for ( Elem* i : elemek )
        delete i;
    
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base20/50
1Accepted0/03ms1960 KiB
2Time limit exceeded0/0600ms12320 KiB
3Accepted2/24ms2340 KiB
4Accepted2/24ms2472 KiB
5Accepted2/24ms2584 KiB
6Accepted2/24ms2800 KiB
7Accepted3/34ms2888 KiB
8Accepted3/34ms3024 KiB
9Accepted3/36ms3256 KiB
10Accepted3/34ms3120 KiB
11Time limit exceeded0/2600ms13384 KiB
12Time limit exceeded0/2566ms13704 KiB
13Time limit exceeded0/2565ms13652 KiB
14Time limit exceeded0/2570ms13848 KiB
15Time limit exceeded0/2578ms14136 KiB
16Time limit exceeded0/2546ms14100 KiB
17Time limit exceeded0/2546ms14132 KiB
18Time limit exceeded0/2555ms14312 KiB
19Time limit exceeded0/2578ms14232 KiB
20Time limit exceeded0/3550ms14172 KiB
21Time limit exceeded0/3583ms14272 KiB
22Time limit exceeded0/3566ms14160 KiB
23Time limit exceeded0/3574ms14224 KiB