To the moon

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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 {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.

 
Input
n m
A1 A2 ... An
... (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

 
Author
HIT
 
Source
给你一个数组,让你维护,m次操作
  询问当前时刻一个区间的和
  询问在t时刻的一个区间的和
  回到t时刻
  时间+1,在此时刻+1下更新一个区间的值
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=5e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
ll a[N];
struct Chairmantree
{
int rt[N*],ls[N*],rs[N*];
ll sum[N*],lazy[N*];
int tot;
void init()
{
tot=;
}
void pushup(int l,int r,int pos)
{
sum[pos]=sum[ls[pos]]+sum[rs[pos]]+1LL*(r-l+)*lazy[pos];
}
void build(int l,int r,int &pos)
{
pos=++tot;
lazy[pos]=;
sum[pos]=;
if(l==r)
{
sum[pos]=a[l];
return;
}
int mid=(l+r)>>;
build(l,mid,ls[pos]);
build(mid+,r,rs[pos]);
pushup(l,r,pos);
}
void update(int L,int R,ll c,int pre,int l,int r,int &pos)
{
pos=++tot;
ls[pos]=ls[pre];
rs[pos]=rs[pre];
sum[pos] = sum[pre];
lazy[pos]=lazy[pre];
if(L==l&&r==R)
{
sum[pos]+=1LL*(r-l+)*c;
lazy[pos]+=c;
return;
}
int mid=(l+r)>>;
if(R<=mid)
update(L,R,c,ls[pre],l,mid,ls[pos]);
else if(L>mid)
update(L,R,c,rs[pre],mid+,r,rs[pos]);
else
{
update(L,mid,c,ls[pre],l,mid,ls[pos]);
update(mid+,R,c,rs[pre],mid+,r,rs[pos]);
}
pushup(l,r,pos);
}
ll query(int L,int R,int l,int r,int pos)
{
if(L==l&&r==R)
return sum[pos];
int mid=(l+r)>>;
ll ans=1LL*lazy[pos]*(R-L+);
if(R<=mid)
ans+=query(L,R,l,mid,ls[pos]);
else if(L>mid)
ans+=query(L,R,mid+,r,rs[pos]);
else
{
ans+=query(L,mid,l,mid,ls[pos]);
ans+=query(mid+,R,mid+,r,rs[pos]);
}
return ans;
}
};
Chairmantree tree;
char s[];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=; i<=n; i++)
scanf("%lld",&a[i]);
tree.init();
tree.build(,n,tree.rt[]);
int now=;
while(m--)
{
scanf("%s",s);
if(s[]=='C')
{
int l,r;
ll c;
scanf("%d%d%lld",&l,&r,&c);
tree.update(l,r,c,tree.rt[now],,n,tree.rt[now+]);
now++;
}
else if(s[]=='Q')
{
int l,r;
scanf("%d%d",&l,&r);
printf("%lld\n",tree.query(l,r,,n,tree.rt[now]));
}
else if(s[]=='H')
{
int l,r,t;
scanf("%d%d%d",&l,&r,&t);
printf("%lld\n",tree.query(l,r,,n,tree.rt[t]));
}
else
{
int x;
scanf("%d",&x);
now=x;
}
}
}
return ;
}

hdu 4348 To the moon 主席树区间更新的更多相关文章

  1. hdu 4348 To the moon (主席树区间更新)

    传送门 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q l r:查询当前时间戳区间[l,r]中所有数的和 . (3)H ...

  2. HDU 4348 To the moon 主席树 在线更新

    http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...

  3. hdu 4348 To the moon (主席树)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...

  4. HDU 4348 To the moon 主席树

    题意: 给出一个长度为\(n(n \leq 10^5)\)的序列,最开始时间\(t=0\),支持下面几个操作: \(C \, l \, r \, d\):将区间\([l,r]\)每个数都加上\(d\) ...

  5. HDU 4348 主席树区间更新

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  7. 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步操作 思路: ...

  8. HDU 4348 To the moon (主席树区间更新)

    题意:首先给你n个数,开始时间为0,最后按照操作输出 给你四种操作: 1. C l r d :  在(l,r)区间都加上d,时间加一2. Q l r :  询问现在(l,r)的区间和3. H l r ...

  9. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

随机推荐

  1. Autofac在项目中应用的体会,一个接口多个实现的情况

    在本人接触的项目中Autofac应用的比较多一些,我理解的他的工作原理就是  注册类并映射到接口,通过注入后返回相应实例化的类! 下面说说我在项目中的实际应用 先来简单介绍下Autofac的使用 1. ...

  2. shell脚本抓取网页信息

    利用shell脚本分析网站数据 # define url time=$(date +%F) mtime=$(date +%T) file=/abc/shell/abc/abc_$time.log ht ...

  3. java jar命令及补丁方法

    用法: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...选项: -c 创建新档案 -t ...

  4. OC开发_Storyboard——UITableView

    一.tableView 1.datasource数据源 (1 构造每一个tableVIewCell的方法:cellForRowAtIndexPath,这里的 dequeueReusableCellWi ...

  5. HDCMS留言插件的使用!

    HDCMS留言插件,JS简单示例: <img src='{|U:'code'}' onclick='this.src='{|U:'code'}&'+Math.random()' /> ...

  6. Hive sql语法详解

      Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构 化的数据文件映射为一张数据库表,并提供完整的SQ ...

  7. Hibernate之核心文件

    一个Hibernate项目核心配置文件主要分为以下三个方面:1.配置文件hibernate.cfg.xml:2.配置文件*.hbm.xml,此文件一般包括映射文件的结构以及各种属性的映射方式:3.Hi ...

  8. SpringMVC中 解决@ResponseBody注解返回中文乱码

    问题:在前端通过get请求服务端返回String类型的服务时,会出现中文乱码问题 原因:由于spring默认对String类型的返回的编码采用的是 StringHttpMessageConverter ...

  9. JAVA内存构成详解

    java memory = direct memory(直接内存) + jvm memory(MaxPermSize +Xmx)   1)直接内存跟堆 直接内存则是一块由程序本身管理的一块内存空间,它 ...

  10. MAC电脑里的休眠功能在哪里?

    Windows7和Ubuntu里都有睡眠和休眠功能,睡眠一般是指挂起到内存,电脑停止运行,数据都在内存里,只需要给内存供电,恢复时很快:休眠是指挂起到硬盘,电脑可以完全停止供电,恢复时从硬盘读取数据, ...