hdu6162 Ch’s gift
地址: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
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?
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
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
思路:
裸的树链剖分套主席树。
#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 ;
}
hdu6162 Ch’s gift的更多相关文章
- 【主席树】【最近公共祖先】hdu6162 Ch’s gift
		
题意:一棵树,每个点有个权值,m次询问,每次给你一条链和两个值a,b,问你这条链上权值在[a,b]之间的权值的和是多少. std竟然是2个log的……完全没必要链剖,每个结点的主席树从其父节点转移过 ...
 - 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 ...
 - HDU 6162 Ch’s gift (树剖 + 离线线段树)
		
Ch’s gift Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
 - 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 ...
 - Ch’s gift
		
Ch’s gift Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Proble ...
 - 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两节 ...
 - HDU 6162 Ch’s gift
		
Mr. Cui is working off-campus and he misses his girl friend very much. After a whole night tossing a ...
 - HDU 6162 Ch's gift(树链剖分+线段树)
		
题意: 已知树上的每个节点的值和节点之间的关系建成了一棵树,现在查询节点u到节点v的最短路径上的节点值在l到r之间的节点值的和. 思路: 用树链剖分将树映射到线段树上,线段树上维护3个值,max,mi ...
 - HDU 6162 Ch’s gift (线段树+树链剖分)
		
题意:给定上一棵树,每个树的结点有一个权值,有 m 个询问,每次询问 s, t , a, b,问你从 s 到 t 这条路上,权值在 a 和 b 之间的和.(闭区间). 析:很明显的树链剖分,但是要用 ...
 
随机推荐
- hdu 1421:搬寝室(动态规划 DP + 排序)
			
搬寝室 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
 - 推荐一个 HTML5在线的流程图工具——ProcessOn
			
一直想找个简单好用的UML建模工具,无意在茫茫百度中看见了网友推荐的N多工具,从中找了一个叫 ProcessOn 的工具,可以说非常棒.如果我是WEB开发人员,我肯定去深入研究HTML5啦,太令人着迷 ...
 - Scilab 的画图函数(3)
			
我们在做数据画图或函数图像时常常须要使用对数坐标系.尤其是数据的范围跨越非常多个数量级时.通常的线性坐标系下无法表现出数据特征. Scilab 中Plot函数无法画出对数坐标.须要使用 plot2d ...
 - ios开发之--NSMutableParagraphStyle与NSParagraphStyle的使用
			
在ios6以后,苹果官方建议用“- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options ...
 - 打印系统所有的PID
			
#!/usr/bin/env python #-*- coding:utf-8 -*- ''' 打印系统所有的PID ''' import os def get_all_pid(): for pid ...
 - 第十三篇:带缓冲的IO( 标准IO库 )
			
前言 在之前,学习了 read write 这样的不带缓冲IO函数. 而本文将讲解标准IO库中,带缓冲的IO函数. 为什么要有带缓冲IO函数 标准库提供的带缓冲IO函数是为了减少 read 和 wri ...
 - Kafka+SpringMVC+Maven应用示例
			
本文借助主流SpringMVC框架向大家介绍如何在具体应用中简单快捷的使用kafka.kafka.maven以及SpringMVC在现在的企业级应用中都占据着非常重要的地位,所以本文将三者结合起来也可 ...
 - 160330、Mybatis整合Spring
			
转自csdn文章 http://haohaoxuexi.iteye.com/blog/1843309 Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前, ...
 - CSS 中 BEM命名方式
			
BEM的意思就是块(block).元素(element).修饰符(modifier),是一种CSS Class 命名方法. 类似于: .block{} .block__element{} .block ...
 - TOMCAT------>web资源访问
			
1.web应用达成war包 通过命令行打war包:jar -cvf xxx.war xxx 因为放到webapps里电脑会自动识别,自动解压 2.relodeable="true" ...