序列终结者 Splay
1.注意在 split 和 merge时要特判一下边界, 否则就会出现边界错误的情况。
2.随时都要维护父指针。
3.在更新 maxv 和翻转标记时要判一下左右儿子是否都存在。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 100000 + 3;
int f[maxn], ch[maxn][2], n,m,numv[maxn], maxv[maxn], siz[maxn], tag[maxn], root, cnt , lazy[maxn];
struct Operation
{
inline int get(int x){ return ch[f[x]][1] == x; }
inline void pushdown(int x)
{
if(tag[x])
{
swap(ch[ch[x][0]][0],ch[ch[x][0]][1]);
swap(ch[ch[x][1]][0],ch[ch[x][1]][1]);
if(ch[x][0]) tag[ch[x][0]] ^= 1;
if(ch[x][1]) tag[ch[x][1]] ^= 1;
tag[x] = 0;
}
if(lazy[x])
{
if(ch[x][0]) maxv[ch[x][0]] += lazy[x], lazy[ch[x][0]] += lazy[x], numv[ch[x][0]] += lazy[x];
if(ch[x][1]) maxv[ch[x][1]] += lazy[x], lazy[ch[x][1]] += lazy[x], numv[ch[x][1]] += lazy[x];
lazy[x] = 0;
}
}
inline void pushup(int x)
{
siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
maxv[x] = numv[x];
if(ch[x][0]) maxv[x] = max(maxv[x], maxv[ch[x][0]]);
if(ch[x][1]) maxv[x] = max(maxv[x], maxv[ch[x][1]]);
}
inline void rotate(int x)
{
int old = f[x], oldf = f[old], which = get(x);
ch[old][which] = ch[x][which ^ 1], f[ch[old][which]] = old;
ch[x][which ^ 1] = old, f[old] = x, f[x] = oldf;
if(oldf) ch[oldf][ch[oldf][1] == old] = x;
pushup(old); pushup(x);
}
inline void splay(int x,int &tar)
{
int a = f[tar];
for(int fa; (fa = f[x]) != a; rotate(x))
if(f[fa] != a) rotate(get(x) == get(fa) ? fa : x);
tar = x;
}
inline int findx(int x, int top)
{
int cur = top;
while(x > 0)
{
pushdown(cur);
int ls = ch[cur][0], rs = ch[cur][1];
if(siz[ls] + 1 == x) return cur;
if(siz[ls] >= x) cur = ls;
else x -= siz[ls] + 1, cur = rs;
}
return 0;
}
inline void split(int &a,int nums,int &b)
{
if(nums == 0)
{
b = a, a = 0; return ;
}
if(nums == siz[a])
{
b = 0; return ;
}
int u = findx(nums, a);
splay(u, a);
b = ch[a][1];
f[ch[a][1]] = 0, ch[a][1] = 0,
pushup(a);
}
inline void merge(int &a,int b)
{
if(!a) { a = b; return ;}
splay(findx(siz[a], a), a);
ch[a][1] = b, f[b] = a;
pushup(a);
}
void build(int l,int r,int &o,int fa)
{
if(l > r) return ;
o = ++cnt;
f[o] = fa, siz[o] = 1;
int mid = (l + r) >> 1;
build(l, mid - 1, ch[o][0], o);
build(mid + 1, r, ch[o][1], o);
pushup(o);
}
}T;
int main()
{
//freopen("input.txt","r",stdin);
scanf("%d%d",&n,&m);
T.build(1, n, root, 0);
while(m--)
{
int ops, l , r, v = 0;
scanf("%d%d%d",&ops,&l,&r);
int a = 0, b = 0;
T.split(root, l - 1, a);
T.split(a, r - l + 1, b);
if(ops == 1)
{
scanf("%d",&v);
maxv[a] += v, lazy[a] += v, numv[a] += v;
T.pushup(a);
}
if(ops == 2)
{
swap(ch[a][0], ch[a][1]);
tag[a] ^= 1;
}
if(ops == 3)
{
printf("%d\n",maxv[a]);
}
T.merge(root, a);
T.merge(root, b);
}
return 0;
}
序列终结者 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 ...
- 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.. ...
- bzoj1251 序列终结者(Splay Tree+懒惰标记)
Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...
随机推荐
- Codeforces 816A/B
A. Karen and Morning 传送门:http://codeforces.com/contest/816/problem/A 水题,参考程序如下: #include <stdio.h ...
- Java代码规范和一些常见问题
本文中的代码规范,是Java标准代码规范中的一小部分,在我看来,是最重要的一部分. 理想目标:不需要写注释,不需要和别人介绍,别人就知道你的项目大致是做什么的,每个类大概实现了什么功能. ...
- 0912MySQL 执行计划explain详解
转自http://blog.itpub.net/29773961/viewspace-1767044/ 该博客内容是比较全的,虽然写的比较晦涩,多读几遍还是不错的 explain命令是查看查询优化器如 ...
- Palindrome Linked List 234
推断是否为回文链栈 时间复杂度为O(n) 空间复杂度为O(1) : 运用递归 保证空间复杂度为O(1): 时间复杂度为O(n): 注意定义了一个全局变量 flag = true 用此标记来标记是否在推 ...
- viz.js操作流程
1.下载依赖的js文件,并引入 <script src="${root }/resources/js/graphviz/viz.js"></script> ...
- spring注入对象类型的属性
一.1.创建service类和Dao类 (1)在service中得到dao对象 2.具体实现过程 (1)在service里边把dao作为类型属性 (2)生成dao类型属性的set方法 public c ...
- 怎样才是一个基本水平的java程序员?
怎样才是一个基本水平的java程序员? 熟悉常用的数据结构,包括数组,链表,树,哈希表等. 熟悉结构化编程和面向对象编程. 能够阅读UML设计图,根据UML语义进行编码 了解RDBMS和SQL的使用, ...
- poj 1321(DFS)
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...
- Epos消费管理系统复制迁移SQL SERVER 2005数据库
先脱机 原来要关闭Epos消费管理系统软件才可以让对应的数据库脱机
- Gym-101915C Shahhoud Training Hussain 模拟
题面 题意:每天有K本书,你最多看P本一天,问N天后多少本书没有看 题解:ans=(K-P)*N; 注意一点就是P>=K的时候,ans=0; #include<bits/stdc++.h& ...