BZOJ4127Abs——树链剖分+线段树

题目描述

给定一棵树,设计数据结构支持以下操作
1 u v d  表示将路径 (u,v) 加d
2 u v  表示询问路径 (u,v) 上点权绝对值的和

输入

第一行两个整数n和m,表示结点个数和操作数
接下来一行n个整数a_i,表示点i的权值接下来n-1行,每行两个整数u,v表示存在一条(u,v)的边接下来m行,每行一个操作,输入格式见题目描述

输出

对于每个询问输出答案

样例输入

4 4
-4 1 5 -2
1 2
2 3
3 4
2 1 3
1 1 4 3
2 1 3
2 3 4

样例输出

10
13
9

提示

对于100%的数据,n,m <= 10^5 且 0<= d,|a_i|<= 10^8

 

如果都是正数直接树链剖分+线段树就行了。

现在有了负数,那不是再维护一个区间正数个数就好了?显然是不够的。

因为区间修改时会把一些负数变为正数,会改变区间正数的个数,所以我们要维护区间三个值:

1、区间绝对值之和

2、区间非负数个数

3、区间最大的负数

当每次修改一个区间时如果这个区间的最大负数会变成非负数,那么说明这个区间的非负数个数会改变,因此要重构这个区间。

怎么重构呢?

对于这个区间的左右子区间,对于不需要重构的子区间下传标记,对于需要重构的子区间就递归重构下去。

因为每个数最多只会被重构一次,因此重构均摊O(nlogn)。总时间复杂度还是O(mlogn)级别。

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int num[800010];
int mx[800010];
ll sum[800010];
int d[100010];
int f[100010];
int son[100010];
int size[100010];
int top[100010];
int to[200010];
int tot;
int head[100010];
int s[100010];
int q[100010];
int n,m;
int x,y,z;
int opt;
int cnt;
ll a[800010];
int next[200010];
int v[100010];
int merge(int x,int y)
{if(x<0&&y<0){return max(x,y);}if(x<0){return x;}if(y<0){return y;}return 0;
}
void add(int x,int y)
{tot++;next[tot]=head[x];head[x]=tot;to[tot]=y;
}
void dfs(int x)
{size[x]=1;d[x]=d[f[x]]+1;for(int i=head[x];i;i=next[i]){if(to[i]!=f[x]){f[to[i]]=x;dfs(to[i]);size[x]+=size[to[i]];if(size[to[i]]>size[son[x]]){son[x]=to[i];}}}
}
void dfs2(int x,int tp)
{s[x]=++cnt;top[x]=tp;q[cnt]=x;if(son[x]){dfs2(son[x],tp);}for(int i=head[x];i;i=next[i]){if(to[i]!=f[x]&&to[i]!=son[x]){dfs2(to[i],to[i]);}}
}
void pushup(int rt)
{num[rt]=num[rt<<1]+num[rt<<1|1];sum[rt]=sum[rt<<1]+sum[rt<<1|1];mx[rt]=merge(mx[rt<<1],mx[rt<<1|1]);
}
void pushdown(int rt,bool x,bool y,int l,int r)
{if(a[rt]){int mid=(l+r)>>1;if(x){if(mx[rt<<1]){mx[rt<<1]+=a[rt];}sum[rt<<1]+=1ll*(2*num[rt<<1]-(mid-l+1))*a[rt];a[rt<<1]+=a[rt];}if(y){if(mx[rt<<1|1]){mx[rt<<1|1]+=a[rt];}sum[rt<<1|1]+=1ll*(2*num[rt<<1|1]-(r-mid))*a[rt];a[rt<<1|1]+=a[rt];}a[rt]=0;}
}
void build(int rt,int l,int r)
{if(l==r){if(v[q[l]]<0){mx[rt]=v[q[l]];}else{num[rt]=1;}sum[rt]=abs(v[q[l]]);return ;}int mid=(l+r)>>1;build(rt<<1,l,mid);build(rt<<1|1,mid+1,r);pushup(rt);
}
void rebuild(int rt,int l,int r,ll c)
{if(l==r){num[rt]=1;sum[rt]=mx[rt]+c;mx[rt]=0;return ;}int mid=(l+r)>>1;c+=a[rt];a[rt]=c;if(mx[rt<<1]&&mx[rt<<1]+c>=0&&mx[rt<<1|1]&&mx[rt<<1|1]+c>=0){a[rt]=0;rebuild(rt<<1,l,mid,c);rebuild(rt<<1|1,mid+1,r,c);}else if(mx[rt<<1]&&mx[rt<<1]+c>=0){pushdown(rt,0,1,l,r);rebuild(rt<<1,l,mid,c);}else if(mx[rt<<1|1]&&mx[rt<<1|1]+c>=0){pushdown(rt,1,0,l,r);rebuild(rt<<1|1,mid+1,r,c);}pushup(rt);
}
void change(int rt,int l,int r,int L,int R,int c)
{if(L<=l&&r<=R){if(mx[rt]+c>=0&&mx[rt]){rebuild(rt,l,r,c);}else{if(mx[rt]){mx[rt]+=c;}a[rt]+=c;sum[rt]+=1ll*(2*num[rt]-(r-l+1))*c;}return ;}int mid=(l+r)>>1;pushdown(rt,1,1,l,r);if(L<=mid){change(rt<<1,l,mid,L,R,c);}if(R>mid){change(rt<<1|1,mid+1,r,L,R,c);}pushup(rt);
}
ll query(int rt,int l,int r,int L,int R)
{if(L<=l&&r<=R){return sum[rt];}pushdown(rt,1,1,l,r);int mid=(l+r)>>1;long long res=0;if(L<=mid){res+=query(rt<<1,l,mid,L,R);}if(R>mid){res+=query(rt<<1|1,mid+1,r,L,R);}return res;
}
void updata(int x,int y,int z)
{while(top[x]!=top[y]){if(d[top[x]]<d[top[y]]){swap(x,y);}change(1,1,n,s[top[x]],s[x],z);x=f[top[x]];}if(d[x]>d[y]){swap(x,y);}change(1,1,n,s[x],s[y],z);
}
ll downdata(int x,int y)
{ll res=0;while(top[x]!=top[y]){if(d[top[x]]<d[top[y]]){swap(x,y);}res+=query(1,1,n,s[top[x]],s[x]);x=f[top[x]];}if(d[x]>d[y]){swap(x,y);}res+=query(1,1,n,s[x],s[y]);return res;
}
int main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d",&v[i]);}for(int i=1;i<n;i++){scanf("%d%d",&x,&y);add(x,y);add(y,x);}dfs(1);dfs2(1,1);build(1,1,n);while(m--){scanf("%d",&opt);scanf("%d%d",&x,&y);if(opt==1){scanf("%d",&z);updata(x,y,z);}else{printf("%lld\n",downdata(x,y));}}
}

转载于:https://www.cnblogs.com/Khada-Jhin/p/9699973.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389614.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据挖掘流程_数据流挖掘

数据挖掘流程1-简介 (1- Introduction) The fact that the pace of technological change is at its peak, Silicon Valley is also introducing new challenges that need to be tackled via new and efficient ways. Continuous research is being carried out to improve th…

北门外的小吃街才是我的大学食堂

学校北门外的那些小吃摊&#xff0c;陪我度过了漫长的大学四年。 细数下来&#xff0c;我最怀念的是…… &#xff08;1&#xff09;烤鸡翅 吸引指数&#xff1a;★★★★★ 必杀技&#xff1a;酥流油 烤鸡翅有蜂蜜味、香辣味、孜然味……最爱店家独创的秘制鸡翅。鸡翅的外皮被…

786. 第 K 个最小的素数分数

786. 第 K 个最小的素数分数 给你一个按递增顺序排序的数组 arr 和一个整数 k 。数组 arr 由 1 和若干 素数 组成&#xff0c;且其中所有整数互不相同。 对于每对满足 0 < i < j < arr.length 的 i 和 j &#xff0c;可以得到分数 arr[i] / arr[j] 。 那么第 k 个…

[LeetCode]最长公共前缀(Longest Common Prefix)

题目描述 编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀&#xff0c;返回空字符串 ""。 示例 1:输入: ["flower","flow","flight"]输出: "fl"示例 2:输入: ["dog","racecar",&quo…

域嵌套太深_pyspark如何修改嵌套结构域

域嵌套太深In our adventures trying to build a data lake, we are using dynamically generated spark cluster to ingest some data from MongoDB, our production database, to BigQuery. In order to do that, we use PySpark data frames and since mongo doesn’t have …

redis小结

Redis 切换到redis的目录 启动&#xff1a;./redis-server 关闭&#xff1a;killall redis-server Redis的数据类型&#xff1a; String字符 list链表 set集合&#xff08;无序&#xff09; Sort Set排序&#xff08;有序&#xff09; hash数据类型 string类型的数据操作 re…

WIN10下ADB工具包安装的教程和总结 --201809

ADB&#xff08;Android Debug Bridge&#xff09;是Android SDK中的一个工具, 使用ADB可以直接操作管理Android模拟器或者真实的Andriod设备。 ADB主要功能有: 在Android设备上运行Shell(命令行)管理模拟器或设备的端口映射在计算机和设备之间上传/下载文件将电脑上的本地APK软…

1816. 截断句子

1816. 截断句子 句子 是一个单词列表&#xff0c;列表中的单词之间用单个空格隔开&#xff0c;且不存在前导或尾随空格。每个单词仅由大小写英文字母组成&#xff08;不含标点符号&#xff09;。 例如&#xff0c;“Hello World”、“HELLO” 和 “hello world hello world”…

spark的流失计算模型_使用spark对sparkify的流失预测

spark的流失计算模型Churn prediction, namely predicting clients who might want to turn down the service, is one of the most common business applications of machine learning. It is especially important for those companies providing streaming services. In thi…

峰识别 峰面积计算 peak detection peak area 源代码 下载

原文:峰识别 峰面积计算 peak detection peak area 源代码 下载Comparative analysis of peak-detection techniques for comprehensive two-dimensional chromatography http://www.docin.com/p-172045359.html http://terpconnect.umd.edu/~toh/spectrum/ipeak.html R…

区块链开发公司谈区块链与大数据的关系

在过去的两千多年的时间长河中&#xff0c;数字一直指引着我们去探索很多未知的科学世界。到目前为止&#xff0c;随着网络和信息技术的发展&#xff0c;一切与人类活动相关的活动&#xff0c;都直接或者间接的连入了互联网之中&#xff0c;一个全新的数字化的世界展现在我们的…

Jupyter Notebook的15个技巧和窍门,可简化您的编码体验

Jupyter Notebook is a browser bases REPL (read eval print loop) built on IPython and other open-source libraries, it allows us to run interactive python code on the browser.Jupyter Notebook是基于IPL和其他开源库构建的基于REPL(读取评估打印循环)的浏览器&#…

给定有权无向图的邻接矩阵如下,求其最小生成树的总权重,代码。

#include<bits/stdc.h> using namespace std; #define INF 0x3f3f3f3f const int maxn 117; int m[maxn][maxn]; int vis[maxn], low[maxn]; /* 对于这道题目来将&#xff0c;m就是临接矩阵&#xff0c;vis是访问标记数组&#xff0c;low是最短距离数组 */ int n; int …

Ubuntu-16-04-编译-Caffe-SSD

该来的还是要来 之前为了偷懒想到使用 Docker 回避 Caffe SSD 编译的难题。结果&#xff0c;「天道好轮回&#xff0c;苍天饶过谁」。Docker 镜像内无法调用 GUI 显示以及摄像头&#xff0c;没法跑 ssd_pascal_webcam.py 做实时 Object Detection。所以没办法又得重新尝试编译 …

bi数据分析师_BI工程师和数据分析师的5个格式塔原则

bi数据分析师Image by Author图片作者 将美丽融入数据 (Putting the Beauty in Data) Have you ever been ravished by Vizzes on Tableau Public that look like only magic could be in play to display so much data in such a pleasing way?您是否曾经被Tableau Public上的…

BSOJ 2423 -- 【PA2014】Final Zarowki

Description 有n个房间和n盏灯&#xff0c;你需要在每个房间里放入一盏灯。每盏灯都有一定功率&#xff0c;每间房间都需要不少于一定功率的灯泡才可以完全照亮。 你可以去附近的商店换新灯泡&#xff0c;商店里所有正整数功率的灯泡都有售。但由于背包空间有限&#xff0c;你…

WPF绑定资源文件错误(error in binding resource string with a view in wpf)

报错&#xff1a;无法将“***Properties.Resources.***”StaticExtension 值解析为枚举、静态字段或静态属性 解决办法&#xff1a;尝试右键单击在Visual Studio解决方案资源管理器的资源文件&#xff0c;并选择属性选项&#xff0c;然后设置自定义工具属性 PublicResXFile cod…

因果推论第六章

因果推论 (Causal Inference) This is the sixth post on the series we work our way through “Causal Inference In Statistics” a nice Primer co-authored by Judea Pearl himself.这是本系列的第六篇文章&#xff0c;我们将通过Judea Pearl本人与他人合着的《引诱统计学…

如何优化网站加载时间

一、背景 我们要监测网站的加载情况&#xff0c;可以使用 window.performance 来简单的检测。 window.performance 是W3C性能小组引入的新的API&#xff0c;目前IE9以上的浏览器都支持。一个performance对象的完整结构如下图所示&#xff1a; memory字段代表JavaScript对内存的…

VMWARE VCSA 6.5安装过程

https://www.tech-coffee.net/step-by-step-deploy-vcenter-server-appliance-vcsa-6-5/ vcsa 6.0&#xff0c;6.5 注册机下载 链接:https://pan.baidu.com/s/1X5V-iWpvxozrwE7Ji099jw 密码:jt8l 转载于:https://www.cnblogs.com/flyhgx/p/9073485.html