地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6162

题目:

Ch’s gift

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 526    Accepted Submission(s): 177

Problem Description
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
 
Source

思路:

  裸的树链剖分套主席树。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e5+;
const int mod=1e9+; vector<int>mp[K];
int top[K],sz[K],fa[K],son[K],id[K],hid[K],deep[K];
int cnt,n,q; void dfs1(int x,int f)
{
sz[x]=,fa[x]=f,son[x]=-,deep[x]=deep[f]+;
for(int i=;i<mp[x].size();i++)
if(mp[x][i]!=f)
{
dfs1(mp[x][i],x);
sz[x]+=sz[mp[x][i]];
if(son[x]==-||sz[son[x]]<sz[mp[x][i]])
son[x]=mp[x][i];
}
}
void dfs2(int x,int f) ///每条边用深度大的节点的序号表示
{
top[x]=f,id[x]=++cnt,hid[id[x]]=x;
if(son[x]!=-) dfs2(son[x],f);
for(int i=;i<mp[x].size();i++)
if(mp[x][i]!=fa[x]&&mp[x][i]!=son[x])
dfs2(mp[x][i],mp[x][i]);
} int tot,ls[K*],rs[K*],rt[K*];
int a[K],b[K];
LL sum[K*];
//sum[o]记录的是该节点区间内出现的数的和
//区间指的是将数离散化后的区间
void build(int &o,int l,int r)
{
o=++tot,sum[o]=;
int mid=l+r>>;
if(l!=r)
build(ls[o],l,mid),build(rs[o],mid+,r);
}
void update(int &o,int l,int r,int last,int x)
{
o=++tot,sum[o]=sum[last]+b[x];
ls[o]=ls[last],rs[o]=rs[last];
if(l==r) return ;
int mid=l+r>>;
if(x<=mid) update(ls[o],l,mid,ls[last],x);
else update(rs[o],mid+,r,rs[last],x);
}
LL query(int lo,int ro,int l,int r,int k)
{
if(k<) return ;
if(r<=k) return sum[ro]-sum[lo];
int mid=l+r>>;
if(k<=mid) return query(ls[lo],ls[ro],l,mid,k);
return query(rs[lo],rs[ro],mid+,r,k)+sum[ls[ro]]-sum[ls[lo]];
}
LL tree_query(int x,int y,int l,int r,int sz)
{
LL ret=;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
ret+=query(rt[id[top[x]]-],rt[id[x]],,sz,r)-query(rt[id[top[x]]-],rt[id[x]],,sz,l-);
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
ret+=query(rt[id[x]-],rt[id[y]],,sz,r)-query(rt[id[x]-],rt[id[y]],,sz,l-);
return ret;
}
int main(void)
{
//freopen("in.acm","r",stdin);
//freopen("out.acm","w",stdout);
while(~scanf("%d%d",&n,&q))
{
cnt=tot=;
memset(mp,,sizeof mp);
for(int i=;i<=n;i++) scanf("%d",a+i),b[i]=a[i];
for(int i=,x,y;i<n;i++)
scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
sort(b+,b++n);
int sz=unique(b+,b++n)-b-;
for(int i=;i<=n;i++)
a[i]=lower_bound(b+,b++sz,a[i])-b;
dfs1(,);
dfs2(,);
build(rt[],,sz);
for(int i=;i<=n;i++)
update(rt[i],,sz,rt[i-],a[hid[i]]);
// for(int i=1;i<=n;i++)
// printf("id[%d]=%d ",i,id[i]);
// printf("\n");
for(int i=,u,v,l,r,tmp;i<=q;i++)
{
scanf("%d%d%d%d",&u,&v,&l,&r);
l=lower_bound(b+,b++sz,l)-b;
tmp=lower_bound(b+,b++sz,r)-b;
if(b[tmp]>r||tmp>sz) r=tmp-;
else r=tmp;
printf("%lld%c",tree_query(u,v,l,r,sz),i==q?'\n':' ');
}
}
return ;
}

最新文章

  1. 11月6日下午PHP分页查询(查询结果也显示为分页)
  2. 获取JAVA[WEB]项目相关路径的几种方法
  3. [Reprint] C++函数模板与类模板实例解析
  4. 【LeetCode】8. String to Integer (atoi) 字符串转整数
  5. C盘清理大作战
  6. [Angualr 2] Watch for changes
  7. Redhat Enterprise 5.4下安装配置Oracle 11g R2详细过程
  8. ajax上传文件进度条
  9. chrome插件 postman 可以调用restful服务
  10. Caffe+VS2015+python3的安装(基于windows)
  11. centos7防火墙开放端口等命令
  12. cd4与cd8比值的意义
  13. 生成线上用https证书,支持通配符和多域名,初学Let’s Encrypt用于IIS,纯本地手动
  14. deepin系统安装成功了之后重启电脑没有deepin启动选项的简单解决办法
  15. 【Python】keras神经网络识别mnist
  16. 关于Dubbo异常之Data length too large
  17. Java Integer常量池
  18. scala (8) 模糊匹配
  19. 给idea配置默认的java jdk
  20. RMI远程方法调用

热门文章

  1. android 清除缓存功能
  2. switch语句相关
  3. 在用VMware虚拟机的时候,有时会发现打开虚拟机时提示“该虚拟机似乎正在使用中。如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权。否则,请按“取消(C)”按钮以防损坏。配置文件: D:\win10x64\Windows 10 x64.vmx。”这是由于虚拟机未正常关闭引起的,下面看看解决办法
  4. phpcms替换类列表页,内容页,主页
  5. WEB安全番外第三篇--关于XXE
  6. UIWindow和UIScreen
  7. 160229-02、Sublime Text 3 快捷键总结
  8. 【转】虚拟机下CentOS7开启SSH连接
  9. 品友互动大数据平台的技术演化 https://www.sohu.com/a/191202836_99982360
  10. 与python的第一次邂逅