板子题,正在努力看懂板子。。

http://blog.csdn.net/acm_cxlove/article/details/7815019

http://www.cnblogs.com/kuangbin/archive/2013/04/21/3034081.html

 #include <cstdio>
#include <cstring>
#include <algorithm> #define keyTree (ch[ ch[root][1] ][0])
#define LL long long using namespace std; const int maxn = ; struct SpalyTree{
int sz[maxn];
int ch[maxn][];
int pre[maxn];
int root,top1,top2;
int ss[maxn], que[maxn]; void Rotate(int x,int f)
{
int y = pre[x];
push_down(y);
push_down(x);
ch[y][!f] = ch[x][f];
pre[ ch[x][f]] = y;
pre[x] = pre[y];
if(pre[x]) ch[pre[y] ][ch[pre[y]][]==y ] = x;
ch[x][f] = y;
pre[y] = x;
push_up(y);
}
void Splay(int x,int goal)
{
push_down(x);
while(pre[x] != goal)
{
if(pre[pre[x] ] == goal)
{
Rotate(x,ch[pre[x] ][] == x);
}
else
{
int y = pre[x],z = pre[y];
int f = (ch[z][] == y);
if(ch[y][f] == x)
{
Rotate(x,!f),Rotate(x,f);
}
else
{
Rotate(y,f),Rotate(x,f);
}
}
}
push_up(x);
if(goal == ) root = x;
}
void RotateTo(int k,int goal)
{
int x = root;
push_down(x);
while(sz[ ch[x][] ] != k)
{
if(k < sz[ ch[x][] ])
{
x = ch[x][];
}
else
{
k -= (sz[ch[x][] ] + );
x = ch[x][];
}
push_down(x);
}
Splay(x,goal);
}
void erase(int x)
{
int father = pre[x];
int head = ,tail = ;
for(que[tail++] = x;head < tail;head++)
{
ss[top2++] = que[head];
if(ch[que[head] ][]) que[tail++] = ch[que[head] ][];
if(ch[que[head] ][]) que[tail++] = ch[que[head] ][];
}
ch[father ][ch[father][]==x ] = ;
push_up(father);
}
void debug(){printf("%d\n",root);Treaval(root);}
void Treaval(int x)
{
if(x)
{
Treaval(ch[x][]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,val = %2d\n",x,ch[x][],ch[x][],pre[x],sz[x],val[x]);
Treaval(ch[x][]);
}
}
void NewNode(int &x,int c)
{
if(top2) x = ss[--top2];
else x = ++top1;
ch[x][] = ch[x][] = pre[x] = ;
sz[x] = ; val[x] = sum[x] = c;
add[x] = ;
}
void push_down(int x)
{
if(add[x])
{
val[x] += add[x];
add[ch[x][] ] += add[x];
add[ch[x][] ] += add[x];
sum[ch[x][] ] += (LL)sz[ch[x][] ]*add[x];
sum[ch[x][] ] += (LL)sz[ch[x][] ]*add[x];
add[x] = ;
}
}
void push_up(int x)
{
sz[x] = +sz[ch[x][] ] + sz[ch[x][] ];
sum[x] = add[x] + val[x] + sum[ch[x][] ] + sum[ch[x][] ];
}
void makeTree(int &x,int l,int r,int f)
{
if(l > r) return ;
int m = (l+r)>>;
NewNode(x,num[m]);
makeTree(ch[x][],l,m-,x);
makeTree(ch[x][],m+,r,x);
pre[x] = f;
push_up(x);
}
void init(int n)
{
ch[][] = ch[][] = pre[] = sz[] = ;
add[] = sum[] = ; root = top1 = ;
NewNode(root, -);
NewNode(ch[root][],-);
pre[top1] = root;
sz[root] = ; for(int i=;i<n;i++)
scanf("%d",&num[i]);
makeTree(keyTree,,n-,ch[root][]);
push_up(ch[root][]);
push_up(root);
}
void update()
{
int l,r,c;
scanf("%d%d%d",&l,&r,&c);
RotateTo(l-,);
RotateTo(r+,root);
add[keyTree] += c;
sum[keyTree] += (LL)c*sz[keyTree];
}
void query()
{
int l,r;
scanf("%d%d",&l,&r);
RotateTo(l-,);
RotateTo(r+,root);
printf("%lld\n",sum[keyTree]);
} int num[maxn];
int val[maxn];
int add[maxn];
LL sum[maxn];
}spt; int n,m; int main()
{
while(~scanf("%d%d",&n,&m))
{
spt.init(n);
char op[];
for(int i=;i<m;i++)
{
scanf("%s",op);
if(op[] == 'Q')
{
spt.query();
}
else
spt.update();
}
}
}

POJ3468/splay树/成段更新的更多相关文章

  1. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

  2. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  3. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  4. hdu 4747【线段树-成段更新】.cpp

    题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...

  5. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...

  6. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )

    线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...

  7. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  8. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

  9. poj 3648 线段树成段更新

    线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...

随机推荐

  1. Django model中的class Meta详解

    通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(models.Model): bar = models.CharFi ...

  2. Python 学习 第十六篇:networkx

    networkx是Python的一个包,用于构建和操作复杂的图结构,提供分析图的算法.图是由顶点.边和可选的属性构成的数据结构,顶点表示数据,边是由两个顶点唯一确定的,表示两个顶点之间的关系.顶点和边 ...

  3. Kafka 入门三问

    目录 1 Kafka 是什么? 1.1 背景 1.2 定位 1.3 产生的原因 1.4 Kafka 有哪些特征 消息和批次 模式 主题和分区 生产者和消费者 broker 和 集群 1.5 Kafka ...

  4. mybatis抽取出的工具-(一)通用标记解析器(即拿即用)

    目录 1. 简介 1.1 mybatis-config.xml 中使用 1.2 xxxMapper.xml 中使用 2. 原理 2.1 GenericTokenParser 成员变量 2.2 Gene ...

  5. Go+Python双剑合璧

    目的 Python调用Go的方法,Python有很多功能强悍又使用简洁的库.而新生军Go的多核心利用率也是非常强悍的.当然这是明面上的优点.反正你有很多理由想要让Python能够调用Go的方法. 实验 ...

  6. Mixing x86 with x64 code (混合编写x86和x64代码)

    几个月前我小小的研究了在WOW64下的32位进程中运行native x64代码. 第二个设想是在64位进程下运行x86代码.它们都是可以的,如我google的一样, 已经有人在使用这两种方法了: ht ...

  7. Pycharm中怎么给字典中的多个键-值对同时加上单引号

    今天看了个爬虫视频,崔庆才讲师的免费视频, 里面一个批量给header加引号2s完成,这波操作让我眼前一亮. 最终还是发现了骚操作的背后手速是真的快. pycharm中按ctrl+r 勾选右上角的Re ...

  8. js-cookie和session

    ###1.cookie 含义: 存储在访问者的计算机中的变量,即存储在客户端 创建一个cookie /* getCookie方法判断document.cookie对象中是否存有cookie,若有则判断 ...

  9. C调用C++, C++调用C方法

    1. C 调用 C++封装好后的函数: -> 在C++中有一个函数 int main_cpp(): -> 首先构建头文件, #ifndef CPP_FILE_H   #define CPP ...

  10. Vue+iview实现添加删除类

    <style> .tab-warp{ border-bottom: solid 1px #e0e0e0; overflow: hidden; margin-top: 30px; posit ...