HDU6162-Ch’s gift((离线+树链剖分)或(在线+树上主席树))

本文介绍了一种解决树上路径权值和问题的方法,通过树链剖分结合主席树实现高效查询。具体地,文章给出了两种实现思路及其代码示例:一种是基于线段树的树链剖分维护;另一种则是利用LCA算法配合简单的递归遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ch’s gift

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 693 Accepted Submission(s): 241

Problem Description
Mr. 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
There are multiple cases.

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
Output m space-separated integers in one line, and the ith number should be the answer to the ith situation.

Sample 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

Sample Output
7 1 4

Source
2017 Multi-University Training Contest - Team 9

Recommend
liuyiding | We have carefully selected several similar problems for you: 6170 6169 6168 6167 6166

题意:

给定一颗树
每一棵节点都有一个权值,再有m个询问,每次询问u,v的树上路径权值在[a,b]范围内的权值和是多少

考虑不太用想的的方法,对于一棵树的每个儿子去建权值线段树,为了支持区间内的查询,要取出树的DFS序,建权值线段树的时候利用主席树的思想,每次一个点只建一条链,根据他父亲的版本去更新,这样做需要先把所有点的权值离散完再去做,建完树上主席树以后,
问题就简单了,对于每个询问,求出LCA,然后答案所在区间就是左右两个城市之间的主席树相减,写起来可能会比较复杂。

稍微想一下,这题不需要进行修改(当然单纯的主席树也不支持修改,要修改还要套一棵树),既然不需要修改那么无论什么时候查询区间内的值都是一样的对吧,那么我们把每个询问拆成两个询问,
[a,b]=[1,b][1,a1]
然后把询问按照右端点排序(左端点都是1没什么意义),再把每个点的权值按照权值大小排序,每次对于一个询问,把权值<=询问右区间的点全部扔到树上去更新,用树链剖分维护树上路径权值和,然后加到答案上即可,(询问在记一个应该加还是应该减)

下面给出树状数组维护树链剖分的代码
复杂度O((n+m)log(n))

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
int n,m,start,end,cnt,k,index,opt;
int fa[maxn],st[maxn],dep[maxn];
int top[maxn],son[maxn],size[maxn],id[maxn];
LL T[maxn],ans[maxn];
int a,b,x,y,z;

struct node
{
  int ed,nxt;
}v[maxn*2];

struct Lv
{
  int d,id;
}w[maxn];

struct Quiry
{
  int u,v,val,id,ty;
}Q[maxn*2];

inline bool cmp(const Lv&A,const Lv&B)
{
  return A.d<B.d;
}

inline bool cmp2(const Quiry&A,const Quiry&B)
{
  return A.val<B.val;
}

inline void add(int x,int y)
{
  v[cnt]=(node){y,st[x]};
  st[x]=cnt++;
}

inline void dfs(int u,int f)
{
  fa[u]=f;
  son[u]=0;size[u]=1;dep[u]=dep[f]+1;
  int ma=0;
  for(int x=st[u];~x;x=v[x].nxt)
  {
    int y=v[x].ed;
    if(y!=f)
    {
      dfs(y,u);
      size[u]+=size[y];
      if(size[y]>ma)son[u]=y;
    }
  }
}

inline void DFS(int u,int tp)
{
  top[u]=tp;
  id[u]=++index;
  if(son[u])DFS(son[u],tp);
  for(int x=st[u];~x;x=v[x].nxt)
  {
    int y=v[x].ed;
    if(y!=fa[u]&&y!=son[u])DFS(y,y);
  }
}

inline void update(int x,LL val)
{
  for(;x<=n;x+=x&-x)T[x]+=val;
}

inline LL get(int x)
{
  LL res=0;
  for(;x;x-=x&-x)res+=T[x];
  return res;
}

inline LL quiry(int u,int v)
{
  int x=top[u],y=top[v];
  LL ans=0;
  while(x!=y)
  {
    if(dep[x]<dep[y])swap(x,y),swap(u,v);
    ans+=get(id[u])-get(id[x]-1);
    u=fa[x];
    x=top[u];
  }
  if(u==v)return ans+get(id[u])-get(id[u]-1);
  if(dep[u]<dep[v])swap(u,v);
  ans+=get(id[u])-get(id[v]-1);
  return ans;
}

int main()
{
  while(~scanf("%d%d",&n,&m))
  {
    memset(st,-1,sizeof(st));
    cnt=0;
    for(int i=1;i<=n;++i)
    {
      scanf("%d",&w[i].d);
      w[i].id=i; 
    }
    sort(w+1,w+1+n,cmp);
    for(int i=1;i<n;++i)
    {
      scanf("%d%d",&x,&y);
      add(x,y);
      add(y,x);
    }
    dfs(1,0);
    index=0;
    DFS(1,1);
    cnt=0;
    for(int i=1;i<=m;++i)
    {
      scanf("%d%d%d%d",&x,&y,&a,&b);
      Q[++cnt]=(Quiry){x,y,a-1,i,-1};
      Q[++cnt]=(Quiry){x,y,b,i,1};
    }

    sort(Q+1,Q+1+cnt,cmp2);

    memset(T,0,sizeof(T));
    memset(ans,0,sizeof(ans));
    int L=1;
    for(int i=1;i<=cnt;++i)
    {
      while(L<=n&&w[L].d<=Q[i].val)
      {
        update(id[w[L].id],w[L].d);
        L++;
      }
      ans[Q[i].id]+=(LL)Q[i].ty*quiry(Q[i].u,Q[i].v);
    }
    for(int i=1;i<m;++i)printf("%lld ",ans[i]);
    printf("%lld\n",ans[m]);
  }
  return 0;
}

_→我才不会说大力出奇迹,现场暴力爬树链跑得比标算快多了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
const int  N =100010;
vector<int> g[N];
int fa[N][20],h[N],w[N];
int n,Q,m,k,a,b;

void dfs(int x,int p)
{
    h[x]=h[p]+1;
    fa[x][0]=p;
    for(int i=1;i<20;++i) fa[x][i]=fa[fa[x][i-1]][i-1];
    int y;
    for(int i=0;i<g[x].size();++i)
    {
        y=g[x][i];
        if(y==p) continue;
        dfs(y,x);
    }
}
int lca(int x,int y)
{
    if(h[x]<h[y]) swap(x,y);
    int d=h[x]-h[y];
    for(int i=0;i<20;++i)
        if((d>>i)&1) x=fa[x][i];
    if(x==y) return x;
    for(int i=19;i>=0;--i)
        if(fa[x][i]!=fa[y][i])
        {
            x=fa[x][i];y=fa[y][i];
        }
    return fa[x][0];
}
int main()
{
    while(~scanf("%d%d",&n,&Q))
    {
        for(int i=1;i<=n;++i) g[i].clear();
        for(int i=1;i<=n;++i) scanf("%d",w+i);
        int x,y;
        for(int i=1;i<n;++i)
        {
            scanf("%d%d",&x,&y);
            g[x].push_back(y);
            g[y].push_back(x);
        }
        dfs(1,0);
        int u,v;
        while(Q--)
        {
            scanf("%d%d%d%d",&u,&v,&a,&b);
            int LCA=lca(u,v);
            LL cnt=0;
            for(int x=u;x!=LCA;x=fa[x][0])if(w[x]>=a&&w[x]<=b)cnt+=(LL)w[x];
            for(int x=v;x!=LCA;x=fa[x][0])if(w[x]>=a&&w[x]<=b)cnt+=(LL)w[x];
            if(w[LCA]>=a&&w[LCA]<=b)cnt+=(LL)w[LCA];
            printf("%lld",cnt);
            if(Q)printf(" ");
        }
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值