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 ;
}

最新文章

  1. 【原创】PageAdminCMS 前台SQL注入漏洞(2)
  2. sql编写将时间转换年月日 时分格式
  3. c++ STL 学习记录 草稿。
  4. 【开源】开发者新闻聚合APP 2.0.3发布(第二个稳定版本)
  5. 通过命令行连接Wifi
  6. jQuery中attr()方法用法实例
  7. kb
  8. iOS开发UI篇—无限轮播(循环利用)
  9. 据说是百度ios面试题
  10. sqlite 使用记录
  11. jquery1.11 操作checkbox:全选、取消全选、获取选择元素、获取取消选择元素(总结)
  12. Delphi中TApplication详解
  13. [Bayesian] “我是bayesian我怕谁”系列 - Variational Autoencoders
  14. 【算法】LeetCode算法题-Maximum Subarray
  15. Android开发——adb连接夜神模拟器
  16. [No000012F]WPF(7/7) - 样式,触发器和动画
  17. (C/C++学习笔记) 六. 表达式
  18. Java 打包下载服务器上选中的文件或目录(带进度条提示)
  19. bzoj4025: 二分图 lct
  20. Android图片压缩框架-Tiny 集成

热门文章

  1. 15-(基础入门篇)GPRS(Air202)GPIO控制点亮一个灯
  2. Hive 数据的导入导出
  3. Nowcoder156F 托米的游戏/CF280C Game on tree 期望
  4. WPF,ListView设置分组
  5. Ionic2 播放mp3功能实现
  6. Vue 开发环境搭建 (Mac)
  7. GATT服务搜索流程(二)
  8. DOS文件转换成UNIX文件格式详解
  9. kvm虚拟化管理平台WebVirtMgr部署-完整记录(0)
  10. 树的最长链-POJ 1985 树的直径(最长链)+牛客小白月赛6-桃花