Submission details
Task:Frog and flies
Sender:aalto26hw_002
Submission time:2026-10-28 18:58:19 +0200
Language:C++ (C++20)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:53:32: error: 'log2' was not declared in this scope
   53 |     int elems = 1 << (int)ceil(log2(n));
      |                                ^~~~
input/code.cpp:53:27: error: 'ceil' was not declared in this scope
   53 |     int elems = 1 << (int)ceil(log2(n));
      |                           ^~~~

Code

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int parent(int node) {
    return node / 2;
}

int left(int node) {
    return 2 * node;
}

int right(int node) {
    return 2 * node + 1;
}

ll getMax(int a, int b, vector<ll>& arr) {
    int n = arr.size() / 2;
    a += n; b += n;
    ll maxVal = 0;
    while (a <= b) {
        if (a%2 == 1) {
            maxVal = max(maxVal, arr[a]);
            a++;
        }
        if (b%2 == 0) {
            maxVal = max(maxVal, arr[b]);
            b--;
        }
        a = parent(a);
        b = parent(b);
    }
    return maxVal;
}

void setVal(int ind, int val, vector<ll>& arr) {
    int n = arr.size() / 2;
    ind += n;
    arr[ind] = val;
    for (ind /= 2; ind >= 1; ind /= 2) {
        arr[ind] = max(arr[left(ind)], arr[right(ind)]);
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    int n;
    cin >> n;

    int elems = 1 << (int)ceil(log2(n));
    vector<ll> tree(2*elems, 0);

    // min, max
    vector<pair<int, int>> adj(elems);

    ll a, b, c;
    for (ll i=0; i<n; i++) {
        cin >> a >> b >> c;
        b--; c--;
        tree[i + elems] = a;
        adj[i] = pair<int, int>(max(i+1, b), c);
    }

    for (int i=elems-1; i>0; i--) {
        tree[i] = max(tree[left(i)], tree[right(i)]);
    }

    ll best = 0;
    for (int i=n-1; i>=0; i--) {
        int left = adj[i].first;
        int right = adj[i].second;
        if (left > right) {
            continue;
        }
        ll tmp_best = tree[i + elems] + getMax(adj[i].first, adj[i].second, tree);
        setVal(i, max(tmp_best, tree[i + elems]), tree);
        best = max(best, tmp_best);
    }

    cout << best << endl;

    return 0;
}