BZOJ 1588 [HNOI2002]营业额统计 (Splaytree)

用Splaytree实现基本的插入,求前驱,求后继操作。

数据有问题。。。有些数据输入n后,后面没有n个数,后面没有的数全都当成0才可以过。


#include 
   
   
    
    
#include 
    
    
     
     
using namespace std;
typedef long long ll;
 
#define lson x->ch[0]
#define rson x->ch[1]
#define ket (root->ch[1]->ch[0])
 
const int maxn = 33333;
const int INF = 1LL<<31-1;
 
struct NODE {
    NODE *pre, *ch[2];
    int val, sz, id;
 
    void up() {
        sz = ch[0]->sz + ch[1]->sz + 1;
    }
}node[maxn], *null = &node[0], *root;
 
struct Splaytree {
    int top;
 
    void Rotate(NODE *x, int c) {
        NODE *y = x->pre;
        y->ch[!c] = x->ch[c];
        x->ch[c]->pre = y;
        x->pre = y->pre;
        if(y->pre != null)  y->pre->ch[ y->pre->ch[1] == y] = x;
        x->ch[c] = y; y->pre = x;
        y->up();
    }
 
    void Splay(NODE *x, NODE *go) {
        while(x->pre != go) {
            if(x->pre->pre == go)   Rotate(x, x->pre->ch[0] == x);
            else {
                NODE *y = x->pre, *z = y->pre;
                int f = z->ch[1] == y;
                if(y->ch[f] == x)   Rotate(y, !f);
                else    Rotate(x, f);
                Rotate(x, !f);
            }
        }
        x->up();
        if(go == null)  root = x;
    }
 
    void RTO(int k, NODE *go) {
        NODE *x = root;
        while(lson->sz != k) {
            if(lson->sz > k)    x = lson;
            else {
                k -= lson->sz + 1;
                x = rson;
            }
        }
        Splay(x, go);
    }
 
    NODE *newnode(int c, NODE *f=null) {
        NODE *x = &node[++top];
        lson = rson = null;
        x->id = top; x->pre = f;
        x->val = c; x->sz = 1;
        return x;
    }
 
    NODE* Insert(int c) {
        if(root == null) return root = newnode(c);
        NODE *x = root;
        while(x->ch[ x->val < c] != null) {
            if(x->val == c) {
                Splay(x, null);
                return null;
            }
            x = x->ch[ x->val < c];
        }
        if(x->val == c) {
            Splay(x, null);
            return null;
        }
        x->ch[ x->val < c] = newnode(c, x);
        Splay(x->ch[x->val
     
     
      
      ch[0] == null)    return -1;
        x = x->ch[0];
        while(x->ch[1] != null) x = x->ch[1];
        return x->val;
    }
 
    int find_nxt(NODE *x) {
        if(x->ch[1] == null)    return -1;
        x = x->ch[1];
        while(x->ch[0] != null) x = x->ch[0];
        return x->val;
    }
 
    void init() {
        null->sz = null->val = null->id = 0;
        top = 0; root = null;
    }
 
    void solve() {
        int n, v;
        scanf("%d%d", &n, &v);
        init();
        Insert(v);
        int ans = v;
        for(int i = 0;i < n-1; i++) {
            if(scanf("%d", &v) == -1) v = 0;
            if(Insert(v) == null)   continue;
            int add = INF;
            int cur = find_pre(root);
            if(cur != -1)   add = min(add, v-cur);
            cur = find_nxt(root);
            if(cur != -1)   add = min(add, cur-v);
            ans += add;
        }
        printf("%d\n", ans);
    }
}spt;
 
int main() {
    spt.solve();
    return 0;
}

     
     
    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BZOJ 2908 题目是一个数据下载任务。这个任务要求下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要求。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值