线段树/树状数组裸题,用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. Vue.js之组件嵌套小demo

    Vue.js之组件嵌套的小demo项目 第一步:初始化一个wabpack项目,这里不在复述.第二步:在components文件夹下新建Header.vue Footer.vue和Users.vue三个 ...

  2. 【题解】Digit Tree

    [题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. ...

  3. SQLServer: 用 ApexSQLLog 恢复 SQL Server 数据

    https://blog.csdn.net/yenange/article/details/50512312

  4. Linxu下jenkins部署和基本配置

    一.OpenJdk1.8安装(tomcat  和 jenkins都依赖与java) ubuntu apt-cache search openjdk       #使用apt-cache搜索可以直接使用 ...

  5. vim常用快捷键记录

    yy复制一行 2yy复制2行 同理 3yy复制3行 p粘贴复制 dd删除一行 ctrl+f 翻页 ctrl+b 上翻 shift+a 跳到行尾进入insert模式 shift+i 跳到行首进入inse ...

  6. python之网络socket编程

    一.网络协议 客户端/服务器架构 1.硬件C/S架构(打印机) 2.软件C/S架构(互联网中处处是C/S架构):B/S架构也是C/S架构的一种,B/S是浏览器/服务器 C/S架构与socket的关系: ...

  7. Centos配置sftp

    sftp配置: ssh -V  使用ssh –V命令来查看openssh的版本,版本必须大于4.8p1,低于这个版本需要升级. 1.添加用户及用户组: groupadd sftp useradd -g ...

  8. ThinkPHP框架基础知识一

    ThinkPHP是一个快速.兼容而且简单的轻量级国产PHP开发框架,诞生于2006年初,原名FCS,2007年元旦正式更名为ThinkPHP,遵循Apache2开源协议发布,从Struts结构移植过来 ...

  9. 在安装mysqli的时候,出现error: ext/mysqlnd/mysql_float_to_double.h: No such file or directory

    /application/php5.:: warning: /ext/mysqli/mysqli_api.c::: error: ext/mysqlnd/mysql_float_to_double.h ...

  10. python__Django 分页

    自定义分页的类: #!/usr/bin/env python # -*- coding: utf-8 -*- # Created by Mona on 2017/9/20 from django.ut ...