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函数: ...
随机推荐
- 一步步实现自己的ORM(二)
在第一篇<一步步实现自己的ORM(一)>里,我们用反射获取类名.属性和值,我们用这些信息开发了简单的INSERT方法,在上一篇文章里我们提到主键为什么没有设置成自增长类型,单单从属性里我们 ...
- Java基础:(五)Object通用方法
一.Object对象的九个方法 getClass():hashCode():equals():clone():toString():notify():notifyAll():wait():finali ...
- AJPFX深入理解之abstract class和interface的区别
含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必 ...
- DetachedCriteria的简单使用
一. DetachedCriteria使得hibernate能够对查询条件进行面向对象的方式来组装.其创建方式有两种: 1.1直接用class创建:DetachedCriteria criteria ...
- rest_framework之视图
写一个出版社的增删改查restful接口 models from django.db import models # Create your models here. from django.db i ...
- 【简记】HTML + CSS 的一些要点(不定时更新)
1.td占据多行 / 列时,其挤开的 td 不写(但是包裹 td 的 tr 要写) 2. display:td 的元素中的文本默认垂直不居中(table中的td中的文本是垂直居中的) 3.th虽然定义 ...
- python基础一 day14 生成器函数进阶(1)
- HTML_5 (1 2 3的代码总结)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python——for表达式
一.for表达式 1.for表达式语法格式及用法 for表达式利用可迭代对象创建新的列表,for表达式也称为列表推导式,具体语法格式如下: [表达式 for 循环计数器 in 可迭代对象] 例: a ...
- DateTime与long互转
DateTime转long: public static long GetDateLong(object time) { DateTime epoc = TimeZone.CurrentTimeZon ...