[BZOJ1012]JSOI2008 最大数|线段树|平衡树

来源:互联网 发布:网络推广绩效考核方案 编辑:程序博客网 时间:2024/05/21 09:27

水题一道,显然线段树水过,不过我为了练练spaly就写了个splay作死,巨慢。。

#include<iostream>#include<cstdio>using namespace std;const int inf=99999999,maxn=200005;int m,d,i,nnode=0,root=0,last,l,get,c[maxn][2],size[maxn],lmax[maxn],num[maxn],pre[maxn];char cc;void update(int x){size[x]=size[c[x][1]]+size[c[x][0]]+1;lmax[x]=max(max(lmax[c[x][0]],lmax[c[x][1]]),num[x]);}void newnode(int &x,int fa,int k){x=++nnode;pre[x]=fa;num[x]=k;size[x]=1;lmax[x]=k;c[x][0]=c[x][1]=0;}void rot(int x,int kind){int y=pre[x];int z=pre[y];c[y][!kind]=c[x][kind];pre[c[x][kind]]=y;c[x][kind]=y;pre[y]=x;pre[x]=z;if (z) c[z][c[z][1]==y]=x;update(y);update(x);if (z) update(z);}void splay(int x,int goal){int y,z,kind;while (pre[x]!=goal){y=pre[x];if (pre[y]==goal) rot(x,c[y][0]==x);else{z=pre[y],kind=c[z][0]==y;if (c[y][kind]!=x) rot(y,kind); else rot(x,!kind);rot(x,kind);} }if (goal==0) root=x;}int findkth(int x,int k){if (k<=size[c[x][0]]) return findkth(c[x][0],k);if (k==size[c[x][0]]+1) return x;if (k>size[c[x][0]]+1) return findkth(c[x][1],k-1-size[c[x][0]]);}void insert(int k){get=root;while (c[get][1]) get=c[get][1];newnode(c[get][0],get,k);splay(c[get][0],0);}void solve(int l){get=findkth(root,nnode-l-1);splay(get,0);last=lmax[c[root][1]];printf("%d\n",last);}void print(int x){if (c[x][0]) print(c[x][0]);printf("x->num:%d,size:%d,pre:%d,lmax:%d\n",num[x],size[x],pre[x],lmax[x]);if (c[x][1]) print(c[x][1]);}int main(){scanf("%d%d",&m,&d);c[0][1]=c[0][0]=size[0]=pre[0]=0;num[0]=lmax[0]=-inf;last=0;newnode(root,0,-inf);newnode(c[root][1],root,-inf);update(root);for (i=1;i<=m;i++){scanf("%c",&cc);scanf("%c%d",&cc,&l);//print(root);if (cc=='A') insert((l+last)%d);else solve(l);}}

0 0
原创粉丝点击