[codevs4655] 序列终结者(Splay)
支持操作:
1.区间加
2.区间翻转
3.区间求最大值
splay模板
注意:update 里更新 max 时需要取 3 个值的 Max
别忘了各种边界讨论
——代码
#include <cstdio>
#define ls son[now][0]
#define rs son[now][1] const int MAXN = , INF = 2e9;
int n, m, root, cnt;
int a[MAXN], size[MAXN], key[MAXN], add[MAXN], max[MAXN], rev[MAXN], f[MAXN], son[MAXN][]; inline void swap(int &x, int &y)
{
x ^= y ^= x ^= y;
} inline int Max(int x, int y)
{
return x > y ? x : y;
} inline int get(int x)
{
return x == son[f[x]][];
} inline void update(int now)
{
if(now)
{
size[now] = ;
if(ls) size[now] += size[ls];
if(rs) size[now] += size[rs]; max[now] = key[now];
if(ls) max[now] = Max(max[now], max[ls]);
if(rs) max[now] = Max(max[now], max[rs]);
}
} inline void pushdown(int now)
{
if(rev[now])
{
swap(ls, rs);
if(ls) rev[ls] ^= ;
if(rs) rev[rs] ^= ;
rev[now] = ;
}
if(add[now])
{
if(ls) add[ls] += add[now], key[ls] += add[now], max[ls] += add[now];
if(rs) add[rs] += add[now], key[rs] += add[now], max[rs] += add[now];
add[now] = ;
}
} inline void build(int x, int y, int fa, int &now)
{
if(x > y) return;
int mid = (x + y) >> ;
now = ++cnt;
f[now] = fa;
build(x, mid - , now, ls);
build(mid + , y, now, rs);
update(now);
} inline void rotate(int x)
{
pushdown(f[x]);
pushdown(x);
int old = f[x], oldf = f[old], wh = get(x); son[old][wh] = son[x][wh ^ ];
f[son[old][wh]] = old; if(oldf) son[oldf][old == son[oldf][]] = x;
f[x] = oldf; son[x][wh ^ ] = old;
f[old] = x; update(old);
update(x);
} inline void splay(int x, int to)
{
for(int fa; (fa = f[x]) != to; rotate(x))
if(f[fa] != to)
rotate(get(x) ^ get(fa) ? x : fa);
if(!to) root = x;
} inline int find(int x)
{
int now = root;
while()
{
pushdown(now);
if(x <= size[ls]) now = ls;
else
{
x -= size[ls];
if(x == ) return now;
x--;
now = rs;
}
}
} int main()
{
int i, k, x, y, v;
scanf("%d %d", &n, &m);
a[] = -INF, a[n + ] = -INF;
build(, n + , , root);
for(i = ; i <= m; i++)
{
scanf("%d %d %d", &k, &x, &y);
x = find(x);
y = find(y + );
splay(x, );
splay(y, x);
if(k == )
{
scanf("%d", &v);
add[son[son[root][]][]] += v;
max[son[son[root][]][]] += v;
key[son[son[root][]][]] += v;
update(son[root][]);
update(root);
}
else if(k == ) rev[son[son[root][]][]] ^= ;
else printf("%d\n", max[son[son[root][]][]]);
}
return ;
}
[codevs4655] 序列终结者(Splay)的更多相关文章
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- 【BZOJ1251】序列终结者 Splay
一道模板题,一直没发现自己的快速读入读不了负数,我竟然能活到现在真是万幸. #include <iostream> #include <cstdio> #define inf ...
- CODEVS 4655 序列终结者-splay(区间更新、区间翻转、区间最值)
4655 序列终结者 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description 网上有许多题,就是给定一个序列,要 ...
- [bzoj1251]序列终结者——splay
题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技 ...
- bzoj 1251序列终结者 splay 区间翻转,最值,区间更新
序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 4594 Solved: 1939[Submit][Status][Discuss] De ...
- bzoj1251 序列终结者(splay)
人生第一发splay,写得巨丑,最后忘记了push_down以后要将子节点maintain 9k代码不忍直视 #define NDEBUG #include<cstdio> #includ ...
- 序列终结者 Splay
1.注意在 split 和 merge时要特判一下边界, 否则就会出现边界错误的情况. 2.随时都要维护父指针. 3.在更新 maxv 和翻转标记时要判一下左右儿子是否都存在. #include< ...
- BZOJ 1251 序列终结者(Splay)
题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术 ...
- 【BZOJ】1251: 序列终结者(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1251 不行..为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint.. ...
随机推荐
- 题解报告:hdu 1261 字串数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1261 Problem Description 一个A和两个B一共可以组成三种字符串:"ABB ...
- XmlPullParser简单教程
官网: http://www.xmlpull.org 本文参考 : http://www.xmlpull.org/v1/download/unpacked/doc/quick_intro.html 1 ...
- 【Visual Studio Code 】使用Visual Studio Code + Node.js搭建TypeScript开发环境
1.准备工作 Node.js Node.js - Official Site Visual Studio Code Visual Studio Code - Official Site 安装Node. ...
- lavas安装
最近在研究pwa,百度基于此写了一套开源框架lavas,学习下: 1.环境准备: lavas 安装.git安装 Node.js:https://nodejs.org/ Git:https://git- ...
- 转】MongoDB 自动分片 auto sharding
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/4/ 感谢! MongoDB 自动分片 auto shard ...
- Sql中创建事务处理
Create Procedure MyProcedure AS Begin Set NOCOUNT ON; Set XACT_ABORT ON; --这句话非常重要 begin try Begin T ...
- ListView相关知识点
最近开发接触到了ListView控件,其实简单的需求基本上源生的都可以满足,下面总结一下开发过程中所遇到的关键点: 1.多级ListView联动,保存位置:即切换第一层ListView的item过程中 ...
- mysql 插入多条记录,重复值不插入
只去除主键与唯一索引的字段,字段为null时 是可以重复插入的domo: insert ignore into table_name(email,phone,user_id) values('test ...
- Python list列表的常用操作方法
本文主要介绍了Python中列表(List)的详解操作方法,包含创建.访问.删除.排序.切片,乘等操作方法 1.创建列表:把逗号分隔的不同的数据项使用方括号括起来 list = [1,2,3,'Jam ...
- CREATE DATABASE - 创建新数据库
SYNOPSIS CREATE DATABASE name [ [ WITH ] [ OWNER [=] dbowner ] [ LOCATION [=] 'dbpath' ] [ TEMPLATE ...