【HDU4348】【主席树】To the moon
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.
A1 A2 ... An
... (here following the m operations. )
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
55
9
15
0
1
/*
唐代高蟾
《金陵晚望》 曾伴浮云归晚翠,犹陪落日泛秋声。
世间无限丹青手,一片伤心画不成。
*/
#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的更多相关文章
- [HDU4348]To the moon(主席树+标记永久化)
学可持久化treap的时候才发现自己竟然没写过需要标记下传的主席树,然而现在发现大部分操作都可以标记永久化,下传会增大占用空间. 这题一种写法是和普通的线段树一样标记下传,注意所有修改操作(包括put ...
- hdu4348 To the moon (主席树 || 离线线段树)
Problem Description Background To The Moon is a independent game released in November 2011, it is a ...
- HDU4348 To the moon (主席树)
标记永久化,除非想\(MLE\) 忽然感到主席树不过是函数式的树套树 #include <iostream> #include <cstdio> #include <cs ...
- To the moon HDU - 4348 (主席树,区间修改)
Background To The Moon is a independent game released in November 2011, it is a role-playing adventu ...
- 【主席树】【bzoj2161】[hdu4348]
#include<cstdio> #include<algorithm> #include<cstring> #define N 400000 using name ...
- [主席树]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 ...
- 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步操作 思路: ...
- 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 询问当前时 ...
- hdu 4348 To the moon (主席树区间更新)
传送门 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q l r:查询当前时间戳区间[l,r]中所有数的和 . (3)H ...
- hdu 4348 To the moon (主席树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...
随机推荐
- Linq中小心使用IndexOf
我们平常在做字符串的模糊查询时,有可能会用到下面的类似LINQ写法: string.IsNullOrEmpty(_SN) ? true : a.SN.IndexOf(_SN) != -1 这条 ...
- leetcode 栈 括号匹配
https://oj.leetcode.com/problems/valid-parentheses/ 遇到左括号入栈,遇到右括号出栈找匹配,为空或不匹配为空, public class Soluti ...
- leetcode Longest Common Prefix 多个字符串的最长字串
public class Solution { public String get(String a,String b) { if(a==""||b=="") ...
- 【索引】UML学习笔记
行为图 交互图 交互概览图 时间图 顺序图 通信图 活动图 状态及图 用例图 结构图 包图 类图 对象图 组件图 部署图 组合结构图
- JavaScript高级程序设计9.pdf
Number是数字值对应的引用类型 var numberObject=new Number(10); Number也重写了valueof().toLocaleString().和toString()方 ...
- [转载]jquery的extend和fn.extend
jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 为扩展j ...
- Apache服务器部署多个进程
本文以xampp安装的apache服务为例进行介绍 1.复制配置文件目录,复制一个新的配置文件目录conf2,区别于原来的配置文件目录conf
- ELK Packetbeat 部署指南(15th)
原文链接:http://www.ttlsa.com/elk/elk-packetbeat-deployment-guide/ Packetbeat 是一个实时网络数据包分析工具,与elasticsea ...
- BZOJ1176: [Balkan2007]Mokia CDQ分治
最近很不对啊=w= 写程序全是bug啊 ans数组开小了竟然一直不知道,小数据没问题大数据拍不过,交上去RE 蛋疼半天 这个主要把每次询问拆成3个询问. #include<cstdio> ...
- java中获取系统属性以及环境变量
java中获取系统属性以及环境变量 System.getEnv()和System.getProperties()的差别 从概念上讲,系统属性 和环境变量 都是名称与值之间的映射.两种机制都能用来将用户 ...