首页 > 其他分享 >BZOJ1503(Splay)

BZOJ1503(Splay)

时间:2023-05-31 17:31:35浏览次数:46  
标签:Node pre ch Splay BZOJ1503 newNode tmp root


题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1503

 

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

struct Node
{
    int val,size,cnt,lazy;
    Node *pre,*ch[2];
    Node()
    {
        size = lazy = cnt = 0;
    }
};

Node *null,*root,*newNode,*last;

void Init()      //新增的newNode为root的父亲节点
{
    null = new(Node);
    root = null;
    newNode = new(Node);
    newNode->ch[0] = root;
    newNode->ch[1] = null;
    root->pre = newNode;
}

void Update(Node* &t)
{
    t->size = t->cnt + t->ch[0]->size + t->ch[1]->size;
    t->ch[0]->pre = t->ch[1]->pre = t;
}

int F(Node *t)    // 判断当前点的是左孩子还是右孩子
{
    return t->pre->ch[1] == t;
}

void Rotate(Node* t,int c)
{
    Node *k = t->ch[c];
    k->pre = t->pre;
    t->pre->ch[F(t)] = k;
    t->ch[c] = k->ch[!c];
    k->ch[!c] = t;
    Update(t);
    Update(k);
}

void Splay(Node* last,Node* newNode)
{
    Node *tmp = last;
    while(tmp->pre != newNode)
    {
        if(tmp->pre->pre == newNode)
            Rotate(tmp->pre,F(tmp));
        else if(F(tmp->pre) == F(tmp))
        {
            Rotate(tmp->pre->pre,F(tmp->pre));
            Rotate(tmp->pre,F(tmp));
        }
        else
        {
            Rotate(tmp->pre,F(tmp));
            Rotate(tmp->pre,F(tmp));
        }
    }
    root = newNode->ch[0];
}

void PushDown(Node* &t)
{
    t->ch[0]->val += t->lazy;
    t->ch[0]->lazy += t->lazy;
    t->ch[1]->val += t->lazy;
    t->ch[1]->lazy += t->lazy;
    t->lazy = 0;
}

void Insert(Node* &t,int x)
{
    if(t == null)
    {
        t=new(Node);
        t->val = x;
        t->ch[0] = t->ch[1] = null;
        t->size = t->cnt = 1;
        last = t;
        return;
    }
    PushDown(t);
    if(t->val == x) t->cnt++;
    if(t->val > x)  Insert(t->ch[0],x);
    if(t->val < x)  Insert(t->ch[1],x);
    Update(t);
}

Node *Find(Node *t,int x)  //查找后继,包括等于自己
{
    if(t == null) return null;
    PushDown(t);
    if(t->val == x) return t;
    if(t->val < x)  return Find(t->ch[1],x);
    if(t->val > x)
    {
        Node *tmp = Find(t->ch[0],x);
        if(tmp != null) return tmp;
        else return t;
    }
}

int Query(Node *t,int k)    //询问第K大
{
    if(t == null) return -1;
    PushDown(t);
    if(t->ch[1]->size >= k) return Query(t->ch[1],k);
    if(t->ch[1]->size + t->cnt >= k) return t->val;
    return Query(t->ch[0],k - t->ch[1]->size - t->cnt);
}

void Work()
{
    int n,limit;
    scanf("%d%d%*c",&n,&limit);
    int x,ans = 0;
    while(n--)
    {
        char c;
        scanf("%c %d%*c",&c,&x);
        if(c == 'I'&&x < limit) continue;
        if(c == 'I')
        {
            Insert(newNode->ch[0],x);
            Update(newNode);
            Splay(last,newNode);
        }
        else if(c == 'A')
        {
            root->val += x;
            root->lazy += x;
        }
        else if(c == 'S')
        {
            last = null;
            last = Find(root,limit + x);
            if(last == null)
            {
                ans += root->size;
                root = null;
                newNode->ch[0] = root;
                Update(newNode);
            }
            else
            {
                Splay(last,newNode);
                ans += root->ch[0]->size;
                root->ch[0] = null;
                root->val -= x;
                root->lazy -= x;
                Update(root);
            }
        }
        else if(c == 'F')
            printf("%d\n",Query(root,x));
    }
    printf("%d\n",ans);
}

int main()
{
    Init();
    Work();
    return 0;
}

 

标签:Node,pre,ch,Splay,BZOJ1503,newNode,tmp,root
From: https://blog.51cto.com/u_16146153/6388690

相关文章

  • display:flex
    display:flex是CSS3中的一种布局方式,它可以让元素以弹性盒子模型的方式进行排列,从而实现灵活的布局效果。该属性可以应用于任意元素,使其成为一个弹性容器,内部的子元素则根据弹性容器的排列规则进行布局。使用display:flex属性时,弹性容器的子元素会默认成为弹性项目,它们可以通过设......
  • 获取并改变display的值
    1.获取display的值//jquery.css("display")//js.style.display; 2.更改display的值//jquery方式.css("display","none");//js方式.style.display="none"; 转自https://blog.csdn.net/qq_41121204/article/details/92995933......
  • 【Shell】Display the ddl for all users in Oracle DB with bash script
     脚本说明:1、普遍用于使用expdp/impdp数据泵进行的数据(全库或者特定schemas)迁移2、适用于无PDB的Oracle环境3、适用于RAC,SI,ADG以及多实例的环境 使用方法:创建脚本为 display_all_users_ddl.sh然后将正文内容贴入并保存,然后执行bash display_all_users_ddl.sh......
  • ZCL_QALV_DISPLAY
    IntroductionHistoryPublicMethodGRID_TABNAMEGRID_ALVIDGRID_CUSTGET_VARIANTGET_FCAT_TABNAMEGET_FCAT_ALVIDCONTAINER_FACTORYEVENTIntroductionClass:ZCL_QALV_DISPLAYALV簡化用法(純Display,不可進editmode)HistoryVersionDateNameDescrip......
  • How to connect to multiple SSD1306 OLED Displays using Raspberry Pi GPIO I2C PIN
    HowtoconnecttomultipleSSD1306OLEDDisplaysusingRaspberryPiGPIOI2CPINAllInOne如何使用RaspberryPi的GPIOI2CPIN连接多个SSD1306OLED显示器demos(......
  • Raspberry Pi & 0.96 inch OLED display All In One
    RaspberryPi&0.96inchOLEDdisplayAllInOneI2CGPIOPythondemos(......
  • 【模板】 Splay
    splay#include<bits/stdc++.h>usingnamespacestd;constintN=5e6+10;intval[N],cnt[N],fa[N],ch[N][2],siz[N];inttot,root;voidmaintain(intx){ siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+cnt[x];}boolget(intx){ returnx==ch......
  • OrchardCore 中的 插件开发/ Shape / DisplayDriver / 视图扩展 / Razor代码注入
    请注意该文章仅限于OrchardCore项目中的DisplayDriver扩展机制,ASP.NETCOREMVC自身并没有对应功能,如果需要可以将相关的OrchardCore模块添加到项目中也可以实现响应功能背景最近一个功能需求,需要使用其它用户模拟身份,所以计划在用户列表页面扩展按钮组功能那么开始看代......
  • 使用nacos配置,启动服务时一直报 Error starting ApplicationContext. To display the
    报错日志如下:ErrorstartingApplicationContext.Todisplaytheconditionsreportre-runyourapplicationwith'debug'enabled.-2023-05-0509:46:02.328[TID:N/A]ERROR8236---[main]o.s.b.d.LoggingFailureAnalysisReporter:***********......
  • vue学习 第九天(1) 元素的显示与隐藏 display (不保留位置) / visibility (保留位置) /
    元素的显示与隐藏本质:让一个元素在页面中隐藏或者显示出来。1、display属性,隐藏后不保留位置1)display::none;隐藏对象2)display:block;除了转换为块级元素之外,同时还有显示元素的意思。display隐藏元素后,不再占有原来的位置。 2......