BZOJ 1588 营业额统计(splay)

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588

题意:给出一个n个数的数列a,对于第i个元素ai定义fi=min(abs(ai-aj)),(1<=j<i),其中f1=a1。输出sum(fi) (1<=i<=n)。

思路:(1)第一步插入x;插入时x小于当前节点插到左孩子,否则插到右孩子;

(2)将新插入的节点splay到根节点;

(3)找根节点的前驱值和后继值。前驱值就是左孩子的最右孩子;后继节点就是右孩子的最左节点。然后比较哪个值更优。

#include <iostream>

#include <cstdio>

#include <string.h>

#include <algorithm>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <map>

#include <ctime>





#define max(x,y) ((x)>(y)?(x):(y))

#define min(x,y) ((x)<(y)?(x):(y))

#define abs(x) ((x)>=0?(x):-(x))

#define i64 long long

#define u32 unsigned int

#define u64 unsigned long long

#define clr(x,y) memset(x,y,sizeof(x))

#define CLR(x) x.clear()

#define ph(x) push(x)

#define pb(x) push_back(x)

#define Len(x) x.length()

#define SZ(x) x.size()

#define PI acos(-1.0)

#define sqr(x) ((x)*(x))

#define MP(x,y) make_pair(x,y)

#define EPS 1e-20



#define FOR0(i,x) for(i=0;i<x;i++)

#define FOR1(i,x) for(i=1;i<=x;i++)

#define FOR(i,a,b) for(i=a;i<=b;i++)

#define FORL0(i,a) for(i=a;i>=0;i--)

#define FORL1(i,a) for(i=a;i>=1;i--)

#define FORL(i,a,b)for(i=a;i>=b;i--)





#define rush(n) for(scanf("%d",&C);C--;)

#define Rush(n)  while(scanf("%d",&n)!=-1)

using namespace std;





void RD(int &x){scanf("%d",&x);}

void RD(i64 &x){scanf("%I64d",&x);}

void RD(u32 &x){scanf("%u",&x);}

void RD(double &x){scanf("%lf",&x);}

void RD(int &x,int &y){scanf("%d%d",&x,&y);}

void RD(i64 &x,i64 &y){scanf("%I64d%I64d",&x,&y);}

void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}

void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}

void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}

void RD(i64 &x,i64 &y,i64 &z){scanf("%I64d%I64d%I64d",&x,&y,&z);}

void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}

void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}

void RD(char &x){x=getchar();}

void RD(char *s){scanf("%s",s);}

void RD(string &s){cin>>s;}





void PR(int x) {printf("%d\n",x);}

void PR(i64 x) {printf("%lld\n",x);}

void PR(u32 x) {printf("%u\n",x);}

void PR(double x) {printf("%.6lf\n",x);}

void PR(char x) {printf("%c\n",x);}

void PR(char *x) {printf("%s\n",x);}

void PR(string x) {cout<<x<<endl;}





const i64 inf=((i64)1)<<60;

const int INF=2147483647;

const int N=2000005;





struct node

{

    i64 key;

    node *c[2];

    node *p;

};



node a[N],*root,*null;

int cnt;





node *newNode(int key,node *p)

{

    node *e=a+(cnt++);

    e->key=key;

    e->c[0]=e->c[1]=null;

    e->p=p;

    return e;

}



void init()

{

    cnt=0;

    null=0;

    null=newNode(INF,0);

}







void rotate(node *x,int k)

{

    node *y=x->p;

    y->c[k]=x->c[!k];

    y->c[k]->p=y;

    x->p=y->p;

    if(y->p->c[0]==y) y->p->c[0]=x;

    else y->p->c[1]=x;

    y->p=x;

    x->c[!k]=y;

    if(root==y) root=x;

}





void splay(node *x,node *y)

{

    while(x->p!=y)

    {

        if(x->p->p==y)

        {

            if(x->p->c[0]==x) rotate(x,0);

            else rotate(x,1);

        }

        else if(x->p->p->c[0]==x->p)

        {

            if(x->p->c[0]==x) rotate(x->p,0);

            else rotate(x,1);

            rotate(x,0);

        }

        else

        {

            if(x->p->c[1]==x) rotate(x->p,1);

            else rotate(x,0);

            rotate(x,1);

        }

    }

}







node* insert(int x)

{

    node *p=root,*pre;

    while(p!=null)

    {

        pre=p;

        if(x<p->key) p=p->c[0];

        else p=p->c[1];

    }

    p=newNode(x,pre);

    if(x<pre->key) pre->c[0]=p;

    else pre->c[1]=p;

    return p;

}





i64 getPre()

{

    node *p=root;

    if(p->c[0]==null) return inf;

    p=p->c[0];

    while(p->c[1]!=null) p=p->c[1];

    return p->key;

}



i64 getNext()

{

    node *p=root;

    if(p->c[1]==null) return inf;

    p=p->c[1];

    while(p->c[0]!=null) p=p->c[0];

    return p->key;

}



int n;



int main()

{

    Rush(n)

    {

        init();

        int i,x;

        i64 ans=0,pre,next;

        node *p;

        FOR1(i,n)

        {

            if(scanf("%d",&x)==-1) x=0;

            if(i==1) ans=x,root=newNode(x,null);

            else

            {

                p=insert(x);

                splay(p,null);

                pre=getPre();

                next=getNext();

                ans+=min(abs(x-pre),abs(x-next));

            }

        }

        PR(ans);

    }

    return 0;

}

  

 

你可能感兴趣的:(play)