【xsy1629】可持久化序列 - 可持久化平衡树
题意
你现在要用数据结构维护一个长度为n的序列。
这个序列支持三种操作:
1 l r:将序列中的第l项到第r项这一段翻转。
2 l r:查询序列中[l,r]这一段的和。
3 p:回到第p个历史版本。
每一个翻转操作的时候记作序列处于一次新的版本,初始的版本是0。
每一个回到历史版本之后再操作的版本都是一个最新版本,不是第p+1版本。
\(1\leq n\leq 50000\)
代码
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;
#define rep(i,a,b) for (int i=(a);i<=(b);i++)
const int N=65536;
const int M=65536;
const int S=8388608;
int n,m;
int a[N];
namespace Treap {
int rt[M],nowVer,allVer;
int tot;
struct Node {
int c[2]; int rev;
int fix; //fix=dep
int dat,sum;
int siz;
Node(int _fix=0,int _dat=0,int _siz=0) {
c[0]=c[1]=rev=0;
fix=_fix;
dat=sum=_dat;
siz=_siz;
}
}tr[S];
struct D {
int c[2];
D(void) {
c[0]=c[1]=0;
}
};
void Update(int x) {
tr[x].sum=tr[tr[x].c[0]].sum+tr[tr[x].c[1]].sum+tr[x].dat;
tr[x].siz=tr[tr[x].c[0]].siz+tr[tr[x].c[1]].siz+1;
}
int New_Node(int key,int fix,int dat) {
int x=++tot;
tr[x]=Node(fix,dat,1);
return x;
}
int Copy_Node(int form) {
int x=++tot;
tr[x]=tr[form];
return x;
}
int Build(int *a,int l,int r,int dep) {
int mid=(l+r)>>1;
int x=New_Node(mid,dep,a[mid]);
if (l<mid)
tr[x].c[0]=Build(a,l,mid-1,dep+1);
if (mid<r)
tr[x].c[1]=Build(a,mid+1,r,dep+1);
Update(x);
return x;
}
void Clear(int x) {
if (!tr[x].rev) return; tr[x].rev=0;
if (tr[x].c[0]>0) {
tr[x].c[0]=Copy_Node(tr[x].c[0]);
tr[tr[x].c[0]].rev^=1;
}
if (tr[x].c[1]>0) {
tr[x].c[1]=Copy_Node(tr[x].c[1]);
tr[tr[x].c[1]].rev^=1;
}
swap(tr[x].c[0],tr[x].c[1]);
}
D Split(int x,int rk) {
D t; if (!x) return t;
int x1=Copy_Node(x); Clear(x1);
if (rk<=tr[tr[x1].c[0]].siz) {
t=Split(tr[x1].c[0],rk);
tr[x1].c[0]=t.c[1]; Update(x1);
t.c[1]=x1;
}
else if (rk==tr[tr[x1].c[0]].siz+1) {
t.c[1]=tr[x1].c[1];
tr[x1].c[1]=0; Update(x1);
t.c[0]=x1;
}
else {
t=Split(tr[x1].c[1],rk-1-tr[tr[x1].c[0]].siz);
tr[x1].c[1]=t.c[0]; Update(x1);
t.c[0]=x1;
}
return t;
}
int Merge(int x1,int x2) {
if (!x1) return x2;
if (!x2) return x1;
if (tr[x1].fix<tr[x2].fix) {
int x3=Copy_Node(x1);
Clear(x3);
tr[x3].c[1]=Merge(tr[x3].c[1],x2);
Update(x3);
return x3;
}
else {
int x3=Copy_Node(x2);
Clear(x3);
tr[x3].c[0]=Merge(x1,tr[x3].c[0]);
Update(x3);
return x3;
}
}
}
int rd(void) {
int x=0,f=1; char c=getchar();
while (!isdigit(c)) {
if (c=='-') f=-1;
c=getchar();
}
while (isdigit(c)) {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
void Goto(int p) {
using namespace Treap;
nowVer=p;
}
void Reverse(int l,int r) {
using namespace Treap;
D t1=Split(rt[nowVer],r);
D t2=Split(t1.c[0],l-1); //use t2.c[1]
int x=Copy_Node(t2.c[1]);
tr[x].rev^=1;
rt[++allVer]=Merge(Merge(t2.c[0],x),t1.c[1]);
nowVer=allVer;
}
int Query(int l,int r) {
using namespace Treap;
D t1=Split(rt[nowVer],r);
D t2=Split(t1.c[0],l-1);
return tr[t2.c[1]].sum;
}
int main(void) {
#ifndef ONLINE_JUDGE
freopen("N.in","r",stdin);
freopen("N.out","w",stdout);
#endif
n=rd(),m=rd();
rep(i,1,n) a[i]=rd();
Treap::rt[0]=Treap::Build(a,1,n,1);
rep(i,1,m) {
int c=rd();
switch (c) {
case 1: {
int l=rd(),r=rd();
Reverse(l,r);
break;
}
case 2: {
int l=rd(),r=rd();
int ans=Query(l,r);
printf("%d\n",ans);
break;
}
case 3: {
int p=rd();
Goto(p);
break;
}
}
}
return 0;
}
【xsy1629】可持久化序列 - 可持久化平衡树的更多相关文章
- 可持久化Trie & 可持久化平衡树 专题练习
[xsy1629]可持久化序列 - 可持久化平衡树 http://www.cnblogs.com/Sdchr/p/6258827.html [bzoj4260]REBXOR - Trie 事实上只是一 ...
- luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树)(主席树)
luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目 #include<iostream> #include<cstdlib> #include< ...
- Redis的两种持久化方式-快照持久化和AOF持久化
Redis为了内部数据的安全考虑,会把本身的数据以文件形式保存到硬盘中一份,在服务器重启之后会自动把硬盘的数据恢复到内存(redis)的里边,数据保存到硬盘的过程就称为"持久化"效 ...
- Redis的两种持久化方式-快照持久化(RDB)和AOF持久化
Redis为了内部数据的安全考虑,会把本身的数据以文件形式保存到硬盘中一份,在服务器重启之后会自动把硬盘的数据恢复到内存(redis)的里边,数据保存到硬盘的过程就称为“持久化”效果. redis有两 ...
- Hibernate,Session方法使得java对象进入持久化状态;持久化对象特征
以下情况java对象进入持久化状态: session.save()方法把临时对象转变为持久化对象. session.load()和session.get()方法得到的对象总是处于持久化状态. sess ...
- Redis数据持久化之AOF持久化
一.RDB持久化的缺点创建RDB文件需要将服务器所有的数据库的数据都保存起来,这是一个非常耗费资源和时间的操作,所以服务器需要隔一段时间才能创建一个新的RDB文件,就也是说创建RDB文件的操作不能执行 ...
- JMS学习(五)--ActiveMQ中的消息的持久化和非持久化 以及 持久订阅者 和 非持久订阅者之间的区别与联系
一,消息的持久化和非持久化 ①DeliveryMode 这是传输模式.ActiveMQ支持两种传输模式:持久传输和非持久传输(persistent and non-persistent deliver ...
- redis系列:RDB持久化与AOF持久化
前言 什么是持久化? 持久化(Persistence),即把数据(如内存中的对象)保存到可永久保存的存储设备中(如磁盘).持久化的主要应用是将内存中的对象存储在数据库中,或者存储在磁盘文件中.XML数 ...
- RabbitMQ学习笔记(6)----RabbitMQ 持久化和非持久化
持久化:将交换机或队列数据保存到磁盘,服务器宕机或重启之后依然存在. 非持久化:将交换机或队列的数据保存到内存中,服务器宕机或重启之后数据将不存在. 在RabbitMQ中也提供了持久化和非持久化方式. ...
随机推荐
- C#在高性能计算领域为什么性能却如此不尽人意
C#的优雅,强大IDE的支持,.net下各语言的二进制兼容,自从第一眼看到C#就被其良好的设计吸引.一直希望将其应用于高性能计算领域,长时间努力却效果却不尽如人意. 对于小的测试代码用例而言,C#用2 ...
- LoadRunner培训初级教程
一 LoadRunner简介 1.1 Loadrunner介绍 LoadRunner 是 HP Mercury Interactive 用来测试应用程序性能的工具 LoadRunner 通过模拟一个 ...
- install phpexcel using composer in thinkPHP
Environment Window 10.1 XAMPP 7.0.9 (PHP 7.0.9) thinkPHP 5.0.1 Steps # visit https://getcomposer.org ...
- requirejs基础教程
一.初识requirejs 随着网站功能逐渐丰富,网页中的js也变得越来越复杂和臃肿,原有通过script标签来导入一个个的js文件这种方式已经不能满足现在互联网开发模式,我们需要团队协作.模块复用. ...
- ImportError: cannot import name 'check_arrays'
from sklearn.utils.validation import check_arrays 执行 from sklearn.utils.validation import check_arra ...
- 对c++ public、protected、private关键字的理解
首先要明确一下: 1.这三个关键字在两种地方会用到,一个是对类的成员变量和成员函数修饰时(比如私有的成员变量,受保护的成员变量·,公有的函数),还有一种是对继承方式的修饰(比如公有继承,保护继承). ...
- c++打开浏览器
调用外部命令 如果用默认浏览器,就调用ShellExecute(NULL, _T("open"), _T("explorer.exe"), _T("h ...
- 【Python】使用Supervisor来管理Python的进程
来源 : http://blog.csdn.net/xiaoguaihai/article/details/44750073 1.问题描述 需要一个python的服务程序在后台一直运行,不能让 ...
- eclipse添加easyExport插件,打开本地文件
下载地址:https://github.com/samsonw/OpenExplorer/downloads 官方:http://sourceforge.net/projects/easystruts ...
- iOS UIButton 设置图片不变型setImage
[btn.imageView setContentMode:UIViewContentModeScaleAspectFill];