192732025-12-03 16:07:22szabelrBináris fa magassága (50 pont)cpp17Elfogadva 50/5017ms2036 KiB
#include <iostream>
#include <algorithm> // For std::max
#include <vector>

using namespace std;

// Using a slightly larger size to be safe, or use vector based on N
const int MAX_SIZE = 131072; // 2^17
int dist[MAX_SIZE];
int best[MAX_SIZE];

void check(int x) {
    // We don't need to pass arrays if they are global
    while (x > 0) {
        int new_val = max(dist[2 * x] + best[2 * x], dist[2 * x + 1] + best[2 * x + 1]);

        // Pruning optimization: 
        // If the value hasn't changed, ancestors won't change either.
        if (best[x] == new_val) break;

        best[x] = new_val;
        x >>= 1; // Bitwise right shift (same as x = x / 2) but faster
    }
}

int main() {
    // 1. Fast I/O
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    if (!(cin >> n >> m)) return 0;

    int limit = 1 << n; // 2. Bitwise shift for 2^n (O(1) calculation)

    // Initialize arrays
    // We can calculate initial 'best' values bottom-up efficiently
    // But keeping your logic structure for clarity:

    // Fill leaf nodes and dist
    for (int i = 0; i < limit * 2; i++) {
        dist[i] = 1;
        best[i] = 0; // Initialize 0 properly
    }

    // Your logic for initializing 'best' based on depth
    // (Optimized slightly to avoid pow inside loop)
    int i = 1;
    int x = n - 1;
    int next_power_of_2 = 2;

    while (i < limit) {
        if (i == next_power_of_2) {
            x--;
            next_power_of_2 <<= 1; // Multiply by 2
        }
        best[i] = x;
        i++;
    }

    int b, c;
    for (int k = 0; k < m; k++) {
        cin >> b >> c;
        dist[b] = c;

        check(b >> 1); // b >> 1 is b/2

        // 3. Use \n instead of endl
        cout << best[1] << "\n";
    }

    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/017ms1516 KiB
3Elfogadva2/21ms508 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva2/21ms316 KiB
6Elfogadva2/21ms316 KiB
7Elfogadva3/31ms440 KiB
8Elfogadva3/31ms500 KiB
9Elfogadva3/31ms316 KiB
10Elfogadva3/31ms500 KiB
11Elfogadva2/217ms1564 KiB
12Elfogadva2/217ms1564 KiB
13Elfogadva2/217ms1572 KiB
14Elfogadva2/217ms1556 KiB
15Elfogadva2/217ms1788 KiB
16Elfogadva2/217ms1588 KiB
17Elfogadva2/217ms1580 KiB
18Elfogadva2/217ms1588 KiB
19Elfogadva2/217ms1588 KiB
20Elfogadva3/317ms1588 KiB
21Elfogadva3/317ms2036 KiB
22Elfogadva3/317ms1588 KiB
23Elfogadva3/317ms1588 KiB