To the moon

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

Total Submission(s): 4287    Accepted Submission(s): 923

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],..., A[N]. On these integers, you need to implement the following operations:

1. C l r d: Adding a constant d for every {Ai | l <= i <= 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 {Ai | l <= i <= 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
可持久化线段树,这道题目会卡内存,所以在pushdown和pushup的时候,新建节点可能会超内存
那么我就可以跳过pushup和pushdown
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <stdio.h> using namespace std;
typedef long long int LL;
const int maxn=1e5;
int rt[maxn*35+5];
int ls[maxn*35+5];
int rs[maxn*35+5];
int p;
LL sum[maxn*35+5];
LL pos[maxn*35+5];
int n,m,t;
int newnode()
{
ls[p]=rs[p]=sum[p]=pos[p]=0;
return p++;
}
void build(int &node,int begin,int end)
{
if(!node) node=newnode();
if(begin==end)
{
scanf("%lld",&sum[node]);
return;
}
int mid=(begin+end)>>1;
build(ls[node],begin,mid);
build(rs[node],mid+1,end);
sum[node]=sum[ls[node]]+sum[rs[node]];
}
void update(int &node,int begin,int end,int left,int right,int val)
{
sum[p]=sum[node];ls[p]=ls[node];rs[p]=rs[node];
pos[p]=pos[node];
node=p;p++;
sum[node]+=1LL*val*(right-left+1);
if(left==begin&&end==right)
{
pos[node]+=val;
return;
}
int mid=(begin+end)>>1;
if(right<=mid) update(ls[node],begin,mid,left,right,val);
else if(left>mid) update(rs[node],mid+1,end,left,right,val);
else
{
update(ls[node],begin,mid,left,mid,val);
update(rs[node],mid+1,end,mid+1,right,val);
}
}
LL query(int node,int begin,int end,int left,int right)
{
if(left<=begin&&end<=right) return sum[node];
LL ret=1LL*pos[node]*(right-left+1);
int mid=(begin+end)>>1;
if(right<=mid) ret+=query(ls[node],begin,mid,left,right);
else if(left>mid) ret+=query(rs[node],mid+1,end,left,right);
else
{
ret+=(query(ls[node],begin,mid,left,mid)+query(rs[node],mid+1,end,mid+1,right)); }
return ret;
}
int main()
{
char x;
while(scanf("%d%d",&n,&m)!=EOF)
{
t=0;
p=0;
build(rt[0],1,n);
int l,r,d,time; LL ans;
for(int i=1;i<=m;i++)
{
cin>>x;
if(x=='C')
{
scanf("%d%d%d",&l,&r,&d);
update(rt[++t]=rt[t-1],1,n,l,r,d);
}
else if(x=='Q')
{
scanf("%d%d",&l,&r);
ans=query(rt[t],1,n,l,r);
printf("%lld\n",ans);
}
else if(x=='H')
{
scanf("%d%d%d",&l,&r,&time);
ans=query(rt[time],1,n,l,r);
printf("%lld\n",ans);
}
else
{
scanf("%d",&time);
t=time;
} }
}
return 0;
}

 

HDU 4348 To the moon(可持久化线段树)的更多相关文章

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

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

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

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

  3. [hdu 4348]区间修改区间查询可持久化线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 一开始把lazy标记给push_down了,后来发现这样会让持久化变乱,然后想到不用push_d ...

  4. 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 ...

  5. HDU 5919 Sequence II(可持久化线段树)

    [题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...

  6. hdu4348 - To the moon 可持久化线段树 区间修改 离线处理

    法一:暴力! 让干什么就干什么,那么久需要可持久化线段树了. 但是空间好紧.怎么破? 不down标记好了! 每个点维护sum和add两个信息,sum是这段真实的和,add是这段整体加了多少,如果这段区 ...

  7. hdu4348 To the moon (可持久化线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 题目大意:给定含有n个数的序列,有以下四种操作 1.C l r d:表示对区间[l,r]中的数加 ...

  8. hdu 5919 Sequence II (可持久化线段树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...

  9. HDU 5820 (可持久化线段树)

    Problem Lights (HDU 5820) 题目大意 在一个大小为50000*50000的矩形中,有n个路灯.(n<=500000) 询问是否每一对路灯之间存在一条道路,使得长度为|x1 ...

随机推荐

  1. 宏里面的(void)0

    在<c标准库>实现assert.h中有一个语句: #define assert(test) ((test)?(void)0 : _Assert(__FILE__":"_ ...

  2. python生成器,函数,数组

    1.什么是生成器用一个比喻来形容,工厂中生产保龄球的流水线,机器每次只生产一个保龄球,下次继续生产下一个,直到停止(原料不足,停止供电等条件)为止.机器就是我们的生成器. 2.使用示例在python中 ...

  3. 每日英语:Do Bicycle Helmet Laws Really Make Riders Safer?

    Typically in transportation — and most social arenas, for that matter — laws promoting safety precau ...

  4. iOS应用代码段瘦身办法

    iOS应用代码段瘦身办法 大型app应对苹果官方代码段大小限制的小伎俩… 背景 苹果官方文档 对二进制 __TEXT 段大小有限制: 代码实在瘦不下去怎么办? 解决方案 利用 rename_secti ...

  5. iOS开发Swift篇—(七)函数

    iOS开发Swift篇—(七)函数 一.函数的定义 (1)函数的定义格式 1 func 函数名(形参列表) -> 返回值类型 { 2 // 函数体... 3 4 } (2)形参列表的格式 形参名 ...

  6. saveFile()方法

    saveFile的原理就是将流写入到需要写入的文件,通过可以用“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行存储,举例:public vo ...

  7. 卡特兰数 catalan number

    作者:阿凡卢 出处:http://www.cnblogs.com/luxiaoxun/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留 ...

  8. [Linux]gcc/libc/glibc

    转自:http://blog.csdn.net/weiwangchao_/article/details/16989713 1.gcc(gnu collect compiler)是一组编译工具的总称. ...

  9. SpringMVC 之类型转换Converter 源代码分析

    SpringMVC 之类型转换Converter 源代码分析 最近研究SpringMVC的类型转换器,在以往我们需要 SpringMVC 为我们自动进行类型转换的时候都是用的PropertyEdito ...

  10. 面向对象设计原则三:里氏替换原则(LSP)

    里氏替换原则(LSP)定义:在任何父类出现的地方都可以用它的子类类替换,且不影响功能.解释说明:其实LSP是对开闭原则的一个扩展,在OO思想中,我们知道对象是由一系列的状态和行为组成的,里氏替换原则说 ...