HDU 3436 splay
来源:互联网 发布:懒猫软件 编辑:程序博客网 时间:2024/04/29 23:18
点击打开链接
题意:给一个1到n的序列,然后有三种操作,每次的rank及query输出某个值
思路:题目需要将某个数放到最前面,用splay来完成,对于top,将这个节点旋转到根节点,删除后加到最前面,然后query和rank的都比较简单,代码参考kuangbin大神
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const ll INF=0x3f3f3f3f3f3f3f3fll;const int maxn=400010;int pre[maxn],ch[maxn][2],size[maxn],root,tot1;//父节点,左右孩子,子树规模,根节点,节点数量int key[maxn],flag[maxn],rev[maxn],m[maxn];//该点的值,懒惰标记int a[maxn],n,q,cnt;int s[maxn],tot2,num[maxn],tmp[maxn],st[maxn],en[maxn];void newnode(int &r,int fa,int k){ r=k; pre[r]=fa;num[r]=size[r]=en[k]-st[k]+1; ch[r][0]=ch[r][1]=0;}void pushup(int r){ size[r]=size[ch[r][0]]+size[ch[r][1]]+num[r];}void buildtree(int &x,int l,int r,int fa){ if(l>r) return ; int mid=(l+r)>>1; newnode(x,fa,mid); buildtree(ch[x][0],l,mid-1,x); buildtree(ch[x][1],mid+1,r,x); pushup(x);}void init(){ root=0; ch[root][0]=ch[root][1]=size[root]=0,num[root]=0;pre[root]=0; buildtree(root,1,cnt,0); pushup(root);}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); } } if(goal==0) root=r;}int get_min(int r){ while(ch[r][0]){ r=ch[r][0]; } return r;}void Remove(){ if(ch[root][0]==0||ch[root][1]==0){ root=ch[root][0]+ch[root][1]; pre[root]=0; }else{ int k=get_min(ch[root][1]); splay(k,root); ch[ch[root][1]][0]=ch[root][0]; root=ch[root][1]; pre[ch[root][0]]=root; pre[root]=0; pushup(root); }}int slove(int x){ int le=1,ri=cnt; while(ri>=le){ int mid=(le+ri)>>1; if(st[mid]<=x&&x<=en[mid]) return mid; if(x<st[mid]) ri=mid-1; else le=mid+1; }}void Top(int x){ int pos=slove(x); splay(pos,0);Remove(); splay(get_min(root),0); ch[pos][0]=0;ch[pos][1]=root; pre[root]=pos; root=pos;pre[root]=0; pushup(root);}int Query(int x){ int pos=slove(x); splay(pos,0); return size[ch[root][0]]+1;}int Rank(int r,int k){ int t=size[ch[r][0]]; if(k<=t) return Rank(ch[r][0],k); else if(k<=t+num[r]) return st[r]+k-t-1; else return Rank(ch[r][1],k-(t+num[r]));}char str[100010][10];int Q[100010];int main(){ char op[10]; int x,y,z,T,cas=1; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&q); int kkk=0; for(int i=0;i<q;i++){ scanf("%s%d",str[i],&Q[i]); if(str[i][0]!='R') tmp[kkk++]=Q[i]; } tmp[kkk++]=1;tmp[kkk++]=n; sort(tmp,tmp+kkk); kkk=unique(tmp,tmp+kkk)-tmp; cnt=1; st[cnt]=tmp[0];en[cnt]=tmp[0]; for(int i=1;i<kkk;i++){ if(tmp[i]-tmp[i-1]>1){ cnt++; st[cnt]=tmp[i-1]+1; en[cnt]=tmp[i]-1; } cnt++; st[cnt]=tmp[i];en[cnt]=tmp[i]; } init(); printf("Case %d:\n",cas++); for(int i=0;i<q;i++){ if(str[i][0]=='T') Top(Q[i]); else if(str[i][0]=='Q') printf("%d\n",Query(Q[i])); else printf("%d\n",Rank(root,Q[i])); } } return 0;}
0 0
- HDU 3436 splay
- 【splay】hdu 3436
- hdu 3436 splay模拟题
- hdu 3436 Queue-jumpers(Splay)
- HDU 3436 Queue-jumpers(Splay)
- HDU 3436 Queue-jumpers splay
- HDU 3436 Queue-jumpers (Splay tree)
- HDU 3436 Queue-jumpers(splay tree)
- hdu 3436 Queue-jumpers(splay tree)
- 【HDU 3436】Queue-jumpers(Splay)
- hdu 3436 splay树+离散化*
- HDU 3436 Queue-jumpers Splay+离散化
- HDU-3436 Queue-jumpers(Splay树)
- 【splay树】hdu 3487
- HDU 3487 Splay
- hdu 4680(splay tree)
- hdu 4699 Editor(Splay)
- hdu-4453-Looploop-splay
- CCF 201312-2 ISBN号码
- Qt学习之十二:标准对话框之二QFileDialog文件对话框
- java中线程执行顺序控制
- Linux服务器上如何查找JDK安装路径的方法
- Oracle中的ROWNUM rowid 以及MySQL中实现rownum功能类似的语句
- HDU 3436 splay
- GitHub 中国区前 100 名到底是什么样的人?
- mysql中RowNum的实现
- 分布式原理与实战(二) ---以FirstDB为例之体系结构
- WARN org.apache.struts2.dispatcher.Dispatcher - Could not find action or result
- 【目标检测:Hough Forest】目标检测原理(三)——基于霍夫森林的目标检测
- FFmpeg音频转码amr转mp3
- C++网络爬虫的实现
- Handle的两种作用解析