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? 

InputThere 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 
OutputOutput 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

思路:
算是比较裸的树链剖分+线段树吧,这道题要求树上任意两点间在区间ab内的值
首先先用树链剖分把这棵树划分轻重链,实际上也是相当于通过类似hash的方式将树形结构变成线形结构
然后就可以用线段树处理了,只要求出在这个区间内的数就行了,直接求出<=b的和<a的做下差就可以了 至于代码的话,树链剖分主要是两个dfs,第一个统计父亲节点fa 当前节点深度deep 子树大小sz ,并在遍历时求出当前节点的重儿子
第二个求出对各个结点的top,对于重儿子来说,top就是沿其所在重链走到顶端的点的位置,对于轻儿子,我们把top定为其本身。
然后对每个结点dfs时先遍历到它的重儿子后遍历轻儿子 最后重链上的点映射到一维数组里得到一串连续的区间,然后就可以直接套线段树了处理了
时间复杂度差不多是是O(nlogn) 实现代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
vector<int> vt[maxn];
int n,q,tot;
int sz[maxn],deep[maxn],fa[maxn],son[maxn],top[maxn],tree[maxn],pre[maxn];
ll ansl[maxn],ansr[maxn],sum[maxn*];
struct node1{
int x,id;
bool operator < (const node1 &a)const{
return x<a.x;
}
}a[maxn]; struct node2{
int x,y,a,b,id;
}op[maxn]; bool cmp1(const node2 &a,const node2 &b){
return a.a<b.a;
} bool cmp2(const node2 &a,const node2 &b){
return a.b<b.b;
} void dfs1(int u,int pre,int d){
deep[u] = d; sz[u] = ; fa[u] = pre;
int len = vt[u].size();
for(int i=;i<len;i++){
int v = vt[u][i];
if(v==pre) continue;
dfs1(v,u,d+);
sz[u] += sz[v];
if(!son[v]||sz[v]>sz[son[u]])
son[u] = v;
}
} void dfs2(int u, int tp){
top[u] = tp;
tree[u] = ++tot;
pre[tree[u]] = u;
if(!son[u]) return ;
dfs2(son[u], tp);
for(int i = , len = vt[u].size(); i < len; i++){
int v = vt[u][i];
if(v != son[u] && v != fa[u])
dfs2(v, v);
}
} void push_up(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
} void update(int rt,int l,int r,int pos,int val){
if(l==r){
sum[rt]+=val; return;
}
int m = (l+r)>>;
if(pos <= m) update(rt<<,l,m,pos,val);
else update(rt<<|,m+,r,pos,val);
push_up(rt);
} ll query(int rt,int l,int r,int L,int R){
if(L<=l&&R>=r) return sum[rt];
int m = (l+r)>>;
ll ret = ;
if(L<=m) ret += query(rt<<,l,m,L,R);
if(m<R) ret += query(rt<<|,m+,r,L,R);
return ret;
} ll ask(int x,int y){ //求两结点路径上的权值和
int fx = top[x],fy = top[y];
ll ans = ;
while(fx!=fy){
if(deep[fx]<deep[fy]) swap(fx,fy),swap(x,y);
ans += query(,,n,tree[fx],tree[x]);
x = fa[fx]; fx = top[x];
}
ans += (deep[x]>deep[y])?query(,,n,tree[y],tree[x]):query(,,n,tree[x],tree[y]);
return ans;
}
int main()
{
ios::sync_with_stdio();
cin.tie();
cout.tie();
while(cin>>n>>q){
tot = ;
memset(son, , sizeof(son));
memset(sz, , sizeof(sz));
for(int i = ; i <= n; i++)
vt[i].clear();
for(int i=;i<=n;i++){
cin>>a[i].x;a[i].id=i;
}
for(int i=;i<n;i++){
int u,v;
cin>>u>>v;
vt[u].push_back(v);vt[v].push_back(u);
}
dfs1(,,); dfs2(,);
for(int i=;i<=q;i++){
cin>>op[i].x>>op[i].y>>op[i].a>>op[i].b; op[i].id = i;
}
memset(sum,,sizeof(sum));
sort(a+,a+n+);
sort(op+,op+q+,cmp1);
for(int i=,j=;i<=q;i++){
while(j<=n&&a[j].x<op[i].a){
· update(,,n,tree[a[j].id],a[j].x);
j++;
}
ansl[op[i].id] = ask(op[i].x,op[i].y);
}
memset(sum,,sizeof(sum));
sort(op+,op++q,cmp2);
for(int i = , j = ; i <= q; i++){
while(j <= n && a[j].x <= op[i].b){
update(, , n, tree[a[j].id], a[j].x);
j++;
}
ansr[op[i].id] = ask(op[i].x, op[i].y);
//cout<<ansr[op[i].id]<<endl;
}
for(int i=;i<=q;i++){
if(i!=) cout<<" ";
cout<<ansr[i]-ansl[i];
}
cout<<endl;
}
return ;
}

HDU 6162 Ch’s gift的更多相关文章

  1. HDU 6162 - Ch’s gift | 2017 ZJUT Multi-University Training 9

    /* HDU 6162 - Ch’s gift [ LCA,线段树 ] | 2017 ZJUT Multi-University Training 9 题意: N节点的树,Q组询问 每次询问s,t两节 ...

  2. 2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  3. HDU 6162 Ch’s gift (树剖 + 离线线段树)

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  4. 2017多校第9场 HDU 6162 Ch’s gift 树剖加主席树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6162 题意:给出一棵树的链接方法,每个点都有一个数字,询问U->V节点经过所有路径中l < ...

  5. HDU 6162 Ch's gift(树链剖分+线段树)

    题意: 已知树上的每个节点的值和节点之间的关系建成了一棵树,现在查询节点u到节点v的最短路径上的节点值在l到r之间的节点值的和. 思路: 用树链剖分将树映射到线段树上,线段树上维护3个值,max,mi ...

  6. HDU 6162 Ch’s gift (线段树+树链剖分)

    题意:给定上一棵树,每个树的结点有一个权值,有 m 个询问,每次询问 s, t ,  a, b,问你从 s 到 t 这条路上,权值在 a 和 b 之间的和.(闭区间). 析:很明显的树链剖分,但是要用 ...

  7. L - Ch’s gift HDU - 6162

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  8. hdu6162 Ch’s gift

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6162 题目: Ch’s gift Time Limit: 6000/3000 MS (Java ...

  9. Ch’s gift

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Proble ...

随机推荐

  1. Excel 2007 底层实现方式

    一.EXCEL的底层实现 能力有限,了解的比较浅,有不足之处望指正,首先看下图: 一. excel2007是使用xml格式来存储的,把一个excel文件后缀改为.zip,打开之后就直接可以看到一个ex ...

  2. ASP.NET Core MVC中URL和数据模型的匹配

    Http GET方法 首先我们来看看GET方法的Http请求,URL参数和ASP.NET Core MVC中Controller的Action方法参数匹配情况. 我定义一个UserController ...

  3. ThinkPad T43续命记

    // Description: 原作于2016年8月25日. Mr. Robot 最近有部叫<黑客军团>(Mr. Robot)的戏比较火.目前第二季已经出到一大半了,深得技术宅和技术宅仰慕 ...

  4. 在平衡树的海洋中畅游(二)——Scapegoat Tree

    在平衡树的广阔天地中,以Treap,Splay等为代表的通过旋转来维护平衡的文艺平衡树占了觉大部分. 然而,今天我们要讲的Scapegoat Tree(替罪羊树)就是一个特立独行的平衡树,它通过暴力重 ...

  5. Intel x86_64 Architecture Background 2

    这里是在学习Intel x86_64体系架构时学习到的一些概念,记录下来以供日后参考.如果有错的地方,欢迎指正! CPU上下文切换(context switch): 这个概念第一次听到对我来说是完全陌 ...

  6. 从Stampery到Chronicled,区块链公证业务的实践

    Stampery就是这样一家利用比特币区块链技术代替公证人的创业公司,能为所有的敏感文件提供具有法律约束力的证明.可以用Stampery证明任何文件,它能很好地保护知识产权,证明遗嘱.宣誓.合同.家庭 ...

  7. Peer Programming Project: 4 Elevators Scheduler 学号后三位 157,165

    1.Advantages and disanvantages of Peer Programming advantages The code are constantly validated by t ...

  8. 【CV】ICCV2015_Unsupervised Visual Representation Learning by Context Prediction

    Unsupervised Visual Representation Learning by Context Prediction Note here: it's a learning note on ...

  9. linux及安全第五周总结

    给MenuOS增加time和time-asm命令 中间过程已省略了,我们所做的只是将menu更新 具体命令如下 rm menu -rf 强制删除 git clone http://github.com ...

  10. java中定时执行任务

    现在项目中用到需要定时去检查文件是否更新的功能.timer正好用于此处. 用法很简单,new一个timer,然后写一个timertask的子类即可. 代码如下: package comz.autoup ...