85812024-01-22 12:24:00qertendBináris fa magassága (50 pont)cpp17Runtime error 0/504ms5776 KiB
#include <iostream>
using namespace std;

int power(int base, int exponent) {
    int out = base;
    while (exponent > 1) {
        out *= base;
        exponent--;
    }
    return out;
}

struct Node {
    int distanceUp;
    int endNodesStart;
    int endNodesEnd;
    int depth;
};

Node newNode(int ID, int treeDepth) {
    int layer = 1;
    float tmp = ID;
    while (tmp >= 2) {
        tmp /= 2;
        layer++;
    }
    Node out;
    out.distanceUp = 1;
    int layerDif =power(2, treeDepth-layer);
    out.endNodesStart = ID*layerDif;
    out.endNodesEnd = ID*layerDif + layerDif-1;
    return out;
}

void updateDepth(int change, Node array[], int start, int end) {
    for (int i = start; i <= end; i++) {
        array[i].depth += change;
    }
}

int max(Node array[], int start, int end) {
    int max = 0;
    for (int i = start; i <= end; i++) {
        if (array[i].depth > max) max = array[i].depth;
    }
    return max;
}

int main() {
    int treeDepth, M;
    cin >> treeDepth;
    cin >> M;
    int longestBranch = 0;
    int arrayLength = power(2, treeDepth)-1;
    int endNodesStart = power(2, treeDepth-1);
    Node allNodes[arrayLength];
    //create Nodes
    for (int i = 0; i < arrayLength; i++) {
        allNodes[i] = newNode(i+1, treeDepth);
    }
    //set default length for end Nodes
    for (int i = endNodesStart; i < arrayLength; i++) {
        allNodes[i].depth = treeDepth-1;
    }
    //read instructions
    for (int i = 0; i < M; i++) {
        int newDistance, nodeID;
        cin >> nodeID;
        nodeID--;
        cin >> newDistance;
        Node &modifiedNode = allNodes[nodeID];
        int change = newDistance-modifiedNode.distanceUp; 
        modifiedNode.distanceUp = newDistance;
        updateDepth(change, allNodes, modifiedNode.endNodesStart, modifiedNode.endNodesEnd);

        if (change > 0) {
            int currentHeight = max(allNodes, modifiedNode.endNodesStart, modifiedNode.endNodesEnd);
            if (currentHeight > longestBranch) longestBranch = currentHeight;
        }
        else if (change < 0) {
            longestBranch = max(allNodes, endNodesStart, arrayLength-1);
        }
        cout << longestBranch << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
base0/50
1Runtime error0/03ms2132 KiB
2Runtime error0/04ms4212 KiB
3Runtime error0/23ms2432 KiB
4Runtime error0/23ms2572 KiB
5Runtime error0/23ms2788 KiB
6Runtime error0/23ms2732 KiB
7Runtime error0/33ms3016 KiB
8Runtime error0/33ms3000 KiB
9Runtime error0/33ms2964 KiB
10Runtime error0/33ms2964 KiB
11Runtime error0/24ms5000 KiB
12Runtime error0/24ms5148 KiB
13Runtime error0/24ms5200 KiB
14Runtime error0/24ms5132 KiB
15Runtime error0/24ms5392 KiB
16Runtime error0/24ms5336 KiB
17Runtime error0/24ms5612 KiB
18Runtime error0/24ms5352 KiB
19Runtime error0/24ms5348 KiB
20Runtime error0/34ms5604 KiB
21Runtime error0/34ms5620 KiB
22Runtime error0/34ms5776 KiB
23Runtime error0/34ms5628 KiB