splay求调

P3369 【模板】普通平衡树

改进之后30pts,全WA
by HS_xh @ 2023-11-03 21:15:35


```cpp #include<bits/stdc++.h> using namespace std; #define int long long const int N=100005,INF=0x3f3f3f3f; int t,opt; int f[N],ch[N][2],key[N],cnt[N],siz[N],sz,root; inline void clear(int x) { ch[x][0]=ch[x][1]=f[x]=cnt[x]=key[x]=siz[x]=0; } inline int get(int x) { return ch[f[x]][1]==x; } inline void update(int x) { if(x) { siz[x]=cnt[x]; if(ch[x][0]) { siz[x]+=siz[ch[x][0]]; } if(ch[x][1]) { siz[x]+=siz[ch[x][1]]; } } } inline void rotate(int x) { int old=f[x],oldf=f[old],which=get(x); ch[old][which]=ch[x][which^1]; f[ch[old][which]]=old; ch[x][which^1]=old; f[old]=x; f[x]=oldf; if(oldf) ch[oldf][ch[oldf][1]==old]=x; update(old); update(x); return; } inline void splay(int x) { for(int fa;fa=f[x];rotate(x)) if(f[fa]) rotate(get(x)==get(fa)? fa : x); root=x; } inline void insert(int v) { if(root==0) { ++sz; root=sz; ch[root][0]=ch[root][1]=f[root]=0; key[root]=v; cnt[root]=siz[root]=1; return; } int cur=root,fa=0; while(1) { if(key[cur]==v) { ++cnt[cur]; update(cur); update(fa); splay(cur); break; } fa=cur; cur=ch[cur][key[cur]<v]; if(cur==0) { ++sz; ch[sz][0]=ch[sz][1]=0; key[sz]=v; siz[sz]=1; cnt[sz]=1; f[sz]=fa; ch[fa][key[fa]<v]=sz; update(fa); splay(sz); break; } } } inline int find(int v) { int ans=0,cur=root; while(1) { if(v<key[cur]) cur=ch[cur][0]; else { ans+=(ch[cur][0] ? siz[ch[cur][0]] : 0); if(v==key[cur]) { splay(cur); return ans+1; } ans+=cnt[cur]; cur=ch[cur][1]; } } } inline int findth(int k) { int cur=root; while(1) { if(ch[cur][0] && k<=siz[ch[cur][0]]) cur=ch[cur][0]; else { int tem=(ch[cur][0] ? siz[ch[cur][0]] : 0) +cnt[cur]; if(cur<=tem) return key[cur]; k-=tem; cur=ch[cur][1]; } } } inline int pre() { int cur=ch[root][0]; while(ch[cur][1]) cur=ch[cur][1]; return cur; } inline int nxt() { int cur=ch[root][1]; while(ch[cur][0]) cur=ch[cur][0]; return cur; } inline void del(int v) { find(v); if(cnt[root]>1) { --cnt[root]; update(root); return; } if(!ch[root][0] && !ch[root][1]) { clear(root); root=0; sz=0; return; } if(!ch[root][0]) { int oldroot=root; root=ch[root][1]; f[root]=0; clear(oldroot); --sz; return; } else if(!ch[root][1]) { int oldroot=root; root=ch[root][0]; f[root]=0; clear(oldroot); --sz; return; } int lpre=pre(),oldroot=root; splay(lpre); f[ch[oldroot][1]]=root; ch[root][1]=ch[oldroot][1]; update(root); return; } signed main() { cin>>t; insert(INF); insert(-INF); int x; while(t--) { cin>>opt; switch(opt) { case 1: { cin>>x; insert(x); break; } case 2: { cin>>x; del(x); break; } case 3: { cin>>x; insert(x); cout<<find(x)-1<<endl; del(x); break; } case 4: { cin>>x; insert(x); cout<<findth(x+1)<<endl; del(x); break; } case 5: { cin>>x; insert(x); cout<<key[pre()]<<endl; del(x); break; } case 6: { cin>>x; insert(x); cout<<key[nxt()]<<endl; del(x); break; } } } } ```
by HS_xh @ 2023-11-03 21:15:58


过了,操作四有问题
by HS_xh @ 2023-11-03 21:35:38


~~我在跟谁说话~~
by HS_xh @ 2023-11-03 21:36:06


orz,但不会(押韵
by SqRt_FiSh @ 2023-11-03 21:43:57


|