Splay的用法
splay区间增减查询
#include<cstdio>
#include<algorithm> using namespace std; const int N = ;
const int INF = 1e9;
typedef long long LL;
int sum[N],data[N],siz[N],tag[N],ch[N][],fa[N],st[N];
int Root,top; inline int read() {
int x = ,f = ;char ch = getchar();
for (; ch<''||ch>''; ch = getchar())
if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = getchar())
x = x * + ch - '';
return x * f;
}
inline void pushup(int x) {
int l = ch[x][],r = ch[x][];
siz[x] = siz[l] + siz[r] + ;
sum[x] = sum[l] + sum[r] + data[x];
}
inline void pushdown(int x) {
if (tag[x]) {
int l = ch[x][],r = ch[x][];
sum[l] += siz[l] * tag[x];sum[r] += siz[r] * tag[x];
data[l] += tag[x];data[r] += tag[x];
tag[l] += tag[x];tag[r] += tag[x];
tag[x] = ;
}
}
inline int son(int x) {
return x==ch[fa[x]][];
}
inline void rotate(int x) {
int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b];
if (z) ch[z][c] = x;else Root = x;fa[x] = z;
ch[x][!b] = y;fa[y] = x;
ch[y][b] = a;if (a) fa[a] = y;
pushup(y),pushup(x);
}
inline void splay(int x,int rt) {
top = ;int p = x;
while (p) st[++top] = p,p = fa[p];
while (top) pushdown(st[top]),top--;
while (fa[x] != rt) {
int y =fa[x],z = fa[y];
if (z==rt) rotate(x);
else {
if (son(x)==son(y)) rotate(y),rotate(x);
else rotate(x),rotate(x);
}
}
}
inline void update(int L,int R,int a) {
splay(L,);splay(R,L);
tag[ch[R][]] += a;
sum[ch[R][]] += siz[ch[R][]] * a;
data[ch[R][]] += a;
pushup(R),pushup(L);
}
inline int query(int L,int R) {
splay(L,);splay(R,L);
return sum[ch[R][]];
}
int build(int l,int r) {
if (l > r) return ;
if (l==r) {
siz[l] = ;sum[l] = data[l];return l;
}
int mid = (l + r) >> ;
int t = build(l,mid-);
fa[t] = mid;ch[mid][] = t;
t = build(mid+,r);
fa[t] = mid;ch[mid][] = t;
pushup(mid);
return mid;
}
int main() {
int n = read(),m = read();
for (int i=; i<=n+; ++i) data[i] = read();
Root = build(,n+);
fa[] = ;
//char s[10];
for (int o,a,b,c,i=; i<=m; ++i) {
o = read();
if (o == ) {
a = read(),b = read(),c = read();
update(a,b+,c);
}
else {
a = read(),b = read();
printf("%d\n",query(a,b+));
}
}
return ;
}
Splay的用法的更多相关文章
- bzoj 2209: [Jsoi2011]括号序列 splay
2209: [Jsoi2011]括号序列 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 833 Solved: 392[Submit][Status ...
- 平衡树简单教程及模板(splay, 替罪羊树, 非旋treap)
原文链接https://www.cnblogs.com/zhouzhendong/p/Balanced-Binary-Tree.html 注意是简单教程,不是入门教程. splay 1. 旋转: 假设 ...
- 在洛谷3369 Treap模板题 中发现的Splay详解
本题的Splay写法(无指针Splay超详细) 前言 首先来讲...终于调出来了55555...调了整整3天..... 看到大部分大佬都是用指针来实现的Splay.小的只是按照Splay的核心思想和原 ...
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
随机推荐
- PHP变量、数据类型、字符串、运算符、条件语句、循环语句、数组、函数
PHP 简介: php :是一门动态交互的计算机语言 静态交互 html css js 但凡是动态交互的 都需要服务器. php都是以.php结尾的文件 Html文件里不能写php代码,但是ph ...
- Mind must be master of the body, strong mind can separate the body from its suffering.
Mind must be master of the body, strong mind can separate the body from its suffering.意志是身体的主人,有顽强的意 ...
- Python+selenium之调用JavaScript
webdriver提供了操作浏览器的前进和后退的方法,但是对于浏览器公东条并没有提供相应的操作方法.于是就需要借助JavaScript来控制浏览器的滚动条.webdriver提供了execute_sr ...
- CefSharp试用
Github地址: https://github.com/cefsharp/CefSharp 首先下载所有源代码下来 然后直接打开Sln 然后就可以直接调试WinForm.Wpf的Example了 注 ...
- div高度不能自适应(子级使用float浮动,父级div高度不能自适应)
1.问题截图: 2.问题描述: 由于地址.公司名长度的不定性,所以每一条地址所在的父级div高度不定,但是需要设置一个最小的高度min-height:48px;但是当内容增加的时候,父级div高度却不 ...
- 转载 - 浅析我曾遇到的几个便宜VPS服务器
本文来自:http://www.jianshu.com/p/7d8cfa87fa32 有些时候可能并不是我们工作和业务的需要,但是网上就是这么的邪门,如果看到便宜的衣服不去购买深怕自己吃亏.所以每年的 ...
- UVA 1599, POJ 3092 Ideal Path 理想路径 (逆向BFS跑层次图)
大体思路是从终点反向做一次BFS得到一个层次图,然后从起点开始依次向更小的层跑,跑的时候选则字典序最小的,由于可能有多个满足条件的点,所以要把这层满足条件的点保存起来,在跑下一层.跑完一层就会得到这层 ...
- 影响一个UIView是否能正常显示的几个因素
在使用代码实现UIView及其子类的对象的时候,经常会遇到创建的某个view没有显示在屏幕上.以下总结了本人遇到过的几种情况.可能还有些其它的原因也会导致view不能正常显示,限于个人经历有限,无法全 ...
- kubernetes-平台日志收集(ELK)
使用ELK Stack收集Kubernetes平台中日志与可视化 K8S系统的组件日志 K8S Cluster里面部署的应用程序日志 日志系统: ELK安装 安装jdk [root@localhost ...
- pbr 5.2.1需使用中科大的源,豆瓣的不行
-bash-4.2$ .tox/tempest/bin/pip install pbr==5.2.1DEPRECATION: Python 2.7 will reach the end of its ...