-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathimplement-router.cpp
48 lines (42 loc) · 1.32 KB
/
implement-router.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Time: ctor: O(1)
// addPacket: O(logn)
// forwardPacket: O(logn)
// getCount: O(logn)
// Space: O(n)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// queue, ordered set
class Router {
public:
Router(int memoryLimit) : size_(memoryLimit) {
}
bool addPacket(int src, int dest, int timestamp) {
if (!lookup_[dest].insert({timestamp, src}).second) {
return false;
}
if (size(q_) == size_){
const auto [s, d, t] = q_.front(); q_.pop();
lookup_[d].erase({t, s});
}
q_.emplace(src, dest, timestamp);
return true;
}
vector<int> forwardPacket() {
if (empty(q_)) {
return {};
}
const auto [s, d, t] = q_.front(); q_.pop();
lookup_[d].erase({t, s});
return {s, d, t};
}
int getCount(int dest, int startTime, int endTime) {
return lookup_[dest].order_of_key({endTime + 1, 0}) -
lookup_[dest].order_of_key({startTime, 0});
}
private:
using ordered_set = tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update>;
queue<tuple<int, int, int>> q_;
unordered_map<int, ordered_set> lookup_;
int size_;
};