[BZOJ3626] [LNOI2014] LCA 離線 樹鏈剖分

題面

考慮到詢問的\(l..r,z\)具有可減性,考慮把詢問差分掉,拆成\(r,z\)\(l-1,z\)

顯然這些LCA一定在\(z\)到根的路徑上。下面的問題就是怎么統計。

考慮不是那么暴力的暴力。

我們似乎可以把\(1..r\)的所有點先瞎搞一下,求出一個點內部有幾個\(1..r\)以內的點,記作\(w[i]\)。另假設\(fson[x]\)表示\(x\)的孩子中\(z\)這個點所在孩子

那么答案就是
\[ (\sum_{x\text{是$z$的祖先}} (w[x]-w[fson[x]])\cdot dep[x] ) + w[z]\cdot dep[z] \]
發現\(x\)有多少祖先,\(dep[x]\)就是幾。所以可以把\(w[x]-w[son[x]]\)放到其所有祖先上各統計一次。

這樣就發現,被減掉的\(w[fson[x]]?\)又會被后面的繼續加回來。

所以最終答案就是
\[ \sum_{x\text{是$z$的祖先}} w[x] \]
那么,如果維護這個\(w[x]\)呢?

從小到大循環\(1..n\),(也就是循環的是詢問的差分后的那個\(r\)指針)然后只需要把\(i\)的祖先都更新一個\(1\)就可以了,然后枚舉掛在\(i\)上的詢問,統計從\(z\)到根的路徑和。

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define REP(i,a,n) for(register int i(a);i<=(n);++i)
#define FEC(i,x,y) for(register int i=head[x],y=g[i].to;i;i=g[i].ne,y=g[i].to)
#define dbg(...) fprintf(stderr,__VA_ARGS__)
#define lc o<<1
#define rc o<<1|1
const int SZ=(1<<21)+1;char ibuf[SZ],*iS,*iT,obuf[SZ],*oS=obuf,*oT=obuf+SZ-1;
#ifdef ONLINE_JUDGE
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,SZ,stdin),(iS==iT?EOF:*iS++)):*iS++)
#else
#define gc() getchar()
#endif
template<typename I>inline void read(I&x){char c=gc();int f=0;for(;c<'0'||c>'9';c=gc())c=='-'?f=1:0;for(x=0;c>='0'&&c<='9';c=gc())x=(x<<1)+(x<<3)+(c&15);f?x=-x:0;}
inline void flush(){fwrite(obuf,1,oS-obuf,stdout);oS=obuf;}
#define printf(...) (iS>iT&&(flush(),1),oS+=sprintf(oS,__VA_ARGS__))
template<typename A,typename B>inline char SMAX(A&a,const B&b){return a<b?a=b,1:0;}
template<typename A,typename B>inline char SMIN(A&a,const B&b){return a>b?a=b,1:0;}
typedef long long ll;const int N=50000+7,P=201314;
int n,m,x,y,z,ans[N];
int dep[N],f[N],num[N],son[N],top[N],dfn[N],pre[N],dfc;
struct Edge{int to,ne;}g[N];int head[N],tot;
inline void Addedge(int x,int y){g[++tot].to=y;g[tot].ne=head[x];head[x]=tot;}
struct Questions{int opt,r,z,*ans;inline char operator<(const Questions&a)const{return r<a.r;}}q[N<<1];inline int SMOD(int x){return x>=P?x-P:x;}
inline void SADD(int&x,const int&y){x+=y;x>=P?x-=P:0;}
inline void SDEL(int&x,const int&y){x-=y;x<0?x+=P:0;}
inline void DFS1(int x,int fa=0){dep[x]=dep[fa]+1;f[x]=fa;num[x]=1;FEC(i,x,y)if(y!=fa){DFS1(y,x);num[x]+=num[y];if(num[y]>num[son[x]])son[x]=y;}
}
inline void DFS2(int x,int pa){top[x]=pa;dfn[x]=++dfc;pre[dfc]=x;if(!son[x])return;DFS2(son[x],pa);FEC(i,x,y)if(y!=f[x]&&y!=son[x])DFS2(y,y);
}
struct Node{int sum,add;}t[N<<2];
inline void Add(int o,int L,int R,int l,int r){if(l<=L&&R<=r)return SADD(t[o].add,1),SADD(t[o].sum,R-L+1);int M=(L+R)>>1;if(l<=M)Add(lc,L,M,l,r);if(r>M)Add(rc,M+1,R,l,r);t[o].sum=SMOD(t[lc].sum+t[rc].sum);SADD(t[o].sum,t[o].add*(R-L+1)%P);//錯誤筆記:Pushup的時候要記得更新標記 
}inline void Add(int l,int r){Add(1,1,n,l,r);}
inline int Sum(int o,int L,int R,int l,int r,int k=0){if(l<=L&&R<=r)return SMOD(t[o].sum+(ll)(R-L+1)*k%P);int M=(L+R)>>1;if(r<=M)return Sum(lc,L,M,l,r,SMOD(k+t[o].add));else if(l>M)return Sum(rc,M+1,R,l,r,SMOD(t[o].add+k));else return SMOD(Sum(lc,L,M,l,r,SMOD(k+t[o].add))+Sum(rc,M+1,R,l,r,SMOD(t[o].add+k)));
}inline int Sum(int l,int r){return Sum(1,1,n,l,r);}
inline void Modify(int x){while(top[x]!=1){Add(dfn[top[x]],dfn[x]);x=f[top[x]];}Add(dfn[1],dfn[x]);
}
inline int Query(int x){int ans=0;while(top[x]!=1){SADD(ans,Sum(dfn[top[x]],dfn[x]));x=f[top[x]];}return SMOD(ans+Sum(dfn[1],dfn[x]));
}int main(){read(n),read(m);REP(i,2,n)read(x),Addedge(x+1,i);REP(i,1,m)read(x),read(y),read(z),++x,++y,++z,q[(i<<1)-1]=Questions{1,y,z,ans+i},q[i<<1]=Questions{-1,x-1,z,ans+i};std::sort(q+1,q+(m<<1)+1);DFS1(1);DFS2(1,1);int p=1;while(p<=(m<<1)&&!q[p].r)++p;//錯誤筆記: 要跳過之前以0開頭沒有用的空詢問 REP(i,1,n){Modify(i);while(p<=(m<<1)&&q[p].r==i)~q[p].opt?SADD(*q[p].ans,Query(q[p].z)):SDEL(*q[p].ans,Query(q[p].z)),++p;}REP(i,1,m)printf("%d\n",ans[i]);return flush(),0;
}

轉載于:https://www.cnblogs.com/hankeke/p/BZOJ3626.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/387966.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387966.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387966.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux查看系統各類信息

說明&#xff1a;Linux下可以在/proc/cpuinfo中看到每個cpu的詳細信息。但是對于雙核的cpu&#xff0c;在cpuinfo中會看到兩個cpu。常常會讓人誤以為是兩個單核的cpu。其實應該通過Physical Processor ID來區分單核和雙核。而Physical Processor ID可以從cpuinfo或者dmesg中找到…

biopython中文指南_Biopython新手指南-第1部分

biopython中文指南When you hear the word Biopython what is the first thing that came to your mind? A python library to handle biological data…? You are correct! Biopython provides a set of tools to perform bioinformatics computations on biological data s…

整合后臺服務和驅動代碼注入

整合后臺服務和驅動代碼注入 Home鍵的驅動代碼&#xff1a; /dev/input/event1: 0001 0066 00000001 /dev/input/event1: 0000 0000 00000000 /dev/input/event1: 0001 0066 00000000 /dev/input/event1: 0000 0000 00000000 對應輸入的驅動代碼&#xff1a; sendevent/dev/…

Java作業09-異常

6. 為如下代碼加上異常處理 byte[] content null; FileInputStream fis new FileInputStream("testfis.txt"); int bytesAvailabe fis.available();//獲得該文件可用的字節數 if(bytesAvailabe>0){content new byte[bytesAvailabe];//創建可容納文件大小的數組…

為數據計算提供強力引擎,阿里云文件存儲HDFS v1.0公測發布

2019獨角獸企業重金招聘Python工程師標準>>> 在2019年3月的北京云棲峰會上&#xff0c;阿里云正式推出全球首個云原生HDFS存儲服務—文件存儲HDFS&#xff0c;為數據分析業務在云上提供可線性擴展的吞吐能力和免運維的快速彈性伸縮能力&#xff0c;降低用戶TCO。阿里…

對食材的敬畏之心極致產品_這些數據科學產品組合將給您帶來敬畏和啟發(2020年中的版本)

對食材的敬畏之心極致產品重點 (Top highlight)為什么選擇投資組合&#xff1f; (Why portfolios?) Data science is a tough field. It combines in equal parts mathematics and statistics, computer science, and black magic. As of mid-2020, it is also a booming fiel…

android模擬用戶輸入

目錄(?)[-] geteventsendeventinput keyevent 本文講的是通過使用代碼&#xff0c;可以控制手機的屏幕和物理按鍵&#xff0c;也就是說不只是在某一個APP里去操作&#xff0c;而是整個手機系統。 getevent/sendevent getevent&sendevent 是Android系統下的一個工具&#x…

真格量化常見報錯信息和Debug方法

1.打印日志 1.1 在代碼中添加運行到特定部分的提示&#xff1a; 如果我們在用戶日志未能看到“調用到OnQuote事件”文字&#xff0c;說明其之前的代碼就出了問題&#xff0c;導致程序無法運行到OnQuote函數里的提示部分。解決方案為仔細檢查該部分之前的代碼是否出現問題。 1.2…

向量積判斷優劣弧_判斷經驗論文優劣的10條誡命

向量積判斷優劣弧There are a host of pathologies associated with the current peer review system that has been the subject of much discussion. One of the most substantive issues is that results reported in leading journals are commonly papers with the most e…

自定義PopView

改代碼是參考一個Demo直接改的&#xff0c;代碼中有一些漏洞&#xff0c;如果發現其他的問題&#xff0c;可以下方直接留言 .h文件 #import <UIKit/UIKit.h> typedef void(^PopoverBlock)(NSInteger index); interface CustomPopView : UIView //property(nonatomic,copy…

線控耳機監聽

當耳機的媒體按鍵被單擊后&#xff0c;Android系統會發出一個廣播&#xff0c;該廣播的攜帶者一個Action名為MEDIA_BUTTON的Intent。監聽該廣播便可以獲取手機的耳機媒體按鍵的單擊事件。 在Android中有個AudioManager類&#xff0c;該類會維護MEDIA_BUTTON廣播的分發&#xf…

當編程語言掌握在企業手中,是生機還是危機?

2019年4月&#xff0c;Java的收費時代來臨了&#xff01; Java是由Sun微系統公司在1995年推出的編程語言&#xff0c;2010年Oracle收購了Sun之后&#xff0c;Java的所有者也就自然變成了Oracle。2019年&#xff0c;Oracle宣布將停止Java 8更新的免費支持&#xff0c;未來Java的…

sql如何處理null值_如何正確處理SQL中的NULL值

sql如何處理null值前言 (Preface) A friend who has recently started learning SQL asked me about NULL values and how to deal with them. If you are new to SQL, this guide should give you insights into a topic that can be confusing to beginners.最近開始學習SQL的…

名言警句分享

“當你想做一件事&#xff0c;卻無能為力的時候&#xff0c;是最痛苦的。”基拉大和轉載于:https://www.cnblogs.com/yuxijun/p/9986489.html

文字創作類App分享-簡書

今天我用Mockplus做了一套簡書App的原型&#xff0c;這是一款文字創作類的App&#xff0c;用戶通過寫文、點贊等互動行為&#xff0c;提高自己在社區的影響力&#xff0c;打造個人品牌。我運用了Mockplus基礎組件、交互組件、移動組件等多個組件庫&#xff0c;簡單拖拽&#xf…

數據可視化 信息可視化_動機可視化

數據可視化 信息可視化John Snow’s map of Cholera cases near London’s Broad Street.約翰斯諾(John Snow)在倫敦寬街附近的霍亂病例地圖。 John Snow, “the father of epidemiology,” is famous for his cholera maps. These maps represent so many of our aspirations …

android 接聽和掛斷實現方式

轉載▼標簽&#xff1a; android 接聽 掛斷 it 分類&#xff1a; android應用技巧 參考&#xff1a;android 來電接聽和掛斷 支持目前所有版本 注意&#xff1a;android2.3版本及以上不支持下面的自動接聽方法。 &#xff08;會拋異常&#xff1a;java.lang.Securi…

Eclipse External Tool Configration Notepad++

Location&#xff1a; C:\Program Files\Notepad\notepad.exe Arguments&#xff1a;  ${resource_loc} 轉載于:https://www.cnblogs.com/rgqancy/p/9987610.html

利用延遲關聯或者子查詢優化超多分頁場景

2019獨角獸企業重金招聘Python工程師標準>>> MySQL并不是跳過offset行&#xff0c;而是取offsetN行&#xff0c;然后返回放棄前offset行&#xff0c;返回N行&#xff0c;那當offset 特別大的時候&#xff0c;效率就非常的低下&#xff0c;要么控制返回的總頁數&…

客戶流失_了解客戶流失

客戶流失Big Data Analytics within a real-life example of digital music service數字音樂服務真實示例中的大數據分析 Customer churn is a key predictor of the long term success or failure of a business. It is the rate at which customers are leaving your busine…