线段树合并学习笔记
Index_SomeOne · · 算法·理论
启发式合并
待完成
线段树合并
通常用于动态开点线段树,可以合并两颗线段树的信息。
思想
线段树合并的思想同样十分暴力。 对于一个动态开点线段树,存在一些子树只有一棵树中包含,对于这些子树我们直接将信息向上传,不进行遍历。
而剩下的点剩下的点直接暴力合并加更新。
复杂度证明
这是一个看似十分暴力的算法,但其复杂度是由保证的。
对于线段树
考虑合并两颗线段树对势能产生的影响:
而我们实际消耗时间为
而在合并前的修改操作,在修改的同时提升了势能。所以一个总点数为
实现
以 P4556 为例。
本题的在一条路径上加可以按照差分的方式处理。例如在从
可以使用线段树维护单点的信息,最后查询答案时合并子树内线段树的信息就是该点的信息。
参考代码
#include<bits/stdc++.h>
#define N 100000
#define Ins (-1000000000)
using namespace std;
struct Val{int maxn,p;};
struct Node{int l,r;Val v;};
struct Chan{int f,hs,size,top,dep;};
int n,m,uu,uv,tot=0,root[N+5],ux,uy,uz,lc,ans[N+5];
Node tree[N*80+5];
Chan chan[N+5];
vector<int>edge[N+5];
int read(){
int f=1,g=0;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while('0'<=ch&&ch<='9'){
g=g*10+ch-'0';
ch=getchar();
}
return f*g;
}
void print(int x){
if(x<0){
putchar('-');
x*=-1;
}
if(x>9)print(x/10);
putchar(x%10+'0');
return;
}
inline Val max(Val x,Val y){
if(x.maxn>y.maxn||x.maxn==y.maxn&&x.p<y.p)return x;
return y;
}
int newNode(){
int p=++tot;
tree[p].l=tree[p].r=0;
tree[p].v={0,0};
return p;
}
void pushup(int x){tree[x].v=max(tree[tree[x].l].v,tree[tree[x].r].v);return;}
void modify(int l,int r,int c,int v,int &p){
if(!p)p=newNode();
if(l==r){
tree[p].v.maxn+=v;
tree[p].v.p=l;
return;
}
int mid=(l+r)>>1;
if(c<=mid)modify(l,mid,c,v,tree[p].l);
if(mid<c)modify(mid+1,r,c,v,tree[p].r);
pushup(p);
return;
}
void merge(int l,int r,int &p,int q){
if(!p||!q){
p=p|q;
return;
}
if(l==r){
tree[p].v.maxn+=tree[q].v.maxn;
return;
}
int mid=(l+r)>>1;
merge(l,mid,tree[p].l,tree[q].l);
merge(mid+1,r,tree[p].r,tree[q].r);
pushup(p);
return;
}
void build1(int x,int dep){
chan[x].dep=dep,chan[x].size=1;
for(int i:edge[x]){
if(i==chan[x].f)continue;
chan[i].f=x,build1(i,dep+1);
chan[x].size+=chan[i].size;
if(chan[i].size>chan[chan[x].hs].size)chan[x].hs=i;
}
return;
}
void build2(int x,int top){
chan[x].top=top;
if(chan[x].hs)build2(chan[x].hs,top);
for(int i:edge[x]){
if(i==chan[x].f||i==chan[x].hs)continue;
build2(i,i);
}
return;
}
int lca(int x,int y){
while(chan[x].top!=chan[y].top){
if(chan[chan[x].top].dep<chan[chan[y].top].dep)swap(x,y);
x=chan[chan[x].top].f;
}
if(chan[x].dep<chan[y].dep)return x;
return y;
}
void printTree(int x,int l,int r){
if(!x)return;
cout<<"l="<<l<<" r="<<r<<" tree["<<x<<"].l="<<tree[x].l<<" tree["<<x<<"].r="<<tree[x].r<<" tree["<<x<<"].v={"<<tree[x].v.maxn<<","<<tree[x].v.p<<"}\n";
if(l==r)return;
int mid=(l+r)>>1;
printTree(tree[x].l,l,mid);
printTree(tree[x].r,mid+1,r);
return;
}
void dfs(int x){
for(int i:edge[x]){
if(i==chan[x].f)continue;
dfs(i);
merge(1,N,root[x],root[i]);
}
ans[x]=max(tree[root[x]].v,{0,0}).p;
return;
}
main(){
tree[0].v={Ins,0};
n=read(),m=read();
for(int i=1;i<n;i++){
uu=read(),uv=read();
edge[uu].push_back(uv);
edge[uv].push_back(uu);
}
build1(1,1);
build2(1,1)
for(int i=1;i<=m;i++){
ux=read(),uy=read(),uz=read();
lc=lca(ux,uy);
modify(1,N,uz,1,root[ux]);
modify(1,N,uz,1,root[uy]);
modify(1,N,uz,-1,root[lc]);
if(chan[lc].f)modify(1,N,uz,-1,root[chan[lc].f]);
}
dfs(1);
for(int i=1;i<=n;i++){
print(ans[i]),putchar('\n');
}
return 0;
}