题解:AT_abc465_c [ABC465C] Reverse Permutation
wangganlin · · 题解
由于
如果
该篇题解纯属整活,请勿模仿,正解是用 deque,
代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdint>
#include <ctime>
using namespace std;
using ll = long long;
inline uint32_t randUint() {
static uint32_t seed = (uint32_t)time(nullptr);
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
return seed;
}
template<typename T>
struct FHQSequenceTreap {
struct Node {
T val;
uint32_t prio;
int size;
bool rev;
int l, r;
Node() : prio(0), size(0), rev(false), l(0), r(0) {}
Node(const T& v) : val(v), prio(randUint()), size(1), rev(false), l(0), r(0) {}
};
vector<Node> tr;
int root;
int cnt;
FHQSequenceTreap(int maxNodeCnt = 1000010) {
tr.resize(maxNodeCnt + 10);
root = 0;
cnt = 0;
}
int newNode(const T& v) {
tr[++cnt] = Node(v);
return cnt;
}
void pushdown(int u) {
if (!tr[u].rev) return;
swap(tr[u].l, tr[u].r);
if (tr[u].l) tr[tr[u].l].rev ^= 1;
if (tr[u].r) tr[tr[u].r].rev ^= 1;
tr[u].rev = false;
}
void update(int u) {
tr[u].size = 1;
if (tr[u].l) tr[u].size += tr[tr[u].l].size;
if (tr[u].r) tr[u].size += tr[tr[u].r].size; // 修复:应该是 tr[tr[u].r].size
}
void splitK(int u, int k, int& l, int& r) {
if (!u) { l = r = 0; return; }
pushdown(u);
int leftSz = tr[tr[u].l].size;
if (leftSz + 1 <= k) {
l = u;
splitK(tr[u].r, k - (leftSz + 1), tr[u].r, r);
update(l);
} else {
r = u;
splitK(tr[u].l, k, l, tr[u].l);
update(r);
}
}
int merge(int l, int r) {
if (!l || !r) return l ? l : r;
pushdown(l); pushdown(r);
if (tr[l].prio > tr[r].prio) {
tr[l].r = merge(tr[l].r, r);
update(l);
return l;
} else {
tr[r].l = merge(l, tr[r].l);
update(r);
return r;
}
}
void pushBack(const T& v) {
int nd = newNode(v);
root = merge(root, nd);
}
void insertPos(int pos, const T& v) {
if (pos < 1) pos = 1;
if (pos > tr[root].size + 1) pos = tr[root].size + 1;
int a, b;
splitK(root, pos - 1, a, b);
int nd = newNode(v);
root = merge(merge(a, nd), b);
}
void erasePos(int pos) {
if (pos < 1 || pos > tr[root].size) return;
int a, mid, b;
splitK(root, pos - 1, a, b);
splitK(b, 1, mid, b);
root = merge(a, b);
}
void rangeReverse(int L, int R) {
if (L > R || L <= 0 || R > tr[root].size) return;
int a, mid, b;
splitK(root, L - 1, a, b);
splitK(b, R - L + 1, mid, b);
if (mid) tr[mid].rev ^= 1;
root = merge(merge(a, mid), b);
}
// 递归填充vector
void dfsCollect(int u, vector<T>& res) {
if (!u) return;
pushdown(u);
dfsCollect(tr[u].l, res);
res.push_back(tr[u].val);
dfsCollect(tr[u].r, res);
}
// 一键导出整棵树到vector
vector<T> getVector() {
vector<T> res;
dfsCollect(root, res);
return res;
}
// 打印(依赖重载<<)
void dfsPrint(int u, ostream& os) {
if (!u) return;
pushdown(u);
dfsPrint(tr[u].l, os);
os << tr[u].val << " ";
dfsPrint(tr[u].r, os);
}
void printAll(ostream& os = cout) {
dfsPrint(root, os);
os << "\n";
}
};
int n, cnt;
string str;
int main() {
cin >> n;
cin >> str;
FHQSequenceTreap<int> tr(n);
for (int i = 1; i <= n; ++i) {
tr.pushBack(i);
}
for (char ch : str) {
++cnt;
if (ch == 'o') {
tr.rangeReverse(1, cnt);
}
}
tr.printAll();
return 0;
}