EOJ#3335 Ch’s gift (Hard)【崔先生的礼物(困难)】

单点时限: 3.0 sec内存限制: 256 MB
Mr. Cui is working off-campus and he misses his girlfriend very much. After a whole night tossing and turning, he decides to get to his girlfriend’s city and of course, with well-chosen gifts. He knows neither too low the price could a gift be since his girlfriend won’t like it, nor too high of it since he might consider not worth to do. So he will only buy gifts with price in the range [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 i-th city, there is a specialty of price ci Cui could buy as a gift. Cui buys 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, buys as many as gifts as possible. Now he wants to know, how much money does he need to afford all the gifts
输入格式
The first line contains two integers n,q(2≤n≤105,1≤q≤105), representing the number of cities and the number of queries. The second line contains n integers c1,c2,…,cn(1≤ci≤109), indicating the price of city i‘sspecialty.Then n−1 lines follows. Each line has two integers x,y(1≤x,y≤n,x≠y), meaning there is road between city x and city y.q line follows. In each line, there are four integers s,t,a,b(1≤s,t≤n,1≤a≤b≤109), which indicates start city, end city, lower bound of the price, upper bound of the price, respectively. There is no guarantee for the relationship between s and t

.输出格式

q integers denoting the answers, respectively for q queries. Use any format as you like.

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
output
7 1 4

题目大意是这样:给你一棵n个节点的树,每个点有一个权值,q个询问,每个询问,让你求出从s到t的路径上所有在[a,b]之内的权值的和。
第一行是n和q
第二行是权值
接下来n-1行,每行一个x,y表示在树上有一条边连接x,y
然后q行是询问s,t,a,b

kk这个题写了好久(哭~)
首先考虑了如果不是一棵树,而是针对一个序列,每次给出一个子序列的左端点l和右端点r,求[a.b]内的权值和。
可能是因为kk比较菜,所以只想到了一种解法······
因为要同时对两方面进行限制——权值大小和下标范围,所以我先想到了主席树(可持久化线段树)
主席树的思路是建立n个版本的线段树,每个版本进行操作时下标是离散化之后的值——没学过主席树的同学可能有点懵,后面kk会再写一篇主席树的博客~
所以在这里我们借用主席树的思路,建立n个版本的线段树,第i个版本代表的是对于序列1~i,将权值加到对应离散化之后的位置之后求的和
有点绕口,我们不妨举个例子
假设n=6,序列为1 4 7 3 10 3
离散化之后就是1 3 4 2 5 2
那么对于第4个版本和第6个版本:
鼠标画的图,不太好看,凑合着看吧·····方便起见,没有画出整个树,只要有一个序列作为参照就行
具体求和的时候呢,就先对于a和b进行一次二分判定应该求哪个区间的和,然后先求出第r个版本的和,再减去第l-1个版本的和,就是答案

那么现在问题扩展到了树上——那怎么办呢?
好办,因为我们相当于是对树上所有点建立一个可持久化线段树,为了保证求解方便,我们用树剖对整棵树剖分一下

完事之后,就边求LCA边算和就行了

复杂度O(nlogn),有点大了嘿嘿嘿,但是在EOJ上能过

代码如下:(代码后面附了一组数据,没有给出答案,不过聪明的你肯定能用计算机解决啦)

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rt long long
#define N 100000
#define inf 1e18
using namespace std;
inline rt read()
{
 register rt u=0LL,v=0LL;register char ch=getchar();
 while(!isdigit(ch)){if(ch=='-')v=1LL;ch=getchar();}
 while(isdigit(ch)){u=u*10+ch-'0';ch=getchar();}
 return v?-u:u;
}
rt n,q,a,b,s,t,c[N+10],tot,ver[200010],nxt[200010],hed[N+10];
rt siz[N+10],top[N+10],fa[N+10],son[N+10],dep[N+10];
rt bn[N+10],bv[N+10],bbv[N+10],cn[N+10];
rt maxn=-inf,minn=inf;
void add(rt x,rt y)
{
 ++tot;
 ver[tot]=y;
 nxt[tot]=hed[x];
 hed[x]=tot;
}
struct node
{
 rt value,rank;rt x; 
}wck[N+10];
bool cmp(node x,node y){return x.value<y.value;}
void dfs1(rt x)
{
 siz[x]=1LL;
 for(int i=hed[x];i;i=nxt[i])
 if(fa[x]!=ver[i])
 {
  fa[ver[i]]=x;
  dep[ver[i]]=dep[x]+1;
  dfs1(ver[i]);
  siz[x]+=siz[ver[i]];
  if(siz[ver[i]]>siz[son[x]])
  son[x]=ver[i];
 }
}
void dfs2(rt x,rt t)
{
// cout<<x<<endl;
// system("pause");
 top[x]=t;
 bn[++tot]=x;
 bv[tot]=c[x];//具体值
 //bbv:离散化之后的值 
 cn[x]=tot;
 if(!son[x])return ;
 dfs2(son[x],t);
 for(int i=hed[x];i;i=nxt[i])
 if(ver[i]!=fa[x]&&ver[i]!=son[x])
 dfs2(ver[i],ver[i]);
}
struct trnode
{
 rt lc,rc;rt c;
}tr[N*20+10];
rt len=0LL,wbn[N+10];
void add2(rt l,rt r,rt &root,rt x,rt v)
{
 if(root==0LL)root=++len;
 tr[root].c+=v;
 if(l==r)return ;
 rt mid=(l+r)/2LL;
 if(x<=mid)add2(l,mid,tr[root].lc,x,v);
 else add2(mid+1,r,tr[root].rc,x,v);
}
void merge(rt &x,rt y)
{
 if(y==0LL)return ;
 if(x==0LL)
 {
  x=y;
  return ;
 }
 tr[x].c+=tr[y].c;
 merge(tr[x].lc,tr[y].lc);
 merge(tr[x].rc,tr[y].rc);
}
rt query(rt l,rt r,rt root,rt aa,rt bb)
{
 if(l>=aa&&r<=bb)
 return tr[root].c;
 rt mid=(l+r)/2LL;
 rt ans=0LL;
 if(aa<=mid)ans+=query(l,mid,tr[root].lc,aa,bb);
 if(bb>mid)ans+=query(mid+1,r,tr[root].rc,aa,bb);
 return ans;
}
rt EF(rt xx,int flag)
{
 if(xx>maxn)return flag ? n:n+1;
 if(xx<minn)return flag ? 0:1;
 rt l=1LL,r=n;
 while(l<=r)
 {
  rt mid=(l+r)/2LL;
  if(wck[mid].value>xx)r=mid-1;
  else if(wck[mid].value==xx)return wck[mid].x;
  else l=mid+1; 
 }
 if(flag)return wck[r].x;
 return wck[l].x;
}
rt lca(rt x,rt y,rt aa,rt bb)//求[aa,bb]之间的和 
{
 rt fx=top[x],fy=top[y];
 rt anss=0;
// cout<<aa<<' '<<bb<<endl;
 if(aa>bb)return 0LL; 
 while(fx!=fy)
 {
  if(dep[fx]<dep[fy])
  {
   anss+=query(1,n,wbn[cn[y]],aa,bb);
   anss-=query(1,n,wbn[cn[fy]-1],aa,bb);
   y=fa[fy];fy=top[y];//之前写的是y=fa[y]和x=fa[x],结果调了一晚上愣是没找出来 
  }
  else
  {
   anss+=query(1,n,wbn[cn[x]],aa,bb);
   anss-=query(1,n,wbn[cn[fx]-1],aa,bb);
  // cout<<x<<' '<<fx<<endl;
   x=fa[fx];fx=top[x];
  }
 }
 if(dep[x]<dep[y])swap(x,y);
 anss+=query(1,n,wbn[cn[x]],aa,bb);
 anss-=query(1,n,wbn[cn[y]-1],aa,bb);
// cout<<x<<' '<<y<<endl;
 return anss;
}
int main()
{
 n=read(),q=read();
 for(int i=1;i<=n;i++)c[i]=read(),maxn=max(maxn,c[i]),minn=min(minn,c[i]);
 for(int i=1;i<n;i++)
 {
  a=read(),b=read();
  add(a,b),add(b,a);
 }
 fa[1]=1;
 top[1]=1;
 dep[1]=1;
 dfs1(1);
 tot=0;
 dfs2(1,1);//树链剖分->lca、压缩 
 for(int i=1;i<=n;i++)
 wck[i].value=c[i],wck[i].rank=i;
 sort(wck+1,wck+n+1,cmp);
 for(int i=1;i<=n;i++)
 {
  if(wck[i].value!=wck[i-1].value)
  bbv[cn[wck[i].rank]]=bbv[cn[wck[i-1].rank]]+1;
  else bbv[cn[wck[i].rank]]=bbv[cn[wck[i-1].rank]];
  wck[i].x=bbv[cn[wck[i].rank]];
 }
 //按照树剖后的顺序离散化
 //wck数组的x是用来后面二分确定大小的 
 for(int i=1;i<=n;i++)
 {
  add2(1,n,wbn[i],bbv[i],bv[i]);
  merge(wbn[i],wbn[i-1]); 
 }
 for(int i=1;i<=q;i++)
 {
  s=read(),t=read(),a=read(),b=read();
  printf("%lld ",lca(s,t,EF(a,0),EF(b,1)));
 }
 return 0;
}
/*
5 3
2441 16241 16241 38661 87445
1 2
3 2
4 1
4 5
3 5 2000 90000
4 5 22345 58621
1 2 2496 17300
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值