版本比较
题解 P3369 【【模板】普通平衡树(Treap/SBT)】
正文
增加 35 行删除 35 行
20 行未改动2122232425262728293031323334353625262728293031323334353637383940414243442 行未改动4748495051515253545513 行未改动69707172737374757677787980818282838485868 行未改动95969798999910010110210322 行未改动12612712812913013013113213313433 行未改动16816917017117217217317417517620 行未改动1971981992002012012022032042052062072072082092102114 行未改动21621721821922022122222322022522622722822923022122222322422522622722822923023123223323423523648 行未改动28528628728828928929029129229356 行未改动3503513523533543543553563573583593603603613623633643 行未改动36836937037137237237337437537625 行未改动40240340440540640640740840941030 行未改动20 行未改动
```cpp#include <iostream>#include <cstdio>#include <cstdlib>2 行未改动using namespace std;#define MAXN 300000#define mx(x) t[x].mx#define mul(x) t[x].mul#define sz(x) t[x].sz #define sz(x) t[x].sz#define fa(x) t[x].fa#define lc(x) t[x].c[0]#define rc(x) t[x].c[1]#define c(x,y) t[x].c[y]13 行未改动 } void Push(int p) { sz(p) = sz(lc(p)) + sz(rc(p)) + mul(p); return; return; } int New(int v, int p) { mx(tot) = v, fa(tot) = p, lc(tot) = rc(tot) = 0, mul(tot) = sz(tot) = 1; return tot++; } void Rotate(int x, bool w) //0 for Zig 1 for Zag (Same as children relationship) { int y = fa(x), z = fa(y), b = c(x,!w); int y = fa(x), z = fa(y), b = c(x,!w); if(b) //y with b fa(b) = y; c(y,w) = b; c(x,!w) = y, fa(y) = x; //x with y8 行未改动 if((xy=(x==rc(y))) == (yz=(y==rc(z))) && z != goal) Rotate(y,yz); if(!goal) root = x; return; return; } int Find(int p, int v) { while(mx(p)!=v && c(p,mx(p)<v))22 行未改动 int p = Find(root,v), w = (mx(p)<v); if(mx(p) == v) { mul(p)++, sz(p)++; return; return; } if(c(p,w)) Splay(Adjnode(p,w),p), c(c(p,w),!w) = New(v,c(p,w)), Push(c(p,w)), Push(p), Splay(c(c(p,w),!w),0);33 行未改动 k -= s+mul(p)-1, p = rc(p); else break; Splay(p,0); return p; return p; }};Splay_Tree T;inline int read()20 行未改动 printf("%d\n",a); break; case 5 :x = read(), T.Find(T.root,x); if(mx(T.root) < x) a = mx(T.root); else else a = mx(T.Adjnode(T.root,0)); printf("%d\n",a); break; case 6 :x = read(), T.Find(T.root,x); if(mx(T.root) > x) a = mx(T.root); else else a = mx(T.Adjnode(T.root,1)); printf("%d\n",a); break; } return 0;4 行未改动
```cpp#include <iostream>#include <cstdio>#include <cstdlib>48 行未改动 inline void Push(int p) { if(p) sz(p) = sz(lc(p)) + sz(rc(p)) + mul(p); return; return; } inline int New(int v, int p) { mx(tot) = v, fa(tot) = p, lc(tot) = rc(tot) = 0, mul(tot) = sz(tot) = 1;56 行未改动 if(fp > sz(root)) Rebuild(root); return true; } inline int Rank(int v) inline int Rank(int v) { int p = root, s = sz(lc(p))+1, ans = 1; for(; mx(p)!=v && c(p,mx(p)<v); p = c(p,mx(p)<v)) if(mx(p) < v) ans += sz(lc(p))+mul(p); return ans+sz(lc(p))+(mx(p)<v ? mul(p):0); return ans+sz(lc(p))+(mx(p)<v ? mul(p):0); } inline int Select(int k) { int p = root, s = sz(lc(p))+1;3 行未改动 else if(s+mul(p)-1 < k) k -= s+mul(p)-1, p = rc(p); else break; return p; return p; }}T;int main(){25 行未改动#include <cstdlib>#include <cstring>#include <algorithm>#include <map>#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/assoc_container.hpp>using namespace std;using namespace __gnu_pbds;#define Node pair<int,int>map <int,int> s;30 行未改动