87152024-01-26 12:52:41qertendBináris fa magassága (50 pont)cpp17Runtime error 0/50566ms6524 KiB
#include <iostream>
#include <cmath>
using namespace std;

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

class Node {
    public:
        int distanceUp;
        int endNodesStart;
        int endNodesEnd;
        int depth;

        void init(int ID, int treeDepth) {
            int layer = 1;
            float tmp = ID;
            while (tmp >= 2) {
                tmp /= 2;
                layer++;
            }
            distanceUp = 1;
            int layerDif = pow(2, treeDepth-layer);
            endNodesStart = ID*layerDif;
            endNodesEnd = ID*layerDif + layerDif-1;
        }
};

/**
 * adds the change to aint nodes from start to end (inclusive)
*/
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;
    int ID;
    for (int i = start; i <= end; i++) {
        if (array[i].depth > max) {
            max = array[i].depth;
            ID = i;
        }
    }
    return max;
}

int main() {
    int treeDepth, M;
    cin >> treeDepth;
    cin >> M;
    int longestBranch = 0;
    int arrayLength = pow(2, treeDepth)-1;
    int endNodesStart = pow(2, treeDepth-1);
    //cerr << "before array\n";
    Node allNodes[arrayLength];
    //cerr << "after array\n";
    //create Nodes
    for (int i = 0; i < arrayLength; i++) {
        allNodes[i].init(i+1, treeDepth);
    }
    //cerr << "created nodes\n";
    //set default length for end Nodes
    for (int i = endNodesStart; i < arrayLength; i++) {
        allNodes[i].depth = treeDepth-1;
    }
    //cerr << "set default lengths\n";
    //read instructions from stdin
    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);
        }
        ////cerr << i << "/" << M << "\r";
        cout << to_string(longestBranch) + "\n";
    }
}

/**
 * njudge wrong answer || time limit hit
 * 
 * => allNodes fails to initialize
*/
SubtaskSumTestVerdictTimeMemory
base0/50
1Accepted0/03ms1812 KiB
2Runtime error0/06ms4148 KiB
3Runtime error0/23ms2372 KiB
4Runtime error0/23ms2572 KiB
5Runtime error0/23ms2656 KiB
6Runtime error0/23ms2632 KiB
7Runtime error0/33ms2992 KiB
8Runtime error0/33ms3084 KiB
9Runtime error0/33ms3272 KiB
10Runtime error0/33ms3492 KiB
11Runtime error0/225ms5620 KiB
12Runtime error0/221ms5628 KiB
13Runtime error0/221ms5896 KiB
14Time limit exceeded0/2566ms4152 KiB
15Runtime error0/230ms6016 KiB
16Runtime error0/27ms6160 KiB
17Runtime error0/26ms6424 KiB
18Runtime error0/26ms6252 KiB
19Runtime error0/26ms6252 KiB
20Runtime error0/321ms6252 KiB
21Runtime error0/323ms6524 KiB
22Runtime error0/321ms6484 KiB
23Runtime error0/328ms6464 KiB