CLCK @ 2022-10-18 21:01:26
如题,大部分按照oiwiki的板子写的
但是insert部分好像出了问题,求帮看看()
十分感激
#include <iostream>
using namespace std;
int rt, tot, fa[100005], ch[100005][2];
int val[100005], cnt[100005], sz[100005];
int v[100005];
void maintain(int x) {
sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + cnt[x];
}
bool get(int x) {
return x == ch[fa[x]][1];
}
void clear(int x) {
ch[x][0] = ch[x][1] = fa[x] = val[x] = sz[x] = cnt[x] = 0;
}
void rotate(int x) {
int y = fa[x], z = fa[y], chk = get(x);
ch[y][chk] = ch[x][chk ^ 1];
if (ch[x][chk ^ 1]) fa[ch[x][chk ^ 1]] = y;
ch[x][chk ^ 1] = y;
fa[y] = x;
fa[x] = z;
if (z) ch[z][y == ch[z][1]] = x;
maintain(y);
maintain(x);
}
/*void splay(int x) {
for (int f = fa[x]; f = fa[x], f; rotate(x)) {
if (fa[f]) rotate(get(x) == get(f) ? f : x);
}
rt = x;
}*/
void splay(int x, int goal){
for (int i; (i = fa[x]) != goal; rotate(x)) {
if (fa[i] != goal) {
rotate(get(x) == get(i) ? i : x);
}
}
if (goal == 0) {
rt = x;
}
}
void insert(int k) {
if (!rt) {
val[++tot] = k;
cnt[tot]++;
rt = tot;
maintain(rt);
return ;
}
int cur = rt, f = 0;
while (1) {
if (val[cur] == k) {
cnt[cur]++;
maintain(cur);
maintain(f);
rotate(cur);
break;
}
f = cur;
cur = ch[cur][val[cur] < k];
if (!cur) {
val[++tot] = k;
cnt[tot]++;
fa[tot] = f;
ch[f][val[f] < k] = tot;
maintain(tot);
maintain(f);
rotate(tot);
break;
}
}
}
int find(int k) {
int cur = rt;
while (1) {
if (ch[cur][0] && k <= sz[ch[cur][0]]) {
cur = ch[cur][0];
} else {
k -= cnt[cur] + sz[ch[cur][0]];
if (k <= 0) {
rotate(cur);
return val[cur];
}
cur = ch[cur][1];
}
}
}
void dfs(int rt) {
if (v[rt]) {
cout << rt << " ";
return ;
}
v[rt] = 1;
if (rt == 0) {
return ;
}
if (ch[rt][0]) dfs(ch[rt][0]);
if (ch[rt][1]) dfs(ch[rt][1]);
cout << rt << " ";
}
int main() {
int m, n;
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
insert(i);
}
while (m--) {
int x, y;
cin >> x >> y;
splay(find(x), 0);
splay(find(y + 2), find(x));
}
dfs(rt);
return 0;
}