fhq treap求调
  • 板块P2073 送花
  • 楼主histcat
  • 当前回复7
  • 已保存回复7
  • 发布时间2022/7/28 15:00
  • 上次更新2023/10/27 18:00:58
查看原帖
fhq treap求调
361592
histcat楼主2022/7/28 15:00

似乎是按大小分裂出了错误,但调不出来qwq,求dalao

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;
mt19937 rnd(233);


struct Node
{
    int val, key;
    int l, r;
    int size, beauty;
}fhq[N];

int cnt, x, y, z, root;

void update(int x)
{
    fhq[x].size = fhq[fhq[x].l].size + fhq[fhq[x].r].size + 1;
}

int newnode(int val, int beauty)
{
    fhq[++cnt].val = val;
    fhq[cnt].size = 1;
    fhq[cnt].beauty = beauty;
    fhq[cnt].key = rnd();
    return cnt;
}

void split_by_size(int now, int siz, int &x, int &y)
{
    if(!now)
    {
        x = y = 0;
        return;
    }
    if(fhq[now].size > siz)
    {
        y = now;
        split_by_size(fhq[now].l, siz, x, fhq[now].l);
        update(now);
    }
    else
    {
        x = now;
        split_by_size(fhq[now].r, siz - fhq[fhq[now].l].size - 1, fhq[now].r, y);
        update(now);
    }
}

void split_by_val(int now, int val, int &x, int &y)
{
    if(!now)
    {
        x = y = 0;
        return;
    }
    if(fhq[now].val <= val)
    {
        x = now;
        split_by_val(fhq[now].r, val, fhq[now].r, y);
        update(now);
    }
    else
    {
        y = now;
        split_by_val(fhq[now].l, val, x, fhq[now].l);
        update(now);
    }
}

int merge(int x, int y)//x上所有val <= y上所有val
{
    if(!x || !y)
    {
        return x + y;
    }
    if(fhq[x].key > fhq[y].key)
    {
        fhq[x].r = merge(fhq[x].r, y);
        update(x);
        return x;
    }
    else
    {
        fhq[y].l = merge(x, fhq[y].l);
        update(y);
        return y;
    }
}

void insert(int val, int beauty)
{
    split_by_val(root, val, x, y);
    split_by_val(x, val - 1, x, z);
    if(z == 0)
    {
        root = merge(merge(x, newnode(val ,beauty)), y);
    }
    else
    {
        root = merge(merge(x, z), y);
    }
}

void delete_the_cheapest()
{
    int a;
    split_by_size(root, 1, a, root);
}

void delete_the_most_expensive()
{
    int siz = fhq[root].size;
    int a;
    split_by_size(root, siz - 1, root, a);
}

int piaoliangzhi, jiaqian;

void dfs(int u)
{
    if(!u) return;
    dfs(fhq[u].l);
    jiaqian += fhq[u].val;
    piaoliangzhi += fhq[u].beauty;
    cout << "--------" << u <<" "<< fhq[u].val <<" "<< fhq[u].beauty << endl;
    dfs(fhq[u].r);
}

int main()
{
    clock_t c1 = clock();
#ifdef LOCAL
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
    //------------------------------
    int opt, a, b;

    while(1)
    {
        scanf("%d", &opt);
        if(opt == -1)
        {
            break;
        }
        else if(opt == 1)
        {
            scanf("%d%d", &a, &b);
            insert(b, a);
            dfs(root);
            cout<< endl;
        }
        else if(opt == 2)
        {
            delete_the_most_expensive();
            dfs(root);
            cout<<endl;
        }
        else
        {
            delete_the_cheapest();
            dfs(root);
            cout<<endl;
        }
    }
    

    cout << piaoliangzhi << " " << jiaqian;
    
    //------------------------------
end:
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
    return 0;
}
2022/7/28 15:00
加载中...