HDU 4388 To the moon
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
A1 A2 ... An
... (here following the m operations. )
Output
Sample Input
Sample Output
Author
Source
Solution
- 只有被修改了的节点才会被新建,而这正是我们所期望的。(注意:从逻辑上讲,上个方法中 push-down 新建的那两个子节点在之前的某个本中就存在了,只是没有建出来。)
- 属于同一版本的新建节点在数组中是连续的,而且显然相邻版本的新建节点也是相邻的。
这样对于回到 $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的更多相关文章
- HDU 4383 To The Moon 解题报告
HDU 4383 To The Moon 题意翻译 已知一个长为\(n\)的序列\(a\),你需要进行下面的四种操作. C l r d 将区间\([l,r]\)中的数加上\(d\),同时时间加\(1\ ...
- hdu 4388 Stone Game II
Stone Game II HDU - 4388 题目大意: 给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则: 1. 任意选择一个堆,假设该堆有x个物品,从中选择 ...
- 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 ...
- HDU 4388 Stone Game II 博弈论 找规律
http://acm.hdu.edu.cn/showproblem.php?pid=4388 http://blog.csdn.net/y1196645376/article/details/5214 ...
- HDU 4348 To the moon 可持久化线段树
To the moon Problem Description BackgroundTo The Moon is a independent game released in November 201 ...
- HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和
To the moonTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...
- 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 询问当前时 ...
- hdu 4348 To the moon (主席树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...
- HDU 4348 To the moon 主席树 在线更新
http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...
随机推荐
- Java调用C/C++编写的第三方dll动态链接库(zz)
这里主要用的方法是JNI.在网上查资料时看到很多人说用JNI非常的复杂,不仅要看很多的文档,而且要非常熟悉C/C++编程.恐怕有很多人在看到诸如此类的评论时已经决定绕道用其他方法了.本文将做详细的介绍 ...
- lecture11-hopfiled网络与玻尔兹曼机
Hinton课程第11课 这部分的课程算是个知识背景,讲述RBM的来源吧,毕竟是按照hopfield--BM-RBM的路线过来的. 因为水平有限,都是直译,如果纠结某句话,肯定看不懂,所以这些课程只需 ...
- Redis的五种数据结构
Redis支持持久化只是它的一件武器,它提供了多达5种数据存储方式: 一 string(字符串) string是最简单的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个val ...
- C# Rotating Oval
This program is used to show how to generate an oval. The moon's orbit around the sun is an oval two ...
- 取消Git代理设置
昨天由于在用sourceTree上传下拉代码的时候,速度实在太慢,就照着百度上的方法设置了代理,结果导致sourceTree无法访问服务器,经检查排除发现可能是因为公司网络不能使用代理,被防火墙挡住了 ...
- 基于tiny4412原生uboot修改制作SD启动并烧写到emmc
最近入手tiny4412的标准板,底板SDK型号为1506.但是因为友善之臂提供的superboot不能进入boot菜单,此时我就不能通过tftp下载内核和通过nfs挂载根文件系统,于是想自己做个ub ...
- 在Winform中播放视频等【DotNet,C#】
在项目中遇到过这样的问题,就是如何在Winform中播放视频.当时考察了几种方式,第一种是直接使用Windows Media Player组件,这种最简单:第二种是利用DirectX直接在窗体或者控件 ...
- iOS开发中的错误整理,百思项目'我的'模块,tableFooterViewHeight的问题.提醒自己对KVO和Block的运用欠缺
一.错误分析:由于tableFooterView中的数据是通过请求服务器后得到的,tableFooterViewHeight也是根据请求过来的数据经过布局子控件而计算出来的.(注意:计算高度是在子线程 ...
- ActiveMQ(七)_伪集群和主从高可用使用
一.本文目的 介绍如何在同一台虚拟机上搭建高可用的Activemq服务,集群数量包含3个Activemq,当Activemq可用数>=2时,整个集群可用. 本 ...
- java-Date、String、Calendar转化
1.Calendar 转化 String //获取当前时间的具体情况,如年,月,日,week,date,分,秒等 Calendar calendat = Calendar.getInstance(); ...