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的更多相关文章

  1. BZOJ 1251: 序列终结者 [splay]

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3778  Solved: 1583[Submit][Status][Discu ...

  2. 【BZOJ1251】序列终结者 Splay

    一道模板题,一直没发现自己的快速读入读不了负数,我竟然能活到现在真是万幸. #include <iostream> #include <cstdio> #define inf ...

  3. CODEVS 4655 序列终结者-splay(区间更新、区间翻转、区间最值)

    4655 序列终结者  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Description 网上有许多题,就是给定一个序列,要 ...

  4. [bzoj1251]序列终结者——splay

    题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技 ...

  5. bzoj 1251序列终结者 splay 区间翻转,最值,区间更新

    序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 4594  Solved: 1939[Submit][Status][Discuss] De ...

  6. bzoj1251 序列终结者(splay)

    人生第一发splay,写得巨丑,最后忘记了push_down以后要将子节点maintain 9k代码不忍直视 #define NDEBUG #include<cstdio> #includ ...

  7. BZOJ 1251 序列终结者(Splay)

    题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术 ...

  8. 【BZOJ】1251: 序列终结者(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1251 不行..为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint.. ...

  9. bzoj1251 序列终结者(Splay Tree+懒惰标记)

    Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...

随机推荐

  1. 1.IDEA的安装

    .1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.

  2. android 权限清单

    常用权限: 读写存储卡装载和卸载文件系统 android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_EXTERNAL_STOR ...

  3. SpringBoot后台如何实现文件上传下载

    1.单文件上传: @RequestMapping(value = "/upload") @ResponseBody public String upload(@RequestPar ...

  4. Ubuntu+XAMPP+Wordpress的安装与配置问题

    Wordpress自动更新以及安装在线主题的时候需要输入FTP信息 打开/opt/lampp/htdocs/wordpress/wp-config.php文件 define('FS_METHOD',' ...

  5. java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.getXmlStandalone()Z解决办法

    2019-05-20 23:02:20.168 |-INFO  [http-nio-8001-exec-2] com.xxx.ecc.cloudbiz.service.payment.impl.Wei ...

  6. jquery非文本框复制

    function selectText(x) { if (document.selection) { var range = document.body.createTextRange();//ie ...

  7. 学习vi和vim编辑器(1):vi文本编辑器

    UNIX系统中有非常多编辑器.能够分为两种类型:行编辑器和全屏编辑器.行编辑器每次仅仅能在屏幕中显示文件的一行,如ed和ex编辑器.全屏编辑器能够在屏幕上显示文件的一部分. vi(读为vee-eye) ...

  8. UVA11234 Expressions

    题目的意思实在是读不懂,又是把栈变成队列什么的.. 只是大体的意思就是把后缀表达式变一下.. 抛开意思,事实上就是依据输入建个树,然后倒序输出.. 拿第一个例子说明:大写代表操作符(+ - × /之类 ...

  9. Error解决:Property&#39;s synthesized getter follows Cocoa naming convention for returning &#39;owned&#39;

    在项目中定义了以new开头的textField.结果报错: 先看我的源代码: #import <UIKit/UIKit.h> @interface ResetPasswordViewCon ...

  10. luogu2606 排列计数

    题目大意 求满足下列条件的排列$P$的数量:$\forall P_i, P_i>P_{\lfloor \frac{i}{2}\rfloor}$. 思路 从下标入手 反过来想,也就是对$\fora ...