我只是一个存模板的,详细的请看这里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模板(序列终结者)的更多相关文章

  1. splay树 序列终结者

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

  2. 【BZOJ1251】序列终结者 Splay

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

  3. 1251. 序列终结者【平衡树-splay】

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

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

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

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

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

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

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

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

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

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

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

  9. 「BZOJ1251」序列终结者 (splay 区间操作)

    题面: 1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 5367  Solved: 2323[Submit][Status][D ...

随机推荐

  1. Exiting an iOS App with Xamarin

    referenced from: http://www.redbitdev.com/exiting-ios-app-with-xamarin-ios/ The team is in the middl ...

  2. 正确使用Block避免Cycle Retain和Crash

    Block简介 Block作为C语言的扩展,并不是高新技术,和其他语言的闭包或lambda表达式是一回事.需要注意的是由于Objective-C在iOS中不支持GC机制,使用Block必须自己管理内存 ...

  3. Ubuntu下添加开机启动项的2种方法

    1.方法一,编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚本,所以我们可以直接在/etc/rc.local中添加启动脚本.当然要添加到语句:exit 0 前面 ...

  4. Phpstorm 放大字体的快捷键是什么?

      这个功能需要设置才能使用: 步骤:control+shift+A功能可以搜索对应功能 输入mouse 设置Change font size(Zoom) ...的按钮打开,然后就可以通过 ctrl+ ...

  5. 江湖问题研究-- intent传递有没有限制大小,是多少?

    出门一步,便是江湖.江湖上有很多流言. 比方这条: intent传递是有限制大小的,详细在40KB左右. 当然也有传言说是1M左右. 数百头母驴为何半夜慘叫? 小卖部安全套为何屡遭黑手? 女生宿舍内裤 ...

  6. 使用 xmllint 验证 odoo xml文件

    Odoo 源码包含了2个 relax ng 文件,也是odoo sa用来验证xml的正确性的. openerp/import_xml.rng openerp/addons/base/rng/view. ...

  7. Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

    Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...

  8. 可在 html5 游戏中使用的 js 工具库

    可在 html5 游戏中使用的 js 工具库 作者: 木頭 时间: September 21, 2014 分类: Utilities,Game 使用 cocos2d-js 3.0 开发游戏项目两三个月 ...

  9. qt的下载链接

    http://download.qt.io/archive/qt/5.8/5.8.0/ http://download.qt.io/archive/qt/ http://download.qt.io ...

  10. SICP 习题 (1.38)解题总结

    SICP 习题1.38 紧跟着习题1.37的方向,要求我们用习题1.37中定义的cont-frac过程计算数学家欧拉大师在论文De Fractionibus Continuis 中提到的e-2的连分式 ...