0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-09 16:58:48 +00:00

#1974. 【模板】左偏树

https://sjzezoj.com/submission/74850
This commit is contained in:
Baoshuo Ren 2023-03-30 16:20:51 +08:00
parent f50cbec887
commit c710afb584
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
45 changed files with 338 additions and 0 deletions

141
S2OJ/1974/1974.cpp Normal file
View File

@ -0,0 +1,141 @@
#include <iostream>
#include <functional>
#include <memory>
#include <stack>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
template <typename T, class Compare = std::less<T>>
class LeftistTree : public std::enable_shared_from_this<LeftistTree<T, Compare>> {
public:
using pointer_type = std::shared_ptr<LeftistTree<T, Compare>>;
protected:
static Compare comp;
private:
size_t dist;
pointer_type lchild, rchild;
std::weak_ptr<LeftistTree<T, Compare>> root;
public:
T id, val;
LeftistTree(const T& _val = 0, const T& _id = 0)
: dist(0),
lchild(nullptr),
rchild(nullptr),
root(),
id(_id),
val(_val) {}
pointer_type find_root() {
std::stack<pointer_type> st;
pointer_type cur = this->shared_from_this();
st.emplace(cur);
while (auto ptr = cur->root.lock()) {
st.emplace(cur = ptr);
}
while (!st.empty()) {
auto ptr = st.top();
st.pop();
ptr->root = cur;
}
cur->root.reset();
return cur;
}
void erase() {
if (lchild) lchild->root.reset();
if (rchild) rchild->root.reset();
root = merge(lchild, rchild);
}
static pointer_type merge(pointer_type x, pointer_type y) {
if (!x) return y;
if (!y) return x;
if (comp(x->val, y->val) || x->val == y->val && comp(x->id, y->id)) std::swap(x, y);
x->rchild = merge(x->rchild, y);
if (!x->lchild || x->lchild->dist < x->rchild->dist) std::swap(x->lchild, x->rchild);
if (x->lchild) x->lchild->root = x;
if (x->rchild) x->rchild->root = x;
x->dist = x->rchild ? x->rchild->dist + 1 : 0;
return x;
}
};
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
std::vector<bool> exists(n + 1, true);
std::vector<LeftistTree<int, std::greater<>>::pointer_type> tr(n + 1);
for (int i = 1, x; i <= n; i++) {
cin >> x;
tr[i] = std::make_shared<decltype(tr)::value_type::element_type>(x, i);
}
while (m--) {
int op;
cin >> op;
if (op == 1) {
int x, y;
cin >> x >> y;
if (exists[x] && exists[y]) {
auto fx = tr[x]->find_root(),
fy = tr[y]->find_root();
if (fx != fy) {
decltype(tr)::value_type::element_type::merge(fx, fy);
}
}
} else { // op == 2
int x;
cin >> x;
if (exists[x]) {
auto fx = tr[x]->find_root();
if (exists[fx->id]) {
cout << fx->val << endl;
fx->erase();
exists[fx->id] = false;
} else {
cout << -1 << endl;
}
} else {
cout << -1 << endl;
}
}
}
return 0;
}

BIN
S2OJ/1974/data/1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/11.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/11.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/12.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/12.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/13.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/13.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/14.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/14.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/15.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/15.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/16.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/16.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/17.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/17.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/18.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/18.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/19.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/19.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/20.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/20.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/9.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/problem.conf (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/std.cpp (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1974/data/val.cpp (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,68 @@
/**
* @file S2OJ/1974/gen.cpp
* @author Baoshuo <i@baoshuo.ren>
* @url https://baoshuo.ren
*/
#include "testlib.h"
#include <iostream>
#include <limits>
using std::cin;
using std::cout;
const char endl = '\n';
#define N_TESTS 20
#define PERCENT_TESTS(_percent) (static_cast<double>(_percent) / 100 * N_TESTS)
int main(int argc, char* argv[]) {
registerGen(argc, argv, 1);
int id = opt<int>("id");
int min_n = 100000,
max_n = 100000; // 1e5
int min_m = 100000,
max_m = 100000; // 1e5
std::cerr << PERCENT_TESTS(30) << endl;
if (id <= PERCENT_TESTS(30)) {
min_n = max_n = 10;
min_m = max_m = 10;
} else if (id <= PERCENT_TESTS(70)) {
min_n = max_n = 1000;
min_m = max_m = 1000;
} else { // all tests
// (none)
}
int n = rnd.next(min_n, max_n);
int m = rnd.next(min_m, max_m);
cout << n << ' ' << m << endl;
for (int i = 1; i <= n; i++) {
const char end_c = i < n ? ' ' : '\n';
cout << rnd.next(std::numeric_limits<int>::min(), std::numeric_limits<int>::max()) << end_c;
}
for (int i = 1; i <= m; i++) {
int op = rnd.next(1, 2);
if (op == 1) {
int x = rnd.next(1, n);
int y = rnd.next(1, n);
cout << op << ' ' << x << ' ' << y << endl;
} else {
int x = rnd.next(1, n);
cout << op << ' ' << x << endl;
}
}
return 0;
}