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.pswp.cn/news/389614.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389614.shtml
英文地址,請注明出處:http://en.pswp.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