To the moon

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5117    Accepted Submission(s): 1152

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
 题意:对于一个长度为n的序列   执行4种操作
C l r d  区间[l,r]的数全部增加d 并且当前时刻增加一
Q l r    求区间[l,r]的和
H l r t  求第t个时刻
B t       返回第t个时刻
题解:据说是主席树区间更新入门题目。
 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
#define N 530005
using namespace std;
ll a[N];
char s[];
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 buildtree(int l,int r,int &pos)
{
pos=++tot;
lazy[pos]=;
sum[pos]=;
if(l==r)
{
sum[pos]=a[l];
return ;
}
int mid=(l+r)>>;
buildtree(l,mid,ls[pos]);
buildtree(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;
}
} tree;
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=; i<=n; i++)
scanf("%I64d",&a[i]);
tree.init();
tree.buildtree(,n,tree.rt[]);
int now=;
while(m--)
{
scanf("%s",s);
if(s[]=='C')
{
int l,r;
ll c;
scanf("%d%d%I64d",&l,&r,&c);
tree.update(l,r,c,tree.rt[now],,n,tree.rt[now+]);
now++;
}
if(s[]=='Q')
{
int l,r;
scanf("%d %d",&l,&r);
printf("%I64d\n",tree.query(l,r,,n,tree.rt[now]));
}
if(s[]=='H')
{
int l,r,t;
scanf("%d %d %d",&l,&r,&t);
printf("%I64d\n",tree.query(l,r,,n,tree.rt[t]));
}
if(s[]=='B')
{
int x;
scanf("%d",&x);
now=x;
}
}
}
return ;
}

HDU 4348 主席树区间更新的更多相关文章

  1. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

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

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

  4. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  5. hdu 1698 线段树 区间更新 区间求和

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

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

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4348 题意: 4种操作: C l r c   区间[l,r]加c,时间+1 Q l r    询问当前时 ...

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

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

  8. HDU(1698),线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 区间更新重点在于懒惰标记. 当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r- ...

  9. HDU 6278 主席树(区间第k大)+二分

    Just h-index Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)To ...

随机推荐

  1. python学习笔记04 --------------基本运算符

    1.算数运算 + 加 - 减 * 乘 /   除 % 取模(先做除法,然后返回余数) ** 乘方(幂运算) //          取整(相除,然后返回商的整数部分) 2.比较运算(返回布尔值) == ...

  2. leetcode-打家劫舍(动态规划)

    你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个代表每 ...

  3. CsvHelper文档-1前言

    CsvHelper文档-1前言 英文文档链接地址:CsvHelper Document 开源项目地址:CsvHelper 翻译于2018-1-5,原本可能会随时更新: 每一段代码都是经过我实际测试的, ...

  4. python3 SQLAlchemy模块使用

    更详细的操作介绍:https://www.imooc.com/article/22343 定义: SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对 ...

  5. mvc中actionresult的返回值类型

    以前一直没注意actionresult都能返回哪些类型的类型值(一直用的公司的内部工具类初始化进行返回的),今天跟大家分享一下(也是转载的别人的日志qaq). 首先我们了解一下对action的要求: ...

  6. cygwin—excellent work!

    使用cygwin的好处在于可以避免直接使用linux同时又能最大限度的节省资源,共享windows的资源. 安装cygwin 安装安简单,当然,你首先需要使用163或者国内或者亚洲比较好的镜像作为下载 ...

  7. 开源自动驾驶仿真平台 AirSim (1) - Unreal Engine

    AirSim 官方Github: https://github.com/Microsoft/AirSim AirSim 是微软的开源自动驾驶仿真平台(其实它还能做很多事情,这里主要用于自动驾驶仿真研究 ...

  8. 3.azkaban3.0测试

    测试目标 azkaban多executor下flow的分配方式 azkaban可以同时执行的flow\job个数 azkaban单个job最小使用的内存 相关配置 executor最大线程数: exe ...

  9. 正确使用memset

    今天做了一道素数打表的题我在使用一个数组记录是否为素数的时候使用了memset,将数组里面的数都清为1,代表是素数,不是素数,就改成0,我在判断这一个数是否为素数是依据也是是0还是1,结果一直存在问题 ...

  10. 如何:调试 .NET Framework 源代码

    文章标题:如何:调试 .NET Framework 源代码 文章地址:https://technet.microsoft.com/zh-cn/cc667410.aspx