HDU 6162 Ch’s gift(LCA)

Ch’s gift

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



题意:给出结点数为n的一棵树,每个结点有权值;m 次询问,对于每次询问:a,b,l,r,求出结点a到结点b路径经过的点中,权值在在区间[l,r]内的权值和。

题解:倍增LCA,求出最近公共祖先,预处理每个结点的父结点pre[i],从a,b向上走到公共祖先,求和。


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include<stdio.h>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAXN 100005
using namespace std;

const long long DEG = 22;
typedef long long LL;

struct SEdge
{
    LL end;
    LL value;
    LL next;
}edge[MAXN*2];
LL head[MAXN], CntEdge;

LL Dist[MAXN];
LL Pre[MAXN][DEG];
LL Ans[MAXN];
bool Visited[MAXN];
LL pre[MAXN];
int v[MAXN];
void Initial()
{
    memset( head, -1, sizeof(head) );
    memset(pre,0,sizeof(pre));
     memset(Ans,0,sizeof(Ans));
     memset(v,0,sizeof(v));
     memset(Dist,0,sizeof(Dist));
     memset(edge,0,sizeof(edge));
    memset(Visited,0,sizeof(Visited));
    memset(Pre,0,sizeof(Pre));
    CntEdge = 0;
}
void AddEdge( LL start, LL end, LL value )
{
    edge[CntEdge].end = end;
    edge[CntEdge].value = value;
    edge[CntEdge].next = head[start];
    head[start] = CntEdge++;
}
void DFS( LL Now )
{
    for( LL j = 1; j < DEG; j++ )
        Pre[Now][j] = Pre[Pre[Now][j-1]][j-1];

    Visited[Now] = true;

    for( LL i = head[Now]; i != -1; i = edge[i].next )
    {
        LL temp = edge[i].end;

        if( Visited[temp] )
            continue;
        pre[temp]=Now;
        Dist[temp] = Dist[Now] + edge[i].value;
        Pre[temp][0] = Now;
        DFS( temp );
    }
}
LL LCA( LL node1, LL node2 )
{
    LL i;
    if( Dist[node1] < Dist[node2] )
        swap( node1, node2 );
    for( LL i = 0, d=Dist[node1]-Dist[node2]; d; i++, d>>=1 )
    {
        if( d&1 )
            node1 = Pre[node1][i];
    }
    if( node1 == node2 )
        return node1;
    for( LL i = DEG-1; i >= 0; i-- )
        if( Pre[node1][i] != Pre[node2][i] )
            node1 = Pre[node1][i], node2 = Pre[node2][i];

    return Pre[node1][0];
}

LL Distance( LL node1, LL node2 )
{
    return Dist[node1] + Dist[node2] - 2*Dist[LCA(node1, node2)];
}
LL sum;


void dfs(LL p,LL an,LL l,LL r)
{
    if(p==an)
        return;
    if(v[p]>=l&&v[p]<=r)
        sum+=v[p];
    dfs(pre[p],an,l,r);
}
int main()
{
    LL n, q;

    while(scanf("%lld%lld",&n,&q) != EOF)
    {
        Initial();
        for(LL i=1;i<=n;i++)
            scanf("%lld",&v[i]);
        for( LL i = 2; i <= n; i++ )
        {
            LL ss;
            LL end, value;
            scanf("%lld%lld",&ss,&end);
            value = 1;
            AddEdge( ss, end, 1 );
            AddEdge( end, ss, 1 );
        }
        DFS(1);
        for( LL i = 0; i < q; i++ )
        {
            LL a,b,l,r;
            sum=0;
            scanf("%lld%lld%lld%lld",&a,&b,&l,&r);
            if(a==b)
            {
                if(l<=v[a]&&v[a]<=r)
                    sum+=v[a];
                Ans[i]=sum;
                continue;
            }
            LL an=LCA(a,b);
            dfs(a,an,l,r);
            dfs(b,an,l,r);
            if(v[an]>=l&&v[an]<=r) sum+=v[an];
            Ans[i]=sum;
        }
        for( LL i = 0; i < q; i++ )
          {
               printf("%lld", Ans[i]);
               if(i!=q-1) printf(" ");
               else printf("\n");
          }
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值