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

 
Author
HIT
Source
【分析】
简单题,唯一要注意的是内存大小...
写指针各种被卡....无奈最后写数组...
看到有人用可持久化树状数组做...我开始算了一下好像会超内存,然后就没写了。。Orzzz
 /*
唐代高蟾
《金陵晚望》 曾伴浮云归晚翠,犹陪落日泛秋声。
世间无限丹青手,一片伤心画不成。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int MAXM = + ;
const int INF = ;
const int SIZE = ;
const int maxnode = 0x7fffffff + ;
using namespace std;
typedef long long ll;
int lson[MAXN], rson[MAXN];
int data[MAXN], add[MAXN];
ll sum[MAXN];
int tot; int insert(int t, int ll, int rr, int d, int l, int r){
int now = ++tot;
lson[now] = lson[t];
rson[now] = rson[t];
add[now] = add[t];
sum[now] = sum[t];
sum[now] += (long long)(d * (rr - ll + ));
if(ll == l && rr == r){
add[now] += d;
return now;
}
int mid = (l + r)>>;
if(rr <= mid) lson[now] = insert(lson[t], ll, rr, d, l, mid);
else if(ll > mid) rson[now] = insert(rson[t], ll, rr, d, mid + , r);
else{
lson[now] = insert(lson[t], ll, mid, d, l, mid);
rson[now] = insert(rson[t], mid + , rr, d, mid + , r);
}
return now;
}
//在t的根内查询[ll, rr]区间的值
long long query(int t, int ll, int rr, int l, int r){
long long Ans = (long long)(add[t] * (rr - ll + ));
if (ll == l && rr == r) return sum[t];
int mid = (l + r)>>;
if (rr <= mid) Ans += query(lson[t], ll, rr, l, mid);
else if (ll > mid) Ans += query(rson[t], ll, rr, mid + , r);
else {
Ans += query(lson[t], ll, mid, l, mid);
Ans += query(rson[t], mid + , rr, mid + , r);
}
return Ans;
}
int build(int ll, int rr){
int now = ++tot;
add[now] = ;
if (ll == rr){
scanf("%lld", &sum[now]);
lson[now] = rson[now] = ;
return now;
}
int mid = (ll + rr)>>;
lson[now] = build(ll, mid);
rson[now] = build(mid + , rr);
sum[now] = sum[lson[now]] + sum[rson[now]];
return now;
} int n ,m;
void work(){
tot = ;
data[] = build(,n);
int now = ;
for (int i = ; i <= m; i++){
char str[];
scanf("%s", str);
if (str[] == 'Q'){
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", query(data[now], l, r, , n));
}else if(str[] == 'C'){
int l, r, d;
scanf("%d%d%d", &l, &r, &d);
data[now+] = insert(data[now], l, r, d, , n);
now++;
}else if(str[] == 'H'){
int l, r, t;
scanf("%d%d%d", &l, &r, &t);
printf("%lld\n", query(data[t], l, r, , n));
}else scanf("%d", &now);
}
printf("\n");
} int main(){ while (scanf("%d%d", &n, &m) != EOF){
//scanf("%d%d", &n, &m);
work();
}
return ;
}

【HDU4348】【主席树】To the moon的更多相关文章

  1. [HDU4348]To the moon(主席树+标记永久化)

    学可持久化treap的时候才发现自己竟然没写过需要标记下传的主席树,然而现在发现大部分操作都可以标记永久化,下传会增大占用空间. 这题一种写法是和普通的线段树一样标记下传,注意所有修改操作(包括put ...

  2. hdu4348 To the moon (主席树 || 离线线段树)

    Problem Description Background To The Moon is a independent game released in November 2011, it is a ...

  3. HDU4348 To the moon (主席树)

    标记永久化,除非想\(MLE\) 忽然感到主席树不过是函数式的树套树 #include <iostream> #include <cstdio> #include <cs ...

  4. To the moon HDU - 4348 (主席树,区间修改)

    Background To The Moon is a independent game released in November 2011, it is a role-playing adventu ...

  5. 【主席树】【bzoj2161】[hdu4348]

    #include<cstdio> #include<algorithm> #include<cstring> #define N 400000 using name ...

  6. [主席树]HDOJ4348 To the moon

    题意:n个数, m个操作 1. C l r d  给[l, r]区间的每个数加上d2. Q l r:   查询[l, r]区间的和3. H l r t: 查询第t个操作时[l, r]区间的和4. B ...

  7. HDU 4348 To the moon(主席树 区间更新)题解

    题意: 给一个数组A[1] ~ A[n],有4种操作: Q l r询问l r区间和 C l r v给l r区间每个数加v H l r t询问第t步操作的时候l r区间和 B t返回到第t步操作 思路: ...

  8. hdu 4348 To the moon (主席树 区间更新)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4348 题意: 4种操作: C l r c   区间[l,r]加c,时间+1 Q l r    询问当前时 ...

  9. hdu 4348 To the moon (主席树区间更新)

    传送门 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q l r:查询当前时间戳区间[l,r]中所有数的和 . (3)H ...

  10. hdu 4348 To the moon (主席树)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...

随机推荐

  1. 大型系统OA--技术

    OA办公自动化系统--技术 1.由于涉及企业的流程控制与规则管理.所以系统对于规范要求的严谨性,导致在小型开发组在进行开发时必须基于成熟的技术架构.开源的lemon OA,采用了如下技术细节: mys ...

  2. 【转】在VMware中安装OS X Yosemite

    原文网址:http://blog.gaohaobo.com/229.html OS X(前称:Mac OS X)操作系统是由苹果公司(Apple Inc.)为其Mac系列产品开发的.基于Unix的专属 ...

  3. 【转】Android ROM研究---Android build system增加模块

    原文网址:http://hualang.iteye.com/blog/1141315 Android build system就是编译系统的意思 在我们需要向自己编译的源代码中增加模块的时候,需要一些 ...

  4. (转载)[MySQL技巧]INSERT INTO… ON DUPLICATE KEY UPDATE

    (转载)http://blog.zol.com.cn/2299/article_2298921.html MySQL 自4.1版以后开始支持INSERT … ON DUPLICATE KEY UPDA ...

  5. web.xml 详解contextConfigLocation 转

    spring的应用初始化流程一直没有搞明白,刚刚又碰到了相关的问题.决定得好好看看这个流程.我们在开发spring的项目当中基本上都会在web.xml通过: <context-param> ...

  6. jemalloc/jemalloc.h: No such file or directory

    Redis 2.6.9 安装报错,提示: zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directoryzmalloc.h ...

  7. vim setting

    django_百度搜索 最近合并代码,发现文件缩进经常不一致,请大家把以下配置放到自己主目录下.vimrc文件中.   set tabstop=4   set shiftwidth=4   set e ...

  8. Java日志记录的5条规则

    日志记录是在软件开发过程中常常需要考虑的关键因素. 当产品运行出错时,日志文件通常是我们进行错误分析的首要选择. 而且,在很多情况下,它们是我们手上唯一可以用来查明发生状况和问题根本原因的信息. 可见 ...

  9. centos下apache thrift的安装

    参考:http://running.iteye.com/blog/1983463  thrift-0.9.0安装 最好切换到root用户操作,避免不必要的麻烦. 进行例子程序tutorial目录下,通 ...

  10. poj 2253 Frogger【最小生成树变形】【kruskal】

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30427   Accepted: 9806 Descript ...