USACO 2019 February Gold Cow Land (树链剖分)

题目要你支持:

  • 更改树上某一点 x x x 权值为 y y y
  • x x x y y y 的路径权值异或和

这一看就是树链剖分模板题么

用线段树维护单点修改,区间查询异或

然后树链剖分维护路径就搞定了

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std ;
#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 100010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

vi e[N] ;
int fa[N], dep[N], sz[N], son[N], dfn[N], seq[N], top[N], a[N] ;
int n, m, cnt ;

void dfs1(int x, int fat) {
	sz[x] = 1 ;
	rep(i, 0, siz(e[x]) - 1) {
		int to = e[x][i] ;
		if (to == fat) continue ;
		fa[to] = x ; dep[to] = dep[x] + 1 ;
		dfs1(to, x) ;
		if (sz[to] > sz[son[x]]) son[x] = to ;
		sz[x] += sz[to] ;
	}
}

void dfs2(int x, int tp) {
	dfn[x] = ++cnt ; seq[cnt] = x ;
	top[x] = tp ;
	if (!son[x]) return ;
	dfs2(son[x], tp) ;
	rep(i, 0, siz(e[x]) - 1) {
		int to = e[x][i] ;
		if (to == fa[x] || to == son[x]) continue ;
		dfs2(to, to) ;
	}
}

struct SegTree {
	int l, r, v ;
	#define ls(x) x << 1
	#define rs(x) x << 1 | 1
	#define l(x) tr[x].l
	#define r(x) tr[x].r
	#define v(x) tr[x].v
} tr[N << 2] ;

void pushup(int x) {
	v(x) = v(ls(x)) ^ v(rs(x)) ;
}

void build(int x, int l, int r) {
	l(x) = l, r(x) = r ;
	if (l == r) {
		v(x) = a[seq[l]] ;
		return ;
	}
	int mid = (l + r) >> 1 ;
	build(ls(x), l, mid) ;
	build(rs(x), mid + 1, r) ;
	pushup(x) ;
}

void modify(int x, int pos, int c) {
	if (l(x) == r(x)) {
		v(x) = c ;
		return ;
	}
	int mid = (l(x) + r(x)) >> 1 ;
	if (pos <= mid) modify(ls(x), pos, c) ;
	else modify(rs(x), pos, c) ;
	pushup(x) ;
}

int query(int x, int l, int r) {
	if (l <= l(x) && r(x) <= r) return v(x) ;
	int mid = (l(x) + r(x)) >> 1, ans = 0 ;
	if (l <= mid) ans ^= query(ls(x), l, r) ;
	if (mid < r) ans ^= query(rs(x), l, r) ;
	return ans ;
}

int Query(int x, int y) {
	int fx = top[x], fy = top[y], ans = 0 ;
	while (fx != fy) {
		if (dep[fx] < dep[fy]) swap(x, y), swap(fx, fy) ;
		ans ^= query(1, dfn[fx], dfn[x]) ;
		x = fa[fx] ; fx = top[x] ;
	}
	if (dep[x] > dep[y]) swap(x, y) ;
	return ans ^ query(1, dfn[x], dfn[y]) ;
}

signed main(){
	freopen("cowland.in", "r", stdin) ;
	freopen("cowland.out", "w", stdout) ;
	// 支持单点修改,查询路径,也就是树链剖分模板题
	scanf("%lld%lld", &n, &m) ;
	rep(i, 1, n) scanf("%lld", &a[i]) ;
	rep(i, 1, n - 1) {
		int a, b ; scanf("%lld%lld", &a, &b) ;
		e[a].pb(b) ; e[b].pb(a) ;
	}
	dfs1(1, -1) ;
	dfs2(1, 1) ;
	build(1, 1, n) ;
	while (m--) {
		int op, x, y ; scanf("%lld%lld%lld", &op, &x, &y) ;
		if (op == 1) modify(1, dfn[x], y) ;
		else printf("%lld\n", Query(x, y)) ;
	}
	return 0 ;
}

/*
写代码时请注意:
	1.ll?数组大小,边界?数据范围?
	2.精度?
	3.特判?
	4.至少做一些
思考提醒:
	1.最大值最小->二分?
	2.可以贪心么?不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构?
	5.统计方案是用dp?模了么?
	6.逆向思维?
*/


你可能感兴趣的:(比赛,线段树,树链剖分)