博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4348 To the moon 可持久化线段树
阅读量:5234 次
发布时间:2019-06-14

本文共 3724 字,大约阅读时间需要 12 分钟。

To the moon

 

Problem Description
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
[N]. 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.
 

 

Input
n 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
 
 
题意:
   
  给你一个数组,让你维护,m次操作
  询问当前时刻一个区间的和
  询问在t时刻的一个区间的和
  回到t时刻
  时间+1,在此时刻+1下更新一个区间的值
 
题解:
  
  函数式线段树的裸体
 
#include 
#include
#include
#include
#include
using namespace std;const int N = 5e6+10, M = 1e3+10, mod = 1000000, inf = 1e9+1000;typedef long long ll;int n,m;int l[N],r[N],root[N],add[N],tot = 0;ll sum[N];int build(int s,int t) { int now = ++tot; add[now] = 0; if(s==t) { scanf("%lld",&sum[now]); l[now] = r[now] = 0; return now; } int mid = (s+t)>>1; l[now] = build(s,mid); r[now] = build(mid+1,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+1)); if(lll==s&&rr==t) return sum[k]; int mid = (s+t)>>1; if(rr<=mid) ans+=query(l[k],lll,rr,s,mid); else if(lll>mid) ans+=query(r[k],lll,rr,mid+1,t); else { ans+=query(l[k],lll,mid,s,mid); ans+=query(r[k],mid+1,rr,mid+1,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+1)); if(lll==s&&rr==t) { add[now]+=d; return now; } int mid = (s+t)>>1; 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+1,t); else { l[now] = update(l[k],lll,mid,d,s,mid); r[now] = update(r[k],mid+1,rr,d,mid+1,t); } return now;}void solve() { tot = 0; root[0] = build(1,n); int now = 0; for(int i=1;i<=m;i++) { char ch[3]; scanf("%s",ch); if(ch[0]=='Q') { int a,b; scanf("%d%d",&a,&b); printf("%lld\n",query(root[now],a,b,1,n)); } else if(ch[0]=='C') { int a,b,d; scanf("%d%d%d",&a,&b,&d); root[now+1] = update(root[now],a,b,d,1,n); now++; } else if(ch[0]=='H') { int a,b,t; scanf("%d%d%d",&a,&b,&t); printf("%lld\n",query(root[t],a,b,1,n)); } else scanf("%d",&now); }}int main() { while(scanf("%d%d",&n,&m)!=EOF) { solve(); } return 0;}

 

转载于:https://www.cnblogs.com/zxhl/p/5469263.html

你可能感兴趣的文章
eas之设置是否在调入列表之前先出过滤框
查看>>
eas之设置单据分录单元格式
查看>>
eas之手工发送消息
查看>>
eas之F7专用选择界面设置
查看>>
eas之删除类别时刷新当前结点的父结点,并定位到当前结点的父结点。
查看>>
eas之修改类别时刷新当前结点的父结点,并定位到当前结点
查看>>
eas之弹出指定的F7
查看>>
eas之得到当前选中的行id
查看>>
eas-dep之客户端上下文
查看>>
eas之常用数据格式
查看>>
eas-dep函数
查看>>
eas-dep界面控件
查看>>
eas之 获取某个资源文件的键值
查看>>
eas-dep客户/服务业务
查看>>
eas之获取打开界面的值
查看>>
eas之复制粘贴
查看>>
eas之服务端常用代码
查看>>
eas之获取汇率
查看>>
eas之获取当前本位币(其实就是单据上边的控件的属性值)
查看>>
eas之获取集合
查看>>