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的用法的更多相关文章

  1. bzoj 2209: [Jsoi2011]括号序列 splay

    2209: [Jsoi2011]括号序列 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 833  Solved: 392[Submit][Status ...

  2. 平衡树简单教程及模板(splay, 替罪羊树, 非旋treap)

    原文链接https://www.cnblogs.com/zhouzhendong/p/Balanced-Binary-Tree.html 注意是简单教程,不是入门教程. splay 1. 旋转: 假设 ...

  3. 在洛谷3369 Treap模板题 中发现的Splay详解

    本题的Splay写法(无指针Splay超详细) 前言 首先来讲...终于调出来了55555...调了整整3天..... 看到大部分大佬都是用指针来实现的Splay.小的只是按照Splay的核心思想和原 ...

  4. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  5. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  6. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  7. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  8. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  9. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

随机推荐

  1. Coroutine(协程)模式与线程

    概念 协程(Coroutine)这个概念最早是Melvin Conway在1963年提出的,是并发运算中的概念,指两个子过程通过相互协作完成某个任务,用它可以实现协作式多任务,协程(coroutine ...

  2. 老技术,UrlRewriter实现全站伪静态

    看人家做淘宝客很火,就做了个网站.seo的话当然需要全站伪静态了,问了下空间商不支持mvc,尼玛,好吧,isapi_rewrite支持吗?“额,不支持!” -_-! 额,好吧,搬出n年前的东西了:微软 ...

  3. 第一课:K线

    1       K线是根据价格走势中形成的四个价位(开盘价.收盘价.最高价.最低价)绘制而成的.K线是最基本的描述股价涨跌的表现符号(记录某种股票一天的价格变动情况). K线构造的四个价格因素:开盘价 ...

  4. [20190625]记录npm的一些常用命令

    1. 安装依赖包 npm install -g packageName //全局安装npm install packageName --save //安装在项目下并写入package.json文件中n ...

  5. i-nex安装教程

    sudo add-apt-repository ppa:i-nex-development-team/stable sudo apt-get updatesudo apt-get i-nex

  6. iOS 最新判断机型设备方法

    #define isIphoneXXS [UIScreen mainScreen].bounds.size.width == 375  && [UIScreen mainScreen] ...

  7. 如何变更站点 AD 域服务器IP地址

    在 winserver 2012  单森林单域,多站点环境中,想把某一个站点AD 域服务器IP地址更改,要如何操作,才能保证客户端正常运行,不影响客户端的运行.有些朋友也经常提出类似问题. 想在不影响 ...

  8. 基于FPGA的DDS任意波形发生器设计

    一.简介       DDS技术最初是作为频率合成技术提出的,由于其易于控制,相位连续,输出频率稳定度高,分辨率高, 频率转换速度快等优点,现在被广泛应用于任意波形发生器(AWG).基于DDS技术的任 ...

  9. 在广州学习PHP零基础可以学习吗?

    PHP现今作为互联网运用很广泛的编程语言,市场需求量也越来越高,而PHP开发工程师的薪资也是一路水涨船高,更多的人看到了PHP的发展前景,纷纷都想投入到PHP的开发大军中来,那么对于很多转行或者零基础 ...

  10. Codeforces Round #321 (Div. 2) E Kefa and Watch (线段树维护Hash)

    E. Kefa and Watch time limit per test 1 second memory limit per test 256 megabytes input standard in ...