Splay+BZOJ1503

调了好久,发现两个问题:

首先插入的时候忘记把p赋给fa的孩子,还有就是pushup的时候加上一个判断

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define Key_value ch[ch[root][1]][0]
using namespace std;
const int maxn=100010;
const int INF=1000000000;
int N,M;
int res;
int ch[maxn][2],pre[maxn],size[maxn],key[maxn];
int root,tot1,tot2,s[maxn];
int det;
void NewNode(int &r,int f,int val)
{if(tot2)r=s[tot2--];else r=++tot1;ch[r][0]=ch[r][1]=0;pre[r]=f;key[r]=val;size[r]=1;
}
void pushup(int r)
{if(r)//加了这个就对了size[r]=size[ch[r][0]]+size[ch[r][1]]+1;
}
void Rotate(int x,int kind)
{int y=pre[x];ch[y][!kind]=ch[x][kind];pre[ch[x][kind]]=y;if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;pre[x]=pre[y];ch[x][kind]=y;pre[y]=x;pushup(y);
}
void Splay(int r,int goal)
{while(pre[r]!=goal){if(pre[pre[r]]==goal){Rotate(r,ch[pre[r]][0]==r);}else{int y=pre[r];int kind=(ch[pre[y]][0]==y);if(ch[y][kind]==r){Rotate(r,!kind);Rotate(r,kind);}else{Rotate(y,kind);Rotate(r,kind);}}}pushup(r);if(goal==0)root=r;
}
int get_kth(int r,int k)
{int t=size[ch[r][0]]+1;if(t==k)return r;if(t>k)return get_kth(ch[r][0],k);return get_kth(ch[r][1],k-t);
}
void debug(int r)
{if(!r)return;printf("%d**%d**%d**%d\n",key[r],r,ch[r][0],ch[r][1]);debug(ch[r][0]);debug(ch[r][1]);
}
int Insert(int x)
{int p=root,fa=0;while(p){if(key[p]>=x)fa=p,p=ch[p][0];else fa=p,p=ch[p][1];}NewNode(p,fa,x);if(key[fa]=M){Insert(k-det);}else if(op[0]=='A')Increase(k);else if(op[0]=='S')Decrease(k);else if(op[0]=='F')Find(k);}printf("%d\n",res);return 0;
}



本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部