89612024-02-08 03:13:37qertendBináris fa magassága (50 pont)cpp17Time limit exceeded 9/50600ms5604 KiB
#include <iostream>
using namespace std;

long arrayLength;

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;
            if (endNodesEnd == arrayLength) endNodesEnd--;
        }
};

/**
 * 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++) {
        //that extra condition is ugly af but it works
        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;
    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++) {
        if (i == 12) {
            cerr << "watch out\n";
        }
        int newDistance, nodeID;
        cin >> nodeID;
        nodeID--;
        cin >> newDistance;
        Node &modifiedNode = allNodes[nodeID];
        int change = newDistance-modifiedNode.distanceUp; 
        modifiedNode.distanceUp = newDistance;
        if (change != 0) 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
base9/50
1Accepted0/03ms1876 KiB
2Time limit exceeded0/0561ms3540 KiB
3Wrong answer0/24ms2256 KiB
4Wrong answer0/24ms2472 KiB
5Wrong answer0/24ms2680 KiB
6Wrong answer0/24ms2772 KiB
7Accepted3/34ms2964 KiB
8Accepted3/34ms3072 KiB
9Accepted3/34ms3072 KiB
10Wrong answer0/34ms3076 KiB
11Time limit exceeded0/2600ms4452 KiB
12Time limit exceeded0/2555ms4708 KiB
13Time limit exceeded0/2574ms4900 KiB
14Time limit exceeded0/2578ms5168 KiB
15Time limit exceeded0/2569ms5228 KiB
16Time limit exceeded0/2554ms5312 KiB
17Time limit exceeded0/2570ms5352 KiB
18Time limit exceeded0/2550ms5344 KiB
19Time limit exceeded0/2564ms5416 KiB
20Time limit exceeded0/3554ms5352 KiB
21Time limit exceeded0/3555ms5324 KiB
22Time limit exceeded0/3554ms5604 KiB
23Time limit exceeded0/3600ms5356 KiB