题目链接
寒假档……几天才做一道题,这道题是一个线段树合并的模板题,考虑到有1e5种颜色,不如就把颜色看作是区间长度,然后我们只需要去填补每个点的区间长度就行了,那我们以此对每个点去建立线段树。
从头节点开始,知道后面的所有节点,我们建立边,去求出LCA的处理,是为了在树上做差分的思想,我们可以对u、v点进行"+1"处理,对lca(u, v)、root[lca][0](也就是LCA的父节点)进行"-1"处理,我们就可以知道这一条边上所有点的值了。
去存下所有上述的四个端点的覆盖问题,然后建立线段树,我们从叶子节点往上处理,再之后,我们遇到的所有节点都需要去合并它的子节点,以此,建立完整棵线段树。
我们要找寻的答案是什么呢?那么不就是改点旗下的线段树的最大值吗!
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
const int all_Color_In = 1e5;
int N, M, head[maxN], cnt, root[maxN][18], deep[maxN], ans[maxN];
struct Eddge
{
int nex, to;
Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
void addEddge(int u, int v)
{
edge[cnt] = Eddge(head[u], v);
head[u] = cnt++;
}
void LCA_dfs(int u, int fa, int depth)
{
root[u][0] = fa;
deep[u] = depth;
for(int i=head[u], v; ~i; i=edge[i].nex)
{
v = edge[i].to;
if(v == fa) continue;
LCA_dfs(v, u, depth + 1);
}
}
void LCA_Pre()
{
LCA_dfs(1, -1, 0);
for(int j=0; (1<<(j + 1))<N; j++)
{
for(int i=1; i<=N; i++)
{
if(root[i][j] < 0) root[i][j+1] = -1;
else root[i][j+1] = root[root[i][j]][j];
}
}
}
int Get_LCA(int x, int y)
{
if(deep[x] < deep[y]) swap(x, y);
int det = deep[x] - deep[y];
for(int i=0; (1<<i)<=det; i++) if((det>>i) & 1) x = root[x][i];
if(x == y) return x;
for(int i=log2(1.*N); i>=0; i--)
{
if(root[x][i] != root[y][i])
{
x = root[x][i];
y = root[y][i];
}
}
return root[x][0];
}
struct data
{
int id, val;
data(int a=0, int b=0):id(a), val(b) {}
friend bool operator < (data e1, data e2) { return e1.val == e2.val ? e1.id > e2.id : e1.val < e2.val; }
friend data operator + (data e1, data e2) { return data(e1.id, e1.val + e2.val); }
};
vector<data> vt[maxN]; //存每个点的旗下的全体颜色以及状态,用差分的思想
struct Tree //动态开线段树合并
{
int lson[maxN*30], rson[maxN*30], tot;
data tree[maxN*30];
Tree() { tot = N; memset(lson, 0, sizeof(lson)); memset(rson, 0, sizeof(rson)); }
void clear() { tot = N; memset(lson, 0, sizeof(lson)); memset(rson, 0, sizeof(rson)); }
void insert(int rt, int l, int r, data val) //插入这样的点
{
if(l == r)
{
tree[rt] = val + tree[rt]; //因为val是一定有值的所以要把val放在前面,跟重载加号的方式有关
return;
}
int mid = HalF;
if(val.id <= mid) insert(lson[rt] = lson[rt] ? lson[rt] : ++tot, l, mid, val);
else insert(rson[rt] = rson[rt] ? rson[rt] : ++tot, mid+1, r, val);
tree[rt] = max(tree[lson[rt]], tree[rson[rt]]);
}
void merge(int p1, int p2, int l, int r) //合并值
{
if(l == r)
{
tree[p1] = tree[p1] + tree[p2];
return;
}
int mid = HalF;
if(lson[p1] && lson[p2]) merge(lson[p1], lson[p2], l, mid);
else if(lson[p2]) lson[p1] = lson[p2];
if(rson[p1] && rson[p2]) merge(rson[p1], rson[p2], mid+1, r);
else if(rson[p2]) rson[p1] = rson[p2];
tree[p1] = max(tree[lson[p1]], tree[rson[p1]]);
}
}King;
void Tree_dfs(int u)
{
for(int i=head[u], v; ~i; i=edge[i].nex)
{
v = edge[i].to;
if(v == root[u][0]) continue;
Tree_dfs(v);
King.merge(u, v, 1, all_Color_In);
}
int len = (int)vt[u].size();
for(int i=0; i<len; i++) King.insert(u, 1, all_Color_In, vt[u][i]);
ans[u] = King.tree[u].id;
}
inline void init()
{
cnt = 0;
memset(head, -1, sizeof(head));
memset(root, -1, sizeof(root));
// King.clear();
King.tot = N;
}
int main()
{
scanf("%d%d", &N, &M);
init();
for(int i=1, e1, e2; i<N; i++)
{
scanf("%d%d", &e1, &e2);
addEddge(e1, e2);
addEddge(e2, e1);
}
LCA_Pre();
for(int i=1, e1, e2, val, lca; i<=M; i++)
{
scanf("%d%d%d", &e1, &e2, &val);
lca = Get_LCA(e1, e2);
vt[e1].push_back(data(val, 1)); vt[e2].push_back(data(val, 1));
vt[lca].push_back(data(val, -1));
if(root[lca][0] >= 0) vt[root[lca][0]].push_back(data(val, -1));
}
Tree_dfs(1);
for(int i=1; i<=N; i++) printf("%d\n", ans[i]);
return 0;
}