博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】...
阅读量:6988 次
发布时间:2019-06-27

本文共 3941 字,大约阅读时间需要 13 分钟。

Ch’s gift

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1354    Accepted Submission(s): 496

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
题目链接:
分析:由于没有修改操作,一个显然的想法是离线处理所有问题
将询问拆成1-x,1-y,1-LCA(x,y),则处理的问题转化为从根到节点的链上的问题。
解决这个问题,我们可以在dfs时向treap插入当前的数,在退出时删除这个数,并且每次维护在该点上的答案。
当然也可以将所有的查询和点权排序,用树链剖分做这个题,在线段树上面插入就ok。
下面给出AC代码:
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 #define LL long long 7 vector
G[100005]; 8 typedef struct 9 { 10 LL id; 11 LL x, y; 12 LL l, r; 13 }Quer; 14 Quer s[100005], cnt[100005]; 15 LL tre[445005]; 16 LL n, val[100005], son[100005], fa[100005], siz[100005], dep[100005]; 17 LL k, top[100005], rak[100005], id[100005], anl[100005], anr[100005]; 18 bool comp1(Quer a, Quer b) 19 { 20 if(a.l
siz[son[u]]) 43 son[u] = v; 44 } 45 } 46 void Sechr(LL u, LL p) 47 { 48 LL v, i; 49 top[u] = p; 50 rak[u] = ++k, id[k] = u; 51 if(son[u]==0) 52 return; 53 Sechr(son[u], p); 54 for(i=0;i
=a && r<=b) 82 return tre[x]; 83 m = (l+r)/2; 84 if(a<=m) 85 sum += Query(l, m, x*2, a, b); 86 if(b>=m+1) 87 sum += Query(m+1, r, x*2+1, a, b); 88 return sum; 89 } 90 LL TreQuery(LL x, LL y) 91 { 92 LL sum, p1, p2; 93 p1 = top[x], p2 = top[y], sum = 0; 94 while(p1!=p2) 95 { 96 if(dep[p1]
dep[y])102 swap(x, y);103 sum += Query(1, n, 1, rak[x], rak[y]);104 return sum;105 }106 107 int main(void)108 {109 LL i, j, x, y, q;110 while(scanf("%lld%lld", &n, &q)!=EOF)111 {112 for(i=1;i<=n;i++)113 G[i].clear();114 for(i=1;i<=n;i++)115 {116 scanf("%lld", &val[i]);117 cnt[i].id = i;118 cnt[i].r = val[i];119 }120 sort(cnt+1, cnt+n+1, compr);121 for(i=1;i<=n-1;i++)122 {123 scanf("%lld%lld", &x, &y);124 G[x].push_back(y);125 G[y].push_back(x);126 }127 memset(siz, 0, sizeof(siz));128 memset(son, 0, sizeof(son));129 k = 0;130 Sechs(1, 0);131 Sechr(1, 1);132 for(i=1;i<=q;i++)133 {134 scanf("%lld%lld%lld%lld", &s[i].x, &s[i].y, &s[i].l, &s[i].r);135 s[i].id = i;136 }137 138 sort(s+1, s+q+1, comp1);139 memset(tre, 0, sizeof(tre));140 j = 1;141 for(i=1;i<=q;i++)142 {143 while(cnt[j].r

 

转载地址:http://wkhpl.baihongyu.com/

你可能感兴趣的文章
AFNetWorking 2.0 使用
查看>>
VMware克隆Linux系统后,网络问题解决
查看>>
Linux系统下vsftp服务器搭建(二)
查看>>
Citrix User Profile Management 设定参考
查看>>
网络库性能对比
查看>>
Linux 文件系统
查看>>
ubuntu下美化myeclipse
查看>>
javascrip对表格的操作(一)
查看>>
Linux课程第二天学习笔记
查看>>
Redis数据库高级实用特性:事务控制
查看>>
jquery.validate remote 和 自定义验证方法
查看>>
Android开发9——Activity的启动模式
查看>>
Python + Django 配置后台管理系统
查看>>
SNMP常用OID
查看>>
安装TMG2010的先决条件
查看>>
Prometheus+Grafana部署监控docker服务
查看>>
Docker Private Registry && 资源限制
查看>>
春招前夕
查看>>
go anonymous function
查看>>
时序列数据库武斗大会之 OpenTSDB 篇
查看>>