HDU6162-Ch’s gift

本文介绍了一道算法题目,利用主席树与树链剖分技术解决路径上权值求和的问题。具体地,给定一棵包含多个节点的树及一系列查询,要求找到指定路径上权值位于给定范围内的节点权值之和。

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

Ch’s gift

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


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个节点n-1条边,每个节点都有一个权值。然后m次查询输入x,y,a,b意思是从节点到x,y这条路径

上的所有权值在区间[a,b]内的权值和

解题思路:主席树+树链剖分,对每一个点建一棵树即可


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <unordered_map>
#include <functional>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

int n, m, a, b, u ,v;
int ss[200009], L[200009 * 40], R[200009 * 40], top[200009];
int s[200009], nt[400009], e[400009], cnt;
int ct[200009], mx[200009], fa[200009], dep[200009];
LL c[200009], sum[200009 * 40] ,ans[200009];

void add(int pre, int &now, int l, int r, LL p)
{
    now = ++cnt, L[now] = L[pre], R[now] = R[pre], sum[now] = sum[pre] + p;
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (p <= mid) add(L[pre], L[now], l, mid, p);
    else add(R[pre], R[now], mid + 1, r, p);
}

LL query(int pre, int now, int l, int r, int p)
{
    if(l==r) return sum[pre]-sum[now];
    LL ans=0;
    int mid=(l+r)>>1;
    if(p<=mid) ans+=query(L[pre], L[now], l, mid, p);
    else ans+=sum[L[pre]]-sum[L[now]]+query(R[pre], R[now], mid+1, r, p);
    return ans;
}

void dfs(int k, int f)
{
    add(ss[f], ss[k], 0, 1000000000, c[k]);
    dep[k] = dep[f] + 1;
    fa[k] = f, ct[k] = 1, mx[k] = 0;
    for (int i = s[k]; ~i; i = nt[i])
    {
        if(e[i]==f) continue;
        dfs(e[i], k);
        ct[k] += ct[e[i]];
        if (ct[e[i]] > ct[mx[k]]) mx[k] = e[i];
    }
}

void Dfs(int k, int t)
{
    top[k] = !t ? k : top[fa[k]];
    if (mx[k]) Dfs(mx[k], 1);
    for (int i = s[k]; ~i; i = nt[i])
    {
        if (e[i] == fa[k] || e[i] == mx[k]) continue;
        Dfs(e[i], 0);
    }
}

LL solve(int x, int y, int k)
{
    LL ans=0;
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        ans+=query(ss[x], ss[fa[top[x]]], 0, 1000000000, k);
        x = fa[top[x]];
    }
    if (dep[x] > dep[y]) swap(x, y);
    ans+=query(ss[y], ss[fa[x]], 0, 1000000000, k);
    return ans;
}

int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        for(int i = 1;i <= n; i++) scanf("%lld", &c[i]);
        memset(s, -1, sizeof s);
        ss[0] = L[0] = R[0] = dep[0] = sum[0] = ct[0] = cnt = 0;
        for (int i = 1; i < n; i++)
        {
            scanf("%d %d", &u, &v);
            nt[cnt] = s[u], s[u] = cnt, e[cnt++] = v;
            nt[cnt] = s[v], s[v] = cnt, e[cnt++] = u;
        }
        dfs(1, cnt=0);
        Dfs(1, 0);
        int flag=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d%d",&u,&v,&a,&b);
            LL ans1=solve(u,v,b),ans2=solve(u,v,a-1);
            ans[i]=ans1-ans2;
        }
        for(int i=1;i<=m;i++)
        {
            if(flag) printf(" ");
            else flag=1;
            printf("%lld",ans[i]);
        }
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值