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 之间的和.(闭区间). 析:很明显的树链剖分,但是要用 ...
随机推荐
- VC++ 轻松实现“闪屏” SplashWnd
我们平时使用的好多软件在运行启动时都会有一个“闪屏”画面显示,一般用于标识软件的一些信息,如软件版本名称.公司等,通过查找资料发现,其实实现起来很简单,一个类就能搞定! SplashWnd.h C+ ...
- Android无线测试之—UiAutomator UiScrollable API介绍六
向前与向后滚动API 一.向前与向后滚动相关API 返回值 API 描述 boolean scrollBackward(int steps) 自动以步长向后滑动 boolean scrollBackw ...
- 《C++ Primer Plus》学习笔记 2.1.1 main()函数
main()函数的基本结构如下: int main() { statements ; } 这几行代码构成了函数定义(function definition),该定义由两部分组成: 第一行int mai ...
- Java Web项目--使用Servlet生成一个页面
为了生成一个servlet对应的网页.我们需要新建一个web.xml,其中将会放置servlet的相关信息.web.xml文件放置在WebContent/WEB-INF/目录下.(我们在Eclipe中 ...
- handlebears使用
Handlebars 文档笔记: http://www.ghostchina.com/handlebars-wen-dang-bi-ji/ Handlebars模板引擎中的each嵌套及源码浅读: h ...
- Imageview如何设置背景颜色
ImageView.setBackgroundColor(android.graphics.Color.parseColor("#ffffff")); ImageView.setB ...
- 重启svn
重启svn svnserve -d -r /svn -d表示后台运行 -r 指定根目录是 /u02/svn
- 很好用的canvas
一.基本用法 1.要使用canvas元素,需要先给定其width和height来设置绘图区域的大小.canvas中间的文本会在浏览器不支持canvas的情况下显示出来. <canvas widt ...
- ZOJ 3605 Find the Marble(dp)
Find the Marble Time Limit: 2 Seconds Memory Limit: 65536 KB Alice and Bob are playing a game. ...
- Spring WebSocket实现消息推送
第一步: 添加Spring WebSocket的依赖jar包 (注:这里使用maven方式添加 手动添加的同学请自行下载相应jar包放到lib目录) <!-- 使用spring websocke ...