86852024-01-25 10:56:50qertendBináris fa magassága (50 pont)cpp17Time limit exceeded 0/50600ms5652 KiB
#include <iostream>
using namespace std;
#define ll long long

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

struct Node {
    int distanceUp;
    ll endNodesStart;
    ll 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;
    ll layerDif =power(2, treeDepth-layer);
    out.endNodesStart = ID*layerDif;
    out.endNodesEnd = ID*layerDif + layerDif-1;
    return out;
}
/**
 * adds the change to all 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;
    }
}

ll max(Node array[], int start, int end) {
    ll 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;
    ll longestBranch = 0;
    ll arrayLength = power(2, treeDepth)-1;
    ll 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";
    }
}

/**
 * njudge runtime error
*/
SubtaskSumTestVerdictTimeMemory
base0/50
1Accepted0/03ms1748 KiB
2Time limit exceeded0/0600ms3196 KiB
3Wrong answer0/24ms2244 KiB
4Wrong answer0/24ms2456 KiB
5Wrong answer0/24ms2536 KiB
6Wrong answer0/24ms2628 KiB
7Wrong answer0/34ms3024 KiB
8Wrong answer0/34ms3060 KiB
9Wrong answer0/34ms3280 KiB
10Wrong answer0/34ms3488 KiB
11Time limit exceeded0/2538ms4836 KiB
12Time limit exceeded0/2541ms4960 KiB
13Time limit exceeded0/2569ms5032 KiB
14Time limit exceeded0/2574ms5144 KiB
15Time limit exceeded0/2550ms4988 KiB
16Time limit exceeded0/2582ms5248 KiB
17Time limit exceeded0/2565ms5252 KiB
18Time limit exceeded0/2558ms5264 KiB
19Time limit exceeded0/2558ms5440 KiB
20Time limit exceeded0/3569ms5448 KiB
21Time limit exceeded0/3574ms5652 KiB
22Time limit exceeded0/3574ms5468 KiB
23Time limit exceeded0/3570ms5416 KiB