hdu 4348 To the moon 主席树区间更新
To the moon
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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
#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 主席树区间更新的更多相关文章
- hdu 4348 To the moon (主席树区间更新)
传送门 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q l r:查询当前时间戳区间[l,r]中所有数的和 . (3)H ...
- HDU 4348 To the moon 主席树 在线更新
http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...
- hdu 4348 To the moon (主席树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...
- HDU 4348 To the moon 主席树
题意: 给出一个长度为\(n(n \leq 10^5)\)的序列,最开始时间\(t=0\),支持下面几个操作: \(C \, l \, r \, d\):将区间\([l,r]\)每个数都加上\(d\) ...
- HDU 4348 主席树区间更新
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- 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步操作 思路: ...
- 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 ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
随机推荐
- Express框架(http服务器 + 路由)
index.js 使用express框架搭建http服务器,和实现路由功能. var express = require('express'); var app = express(); // 主页输 ...
- 几行小代码,将Testlink的xml用例导入至excel
最近在使用Testlink时,发现导入的用例是xml格式,且没有合适的工具转成excel格式,xml使用excel打开显示的东西也太多,网上也有相关工具转成csv格式的,结果也不合人意. 那求人不如尔 ...
- Thrift 入门之helloWorld
不多说,先看项目结构 首先先编写一个hello.thrift的文件 hello.thrift namespace java sawshaw service HelloService { string ...
- js对字符串进行加密和解密方法!
在做一些微信小程序,或混合 app 的时候,或者是考虑到一些 JS 数据安全的问题.可能会使用到 JS 对用户信息进行缓存. 例如在开发:微信小程序对用户进行加密缓存,开发混合APP对用户信息进行加密 ...
- aliyun oss 文件上传 java.net.SocketTimeoutException Read timed out 问题分析及解决
upload ClientException Read timed out com.aliyun.openservices.ClientException: Read timed out ...
- slave have equal MySQL Server UUIDs原因及解决
最近在部署MySQL主从复制架构的时候,碰到了"Last_IO_Error: Fatal error: The slave I/O thread stops because master a ...
- ORA-39006、ORA-39065、ORA-01403、ORA-39097错误解决办法
今天有同事找说是expdp到出数据时报错: 处理方法:sys用户下执行如下语句重新生成DATAPUMP API用到的视图问题就解决了. SQL> @?/rdbms/admin/catmeta.s ...
- Python开发【笔记】:进程
序言 进程与线程概述: 很多同学都听说过,现代操作系统比如Mac OS X,UNIX,Linux,Windows等,都是支持“多任务”的操作系统. 什么叫“多任务”呢?简单地说,就是操作系统可以同时运 ...
- Python开发【模块】:Requests(一)
Requests模块 1.模块说明 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urlli ...
- 双态运维:如何让CMDB配置维护更贴近人性
近来很多行业内的大佬关于CMDB连连发声,CMDB的关注度持续高涨,CMDB的前生就是长满雀斑的丑媳妇,扭扭捏捏不受待见这么多年,终于熬出头要见公婆了.哎,她的贤惠谁能懂? 言归正传,在拜读了多篇大牛 ...