IndexSet
概要
計算量
insert()
: O(1)
erase()
: O(1)
count()
: O(1)
clear()
: O(1)
size()
: O(1)
items()
: O(1)
コード
class IndexSet {
int N;
vector<int> v, pos;
public:
IndexSet(int N) : N(N), pos(N, -1) {
}
void insert(int x) {
assert(0 <= x && x < N);
if (pos[x] != -1) return;
pos[x] = v.size();
v.push_back(x);
}
void erase(int x) {
assert(0 <= x && x < N);
if (pos[x] == -1) return;
int p = pos[x];
int b = v.back();
v[p] = b;
v.pop_back();
pos[b] = p;
pos[x] = -1;
}
int count(int x) {
assert(0 <= x && x < N);
if (pos[x] == -1) return 0;
return 1;
}
void clear() {
v.clear();
pos.assign(N, -1);
}
size_t size() const {
return v.size();
}
const vector<int>& items() const {
return v;
}
};
Verified