题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3224
!解锁新技能:替罪羊树
一道无论平衡树是否平衡都可以过的题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
#include<bits/stdc++.h> #define MAXN 100010 #define inf 1000000007 typedef long long ll; typedef double db; using namespace std; inline int rd(){ int x=0,y=1;char c=getchar(); while(c<'0' || c>'9'){if(c=='-')y=-y;c=getchar();} while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar(); return x*y; } int nums[MAXN]; struct ScapeGoatTree{ int son[MAXN][2],sz[MAXN],fa[MAXN],key[MAXN]; int root,tot,sum; db alpha; void init(){ sum=0; alpha=0.75; tot=2,root=1; key[1]=-inf,sz[1]=2,son[1][1]=2,fa[1]=0; key[2]=inf,sz[2]=1,fa[2]=1; } int getlr(int x){ return son[fa[x]][1]==x; } bool balance(int x){ return (db)sz[x]*alpha>=(db)sz[son[x][0]] && (db)sz[x]*alpha>=(db)sz[son[x][1]]; } void dfs(int x){ if(!x)return; dfs(son[x][0]); nums[++sum]=x; dfs(son[x][1]); } int build(int l,int r){ if(l>r)return 0; int mid=(l+r)/2,tmp=nums[mid]; fa[son[tmp][0]=build(l,mid-1)]=tmp; fa[son[tmp][1]=build(mid+1,r)]=tmp; sz[tmp]=sz[son[tmp][0]]+sz[son[tmp][1]]+1; return tmp; } void rebuild(int x){ sum=0;dfs(x); int which=getlr(x),f=fa[x]; int tmp=build(1,sum); fa[tmp]=f; son[f][which]=tmp; if(root==x)root=tmp; } void ins(int x){ int tmp=root,f=0; key[++tot]=x,sz[tot]=1; while(tmp){ sz[tmp]++; f=tmp; tmp=son[tmp][key[tmp]<=x]; if(!tmp){ fa[tot]=f; son[f][key[f]<=x]=tot; break; } } int reb=0; for(int i=tot; i; i=fa[i])if(!balance(i))reb=i; if(reb)rebuild(reb); } int findrk(int x){ int tmp=root,ret=0; while(tmp){ if(key[tmp]>=x)tmp=son[tmp][0]; else ret+=sz[son[tmp][0]]+1,tmp=son[tmp][1]; } return ret; } int findkth(int x){ int tmp=root; while(tmp){ if(sz[son[tmp][0]]==x-1)break; else if(sz[son[tmp][0]]>=x)tmp=son[tmp][0]; else x-=sz[son[tmp][0]]+1,tmp=son[tmp][1]; } return key[tmp]; } int getnum(int x){ int tmp=root; while(tmp){ if(key[tmp]==x)return tmp; tmp=son[tmp][key[tmp]<x]; } } int pre(int x){ int tmp=root,ret=-inf; while(tmp){ if(key[tmp]<x)ret=max(ret,key[tmp]),tmp=son[tmp][1]; else tmp=son[tmp][0]; } return ret; } int nxt(int x){ int tmp=root,ret=inf; while(tmp){ if(key[tmp]>x)ret=min(ret,key[tmp]),tmp=son[tmp][0]; else tmp=son[tmp][1]; } return ret; } void del(int x){ x=getnum(x); if(son[x][0] && son[x][1]){ int tmp=son[x][0]; while(son[tmp][1])tmp=son[tmp][1]; key[x]=key[tmp]; x=tmp; } int ss=son[x][0]?son[x][0]:son[x][1],which=getlr(x); son[fa[x]][which]=ss; fa[ss]=fa[x]; for(int i=x; i; i=fa[i])sz[i]--; if(x==root)root=ss; } }tree; int main(){ int n=rd(); tree.init(); while(n--){ int op=rd(),x=rd(); if(op==1)tree.ins(x); if(op==2)tree.del(x); if(op==3)printf("%d\n",tree.findrk(x)); if(op==4)printf("%d\n",tree.findkth(x+1)); if(op==5)printf("%d\n",tree.pre(x)); if(op==6)printf("%d\n",tree.nxt(x)); } return 0; } |
太神辣QwQ
orz orz
%%%%%%%%%%%