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询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...
随机推荐
- [LeetCode] Rank Scores -- 数据库知识(mysql)
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- matplotlib 入门之Image tutorial
文章目录 载入图像为ndarray 显示图像 调取各个维度 利用cmp 获得像素点的RGB的统计 通过clim来限定rgb 标度在下方 插值,马赛克,虚化 matplotlib教程学习笔记 impor ...
- Python-类的绑定方法与非绑定方法
类中定义的函数分成两大类 一:绑定方法(绑定给谁,谁来调用就自动将它本身当作第一个参数传入): 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 类.boud_method() ...
- 02-HTML之head标签
head标签 head内常用标签表 标签 类型 意义 <title></titile> 双闭合标签 定义网页标题 <style></style> 双闭合 ...
- servlet程序HTTP Status 500 - Error instantiating servlet class 解决
在eclipase 中编译 servlet 但是一致报 HTTP Status 500 - Error instantiating servlet class XXX类 的问题 , 解决方法 1. ...
- centos7 network eno16777736
Network service not running - eno16777736 not activated - CentOShttps://www.centos.org/forums/viewto ...
- 安装openssl
此方法安装原因: 由于我用是非企业版 redhat 没有注册 有很多的yum 不能安装 openssl是在其中. 开始安装: 1.虚拟机挂载ios 镜像文件 2.进入终端 cd /media/RH ...
- [服务器]Gartner:2018年第四季度全球服务器收入增长17.8% 出货量增长8.5%
Gartner:2018年第四季度全球服务器收入增长17.8% 出货量增长8.5% Gartner 是不是也是花钱买榜的主啊.. 简单看了一下 浪潮2018Q4的营收18亿刀 (季度营收110亿人民币 ...
- spring的xml配置里,最好不要配置xsd的版本名称
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 设计模式之原型模式(c++)
问题描述 看到这个模式,很容易想到小时候看的<西游记>,齐天大圣孙悟空发飙的时候可以通过自己头上的 3 根毛立马复制出来成千上万的孙悟空, 对付小妖怪很管用(数量最重要). Prototy ...