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 Submission(s): 8372 Accepted Submission(s): 1986
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
长度为n的整数序列,支持四个操作
1:Q l r 输出区间[l,r]的总和
2:C l r x 区间[l,r]的每个值都增加x,此时时间增加1
3:H l r t 询问在t时刻区间[l,r]的总和
4:B t 时间回到t
延时标记如果下传的话,内存不够,所以要节省内存,按照正常的区间更新的操作,标记下传,就新开节点,这道题中,我们不新开节点,用一个标记来记录当前节点的整段区间被累加了多少,当询问的时候,从根节点走到目标节点的过程中累加所经过节点上的标记值就可以。
所以就不能用以前的查询写法,if(L<=m) ... if(R> m) ... ,就需要换一种写法,就是这里:
//if(L<=m) ret+=query(ls[rt],L,R,lson,c);
//if(R> m) ret+=query(rs[rt],L,R,rson,c);
if(R<=m) ret+=query(ls[rt],L,R,lson);
else if(L> m) ret+=query(rs[rt],L,R,rson);
else ret+=query(ls[rt],L,m,lson)+query(rs[rt],m+,R,rson);
return ret;
这道题真的自闭,wa了一晚上,最后发现,延时标记累加的时候,lazy[rt]*(R-L+1)写成了lazy[rt]*(r-l+1)。。。
其他的就是时间回到t的时候,直接将时间的值更新就可以。
代码:
//HDU 4348.To the moon-可持久化线段树-区间更新,延时标记不下传,优化空间
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define lson l,m
#define rson m+1,r int rt[maxn],ls[maxn<<],rs[maxn<<],sz;
ll sum[maxn<<],lazy[maxn<<]; void pushup(int rt,int m)
{
sum[rt]=sum[ls[rt]]+sum[rs[rt]]+(ll)lazy[rt]*m;
} void build(int &rt,int l,int r)
{
rt=++sz;lazy[rt]=;
if(l==r){
scanf("%lld",&sum[rt]);
return ;
} int m=(l+r)>>;
build(ls[rt],lson);
build(rs[rt],rson);
pushup(rt,r-l+);
} void update(int pre,int &rt,int L,int R,int l,int r,ll c)
{
rt=++sz;lazy[rt]=lazy[pre];sum[rt]=sum[pre];
ls[rt]=ls[pre];rs[rt]=rs[pre];
if(L<=l&&r<=R){
lazy[rt]+=c;
sum[rt]+=(ll)c*(r-l+);
return ;
} int m=(l+r)>>;
if(L<=m) update(ls[pre],ls[rt],L,R,lson,c);
if(R> m) update(rs[pre],rs[rt],L,R,rson,c);
pushup(rt,r-l+);
} ll query(int rt,int L,int R,int l,int r)
{
if(L<=l&&r<=R){
return sum[rt];
} //ll ret=(ll)lazy[rt]*(r-l+1);
ll ret=(ll)lazy[rt]*(R-L+);
int m=(l+r)>>;
//if(L<=m) ret+=query(ls[rt],L,R,lson,c);
//if(R> m) ret+=query(rs[rt],L,R,rson,c);
if(R<=m) ret+=query(ls[rt],L,R,lson);
else if(L> m) ret+=query(rs[rt],L,R,rson);
else ret+=query(ls[rt],L,m,lson)+query(rs[rt],m+,R,rson);
return ret;
} char op[]; int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)){
sz=;memset(rt,,sizeof(rt));
build(rt[],,n);
int tag=;
for(int i=;i<=m;i++){
scanf("%s",op);
if(op[]=='C'){
int l,r;ll c;
scanf("%d%d%lld",&l,&r,&c);
tag++;
update(rt[tag-],rt[tag],l,r,,n,c);
}
else if(op[]=='Q'){
int l,r;
scanf("%d%d",&l,&r);
printf("%lld\n",query(rt[tag],l,r,,n));
}
else if(op[]=='H'){
int l,r,d;
scanf("%d%d%d",&l,&r,&d);
printf("%lld\n",query(rt[d],l,r,,n));
}
else if(op[]=='B'){
int x;
scanf("%d",&x);
tag=x;
}
}
}
return ;
}
讲道理,这题还是没完全弄明白,还是有点迷糊。
mdzz。。。
HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))的更多相关文章
- BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 9280 Solved: 2421 ...
- HDU 2665.Kth number-可持久化线段树(无修改区间第K小)模板 (POJ 2104.K-th Number 、洛谷 P3834 【模板】可持久化线段树 1(主席树)只是输入格式不一样,其他几乎都一样的)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4417.Super Mario-可持久化线段树(无修改区间小于等于H的数的个数)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 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 To the moon(可持久化线段树)
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- SPOJ Meteors - 可持久化线段树 - 二分法
Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The plan ...
- HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 5820 (可持久化线段树)
Problem Lights (HDU 5820) 题目大意 在一个大小为50000*50000的矩形中,有n个路灯.(n<=500000) 询问是否每一对路灯之间存在一条道路,使得长度为|x1 ...
随机推荐
- jQuery Lightbox效果插件Boxer
演示:http://www.jq22.com/yanshi1139 下载:链接: https://pan.baidu.com/s/1o8zaV2q 密码: 2ccy Boxer 是一款基于 jQuer ...
- JS之递归(例题:猴子吃桃)
例题1:公园里有200个桃子,猴子每天吃掉一半以后扔掉一个,问6天以后还剩余多少桃子? var sum = 200; for(var i= 0;i<6;i++) { sum = parseInt ...
- Spring注解概览(数漫江湖)
从Java5.0开始,Java开始支持注解.Spring做为Java生态中的领军框架,从2.5版本后也开始支持注解.相比起之前使用xml来配置Spring框架,使用注解提供了更多的控制Spring框架 ...
- windows10安装oracle11g报错ORA-01034、ORA-01078
ORA-01034表示数据库实例未建立,可以先用管理员账号进入一个空白实例 sqlplus / as sysdba; 如果您当前使用的账号是安装oracle的账号,则不需要账号密码就可以登陆oracl ...
- 大聊Python----json与pickle数据序列化
用于序列化的两个模块 ☆json,用于字符串和python数据类型间进行转换 ☆pickle,用于python特有的类型和python的数据类型间进行转换 Json模块提供了四个功能:dumps.du ...
- Python 关于拷贝(copy)汇总(列表拷贝 // 字典拷贝 // 自定义对象拷贝)
1.列表拷贝 引用是指保存的值为对象的地址.在 Python 语言中,一个变量保存的值除了基本类型保存的是值外,其它都是引用,因此对于它们的使用就需要小心一些.下面举个例子: 问题描述:已知一个列表, ...
- css控制文字换行
1.word-wrap 设置为break-word时,文本中的长单词或url可以换行 <p style="width:100px;word-wrap:break-word;border ...
- HashMap 、LinkedHashMap、HashTable、TreeMap 和 Properties 的区别
HashMap 1.线程不安全: 2.允许null value 和 null key: 3.访问效率比较高: 4.Java1.2引进的Map接口的一个实现: 5.轻量级: 6.根据键的HashCode ...
- Python3 PyPAML 模块(配置文件的操作)
YAML 是专门用来写配置文件的语言,非常简洁和强大 它的基本语法规则如下: 1.大小写敏感 2.使用缩进表示层级关系 3.缩进时不允许使用Tab键,只允许使用空格. 4.缩进的空格数目不重要,只要相 ...
- 25个Linux相关的网站【转】
转自:http://www.cnblogs.com/Lindaman/p/4552805.html 下面是25个最具有影响力,也是最重要的Linux网站,这些网站提供了Linux的分发包,软件,文件, ...