#include <iostream>
#include <map>
#include <vector>
#include <unordered_set>
using namespace std;
struct Node{
int x;
int s;
int e;
int m;
Node* l;
Node* r;
Node(int X, int S, int E, int M, Node* L = NULL, Node* R = NULL): x(X), s(S), e(E), m(M), l(L), r(R) {}
};
struct Booking{
int s;
int e;
int car;
Booking(int i, int S, int E): car(i), s(S), e(E) {}
};
class Register{
private:
Node *head;
multimap<int,int> starts;
multimap<int,int> ends;
vector<Booking> v;
void intersect(Node *node, int x, unordered_set<int> &cars){
if(!node) return;
if(node->m<=x) return intersect(node->r,x,cars);
if(node->s>x) return intersect(node->l,x,cars);
if(node->e>x) cars.insert(v[node->x].car);
intersect(node->l,x,cars);
intersect(node->r,x,cars);
}
Node* insert(Node* &head, Node* node){
if(!head) return head = node;
if(head->s<=node->s) return insert(head->r,node);
head->m = max(head->m, node->m);
return insert(head->l,node);
}
public:
unordered_set<int> getBookedCars(int s, int e){
unordered_set<int> cars;
for(auto it = starts.lower_bound(s); it!=starts.end()&&it->first<e; it++)
cars.insert(v[it->second].car);
for(auto it = ends.upper_bound(s); it!=ends.end()&&it->first<=e; it++)
cars.insert(v[it->second].car);
intersect(head,s,cars);
return cars;
}
void addBooking(int car, int s, int e){
int i = v.size();
v.push_back(Booking(car,s,e));
starts.insert({s,i});
ends.insert({e,i});
Node* node = new Node(i,s,e,e);
insert(head, node);
}
};
int main() {
// your code goes here
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8dW5vcmRlcmVkX3NldD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IE5vZGV7CglpbnQgeDsKCWludCBzOwoJaW50IGU7CglpbnQgbTsKCU5vZGUqIGw7CglOb2RlKiByOwoJTm9kZShpbnQgWCwgaW50IFMsIGludCBFLCBpbnQgTSwgTm9kZSogTCA9IE5VTEwsIE5vZGUqIFIgPSBOVUxMKTogeChYKSwgcyhTKSwgZ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