当前位置: 代码迷 >> 综合 >> hdu-3436-Queue-jumpers-伸展树
  详细解决方案

hdu-3436-Queue-jumpers-伸展树

热度:14   发布时间:2023-12-19 10:41:41.0

5KB的代码。。。250+行。。。就错在一个离散化上,郁闷了好久。。。

RANK就是找出第K位是多少

TOP是将某个人移至队首,对中间区间没有影响

QUERY是某个人的位置

则:

TOP:将目标点旋转至根部,然后删除,最后插入到队首

RANK:通过size查找即可,注意每个点的size是区间长度

QUERY:把该点旋转至根部,左子树的大小+1便是结果

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 220000
#define mem(a,b) memset(a,b,sizeof(a))
struct qu
{int st;int ed;
} cnt[maxn];
int num;
int search(int x)
{int l=1;int r=num+1;int mid=(l+r)/2;while(l<r){if(cnt[mid].st<=x)l=mid+1;else r=mid;mid=(l+r)/2;}mid-=1;return mid;
}
struct list
{int x;int st;friend bool operator <(const list &a,const list &b){return a.x<b.x;}
} node[maxn],nodes[maxn];
int pre[maxn],ch[maxn][2],root,tot;
int size[maxn];
int val[maxn];
int pos[maxn];
int key[maxn];
int sum[maxn];
int n;
void Treaval(int x)
{if(x){Treaval(ch[x][0]);printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,val = %2d , key = %2d \n",x,ch[x][0],ch[x][1],pre[x],size[x],val[x],key[x]);Treaval(ch[x][1]);}
}
void debug()
{printf("debug--%d\n",root);Treaval(root);
}
//debug专用
void init()
{root=tot=0;mem(pre,0);mem(ch,0);mem(size,0);num=1;
}
void newnode(int &x,int father,int k)
{x=++tot;pre[x]=father;val[x]=cnt[k].ed-cnt[k].st+1;size[x]=val[x];pos[k]=x;key[x]=k;ch[x][0]=ch[x][1]=0;
}
void push_down(int x)
{}
void push_up(int x)
{size[x]=size[ch[x][0]]+size[ch[x][1]]+val[x];
}
void rot(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;push_up(y);
}
void splay(int x,int goal)
{while(pre[x]!=goal){if(pre[pre[x]]==goal){rot(x,ch[pre[x]][0]==x);}else{int y=pre[x];int kind=ch[pre[y]][0]==y;if(ch[y][kind]==x){rot(x,!kind);rot(x,kind);}else{rot(y,kind);rot(x,kind);}}}push_up(x);if(goal==0)root=x;
}
int getmax(int x)
{while(ch[x][1]){x=ch[x][1];}return x;
}
void erase()
{int m=getmax(ch[root][0]);splay(m,root);ch[m][1]=ch[root][1];pre[ch[root][1]]=m;root=m;pre[root]=0;push_up(root);
}
void insert(int &r,int k,int father)
{if(r==0){newnode(r,father,k);}else{insert(ch[r][0],k,r);push_up(r);}
}
void top(int x)
{int s=search(x);int b=pos[s];splay(b,0);if(!ch[root][0]||!ch[root][1]){root=ch[root][0]+ch[root][1];pre[root]=0;}else erase();insert(root,s,0);splay(pos[s],0);
}
void buildtree(int &x,int l,int r,int father)
{if(l>r)return ;int mid=(l+r)/2;newnode(x,father,mid);buildtree(ch[x][0],l,mid-1,x);buildtree(ch[x][1],mid+1,r,x);push_up(x);
}
int get_rank(int r,int k)
{int t=size[ch[r][0]];if(k<=t)return get_rank(ch[r][0],k);else if(k<=t+val[r])return cnt[key[r]].st+(k-t)-1;else return get_rank(ch[r][1],k-t-val[r]);
}
int get_kth(int x)
{int s=search(x);int p=pos[s];splay(p,0);return size[ch[root][0]]+1;
}
int main()
{int T,_;scanf("%d",&T);for(_=1; _<=T; _++){init();int q,i;char str[110];int x;scanf("%d%d",&n,&q);for(i=1; i<=q; i++){scanf("%s %d",str,&x);if(strcmp(str,"Top")==0)node[i].st=1;else if(strcmp(str,"Query")==0)node[i].st=2;else node[i].st=3;node[i].x=x;nodes[i]=node[i];}sort(node+1,node+q+1);node[0].x=0;for(i=1; i<=q; i++){if(node[i].st==3)continue;if(node[i].x-cnt[num-1].ed>1){cnt[num].st=cnt[num-1].ed+1;cnt[num++].ed=node[i].x-1;}if(node[i].x-cnt[num-1].ed>0){cnt[num].st=node[i].x;cnt[num++].ed=node[i].x;}}if(cnt[num-1].ed!=n){cnt[num].st=cnt[num-1].ed+1;cnt[num].ed=n;}else num--;buildtree(root,1,num,0);printf("Case %d:\n",_);for(i=1; i<=q; i++){if(nodes[i].st==1){top(nodes[i].x);}else if(nodes[i].st==2){cout<<get_kth(nodes[i].x)<<endl;}else{cout<<get_rank(root,nodes[i].x)<<endl;}}}return 0;
}