HDU 4348 To the moon 可持久化线段树
To the moon
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.
A1 A2 ... An
... (here following the m operations. )
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
55
9
15
0
1
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5e6+, M = 1e3+, mod = , inf = 1e9+;
typedef long long ll; int n,m;
int l[N],r[N],root[N],add[N],tot = ;
ll sum[N];
int build(int s,int t) {
int now = ++tot;
add[now] = ;
if(s==t) {
scanf("%lld",&sum[now]);
l[now] = r[now] = ;
return now;
}
int mid = (s+t)>>;
l[now] = build(s,mid);
r[now] = build(mid+,t);
sum[now] = sum[l[now]]+sum[r[now]];
return now;
}
//查询在以t为根内[ll,rr]区间的值
ll query(int k,int lll,int rr,int s,int t) {
ll ans = (add[k]*(rr-lll+));
if(lll==s&&rr==t) return sum[k];
int mid = (s+t)>>;
if(rr<=mid) ans+=query(l[k],lll,rr,s,mid);
else if(lll>mid) ans+=query(r[k],lll,rr,mid+,t);
else {
ans+=query(l[k],lll,mid,s,mid);
ans+=query(r[k],mid+,rr,mid+,t);
}
return ans;
}
int update(int k,int lll,int rr,int d,int s,int t) {
int now = ++tot;
l[now] = l[k];
r[now] = r[k];
add[now] = add[k];
sum[now] = sum[k];
sum[now]+=(ll) (d*(rr-lll+));
if(lll==s&&rr==t) {
add[now]+=d;
return now;
}
int mid = (s+t)>>;
if(rr<=mid) l[now] = update(l[k],lll,rr,d,s,mid);
else if(lll>mid) r[now] = update(r[k],lll,rr,d,mid+,t);
else {
l[now] = update(l[k],lll,mid,d,s,mid);
r[now] = update(r[k],mid+,rr,d,mid+,t);
}
return now;
}
void solve() {
tot = ;
root[] = build(,n);
int now = ;
for(int i=;i<=m;i++) {
char ch[];
scanf("%s",ch);
if(ch[]=='Q') {
int a,b;
scanf("%d%d",&a,&b);
printf("%lld\n",query(root[now],a,b,,n));
}
else if(ch[]=='C') {
int a,b,d;
scanf("%d%d%d",&a,&b,&d);
root[now+] = update(root[now],a,b,d,,n);
now++;
}
else if(ch[]=='H') {
int a,b,t;
scanf("%d%d%d",&a,&b,&t);
printf("%lld\n",query(root[t],a,b,,n));
}
else scanf("%d",&now);
}
}
int main() {
while(scanf("%d%d",&n,&m)!=EOF) {
solve();
}
return ;
}
HDU 4348 To the moon 可持久化线段树的更多相关文章
- 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]区间修改区间查询可持久化线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 一开始把lazy标记给push_down了,后来发现这样会让持久化变乱,然后想到不用push_d ...
- 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 5919 Sequence II(可持久化线段树)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...
- hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
法一:暴力! 让干什么就干什么,那么久需要可持久化线段树了. 但是空间好紧.怎么破? 不down标记好了! 每个点维护sum和add两个信息,sum是这段真实的和,add是这段整体加了多少,如果这段区 ...
- hdu4348 To the moon (可持久化线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 题目大意:给定含有n个数的序列,有以下四种操作 1.C l r d:表示对区间[l,r]中的数加 ...
- hdu 5919 Sequence II (可持久化线段树)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...
- HDU 4348 To the moon(可持久化线段树)
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- HDU 5820 (可持久化线段树)
Problem Lights (HDU 5820) 题目大意 在一个大小为50000*50000的矩形中,有n个路灯.(n<=500000) 询问是否每一对路灯之间存在一条道路,使得长度为|x1 ...
随机推荐
- Brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3624 Accepted: 1879 Descript ...
- MSSQL复习
1.用户角色: 登录名就相当于一个用户 角色相当于把你的操作权限分组了 2.数据系统结构(略) 网络连接接口 关系引擎 存储引擎 内存 3.数据库的结构 数据库 架构 对象(在Sql server中将 ...
- [POJ1007]DNA Sorting
[POJ1007]DNA Sorting 试题描述 One measure of ``unsortedness'' in a sequence is the number of pairs of en ...
- win7 64位系统HP LaserJet P1008 / HP LaserJet P1008 P1007 驱动安装成功,但无法打印的原因
HP LaserJet P1008 打印机驱动安装成功,但是无法打印相关文档的原因是: 1.打印机是水货,惠普中国提供的驱动和该打印机不符合.显示的应该是HP LaserJet Professiona ...
- 对比WDCP面板与AMH面板的区别与选择
转载: http://www.laozuo.org/2760.html | 老左博客 随着VPS主机的性价比提高(其实就是降价)我们很多站长会越来越多的选择使用VPS搭建网站或者运营一些项目,相比较而 ...
- tornado--之cookie自定义(还有session)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhAAAAHzCAIAAAD+WrNvAAAgAElEQVR4nOy993cTV7/vf/6qu865ob
- django-jinjia 集成
现成包可以参考这里: http://niwibe.github.io/django-jinja/ Requirements Python 2.7, 3.3 or 3.4 Django 1.4, 1. ...
- 详解mysql int类型的长度值问题
我的朋友海滨问我mysql在建表的时候int类型后的长度代表什么? 是该列允许存储值的最大宽度吗? 为什么我设置成int(1), 也一样能存10,100,1000呢. 当时我虽然知道int(1),这个 ...
- 创建一个最简单的Linux随机启动服务
转自: http://xiaoxia.org/2011/11/15/create-a-simple-linux-daemon/
- 【云计算】Kubernetes、Marathon等框架需要解决什么样的问题?
闲谈Kubernetes 的主要特性和经验分享 Capitalonline全球云主机.全球私有网络,免费试用进行时 » 主要介绍 Kubernetes 的主要特性和一些经验.先从整体上 ...