89592024-02-08 02:07:23qertendBináris fa magassága (50 pont)cpp17Time limit exceeded 0/50600ms5812 KiB
#include <iostream>
using namespace std;

long powerOf2(int exponent) {
    long out = 1;
    out <<= exponent;
    return out;
}

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

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

/**
 * adds the change to end 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, long start, long end) {
    int max = 0;
    int ID;
    for (long 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;
    long arrayLength = powerOf2(treeDepth)-1;
    long endNodesStart = powerOf2(treeDepth-1);
    //cerr << "before array\n";
    Node allNodes[arrayLength];
    //cerr << "after array\n";
    //create Nodes
    for (long i = 0; i < arrayLength; i++) {
        allNodes[i].init(i+1, treeDepth);
    }
    //cerr << "created nodes\n";
    //set default length for end Nodes
    for (long 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) {
            long 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";
        //cerr << i << "/" << M << "\r";
    }
}

/**
 * Node array[] as function parameter not an array??
*/
SubtaskSumTestVerdictTimeMemory
base0/50
1Accepted0/03ms1872 KiB
2Time limit exceeded0/0600ms3392 KiB
3Wrong answer0/24ms2368 KiB
4Wrong answer0/24ms2524 KiB
5Wrong answer0/24ms2700 KiB
6Wrong answer0/24ms2912 KiB
7Wrong answer0/34ms3132 KiB
8Wrong answer0/34ms3340 KiB
9Wrong answer0/34ms3428 KiB
10Wrong answer0/34ms3436 KiB
11Time limit exceeded0/2600ms4748 KiB
12Time limit exceeded0/2550ms4940 KiB
13Time limit exceeded0/2570ms5020 KiB
14Time limit exceeded0/2582ms5228 KiB
15Time limit exceeded0/2531ms5164 KiB
16Time limit exceeded0/2541ms5360 KiB
17Time limit exceeded0/2570ms5364 KiB
18Time limit exceeded0/2565ms5580 KiB
19Time limit exceeded0/2569ms5608 KiB
20Time limit exceeded0/3570ms5576 KiB
21Time limit exceeded0/3565ms5748 KiB
22Time limit exceeded0/3554ms5812 KiB
23Time limit exceeded0/3550ms5700 KiB