【树链剖分】 HDOJ 3966 Aragorn's Story

树链剖分模板题。。。

#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 5000005
#define eps 1e-10
#define mod 1000000007
#define INF 1e17
#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

char ss[maxn];
int size[maxn];
int tree[maxn];
int dep[maxn];
int son[maxn];
int top[maxn];
int num[maxn];
int fa[maxn];
int w[maxn];

struct Edge
{
	int v;
	Edge *next;
}pool[maxn << 1], *H[maxn], *edges;

int n, m, z;

void init(void)
{
	z = 0;
	edges = pool;
	memset(H, 0, sizeof H);
	memset(son, 0, sizeof son);
	memset(tree, 0, sizeof tree);
}

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

void add(int x, int v)
{
	for(int i = x; i <= n; i += lowbit(i)) tree[i] += v;
}

int query(int x)
{
	int ans = 0;
	for(int i = x; i > 0; i -= lowbit(i)) ans += tree[i];
	return ans;	
}

void read(void)
{
	int u, v;
	for(int i = 1; i <= n; i++) scanf("%d", &num[i]);
	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]) {
			fa[v] = u;
			dep[v] = dep[u] + 1;
			dfs1(v);
			size[u] += size[v];
			if(size[son[u]] < size[v]) son[u] = v;
		}
	}
}

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

}

void updata(int a, int b, int c)
{
	int f1 = top[a], f2 = top[b];
	while(f1 != f2) {
		if(dep[f1] < dep[f2]) {
			swap(f1, f2);
			swap(a, b);
		}
		add(w[f1], c);
		add(w[a]+1, -c);
		a = fa[f1], f1 = top[a];
	}

	//if(a == b) return;
	if(dep[a] > dep[b]) swap(a, b);
	add(w[a], c);
	add(w[b]+1, -c);
}

void work(void)
{
	dfs1(1);
	dfs2(1, 1);
	
	for(int i = 1;  i <= n; i++) {
		add(w[i], num[i]);
		add(w[i]+1, -num[i]);
	}
	
	int x, u, v, k;
	while(m--) {
		scanf("%s", ss);
		if(ss[0] == 'Q') {
			scanf("%d", &x);
			printf("%d\n", query(w[x]));
		}
		else {
			scanf("%d%d%d", &u, &v, &k);
			if(ss[0] == 'D') k = -k;
			updata(u, v, k);
		}
	}
}

int main(void)
{
	while(scanf("%d%*d%d", &n, &m)!=EOF) {
		init();
		read();
		work();
	}

	return 0;
}


你可能感兴趣的:(HDU)