RT,目前发现如果我在每次操作后全部把树遍历一遍,把标记下放下去就对了(会超时),不这样做样例就会RE。
显然正解是不能全部遍历的。(下面的代码是错误的代码,主函数注释掉的那个print就是遍历树的函数)。
另外下面的代码我加的是换行,原题是要求空格的。
// Real_Splay
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+5, INF=1e9;
struct qwq2{
int i,a;
}e[N];
bool cmp(const qwq2 &a,const qwq2 &b){
if(a.a==b.a) return a.i<b.i;
return a.a<b.a;
}
struct qwq{
int ch[2];
int fa,val,siz,laz;
qwq(){
ch[0]=ch[1]=fa=val=siz=laz=0;
}
}t[N];
int rt,tot;
void maintain(int p){
t[p].siz=t[t[p].ch[0]].siz+t[t[p].ch[1]].siz+1;
}
bool get(int p){
return p==t[t[p].fa].ch[1];
}
void clear(int p){
t[p].ch[0]=t[p].ch[1]=t[p].fa=t[p].val=t[p].siz=t[p].laz=0;
}
void pushdown(int p){
if(t[p].laz){
swap(t[p].ch[0],t[p].ch[1]);
t[t[p].ch[0]].laz^=1;
t[t[p].ch[1]].laz^=1;
t[p].laz=0;
}
}
void rotate(int x){
int y=t[x].fa,z=t[y].fa, chk=get(x);
pushdown(y);
pushdown(x);
t[y].ch[chk]=t[x].ch[chk^1];
if(t[x].ch[chk^1]) t[t[x].ch[chk^1]].fa=y;
t[x].ch[chk^1]=y;
t[y].fa=x;
t[x].fa=z;
if(z) t[z].ch[y==t[z].ch[1]]=x;
maintain(y);
maintain(x);
}
void splay(int x,int goal=0){
// cout<<"Splay:: "<<x<<" "<<t[x].val<<"\n";
if(!goal) rt=x;
while(t[x].fa!=goal){
int f=t[x].fa,g=t[f].fa;
if(g!=goal){
if(get(f)==get(x)) rotate(x);
else rotate(f);
}
rotate(x);
}
}
int pos[N];
int build(int f,int l,int r){
if(l>r) return 0;
int mid=(l+r)>>1, nw=++tot;
pos[mid]=tot;
t[nw].val=e[mid].a,t[nw].fa=f;
t[nw].ch[0]=build(nw,l,mid-1);
t[nw].ch[1]=build(nw,mid+1,r);
maintain(nw);
return nw;
}
int kth(int val){
int nw=rt;
while(1){
pushdown(nw);
if(t[nw].ch[0] && val<=t[t[nw].ch[0]].siz)
nw=t[nw].ch[0];
else{
val-=1+t[t[nw].ch[0]].siz;
if(val<=0) return nw;
nw=t[nw].ch[1];
}
}
}
int n;
void print(int p){
pushdown(p);
if(t[p].ch[0]) print(t[p].ch[0]);
// if(t[p].val>=1 && t[p].val<=n){
// printf("%d ",t[p].val);
// cout<<p<<" "<<t[p].val<<" "<<t[p].fa<<" "<<t[p].laz<<"\n";
// }
if(t[p].ch[1]) print(t[p].ch[1]);
}
int main(){
scanf("%d",&n);
for(int i=2;i<=n+1;i++){
scanf("%d",&e[i].a);
e[i].i=i;
}
e[1].a=-INF; e[1].i=1;
e[n+2].a=INF; e[n+2].i=n+2;
rt=build(0,1,n+2);
sort(e+1,e+n+3,cmp);
for(int i=2;i<=n+1;i++){
splay(pos[e[i].i]);
int nans=t[t[rt].ch[0]].siz;
printf("%d\n",nans);
int x=kth(i-1),y=kth(nans+2);
splay(x); splay(y,x);
t[t[y].ch[0]].laz^=1;
// print(rt);
printf("\n");
}
return 0;
}