HDU4348To the moon主席树,区间修改
题意:
一个长度为n的数组,4种操作 :
(1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 。
(2)Q l r:查询当前时间戳区间[l,r]中所有数的和 。
(3)H l r t:查询时间戳t区间[l,r]的和 。
(4)B t:将当前时间戳置为t 。
(第一次修改t为1;
思路:被https://www.cnblogs.com/hsd-/p/6506175.html思路坑了一下午,好吧就是自己没懂
然后看到vjudge上的一个思路;感觉这个思路更清晰;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
typedef long long ll;
const int maxn = ; struct node {
int l,r;
ll sum,lazy;
}T[maxn*];
int root[maxn],cur,tot;
ll a[maxn]; void build(int l,int r,int &pos)
{
pos = tot++;
T[pos].sum = T[pos].lazy = ;
// T[pos].l = l,T[pos].r = r;
if(l==r)
{
T[pos].sum = a[l];
return;
}
int mid = (l+r)>>;
build(l,mid,T[pos].l);
build(mid+,r,T[pos].r);
T[pos].sum = T[T[pos].l].sum + T[T[pos].r].sum;
}
void update(int L,int R, int &x, int y , int l, int r, int d)//这里的x和上面的pos类似
{
x = tot++;
T[x] = T[y]; if(l>=L && r<=R)
{
T[x].sum += 1ll*(r - l + ) * d; //这里用目标区间的L,R;因为目标区间每个值都要加上
T[x].lazy += d;
return;
}
int mid = (l+r)>>;
if(L <= mid)
update(L, R, T[x].l, T[y].l, l, mid, d);
if(R > mid)
update(L, R, T[x].r, T[y].r, mid+,r, d); T[x].sum = T[T[x].l].sum + T[T[x].r].sum + 1ll*(r-l+) * T[x].lazy;
}
ll query(int L,int R,int x,int l,int r)
{
if(L<=l && R>=r)
{
return T[x].sum;
}
ll ans = 1ll*T[x].lazy*(min(R,r)-max(L,l) + );
int mid = (l+r)>>;
if(R > mid)
ans += query(L,R,T[x].r, mid+, r);
if(L<=mid)
ans += query(L,R,T[x].l, l,mid);
return ans;
}
int main()
{
int n,m;
int flag = ;
while(~scanf("%d%d", &n, &m))
{
if(flag)puts("");
flag = ;
tot = ,cur = ;
for(int i=; i<=n; i++)
{
scanf("%lld", &a[i]);
}
// root[0] = tot++;
build(,n,root[]);
while(m--)
{
char op[];
scanf("%s" , op);
if(op[]=='Q')
{
int l,r;
scanf("%d%d",&l,&r);
printf("%lld\n",query(l,r,root[cur],,n));
}
else if(op[]=='C')
{
int l,r;
ll d;
scanf("%d%d%lld", &l, &r, &d);
cur++;
update(l,r,root[cur],root[cur-], , n, d);
}
else if(op[]=='H')
{
int l,r,t;
scanf("%d%d%d",&l,&r,&t);
printf("%lld\n",query(l,r,root[t],,n));
}
else if(op[]=='B')
{
scanf("%d",&cur);
}
}
}
return ;
}
HDU4348To the moon主席树,区间修改的更多相关文章
- HDU 4348 To the moon(主席树区间修改)
题意 给你一个区间,支持如下操作: 在一段区间内加上一个值,并生成一个历史版本 查询某个版本下一段区间内的和 回到一个历史版本上并舍弃之后的版本 做法 这就是主席树区间修改裸题啦QwQ 上一篇博客我讲 ...
- 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 主席树区间更新
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Prob ...
- zoj2112 树状数组+主席树 区间动第k大
Dynamic Rankings Time Limit: 10000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Subm ...
- HDU 4348 主席树区间更新
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 5919 主席树(区间不同数的个数 + 区间第k大)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)
题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...
- 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
随机推荐
- 脱壳0-FSG壳-详细流程
目录 @ 1 拿到当前加壳程序,用exeinfo/PeID 看一下信息 可以看出是很老的壳FSG. 分析: Entry Point : 000000154,熟悉PE结构的知道,入口点(代码)揉进P ...
- ArchSummit分享 | 高德地图App架构演化与实践
讲师介绍 郝仁杰,高德地图无线开发专家.在7月13日落幕的2019年ArchSummit峰会上就高德地图近几年的App架构演化和实践进行了分享. 背景概述 高德是国内领先的数字地图内容.导航和位置服务 ...
- JS-对象中写方法
- jplayer
简介 jplayer是个用JavaScript写的完全免费和开源的媒体库(media library).作为jQuery插件的一员,使用jPlayer可以在你的网页上轻松加入跨平台的音乐和视频 使用方 ...
- go单元测试
testing模块 测试代码放在当前包以_test.go结尾的文件中 测试函数以Test为名称前缀 测试命令(go test) 正常编译操作(go build/install)会忽略测试文件 单例模式 ...
- H3C软件开发笔试面试总结
注:我目前是陕西师范大学计算机科学学院本科生,在西安参加笔试以及面试 先是笔试,我选择的是JAVA方向,笔试选择题目主要是一些基础性的题目,然后简答题问了final.finally.finallize ...
- JS 自执行函数
由于自己js基础知识薄弱,很多js的知识还没有掌握,所以接下来会经常写一些关于js基础知识的博客,也算给自己提个醒吧. js自执行函数,听到这个名字,首先会联想到函数.接下来,我来定义一个函数: fu ...
- HTML第六章 盒子模型
什么是盒子模型: (1)边框: (2)内边距: (3)外边距: (4)元素内容·: (5)背景色·: 边框: 属性: 颜色(border-color),粗细(border-width),样式(bord ...
- 03、Swagger2和Springmvc整合详细记录(爬坑记录)
时间 内容 备注 2018年6月18日 基本使用 spirngmvc整合swagger2 开始之前这个系列博文基本是,在项目的使用中一些模块的内容记录,但是后期逐渐优化,不单单是整合内容. swagg ...
- 客户端埋点实时OLAP指标计算方案
背景 产品经理想要实时查询一些指标数据,在新版本的APP上线之后,我们APP的一些质量指标,比如课堂连接掉线率,课堂内崩溃率,APP崩溃率等指标,以此来看APP升级之后上课的体验是否有所提升,上课质量 ...