版本比较
一种很快的块状链表
正文
增加 22 行删除 22 行
31 行未改动32333435363738394014 行未改动55565758596061626313 行未改动77787980818283848586876 行未改动9495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261276 行未改动13413513613713813914014114214314414514614735 行未改动1831841851861871881891901915 行未改动19719819920020120220320420516 行未改动2222232242252262272282292302312322332342352362372382392404 行未改动2452462472482492502512522532542552562572586 行未改动2652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972986 行未改动30530630730830931031131231331431531631731840 行未改动31 行未改动static constexpr int DEFAULT_LOAD_FACTOR = 150;int len, load;std::vector<std::vector<T>> lists;std::vector<T> maxes, index;void expand(int pos) {if ((int) lists[pos].size() > (load << 1)) {std::vector<T> half(lists[pos].begin() + load, lists[pos].end());lists[pos].erase(lists[pos].begin() + load, lists[pos].end());14 行未改动index[i] += lists[i - 1].size();if (i + (i & -i) < n) index[i + (i & -i)] += index[i];}}std::pair<int, int> pos(int idx) {if (idx < (int) lists[0].size()) return std::make_pair(0, idx);if (index.empty()) build_index();int p = 0, n = index.size();13 行未改动template <class It> sorted_vector(const It& bg, const It& ed): len(0), load(DEFAULT_LOAD_FACTOR) {extend(bg, ed);}int size() const {return len;}bool empty() const {return maxes.empty();}void clear() {len = 0, lists.clear(), maxes.clear(), index.clear();}void add(const T& val) {if (!maxes.empty()) {int pos = std::upper_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) pos--, lists[pos].emplace_back(val), maxes[pos] = val;6 行未改动template <class It> void extend(const It& bg, const It& ed) {if ((ed - bg) * 4 < len) {for (It it = bg; it != ed; it++) add(*it);return;}vector<T> a(bg, ed);for (const auto& vec : lists) a.insert(a.end(), vec.begin(), vec.end());std::sort(a.begin(), a.end());clear(), len = a.size();for (int pos = 0; pos < len; pos += load) {std::vector<T> vec(a.begin() + pos, a.begin() + std::min(len, pos + load));lists.emplace_back(vec), maxes.emplace_back(vec.back());}}bool erase(const T& val) {if (maxes.empty()) return false;int pos = std::lower_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) return false;int idx = std::lower_bound(lists[pos].begin(), lists[pos].end(), val) - lists[pos].begin();if (lists[pos][idx] != val) return false;lists[pos].erase(lists[pos].begin() + idx), len--;int n = lists[pos].size();if (n > (load >> 1)) {maxes[pos] = lists[pos].back();if (!index.empty()) {int n = index.size();for (int i = pos + 1; i < n; i += (i & -i)) index[i]--;}} else if (lists.size() > 1) {if (!pos) pos++;int pre = pos - 1;lists[pre].insert(lists[pre].end(), lists[pos].begin(), lists[pos].end());6 行未改动lists.erase(lists.begin() + pos), maxes.erase(maxes.begin() + pos);index.clear();} return true;}T operator[] (int idx) {auto pir = pos(idx);return lists[pir.first][pir.second];}int lower_bound(const T& val) {if (maxes.empty()) return 0;int pos = std::lower_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) return len;35 行未改动static constexpr int DEFAULT_LOAD_FACTOR = 340;int len, load, offset;std::vector<std::vector<T>> lists;std::vector<T> maxes, index;void expand(int pos) {if ((int) lists[pos].size() > (load << 1)) {std::vector<T> half(lists[pos].begin() + load, lists[pos].end());lists[pos].erase(lists[pos].begin() + load, lists[pos].end());5 行未改动for (int i = offset + pos; i; i = (i - 1) >> 1) index[i]++;index[0]++;}}std::vector<int> parent(const std::vector<int>& a) {int n = a.size();std::vector<int> res(n >> 1);for (int i = 0; i < (n >> 1); i++) res[i] = a[i << 1] + a[i << 1 | 1];16 行未改动while (tree.back().size() > 1) tree.emplace_back(parent(tree.back()));for (int i = tree.size() - 1; i >= 0; i--) index.insert(index.end(), tree[i].begin(), tree[i].end());offset = (dep << 1) - 1;}std::pair<int, int> pos(int idx) {if (idx < (int) lists[0].size()) return std::make_pair(0, idx);if (index.empty()) build_index();int p = 0, n = index.size();for (int i = 1; i < n; i = p << 1 | 1) {if (idx < index[i]) p = i;else idx -= index[i], p = i + 1;} return std::make_pair(p - offset, idx);}int loc(int pos, int idx) {if (pos == 0) return idx;if (index.empty()) build_index();int tot = 0;4 行未改动public:sorted_vector() : len(0), load(DEFAULT_LOAD_FACTOR), offset(0) {}int size() const {return len;}bool empty() const {return maxes.empty();}void clear() {len = 0, offset = 0;lists.clear(), maxes.clear(), index.clear();}void add(const T& val) {if (!maxes.empty()) {int pos = std::upper_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) pos--, lists[pos].emplace_back(val), maxes[pos] = val;6 行未改动template <class It> void extend(const It& bg, const It& ed) {if ((ed - bg) * 4 < len) {for (It it = bg; it != ed; it++) add(*it);return;}vector<T> a(bg, ed);for (const auto& vec : lists) a.insert(a.end(), vec.begin(), vec.end());std::sort(a.begin(), a.end());clear(), len = a.size();for (int pos = 0; pos < len; pos += load) {std::vector<T> vec(a.begin() + pos, a.begin() + std::min(len, pos + load));lists.emplace_back(vec), maxes.emplace_back(vec.back());}}bool erase(const T& val) {if (maxes.empty()) return false;int pos = std::lower_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) return false;int idx = std::lower_bound(lists[pos].begin(), lists[pos].end(), val) - lists[pos].begin();if (lists[pos][idx] != val) return false;lists[pos].erase(lists[pos].begin() + idx), len--;int n = lists[pos].size();if (n > (load >> 1)) {maxes[pos] = lists[pos].back();if (!index.empty()) {for (int i = offset + pos; i; i = (i - 1) >> 1) index[i]--;index[0]--;}} else if (lists.size() > 1) {if (!pos) pos++;int pre = pos - 1;lists[pre].insert(lists[pre].end(), lists[pos].begin(), lists[pos].end());6 行未改动lists.erase(lists.begin() + pos), maxes.erase(maxes.begin() + pos);index.clear();} return true;}T operator[] (int idx) {auto pir = pos(idx);return lists[pir.first][pir.second];}int lower_bound(const T& val) {if (maxes.empty()) return 0;int pos = std::lower_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) return len;40 行未改动31 行未改动32333435363738394014 行未改动55565758596061626313 行未改动77787980818283848586876 行未改动9495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261276 行未改动13413513613713813914014114214314414514614735 行未改动1831841851861871881891901915 行未改动19719819920020120220320420516 行未改动2222232242252262272282292302312322332342352362372382392404 行未改动2452462472482492502512522532542552562572586 行未改动2652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972986 行未改动30530630730830931031131231331431531631731840 行未改动31 行未改动static constexpr int DEFAULT_LOAD_FACTOR = 150;int len, load;std::vector<std::vector<T>> lists;std::vector<T> maxes, index;void expand(int pos) {if ((int) lists[pos].size() > (load << 1)) {std::vector<T> half(lists[pos].begin() + load, lists[pos].end());lists[pos].erase(lists[pos].begin() + load, lists[pos].end());14 行未改动index[i] += lists[i - 1].size();if (i + (i & -i) < n) index[i + (i & -i)] += index[i];}}std::pair<int, int> pos(int idx) {if (idx < (int) lists[0].size()) return std::make_pair(0, idx);if (index.empty()) build_index();int p = 0, n = index.size();13 行未改动template <class It> sorted_vector(const It& bg, const It& ed): len(0), load(DEFAULT_LOAD_FACTOR) {extend(bg, ed);}int size() const {return len;}bool empty() const {return maxes.empty();}void clear() {len = 0, lists.clear(), maxes.clear(), index.clear();}void add(const T& val) {if (!maxes.empty()) {int pos = std::upper_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) pos--, lists[pos].emplace_back(val), maxes[pos] = val;6 行未改动template <class It> void extend(const It& bg, const It& ed) {if ((ed - bg) * 4 < len) {for (It it = bg; it != ed; it++) add(*it);return;}vector<T> a(bg, ed);for (const auto& vec : lists) a.insert(a.end(), vec.begin(), vec.end());std::sort(a.begin(), a.end());clear(), len = a.size();for (int pos = 0; pos < len; pos += load) {std::vector<T> vec(a.begin() + pos, a.begin() + std::min(len, pos + load));lists.emplace_back(vec), maxes.emplace_back(vec.back());}}bool erase(const T& val) {if (maxes.empty()) return false;int pos = std::lower_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) return false;int idx = std::lower_bound(lists[pos].begin(), lists[pos].end(), val) - lists[pos].begin();if (lists[pos][idx] != val) return false;lists[pos].erase(lists[pos].begin() + idx), len--;int n = lists[pos].size();if (n > (load >> 1)) {maxes[pos] = lists[pos].back();if (!index.empty()) {int n = index.size();for (int i = pos + 1; i < n; i += (i & -i)) index[i]--;}} else if (lists.size() > 1) {if (!pos) pos++;int pre = pos - 1;lists[pre].insert(lists[pre].end(), lists[pos].begin(), lists[pos].end());6 行未改动lists.erase(lists.begin() + pos), maxes.erase(maxes.begin() + pos);index.clear();} return true;}T operator[] (int idx) {auto pir = pos(idx);return lists[pir.first][pir.second];}int lower_bound(const T& val) {if (maxes.empty()) return 0;int pos = std::lower_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) return len;35 行未改动static constexpr int DEFAULT_LOAD_FACTOR = 340;int len, load, offset;std::vector<std::vector<T>> lists;std::vector<T> maxes, index;void expand(int pos) {if ((int) lists[pos].size() > (load << 1)) {std::vector<T> half(lists[pos].begin() + load, lists[pos].end());lists[pos].erase(lists[pos].begin() + load, lists[pos].end());5 行未改动for (int i = offset + pos; i; i = (i - 1) >> 1) index[i]++;index[0]++;}}std::vector<int> parent(const std::vector<int>& a) {int n = a.size();std::vector<int> res(n >> 1);for (int i = 0; i < (n >> 1); i++) res[i] = a[i << 1] + a[i << 1 | 1];16 行未改动while (tree.back().size() > 1) tree.emplace_back(parent(tree.back()));for (int i = tree.size() - 1; i >= 0; i--) index.insert(index.end(), tree[i].begin(), tree[i].end());offset = (dep << 1) - 1;}std::pair<int, int> pos(int idx) {if (idx < (int) lists[0].size()) return std::make_pair(0, idx);if (index.empty()) build_index();int p = 0, n = index.size();for (int i = 1; i < n; i = p << 1 | 1) {if (idx < index[i]) p = i;else idx -= index[i], p = i + 1;} return std::make_pair(p - offset, idx);}int loc(int pos, int idx) {if (pos == 0) return idx;if (index.empty()) build_index();int tot = 0;4 行未改动public:sorted_vector() : len(0), load(DEFAULT_LOAD_FACTOR), offset(0) {}int size() const {return len;}bool empty() const {return maxes.empty();}void clear() {len = 0, offset = 0;lists.clear(), maxes.clear(), index.clear();}void add(const T& val) {if (!maxes.empty()) {int pos = std::upper_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) pos--, lists[pos].emplace_back(val), maxes[pos] = val;6 行未改动template <class It> void extend(const It& bg, const It& ed) {if ((ed - bg) * 4 < len) {for (It it = bg; it != ed; it++) add(*it);return;}vector<T> a(bg, ed);for (const auto& vec : lists) a.insert(a.end(), vec.begin(), vec.end());std::sort(a.begin(), a.end());clear(), len = a.size();for (int pos = 0; pos < len; pos += load) {std::vector<T> vec(a.begin() + pos, a.begin() + std::min(len, pos + load));lists.emplace_back(vec), maxes.emplace_back(vec.back());}}bool erase(const T& val) {if (maxes.empty()) return false;int pos = std::lower_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) return false;int idx = std::lower_bound(lists[pos].begin(), lists[pos].end(), val) - lists[pos].begin();if (lists[pos][idx] != val) return false;lists[pos].erase(lists[pos].begin() + idx), len--;int n = lists[pos].size();if (n > (load >> 1)) {maxes[pos] = lists[pos].back();if (!index.empty()) {for (int i = offset + pos; i; i = (i - 1) >> 1) index[i]--;index[0]--;}} else if (lists.size() > 1) {if (!pos) pos++;int pre = pos - 1;lists[pre].insert(lists[pre].end(), lists[pos].begin(), lists[pos].end());6 行未改动lists.erase(lists.begin() + pos), maxes.erase(maxes.begin() + pos);index.clear();} return true;}T operator[] (int idx) {auto pir = pos(idx);return lists[pir.first][pir.second];}int lower_bound(const T& val) {if (maxes.empty()) return 0;int pos = std::lower_bound(maxes.begin(), maxes.end(), val) - maxes.begin();if (pos == (int) maxes.size()) return len;40 行未改动