• 【codevs 1296】营业额统计 水~~


    今天下午先写一个Splay水题来复习一下Splay模板。是不是有点太水了做这种水题我有点良心不安。

    可笑的是一开始我竟然WA了一组,看来是我低估水题的数据范围了,我是空节点直接返回inf或-inf,明显是不合理的。比赛时再犯这种低级错误就真的滚粗了。

    2016-07-29 当时我确实狂妄自大~~~~(>_<)~~~~

    #include<cstdio>
    #include<cctype>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int inf=1E9;
    inline const int max(const int &a,const int &b){return a>b?a:b;}
    inline const int min(const int &a,const int &b){return a<b?a:b;}
    struct node{
    	node();
    	node *fa,*ch[2];
    	int d;
    	short pl(){return this==fa->ch[1];}
    }*null;
    node::node(){ch[0]=ch[1]=fa=null;}
    namespace Splay{
    	node *ROOT;
    	void build(){
    		null=new node;
    		*null=node();
    		ROOT=null;
    	}
    	void rotate(node *k){
    		node *r=k->fa; if (k==null||r==null) return;
    		int x=k->pl()^1;
    		r->ch[x^1]=k->ch[x];
    		r->ch[x^1]->fa=r;
    		if (r->fa==null) ROOT=k;
    		else r->fa->ch[r->pl()]=k;
    		k->fa=r->fa; r->fa=k;
    		k->ch[x]=r;
    	}
    	void splay(node *r,node *tar=null){
    		for (;r->fa!=tar;rotate(r))
    		 if (r->fa->fa!=tar)rotate(r->pl()==r->fa->pl()?r->fa:r);
    	}
    	void insert(int x){
    		if (ROOT==null){
    			ROOT=new node;
    			ROOT->d=x;
    			return;
    		}node *r=ROOT;
    		while (1){
    			int c;
    			if (x<r->d) c=0;
    			else c=1;
    			if (r->ch[c]==null){
    				r->ch[c]=new node;
    				r->ch[c]->d=x;
    				r->ch[c]->fa=r;
    				splay(r->ch[c]);
    				return;
    			}else r=r->ch[c];
    		}
    	}
    	int leftmax(){
    		node *r=ROOT->ch[0];
    		while (r->ch[1]!=null) r=r->ch[1];
    		return r->d;
    	}
    	int rightmin(){
    		node *r=ROOT->ch[1];
    		while (r->ch[0]!=null) r=r->ch[0];
    		return r->d;
    	}
    	int que(){
    		int x=inf;
    		if (ROOT->ch[0]!=null) x=ROOT->d-leftmax();
    		if (ROOT->ch[1]!=null) x=min(x,rightmin()-ROOT->d);
    		return x;
    	}
    }
    int getint(){char c;int fh=1;while (!isdigit(c=getchar()))if(c=='-')fh=-1;int a=c-'0';while (isdigit(c=getchar()))a=a*10+c-'0';return a*fh;}
    int main(){
    	int n=getint();
    	Splay::build();
    	int x=getint();
    	long long ans=x;
    	Splay::insert(x);
    	for (int i=1;i<n;++i){
    		x=getint();
    		Splay::insert(x);
    		ans+=Splay::que();
    	}printf("%lld
    ",ans);
    	return 0;
    }
    

    这样就可以了

  • 相关阅读:
    +7白盒测试与黑盒测试的定义与区别
    对于逆序数组,,选择排序 和 插入排序 那个更快?
    +1.1向量 链表 数组
    3.2 二十三种设计模式全解析
    3.1软件体系结构风格
    Mysql: 强制走索引:mysql between 日期索引 索引问题-日期索引使用
    php: xampp安装对应的phalcon版本(3.2.2-php5.6):比如redis-php5.6, php_igbinary-5.6
    京东自营,你TM太坑了。
    php:微信公众平台上传多媒体接口 41005错误
    php:如何使用PHP排序, key为字母+数字的数组(多维数组)
  • 原文地址:https://www.cnblogs.com/abclzr/p/5198357.html
Copyright © 2020-2023  润新知