87162024-01-26 12:55:43qertendBináris fa magassága (50 pont)cpp17Futási hiba 0/50569ms6236 KiB
#include <iostream>
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 = power(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 = power(2, treeDepth)-1;
    int endNodesStart = power(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
*/
RészfeladatÖsszpontTesztVerdiktIdőMemória
base0/50
1Elfogadva0/03ms1688 KiB
2Futási hiba0/04ms4076 KiB
3Futási hiba0/23ms2292 KiB
4Futási hiba0/23ms2432 KiB
5Futási hiba0/23ms2644 KiB
6Futási hiba0/23ms2800 KiB
7Futási hiba0/33ms3012 KiB
8Futási hiba0/33ms3096 KiB
9Futási hiba0/33ms3356 KiB
10Futási hiba0/33ms3500 KiB
11Futási hiba0/228ms5484 KiB
12Futási hiba0/220ms5584 KiB
13Futási hiba0/229ms5880 KiB
14Időlimit túllépés0/2569ms3996 KiB
15Futási hiba0/219ms5908 KiB
16Futási hiba0/24ms5652 KiB
17Futási hiba0/24ms5712 KiB
18Futási hiba0/24ms5652 KiB
19Futási hiba0/24ms5652 KiB
20Futási hiba0/320ms5912 KiB
21Futási hiba0/320ms5964 KiB
22Futási hiba0/328ms6236 KiB
23Futási hiba0/320ms6144 KiB