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. Center os FTP配置

    原文:http://www.aicoffees.com/itshare/412261137.html

  2. IOS开发/iphone开发多线程

    有时候可能有很多功能要同时实现,例如每隔多长时间就会检测程序网络连接,又或者有时候需要从服务器下载一个不小的文件,如果用单线程几乎是不可想的事情,程序将会卡的无法使用,用到多线程和不用多线程,给用户的 ...

  3. angular-ngSanitize模块-linky过滤器详解

    本篇主要讲解angular中的linky这个过滤器.此过滤器依赖于ngSanitize模块. linky能找出文本中的链接,然后把它转换成html链接.什么意思,就是说,一段文本里有一个链接,但是这个 ...

  4. linux下可以禁用的一些服务

    linux下多软件/多脚本之间的配合: 包括做好 “实体”和“配置”两个方面的事情 “实体”是指实实在在的脚本文件,服务脚本: “配置”是指其他与之交互的.协同工作的软件.脚本,要进行适当的配置,告知 ...

  5. javascript仿天猫加入购物车动画效果

    javascript仿天猫加入购物车动画效果   注意:首先需要声明的是:代码原思路不是我写的,是在网上找的这种效果,自己使用代码封装了下而已:代码中都有注释,我们最主要的是理解抛物线的思路及在工作中 ...

  6. ubuntu 14.04 安装mysql server初级教程

    序,mysql数据库是开源的,被大多数企业所使用 目录 一.apt-get install 软件安装原理剖析二.安装mysql server三.配置和管理msyql 一.apt-get install ...

  7. 如何更改firefox默认搜索引擎?一步搞定!

    由于开发设计的需要,ytkah平时习惯使用firefox作为默认浏览器,火狐浏览器可添加的扩展功能比较,比如firebug.nofollow.seoquake等,还有比较友好的功能就是选中关键词拖动直 ...

  8. 多线程 or 多进程 (转强力推荐)

    在Unix上编程采用多线程还是多进程的争执由来已久,这种争执最常见到在C/S通讯中服务端并发技术 的选型上,比如WEB服务器技术中,Apache是采用多进程的(perfork模式,每客户连接对应一个进 ...

  9. Bridge 使用

  10. Recover Rotated Sorted Array

    Given a rotated sorted array, recover it to sorted array in-place. Clarification What is rotated arr ...