Recently, a variety of blind writing topics, feel the need to review the data structure
Write a splay calm down (hand speed too slow, later to practice more)
Using Splay is the most direct method, but I feel that discrete waves should be able to be made (not carefully thought)
Now no very pursuit of code beautiful, feel the first to fight the Fast O (∩_∩) o
1#include <bits/stdc++.h>2 #defineINF 10000000003 using namespacestd;4 introot,n,n,x;5 intfa[100005],c[100005][2],a[100005];6 voidRotintx)7 {8 intfat=fa[x],k=c[fat][1]==x;9c[fat][k]=c[x][!k];fa[c[x][!k]]=fat;Tenfa[x]=fa[fat];c[fa[fat]][c[fa[fat]][1]==fat]=x; Onefa[fat]=x;c[x][!k]=fat; A } - voidSplay (intXintg) - { the for(inty; (y=fa[x])! =G;rot (x)) - if(fa[y]!=g) - if((c[y][1]==x) = = (c[fa[y]][1]==y)) rot (y);Elserot (x); - if(g==0) root=x; + } - intLowerintp) + { A if(!root)return 0; at intans=INF; - for(intI=root;i (P>=a[i])? i=c[i][1]:i=c[i][0]) - if(a[i]>=p) -ans=min (ans,a[i]); - returnans-p; - } in intUpperintp) - { to if(!root)return 0; + intans=-INF; - for(intI=root;i (P>=a[i])? i=c[i][1]:i=c[i][0]) the if(a[i]<=p) *ans=Max (ans,a[i]); $ returnPans;Panax Notoginseng } - voidAddintx) the { +a[++n]=x;c[n][0]=c[n][1]=0; A if(!root) the { +root=N; - return; $ } $ for(intI=root;1;) - if(x<=A[i]) - if(!c[i][0]) the { -c[i][0]=n,fa[n]=i;WuyiSplay (N,0); the Break; - } Wu Elsei=c[i][0]; - Else About if(!c[i][1]) $ { -c[i][1]=n,fa[n]=i; -Splay (N,0); - Break; A } + Elsei=c[i][1]; the } - intMain () $ { thescanf"%d", &n);intans=0; the for(intI=1; i<=n;i++) the { thescanf"%d",&x); - if(i==1) ans+=x; in Else theans+=min (Upper (x), lower (x)); the Add (x); About } theprintf"%d\n", ans); the return 0; the}
bzoj1588: [HNOI2002] turnover statistics splay blind writing