题意:

给一个数组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步操作

思路:

用主席树维护常规的线段树。我们之前已经知道了主席树单点更新,只要新增一条链就ok了,区间更新也有点差不多。我们每次要更新都用一个lazy标记,但是显然我们不能去更新那些已经存在的区间,那么我们就新建出所有要更新的区间。因为可持久化线段树有些结点不是自己的,所以我们不能直接简单的push up和push down,那么我们在对区间加v的时候处理为:如果这个区间完全是新建的(L <= l && R >= r),那么我直接加lazy标记就好,不是的话就直接更新要更新的区间的那部分 T[now].sum += (min(R, r) - max(L, l) + 1) * v ,然后继续往下更新。我在查询的时候,遇到lazy都要直接加上,因为我这个lazy是不往下传的,所以你往下走的时候要先加上指定的区间的lazy。

代码:

#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + ;
const int M = maxn * ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
int a[maxn], root[maxn], tot;
int n, m;
struct node{
int lson, rson;
ll sum, lazy;
}T[maxn * ];
void init(){
tot = ;
}
void pushUp(int rt){
T[rt].sum = T[T[rt].lson].sum + T[T[rt].rson].sum;
}
void build(int l, int r, int &rt){
rt = ++tot;
T[rt].lazy = T[rt].sum = ;
if(l == r){
T[rt].sum = a[l];
return;
}
int m = (l + r) >> ;
build(l, m,T[rt].lson);
build(m + , r, T[rt].rson);
pushUp(rt);
}
void update(int l, int r, int L, int R, int &now, int pre, ll v){
T[++tot] = T[pre], now = tot;
T[now].sum += (min(R, r) - max(L, l) + ) * v;
if(L <= l && R >= r){
T[now].lazy += v;
return;
}
int m = (l + r) >> ;
if(L <= m)
update(l, m, L, R, T[now].lson, T[pre].lson, v);
if(R > m)
update(m + , r, L, R, T[now].rson, T[pre].rson, v);
}
ll query(int l, int r, int L, int R, int rt){
if(L <= l && R >= r){
return T[rt].sum;
}
int m = (l + r) >> ;
ll ans = (min(R, r) - max(L, l) + ) * T[rt].lazy;
if(L <= m)
ans += query(l, m, L, R, T[rt].lson);
if(R > m)
ans += query(m + , r, L, R, T[rt].rson);
return ans;
}
int main(){
while(~scanf("%d%d", &n, &m)){
init();
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
int time = ;
build(, n, root[time]);
while(m--){
char o[];
int l, r, v;
scanf("%s", o);
if(o[] == 'C'){
scanf("%d%d%d", &l, &r, &v);
++time;
update(, n, l, r, root[time], root[time - ], v);
}
else if(o[] == 'Q'){
scanf("%d%d", &l, &r);
printf("%lld\n", query(, n, l, r, root[time]));
}
else if(o[] == 'H'){
scanf("%d%d%d", &l, &r, &v);
printf("%lld\n", query(, n, l, r, root[v]));
}
else{
scanf("%d", &v);
time = v;
}
}
}
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 主席树区间更新

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

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

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

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

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

  5. HDU 4348 To the moon 主席树

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

  6. HDU 4348 主席树区间更新

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

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

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

  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. net-tools与iproute2

    net-tools与iproute2 我们知道redhat系列发行版7x版本中最小化安装是没有net-tools工具包的,默认使用iproute2工具包,最直观的感受是ifconfig命令被ip命令所 ...

  2. C++ 实验2

    #include <iostream> using namespace std; template<class T> void insertionSort(T a[],int ...

  3. turtlebot3 ubuntu mate 实现vnc连接

    tuutlebot3 ubuntu mate 实现vnc连接 摘要: 在turtlebot3 安装的nbuntu mate系统实验过. 实现内容 x11vnc 安装 x11vnc自动启动 vnc分辩率 ...

  4. RN如何基于js代码手动打一个main.jsbundle

    react-native bundle --entry-file index.js --bundle-output ./ios/bundle/main.jsbundle --platform ios ...

  5. 最好还是用#pragma once

    最好还是用#pragma once  https://www.cnblogs.com/techdreaming/p/4921780.html

  6. MIPI-Layout说明(转载)

    转载于:http://www.ccm99.com/thread-3713-1-1.html 前言:随着新的总线协议不断提高信号速率,如今的PCB 设计人员需要充分理解高速布线的要求并控制PCB 走线的 ...

  7. 数据仓库建模对比: 比较表格和多维解决方案 (Comparing tabular and multidimensional solutions)

    笔记记下来,划重点: https://docs.microsoft.com/zh-cn/sql/analysis-services/comparing-tabular-and-multidimensi ...

  8. 拼多多(7pdd)微信跳转h5页面打开app跳转任意url关注技术weixin://dl/business/?ticket

    拼多多微信跳转接口利用了微信官方的weixin://dl/business/?ticket技术,此类接口可以在官方接口中找到,分析代码如下: <title>拼多多</title> ...

  9. nginx----------前端写了一套带有vue路由的的功能。放到nginx配置的目录下以后,刷新会报404未找到。

    1. 这是根据实际情况来写的. location /h5/activity/wechat/ {            index  index.html index.htm index.php;    ...

  10. UGUI中UI与模型混合显示

    法一: 利用Render Texture 在project面板创建 在面板中在创建一个Camera,对准要显示的模型 对Render Texture 进行设置 在Canvas下创建RawImage 就 ...