[ C++ ] [ Using Map and Upper Bound ]
Code\n\nclass SnapshotArray {\nprivate:\n    map<int, map<int, int>> snaps;\n    int snapId = 0;\npublic:\n    SnapshotArray(int length) {\n        for (int i =
# Code\n```\nclass SnapshotArray {\nprivate:\n    map<int, map<int, int>> snaps;\n    int snapId = 0;\npublic:\n    SnapshotArray(int length) {\n        for (int i = 0; i < length; i++) {\n            map<int, int> mp; mp[0] = 0;\n            snaps[i] = mp;\n        }\n    }\n    \n    void set(int index, int val) {\n        snaps[index][snapId] = val;\n    }\n    \n    int snap() {\n        return snapId++;\n    }\n    \n    int get(int index, int snap_id) {\n        auto it = snaps[index].upper_bound(snap_id); it--;\n        return it->second;\n    }\n};\n/**\n * Your SnapshotArray object will be instantiated and called as such:\n * SnapshotArray* obj = new SnapshotArray(length);\n * obj->set(index,val);\n * int param_2 = obj->snap();\n * int param_3 = obj->get(index,snap_id);\n */\n```
['C++']