Description

 给定一棵树,设计数据结构支持以下操作

    1 u v d  表示将路径 (u,v) 加d

    2 u v  表示询问路径 (u,v) 上点权绝对值的和

Input

第一行两个整数n和m,表示结点个数和操作数
接下来一行n个整数a_i,表示点i的权值

接下来n-1行,每行两个整数u,v表示存在一条(u,v)的边

接下来m行,每行一个操作,输入格式见题目描述

Output

对于每个询问输出答案

Sample Input

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

Sample Output

10
13
9

HINT

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

 
先树链剖分一下转化成序列问题。
然后考虑对于一个区间记录一下正数和负数的个数,那么区间增加就可以直接打上懒标记来维护区间和了。
但是这么做有一个问题,当一个负数被加成正数的时候,其正负性会改变。
我们注意到这道题有一个重要的性质:d>=0,即增加的数永远是正的。
这提示我们,每次修改可以暴力出即将变号的数的位置。
我们再记录一个maxv表示区间内最大负数的值,若区间内没有负数,则maxv=-inf。
然后对于每次修改,我们暴力dfs,直到当前区间的maxv+val<0为止。
这样均摊复杂度就是O(nlogn)的了。
那么总复杂度为O(mlog^n+nlogn)。
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
typedef long long ll;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=100010;
const ll inf=1ll<<60;
int n,m,val[maxn],first[maxn],next[maxn<<1],to[maxn<<1],e;
void AddEdge(int u,int v) {
to[++e]=v;next[e]=first[u];first[u]=e;
to[++e]=u;next[e]=first[v];first[v]=e;
}
int dep[maxn],fa[maxn],siz[maxn],son[maxn];
void dfs(int x) {
dep[x]=dep[fa[x]]+1;siz[x]=1;
ren if(to[i]!=fa[x]) {
fa[to[i]]=x;dfs(to[i]);
siz[x]+=siz[to[i]];
if(siz[to[i]]>siz[son[x]]) son[x]=to[i];
}
}
int top[maxn],st[maxn],pos[maxn],ToT;
void build(int x,int tp) {
top[x]=tp;st[x]=++ToT;pos[ToT]=x;
if(son[x]) build(son[x],tp);
ren if(to[i]!=fa[x]&&to[i]!=son[x]) build(to[i],to[i]);
}
ll sumv[maxn<<2],addv[maxn<<2],maxv[maxn<<2],ctot[maxn<<2];
void maintain(int o,int l,int r) {
int lc=o<<1,rc=lc|1;
sumv[o]=sumv[lc]+sumv[rc];
maxv[o]=max(-inf,max(maxv[lc],maxv[rc]));
ctot[o]=ctot[lc]+ctot[rc];
if(addv[o]) sumv[o]+=ctot[o]*addv[o],maxv[o]+=addv[o];
}
void pushdown(int o) {
if(addv[o]) {
int lc=o<<1,rc=lc|1;
addv[lc]+=addv[o];addv[rc]+=addv[o];
sumv[lc]+=addv[o]*ctot[lc];sumv[rc]+=addv[o]*ctot[rc];
maxv[lc]+=addv[o];maxv[rc]+=addv[o];
addv[o]=0;
}
}
void build(int o,int l,int r) {
if(l==r) {
sumv[o]=abs(val[pos[l]]);
maxv[o]=val[pos[l]]<0?val[pos[l]]:-inf;
ctot[o]=val[pos[l]]>=0?1:-1;
}
else {
int mid=l+r>>1,lc=o<<1,rc=lc|1;
build(lc,l,mid);build(rc,mid+1,r);
maintain(o,l,r);
}
}
ll query(int o,int l,int r,int ql,int qr) {
if(ql<=l&&r<=qr) return sumv[o];
else {
pushdown(o);ll res=0;
int mid=l+r>>1,lc=o<<1,rc=lc|1;
if(ql<=mid) res+=query(lc,l,mid,ql,qr);
if(qr>mid) res+=query(rc,mid+1,r,ql,qr);
return res;
}
}
void update(int o,int l,int r,int ql,int qr,int v) {
if(ql<=l&&r<=qr) {
if(maxv[o]+v>=0) {
if(l==r) {
addv[o]=0;sumv[o]=v-sumv[o];
maxv[o]=-inf;ctot[o]=1;
}
else {
pushdown(o);int mid=l+r>>1,lc=o<<1,rc=lc|1;
update(lc,l,mid,ql,qr,v);update(rc,mid+1,r,ql,qr,v);
maintain(o,l,r);
}
}
else {
if(l<r) addv[o]+=v,maintain(o,l,r);
else sumv[o]+=ctot[o]*v,maxv[o]+=v;
}
}
else {
pushdown(o);int mid=l+r>>1,lc=o<<1,rc=lc|1;
if(ql<=mid) update(lc,l,mid,ql,qr,v);
if(qr>mid) update(rc,mid+1,r,ql,qr,v);
maintain(o,l,r);
}
}
void query(int x,int y) {
int f1=top[x],f2=top[y];ll ans=0;
while(f1!=f2) {
if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
ans+=query(1,1,n,st[f1],st[x]);
x=fa[f1];f1=top[x];
}
if(dep[x]>dep[y]) swap(x,y);
printf("%lld\n",ans+query(1,1,n,st[x],st[y]));
}
void update(int x,int y,int v) {
int f1=top[x],f2=top[y];
while(f1!=f2) {
if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
update(1,1,n,st[f1],st[x],v);
x=fa[f1];f1=top[x];
}
if(dep[x]>dep[y]) swap(x,y);
update(1,1,n,st[x],st[y],v);
}
int main() {
n=read();m=read();
rep(i,1,n) val[i]=read();
rep(i,2,n) AddEdge(read(),read());
dfs(1);build(1,1);
build(1,1,n);
while(m--) {
int t=read(),x=read(),y=read();
if(t==1) update(x,y,read());
else query(x,y);
}
return 0;
}

  

BZOJ4127: Abs的更多相关文章

  1. bzoj4127 Abs 树链剖分+线段树+均摊分析

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4127 题解 首先区间绝对值和可以转化为 \(2\) 倍的区间正数和 \(-\) 区间和.于是问 ...

  2. 【BZOJ-4127】Abs 树链剖分 + 线段树 (有趣的姿势)

    4127: Abs Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 381  Solved: 132[Submit][Status][Discuss] ...

  3. 【bzoj4127】Abs 树链剖分+线段树

    题目描述 给定一棵树,设计数据结构支持以下操作 1 u v d 表示将路径 (u,v) 加d 2 u v 表示询问路径 (u,v) 上点权绝对值的和 输入 第一行两个整数n和m,表示结点个数和操作数 ...

  4. [bzoj4127]Abs_树链剖分_线段树

    Abs bzoj-4127 题目大意:给定一棵数,支持链加和链上权值的绝对值的和. 注释:$1\le n,m \le 10^5$,$\delta \ge 0$,$|a_i|\le 10^8$. 想法: ...

  5. Math.abs()方法 取绝对值

    定义和用法 abs() 方法可返回数的绝对值. 语法 Math.abs(x) 参数 描述 x 必需.必须是一个数值. 返回值 x 的绝对值. 实例 在本例中,我将取得正数和负数的绝对值: <sc ...

  6. 实时控制软件设计作业_01——汽车ABS系统分析

    制动防抱死系统(antilock brake system)简称ABS.作用就是在汽车制动时,自动控制制动器制动力的大小,使车轮不被抱死,处于边滚边滑(滑移率在20%左右)的状态,以保证车轮与地面的附 ...

  7. 实时控制软件设计第一周作业-汽车ABS软件系统案例分析

    汽车ABS软件系统案例分析 ABS 通过控制作用于车轮制动分泵上的制动管路压力,使汽车在紧急刹车时车轮不会抱死,这样就能使汽车在紧急制动时仍能保持较好的方向稳定性. ABS系统一般是在普通制动系统基础 ...

  8. 汽车ABS系统-第一周作业

    ABS系统也成防抱死系统(Anti-lock Braking System),由罗伯特·博世有限公司所开发的一种在摩托车和汽车中使用,它会根据各车轮角速度信号,计算得到车速.车轮角减速度.车轮滑移率: ...

  9. all ,any,abs的使用

    #!/usr/bin/env python #all循环参数,如果每个元素都为真,那么all的返回值为真 r = all([True,'sad','asd']) print(r) #any 只有一个真 ...

随机推荐

  1. 【JAVA网络流之浏览器与服务器模拟】

    一.模拟服务器获取浏览器请求http信息 代码: package p06.TCPTransferImitateServer.p01.ImitateServer; import java.io.IOEx ...

  2. Oracle buffer cache

    Buffer Cache buffer cache 结构图 HASH链 ORACLE使用HASH算法,把buffer cache中每个buffer的buffer header串联起来,组成多条hash ...

  3. JetBrains发布了IntelliJ IDEA 2016.1

    JetBrains日前发布了IntelliJ IDEA 2016.1,即他们最受欢迎的IDE的最新版本.这个新版本应该是考虑了多语言开发者的需求,其在许多语言和技术都有很多的增强:然而最惹人注目的变化 ...

  4. 使用windows的远程桌面连接连接Ubuntu

    想起来用笔记本连接一个windows server时只需要在远程桌面连接里面输入一下ip地址然后账号密码就可以了,十分简单.于是乎既然装了个Ubuntu当服务器使那么我就业来远程连接一下,由于wind ...

  5. uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型 - 大总结,看完全明白了

    转自:http://blog.csdn.net/kiddy19850221/article/details/6655066 uint8_t / uint16_t / uint32_t /uint64_ ...

  6. vim实现全选功能

    转自:http://blog.csdn.net/csh159/article/details/7533872 曾经也在找看看有没有快捷的方法全选,但是网上很多都是重复,并且错误的,比如: 1,$y,这 ...

  7. 在Asp.Net MVC中用Ajax回调后台方法

    在Asp.Net MVC中用Ajax回调后台方法基本格式: var operData = ...; //传递的参数(action中定义的) var type = ...; //传递的参数(action ...

  8. NSFileManager 的基本使用方法

    本方法已有个人总结, int main(int argc, const char * argv[]) { @autoreleasepool { NSString *path=@"/Users ...

  9. 硬件断点 DrxHook

    硬件断点 DrxHook 硬件断点的实现需要依赖于调试寄存器 DR0~DR7  调试寄存器 DR0~DR3-----调试地址寄存器DR4~DR5-----保留DR6 -----调试状态寄存器 指示哪个 ...

  10. 使用android ProgressBar和Toast生成一个界面

    首先我需要这样一个界面 这个界面是在使用AudioManager.adjustStreamVolume(int streamType, int direction, int flags)显示出来的,记 ...