首页 > 其他分享 >模板

模板

时间:2024-06-15 12:54:35浏览次数:8  
标签:return get int tr rank key 模板

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010, INF = 1e8;

int n;
struct Node
{
    int l, r;
    int key, val;
    int cnt, size;
}tr[N];

int root, idx;

void pushup(int p)
{
    tr[p].size = tr[tr[p].l].size + tr[tr[p].r].size + tr[p].cnt;
}

int get_node(int key)
{
    tr[ ++ idx].key = key;
    tr[idx].val = rand();
    tr[idx].cnt = tr[idx].size = 1;
    return idx;
}

void zig(int &p)    // 右旋
{
    int q = tr[p].l;
    tr[p].l = tr[q].r, tr[q].r = p, p = q;
    pushup(tr[p].r), pushup(p);
}

void zag(int &p)    // 左旋
{
    int q = tr[p].r;
    tr[p].r = tr[q].l, tr[q].l = p, p = q;
    pushup(tr[p].l), pushup(p);
}

void build()
{
    get_node(-INF), get_node(INF);
    root = 1, tr[1].r = 2;
    pushup(root);

    if (tr[1].val < tr[2].val) zag(root);
}


void insert(int &p, int key)
{
    if (!p) p = get_node(key);
    else if (tr[p].key == key) tr[p].cnt ++ ;
    else if (tr[p].key > key)
    {
        insert(tr[p].l, key);
        if (tr[tr[p].l].val > tr[p].val) zig(p);
    }
    else
    {
        insert(tr[p].r, key);
        if (tr[tr[p].r].val > tr[p].val) zag(p);
    }
    pushup(p);
}

void remove(int &p, int key)
{
    if (!p) return;
    if (tr[p].key == key)
    {
        if (tr[p].cnt > 1) tr[p].cnt -- ;
        else if (tr[p].l || tr[p].r)
        {
            if (!tr[p].r || tr[tr[p].l].val > tr[tr[p].r].val)
            {
                zig(p);
                remove(tr[p].r, key);
            }
            else
            {
                zag(p);
                remove(tr[p].l, key);
            }
        }
        else p = 0;
    }
    else if (tr[p].key > key) remove(tr[p].l, key);
    else remove(tr[p].r, key);

    pushup(p);
}

int get_rank_by_key(int p, int key)    // 通过数值找排名
{
    if (!p) return 1;   // 本题中不会发生此情况
    if (tr[p].key == key) return tr[tr[p].l].size + 1;
    if (tr[p].key > key) return get_rank_by_key(tr[p].l, key);
    return tr[tr[p].l].size + tr[p].cnt + get_rank_by_key(tr[p].r, key);
}

int get_key_by_rank(int p, int rank)   // 通过排名找数值
{
    if (!p) return INF;     // 本题中不会发生此情况
    if (tr[tr[p].l].size >= rank) return get_key_by_rank(tr[p].l, rank);
    if (tr[tr[p].l].size + tr[p].cnt >= rank) return tr[p].key;
    return get_key_by_rank(tr[p].r, rank - tr[tr[p].l].size - tr[p].cnt);
}

int get_prev(int p, int key)   // 找到严格小于key的最大数
{
    if (!p) return -INF;
    if (tr[p].key >= key) return get_prev(tr[p].l, key);
    return max(tr[p].key, get_prev(tr[p].r, key));
}

int get_next(int p, int key)    // 找到严格大于key的最小数
{
    if (!p) return INF;
    if (tr[p].key <= key) return get_next(tr[p].r, key);
    return min(tr[p].key, get_next(tr[p].l, key));
}

int main()
{
    build();

    scanf("%d", &n);
    while (n -- )
    {
        int opt, x;
        scanf("%d%d", &opt, &x);
        if (opt == 1) insert(root, x);
        else if (opt == 2) remove(root, x);
        else if (opt == 3) printf("%d\n", get_rank_by_key(root, x) - 1);
        else if (opt == 4) printf("%d\n", get_key_by_rank(root, x + 1 ));
        else if (opt == 5) printf("%d\n", get_prev(root, x));
        else printf("%d\n", get_next(root, x));
    }

    return 0;
}

啊啊啊

标签:return,get,int,tr,rank,key,模板
From: https://www.cnblogs.com/fanrunze/p/18249202

相关文章

  • flask路由系统、偏函数、CBV、模板、请求响应、session、请求扩展
    路由系统1代码演示23fromflaskimportFlask45app=Flask(__name__)67app.debug=True8#路由基本使用9#@app.route('/',methods=['GET'])10#@app.get()11#@app.post()12defindex(name):13print(name)14return&......
  • 【前端求助帖】关于使用element-plus select 模板嵌套popover中使用select选择后,上一
    先看下效果主页代码如下项目使用的是Vue3+vite,下载后,直接pnpm i安装依赖, pnpmdev就是可以跑起来<el-buttontype="warning"round@click="openDia">打开弹框</el-button><el-dialogv-model="dialogTableVisible"title="业务"width=......
  • 清新优雅&高颜值!一个基于Vue3实现的后台管理模板
    大家好,我是Java陈序员。今天,给大家介绍一个高颜值的开源后台管理模板,已经收获了8k+Star!关注微信公众号:【Java陈序员】,获取开源项目分享、AI副业分享、超200本经典计算机电子书籍等。项目介绍SoybeanAdmin——一个清新优雅、高颜值且功能强大的后台管理模板。基于最新的......
  • 设计模式:一个应用理解模板方法模式
    一个需求在实际生产开发中,数据库初始化、升级是没办法规避的,一般常见的方案是外挂一套初始化脚本加一堆SQL文件,其实可以把这个过程做到系统里,做到一个程序包内自带数据库的初始化,或者数据库升级,所以需求就是做一个数据库的初始化、升级的java功能,使用过flyway的同学应该能更明......
  • Beta版会议总结(事后诸葛亮模板)
    **1.*以“事后诸葛亮”为模板总结会议header1、我们的软件要解决什么问题?是否定义的很清楚?是否对典型用户和典型场景有清晰的描述?主要是要方便老师学生的生活,少跑一趟取快递时间可用做其他事情,而取快递的人可以通过拿一次快递,挣一顿饭钱,方便自己方便他人;......
  • 基于C#开发web网页管理系统模板流程-总集篇
    第一篇基于C#开发web网页管理系统模板流程-登录界面和主界面_c#的网页编程-CSDN博客第二篇基于C#开发web网页管理系统模板流程-主界面管理员录入和编辑功能完善_c#网页设计-CSDN博客第三篇基于C#开发web网页管理系统模板流程-主界面管理员入库和出库功能完善_c#web程序......
  • 创建entity模板,equals hashcode 方法模板
    创建entity模板,equalshashcode方法模板如下为FLINK官网实体类demoequalshashcode方法模板可以参考////Sourcecoderecreatedfroma.classfilebyIntelliJIDEA//(poweredbyFernFlowerdecompiler)//packageorg.apache.flink.walkthrough.common.entity;im......
  • Laravel 解决blade模板转义html标签问题
    当我们使用富文本编译器(如:Ueditor编译器)保存编辑的内容后,在blade模板中,想要显示原生的html标签内容时该怎么做?首先,了解下laravel{{变量名}}与{!!变量名!!}区别{{变量名}}:转义输出,只是被当成普通的字符串输出{!!变量名!!}:原生输出html,比如图片,链接,JS代码等实例:编译器......
  • 组长:你熟悉过React,开发个Next项目模板吧,我:怎么扯上关系的?
    组长:你熟悉过React,开发个Next项目模板吧,我:怎么扯上关系的?最近工作安排我开发一个Next.js项目模板,心里默笑,React用得少得都快忘光了,现在得搞Next?虽然我曾是React的老用户,但转投Vue阵营已久,React的点点滴滴早已一干二净。不过,挑战归挑战,规矩还得照做。我们通常会用内部工具来搭......
  • 仿函数&模板特化
    仿函数基本介绍仿函数的本质就是一个类,此类中运算符重载了括号()!所以它使用起来和函数很相似,就叫做仿函数在标准库的优先级队列的类模板中有一个缺省参数叫less,这个less就是一个仿函数,它会将优先级队列变成大堆,在算法库的sort函数默认是升序,其实就是用的less......