HDU 5044 Tree(树链剖分)

HDU 5044 Tree

题目链接

就简单的树链剖分,不过坑要加输入外挂,还要手动扩栈

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 100005;

#pragma comment(linker, "/STACK:1024000000,1024000000")

void scanf_(int &num) {
	char in;
	bool neg=false;
	while(((in=getchar()) > '9' || in<'0') && in!='-') ;
	if(in=='-')
	{
		neg=true;
		while((in=getchar()) >'9' || in<'0');
	}
	num=in-'0';
	while(in=getchar(),in>='0'&&in<='9')
		num*=10,num+=in-'0';
	if(neg)
		num=0-num;
}

int dep[N], fa[N], son[N], sz[N], top[N], id[N], idx, val[N];
int first[N], next[N * 2], vv[N * 2], en;
int bit[2][N];

void init() {
	en = 0;
	idx = 0;
	memset(first, -1, sizeof(first));
	memset(bit, 0, sizeof(bit));
}

void add_Edge(int u, int v) {
	vv[en] = v;
	next[en] = first[u];
	first[u] = en++;
}

void dfs1(int u, int f, int d) {
	dep[u] = d;
	sz[u] = 1;
	fa[u] = f;
	son[u] = 0;
	for (int i = first[u]; i + 1; i = next[i]) {
		int v = vv[i];
		if (v == f) continue;
		dfs1(v, u, d + 1);
		sz[u] += sz[v];
		if (sz[son[u]] < sz[v])
			son[u] = v;
	}
}

void dfs2(int u, int tp) {
	id[u] = ++idx;
	top[u] = tp;
	if (son[u]) dfs2(son[u], tp);
	for (int i = first[u]; i + 1; i = next[i]) {
		int v = vv[i];
		if (v == fa[u] || v == son[u]) continue;
		dfs2(v, v);
	}
}

inline int lowbit(int x) {
	return x&(-x);
}

void add(int ty, int x, int v) {
	while (x < N) {
		bit[ty][x] += v;
		x += lowbit(x);
	}
}

void add(int ty, int l, int r, int v) {
	add(ty, l, v);
	add(ty, r + 1, -v);
}

int get(int ty, int x) {
	int ans = 0;
	while (x) {
		ans += bit[ty][x];
		x -= lowbit(x);
	}
	return ans;
}

void gao(int ty, int u, int v, int w) {
	int tp1 = top[u], tp2 = top[v];
	while (tp1 != tp2) {
		if (dep[tp1] < dep[tp2]) {
			swap(tp1, tp2);
			swap(u, v);
		}
		add(ty, id[tp1], id[u], w);
		u = fa[tp1];
		tp1 = top[u];
	}
	if (dep[u] > dep[v]) swap(u, v);
	if (ty == 1) {
		if (u != v)
			add(ty, id[son[u]], id[v], w);
	} else add(ty, id[u], id[v], w);
}

int T, n, m, s[N], t[N];

int main() {
	int cas = 0;
	scanf("%d", &T);
	while (T--) {
		init();
		scanf("%d%d", &n, &m);
		for (int i = 1; i < n; i++) {
			scanf_(s[i]); scanf_(t[i]);
			add_Edge(s[i], t[i]);
			add_Edge(t[i], s[i]);
		}
		dfs1(1, 0, 1);
		dfs2(1, 1);
		for (int i = 1; i < n; i++) {
			if (dep[s[i]] < dep[t[i]])
				swap(s[i], t[i]);
		}
		char Q[10];
		int u, v, k;
		while (m--) {
			scanf("%s", Q);
			scanf_(u); scanf_(v); scanf_(k);
			if (Q[3] == '1') gao(0, u, v, k);
			else gao(1, u, v, k);
		}
		printf("Case #%d:\n", ++cas);
		for (int i = 1; i <= n; i++)
			printf("%d%c", get(0, id[i]), i == n ? '\n' : ' ');
		for (int i = 1; i < n; i++)
			printf("%d%c", get(1, id[s[i]]), i == n - 1 ? '\n' : ' ');
		if (n == 1) printf("\n");
	}
	return 0;
}
时间: 2024-10-24 22:05:59

HDU 5044 Tree(树链剖分)的相关文章

HDU 5044 Tree 树链剖分

一棵树,初始边权和点权都为0 现在有m个操作,每一个操作: ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k. ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k. 操作完后,输出每一个点的点权和每一条边的边权(边权按照输入顺序输出) 我们把边权也当做点权处

HDU 5044 (树链剖分+树状数组+点/边改查)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044 题目大意:修改链上点,修改链上的边.查询所有点,查询所有边. 解题思路: 2014上海网赛的变态树链剖分模板题.将以往树链剖分的点&边修改和查询合在一起之后,难度上去不少. 第一个卡人点是读入优化. 第二个卡人点是树状数组.由于要查询所有点,如果使用线段树,每次都要扫到底层才能取出点值,必T无疑. 然后使用树状数组之后,树链剖分的点/边修改写法有些变动. 点查询变化不大. 边查询只要查询一下

hdu 5458 Stability(树链剖分+并查集)

Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total Submission(s): 1347    Accepted Submission(s): 319 Problem Description Given an undirected connected graph G with n nodes and m edges, with possibly r

SPOJ375 Query on a tree 树链剖分

SPOJ375  Query on a tree   树链剖分 no tags You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of

SPOJ - QTREE 375 Query on a tree 树链剖分+线段树

操作1:修改第k条边权. 操作2:询问两点间最大边权. 树链剖分,然后线段树维护最大值 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #inclu

HDU 3966 RE 树链剖分 Aragorn&#39;s Story

给一棵点带权的图 有这样一个操作: 使树上某一条路径所有点权值增减 每次询问某个点现在的权值. 树链剖分完以后,就是线段树的成段更新了. 这题感觉A不了了,无限RE,手动开栈也没卵用. 还是把我辛辛苦苦写的代码贴一下吧. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 using

hdu 4912 Paths on the tree(树链剖分+贪心)

题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道,要求尽量选出多的通道,并且两两通道不想交. 解题思路:用树链剖分求LCA,然后根据通道两端节点的LCA深度排序,从深度最大优先选,判断两个节点均没被标 记即为可选通道.每次选完通道,将该通道LCA以下点全部标记. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include

Aizu 2450 Do use segment tree 树链剖分+线段树

Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show.php?pid=39566 Description Given a tree with n (1 ≤ n ≤ 200,000) nodes and a list of q (1 ≤ q ≤ 100,000) queries, process the queries in order and out

spoj 375 Query on a tree (树链剖分)

Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of the i-th edge to ti or Q

SPOJ Query on a tree 树链剖分 水题

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of the i-th edge to tior QUERY a b : ask fo