HDU 6162 树链剖分

Ch’s gift

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 712    Accepted Submission(s): 247


Problem Description
Mr. Cui is working off-campus and he misses his girl friend very much. After a whole night tossing and turning, he decides to get to his girl friend's city and of course, with well-chosen gifts. He knows neither too low the price could a gift be since his girl friend won't like it, nor too high of it since he might consider not worth to do. So he will only buy gifts whose price is between [a,b].
There are n cities in the country and (n-1) bi-directional roads. Each city can be reached from any other city. In the ith city, there is a specialty of price ci Cui could buy as a gift. Cui buy at most 1 gift in a city. Cui starts his trip from city s and his girl friend is in city t. As mentioned above, Cui is so hurry that he will choose the quickest way to his girl friend(in other words, he won't pass a city twice) and of course, buy as many as gifts as possible. Now he wants to know, how much money does he need to prepare for all the gifts?
 

Input
There are multiple cases.

For each case:
The first line contains tow integers n,m(1≤n,m≤10^5), representing the number of cities and the number of situations.
The second line contains n integers c1,c2,...,cn(1≤ci≤10^9), indicating the price of city i's specialty.
Then n-1 lines follows. Each line has two integers x,y(1≤x,y≤n), meaning there is road between city x and city y.
Next m line follows. In each line there are four integers s,t,a,b(1≤s,t≤n;1≤a≤b≤10^9), which indicates start city, end city, lower bound of the price, upper bound of the price, respectively, as the exact meaning mentioned in the description above
 

Output
Output m space-separated integers in one line, and the ith number should be the answer to the ith situation.
 

Sample Input
  
  
5 3 1 2 1 3 2 1 2 2 4 3 1 2 5 4 5 1 3 1 1 1 1 3 5 2 3
 

Sample Output
  
  
7 1 4
 

题意:有一棵树,有n个点(n - 1)条边,即生成树,每个点带有一个权值,构成一个图后,有m次询问,问你从点u 到 v 之间权值在 a 到 b 之间的点的权值之和,点有1e5个,所以这里需要用到树链剖分,但是有个问题不好解决,就是区间 (a,b)的问题,权值必须要在这个区间内的才计算权值,所以它不能单纯地套版。

思路:对于这个(a,b)区间可以这样处理,弄多一个maxx 和 minn 数组,像sum数组一样用来存一个区间的最大值和最小值,那么对于你给定的(a,b)区间,通过maxx和minn判断这个区间的值是否在(a,b)里,如果在直接返回sum值,否则返回0就可以了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define ll long long
#define maxn 101000

struct Cas{	//存输出的询问 
	int x,y;
	ll a,b;
}cas[maxn];
struct node{	//存边 
	int u,v,nxt;
}edge[maxn * 3];
int head[maxn],cnt,idx;  
ll c[maxn],val[maxn];
int deep[maxn],id[maxn],son[maxn],top[maxn],size[maxn],fa[maxn];
ll sum[maxn * 4],maxx[maxn * 4],minn[maxn * 4];

void add(int u,int v){
	edge[cnt].u = u;
	edge[cnt].v = v;
	edge[cnt].nxt = head[u];
	head[u] = cnt++;   
}
void dfs1(int u,int pre,int dep){
	deep[u] = dep;	//存点的深度 
	size[u] = 1;	// size[u]表示以u为根的子树的节点数 
	son[u] = 0;		//son[u]表示与k在同一重链上的u的儿子节点(姑且称为重儿子) 
	fa[u] = pre;	// 父亲节点 
	for(int i = head[u];i != -1;i = edge[i].nxt){
		int v = edge[i].v;
		if(v == pre)
			continue;
		dfs1(v,u,dep + 1);
		size[u] += size[v];
		if(size[son[u]] < size[v])
			son[u] = v;
	}
}
void dfs2(int u,int tp){
	top[u] = tp;	 //top[u]表示k所在的链的顶端节点
	id[u] = ++idx;		//id[u]表示节点k在线段树中的编号 
	if(son[u])
		dfs2(son[u],tp);
	for(int i = head[u];i != -1;i = edge[i].nxt){
		int v = edge[i].v;
		if(v == fa[u] || v == son[u])
			continue;
		dfs2(v,v);
	}
}
void TreeWork(int n){
	dfs1(1,0,0);
	dfs2(1,1);
	for(int i = 1;i <= n;i++){
		val[id[i]] = c[i]; //存对应的权值 
	}
}
void build(int u,int l,int r){	//建立线段树 
	if(l == r){
		minn[u] = maxx[u] = sum[u] = val[l];
		return ;
	}
	int mid = (l + r) >> 1;
	build(u << 1,l,mid);
	build(u << 1 | 1,mid + 1,r);
	sum[u] = sum[u << 1] + sum[u << 1 | 1];
	minn[u] = min(minn[u << 1],minn[u << 1 | 1]);
	maxx[u] = max(maxx[u << 1],maxx[u << 1 | 1]);
}

ll query(int u,int l,int r,int L,int R,ll a,ll b){
	if(L <= l && r <= R){
		if(minn[u] > b || maxx[u] < a)	//如果不在范围内返回0 
			return 0;
		if(minn[u] >= a && maxx[u] <= b) //在范围内返回 sum 
			return sum[u];
	}
	int mid = (l + r) >> 1;
	ll ans = 0;
	if(L <= mid)
		ans += query(u << 1,l,mid,L,R,a,b);
	if(R > mid)
		ans += query(u << 1 | 1,mid + 1,r,L,R,a,b);
	return ans;
}
ll solve(int u,int v,ll a,ll b,int n){
	int tp1 = top[u],tp2 = top[v];
	ll ans = 0;
	while(tp1 != tp2){
		if(deep[tp1] < deep[tp2]){
			swap(tp1,tp2);
			swap(u,v);
		}
		ans += query(1,1,n,id[tp1],id[u],a,b);
		u = fa[tp1];
		tp1 = top[u];
	}
	if(u == v){
		if(c[u] >= a && c[u] <= b){
			ans += c[u];
		}
		return ans;
	}
	if(deep[u] > deep[v]){
		swap(u,v);
	}
	ans += query(1,1,n,id[u],id[v],a,b);
	return ans;
}
void SegmentTree(int n,int m){
	build(1,1,n);
	int x,y;
	ll a,b;
	for(int i = 1;i <= m;i++){
		if(i == 1)
			printf("%lld",solve(cas[i].x,cas[i].y,cas[i].a,cas[i].b,n));
		else
			printf(" %lld",solve(cas[i].x,cas[i].y,cas[i].a,cas[i].b,n));
	}
	puts("");
}
int main(){
	int n,m,u,v;
	while(scanf("%d %d",&n,&m) != EOF){
		memset(head,-1,sizeof(head));
		cnt = idx = 0;
		for(int i = 1;i <= n;i++)
			scanf("%lld",&c[i]);
		for(int i = 1;i < n;i++){
			scanf("%d %d",&u,&v);
			add(u,v);
			add(v,u);
		}
		for(int i = 1;i <= m;i++)
			scanf("%d %d %lld %lld",&cas[i].x,&cas[i].y,&cas[i].a,&cas[i].b);
		TreeWork(n);
		SegmentTree(n,m);
	}
	return 0;
} 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值