HDU 4348 To the moon 可持久化线段树
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 <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 可持久化线段树的更多相关文章
- HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和
To the moonTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...
- [hdu 4348]区间修改区间查询可持久化线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 一开始把lazy标记给push_down了,后来发现这样会让持久化变乱,然后想到不用push_d ...
- 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 ...
- HDU 5919 Sequence II(可持久化线段树)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...
- hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
法一:暴力! 让干什么就干什么,那么久需要可持久化线段树了. 但是空间好紧.怎么破? 不down标记好了! 每个点维护sum和add两个信息,sum是这段真实的和,add是这段整体加了多少,如果这段区 ...
- hdu4348 To the moon (可持久化线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 题目大意:给定含有n个数的序列,有以下四种操作 1.C l r d:表示对区间[l,r]中的数加 ...
- hdu 5919 Sequence II (可持久化线段树)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...
- HDU 4348 To the moon(可持久化线段树)
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- HDU 5820 (可持久化线段树)
Problem Lights (HDU 5820) 题目大意 在一个大小为50000*50000的矩形中,有n个路灯.(n<=500000) 询问是否每一对路灯之间存在一条道路,使得长度为|x1 ...
随机推荐
- webservice原理
webservice的工作原理 WebService的主要目标是跨平台的可互操作性.为了达到这一目标,WebService完全基于XML(可扩展标记语言).XSD(XMLSchema)等独立于平台 ...
- 【原创】express3.4.8源码解析之路由中间件
前言 注意:旧文章转成markdown格式. 跟大家聊一个中间件,叫做路由中间件,它并非是connect中内置的中间件,而是在express中集成进去的. 显而易见,该中间件的用途就是 ------ ...
- javascript模板插件amaze.js
摘要: 最近在开发项目时,异步接口需要前端渲染数据,js拼接太低级,必然要用模板插件.之前用过基于jQuery的和juicer等插件,考虑到以后公司项目上的统一,移动端和pc端上的统一,以及可维护性, ...
- 繁华模拟赛 Vincent的城堡
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- Python webpy微信公众号开发之 回复图文消息
新建图文回复模板reply_pictext.xml: $def with (toUser,fromUser,createTime,title1,description1,picurl1,url1)&l ...
- [Effective JavaScript 笔记]第34条:在原型中存储方法
js中完全有可能不借助原型进行编程.不用在其原型中定义任何的方法. 创建对象 构造函数法 所有属性和方法都在构造函数中定义 function User(name,pwd){ this.name=nam ...
- HLG1744组合数学问题与lucas定理运用
The figure below shows Pascal's Triangle: Baby H divides Pascal's Triangle into some Diagonals, like ...
- TCP/IP详解学习笔记(5)-- ICMP:internet 控制报文协议
1.概述 ICMP是(Internet Control Message Protocol)Internet控制报文协议.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制 ...
- iOS xib中TableView创建的2种模式
在xcode 5.0中 用xib编辑tableview有2种模式,见下图 其中,dynamic prototype 动态原型 表示tableview会询问它指定的 data source获取数据,如果 ...
- (转)SQL Server 的事务和锁(一)
SQL Server 的事务和锁(一) 最近在项目中进行压力测试遇到了数据库的死锁问题,简言之,如下的代码在 SERIALIZABLE 隔离级别造成了死锁: 1 2 3 4 5 6 7 8 9 1 ...