营业额统计

裸地平衡树,维护前驱后继。

// luogu-judger-enable-o2
#include<cstdio>
#include<iostream>
using namespace std;
int abs(int x){return x>0?x:-x;}
int n,root=0,tot=0,ans,inf=0x7ffffff;
struct Splaytree{
    int ch[110000][3];
    int key[110000], size[190099],cnt[110000],f[110000];
    inline void clear(int x){
        key[x]=size[x]=cnt[x]=f[x]=ch[x][0]=ch[x][1]=0;
    }
    inline void update(int x){
        if(x){
            size[x]=cnt[x];
            if(ch[x][0])
            size[x]+=size[ch[x][0]];
            if(ch[x][1])
            size[x]+=size[ch[x][1]];
        }       
    }
    inline int get(int x){
        return ch[f[x]][1]==x;
    }
    inline void retoto(int x){
        int old=f[x],old2=f[f[x]],opt=get(x),opt2=get(f[x]);
        ch[old][opt]=ch[x][opt^1];f[ch[x][opt^1]]=old;
        f[old]=x;ch[x][opt^1]=old;
        f[x]=old2;
        if(old2)ch[old2][opt2]=x;
        update(old);update(x);
    }
    inline void splay(int x){
        for(int i;i=f[x];retoto(x)) if(f[i]) {
            retoto((get(x)==get(i))?i:x);
        }
        root=x;
    }
    inline void insert(int x){
        int tmp=root,fa=0;
        if(!root){f[++tot]=ch[tot][1]=ch[tot][0]=0,key[tot]=x,size[tot]=cnt[tot]=1,root=tot;return;}
        while(1){
            if(key[tmp]==x) {
                cnt[tmp]++,update(tmp),update(fa);splay(tmp);return ;
            }
            fa=tmp;
            tmp=ch[tmp][key[tmp]<x];
            if(!tmp){
                f[++tot]=fa,ch[fa][key[fa]<x]=tot,key[tot]=x,size[tot]=cnt[tot]=1;
                update(fa);
                splay(tot);return;
            }
        }
    }

    inline int pre(){
        if(cnt[root]>1) return key[root];
        int tmp=ch[root][0];if(!tmp)return inf;
        while(ch[tmp][1]) tmp=ch[tmp][1];
        return key[tmp];
    }
    inline int next(){
        if(cnt[root]>1) return key[root];
        int tmp=ch[root][1];if(!tmp) return -inf;
        while(ch[tmp][0]) tmp=ch[tmp][0];
        return key[tmp];
    }
}s;
int main(){
    scanf("%d",&n);//s.insert(-inf),s.insert(inf);
    for(int i=1,x;i<=n;i++){
        scanf("%d",&x);
        s.insert(x);
        ans+=(i==1)?x:min(abs(x-s.pre()),abs(s.next()-x));
    }
    printf("%d",ans); 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你可以参考以下示例代码实现Java销售额查询和营业额统计: ```java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class SalesStatistics { private Map<String, Double> salesData = new HashMap<>(); public SalesStatistics() { // 初始化销售数据 salesData.put("2021-01-01", 1000.0); salesData.put("2021-01-02", 1500.0); salesData.put("2021-01-03", 2000.0); salesData.put("2021-01-04", 1200.0); salesData.put("2021-01-05", 1800.0); } // 查询指定日期的销售额 public double getSalesByDate(String date) { if (salesData.containsKey(date)) { return salesData.get(date); } else { return 0.0; } } // 统计指定日期范围内的营业额 public double getTotalSales(String startDate, String endDate) { double totalSales = 0.0; for (String date : salesData.keySet()) { if (date.compareTo(startDate) >= 0 && date.compareTo(endDate) <= 0) { totalSales += salesData.get(date); } } return totalSales; } // 获取所有销售日期 public List<String> getAllSalesDates() { return new ArrayList<>(salesData.keySet()); } } ``` 使用示例: ```java public static void main(String[] args) { SalesStatistics salesStatistics = new SalesStatistics(); // 查询指定日期的销售额 double sales = salesStatistics.getSalesByDate("2021-01-02"); System.out.println("2021-01-02的销售额为:" + sales); // 统计指定日期范围内的营业额 double totalSales = salesStatistics.getTotalSales("2021-01-02", "2021-01-04"); System.out.println("2021-01-02到2021-01-04的营业额为:" + totalSales); // 获取所有销售日期 List<String> salesDates = salesStatistics.getAllSalesDates(); System.out.println("所有销售日期为:" + salesDates); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值