您的位置:首页 > 其它

BZOJ 1588: [HNOI2002]营业额统计

2017-05-20 20:17 309 查看


二次联通门:BZOJ1588:[HNOI2002]营业额统计

/*
BZOJ1588:[HNOI2002]营业额统计

splay
每次插入一个数
查询它的前驱与后继

有两点需要注意
1.大部分的数据有误。。即输入的数据不够。。
2.判断前驱后继是否存在
3.注意判断该数是否已出现过
*/
#include<cstdio>

#defineMax50000
#defineINF1e8

namespaceZ
{
inlineintmin(inta,intb)
{
returna<b?a:b;
}

inlineintabs(intx)
{
returnx>0?x:-x;
}

voidread(int&now)
{
now=0;
registercharword=getchar();
booltemp=false;
while(word<'0'||word>'9')
{
if(word=='-')
temp=true;
word=getchar();
}
while(word>='0'&&word<='9')
{
now=now*10+word-'0';
word=getchar();
}
if(temp)
now=-now;
}
}

classSplay_Tree_Type
{
private:

structSplay_Tree_Date
{
intkey;
intweigth;
intsize;
intfather;
intchild[2];
}
tree[Max];

intCount;
intRoot;

inlineintGet_Son(intnow)
{
returntree[tree[now].father].child[1]==now;
}

inlinevoidRotate(intnow)
{
intfather=tree[now].father;
intGrand=tree[father].father;
intpos=Get_Son(now);
tree[father].child[pos]=tree[now].child[pos^1];
tree[tree[father].child[pos]].father=father;
tree[now].child[pos^1]=father;
tree[father].father=now;
tree[now].father=Grand;
if(Grand)
tree[Grand].child[tree[Grand].child[1]==father]=now;
Update(father);
Update(now);
}

inlinevoidUpdate(intnow)
{
tree[now].size=tree[now].weigth;
if(tree[now].child[0])
tree[now].size+=tree[tree[now].child[0]].size;
if(tree[now].child[1])
tree[now].size+=tree[tree[now].child[1]].size;
}

inlinevoidSplay(intnow)
{
for(intfather;father=tree[now].father;Rotate(now))
if(tree[father].father)
Rotate(Get_Son(now)==Get_Son(father)?father:now);
Root=now;
}

public:

voidInsert(intx)
{
if(!Root)
{
Count++;
tree[Count].key=x;
tree[Count].size=1;
tree[Count].weigth=1;
Root=Count;
return;
}
intnow=Root,father=0;
while(true)
{
if(tree[now].key==x)
{
tree[now].size++;
tree[now].weigth++;
Splay(now);
return;
}
father=now;
now=tree[now].child[x>tree[now].key];
if(!now)
{
Count++;
tree[father].child[x>tree[father].key]=Count;
tree[Count].key=x;
tree[Count].father=father;
tree[Count].size=1;
tree[Count].weigth=1;
Splay(Count);
return;
}
}
}

intGet_Prefix()
{
if(tree[Root].weigth>1)
returntree[Root].key;
intnow=tree[Root].child[0];
if(now==0)
returnINF;
while(tree[now].child[1])
now=tree[now].child[1];
returntree[now].key;
}

intGet_Suffix()
{
if(tree[Root].weigth>1)
returntree[Root].key;
intnow=tree[Root].child[1];
if(now==0)
returnINF;
while(tree[now].child[0])
now=tree[now].child[0];
returntree[now].key;
}
};

Splay_Tree_TypeMake;

intN;

intmain(intargc,char*argv[])
{
Z::read(N);
intx;
intPrefix,Suffix;
Z::read(x);
Make.Insert(x);
intAnswer=Z::abs(x);
for(inti=1;i<N;i++)
{
if(scanf("%d",&x)==EOF)
x=0;
Make.Insert(x);
Prefix=Make.Get_Prefix();
Suffix=Make.Get_Suffix();
Answer+=Z::min(Z::abs(x-Prefix),Z::abs(x-Suffix));
}
printf("%d",Answer);
return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: