#include #include #include #include using namespace std; set s; void insert(int key) { s.insert(key); } void remove(int key) { s.erase(key); } int closest(int key) { assert(s.begin() != s.end()); auto it = lower_bound(s.begin(), s.end(), key); if (it == s.end()) return *(--it); if (it == s.begin()) return *it; int a = *it; int b = *(--it); if (abs(a - key) < abs(b - key)) { return a; } return b; }