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

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. IIS配置Url重写实现http自动跳转https的重定向方法(100%解决)

    引言 本文推荐阅读地址:https://www.52abp.com/BlogDetails/10008 这种文章网上可以说一搜一大把,但是我为什么还要写呢,因为一搜一把没把我气死,都是东抄西挪的东西, ...

  2. 快速导入导出Oracle数据demo(sqlldr、UTL_FILE)

    本文演示快速sqlldr导入.UTL_FILE导出Oracle表数据实例 表结构如下,演示数据约112万,可自行准备. create table MemberPointDemo ( MEMBERID ...

  3. nodejs源码编译-mipse64el架构

    下载nodejs,node-v6.1.0.tar.gz 链接: https://pan.baidu.com/s/1eCtNBWD5yaKiQIHp3pRKew 提取码: faun 注意对应版本的gcc ...

  4. abbix通过JMX监控Tomcat(被监控端安装Tomat的服务器防火墙策略iptables配置)

    原文地址:http://jaychang.iteye.com/blog/2214830 一.目前的环境 被监控端192.168.153.191 /usr/local/tomcat 下载了catalin ...

  5. 小菜鸡儿的第三次OO博客

    规格化设计历史 规格化设计的历史目前网上的资料并不多,百度谷歌必应也表示无能为力...... 在这里结合现实情况讲一讲自己对程序规格化的理解,首先代码规格化对代码的影响是间接的,或许它不能让你代码里面 ...

  6. ARC 066D Xor Sum AtCoder - 2272 (打表找规律)

    Problem Statement You are given a positive integer N. Find the number of the pairs of integers u and ...

  7. p151开映射札记

    1. 如何理解这句话? 2.连续有什么用? 3.为什么区间包含,经过算子T还是包含? 谢谢 谢谢学长 我懂了  1.2.     3有点儿模糊 1.连续等价于开集原像是开集,而可逆算子的逆的原像就是的 ...

  8. awk+sed编程

  9. HashSet中存放不重复元素

    一.自定义对象存放在hashSet中,保证元素不重复.重写hashCode()和equals()方法 public class Student{ private String name; privat ...

  10. O(N) 求数组中最大子串和

    int MaxSubSum3(int *arr, int len) { int i; long long MaxSum = 0; long long CurSum = 0; for(int i = 0 ...