HDU 6162 Ch's gift

来源:互联网 发布:秦时明月动漫知乎 编辑:程序博客网 时间:2024/05/29 18:39

Ch’s gift

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


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 31 2 1 3 21 22 43 12 54 5 1 31 1 1 13 5 2 3
 

Sample Output
7 1 4
 

Source
2017 Multi-University Training Contest - Team 9

题意:
有一棵树,n个节点n-1条边
每个节点都有一个权值
然后m次查询输入x,y,a,b意思是从节点到x,y这条路径
上的所有权值在区间[a,b]内的权值和

题解:
一题树链剖分 第一次学 讲也讲不清楚 大家就链接到别的地方看看
无法做过多的解释
树链剖分


#include <iostream>#include <bits/stdc++.h>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define mid (l + r) >> 1using namespace std;const int N = 1e5+1000;typedef long long LL;struct M {int x,y;LL a,b;} A[N];struct node {int u,v;int nex;} eage[N*3];int head[N],cnt,idx;LL c[N],val[N];int deep[N],id[N],son[N],siz[N],fa[N],top[N];LL sum[N*4],mx[N*4],mi[N*4];void ADD(int u,int v) {eage[cnt].u = u;eage[cnt].v = v;eage[cnt].nex = head[u];head[u] = cnt++;}void dfs1(int u,int pre,int dep) {deep[u] = dep;siz[u] = 1;son[u] = 0;fa[u] = pre;for(int i = head[u]; i != -1; i = eage[i].nex) {int v = eage[i].v;if(v == pre)continue;dfs1(v,u,dep+1);siz[u] += siz[v];if(siz[son[u]] < siz[v])son[u] = v;}}void dfs2(int u,int tp) {top[u] = tp;id[u] = ++idx;if(son[u])dfs2(son[u],tp);for(int i = head[u]; i!=-1; i=eage[i].nex) {int v = eage[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) {mi[u] = mx[u] = sum[u] = val[l];return ;}int m = mid;build(u<<1,l,m);build(u<<1|1,m+1,r);sum[u] = sum[u<<1] + sum[u<<1|1];mi[u] = min(mi[u<<1],mi[u<<1|1]);mx[u] = max(mx[u<<1],mx[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(mi[u] > b || mx[u] < a)return 0;if(mi[u] >= a && mx[u] <= b)return sum[u];}int m = mid;LL ans = 0;if(L <= m)ans += query(u<<1,l,m,L,R,a,b);if(R > m)ans += query(u<<1|1,m+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 = 0; i < m; i++) {if(i)printf(" %lld",solve(A[i].x,A[i].y,A[i].a,A[i].b,n));else printf("%lld",solve(A[i].x,A[i].y,A[i].a,A[i].b,n));}puts("");}int main() {int n,m,u,v;while(~scanf("%d%d",&n,&m)) {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 = 0; i < m; i++)scanf("%d %d %lld %lld",&A[i].x,&A[i].y,&A[i].a,&A[i].b);TreeWork(n);SegmentTree(n,m);}return 0;}



原创粉丝点击