Splay模板(序列终结者)
我只是一个存模板的,详细的请看这里http://blog.csdn.net/whai362/article/details/47298133
题目链接:http://www.codevs.cn/problem/4655/
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long LL;
const int INF = 2e9 + 1e8; const int MOD = 1e9 + ;
const double eps = 0.0000000001; #define MSET(a, b) memset(a, b, sizeof(a)) const int maxn = 1e6 + ;
int max(int a, int b, int c)
{
return max(a, max(b, c));
} struct SplayTree
{
struct Node
{
int son[], big, val, lazy, sz;
bool rev;
void init(int _val)
{
val = big = _val;
sz = ;
lazy = rev = son[] = son[] = ;
}
} T[maxn];
int root, fa[maxn];
void pushup(int i)
{
T[i].big=T[i].val,T[i].sz=;
if(T[i].son[])
{
T[i].big=max(T[i].big,T[T[i].son[]].big);
T[i].sz+=T[T[i].son[]].sz;
}
if(T[i].son[])
{
T[i].big=max(T[i].big,T[T[i].son[]].big);
T[i].sz+=T[T[i].son[]].sz;
}
}
void pushdown(int i)
{
if (i == )
return;
if (T[i].lazy)
{
for (int k = ; k < ; k++)
{
if (T[i].son[k])
{
T[T[i].son[k]].lazy += T[i].lazy;
T[T[i].son[k]].val += T[i].lazy;
T[T[i].son[k]].big += T[i].lazy;
}
}
T[i].lazy = ;
}
if (T[i].rev)
{
for (int k = ; k < ; k++)
if (T[i].son[k])
T[T[i].son[k]].rev ^= ;
swap(T[i].son[], T[i].son[]);
T[i].rev = ;
}
}
/** 旋转操作
* 传入x,旋转x与x的父亲这两个节点;
*/
void rotate(int x, int d)
{
int y = fa[x], z = fa[y];
T[y].son[!d] = T[x].son[d], fa[T[x].son[d]] = y;
T[x].son[d] = y, fa[y] = x;
T[z].son[T[z].son[] == y] = x, fa[x] = z;
pushup(y);
}
void splay(int x, int goal)
{
if (x == goal)
return;
while (fa[x] != goal)
{
int y = fa[x], z = fa[y];
pushdown(z), pushdown(y), pushdown(x);
int dirx = (T[y].son[] == x), diry = (T[z].son[] == y);
if (z == goal)
rotate(x, dirx);
else
{
if (dirx == diry)
rotate(y, diry);
else
rotate(x, dirx);
rotate(x, diry);
}
}
pushup(x);
if (goal == )
root = x;
}
/**
* select(pos) 返回第pos+1个元素;
*/
int Select(int pos)
{
int u = root;
pushdown(u);
while (T[T[u].son[]].sz != pos)
{
if (pos < T[T[u].son[]].sz)
u = T[u].son[];
else
{
pos = pos - ( + T[T[u].son[]].sz);
u = T[u].son[];
}
pushdown(u);
}
return u;
}
void update(int l, int r, int val)
{
int x = Select(l - ), y = Select(r + );
splay(x, );
splay(y, x);
T[T[y].son[]].val += val;
T[T[y].son[]].big += val;
T[T[y].son[]].lazy += val;
}
void turn(int l, int r)
{
int x = Select(l - ), y = Select(r + );
splay(x, );
splay(y, x);
T[T[y].son[]].rev ^= ;
}
int query(int l, int r)
{
int x = Select(l - ), y = Select(r + );
splay(x, );
splay(y, x);
return T[T[y].son[]].big;
}
int build(int L, int R)
{
if (L > R)
return ;
if (L == R)
return L;
int mid = (L + R) >> , sL, sR;
T[mid].son[] = sL = build(L, mid - );
T[mid].son[] = sR = build(mid + , R);
fa[sL] = fa[sR] = mid;
pushup(mid);
return mid;
} void init(int n)
{
T[].init(-INF), T[n + ].init(-INF);
for (int i = ; i <= n + ; i++)
T[i].init();
root = build(, n + ), fa[root] = ;
fa[] = , T[].son[] = root, T[].sz = ;
}
} re; int main()
{
int n, m;
scanf("%d%d", &n, &m);
re.init(n);
for (int i = , a, b, c, d; i < m; i++)
{
scanf("%d", &a);
if (a == )
{
scanf("%d%d%d", &b, &c, &d);
re.update(b, c, d);
}
else if (a == )
{
scanf("%d%d", &b, &c);
re.turn(b, c);
}
else
{
scanf("%d%d", &b, &c);
printf("%d\n", re.query(b, c));
}
}
return ;
} /**************************************************/
/** Copyright Notice **/
/** writer: wurong **/
/** school: nyist **/
/** blog : http://blog.csdn.net/wr_technology **/
/**************************************************/
Splay模板(序列终结者)的更多相关文章
- splay树 序列终结者
/* 4655 序列终结者 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description 网上有许多题,就是给定一个序 ...
- 【BZOJ1251】序列终结者 Splay
一道模板题,一直没发现自己的快速读入读不了负数,我竟然能活到现在真是万幸. #include <iostream> #include <cstdio> #define inf ...
- 1251. 序列终结者【平衡树-splay】
Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...
- [bzoj1251]序列终结者——splay
题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技 ...
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- 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.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...
- 「BZOJ1251」序列终结者 (splay 区间操作)
题面: 1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 5367 Solved: 2323[Submit][Status][D ...
随机推荐
- 【webpack2】-- 入门与解析
每次学新东西总感觉自己是不是变笨了,看了几个博客,试着试着就跑不下去,无奈只有去看官方文档. webpack是基于node的.先安装最新的node. 1.初始化 安装node后,新建一个目录,比如ht ...
- Android -- AsyncTask 使用和缺陷
一.AsyncTask的基本用法 由于AsyncTask是一个抽象类,所以如果我们想使用它,就必须要创建一个子类去继承它.在继承时我们可以为AsyncTask类指定三个泛型参数,这三个参数的用途如下: ...
- MYSQL查询的四种情况
1 普通连接查询 select 表1字段1,表2字段2,from 表1,表2,where 表1.字段1==表2.字段2 2 inner join查询 select 表1字段1 ,表2字段2,from ...
- Keras学习
参加比赛用到了keras,虽然之前用tensorflow,但是感觉tensorflow的确不太友好,api比较难读,然后就学习keras使用 随着深入,发现keras的api确实比较友好 跑了一些ex ...
- python 图像识别转文字
rom PIL import Image import pytesseract #上面都是导包,只需要下面这一行就能实现图片文字识别 #text=pytesseract.image_to_string ...
- vue2.0 自定义指令
Vue指令 Vue的指令以v-开头,作用在HTML元素上,将指令绑定在元素上,给绑定的元素添加一些特殊行为. 例如: <h1 v-if="yes">Yes</h1 ...
- 《好好说话》zz
最近,<奇葩说>闹出来了一些不愉快. 在半决赛中,姜思达惜败,愤怒的粉丝把矛头指向那场比赛的其他人.最终,马薇薇.黄执中和网友们吵起来了. 这件事本不算大事,毕竟娱乐业就是这个样子.刚刚好 ...
- 关于Yapi出现 请求异常,请检查 chrome network 错误信息...
项目开发中由于后台接口还没有,打算使用mock模拟本地数据,配置好接口,运行接口出现 检查了cross-request插件是否安装以及激活,发现没有问题,最后发现是我的请求地址写错了,,这里请求地址需 ...
- java 定义一个同步map内存去重法
实例:
- 记录一次在 VirtualBox的添加共享windows文件后,发现没有共享文件的事
在VirtualBox设置完桥接添加ip后,在设备中添加共享windows文件,“e:\work ”,发现共享目录没有文件.使用了各种reboot之后,还是没有发现共享文件夹,重新设置还是不行,用mo ...