POJ3468/splay树/成段更新
板子题,正在努力看懂板子。。
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树/成段更新的更多相关文章
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
- 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. 这题 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- poj 3648 线段树成段更新
线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...
随机推荐
- 【C# 复习总结】类、继承和接口
1 类 定义新的数据类型以及这些新的数据类型进行相互操作的方法 定义方式: class Cat { } class Cat:object { } C#中所有的类都是默认由object类派生来的,显示指 ...
- MySQL表结构变更,不可不知的Metadata Lock
在线上进行DDL操作时,相对于其可能带来的系统负载,其实,我们最担心的还是MDL其可能导致的阻塞问题. 一旦DDL操作因获取不到MDL被阻塞,后续其它针对该表的其它操作都会被阻塞.典型如下,如阻塞稍久 ...
- python第八章:多任务--小白博客
多线程threading 多线程特点: #线程的并发是利用cpu上下文的切换(是并发,不是并行)#多线程执行的顺序是无序的#多线程共享全局变量#线程是继承在进程里的,没有进程就没有线程#GIL全局解释 ...
- 【fetch跨域请求】cors
当使用fetch 发起跨域请求时,CORS(跨域资源共享Cross-origin resource sharing) 请求fetch const body = {name:"Good boy ...
- STL vector用法
基本操作 1.构造函数 vector():创建一个空vector vector(int nSize):创建一个vector,元素个数为nSize vector(int nSize,const t&am ...
- 书城项目第五阶段---book表的curd
JavaEE三层架构分析 MVC
- p86商空间也是Banach空间
1.为什么要引入Zk? 2.为什么这个等式成立,和为什么要引入uk? 3.为什么为什么等于0? 属于M,则商空间是0元,p128最上面的第二个笔记
- 如何恢复Eclipse中被误删除的文件
在使用Eclipse时,可能会不小心误删除一些文件,没关系,Eclipse有个非常强大的功能,能让这些误删除的文件恢复回来,下面就来介绍一下. 工具/原料 Eclipse Kepler 方法/步骤 ...
- Python并发编程
进程 相关概念 进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本 ...
- 使用junit测试
package creeper; import java.util.Scanner; public class size { private static int intercePosition = ...