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. 13.JAVA-包package、import使用

    1.包的定义 之前我们学习java时,生成的class文件都是位于当前目录中,假如出现了同名文件,则会出现文件覆盖问题,因此就需要设置不同的目录(定义包),来解决同名文件冲突问题. 并且在大型项目中, ...

  2. When you want to give up, remember why you started.

    When you want to give up, remember why you started.当你想要放弃的时候,请记住当初你为何而开始.

  3. android 跨进程通讯 AIDL

    跨进程如何通讯?两个进程无法直接通讯,通过Android系统底层间接通讯.基于service的aidl实现跨进程通讯. 什么叫AIDL? Android interface definition la ...

  4. Outlook 2016 自动发送/接收无法正常工作

    如果您的自动/发送接收由于某种原因停止工作,可能会非常令人沮丧,因为您必须记住手动执行发送/接收(F9).如果您遇到Outlook无法自动发送或接收电子邮件的问题,可以尝试以下几项操作. #1 发送/ ...

  5. Python+selenium之截图图片并保存截取的图片

    本文转载:http://blog.csdn.net/u011541946/article/details/70141488 http://www.cnblogs.com/timsheng/archiv ...

  6. SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)

    题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? ...

  7. 11gR2 如何诊断节点重启问题

    本文对如何诊断11gR2 GI环境下的节点重启问题进行了一些介绍. 首先,像10g版本一样,我们首先介绍在GI中能够导致节点重启的进程.1.Ocssd.bin:这个进程的功能和10g版本的功能基本差不 ...

  8. poj 3159 Candies (差分约束)

    一个叫差分约束系统的东西.如果每个点定义一个顶标x(v),x(t)-x(s)将对应着s-t的最短路径. 比如说w+a≤b,那么可以画一条a到b的有向边,权值为w,同样地给出b+w2≤c,a+w3≤c. ...

  9. UVA 1599, POJ 3092 Ideal Path 理想路径 (逆向BFS跑层次图)

    大体思路是从终点反向做一次BFS得到一个层次图,然后从起点开始依次向更小的层跑,跑的时候选则字典序最小的,由于可能有多个满足条件的点,所以要把这层满足条件的点保存起来,在跑下一层.跑完一层就会得到这层 ...

  10. ios常见错误之 Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?

    Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the desi ...