【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 ...
随机推荐
- windows 7 下 BCGControlBar 的安装破解
一定要以管理员权限启动,否则没有注册码输入框,不能破解,折腾我好几遍 谨记
- HDU -- 4496
D-City Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- C++中的虚函数总结
一.什么是虚函数.纯虚函数.抽象基类 虚函数:在某基类中声明为 virtual 并在一个或多个派生类中被重新定 义的成员函数. 纯虚函数:是一种特殊的虚函数,使用virtual关键字,并且在其后面加上 ...
- Two kinds of Quaternion SlerpImp (Unity)
using UnityEngine;using System.Collections; public class SlerpImp{ static float Dot(Quaternion a, Qu ...
- Hibernate(一)JDBC简介
在了解Hibernate之前,我们先回顾一下传统JDBC访问数据库的相关内容.重点在于分析JDBC访问存在哪些 缺陷,这些缺陷在Hibernate中是如何思考和解决的? JDBC主要对象 DriveM ...
- Pattern()和Matcher() 用法
1.简介: java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 它包括两个类:Pattern和Matcher . Pattern: 一个Pattern是一个 ...
- 【转】jQuery列表拖动排列-jquery list dragsort插件参数和使用方法
转自:http://www.itokit.com/2014/0820/75058.html 我们在编辑页面元素排序的时候,我推荐使用jquery插件:dragsort. dragsort官网地址:ht ...
- ELK监控系统nginx / mysql慢日志
ELK监控系统nginx / mysql慢日志 elasticsearch logstash kibana ELK监控系统nginx日志 1.环境准备 centos6.8_64 mini IP:192 ...
- nodejs端口被占用。
I had the same issue. I ran: $ ps aux | grep node to get the process id, then: $ sudo kill -9 follow ...
- 【javascript基础知识】javascript中的转义序列和特殊数值常量
javascript的转义序列 \0 NUL字符(\u0000) \b 退格符(\u0008) \t 水平制表符(\u0009) \n 换行符(\u000A) \v 垂直制表符(\u000B) \f ...