85742024-01-22 11:48:58tamasmarkBináris fa magassága (50 pont)cpp17Wrong answer 21/50144ms95368 KiB
// Binarisfa2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <deque>
#include <cmath>
#include <algorithm>

using namespace std;

struct adat {
    int hossz=1,sum,lat;
    deque<int>bin;
};
deque<int>binaris;
deque<adat>x;
int a, b,maxi,db,n,m,szint;
void atalakit(int a)
{
    while (a > 0)
    {
        binaris.push_front(a % 2);
        a = a / 2;
    }
}

void melysegi(int csp)
{
    if (csp * 2 + 1 <= pow(2, n) - 1 && csp * 2 <= pow(2, n) - 1)
    {
        x[csp].sum = max(x[csp * 2].sum + x[csp * 2].hossz, x[(csp * 2) + 1].sum + x[(csp * 2) + 1].hossz);
    }
    if (binaris.size() > 1)
    {
        if (binaris.back() == 1)
        {
            binaris.pop_back();
            melysegi((csp - 1) / 2);
        }
        else if (binaris.back() == 0)
        {
            binaris.pop_back();
            melysegi(csp / 2);
        }

    }
}
void melysegib(int csp)
{
    x[csp].lat = true;
    szint++;
    x[csp].sum = n - szint;
    
    if (csp * 2 <= pow(2, n) - 1)
    {
        melysegi(csp * 2);
        szint--;
    }
    if (csp * 2 + 1 <= pow(2, n) - 1)
    {
        melysegi(csp * 2 + 1);
        szint--;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    x.resize(pow(2, n)); 
    melysegib(1);

    for (int i = 1; i <= m; ++i)
    {
        cin >> a >> b;
        x[a].hossz = b;
        atalakit(a);
        melysegi(a);
        binaris.clear();
        cout << x[1].sum << "\n";
    }
    return 0;
}
/*
3 5
2 6
4 3
6 7
5 4
2 1
*/
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
SubtaskSumTestVerdictTimeMemory
base21/50
1Accepted0/03ms2012 KiB
2Wrong answer0/0116ms92784 KiB
3Accepted2/23ms2260 KiB
4Accepted2/23ms2336 KiB
5Accepted2/23ms2608 KiB
6Accepted2/23ms2556 KiB
7Wrong answer0/33ms2848 KiB
8Accepted3/33ms3104 KiB
9Wrong answer0/34ms3532 KiB
10Wrong answer0/33ms3492 KiB
11Accepted2/2143ms94284 KiB
12Accepted2/2136ms94368 KiB
13Accepted2/2129ms94244 KiB
14Accepted2/2127ms94452 KiB
15Accepted2/2144ms94700 KiB
16Wrong answer0/2101ms94464 KiB
17Wrong answer0/2105ms94724 KiB
18Wrong answer0/2101ms95044 KiB
19Wrong answer0/2115ms95052 KiB
20Wrong answer0/3120ms94852 KiB
21Wrong answer0/3119ms95256 KiB
22Wrong answer0/3120ms95252 KiB
23Wrong answer0/3119ms95368 KiB