【树链剖分】 FZU 2176 easy problem

建立5颗线段树,用树链剖分维护一下就好了。。。。

#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 50005
#define maxm 100005
#define eps 1e-10
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
// head

struct Edge
{
	int v;
	Edge *next;
}E[maxm], *H[maxn], *edges;

int sum[5][maxn << 2];
int size[maxn];
int top[maxn];
int son[maxn];
int dep[maxn];
int fa[maxn];
int w[maxn];
int z, n, m, k;

void addedges(int u, int v)
{
	edges->v = v;
	edges->next = H[u];
	H[u] = edges++;
}

void init(void)
{
	z = 0;
	edges = E;
	memset(H, 0, sizeof H);
}

void read(void)
{
	int u, v;
	scanf("%d%d%d", &n, &m, &k);
	for(int i = 1; i < n; i++) {
		scanf("%d%d", &u, &v);
		addedges(u, v);
		addedges(v, u);
	}
}

void dfs1(int u)
{
	size[u] = 1, son[u] = 0;
	for(Edge *e = H[u]; e; e = e->next) {
		int v = e->v;
		if(v != fa[u]) {
			dep[v] = dep[u] + 1;
			fa[v] = u;
			dfs1(v);
			size[u] += size[v];
			if(size[son[u]] < size[v]) son[u] = v;
		}
	}
}

void dfs2(int u, int tp)
{
	w[u] = ++z, top[u] = tp;
	if(son[u]) dfs2(son[u], tp);
	for(Edge *e = H[u]; e; e = e->next) {
		int v = e->v;
		if(v != fa[u] && v != son[u]) dfs2(v, v);
	}
}

void pushup(int o)
{
	for(int i = 0; i < k; i++) sum[i][o] = sum[i][ls] + sum[i][rs];
}

void build(int o, int L, int R)
{
	for(int i = 0; i < 5; i++) sum[i][o] = 0;
	if(L == R) return;
	int mid = (L + R) >> 1;
	build(lson);
	build(rson);
}

int query(int o, int L, int R, int kk, int ql, int qr)
{
	if(ql <= L && qr >= R) return sum[kk][o];
	int mid = (L + R) >> 1, ans = 0;
	if(ql <= mid) ans += query(lson, kk, ql, qr);
	if(qr > mid) ans += query(rson, kk, ql, qr);
	return ans;
}

void updata(int o, int L, int R, int kk, int q, int val)
{
	if(L == R) {
		sum[kk][o] += val;
		return;
	}
	int mid = (L+ R) >> 1;
	if(q <= mid) updata(lson, kk, q, val);
	else updata(rson, kk, q, val);
	pushup(o);
}

int res[5];

int solve(int x)
{
	int t = dep[x] % k;
	int a = x, b = 1, kk;
	int f1 = top[a], f2 = top[b];
	memset(res, 0, sizeof res);
	while(f1 != f2) {
		if(dep[f1] < dep[f2]) {
			swap(f1, f2);
			swap(a, b);
		}
		kk = t + 1;
		for(int i = 0; i < k; i++) {
			kk = (kk - 1 + k) % k;
			res[i] += query(1, 1, n, kk, w[f1], w[a]);
		}
		a = fa[f1], f1 = top[a];
	}

	if(dep[a] > dep[b]) {
		swap(f1, f2);
		swap(a, b);
	}
	kk = t + 1;
	for(int i = 0; i < k; i++) {
		kk = (kk - 1 + k) % k;
		res[i] += query(1, 1, n, kk, w[a], w[b]);
	}

	int ans = 0;
	for(int i = 0; i < k; i++) ans += (i+1) * res[i];
	return ans;
}

void work(void)
{
	dfs1(1);
	dfs2(1, 1);
	build(1, 1, n);
	int kk, x, val;
	while(m--) {
		scanf("%d", &kk);
		if(kk == 1) {
			scanf("%d%d", &x, &val);
			updata(1, 1, n, dep[x] % k, w[x], val);
			
		}
		else {
			scanf("%d", &x);
			printf("%d\n", solve(x));
		}
	}
}

int main(void)
{
	int _, __;
	while(scanf("%d", &_)!=EOF) {
		__ = 0;
		while(_--) {
			init();
			read();
			printf("Case#%d:\n", ++__);
			work();
		}
	}

	return 0;
}


你可能感兴趣的:(HDU)