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 ...
随机推荐
- Tomcat+MySQL常见调优参数
一.Tomcat 调优 (一).Tomcat内存优化 参数一: vim /tomcat/bin/catalina.sh CATALINA_OPTS="-server -Xms128m -Xm ...
- 01-Spring Security框架学习
目录 01-Spring Security框架学习 简介 Spring Security 是什么 Spring Security 解决那些问题 Spring Security 的优点 历史背景 Spr ...
- Cobbler 自动安装CentOS7
1. Cobbler介绍 Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装.重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等.Cobbler可以使用命 ...
- centos开发环境安装
执行 yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel ...
- SpringMVC的流程
Springmvc的流程 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求后,调用HandlerMapping处理映射器,请求获取Hand ...
- js 数组对象深拷贝
js 数组对象深拷贝 结论:对象的拷贝不能采用直接赋值的方式. 背景 踩过的坑如下: formData本来是父组件传过来的,但是我不想直接用,于是我直接赋值给一个formDataCopy的对象. 但是 ...
- centos虚拟机配置静态ip
昨天在配置虚拟机的时候因为之前没有设置静态IP,而是使用DHCP动态分配的,导致关机后下次开机虚拟机的ip是随机变动的.严重影响了工作体验啊,遂设置静态ip以保全! 虚拟机使用的是CentOS6.5, ...
- android ——悬浮按钮及可交互提示
一.悬浮按钮 FloatingActionButton是Design Support中的一个控件,它会默认colorAccent作为按钮的颜色,还可以给按钮一个图标. 这是没有图标的,这是有图标的. ...
- tk.mybatis扩展通用接口
一.tk.mybatis已经为我们封装好了许多拆箱即用的通用mapper,但在实际的项目开发中想必不少小伙伴在数据库设计中都会采用逻辑删除这种方案,再去使用通用的mapper接口就不行了.这时候就需要 ...
- java后端开发面经 数据库相关
小姐姐:怎么理解感情中的付出和回报? 你答:有这样一个故事,讲的是一个小男孩和一个小女孩,这个小男孩呢,用很多好玩石头,而这个小女孩呢,有好多好吃的糖果,有一天,他们相互约定:小男孩用所有的石头交互小 ...