0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 19:45:24 +00:00

3506. [Cqoi2014]排序机械臂

https://hydro.ac/d/bzoj/record/6322e9bc32ae6c3198cc971e
This commit is contained in:
Baoshuo Ren 2022-09-15 17:00:51 +08:00
parent aa464c71a2
commit 7579397dc7
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
21 changed files with 243 additions and 0 deletions

183
BZOJ/3506/3506.cpp Normal file
View File

@ -0,0 +1,183 @@
#include <iostream>
#include <algorithm>
#include <chrono>
#include <limits>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
struct node {
node *lchild, *rchild;
size_t size;
unsigned key;
int value, min;
bool reversed;
node()
: lchild(nullptr),
rchild(nullptr),
size(0),
key(rand()),
value(0),
min(std::numeric_limits<int>::max()),
reversed(false) {}
node(int _value)
: lchild(nullptr),
rchild(nullptr),
size(1),
key(rand()),
value(_value),
min(_value),
reversed(false) {}
~node() {
if (lchild != nullptr) delete lchild;
if (rchild != nullptr) delete rchild;
}
inline size_t lsize() {
return lchild == nullptr ? 0 : lchild->size;
}
inline size_t rsize() {
return rchild == nullptr ? 0 : rchild->size;
}
inline void pushup() {
size = lsize() + 1 + rsize();
min = value;
if (lchild != nullptr) {
min = std::min(min, lchild->min);
}
if (rchild != nullptr) {
min = std::min(min, rchild->min);
}
}
inline void pushdown() {
if (reversed) {
std::swap(lchild, rchild);
if (lchild != nullptr) lchild->reversed = !lchild->reversed;
if (rchild != nullptr) rchild->reversed = !rchild->reversed;
reversed = false;
}
}
};
int n, b[N];
std::pair<int, int> a[N];
node *root;
std::pair<node *, node *> split(node *u, int k) {
if (u == nullptr) return std::make_pair(nullptr, nullptr);
u->pushdown();
if (k <= u->lsize()) {
auto o = split(u->lchild, k);
u->lchild = o.second;
u->pushup();
o.second = u;
return o;
}
auto o = split(u->rchild, k - u->lsize() - 1);
u->rchild = o.first;
u->pushup();
o.first = u;
return o;
}
node *merge(node *x, node *y) {
if (x == nullptr) return y;
if (y == nullptr) return x;
if (x->key < y->key) {
x->pushdown();
x->rchild = merge(x->rchild, y);
x->pushup();
return x;
}
y->pushdown();
y->lchild = merge(x, y->lchild);
y->pushup();
return y;
}
void reverse(int k) {
auto x = split(root, k);
auto y = split(x.first, k - 1);
if (y.first != nullptr) y.first->reversed = !y.first->reversed;
delete y.second;
root = merge(y.first, x.second);
}
int find(node *p) {
int k = 1;
while (p != nullptr) {
p->pushdown();
if (p->lchild != nullptr && p->min == p->lchild->min) {
p = p->lchild;
} else if (p->rchild != nullptr && p->min == p->rchild->min) {
k += p->lsize() + 1;
p = p->rchild;
} else {
return k + p->lsize();
}
}
return -1;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
}
std::sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) {
b[a[i].second] = i;
}
for (int i = 1; i <= n; i++) {
root = merge(root, new node(b[i]));
}
for (int i = 1; i <= n; i++) {
int k = find(root);
reverse(k);
cout << k + i - 1 << ' ';
}
cout << endl;
// Cleanup
delete root;
return 0;
}

BIN
BZOJ/3506/data/1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/3506/data/9.out (Stored with Git LFS) Normal file

Binary file not shown.