线段树合并 RE 不能运行求助
查看原帖
线段树合并 RE 不能运行求助
118196
zimujun楼主2021/3/28 14:46

RT

#include <bits/stdc++.h>

const int Maxn = 1e5 + 5;

template <typename Temp> inline void read(Temp & res) {
	Temp fh = 1; res = 0; char ch = getchar();
	for(; !isdigit(ch); ch = getchar()) if(ch == '-') fh = -1;
	for(; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ '0');
	res = res * fh;
}

int n, m, x, y, z;

//Edge
namespace E { 
	struct e {
		int to, nxt;
	} b[Maxn << 1];
	int head[Maxn], cnt;
	void add(int u, int v) {
		b[++cnt] = (e) {v, head[u]}; head[u] = cnt; 
	}
}
//Segment Tree (modify, Merge)
namespace S {
	struct Unit {
		int value, id, ls, rs;
		Unit() {value = id = ls = rs = 0;}
	} tr[Maxn << 8];
	int cnt, rt[Maxn];
	void pushup(int t) {
		if(!tr[t].rs) tr[t].value = tr[tr[t].ls].value, tr[t].id = tr[tr[t].ls].id;
		else if(!tr[t].ls) tr[t].value = tr[tr[t].rs].value, tr[t].id = tr[tr[t].rs].id;
		else if(tr[tr[t].ls].value >= tr[tr[t].rs].value) tr[t].value = tr[tr[t].ls].value, tr[t].id = tr[tr[t].ls].id;
		else tr[t].value = tr[tr[t].rs].value, tr[t].id = tr[tr[t].rs].id;
	}
	void modify(int pos, int d, int l, int r, int t) {
		if(l == r) {
			tr[t].value += d; tr[t].id = pos; return;
		}
		int mid = (l + r) >> 1;
		if(pos <= mid) modify(pos, d, l, mid, tr[t].ls ? tr[t].ls : tr[t].ls = ++cnt);
		else modify(pos, d, mid + 1, r, tr[t].rs ? tr[t].rs : tr[t].rs = ++cnt);
		pushup(t);
	}
	int Merge(int t, int s, int l, int r) {
		if(!t) return s;
		if(!s) return t;
		if(l == r) {
			tr[t].value += tr[s].value;
			return t;
		}
		int mid = (l + r) >> 1;
		tr[t].ls = Merge(tr[t].ls, tr[s].ls, l, mid);
		tr[t].rs = Merge(tr[t].rs, tr[s].rs, mid + 1, r);
		pushup(t); return t;
	}
}
//LCA (cut lca)
namespace L {
	int w[Maxn], top[Maxn], dep[Maxn], fa[Maxn], h[Maxn];
	void dfs_w(int t, int f) {
		dep[t] = dep[f] + 1; w[t] = 1;
		top[t] = t; fa[t] = f;
		for(int i = E::head[t]; i; i = E::b[i].nxt) {
			int tto = E::b[i].to;
			if(tto == f) continue;
			dfs_w(tto, t);
			w[t] += w[tto];
			if(w[tto] > w[h[t]]) h[t] = tto;
		}
	}
	void dfs_h(int t, int f) {
		top[t] = h[f] == t ? top[f] : t;
		for(int i = E::head[t]; i; i = E::b[i].nxt) {
			int tto = E::b[i].to;
			if(tto == f) continue;
			dfs_h(tto, t);
		}
	}
	int lca(int a, int b) {
		while(top[a] ^ top[b]) {
			if(dep[top[a]] < dep[top[b]]) a ^= b ^= a ^= b;
			a = fa[top[a]];
		}
		return dep[a] < dep[b] ? a : b;
	}
}

void dfs(int t, int fa) {
	for(int i = E::head[t]; i; i = E::b[i].nxt) {
		int tto = E::b[i].to;
		if(tto == fa) continue;
		dfs(tto, fa);
		S::Merge(S::rt[t], S::rt[tto], 1, 100000);
	}
}

int main() {
	read(n); read(m);
	for(int i = 1; i <= n; ++i) S::rt[i] = ++S::cnt;
	for(int i = 2; i <= n; ++i) read(x), read(y), E::add(x, y), E::add(y, x);
	L::dfs_w(1, 0); L::dfs_h(1, 0);
	while(m--) {
		
		read(x), read(y), read(z);
		S::modify(z, 1, 1, 100000, S::rt[x]);
		S::modify(z, 1, 1, 100000, S::rt[y]);
		x = L::lca(x, y);
		S::modify(z, -1, 1, 100000, S::rt[x]);
		S::modify(z, -1, 1, 100000, S::rt[L::fa[x]]);
	}
	dfs(1, 0);
	for(int i = 1; i <= n; ++i)
		printf("%d\n", S::tr[S::rt[i]].id);
	return 0;
}
2021/3/28 14:46
加载中...