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. 外行人都能看懂的WebFlux,错过了血亏!

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 本文知识点架构: 如果有关注我公众号文章的同学就会发 ...

  2. [Office] VBA Practice

    1. 使用DateTimePicker控件 VBA中默认的User Form的Toolbox中的控件并不包含DateTimePicker,是接受时间相关数据的输入,在Toolbox上右击“Additi ...

  3. 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)

    本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...

  4. C语言程序设计100例之(14):丑数

    例14   丑数 问题描述 丑数是其质因子只可能是2,3或5的数.前10个丑数分别为1, 2, 3, 4, 5, 6, 8, 9, 10, 12.输入一个正整数n,求第n个丑数. 输入格式 每行为一个 ...

  5. nyoj 74-小学生算术(进位问题)

    74-小学生算术 内存限制:64MB 时间限制:3000ms 特判: No 通过数:23 提交数:53 难度:1 题目描述: 很多小学生在学习加法时,发现“进位”特别容易出错.你的任务是计算两个三位数 ...

  6. nyoj 733-万圣节派对 (printf("%06d", i))

    733-万圣节派对 内存限制:64MB 时间限制:1000ms 特判: No 通过数:5 提交数:7 难度:1 题目描述: 万圣节有一个Party,XadillaX显然也要去凑热闹了.因为去凑热闹的人 ...

  7. nyoj 463-九九乘法表

    463-九九乘法表 内存限制:64MB 时间限制:1000ms 特判: No 通过数:16 提交数:41 难度:1 题目描述: 小时候学过的九九乘法表也许将会扎根于我们一生的记忆,现在让我们重温那些温 ...

  8. 关于Java 值传递深度分析

    首先说观点:java只有值传递没有引用传递 然后再来看看值传递与引用传递两者的定义 值传递(pass by value)是指在调用函数时将实际参数复制一份传递到函数中,这样在函数中如果对参数进行修改, ...

  9. 关闭Eslint检查

    Eslint是一个JavaScript的校验插件,通常用来校验语法或代码的书写风格.不过有点过于严格,通常缩减不同也会报错... 这会影响工作效率啊,最好就是在创建项目的时候不要ESlint(选择No ...

  10. ubuntu 16.04 和 windows 10系统安装mysql 允许远程访问 | mysql user guide on ubuntu 16.04 and windows 10

    本文首发于个人博客https://kezunlin.me/post/36e618e7/,欢迎阅读! mysql user guide on ubuntu 16.04 and windows 10 Pa ...