To the moon

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

 
 
题意:
   
  给你一个数组,让你维护,m次操作
  询问当前时刻一个区间的和
  询问在t时刻的一个区间的和
  回到t时刻
  时间+1,在此时刻+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 可持久化线段树的更多相关文章

  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]区间修改区间查询可持久化线段树

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

  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 5919 Sequence II(可持久化线段树)

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

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

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

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

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

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

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

  8. HDU 4348 To the moon(可持久化线段树)

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

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

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

随机推荐

  1. 实现SVN与WEB同步解决方案(转)

    实现SVN与WEB同步解决方案 1)设置WEB服务器根目录为/www/default 2)checkout一份SVN svn co svn://localhost/oplinux /www/defau ...

  2. CSS3 transforms 3D翻开

    R T L B   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  3. kindle paperwhite折腾记

    在亚马逊官网上买了一个kindle paperwhite 一代(849元) , 打算再买个皮套, 淘宝店  http://detail.tmall.com/item.htm?spm=a230r.1.1 ...

  4. sql重复记录查询

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select  peopleId  fro ...

  5. TCP协议漏洞影响大量Linux设备

    导读 本周三在得州奥斯丁举行的 USENIX 安全研讨会上,加州大学河滨分校研究生 Yue Cao 将报告一个严重的TCP协议边信道漏洞(PDF),该漏洞允许攻击者远程劫持任意两主机之间的会话.该漏洞 ...

  6. ssh面试题

    ssh面试题  创建时间: 2015-8-12 22:37 来源: http://wenku.baidu.com/link?url=cw1B46f98hAde0kmr3J-wv7PpklZJRmf6I ...

  7. encode与decode,unicode与中文乱码的问题

    encode是指将unicode字符编码成其他字符集的字符,如utf-8,ascii等: 而decode是指将其他字符编码,如utf-8转换成unicode编码. encode是指将人类用的语言(字符 ...

  8. 【OpenStack】OpenStack系列12之OpenStack自动化测试详解

    参考文档: https://github.com/yongluo2013/osf-openstack-training/blob/master/installation/How-to-setup-op ...

  9. css调用外部样式和css样式说明剧中显示

    <title>边走边乔</title><link href="css/style.css" rel="stylesheet" ty ...

  10. 【转】Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...