Title Link: http://www.lydsy.com/JudgeOnline/problem.php?id=1588
is another treap template problem ... Always do the template problem is not good ...
Also keep in mind: Bzoj with Srand (time (0)) will re!
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <ctime>6 #defineRep (i,l,r) for (int i=l; i<=r; i++)7 #defineCLR (x, y) memset (x,y,sizeof (×))8typedefLong Longll;9typedef unsignedLong Longull;Ten using namespacestd; One Const intINF =0x3f3f3f3f; A Const intMAXN =50010; - structnode{ - intv,w,l,r,rnd,size; the }T[MAXN]; - intn,x,t1,t2,tot=0, root=0, ans=0; -InlineintRead () { - intAns =0, F =1; + Charc =GetChar (); - while(!IsDigit (c)) { + if(c = ='-') F =-1; Ac =GetChar (); at } - while(IsDigit (c)) { -Ans = ans *Ten+ C-'0'; -c =GetChar (); - } - returnAns *F; in } -InlinevoidUpdateintW) { toT[w].size = t[t[w].l].size + t[t[w].r].size +T[W].W; + } - voidRotl (int&W) { the intK = T[W].R; T[W].R = T[K].L; T[K].L =W; *Update (w); Update (k); W =K; $ }Panax Notoginseng voidROTR (int&W) { - intK = T[W].L; T[W].L = T[K].R; T[K].R =W; theUpdate (w); Update (k); W =K; + } A voidInsertintXint&W) { the if(!W) { +W = ++tot; T[W].V = x; T[w].size = T[W].W =1; -T[w].rnd = rand (); T[W].L = T[W].R =0;return; $ } $t[w].size++;if(T[W].V = = x) t[w].w++; - Else if(X <t[w].v) { - Insert (X,T[W].L); the if(T[t[w].l].rnd <t[w].rnd) ROTR (w); - }Wuyi Else{ the Insert (X,T[W].R); - if(T[t[w].r].rnd <t[w].rnd) Rotl (w); Wu } - } About voidbefintXintW) { $ if(!W)return; - if(T[W].V <=x) { -T1 =t[w].v; - bef (X,T[W].R); A } + Elsebef (X,T[W].L); the } - voidAftintXintW) { $ if(!W)return; the if(T[W].V >=x) { thet2 =t[w].v; the aft (X,T[W].L); the } - Elseaft (X,T[W].R); in } the intMain () { then =read (); AboutRep (I,1, N) { the if(SCANF ("%d", &x) = = EOF) x =0; theT1 =-inf; t2 =INF; the bef (x,root); aft (x,root); + if(i = =1) ans + =x; - ElseAns + = min (x-t1,t2-x); the Insert (x,root);Bayi } theprintf"%d\n", ans); the return 0; -}View Code
BZOJ1588 [HNOI2002] turnover statistics