hdu-6162 Ch’s gift(树链剖分)

来源:互联网 发布:大学英语6级听力知乎 编辑:程序博客网 时间:2024/06/06 16:36

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6162

Ch’s gift

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


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 31 2 1 3 21 22 43 12 54 5 1 31 1 1 13 5 2 3
 

Sample Output
7 1 4
 

Source
2017 Multi-University Training Contest - Team 9
题目大意:

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

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

题解:

很显然的树剖题可是当时没有想到怎么处理区间【a,b】内的数。赛后听同学说后茅塞顿开,反正当时没有想到。树剖后映射到线段树上的过程就不说了,裸地树剖知识。然后在线段树上对每个节点存储当前区间的最大值,最小值and区间和。当进行到符合要求的区间后,如果最小值>b||最大值<a说明这段区间是都不能取得直接返回0,如果a<=mi<=mx<=b说明这段区间都符合要求就直接返回区间和,否则继续递归。赛后1A。

#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;}


原创粉丝点击