# 【bzoj3307】雨天的尾巴 权值线段树合并

N个点，形成一个树状结构。有M次发放，每次选择两个点x,y，对于x到y的路径上（含x,y）每个点发一袋Z类型的物品。完成所有发放后，每个点存放最多的是哪种物品。

3 2
1 2
1 3
1 3 1
2 3 2

1
2
1

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
using namespace std;
int head[N] , to[N << 1] , next[N << 1] , cnt , fa[N][18] , deep[N] , log[N] , x[N] , y[N] , z[N] , v[N];
int m , ls[N * 60] , rs[N * 60] , mx[N * 60] , mp[N * 60] , root[N] , tot , ans[N];
void add(int x , int y)
{
}
void dfs(int x)
{
int i;
for(i = 1 ; (1 << i) <= deep[x] ; i ++ ) fa[x][i] = fa[fa[x][i - 1]][i - 1];
for(i = head[x] ; i ; i = next[i])
if(to[i] != fa[x][0])
fa[to[i]][0] = x , deep[to[i]] = deep[x] + 1 , dfs(to[i]);
}
int lca(int x , int y)
{
int i;
if(deep[x] < deep[y]) swap(x , y);
for(i = log[deep[x] - deep[y]] ; ~i ; i -- )
if((1 << i) <= deep[x] - deep[y])
x = fa[x][i];
for(i = log[deep[x]] ; ~i ; i -- )
if((1 << i) <= deep[x] && fa[x][i] != fa[y][i])
x = fa[x][i] , y = fa[y][i];
return x == y ? x : fa[x][0];
}
void pushup(int x)
{
if(mx[ls[x]] >= mx[rs[x]]) mx[x] = mx[ls[x]] , mp[x] = mp[ls[x]];
else mx[x] = mx[rs[x]] , mp[x] = mp[rs[x]];
}
void update(int p , int a , int l , int r , int &x)
{
if(!x) x = ++tot;
if(l == r)
{
mx[x] += a , mp[x] = p;
return;
}
int mid = (l + r) >> 1;
if(p <= mid) update(p , a , l , mid , ls[x]);
else update(p , a , mid + 1 , r , rs[x]);
pushup(x);
}
int merge(int l , int r , int x , int y)
{
if(!x) return y;
if(!y) return x;
if(l == r)
{
mx[x] += mx[y];
return x;
}
int mid = (l + r) >> 1;
ls[x] = merge(l , mid , ls[x] , ls[y]);
rs[x] = merge(mid + 1 , r , rs[x] , rs[y]);
pushup(x);
return x;
}
void solve(int x)
{
int i;
for(i = head[x] ; i ; i = next[i])
if(to[i] != fa[x][0])
solve(to[i]) , root[x] = merge(1 , m , root[x] , root[to[i]]);
if(mx[root[x]]) ans[x] = v[mp[root[x]]];
}
int main()
{
int n , i , a , b;
scanf("%d%d" , &n , &m);
for(i = 2 ; i <= n ; i ++ ) scanf("%d%d" , &a , &b) , add(a , b) , add(b , a) , log[i] = log[i >> 1] + 1;
dfs(1);
for(i = 1 ; i <= m ; i ++ ) scanf("%d%d%d" , &x[i] , &y[i] , &z[i]) , v[i] = z[i];
sort(v + 1 , v + m + 1);
for(i = 1 ; i <= m ; i ++ )
{
z[i] = lower_bound(v + 1 , v + m + 1 , z[i]) - v , a = lca(x[i] , y[i]);
update(z[i] , 1 , 1 , m , root[x[i]]) , update(z[i] , 1 , 1 , m , root[y[i]]);
update(z[i] , -1 , 1 , m , root[a]);
if(fa[a][0]) update(z[i] , -1 , 1 , m , root[fa[a][0]]);
}
solve(1);
for(i = 1 ; i <= n ; i ++ ) printf("%d\n" , ans[i]);
return 0;
}


posted @ 2017-08-05 10:59  GXZlegend  阅读(414)  评论(0编辑  收藏  举报