传送门

To the moon

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Background

To The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker.
The
premise of To The Moon is based around a technology that allows us to
permanently reconstruct the memory on dying man. In this problem, we'll
give you a chance, to implement the logic behind the scene.

You‘ve been given $N$ integers $A_1, A_2, \dots, A_n$. On these integers, you need to implement the following operations:
1. C $l$ $r$ $d$: Adding a constant $d$ for every $\{A_i \mid l \le i \le r \}$, and increase the time stamp by 1, this is the only operation that will cause the time stamp increase.
2. Q $l$ $r$: Querying the current sum of $\{A_i \mid l \le i \le r\}$.
3. H l r t: Querying a history sum of {Ai | l <= i <= r} in time t.
4. B t: Back to time t. And once you decide return to a past, you can never be access to a forward edition anymore.
.. N, M ≤ 105, |A[i]| ≤ 109, 1 ≤ l ≤ r ≤ N, |d| ≤ 104 .. the system start from time 0, and the first modification is in time 1, t ≥ 0, and won't introduce you to a future state.

 

Input

n m
A1 A2 ... An
... (here following the m operations. )
 

Output

... (for each query, simply print the result. )

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

2 4
0 0
C 1 1 1
C 2 2 -1
Q 1 2
H 1 2 1

Sample Output

4
55
9
15

0
1

Author

HIT

Source


Solution

裸的可持久化线段树,但卡空间,只给 64 M。这是我写的第 3 道可持久化线段树的题目。
这篇随笔主要讨论:可持久化线段树在维护区间更新(相对于单点更新而言)操作,需要打懒标记(lazy-tag,以下简称“标记”或“tag”)时如何节省空间(内存)。
另外这个题目还涉及到一个“时间倒流”的操作——回到线段树的某个之前版本并删除在此之后的所有版本,我们也简单讨论下在这个操作后如何回收后续版本占用的空间
 
可持久化数据结构用于维护某个数据结构的不同历史版本,在逻辑上我们应当认为这些不同的version属于同一个object。一般来说,一个数据结构由若干元素构成,可持久化数据结构的思想是:相邻两个版本共用相同元素,在后一版本中只新建被更改了的元素。线段树(属于二叉树)的元素就是节点,节点维护着某个区间的信息。
 
当我们修改一个节点时当然要新建一个节点作为该节点的当前版本;如果该节点上有标记,非可持久化线段树的写法是:先将该标记push-down到两个子节点上,然后把当前节点的标记消除,最后再从两个子节点push-up来更新该节点。如果可持久化线段树也按这种方式处理标记,那么每次push-down就需要新建两个子节点,这两个子节点是和修改之前的父节点同一版本的,而不是当前版本的(“当前版本”是指修改之后的父节点的版本)。这样在query和update的过程中,push-down会新建大量节点,而且这么做还有一个弊端——它导致属于同一版本的新建节点在数组中不连续,这一点后面还会谈到。
 
对于标记还有另为一种处理方法——Never push down
在 query 或 update 的过程中不 push-down 任何节点的 tag,查询时,如果当前节点有标记,就把该标记对要查询的区间 $[l, r]$ 和该节点表示的区间 $[L, R]$ 的交集 $[\max(l, L), \min(r, R)]$ 的贡献更新到答案中去。
 
这么做的效果就是:
  1. 只有被修改了的节点才会被新建,而这正是我们所期望的。(注意:从逻辑上讲,上个方法中 push-down 新建的那两个子节点在之前的某个本中就存在了,只是没有建出来。)
  2. 属于同一版本的新建节点在数组中是连续的,而且显然相邻版本的新建节点也是相邻的。

这样对于回到 $t$ 时刻的“时间倒流”操作,只要把数组的 $\mathrm{tail}$ 直接置成 $\mathrm{tail}_t$ 就好了。($t$ 时刻内新建的节点在 $[\mathrm{tail}_{t-1}, \mathrm{tail}_t)$ 区间内)

Implementation

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+, M=2.5e6+;
typedef long long LL;
int ls[M], rs[M], tail, now, root[N];
LL add[M], sum[M]; void push_up(int id){
sum[id]=sum[ls[id]]+sum[rs[id]];
} int get_cur(int id, int now){
return tail++;
} LL query(int id, int L, int R, int l, int r){
if(l>R || L>r) return ;
if(l<=L && R<=r) return sum[id];
int mid=L+R>>;
return add[id]*(min(R, r)-max(L, l)+)+query(ls[id], L, mid, l, r)+query(rs[id], mid+, R, l, r);
} int Add(int id, int L, int R, int l, int r, int v){
if(l>R || L>r) return id;
int cur=get_cur(id, now); add[cur]=add[id], sum[cur]=sum[id]+(min(R, r)-max(L, l)+)*v; //copy tag only if(l<=L && R<=r){
add[cur]+=v;
ls[cur]=ls[id], rs[cur]=rs[id];
} else{
int mid=L+R>>;
ls[cur]=Add(ls[id], L, mid, l, r, v);
rs[cur]=Add(rs[id], mid+, R, l, r, v);
}
return cur;
} void init(int id, int L, int R){
add[id]=;
if(L==R){
scanf("%lld", sum+id);
return;
}
int mid=L+R>>;
init(ls[id]=tail++, L, mid);
init(rs[id]=tail++, mid+, R);
push_up(id);
} int main(){
// cout<<M<<endl;
for(int n, m; cin>>n>>m; ){
now=, tail=, init(root[now]=tail++, , n);
char op[];
int l, r, d, t; for(; m--; ){
scanf("%s", op);
if(*op=='C'){
scanf("%d%d%d", &l, &r, &d);
++now, root[now]=Add(root[now-], , n, l, r, d); //error-prone
}
else if(*op=='Q'){
scanf("%d%d", &l, &r);
printf("%lld\n", query(root[now], , n, l, r));
}
else if(*op=='H'){
scanf("%d%d%d", &l, &r, &t);
printf("%lld\n", query(root[t], , n, l, r)); //error-prone
}
// you can never access a forward edition anymore.
else scanf("%d", &t), now=t;
}
}
}
 
 

HDU 4388 To the moon的更多相关文章

  1. HDU 4383 To The Moon 解题报告

    HDU 4383 To The Moon 题意翻译 已知一个长为\(n\)的序列\(a\),你需要进行下面的四种操作. C l r d 将区间\([l,r]\)中的数加上\(d\),同时时间加\(1\ ...

  2. hdu 4388 Stone Game II

    Stone Game II HDU - 4388 题目大意: 给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则: 1. 任意选择一个堆,假设该堆有x个物品,从中选择 ...

  3. HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. HDU 4388 Stone Game II 博弈论 找规律

    http://acm.hdu.edu.cn/showproblem.php?pid=4388 http://blog.csdn.net/y1196645376/article/details/5214 ...

  5. HDU 4348 To the moon 可持久化线段树

    To the moon Problem Description BackgroundTo The Moon is a independent game released in November 201 ...

  6. HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和

    To the moonTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...

  7. hdu 4348 To the moon (主席树 区间更新)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4348 题意: 4种操作: C l r c   区间[l,r]加c,时间+1 Q l r    询问当前时 ...

  8. hdu 4348 To the moon (主席树)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...

  9. HDU 4348 To the moon 主席树 在线更新

    http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...

随机推荐

  1. WP8.1中C++的winodws运行时组件位移操作的差异

    最近学习WP8.1应用开发,想把C语言的SM3国密算法移植到手机app中.由于把C语言的代码转换成C#代码工作量较大,因此只能用winodws运行时组件来实现. SM3国密算法是一种HASH算法,具体 ...

  2. 每一个C#开发者必须知道的13件事情

    1.开发流程 程序的Bug与瑕疵往往出现于开发流程当中.只要对工具善加利用,就有助于在你发布程序之前便将问题发现,或避开这些问题. 标准化代码书写 标准化代码书写可以使代码更加易于维护,尤其是在代码由 ...

  3. Linux sysinfo获取系统相关信息

    Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h& ...

  4. 项目分布式部署那些事(2):基于OCS(Memcached)的Session共享方案

    在不久之前发布了一篇"项目分布式部署那些事(1):ONS消息队列.基于Redis的Session共享,开源共享",因为一些问题我们使用了阿里云的OCS,下面就来简单的介绍和分享下相 ...

  5. Android开发自学笔记(Android Studio1.3.1)—2.开始第一个Android应用

    一.前言      使用Android Studio开发Android应用是一件非常简单的事情,因为它会帮你自动完成很多工作.本篇我们主要完成一个单击按钮在文本框显示当前时间的简单应用,借此来演示一下 ...

  6. 转载:ZooKeeper Programmer's Guide(中文翻译)

    本文是为想要创建使用ZooKeeper协调服务优势的分布式应用的开发者准备的.本文包含理论信息和实践信息. 本指南的前四节对各种ZooKeeper概念进行较高层次的讨论.这些概念对于理解ZooKeep ...

  7. 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure

    yu Code 15 Comments  机器学习(ML),自然语言处理(NLP),信息检索(IR)等领域,评估(Evaluation)是一个必要的 工作,而其评价指标往往有如下几点:准确率(Accu ...

  8. MVC架构设计——EF-Code First

    详情参考:http://www.cnblogs.com/guomingfeng/archive/2013/05/28/mvc-ef-repository.html

  9. hdu1754 I hate it线段树模板 区间最值查询

    题目链接:这道题是线段树,树状数组最基础的问题 两种分类方式:按照更新对象和查询对象 单点更新,区间查询; 区间更新,单点查询; 按照整体维护的对象: 维护前缀和; 维护区间最值. 线段树模板代码 # ...

  10. 作业成绩 final-review 20161201-1203 15:05

    final-review阶段,20161201-20161203 15:05 final 评论II截止 20161204 09:00 申诉截止时间 20161207 12:00,微信联系杨贵福. 凡描 ...