[主席树]HDOJ4348 To the moon
题意:n个数, m个操作
1. C l r d 给[l, r]区间的每个数加上d
2. Q l r: 查询[l, r]区间的和
3. H l r t: 查询第t个操作时[l, r]区间的和
4. B t: 回到第t个操作之后
因为有查询历史的区间和,故用主席树(保留了历史)
区间更新直接更新到每个子节点即可
出题人的题解 
神么链接都打不开
#define lson l, m
#define rson m+1, r const int N=1e5+;
const double eps=1e-;
int L[N<<], R[N<<], sum[N<<];
LL ans[N<<];
int tot;
int a[N], T[N], Hash[N]; void pushup(int rt)
{
ans[rt]=ans[L[rt]]+ans[R[rt]];
} int build(int l, int r)
{
int rt=(++tot);
sum[rt]=;
if(l==r)
{
scanf("%I64d", &ans[rt]);
L[rt]=R[rt]=;
return rt;
}
int m=(l+r)>>;
L[rt]=build(lson);
R[rt]=build(rson);
pushup(rt);
return rt;
} int update(int pre, int _L, int _R, int l, int r, LL x)
{
int rt=(++tot);
L[rt]=L[pre], R[rt]=R[pre], sum[rt]=sum[pre], ans[rt]=ans[pre]+x*(_R-_L+);
if(_L<=l && r<=_R)
{
sum[rt]+=x;
return rt;
}
int m=(l+r)>>;
if(_R<=m)
L[rt]=update(L[pre], _L, _R, lson, x);
else if(_L>m)
R[rt]=update(R[pre], _L, _R, rson, x);
else
{
L[rt]=update(L[rt], _L, m, lson, x);
R[rt]=update(R[rt], m+, _R, rson, x);
}
return rt;
} LL query(int rt, int _L, int _R, int l, int r)
{
LL cnt=sum[rt]*(LL)(_R-_L+);
if(_L<=l && r<=_R)
return ans[rt];
int m=(l+r)>>;
if(_R<=m)
cnt+=query(L[rt], _L, _R, lson);
else if(_L>m)
cnt+=query(R[rt], _L, _R, rson);
else
{
cnt+=query(L[rt], _L, m, lson);
cnt+=query(R[rt], m+, _R, rson);
}
return cnt;
} int main()
{
int n, m;
while(~scanf("%d%d", &n, &m))
{
tot=;
T[]=build(, n);
int time=;
while(m--)
{
char op[];
scanf("%s", op);
if(op[]=='Q')
{
int l, r;
scanf("%d%d", &l, &r);
printf("%I64d\n", query(T[time], l, r, , n));
}
else if(op[]=='C')
{
int l, r;
LL x;
scanf("%d%d%I64d", &l, &r, &x);
T[time+]=update(T[time++], l, r, , n, x);
}
else if(op[]=='H')
{
int l, r, rt;
scanf("%d%d%d", &l, &r, &rt);
printf("%I64d\n", query(T[rt], l, r, , n));
}
else
scanf("%d", &time);
}
}
return ;
}
HDOJ 4348
[主席树]HDOJ4348 To the moon的更多相关文章
- 【HDU4348】【主席树】To the moon
Problem Description BackgroundTo The Moon is a independent game released in November 2011, it is a r ...
- To the moon HDU - 4348 (主席树,区间修改)
Background To The Moon is a independent game released in November 2011, it is a role-playing adventu ...
- hdu4348 To the moon (主席树 || 离线线段树)
Problem Description Background To The Moon is a independent game released in November 2011, it is a ...
- 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 (主席树 区间更新)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4348 题意: 4种操作: C l r c 区间[l,r]加c,时间+1 Q l r 询问当前时 ...
- 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 (主席树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...
- HDU 4348 To the moon(主席树区间修改)
题意 给你一个区间,支持如下操作: 在一段区间内加上一个值,并生成一个历史版本 查询某个版本下一段区间内的和 回到一个历史版本上并舍弃之后的版本 做法 这就是主席树区间修改裸题啦QwQ 上一篇博客我讲 ...
- [HDU4348]To the moon(主席树+标记永久化)
学可持久化treap的时候才发现自己竟然没写过需要标记下传的主席树,然而现在发现大部分操作都可以标记永久化,下传会增大占用空间. 这题一种写法是和普通的线段树一样标记下传,注意所有修改操作(包括put ...
随机推荐
- System Generator入门
System generator 安装之后会在Simulin模块库中添加一些Xilinx FPGA专用的模块库,包括Basic Element,Communication,Control Logi ...
- 字符串反转(StringBuffer)
package com.java1234.chap03.sec08; public class zifufanzhuan { public static void main(String[] args ...
- 转:JAVA中this用法小结
转:http://blog.sina.com.cn/s/blog_6a6badc90100t8hm.html#SinaEditor_Temp_FontName Java关键字this只能用于方法方法体 ...
- Ping N个IP测试网络连通性
#-----------------------Smokeping移动节点-------------------##! /bin/bashecho "------------- Statin ...
- RAID 0+1
正如其名字一样RAID 0+1是RAID 0和RAID 1的组合形式,也称为RAID 01. 以四个磁盘组成的RAID 0+1为例,其数据存储方式如图所示:RAID 0+1是存储性能和数据安全兼顾的方 ...
- 例题6-7 Trees on the level ,Uva122
本题考查点有以下几个: 对数据输入的熟练掌握 二叉树的建立 二叉树的宽度优先遍历 首先,特别提一下第一点,整个题目有相当一部分耗时在了第一个考查点上(虽然有些不必要,因为本应该有更简单的方法).这道题 ...
- python time模块和datetime模块详解
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...
- python的各种推导式(列表推导式、字典推导式、集合推导式)
推导式comprehensions(又称解析式),是Python的一种独有特性.推导式是可以从一个数据序列构建另一个新的数据序列的结构体. 共有三种推导,在Python2和3中都有支持: 列表(lis ...
- WPF中的瀑布流布局(TilePanel)控件
最近在用wpf做一个metro风格的程序,需要用到win8风格的布局容器,只能自己写一个了.效果如下 用法 : <local:TilePanel ...
- IE6和IE7的line-height和现代浏览器不一致的问题
1.我们发现在网页中设置line-height后,现代浏览器显示正常,可是在IE6 IE7下却不能正确解析,这时需要再额外的为旧版浏览器声明: p{ line-height: 30px; *line- ...