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. Mybatis中的别名的起源

    1.Mybatis中的别名的起源 我们对别名的认识最初是在数据库中,例如:数据库之select时取别名的做法是这样的: select 列名 as 列别名,//方法1 列名 列别名,//方法2 from ...

  2. 数据结构之队列and栈总结分析

    一.前言: 数据结构中队列和栈也是常见的两个数据结构,队列和栈在实际使用场景上也是相辅相成的,下面简单总结一下,如有不对之处,多多指点交流,谢谢. 二.队列简介 队列顾名思义就是排队的意思,根据我们的 ...

  3. 【笔记】nginx部署静态网站

    安装nginx 本地到官网下载,然后把压缩包传到服务器上 安装三个依赖 apt-get install libpcre3 libpcre3-dev apt-get install zlib1g-dev ...

  4. 导入maven项目pom报错

    打开window->preferences->maven->user settings更换settings.xml即可

  5. 将 /u 转变为 utf-8 编码

    将 /u 转变为 utf-8 编码 PHP实例: $result = {"errno":-1,"message":"\u8bbf\u95ee\u5fa ...

  6. 领扣(LeetCode)各位相加 个人题解

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...

  7. [学习笔记] 在Eclipse中使用Hibernate,并创建第一个工程

    在Eclipse中使用Hibernate 安装 Hibernate Tools 插件 https://tools.jboss.org/downloads/ Add the following URL ...

  8. SpringBoot系列教程Web篇之开启GZIP数据压缩

    本篇可以归纳在性能调优篇,虽然内容非常简单,但效果可能出乎预料的好: 分享一个真实案例,我们的服务部署在海外,国内访问时访问服务时,响应有点夸张:某些返回数据比较大的接口,耗时在 600ms+上,然而 ...

  9. 前端的构建化工具Webpack

    经常看到如jquery-3.0.0.js和jquery-3.0.0-min.js等两相似的文件名. 其实以上两个文件名的内容是一样的,不过带min代表的是占用最小的空间,为项目提高性能.压缩的部分如换 ...

  10. Java程序性能优化之性能概述

    性能的基本概念 一).什么叫程序的性能? 程序运行所需的内存和时间. 二).性能的表现形式: 1).执行速度: 程序的反应是否迅速,响应时间是否足够短. 2).启动时间:程序从运行到可以处理正常业务所 ...