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. PowerDesigner列名、注释内容互换

    资料来源:PowerDesigner列名.注释内容互换 文中一共提供了2种操作的代码. (1)将Name中的字符COPY至Comment中 (2)将Comment中的字符COPY至Name中 使用方法 ...

  2. Master原理

    1.主备切换机制原理剖析与源码分析 2.注册机制原理剖析与源码分析 3.状态改变处理机制源码分析 4.资源调度机制源码分析(schedule(),两种资源调度算法)(核心) 一.主备切换机制原理 1. ...

  3. Mssql 查询某记录前后N条

    Sqlserver 查询指定记录前后N条,包括当前数据 条件 [ID] 查询 [N]条 select * from [Table] where ID in (select top ([N]+1) ID ...

  4. nyoj 845-无主之地1 (struct)

    845-无主之地1 内存限制:64MB 时间限制:1000ms 特判: No 通过数:8 提交数:16 难度:0 题目描述: 子晓最近在玩无主之地1,他对这个游戏的评价不错,结合了FPS与RPG元素, ...

  5. MAC系统下,Jmeter5.1.1 无法录制问题

    问题一: 点击[start]先出现一个检查证书信息的弹窗,确保删除旧的安装新的,并且需要信任证书 (一般证书只需要信任一下即可,每次启动都会有这个弹窗提醒) 问题二: MAC OS系统使用Jmeter ...

  6. 程序员修神之路--kubernetes是微服务发展的必然产物

    菜菜哥,我昨天又请假出去面试了 战况如何呀? 多数面试题回答的还行,但是最后让我介绍微服务和kubernetes的时候,挂了 话说微服务和kubernetes内容确实挺多的 那你给我大体介绍一下呗 可 ...

  7. Video的自我学习

    直播原理 视频协议 HLS协议  [主要是直播方面(好用,但延时)] HTTP Live Streaming(缩写是HLS)是一个由苹果公司提出的基于HTTP的流媒体网络传输协议. 是苹果公司Quic ...

  8. 减少HTTP请求的方式

    1. 图片地图 缺点:坐标难定义:除了矩形之外几乎无法定义其他形状:通过DHTML(动态DOM操作)创建的图片地图在 IE 不兼容 <img usemap="#map1" b ...

  9. Ubuntu 16.04上源码编译Poco并编写cmake文件 | guide to compile and install poco cpp library on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/281dd8cd/,欢迎阅读! guide to compile and install poco cpp library on u ...

  10. Redis为什么是单线程、及高并发快的3大原因详解

    Redis的高并发和快速原因 1.redis是基于内存的,内存的读写速度非常快: 2.redis是单线程的,省去了很多上下文切换线程的时间: 3.redis使用多路复用技术,可以处理并发的连接.非阻塞 ...