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. vue的路由安全验证

    在传统的网页中: view层是由后端控制的,用户的请求到达后端的控制器中,只有当安安全全没有丝毫异常的情况下,后端才会将完成数据的渲染,返回给前端视图 前后端分离的项目: view层的切换权,转交给了 ...

  2. 《计算机网络 自顶向下方法》 第8章 计算机网络中的安全 Part2

    SSL(使 TCP 连接安全) SSL(Secure Socket Layer),即安全套接字层,是对 TCP 的强化 HTTPS 使用 SSL,而 HTTP 不使用 SSL 通过采用机密性.数据完整 ...

  3. Typings移除Deprecated Warning

    使用TypeScript进行开发中,经常遇到如下的Deprecated Warning.虽然没有实际影响,但看多了,确实挺烦. 要想消除这些Warning,需要以下几个步骤: 步骤一,确认Warnin ...

  4. 基于Docker的Mysql主从复制

    基于Docker的Mysql主从复制搭建 为什么基于Docker搭建? 资源有限 虚拟机搭建对机器配置有要求,并且安装mysql步骤繁琐 一台机器上可以运行多个Docker容器 Docker容器之间相 ...

  5. Windows下搭建远程Linux主机的图形化本地开发环境

    在实际开发中,项目的类生产.生产环境一般都是选择Linux为服务器进行部署. 相应的,我们的开发最好也在Linux环境下进行,否则容易引发其他的问题,比如不同环境下功能不一致.库依赖差异等. 但是Li ...

  6. Serlvet之cookie和session学习

    HTTP 协议 Web通信需要一种语言,就像中国人讲中文,欧美说英文,Web使用的HTTP协议,也叫超文本协议. 使用HTTP协议的人分为两类:客户端和服务端.请求资源的角色是客户端,提供资源的是服务 ...

  7. windwos 10 安装flask

    1 安装python2.7.13 安装文件为:python-2.7.13.amd64.msi,因为python2.7.13中已经包含了pip. 在安装过程中选中[Add python.exe to P ...

  8. 剑指Offer-20.包含min函数的栈(C++/Java)

    题目: 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 分析: 因为题目要求得到栈中最小元素的min函数时间复杂度为O(1),这里便不选择遍历栈 ...

  9. ZeroC Ice发送大数据

    继上文,我们使用ZeroC Ice传递大块数据时,通常有两种做法,一种是一次请求,另一种就是分多次请求(,这种做法在官方文档有例子).选哪一种根据需要而定. 当分多次请求来完成一大块数据,到底选择每次 ...

  10. 报错:尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。

    问题: 在写windows服务时,发布后日志报错:尝试加载 Oracle 客户端库时引发 BadImageFormatException.如果在安装 32 位 Oracle 客户端组件的情况下以 64 ...