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

题解:LCA求最近公共祖先,里面加一个判断节点的权值是否满足在L与R之间,如满足求和;(树链剖分&线段树也可)

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
typedef long long LL;
int n,m,u,v,tot,s,t,up,down,sum;
int first[maxn],dep[maxn],fa[maxn];
LL flag[maxn],val[maxn];
struct Node{
int to,net;
} edge[maxn<<1]; void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].net=first[u];
first[u]=tot++;
} void dfs(int u,int f)
{
for(int e=first[u];e!=-1;e=edge[e].net)
{
int v=edge[e].to;
if(v==f) continue;
fa[v]=u; dep[v]=dep[u]+1;
dfs(v,u);
}
} LL LCA(int a,int b)
{
LL ans=0;
if(dep[a]<dep[b]) swap(a,b);
while(dep[a]!=dep[b])
{
if(val[a]>=down&&val[a]<=up) ans+=val[a];
a=fa[a];
}
while(a!=b)
{
if(val[a]>=down&&val[a]<=up) ans+=val[a];
if(val[b]>=down&&val[b]<=up) ans+=val[b];
a=fa[a],b=fa[b];
}
if(val[a]>=down&&val[a]<=up) ans+=val[a];
return ans;
} void Init()
{
tot=0; sum=0;
memset(first,-1,sizeof first);
memset(fa,0,sizeof fa);
memset(dep,0,sizeof dep);
} int main()
{
while(~scanf("%d%d",&n,&m))
{
Init();
for(int i=1;i<=n;i++) scanf("%d",val+i);
for(int i=1;i<=n-1;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs(1,0); for(int i=0;i<m;i++)
{
scanf("%d%d%d%d",&s,&t,&down,&up);
flag[sum++]=LCA(s,t);
}
for(int i=0;i<sum;i++) i==sum-1 ? printf("%lld\n",flag[i]) : printf("%lld ",flag[i]);
} return 0;
}

(全国多校重现赛一)B-Ch's gifts的更多相关文章

  1. (全国多校重现赛一)F-Senior Pan

    Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory pro ...

  2. (全国多校重现赛一)D Dying light

    LsF is visiting a local amusement park with his friends, and a mirror room successfully attracts his ...

  3. (全国多校重现赛一) J-Two strings

    Giving two strings and you should judge if they are matched.  The first string contains lowercase le ...

  4. (全国多校重现赛一) H Numbers

    zk has n numbers a1,a2,...,ana1,a2,...,an. For each (i,j) satisfying 1≤i<j≤n, zk generates a new ...

  5. (全国多校重现赛一)E-FFF at Valentine

    At Valentine's eve, Shylock and Lucar were enjoying their time as any other couples. Suddenly, LSH, ...

  6. (全国多校重现赛一)A-Big Binary Tree

    You are given a complete binary tree with n nodes. The root node is numbered 1, and node x's father ...

  7. 2016ACM/ICPC亚洲区沈阳站-重现赛赛题

    今天做的沈阳站重现赛,自己还是太水,只做出两道签到题,另外两道看懂题意了,但是也没能做出来. 1. Thickest Burger Time Limit: 2000/1000 MS (Java/Oth ...

  8. 2016 CCPC 东北地区重现赛

    1. 2016 CCPC 东北地区重现赛 2.总结:弱渣,只做出01.03.05水题 08   HDU5929 Basic Data Structure    模拟,双端队列 1.题意:模拟一个栈的操 ...

  9. 2016 CCPC长春重现赛

    1.2016中国大学生程序设计竞赛(长春)-重现赛 2.总结:会做的太少,应变能力也不行,或者说猜题目的能力不行 02  水 04  HDU 5914  Triangle 1.题意:1~n,n个数,问 ...

随机推荐

  1. CMDB连接方式

    1.agent agent (放在每台客户端服务器上,定时任务) 脚本演示 # 采集本机的信息 执行命令 import subprocess v1 = subprocess.getoutput('ip ...

  2. Class文件结构全面解析(上)

    什么是Class文件? 在Java刚刚诞生的时候就提出了一个非常著名的口号:"一次编写,到处运行.(Write Once,Run Anywhere)".为了实现平台无关性,各种不同 ...

  3. Java 读取Word批注中的文本和图片

    本文将介绍读取Word批注的方法,包括读取Word批注中的文本及图片.关于操作Word批注的方法还可以参考这两篇文章:Java 添加.回复.修改.删除Word批注:Java 给Word指定字符串添加批 ...

  4. beyong Compare4解决30天的评估期结束

    刚开始是删掉注册表的CacheId(无效) 1.在搜索栏中输入 regedit ,打开注册表2.删除项目CacheId :HKEY_CURRENT_USER\Software\Scooter Soft ...

  5. jwt 实践应用以及特殊案例思考

    JSON Web Token 是 rfc7519 出的一份标准,使用 JSON 来传递数据,用于判定用户是否登录状态. jwt 之前,使用 session 来做用户认证. 以下代码均使用 javasc ...

  6. Microsoft Dynamics CRM 批量上传web资源(非官方WebResourceUtility)并替换实体图标

    背景: 去年以前可以按照目录WebResourceUtility批量上传web资源,昨天发现用不了了,拿到WebResourceUtility源码改了一下都不是很方便,感觉官方写的太冗余,太长了,跟我 ...

  7. Nginx 跨域代理

    安装环境: win10 nginx-1.17.2 安装: 在写前端调用后台接口时,报了跨域的错误. 调试地址:http://localhost:5500/demo/encAjax.html 接口地址: ...

  8. Linux考题(一)

    1.创建目录/data/oldboy,并且在该目录下创建oldboy.txt,然后在文件oldybos.txt里写入内容“inet addr:192.168.228.128  Bcast:192.16 ...

  9. NN入门,手把手教你用Numpy手撕NN(三)

    NN入门,手把手教你用Numpy手撕NN(3) 这是一篇包含极少数学的CNN入门文章 上篇文章中简单介绍了NN的反向传播,并利用反向传播实现了一个简单的NN,在这篇文章中将介绍一下CNN. CNN C ...

  10. nginx支持wss配置

    nginx证书 nginx.conf配置