线段树/树状数组裸题,用splay写

splay也是基本操作pushup pushdown

话说我就是找不到全一点的模板,我自己写又全是bug,导致代码风格一直变来变去= =

关键是建树和区间操作(区间和,区间翻转,区间合并这几个写法都很难统一)

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std;
using namespace __gnu_cxx; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct Node{
Node *ch[],*fa;
int id,s,v,add;
ll sum;
void pushdown()
{
if(add)
{
if(ch[])
{
ch[]->v += add;
ch[]->add += add;
ch[]->sum += (ll)add*ch[]->s;
}
if(ch[])
{
ch[]->v += add;
ch[]->add += add;
ch[]->sum += (ll)add*ch[]->s;
}
add=;
}
}
void pushup()
{
s = ch[]->s + ch[]->s + ;
sum = v + ch[]->sum + ch[]->sum;
}
};
Node *root,NODE[N],*null=&NODE[];
int num[N],n,top;
struct SplayTree{
void Rotate(Node *x,int f)
{
Node* y= x->fa;
y->pushdown();x->pushdown();
y->ch[!f] = x->ch[f];
x->ch[f]->fa = y;
x->fa = y->fa;
if(x->fa!=null)y->fa->ch[y->fa->ch[]==y]=x;
x->ch[f] = y;
y->fa=x;
y->pushup();
}
void splay(Node* x,Node* goal)//把x splay到goal下面
{
x->pushdown();
while(x->fa!=goal)
{
if(x->fa->fa == goal)Rotate(x,x->fa->ch[]==x);
else
{
Node *y=x->fa,*z=y->fa;
int f=(z->ch[]==y);
y->ch[f]==x ? Rotate(x,!f):Rotate(y,f);
Rotate(x,f);
}
}
x->pushup();
if(goal==null)root=x;
}
void RTO(int k,Node *goal)//把排名为k的节点splay到goal下面
{
Node *x=root;
x->pushdown();
while(x->ch[]->s+!=k)
{
if(k < x->ch[]->s+)x=x->ch[];
else
{
k -= x->ch[]->s+;
x = x->ch[];
}
x->pushdown();
}
splay(x,goal);
}
Node* newnode(Node* fa,int v)
{
Node *x=&NODE[++top];
x->id=top;
x->ch[]=x->ch[]=null;
x->s=;
x->v=x->sum=v;
x->add=;
x->fa=fa;
return x;
}
void build(Node* &x,int l,int r,Node* fa)
{
if(l>r)return ;
int m=(l+r)>>;
x=newnode(fa,num[m]);
build(x->ch[],l,m-,x);
build(x->ch[],m+,r,x);
x->pushup();
}
void debug(Node* x)
{
if(x!=null)
{
debug(x->ch[]);
cout<<x->v<<" "<<x->sum<<endl;
debug(x->ch[]);
}
}
void init(int n)
{
top=;
null->id=;
null->fa = null->ch[] = null->ch[] = NULL;
null->s = null->add = null->v = null->sum = ;
root = newnode(null,-);
root->ch[] = newnode(root,-);
root->s=;
for(int i=;i<=n;i++)scanf("%d",&num[i]);
build(root->ch[]->ch[],,n,root->ch[]);
root->ch[]->pushup();root->pushup();
}
void update()
{
int l,r,c;
scanf("%d%d%d",&l,&r,&c);
RTO(l,null);
RTO(r+,root);
root->ch[]->ch[]->add += c;
root->ch[]->ch[]->v += c;
root->ch[]->ch[]->sum += (ll)c*root->ch[]->ch[]->s;
}
void query()
{
int l,r;
scanf("%d%d",&l,&r);
RTO(l,null);
RTO(r+,root);
printf("%lld\n",root->ch[]->ch[]->sum);
}
}spt;
int main()
{
int n,q;
scanf("%d%d",&n,&q);
spt.init(n);
while(q--)
{
char s[];
scanf("%s",s);
if(s[]=='Q')spt.query();
else spt.update();
}
return ;
}
/************
10 5
1 5 64 8 2 47 6 4 6 7
************/

POJ3468 splay的更多相关文章

  1. poj3468 splay(成段跟新 区间求和)

    用splay做了一遍. 建树时是按照数列序号从小到大排好的,每个节点左子树的序号小于右子树的序号及这个节点本身.由于查询[l,r]要伸展l-1,r+1所以我们要多加2个结点,保证边界处理时不出问题.由 ...

  2. POJ3468/splay树/成段更新

    板子题,正在努力看懂板子.. http://blog.csdn.net/acm_cxlove/article/details/7815019 http://www.cnblogs.com/kuangb ...

  3. POJ-3468 A Simple Problem with Integers Splay Tree区间练习

    题目链接:http://poj.org/problem?id=3468 以前用线段树做过,现在用Splay Tree A了,向HH.kuangbin.cxlove大牛学习了各种Splay各种操作,,, ...

  4. Splay POJ3468(老题新做)

    A Simple Problem with Integers Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d ...

  5. POJ3468:A Simple Problem with Integers (线段树||树状数组||Splay解决基本问题的效率对比)

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  6. POJ 3468 A Simple Problem with Integers (splay tree入门)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 47944   ...

  7. poj 3468 Splay 树

    大二上的时候.写过一个AVL的操作演示,今天一看Splay.发现和AVL事实上一样,加上线段树的基础,懒惰标记什么都知道.学起来轻松很多哦 我參考的模板来自这里  http://blog.csdn.n ...

  8. BZOJ 1251: 序列终结者 [splay]

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3778  Solved: 1583[Submit][Status][Discu ...

  9. [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:   文本:由0个或 ...

随机推荐

  1. MySql 安装常见问题汇总

    说明: 以下是针对 Mac 10.11 系统 以前,安装 MySql 数据库后, 设置的密码过于复杂,想更改为简单的密码, 方便数据库的使用. 1. 关闭和启动 MySql 数据库的方法: Syste ...

  2. Python Selenium 自动化测试

    本文转载 作者:灰蓝蓝蓝蓝蓝蓝链接:http://www.jianshu.com/p/5188cb3ab790來源:简书著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.更多技术博客 ...

  3. 斯坦福大学Andrew Ng - 机器学习笔记(2) -- 逻辑回归 & 正则化

    大概用了一个月,Andrew Ng老师的机器学习视频断断续续看完了,以下是个人学习笔记,入门级别,权当总结.笔记难免有遗漏和误解,欢迎讨论. 鸣谢:中国海洋大学黄海广博士提供课程视频和个人笔记,在此深 ...

  4. 教你管理SQL实例系列(1-15)

    全系列转自:51CTO ->jimshu http://jimshu.blog.51cto.com 目录及原本连接如下: 教你管理SQL实例(1)数据库实例 教你管理SQL实例(2)服务启动帐户 ...

  5. 剑指offer面试54题

    面试54题: 题目:二叉搜索树的第K大节点 题:给定一颗二叉搜索树,请找出其中的第k小的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4. 解题思 ...

  6. python之数据的序列化

    参考博客:http://www.cnblogs.com/yyds/p/6563608.html 数据的序列化功能表 json.dumps() 将python数据类型转换为(json)字符串 json. ...

  7. pyhton3 hashlib模块

    hashlib模块提供一下常量属性 hashlib.algorithms_guaranteed 获取保证在所有平台上此模块支持的哈希算法名称的集合 hashlib.algorithms_availab ...

  8. 容器排序之sort,stable_sort

    bool isShorter(const string &s1, const string &sz){ return s1.size() < sz.size(); } int m ...

  9. CSS3中新颖的布局方法

    本人已经很久没用 bootstrap 什么的了,而现阶段一点卑微的梦想就是自己做框架,毕竟也才入门不久. 所以在寻找布局的共通性/稳定性及拓展性时,会发觉 CSS3 的这三种方法比栅栏布局要有趣得多. ...

  10. 教你在windows10环境下如何安装minepy并成功运行!

    在学习使用sklearn做单机特征工程这篇文章时,发现在计算互信息时from minepy import MINE代码运行出错ModuleNotFoundError: No module named ...