B - Housewife Wind POJ - 2763 + tree sectional right side is converted into the right point

B - Housewife Wind

 POJ - 2763 

Because the tree cross + segment tree can only solve the problem right point, so this topic to the right side of the general right to be converted into points.

After I know this topic is very simple.

How transformed it, put the right side into greater depth between the two points on it.

Also note that this point right common ancestor to be subtracted.

#include <cstdio> 
#include <CString> 
#include <algorithm> 
#include <the iostream> 
#include <algorithm> 
#include <the cstdlib> 
#include <Vector> 
#include <Stack> 
#include <Map> 
#include <String> 
INF 0x3f3f3f3f #define 
#define inf64 0x3f3f3f3f3f3f3f3f 
the using namespace STD; 
typedef Long Long LL; 
const int = 4E5 + MAXN 10; 

int F [MAXN]; // save u parent node F 
int dep [maxn]; // dep storage node u depth 
int siz [maxn]; // siz stored in child nodes of the root of the number of u 
int son [maxn]; // son son save weight u 
int rk [maxn]; // rk in this order dfs the tree node corresponding 
int top [maxn]; // top node saves the current node to the top strand where 
int id [maxn]; // dfs execution order of 

int a [maxn], n; 
ll sum [maxn * 4];
//------------------线段树部分---------------//
void push_up(int id) {
	sum[id] = sum[id << 1] + sum[id << 1 | 1];
}

void build(int id, int l, int r) {
	if (l == r) {
		sum[id] = a[rk[l]];
		return;
	}
	int mid = (l + r) >> 1;
	build(id << 1, l, mid);
	build(id << 1 | 1, mid + 1, r);
	push_up(id);
}

void update(int id, int l, int r, int pos, int val) {
	if (l == r) {
		sum[id] = val;
		return;
	}
	int mid = (l + r) >> 1;
	if (pos <= mid) update(id << 1, l, mid, pos, val);
	else  update(id << 1 | 1, mid + 1, r, pos, val);
	push_up (ID); 
} 

LL Query (ID int, int L, R & lt int, int X, Y int) { 
	// the printf ( "ID =% DR = DL =%%% Dy DX = D =% \ n-", ID, L, R & lt, X, Y); 
// int top [maxn]; // top chain save the current node to the top node located
	IF (X <Y && = L> = R & lt) return SUM [ID]; 
	int MID = (L + R & lt) >>. 1; 
	LL ANS = 0; 
	IF (X <= MID) ANS = (Query + ANS (ID . 1 <<, L, MID, X, Y)); 
	IF (Y> MID) ANS = (Query + ANS (ID <<. 1 |. 1,. 1 + MID, R & lt, X, Y)); 
	return ANS; 
} 



------------------- // ------------------------ tree chain split / / 
// int F [MAXN]; // save F u parent node 
// int dep [maxn]; // dep depth storage node u 
// int siz [maxn]; // siz u is stored in the root number of child nodes 
// int son [maxn]; // son son save weight u 
// int rk [maxn]; // rk node in the tree dfs sequence corresponding to the current 
// int id [maxn]; // dfs execution sequence of 
struct Node { 
	int V, NXT; 
	Node (= 0 V int, int NXT = 0): V (V), NXT (NXT) {} 
} EX [MAXN]; 
int head [MAXN], 0 = CNT, TOT; 
void the init () {
	cnt = 0, tot = 0;
	memset(son, 0, sizeof(son));
	memset(head, -1, sizeof(head));
}
void add(int u, int v, int w) {
	ex[cnt] = node(v, head[u]);;
	head[u] = cnt++;
	ex[cnt] = node(u, head[v]);
	head[v] = cnt++;
}


void dfs1(int u, int fa, int depth) {
	f[u] = fa; dep[u] = depth; siz[u] = 1;
	for (int i = head[u]; i != -1; i = ex[i].nxt) {
		int v = ex[i].v;
		if (v == fa) continue;
		dfs1(v, u, depth + 1);
		siz[u] += siz[v];
		IF (SIZ [V]> SIZ [Son [U]]) Son [U] = V; 
	} 
} 

void DFS2 (int U, int T) { 
	Top [U] = T; 
	IF (DEP [X]> DEP [Y]) the swap (X, Y);
	id[u] = ++tot;//标记dfs序
	rk[tot] = u;//序号tot对应的结点u
	IF return (Son [U]!); 
	DFS2 (Son [U], T); 
	/ * We selected priority access weight son to ensure that all nodes dfs sequence on the heavy chain is continuous, 
	a point, and its weight son in the same heavy chain, the top or t son's weight heavy chain * / 
	for (int I = head [U]; I = -1;! = I EX [I] .nxt) { 
		int = V EX [I] .v ; 
		IF (!! V Son = [U] && V F = [U]) DFS2 (V, V); // a point at the bottom end of the light chain, then it must be the top of its own 
	} 
} 

LL of query2 (int X, int Y) { 
	LL RET = 0; 
	the while (Top [X] = Top [Y]) {! 
		IF (DEP [Top [X]] <DEP [Top [Y]]) the swap (X, Y); 
		RET = (RET + Query (. 1,. 1, n-, ID [Top [X]], ID [X])); 
		X = F [Top [X]]; 
	} 
	RET = RET + Query (. 1,. 1, n- , ID [X], ID [Y]) - Query (. 1,. 1, n-, ID [X], ID [X]); 
	return RET; 
} 

// ------------- ----- chain tree split end ------------------- //

struct heapnode {
	int u, v, w;
	heapnode(int u = 0, int v = 0, int w = 0) :u(u), v(v), w(w) {}
}exa[maxn];

int main() {
	
	int m, s;
	while(scanf("%d%d%d", &n, &m, &s)!=EOF)
	{
		init();
		for (int i = 1; i < n; i++) {
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			add(u, v, w);
			exa[i] = heapnode(u, v, w);
		}
		dfs1(1, -1, 1), dfs2(1, 1);
		for (int i = 1; i < n; i++) {
			heapnode now = exa[i];
			if (dep[now.u] > dep[now.v]) swap(now.u, now.v);
			a[now.v] = now.w;
		}
		build(1, 1, n);
		while (m--) {
			int opt, i, w, u;
			scanf("%d", &opt);
			if (opt == 0) {
				scanf("%d", &u);
				int ans = query2(s, u), x;
				printf("%d\n", ans);
				s = u;
			}
			else {
				scanf("%d%d", &i, &w);
				heapnode now = exa[i];
				if (dep[now.u] > dep[now.v]) swap(now.u, now.v);
				int num = now.v;
				update(1, 1, n, id[num], w);
			}
		}
	}
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/EchoZQN/p/11346398.html