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 2527:Safe Or Unsafe(数据结构,哈夫曼树,求WPL)
Safe Or Unsafe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 漫游Kafka实战篇之搭建Kafka运行环境(2)
接下来一步一步搭建Kafka运行环境. Step 1: 下载Kafka 点击下载最新的版本并解压. > tar -xzf kafka_2.9.2-0.8.1.1.tgz > cd kafk ...
- Microsoft Word、Excel、PowerPoint转Pdf
Worksheet.ExportAsFixedFormat Method Mark: The ExportAsFixedFormat method is used to publish a workb ...
- hdu 1561(树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1561 思路:dp[u][i]表示以u为根的树选了i个子节点. #include<iostream ...
- Java各种日期格式的获取和设置指定日期
因为近期在做一个项目,发现项目中日期设置的bug,于是查阅了多方资料后.最终攻克了,为此写篇总结.方便日后的查阅. 多的不说了.直接上代码 package com.example.testdate; ...
- 实际用户ID,有效用户ID,保存的设置用户ID
Unix中常见的几个概念,下面做一个解释. 首先需要明确一点,这几个概念都是和进程相关的. real user ID表示的是实际上进程的执行者是谁,effective user ID主要用于校验该进程 ...
- leetcode -- Best Time to Buy and Sell Stock III TODO
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- UE4打包程序没有声音-需要安装UE4PrereqSetup_x64.exe
一个UE4工程打包之后,放到一台新机器,最好安装一下UE4自带的Prerequisites,否则可能会出现没有声音的问题 此安装程序位于WindowsNoEditor\Engine\Extras\Re ...
- MySQL- INSTR 函数的用法
测试数据库: MYSQL数据库 INSTR(STR,SUBSTR) 在一个字符串(STR)中搜索指定的字符(SUBSTR),返回发现指定的字符的位置(INDEX); STR 被搜索的字符串 SUBST ...
- Storm基础概念与单词统计示例
Storm基本概念 Storm是一个分布式的.可靠地.容错的数据流处理系统.Storm分布式计算结构称为Topology(拓扑)结构,顾名思义,与拓扑图十分类似.该拓扑图主要由数据流Stream.数据 ...