Splay初步【bzoj1503】
做了一道水题,把bzoj1503用Splay重新写了一下。
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define drep(i, a, b) for (int i = a; i >= b; i--)
#define mp make_pair
#define pb push_back
#define clr(x) memset(x, 0, sizeof(x))
#define xx first
#define yy second
using namespace std;
typedef long long i64;
typedef pair<int, int> pii;
const int inf = ~0U >> ;
const i64 INF = ~0ULL >> ;
//*************************** struct node {
node *pre, *s[];
int key, size, mul;
node() { pre = s[] = s[] = ; size = mul = ; }
node(int _key) :key(_key) { pre = s[] = s[] = ; size = mul = ; }
bool getlr() { return pre->s[] == this; }
node *link(int w, node *p) { s[w] = p; if (p) p->pre = this; return this; }
void update() { size = mul + (s[] ? s[]->size : ) + (s[] ? s[]->size : ); }
} *root;
void rot(node* p) {
node *q = p->pre->pre;
p->getlr() ? p->link(, p->pre->link(, p->s[])) : p->link(, p->pre->link(, p->s[]));
p->pre->update();
if (q) q->link(q->s[] == p->pre, p);
else { p->pre = ; root = p; }
}
void splay(node *p, node *tar) {
while (p->pre != tar && p->pre->pre != tar)
p->getlr() == p->pre->getlr() ? (rot(p->pre), rot(p)) : (rot(p), rot(p));
if (p->pre) rot(p);
p->update();
}
void insrt(int k) {
node *p = root, *q = NULL;
while (p) {
q = p;
if (k > p->key) p = p->s[];
else if (k < p->key) p = p->s[];
else break;
}
if (!p) {p = new node(k);
if (!q) { root = p; return; }
q->link(q->key < k, p); q->update(); splay(p, );
}
else { p->mul++; splay(p, ); }
}
node *findkth(int x) {
node *p = root;
node *t;
while () {
int w = (p->s[] ? p->s[]->size : );
if (x <= w) t = p->s[];
else if (x > w + p->mul) { x -= w + p->mul; t = p->s[]; }
else break;
p = t;
}
splay(p, ); return p;
} node *find(node *p, int x) {
if (p->key < x) return find(p->s[], x);
else if (p->key > x) return find(p->s[], x);
else return p;
} int main() {
int ori();
int n, m;
scanf("%d%d", &n, &m);
int tot();
char op[]; int x;
while (n--) {
scanf("%s%d", op, &x);
if (op[] == 'I') {
if (x >= m) {
ori++;
insrt(x - tot);
}
}
else if (op[] == 'A') tot += x;
else if (op[] == 'S') {
tot -= x;
insrt(m - tot - );
splay(find(root, m - tot - ), );
root = root->s[];
if (root) root->pre = ;
}
else if (op[] == 'F') {
if (!root || x > root->size) puts("-1");
else printf("%d\n", findkth(root->size - x + )->key + tot);
}
}
printf("%d\n", ori - (root ? root->size : ));
return ;
}
Splay初步【bzoj1503】的更多相关文章
- BZOJ1503 [NOI2004]郁闷的出纳员 splay
原文链接http://www.cnblogs.com/zhouzhendong/p/8086240.html 题目传送门 - BZOJ1503 题意概括 如果某一个员工的工资低于了min,那么,他会立 ...
- 【题解】 bzoj1503: [NOI2004]郁闷的出纳员 (Splay)
bzoj1503,懒得复制,戳我戳我 Solution: 我知不知道我是那根筋抽了突然来做splay,调了起码\(3h+\),到第二天才改出来(我好菜啊),当做训练调错吧 一个裸的splay,没啥好说 ...
- 【BZOJ1503】 [NOI2004]郁闷的出纳员 splay
splay模板题,都快把我做忧郁了. 由于自己调两个坑点. 1.删除时及时updata 2.Kth 考虑k满足该点的条件即r->ch[1]->size+1<=k && ...
- Splay的初步学习
具体是啥,qwq 有时间再补吧,贴一下代码: #include<iostream> #include<cstdio> #include<cstring> #incl ...
- bzoj1503 Splay 维护名次数,支持删除
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 题解: 维护一颗Splay和一个外部变量,树中每个节点表示一个人,节点权值a + 外部变 ...
- bzoj1503[NOI2004]郁闷的出纳员——Splay
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1503 好奇怪呀!为什么而TLE? 各种修改终于卡时过了.可是大家比我快多了呀?难道是因为自己 ...
- bzoj1503 郁闷的出纳员 splay版
自己yy的写法 可能有点奇怪吧 详情看代码 还是蛮短的 #include<cstdio> #include<cstring> #include<algorithm> ...
- [BZOJ1503]郁闷的出纳员(Splay)
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...
- BZOJ1503: [NOI2004]郁闷的出纳员(Splay)
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的 工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经 ...
随机推荐
- hdu 3345 War Chess
War Chess Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- Discuz登录慢、退出也慢的原因?
Discuz登录慢.退出也慢的原因? 2009-02-21 12:50:11 分类: 转载自:http://www.aiseminar.cn/bbs/thread-201-1-1.html 由于服务 ...
- zencart产品详细页面调用数据库显示tags标签
给商品信息页面添加一些tag关键词标签有利于谷歌的收录,也有利于关键词的SEO,实现这个功能并不难.其实就是给zencart添加一个功能模块, 具体方法是: 1,在mudules目录下面新建一个以 ...
- L9,a cold welcome
expression: a large crowd of 一大群 in twenty minutes’time 20分钟之后 一些时间使用的介词 in two year‘s time on Satur ...
- SpringMVC中获得HttpRequest对象的方法
1. 使用@autowired注入HttpRequest 2. 在方法中直接声明形参有HttpRequest即可. 3. 使用一个Listener,然后获取.
- 编写一条sql命令,sql删除没有中文的表
删除包含中文的 和不饱和中文的字段 SHOW create table pages; drop table if exists `film`; CREATE TABLE `film` ( `id` i ...
- Scroll View 深入
转载自:http://mobile.51cto.com/hot-430409.htm 可能你很难相信,UIScrollView和一个标准的UIView差异并不大,scroll view确实会多一些方法 ...
- Web安全检测工具的使用.
Nikto2 Nikto2是一款使用perl语言写的多平台扫描软件,是一款命令行模式的工具,它可以扫描指定主机的WEB类型 主机名.特定目录.Cookie.特定CGI漏洞.XSS漏洞.sql注入漏洞. ...
- python的一些语法糖
1 Python中if-else语句的多种写法 a, b, c = 1, 2, 3 1.常规 if a>b: c = a else: c = b 2.表达式 c = a if a>b ...
- Contaminated Milk
Contaminated Milk 题目描述 Farmer John, known far and wide for the quality of the milk produced on his f ...