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. 新浪微博客户端(5)-自定义UISearchBar

    iOS自带的UISearchBar有很多限制,我们可以使用UITextField做出一个类似于SearchBar的效果. //===================================== ...

  2. 下面将详细说明useradd与usermod 的参数及用法!

    下面将详细说明useradd与usermod 的参数及用法! 说到这里要另外两句,关于linux下口令相关的文件存放位置说明/usr/bin/passwd 包含 passwd 命令. /etc/pas ...

  3. 【原创】ui.router源码解析

    Angular系列文章之angular路由 路由(route),几乎所有的MVC(VM)框架都应该具有的特性,因为它是前端构建单页面应用(SPA)必不可少的组成部分. 那么,对于angular而言,它 ...

  4. 如何在WordPress中使用七牛云存储

    序:七牛云存储可以方便的将网站的图片等数据镜像到七牛云存储的空间,直接从云端将数据返回给用户.这样可以大大节省网站的空间,提升网站的访问速度. 真正显示一键实现WordPress博客静态文件CDN加速 ...

  5. C#中跨线程访问控件问题解决方案

    net 原则上禁止跨线程访问控件,因为这样可能造成错误的发生,推荐的解决方法是采用代理,用代理方法来间接操作不是同一线程创建的控件. 第二种方法是禁止编译器对跨线程访问作检查,可以实现访问,但是出不出 ...

  6. tolua.cast的实用方法

    local name = (tolua.cast(sender, "ccui.Button")):getTitleText()

  7. 让Linux下的打印机hp1020、hp p1008自动加载固件

    前言: 前段时间,处理公司打印机服务器Linux化工作.遇到问题如下:hp1020.hp1008断电后不能继续打印.而其他打印机在连接Linux打印机的情况下,断电后也能正常打印. 鉴于此情况,我搜寻 ...

  8. django动态表格总结

    应用场景: A与B之间存在一对多关系. CBV实现方案: CreateView/UpdateView + inlineformset + jquery 具体: view方面:重写post/get方法, ...

  9. android.content.ActivityNotFoundException: Unable to find explicit activity class have you declared this activity in your AndroidManifest.xml?

    在整合PullToRefresh的时候出现如下异常 10-22 23:20:01.826 32331-32331/com.example.news.andoridnewsapp E/AndroidRu ...

  10. iOS constraint被应用于view上的时间

    在viewdidload时,constraint是没有被应用的,之后在layoutSubviews时,系统应用了constraint.但是我感觉在viewWillLayoutSubviews函数时就已 ...