Segment Tree + binary search longest | O(1) time build, upload, O(logn) time logest(), O(n) space.
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass LUPrefix {\n    vector<int> seg{};\n    int n{0};\n    void upd(int i, int l, int h, int ind) {\n        if (l == h) {\n            // assert(i>=0 && i<=4*n + 4);\n            seg[i] = 1;\n            // cout << "seg[" << i << "] - " << l << " aka " << ind << " -> 1" << endl;\n            return;\n        }\n        int m = (l + h) / 2;\n        if (ind <= m) {\n            upd(i * 2, l, m, ind);\n        } else {\n            upd(i * 2 + 1, m + 1, h, ind);\n        }\n        seg[i] = seg[i * 2] + seg[i * 2 + 1];\n        // cout << "seg[" << i << "] - [" << l << "," << h << "] -> " << seg[i] << endl;\n    }\n    int qpref(int i, int l, int h) {\n        if (seg[i] == h - l + 1) {\n            return seg[i];\n        }\n        if (l == h) {\n            // cout << "ans pref - seg[" << i << "] - [" << l <<  "," << h << "] = " << seg[i] << endl;\n            return seg[i];\n        }\n        int m = (l + h) / 2;\n        int ans = qpref(i * 2, l, m);\n        if (ans == m - l + 1) {\n            // how to know 1,2,..,n all uploaded? total is n, aka n - 1 + 1;\n            ans += qpref(i * 2 + 1, m + 1, h);\n        }\n        // cout << "ans pref - [" << l << "," << h << "] = " << ans << endl;\n        return ans;\n    }\n    // int qpref(int i, int l, int h) {\n    //     if (l == h) {\n    //         // cout << "ans pref - seg[" << i << "] - [" << l <<  "," << h << "] = " << seg[i] << endl;\n    //         return seg[i];\n    //     }\n    //     int m = (l + h) / 2;\n    //     int ans = qpref(i * 2, l, m);\n    //     if (ans == m - l + 1) {\n    //         // how to know 1,2,..,n all uploaded? total is n, aka n - 1 + 1;\n    //         ans += qpref(i * 2 + 1, m + 1, h);\n    //     }\n    //     // cout << "ans pref - [" << l << "," << h << "] = " << ans << endl;\n    //     return ans;\n    // }\npublic:\n    LUPrefix(int n) {\n        seg.resize(4 * n, 0);\n        // upd(0, 0, n-1, 0);\n        this->n = n;\n    }\n\n    void upload(int video) { \n        assert(n != 0);\n        upd(1, 1, n, video); }\n\n    int longest() { \n        assert(n != 0);\n        return qpref(1, 1, n); \n    }\n};\n\n/**\n * Your LUPrefix object will be instantiated and called as such:\n * LUPrefix* obj = new LUPrefix(n);\n * obj->upload(video);\n * int param_2 = obj->longest();\n */\n```
['C++']