HDU6162-Ch’s gift
来源:互联网 发布:经营杠杆的理解知乎 编辑:程序博客网 时间:2024/06/05 15:15
Ch’s gift
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 352 Accepted Submission(s): 113
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?
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
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 <cstdio>#include <cstring>#include <string>#include <cmath>#include <map>#include <set>#include <algorithm>#include <vector>#include <bitset>#include <stack>#include <queue>#include <unordered_map>#include <functional>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;int n, m, a, b, u ,v;int ss[200009], L[200009 * 40], R[200009 * 40], top[200009];int s[200009], nt[400009], e[400009], cnt;int ct[200009], mx[200009], fa[200009], dep[200009];LL c[200009], sum[200009 * 40] ,ans[200009];void add(int pre, int &now, int l, int r, LL p){ now = ++cnt, L[now] = L[pre], R[now] = R[pre], sum[now] = sum[pre] + p; if (l == r) return; int mid = (l + r) >> 1; if (p <= mid) add(L[pre], L[now], l, mid, p); else add(R[pre], R[now], mid + 1, r, p);}LL query(int pre, int now, int l, int r, int p){ if(l==r) return sum[pre]-sum[now]; LL ans=0; int mid=(l+r)>>1; if(p<=mid) ans+=query(L[pre], L[now], l, mid, p); else ans+=sum[L[pre]]-sum[L[now]]+query(R[pre], R[now], mid+1, r, p); return ans;}void dfs(int k, int f){ add(ss[f], ss[k], 0, 1000000000, c[k]); dep[k] = dep[f] + 1; fa[k] = f, ct[k] = 1, mx[k] = 0; for (int i = s[k]; ~i; i = nt[i]) { if(e[i]==f) continue; dfs(e[i], k); ct[k] += ct[e[i]]; if (ct[e[i]] > ct[mx[k]]) mx[k] = e[i]; }}void Dfs(int k, int t){ top[k] = !t ? k : top[fa[k]]; if (mx[k]) Dfs(mx[k], 1); for (int i = s[k]; ~i; i = nt[i]) { if (e[i] == fa[k] || e[i] == mx[k]) continue; Dfs(e[i], 0); }}LL solve(int x, int y, int k){ LL ans=0; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); ans+=query(ss[x], ss[fa[top[x]]], 0, 1000000000, k); x = fa[top[x]]; } if (dep[x] > dep[y]) swap(x, y); ans+=query(ss[y], ss[fa[x]], 0, 1000000000, k); return ans;}int main(){ while (~scanf("%d%d", &n, &m)) { for(int i = 1;i <= n; i++) scanf("%lld", &c[i]); memset(s, -1, sizeof s); ss[0] = L[0] = R[0] = dep[0] = sum[0] = ct[0] = cnt = 0; for (int i = 1; i < n; i++) { scanf("%d %d", &u, &v); nt[cnt] = s[u], s[u] = cnt, e[cnt++] = v; nt[cnt] = s[v], s[v] = cnt, e[cnt++] = u; } dfs(1, cnt=0); Dfs(1, 0); int flag=0; for(int i=1;i<=m;i++) { scanf("%d%d%d%d",&u,&v,&a,&b); LL ans1=solve(u,v,b),ans2=solve(u,v,a-1); ans[i]=ans1-ans2; } for(int i=1;i<=m;i++) { if(flag) printf(" "); else flag=1; printf("%lld",ans[i]); } printf("\n"); } return 0;}
阅读全文
0 0
- HDU6162-Ch’s gift
- hdu6162 Ch’s gift
- HDU6162 Ch’s gift【LCA】
- hdu6162 Ch’s gift(LCA)
- HDU6162-Ch’s gift((离线+树链剖分)或(在线+树上主席树))
- EOJ3335&&hdu6162 Ch’s gift 树剖/dfs序/离线查询/主席树,各显神通
- 2017多校九 hdu6162 02题 Ch's gift dfs序+树状数组+离散化 / 树链剖分+线段树
- Ch’s gift HDU
- Ch’s gift HDU
- Ch’s gift HDU
- hdu 6162 Ch’s gift
- HDU 6162 Ch’s gift
- HDU 6162 Ch’s gift
- HDU 6162 Ch's gift
- HDU 6162 Ch’s gift
- hdu-6162 Ch’s gift 主席树
- hdu-6162 Ch’s gift(树链剖分)
- HDU 6162 Ch’s gift(LCA)
- 小总结
- 2017.8.22暑假集训第二十三天(下午训练赛)
- Oracle应用开发实战
- Java学习笔记1
- java配置环境变量
- HDU6162-Ch’s gift
- POJ 3264 Balanced Lineup(ST表)
- Tensorflow实现逻辑分类器
- 如何学习设计模式
- 【BZOJ3091】城市旅行
- oracle知识点的梳理及复习
- 环境变量错误导致Linux指令不可用
- 程序员的自信和骄傲
- SqlSession(四)