思路:
我们要知道 splay 区间翻转的本质是什么,
本质就是 每个节点编号可以随便改变, 想怎么变怎么变,变完之后会落在一个下标上,
然而splay内在的数列下标是满足splay 的性质,
当前节点的左子树的下标小于当前节点,
当前节点的右子树的下标大于当前节点.
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int sz[N], cnt[N], fa[N], ch[N][2], rt, n, m, lazy[N], ans[N], tot;
stack<int>S;
struct node {
int a, id;
bool operator < (node A)const {
if (a != A.a) return a < A.a;
return id < A.id;
}
} f[N];
void pushup(int x) {
sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
}
bool get(int x) {
return ch[fa[x]][1] == x;
}
void pushdown(int x) {
if (lazy[x]) {
lazy[ch[x][0]] ^= 1;
lazy[ch[x][1]] ^= 1;
swap(ch[x][0], ch[x][1]);
lazy[x] = 0;
}
}
void rotate(int x) {
int y = fa[x], z = fa[y], chk = get(x);
pushdown(y);
pushdown(x);
ch[y][chk] = 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;
pushup(x);
pushup(y);
}
void splay(int x, int goal) {
while(fa[x] != goal) {
int y = fa[x], z = fa[y];
if (z != goal)
(ch[z][1] == y) ^ (ch[y][1] == x) ? rotate(x) : rotate(y);
rotate(x);
}
if (goal == 0) rt = x;
}
int find(int k) {
int now = rt;
while(1) {
pushdown(now);
if (ch[now][0] && k <= sz[ch[now][0]]) {
now = ch[now][0];
} else {
if (ch[now][0]) k -= sz[ch[now][0]];
k --;
if (k <= 0) return now;
now = ch[now][1];
}
}
}
int build(int l, int r, int root) {
int now = (l + r) >> 1;
fa[now] = root;
if (l < now) ch[now][0] = build(l, now - 1, now);
if (r > now) ch[now][1] = build(now + 1, r, now);
pushup(now);
return now;
}
void solve(int x) {
int y = x;
while(!S.empty()) S.pop();
while(fa[y] != 0) {
S.push(fa[y]);
y = fa[y];
}
while(!S.empty()) {
y = S.top();
S.pop();
pushdown(y);
}
splay(x, 0);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &f[i].a);
f[i].id = i;
}
int x, y;
sort(f + 1, f + n + 1); //每个节点的节点编号就是 id + 1, 因为build 的时候比较巧妙.
f[0].id = 0, f[n + 1].id = n + 1;
build(1, n + 2, 0);
for (int i = 1; i <= n; ++i) {
solve(f[i].id + 1); // 1 号节点,n + 1 号节点 是两边节点,所以要加一
ans[i] = sz[ch[rt][0]];
x = find(i); // 区间是 [i+ 1, ans[i] + 1] , 要左边减一,右边加一 .
y = find(ans[i] + 2);
splay(x, 0);
splay(y, x);
lazy[ch[ch[rt][1]][0]] ^= 1;
}
for (int i = 1; i <= n; ++i)
printf("%d ", ans[i]);
printf("\n");
return 0;
}
/*
6
3 4 5 1 6 2
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=100005;
int n,fa[N],ch[N][2],val[N],siz[N],tot,pos[N],rt;
struct Node{
int id,v;
}a[N];
bool cmp1(Node x,Node y) {return x.v==y.v?x.id<y.id:x.v<y.v;}
bool cmp2(Node x,Node y) {return x.id<y.id;}
bool rev[N];
inline void pushup(int x) {siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;}
void pushdown(int x) {
if(rev[x]) {
swap(ch[x][0],ch[x][1]);
rev[ch[x][0]]^=1,rev[ch[x][1]]^=1;
rev[x]=0;
}
}
int build(int f,int l,int r) {
if(l>r) return 0;
int now=++tot,mid=l+r>>1;
val[now]=a[mid].v;fa[now]=f;
pos[a[mid].v]=now;
ch[now][0]=build(now,l,mid-1);
ch[now][1]=build(now,mid+1,r);
pushup(now);
return now;
}
inline void rotate(int x) {
int f=fa[x],ff=fa[f];
pushdown(f);pushdown(x);
bool tag=ch[fa[x]][1]==x;
ch[f][tag]=ch[x][tag^1];
fa[ch[f][tag]]=f;
fa[f]=x;
ch[x][tag^1]=f;
fa[x]=ff;
if(ff) ch[ff][f==ch[ff][1]]=x;
pushup(f);pushup(x);
}
inline void splay(int x,int tar){
for(int f;(f=fa[x])!=tar;rotate(x)) if(fa[f]!=tar)rotate((x==ch[fa[x]][0])==(f==ch[fa[f]][0])?f:x);
if(!tar) rt=x;
}
int nxt(){
pushdown(rt);
int x=ch[rt][1];
while(pushdown(x),ch[x][0]) x=ch[x][0];
return x;
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i+1].v),a[i+1].id=i+1;
a[1].v=0;a[n+2].v=n+1;
sort(a+2,a+2+n,cmp1);
for(int i=2;i<=n+1;i++) a[i].v=i-1;
sort(a+2,a+2+n,cmp2);
rt=build(0,1,n+2);
for(int i=1;i<=n;i++) {
int x=pos[i];splay(x,0);
printf("%d ",siz[ch[x][0]]);
x=nxt();
int y=pos[i-1];
splay(y,0);
splay(x,y);
rev[ch[x][0]]^=1;
}
}