0981.cpp (737B)
1 class TimeMap { 2 unordered_map<string, vector<pair<int, string>>> um; 3 4 public: 5 void set(const string &key, const string &value, int timestamp) { 6 um[key].emplace_back(timestamp, value); 7 } 8 9 string get(const string &key, int timestamp) { 10 const auto &vec = um[key]; 11 int low = 0, high = size(vec) - 1; 12 string res = ""; 13 14 while (low <= high) { 15 const auto mid = low + (high - low) / 2; 16 17 if (vec[mid].first == timestamp) return vec[mid].second; 18 if (vec[mid].first >= timestamp) 19 high = mid - 1; 20 else { 21 res = vec[mid].second; 22 low = mid + 1; 23 } 24 } 25 26 return res; 27 } 28 };