hdu 6162 树链剖分

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次查询,每次查询x, y, a, b表示节点x到y的路径上所有在[a, b]范围内的权值和


思路:树链剖分裸题,利用树链剖分将树中点映射到数组,然后建立线段树,维护一下最大值,最小值,区间和就可以了


ac代码:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+1000;
typedef long long LL;
struct M{
    int x,y;
    LL a,b;
}A[N];
struct node{
    int u,v;
    int nex;
}eage[N*3];
int head[N],cnt,idx;
LL c[N],val[N];
int deep[N],id[N],son[N],siz[N],fa[N],top[N];
LL sum[N*4],mx[N*4],mi[N*4];
void ADD(int u,int v){
    eage[cnt].u = u;
    eage[cnt].v = v;
    eage[cnt].nex = head[u];
    head[u] = cnt++;
}
void dfs1(int u,int pre,int dep){
    deep[u] = dep;
    siz[u] = 1;
    son[u] = 0;
    fa[u] = pre;
    for(int i = head[u];i!=-1;i=eage[i].nex){
        int v = eage[i].v;
        if(v == pre)continue;
        dfs1(v,u,dep+1);
        siz[u]+=siz[v];
        if(siz[son[u]]<siz[v])
            son[u] = v;
    }
}
void dfs2(int u,int tp){
    top[u] = tp;
    id[u] = ++idx;
    if(son[u])dfs2(son[u],tp);
    for(int i = head[u];i!=-1;i=eage[i].nex){
        int v = eage[i].v;
        if(v == fa[u]||v == son[u])continue;
        dfs2(v,v);
    }
}
void TreeWork(int n){
    dfs1(1,0,0);
    dfs2(1,1);
    for(int i=1;i<=n;i++){
        val[id[i]] = c[i];
    }
}
void build(int u,int l,int r){
    if(l==r){
        mi[u]=mx[u]=sum[u]=val[l];
        return ;
    }
    int mid = (l+r)>>1;
    build(u<<1,l,mid);
    build(u<<1|1,mid+1,r);
    sum[u] = sum[u<<1] + sum[u<<1|1];
    mi[u] = min(mi[u<<1],mi[u<<1|1]);
    mx[u] = max(mx[u<<1],mx[u<<1|1]);
}
LL query(int u,int l,int r,int L,int R,LL a,LL b){
    if(L<=l&&r<=R){
        if(mi[u]>b||mx[u]<a)return 0;
        if(mi[u]>=a&&mx[u]<=b)return sum[u];
    }
    int mid = (l+r)>>1;
    LL ans = 0;
    if(L<=mid)
        ans+=query(u<<1,l,mid,L,R,a,b);
    if(R>mid)
        ans+=query(u<<1|1,mid+1,r,L,R,a,b);
    return ans;
}
LL solve(int u,int v,LL a,LL b,int n){
    int tp1 = top[u],tp2 = top[v];
    LL ans = 0;
    while(tp1!=tp2){//不同链的往一条链上凑
        if(deep[tp1]<deep[tp2]){
            swap(tp1,tp2);
            swap(u,v);
        }
        ans+=query(1,1,n,id[tp1],id[u],a,b);
        u = fa[tp1];
        tp1 = top[u];
    }
    if(u==v){
        if(c[u]>=a&&c[u]<=b)ans+=c[u];
        return ans;
    }
    if(deep[u]>deep[v])swap(u,v);
    ans+=query(1,1,n,id[u],id[v],a,b);
    return ans;
}
void SegmentTree(int n,int m){
    build(1,1,n);
    int x,y;
    LL a,b;
    for(int i=0;i<m;i++){
        if(i==0)printf("%lld",solve(A[i].x,A[i].y,A[i].a,A[i].b,n));
        else printf(" %lld",solve(A[i].x,A[i].y,A[i].a,A[i].b,n));
    }
    printf("\n");
}
int main(){
    int n,m,u,v;
    while(~scanf("%d%d",&n,&m)){
        memset(head,-1,sizeof(head));
        cnt = idx = 0;
        for(int i=1;i<=n;i++)
            scanf("%lld",&c[i]);
        for(int i=1;i<n;i++){
            scanf("%d%d",&u,&v);
            ADD(u,v);
            ADD(v,u);
        }
        for(int i=0;i<m;i++)
        scanf("%d%d%lld%lld",&A[i].x,&A[i].y,&A[i].a,&A[i].b);
        TreeWork(n);
        SegmentTree(n,m);
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值