BZOJ1503(Splay)
题目: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;
}
BZOJ1503(Splay)相关推荐
- 【题解】 bzoj1503: [NOI2004]郁闷的出纳员 (Splay)
bzoj1503,懒得复制,戳我戳我 Solution: 我知不知道我是那根筋抽了突然来做splay,调了起码\(3h+\),到第二天才改出来(我好菜啊),当做训练调错吧 一个裸的splay,没啥好说 ...
- Splay初步【bzoj1503】
做了一道水题,把bzoj1503用Splay重新写了一下. 1 #include <bits/stdc++.h> 2 #define rep(i, a, b) for (int i = a ...
- [BZOJ1503]郁闷的出纳员(Splay)
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...
- 【codevs1286】【BZOJ1503】郁闷的出纳员,splay练习
传送门1 传送门2 写在前面:代码一长,细节问题就多了起来,还是需要熟练啊 思路:插入,删除,查第k大的splay,再加一个改值函数,因为改动工资的操作很少(才100),所以直接暴力从根开始往下改,小 ...
- 二逼平衡树——树套树(线段树套Splay平衡树)
题面 Bzoj3196 解析 线段树和Splay两棵树套在一起,常数直逼inf,但最终侥幸过了 思路还是比较简单, 在原数组维护一个下标线段树,再在每一个线段树节点,维护一个对应区间的权值Splay. ...
- 简析平衡树(三)——浅谈Splay
前言 原本以为\(Treap\)已经很难了,学习了\(Splay\),我才知道,没有最难,只有更难.(强烈建议先去学一学\(Treap\)再来看这篇博客) 简介 \(Splay\)是平衡树中的一种,除 ...
- AVL树、splay树(伸展树)和红黑树比较
AVL树.splay树(伸展树)和红黑树比较 一.AVL树: 优点:查找.插入和删除,最坏复杂度均为O(logN).实现操作简单 如过是随机插入或者删除,其理论上可以得到O(logN)的复杂度,但是实 ...
- bzoj1251: 序列终结者 (splay)
splay可以用于维护序列,比如noi的维修序列,比如这道 发现当时splay没写总结,也没题解 然后重新写splay竟然耗了一个晚上 结果是因为max[0]没有附最小值!!血一样的教训 最后祭出in ...
- BZOJ 1503 郁闷的出纳员(splay)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 题意:给出一个数列(初始为空),给出一个最小值Min,当数列中的数字小于Min时自动 ...
最新文章
- sql server 2008数据复制
- 剪刀、石头、布机器人比赛
- oracle 三列数值相加,Oracle SQL/PLSQL:按货币拆分和求和值的分层查询
- .NET Core 3.0之创建基于Consul的Configuration扩展组件
- Python中几个有趣的函数
- Bootstrap CSS 编码规范之Class 命名规范
- UVALive6336 HDU4450 Draw Something【水题】
- Spring 下 MyBatis 的基本使用
- Mysql Fabric实现学习笔记
- 程序猿必备的数电知识,快来看看你掌握多少!(建议收藏)
- 【考研计算机网络】 强化笔记
- 计算机基础——11种排序(sort)算法
- Vuex 的简单模拟、了解Vuex
- ubuntu常用功能安装集锦
- python 语音识别 中文_python中文语音识别
- 获取基因的所有转录本(不同亚型)的外显子区域
- 协议、接口、服务的联系
- FPGA基础设计(二):PS2键盘控制及短按、长按
- Mybatis - xml文件标签中写注释
- python yaml PyYaml入门