L - Ch’s gift HDU - 6162
Ch’s gift
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2534 Accepted Submission(s): 887
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=6162
Problem Description
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
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
Sample Input
Sample Output
Source
题意
题解
AC代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100050
#define INF 123456789
int n,m;
int tot,last[N];
ll ans[N];
int cnt,fa[N],dp[N],size[N],son[N],rk[N],kth[N],top[N];
struct Query
{
int l,r,id; ll val;
bool operator <(const Query&b)const
{return val<b.val;}
}a[N],que[N<<];
struct Edge{int from,to,s;}edges[N<<];
struct Tree{int l,r;ll sum;}tr[N<<];
template<typename T>void read(T&x)
{
ll k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void AddEdge(int x,int y)
{
edges[++tot]=Edge{x,y,last[x]};
last[x]=tot;
}
void dfs1(int x,int pre)
{
fa[x]=pre;
dp[x]=dp[pre]+;
size[x]=;
son[x]=;
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==pre)continue;
dfs1(e.to,x);
size[x]+=size[e.to];
if (size[e.to]>size[son[x]])son[x]=e.to;
}
}
void dfs2(int x,int y)
{
rk[x]=++cnt;
kth[cnt]=x;
top[x]=y;
if (son[x]==)return;
dfs2(son[x],y);
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==fa[x]||e.to==son[x])continue;
dfs2(e.to,e.to);
}
}
void bt(int x,int l,int r)
{
tr[x].l=l; tr[x].r=r; tr[x].sum=;
if (l==r)return;
int mid=(l+r)>>;
bt(x<<,l,mid);
bt(x<<|,mid+,r);
}
void update(int x,int p,ll tt)
{
if (p<=tr[x].l&&tr[x].r<=p)
{
tr[x].sum+=tt;
return;
}
int mid=(tr[x].l+tr[x].r)>>;
if (p<=mid)update(x<<,p,tt);
if (mid<p)update(x<<|,p,tt);
tr[x].sum=tr[x<<].sum+tr[x<<|].sum;
}
ll query(int x,int l,int r)
{
if (l<=tr[x].l&&tr[x].r<=r)
return tr[x].sum;
int mid=(tr[x].l+tr[x].r)>>; ll ans=;
if (l<=mid)ans+=query(x<<,l,r);
if (mid<r)ans+=query(x<<|,l,r);
return ans;
}
ll get_sum(int x,int y)
{
int fx=top[x],fy=top[y];ll ans=;
while(fx!=fy)
{
if (dp[fx]<dp[fy])swap(x,y),swap(fx,fy);
ans+=query(,rk[fx],rk[x]);
x=fa[fx]; fx=top[x];
}
if (dp[x]<dp[y])swap(x,y);
ans+=query(,rk[y],rk[x]);
return ans;
}
void work()
{
read(n); read(m);
for(int i=;i<=n;i++)read(a[i].val),a[i].id=i;
for(int i=;i<=n-;i++)
{
int x,y;
read(x); read(y);
AddEdge(x,y);
AddEdge(y,x);
}
int num=;
for(int i=;i<=m;i++)
{
int l,r,x,y;
read(l); read(r); read(x);read(y);
que[++num]=Query{l,r,-i,x-};
que[++num]=Query{l,r,i,y};
}
sort(a+,a+n+);
sort(que+,que+num+);
dfs1(,);
dfs2(,);
bt(,,n);
int ds=;
for(int i=;i<=num;i++)
{
while(ds<=n&&a[ds].val<=que[i].val)
{
update(,rk[a[ds].id],a[ds].val);
ds++;
}
ll sum=get_sum(que[i].l,que[i].r);
if (que[i].id<) ans[-que[i].id]-=sum;
else ans[que[i].id]+=sum;
}
printf("%lld",ans[]);
for(int i=;i<=m;i++)printf(" %lld",ans[i]);
printf("\n");
}
void clear()
{
tot=; cnt=;
memset(last,,sizeof(last));
memset(ans,,sizeof(ans));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
//freopen("my.out","w",stdout);
#endif
while()
{
clear();
work();
}
}
TLE代码(树链剖分+主席树)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100050
#define INF 123456789
int n,m,w[N];ll b[N];
int tot,last[N];
int tree_num,root[N];
int cnt,fa[N],dp[N],size[N],son[N],rk[N],kth[N],top[N];
struct Edge{int from,to,s;}edges[N<<];
struct Tree{int l,r,ls,rs;ll sum;}tr[];
template<typename T>void read(T&x)
{
ll k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void AddEdge(int x,int y)
{
edges[++tot]=Edge{x,y,last[x]};
last[x]=tot;
}
void dfs1(int x,int pre)
{
fa[x]=pre;
dp[x]=dp[pre]+;
size[x]=;
son[x]=;
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==pre)continue;
dfs1(e.to,x);
size[x]+=size[e.to];
if (size[e.to]>size[son[x]])son[x]=e.to;
}
}
void dfs2(int x,int y)
{
rk[x]=++cnt;
kth[cnt]=x;
top[x]=y;
if (son[x]==)return;
dfs2(son[x],y);
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==fa[x]||e.to==son[x])continue;
dfs2(e.to,e.to);
}
}
void bt(int &x,int l,int r)
{
x=++tree_num;
tr[x].l=l; tr[x].r=r; tr[x].sum=;
if (l==r)return;
int mid=(l+r)>>;
bt(tr[x].ls,l,mid);
bt(tr[x].rs,mid+,r);
}
void add(int &x,int last,int p)
{
x=++tree_num;
tr[x]=tr[last];
tr[x].sum+=b[p];
if (tr[x].l==tr[x].r)return;
int mid=(tr[x].l+tr[x].r)>>;
if(p<=mid)add(tr[x].ls,tr[last].ls,p);
else add(tr[x].rs,tr[last].rs,p);
}
ll ask(int x,int y,int p)
{
if (tr[x].r<=p)return tr[y].sum-tr[x].sum;
int mid=(tr[x].l+tr[x].r)>>;ll ans=;
if (<=mid)ans+=ask(tr[x].ls,tr[y].ls,p);
if (mid<p)ans+=ask(tr[x].rs,tr[y].rs,p);
return ans;
}
ll get_sum(int x,int y,int tt)
{
int fx=top[x],fy=top[y];ll ans=;
while(fx!=fy)
{
if (dp[fx]<dp[fy])swap(x,y),swap(fx,fy);
ans+=ask(root[rk[fx]-],root[rk[x]],tt);
x=fa[fx]; fx=top[x];
}
if (dp[x]<dp[y])swap(x,y);
ans+=ask(root[rk[y]-],root[rk[x]],tt);
return ans;
}
void work()
{
read(n); read(m);
int num=;
for(int i=;i<=n;i++)read(w[i]),b[++num]=w[i];
b[++num]=INF;
for(int i=;i<=n-;i++)
{
int x,y;
read(x); read(y);
AddEdge(x,y);
AddEdge(y,x);
}
sort(b+,b+num+);
num=unique(b+,b+num+)-b-;
dfs1(,);
dfs2(,);
bt(root[],,num);
for(int i=;i<=n;i++)
{
int tt=lower_bound(b+,b+num+,w[kth[i]])-b;
add(root[i],root[i-],tt);
}
for(int i=;i<=m;i++)
{
if (i>)printf(" ");
int x,y,l,r;
read(x); read(y); read(l); read(r);
l=lower_bound(b+,b+num+,l)-b-;
r=upper_bound(b+,b+num+,r)-b-;
ll ans=get_sum(x,y,r);
ans-=get_sum(x,y,l);
printf("%lld",ans);
}
printf("\n");
}
void clear()
{
tot=; cnt=; tree_num=;
memset(last,,sizeof(last));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
while()
{
clear();
work();
}
}
L - Ch’s gift HDU - 6162的更多相关文章
- 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 | 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 (树剖 + 离线线段树)
Ch’s gift Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- hdu6162 Ch’s gift
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6162 题目: Ch’s gift Time Limit: 6000/3000 MS (Java ...
- Ch’s gift
Ch’s gift Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Proble ...
- 2017多校第9场 HDU 6162 Ch’s gift 树剖加主席树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6162 题意:给出一棵树的链接方法,每个点都有一个数字,询问U->V节点经过所有路径中l < ...
- 【HDU 6162】 Ch’s gift
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6162 [算法] 离线树剖 我们知道,u到v路径上权值为[A,B]的数的和 = u到v路径上权值小于 ...
- 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 ...
随机推荐
- 【原创】 HBase 配置指南
HBase 默认配置 Centos6.5下Hbase配置 官网配置文档:http://hbase.apache.org/book.html#_configuration_files 中文翻译转自: ...
- PL/SQL 训练03 --异常
--程序员在开发的时候,经常天真的认为这个世界是完美的,用户如同自己般聪明,总能按照自己设想的方式--操作系统输入数据.但残酷的事实告诉我们,这是不可能的事情,用户总会跟我们相反的方式操作系统--于是 ...
- 概念与用法-cookie,session,auth (认证系统)
COOKIE 与 SESSION 概念 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie ...
- python开发模块基础:time&random
一,time模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该首先导入这个模块 常用方法1.(线程)推迟指定的时间运行.单位为秒. time.sleep(1) #括号内为整数 2.获取当前 ...
- WP10通过StreamSocket连接C++服务器
注:当服务端和手机模拟器运行在一台机器时,会有奇怪错误.将服务端放在其它机器上更改客户端连接地址,运行正常.或者直接用本机modern调试也可以. 实例化一个对象 StreamSocket _clie ...
- sata2.0和sata3.0的区别
sata是指电脑主板上的硬盘接口,3.0是2.0的升级版本,发布于2009年,所以2010年之前的电脑主板基本都只提供sata2.0接口,如果你不知道自己的电脑是sata2.0还是sata3.0,想想 ...
- UIWidget
[UIWidget] UIWidget在NGUI中的层次如下. 根据上篇所述,UIRect实现实现了Anchor功能.而Widget提供的功能也很简单,如下: 可以看到,widget只提供四个属性,a ...
- 【转】request的cache-control和response cache-control不同点
原文地址:http://www.cnblogs.com/lwhkdash/archive/2012/11/04/2748291.html HTTP协议中,关于一些头域的解释很模糊,网上的解释有些甚至是 ...
- c语言学习笔记-if语句块一定要加分号
if(a>6) printf("hello");//语句1 printf("world");//语句2 当a>6的时候,执行的分支语句是语句1,而不 ...
- PHP加密与解密
password_hash ( string $password , integer $algo [, array $options ] ) 加密,生成60位得字符串 $algo:一个用来在散列密码时 ...