HDU - 4348 To the moon (可持久化线段树,区间查询加累加)
2017-10-25 21:12
489 查看
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
. On these integers, you need to implement the following operations:
1. C l r d: Adding a constant d for every {A i | 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 {A i | l <= i <= r}.
3. H l r t: Querying a history sum of {A i | 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 ≤ 10 5, |A [i]| ≤ 10 9, 1 ≤ l ≤ r ≤ N, |d| ≤ 10 4 .. 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.
Inputn m
A 1 A 2 ... A n
... (here following the m operations. )
Output... (for each query, simply print the result. )
Sample Input
Sample Output
题解:简单的 可持久化线段树+区间查询,区间累加。搞了半天才搞懂并调试好所有代码。又一模板get!这里少了下推函数,可持久化线段树的特性,很巧妙。
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
int lc[3000010];//左孩子
int rc[3000010];//右孩子
ll sum[3000010];//区间和
ll lazy[3000010];//懒惰标记
int n,m,cnt,x,y,z,now;
//原数组,第N次操作的根节点
int A[100010],T[100010];
char op[3];
//建一棵空树
void build(int &now,int l,int r){
now=++cnt;
if(l==r){
sum[now]=A[l];
return;
}
build(lc[now],l,(l+r)/2);
build(rc[now],(l+r)/2+1,r);
//push_up
sum[now]=sum[lc[now]]+sum[rc[now]];
}
void add(int &now,int l,int r,int val,int L,int R){
//新建一个节点,并初始化为前一棵线段树的节点的值
cnt++;
lc[cnt]=lc[now];
rc[cnt]=rc[now];
sum[cnt]=sum[now];
lazy[cnt]=lazy[now];
now=cnt;
sum[now]+=1ll*val*(r-l+1);
if(l==L&&r==R){
lazy[now]+=val;//打上懒惰标记
return;
}
if(r<=(L+R)/2)
add(lc[now],l,r,val,L,(L+R)/2);
else
if(l>(L+R)/2)
add(rc[now],l,r,val,(L+R)/2+1,R);
else{
add(lc[now],l,(L+R)/2,val,L,(L+R)/2);
add(rc[now],(L+R)/2+1,r,val,(L+R)/2+1,R);
}
}
ll query(int now,int l,int r,int L,int R){
if(l==L&&r==R)
return sum[now];
ll tmp=1ll*(r-l+1)*lazy[now];//超级巧妙,不用下推了,直接算就对了!
if(r<=(L+R)/2)
return tmp+query(lc[now],l,r,L,(L+R)/2);
if(l>(L+R)/2)
return tmp+query(rc[now],l,r,(L+R)/2+1,R);
return tmp+query(lc[now],l,(L+R)/2,L,(L+R)/2)+query(rc[now],(L+R)/2+1,r,(L+R)/2+1,R);
}
int main(){
while(~scanf("%d%d",&n,&m)){
now=cnt=0;
for(int i=1;i<=n;++i)
scanf("%d",&A[i]);
build(T[0],1,n);
for(int i=1;i<=m;++i){
scanf("%s",op);
if(op[0]=='C'){
scanf("%d%d%d",&x,&y,&z);
++now;
add(T[now]=T[now-1],x,y,z,1,n);
}
else
if(op[0]=='Q'){
scanf("%d%d",&x,&y);
printf("%I64d\n",query(T[now],x,y,1,n));
}
else
if(op[0]=='H'){
scanf("%d%d%d",&x,&y,&z);
printf("%I64d\n",query(T[z],x,y,1,n));
}
else
scanf("%d",&now);
}
}
}
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
. On these integers, you need to implement the following operations:
1. C l r d: Adding a constant d for every {A i | 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 {A i | l <= i <= r}.
3. H l r t: Querying a history sum of {A i | 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 ≤ 10 5, |A [i]| ≤ 10 9, 1 ≤ l ≤ r ≤ N, |d| ≤ 10 4 .. 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.
Inputn m
A 1 A 2 ... A n
... (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
题解:简单的 可持久化线段树+区间查询,区间累加。搞了半天才搞懂并调试好所有代码。又一模板get!这里少了下推函数,可持久化线段树的特性,很巧妙。
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
int lc[3000010];//左孩子
int rc[3000010];//右孩子
ll sum[3000010];//区间和
ll lazy[3000010];//懒惰标记
int n,m,cnt,x,y,z,now;
//原数组,第N次操作的根节点
int A[100010],T[100010];
char op[3];
//建一棵空树
void build(int &now,int l,int r){
now=++cnt;
if(l==r){
sum[now]=A[l];
return;
}
build(lc[now],l,(l+r)/2);
build(rc[now],(l+r)/2+1,r);
//push_up
sum[now]=sum[lc[now]]+sum[rc[now]];
}
void add(int &now,int l,int r,int val,int L,int R){
//新建一个节点,并初始化为前一棵线段树的节点的值
cnt++;
lc[cnt]=lc[now];
rc[cnt]=rc[now];
sum[cnt]=sum[now];
lazy[cnt]=lazy[now];
now=cnt;
sum[now]+=1ll*val*(r-l+1);
if(l==L&&r==R){
lazy[now]+=val;//打上懒惰标记
return;
}
if(r<=(L+R)/2)
add(lc[now],l,r,val,L,(L+R)/2);
else
if(l>(L+R)/2)
add(rc[now],l,r,val,(L+R)/2+1,R);
else{
add(lc[now],l,(L+R)/2,val,L,(L+R)/2);
add(rc[now],(L+R)/2+1,r,val,(L+R)/2+1,R);
}
}
ll query(int now,int l,int r,int L,int R){
if(l==L&&r==R)
return sum[now];
ll tmp=1ll*(r-l+1)*lazy[now];//超级巧妙,不用下推了,直接算就对了!
if(r<=(L+R)/2)
return tmp+query(lc[now],l,r,L,(L+R)/2);
if(l>(L+R)/2)
return tmp+query(rc[now],l,r,(L+R)/2+1,R);
return tmp+query(lc[now],l,(L+R)/2,L,(L+R)/2)+query(rc[now],(L+R)/2+1,r,(L+R)/2+1,R);
}
int main(){
while(~scanf("%d%d",&n,&m)){
now=cnt=0;
for(int i=1;i<=n;++i)
scanf("%d",&A[i]);
build(T[0],1,n);
for(int i=1;i<=m;++i){
scanf("%s",op);
if(op[0]=='C'){
scanf("%d%d%d",&x,&y,&z);
++now;
add(T[now]=T[now-1],x,y,z,1,n);
}
else
if(op[0]=='Q'){
scanf("%d%d",&x,&y);
printf("%I64d\n",query(T[now],x,y,1,n));
}
else
if(op[0]=='H'){
scanf("%d%d%d",&x,&y,&z);
printf("%I64d\n",query(T[z],x,y,1,n));
}
else
scanf("%d",&now);
}
}
}
相关文章推荐
- HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和
- Hdu-4348 To the moon(可持久化线段树)
- hdu-4348-To the moon(主席树更新,区间更新,区间和查询,保存历史版本返回)
- HDU 4348 To the moon(可持久化线段树)
- HDU 4348 To the moon(可持久化线段树)
- 【HDU】4348 To the moon 【可持久化线段树】
- 【HDU 4348】To the moon【可持久化线段树】
- HDU 4348 To the moon (可持久化线段树+更新)
- 【可持久化线段树】【hdu 4348】To the moon
- HDU 4348 To the moon 可持久化线段树
- hdu-4348-To the moon-主席树在线区间更新
- [hdu 4348]区间修改区间查询可持久化线段树
- hdu-4348-To the moon-离线处理 or 主席树区间更新
- HDU 4348 To the moon(可持久化线段树+内存池)
- HDU 4348 To the moon(主席树区间修改)
- hdu 4348 To the moon 主席树区间更新
- hdu 4348 To the moon(线段树成段更新) 2012 Multi-University Training Contest 5
- 【HDU - 4348】To the moon(主席树在线区间更新)
- hdu - 4348 - To the moon - 线段树+历史询问
- HDU 4348 To the moon 主席树 + 区间更新