Bzoj P1588 [HNOI2002]营业额统计___treap

题目大意:

NAiN

N<=32767
Ai<=1,000,000
T<=231

分析:

直接构造一颗treap每次求前驱,后继即可
注意前驱跟后继要考虑相同数的情况

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define INF 23333333
#define N 100005

using namespace std;

struct Treap {
    int l, r, dat, val, cnt;
} a[N];
int tot, root, n;

int New(int val) {
    a[++tot].val = val;
    a[tot].dat = rand();
    a[tot].cnt = 1;
    return tot;
}

void Build() { 
    New(-INF); New(INF);
    root = 1; 
    a[root].r = 2;
}

void zig(int &p) {
    int q = a[p].l;
    a[p].l = a[q].r;
    a[q].r = p;
    p = q;
}

void zag(int &p) {
    int q = a[p].r;
    a[p].r = a[q].l;
    a[q].l = p;
    p = q;
}

void Insert(int &p, int val) {
    if (p == 0) {
        p = New(val);
        return;
    }
    if (val == a[p].val) {
        a[p].cnt++;
        return;
    }
    if (val < a[p].val) {
        Insert(a[p].l, val);
        if (a[p].dat < a[a[p].l].dat) zig(p);
    }
    else {
        Insert(a[p].r, val);
        if (a[p].dat < a[a[p].r].dat) zag(p);
    }
}

int GetPre(int val) {
    int ans = 1;
    int p = root;
    while (p) {
        if (val == a[p].val) {
            ans = p;
            break;
        }
        if (a[p].val < val && a[p].val > a[ans].val) ans = p;
        p = val < a[p].val ? a[p].l : a[p].r;
    }
    return a[ans].val;
}

int GetNext(int val) {
    int ans = 2;
    int p = root;
    while (p) {
        if (val == a[p].val) {
            ans = p;
            break;
        }
        if (a[p].val > val && a[p].val < a[ans].val) ans = p;
        p = val < a[p].val ? a[p].l : a[p].r;
    }
    return a[ans].val;
}

int main() {
    Build();
    scanf("%d", &n);
    int ans = 0, x;
    for (int i = 1; i <= n; i++) {
           scanf("%d", &x);
           int a = GetPre(x);
           int b = GetNext(x);
           Insert(root, x);
           if (i != 1) ans += min(x - a, b - x);
                  else ans += x;
    }
    printf("%d\n", ans);
}

阅读更多
版权声明:欢迎借鉴,谢绝抄搬。 https://blog.csdn.net/Gx_Man_VIP/article/details/80457154
个人分类: C++ Treap
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭