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. 【转】Eclipse使用git最简易流程

    原文网址:http://www.cnblogs.com/ZhangWanFan/p/3993733.html git有诸多好处,网上都说的很清楚了,在这里我不再赘述.对于我来说,私下里想做一些项目,而 ...

  2. A configuration error occurred during startup. Please verify the preference field with the prompt: Cannot connect to vm

    1.报错图 解决方法: Window->Preferences->MyEclipse Enterprice Workbench->Servers->Tomcat->选择你 ...

  3. Delphi 6 Web Services初步评估之三(转)

    Delphi 6 Web Services初步评估之三(转)   Delphi 6 Web Services初步评估之三(转)★ 测试总体印象:在整个测试中,对Delphi 6创建的Web Servi ...

  4. Java同步

    同步:★★★★★ 好处:解决了线程安全问题. 弊端:相对降低性能,因为判断锁需要消耗资源,产生了死锁. 定义同步是有前提的: 1,必须要有两个或者两个以上的线程,才需要同步. 2,多个线程必须保证使用 ...

  5. Android学习笔记(三)Application类简介

    每次运行APP时,Application类都保持实例化状态.与Activity不同,配置改变不会导致应用程序重启.通过继承Application类,可以完成一下3项工作: · 对Android运行时广 ...

  6. Skype的故事:几乎所有风投都想投 犯罪分子洗钱必备

    Skype的故事:几乎所有风投都想投 犯罪分子洗钱必备 转载自: http://news.chinaventure.com.cn/11/7/1381032922.shtml 今年是 Skype 网络电 ...

  7. android 48 广播

    系统开始重启会发送开机重启广播,电量低的时候会发送电量低的广播,广播注册有2种:系统说明文件xml注册和Java代码注册,前者是静态注册(全局注册)后者是动态注册(依赖于当时组建,组件销毁就收不到广播 ...

  8. limit-进程句柄限制

    在Linux下面部署应用的时候,有时候会遇上Socket/File: Can’t open so many files的问题,比如还有Squid做代理,当文件打开数到900多时速能就非常快的下降,有可 ...

  9. 计算机体系结构-内存调优IPC OOMK

    man ipc [root@server1 proc]# man ipcIPC(2)                     Linux Programmer’s Manual             ...

  10. tcmalloc源码剖析的资料

    1. https://seanhn.wordpress.com/2011/04/14/exploit-necromancy-in-tcmalloc-reviving-the-4-to-n-byte-o ...