zoukankan      html  css  js  c++  java
  • Splay源代码(营业额统计 2234)

           #pragma GCC optimize(2)
            #include<bits/stdc++.h>
            using namespace std;
            int n,L=1,R=0,ans,root=1,cnt=1,minn,maxx;
            struct tree{
                int son[2];int num;int fa;
            }N[1000005];
            void searchr(int k){
                if(N[k].son[L]==0&&k!=root) return;
                if(N[root].son[R]==0) return;
                if(k==root){
                    minn=N[N[k].son[R]].num;
                    searchr(N[k].son[R]);
                }
                if(k!=root){
                    minn=N[N[k].son[L]].num;
                    searchr(N[k].son[L]);
                }
            }
            void searchl(int k){
                if(N[k].son[R]==0&&k!=root) return;
                if(N[root].son[L]==0) return;
                if(k==root){
                    maxx=N[N[k].son[L]].num;
                    searchl(N[k].son[L]);
                }
                if(k!=root){
                    maxx=N[N[k].son[R]].num;
                    searchl(N[k].son[R]);
                }
            }
            void rotate(int k){
                if(N[N[k].fa].son[L]==k){
                    int t=N[k].son[R];
                    int fafa=N[N[k].fa].fa;
                    int fa=N[k].fa;
                    N[k].son[R]=fa;
                    N[fa].fa=k;
                    N[k].fa=fafa;
                    if(fafa!=0){
                      int y=R;
                      if(fa==N[fafa].son[L]) y=L;
                      N[fafa].son[y]=k;
                    }
                     N[fa].son[L]=t;
                    if(t!=0)N[t].fa=fa;
                }
                else{
                    int t=N[k].son[L];
                    int fafa=N[N[k].fa].fa;
                    int fa=N[k].fa;
                    N[k].son[L]=fa;
                    N[fa].fa=k;
                    N[k].fa=fafa;
                    if(fafa!=0){
                      int y=R;
                      if(fa==N[fafa].son[L]) y=L;
                      N[fafa].son[y]=k;
                    }
                    N[fa].son[R]=t;
                    if(t!=0) N[t].fa=fa;
                }
            }
            void splay(int k){
                while(N[k].fa!=0){
                    if(N[N[k].fa].fa==0){
                       rotate(k);
                       if(N[root].fa!=0) root=N[root].fa;
                    }
                    else{
                       int u1=L;
                       if(N[k].fa==N[N[N[k].fa].fa].son[R]) u1=R;
                       int u2=L;
                       if(k==N[N[k].fa].son[R]) u2=R;
                       if(u1==u2) rotate(N[k].fa);
                       else rotate(k);
                       if(N[root].fa!=0) root=N[root].fa;
                       rotate(k);
                       if(N[root].fa!=0) root=N[root].fa;
                    }
                }
                minn=10000000;
                maxx=-10000000;
                searchl(root);
                searchr(root);
                int a1=abs(minn-N[k].num);
                int a2=abs(N[k].num-maxx);
                ans+=a1<=a2?a1:a2;
            }
            void build(int k,int a){
                if(N[k].num<a){
                    if(N[k].son[R]==0){
                       int y;++cnt;y=cnt;
                       N[k].son[R]=y;
                       N[y].num=a;
                       N[y].fa=k;
                       splay(y);return;
                    }
                    else build(N[k].son[R],a);
                }
                if(N[k].num>a){
                    if(N[k].son[L]==0){
                       int y;++cnt;y=cnt;
                       N[k].son[L]=y;
                       N[y].num=a;
                       N[y].fa=k;
                       splay(y);return;
                    }
                    else build(N[k].son[L],a);
                }
            }
            int main()
            {
                N[0].num=100000000;
                scanf("%d",&n);
                int a;
                scanf("%d",&a);
                N[1].num=a;
                ans+=a;
                for(int i=2;i<=n;++i){
                    scanf("%d",&a);
                    build(root,a);
                }
                printf("%d",ans);
                return 0;
    }
    
  • 相关阅读:
    【Linux常用命令】 cat
    【Linux常用命令】 chmod
    【2012.4.22】北京植物园&卧佛寺
    【Linux常用命令】 重定向输出 > 和 >>
    一些话
    linux下查看用户个数和具体名字
    【Linux常用命令】 ls
    Ethernet frame
    防止修改类和方法
    redis数据批量导入导出
  • 原文地址:https://www.cnblogs.com/mudrobot/p/13328931.html
Copyright © 2011-2022 走看看