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函数: ...
随机推荐
- 用户 'IIS APPPOOL\**' 登录失败的解决方案(项目部署到本地IIS上打开网页出现报错)
为开发方便-将项目部署到本地IIS上打开网页出现报错 1.打开IIS管理 2.点击应用池 3.找到你部署的网站名,右键“高级设置”——>“进程模型”——>“标识”修改为localsyste ...
- SpringBoot 2.x (15):Actuator监控
Actuator监控:SpringBoot自带的,对生成环境进行监控的系统 使用:既然是监控,那就不能监控一个空项目 这里我使用SpringBoot整合MyBatis的Demo: https://ww ...
- U3D加载服务器上的assetbundle
在Unity3D中,如果加载服务器上的AssetBundle,总是会提示找不到crossdomain.xml文件,即使添加了该文件,也会报同样的错误.属于跨域访问报错的问题. 官方的解决方案如下: h ...
- 最具士兵突击实战类型的JavaScript
JavaScript实战一书的基础知识部分帮助读者快速踏入JavaScript领域之门,jQuery部分帮助读者随心所欲地去工作,HTML5部分帮读者搭上时代的班车,Node.JS则可以让读者屹立在技 ...
- python爬虫之路——初识爬虫原理
爬虫主要做两件事 ①模拟计算机对服务器发起Request请求 ②接收服务器端的Response内容并解析,提取所需的信息 互联网页面错综复杂,一次请求不能获取全部信息.就需要设计爬虫的流程. 本书主要 ...
- 10款免费的MySQL数据库图形化管理工具
绝大多数的关系数据库都明显不同于MS Access,它们都有两个截然不同的部分:后端作为数据仓库,前端作为用于数据组件通信的用户界面.这种设计非常巧妙,它并行处理两层编程模型,将数据 层从用户界面中分 ...
- Android(java)学习笔记117:SharedPreferences(轻量级存储类)
1.SharedPreferences是Android平台上一个轻量级的存储类,简单的说就是可以存储一些我们需要的变量信息.2个activity 之间的数据传递除了可以他通过intent来传递数据,还 ...
- Verilog设计分频器(面试必看)
分频器是指使输出信号频率为输入信号频率整数分之一的电子电路.在许多电子设备中如电子钟.频率合成器等,需要各种不同频率的信号协同工作,常用的方法是以稳定度高的晶体振荡器为主振源,通过变换得到所需要的各种 ...
- Hbase 操作工具类
依赖jar <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-cli ...
- PAT 乙级 1045
题目 题目地址:PAT 乙级 1045 题解 本题的解法比较巧妙,刚开始的试着用暴力求解,果不其然时间超限…… 变换思路,既然对于每个元素来说满足的条件是前小后大,那么对数组排序,对应的位置相等的即为 ...