Hdu 6162 Ch’s gift【思维+树链剖分+线段树】

46 篇文章 0 订阅
7 篇文章 0 订阅

Ch’s gift

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


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

题目大意:


给出N个点的一棵树,M个询问。

现在每个点都有对应的权值,每个询问询问我们从x到y的路径上,权值区间在【a,b】之间的点权和。


思路:


①首先这是在树上对链的查询操作,我们肯定要用到树剖来用线段树维护查询。


②那么我们考虑每个查询,其查询的链是从x到y的,权值是在【a,b】之间的。

区间的查询很套路,离线操作对查询排序。

如果我们直接对所有的查询区间按照左端点(a)从小到大排序的话,我们很容易查询到一条链上从【a,maxn】的权值和。

那么我们不妨再按照右端点+1(b+1)从小到大排序,然后我们也能很容易查询到一条链上从【b+1,maxn】的权值和,那么对于当前这个询问,我们只要拿之前求出来的值减掉这部分求出来的值即可。


③注意数据范围,要用LL.


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
#define ll __int64
struct node
{
    ll x,y,A,pos,flag;
}b[105000*4];
struct node2
{
    ll u,val;
}a[105000*4];
vector<ll>mp[105000];
ll val[105000];
ll n,m;
ll cmp2(node2 a,node2 b)
{
    return a.val<b.val;
}
ll cmp(node a,node b)
{
    return a.A<b.A;
}
/*******************************/
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
ll tree[105000*10];
void pushup(ll rt)
{
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void update(ll L,ll R,ll c,ll l,ll r,ll rt)
{
    if(L<=l&&r<=R)//覆盖的是区间~
    {
        tree[rt]=c;
        return ;
    }
    ll m=(l+r)/2;
    if(L<=m)
    {
        update(L,R,c,lson);
    }
    if(m<R)
    {
        update(L,R,c,rson);
    }
    pushup(rt);
}
ll Query(ll L,ll R,ll l,ll r,ll rt)
{
    if(L<=l&&r<=R)
    {
        return tree[rt];
    }
    else
    {
        ll m=(l+r)>>1;
        ll ans=0;
        if(L<=m)
        {
            ans+=Query(L,R,lson);
        }
        if(m<R)
        {
            ans+=Query(L,R,rson);
        }
        pushup(rt);
        return ans;
    }
}
void build( ll l ,ll r , ll rt )
{
    if( l == r )
    {
        tree[rt]=0;
        return ;
    }
    else
    {
        ll m = (l+r)>>1 ;
        build(lson) ;
        build(rson) ;
        pushup(rt) ;
        return ;
    }
}
/*******************************/
ll cnt;
ll ans[105000];
ll son[105000];
ll size[105000];
ll depth[105000];
ll fa[105000];
ll dfn[105000];
ll Top[105000];
void Dfs(ll u,ll from,ll d)
{
    size[u]=1;
    son[u]=-1;fa[u]=from;depth[u]=d;
    for(ll i=0;i<mp[u].size();i++)
    {
        ll v=mp[u][i];
        if(v==from)continue;
        Dfs(v,u,d+1);
        size[u]+=size[v];
        if(son[u]==-1||size[v]>size[son[u]])
        {
            son[u]=v;
        }
    }
}
void Dfs2(ll u,ll from,ll top)
{
    Top[u]=top;dfn[u]=++cnt;
    if(son[u]!=-1)
    {
        Dfs2(son[u],u,top);
    }
    for(ll i=0;i<mp[u].size();i++)
    {
        ll v=mp[u][i];
        if(v==from||v==son[u])continue;
        Dfs2(v,u,v);
    }
}
void Slove(ll x,ll y,ll pos,ll flag)
{
    ll fx=Top[x],fy=Top[y];
    ll sum=0;
    while(fx!=fy)
    {
        if(depth[fx]<depth[fy])
        {
            swap(fx,fy);
            swap(x,y);
        }
        sum+=Query(dfn[fx],dfn[x],1,n,1);
        x=fa[fx];fx=Top[x];
    }
    if(depth[x]<depth[y])
    {
        swap(x,y);
    }
    sum+=Query(dfn[y],dfn[x],1,n,1);
    ans[pos]+=flag*sum;
}
/*******************************/
int main()
{
    while(~scanf("%I64d%I64d",&n,&m))
    {
        cnt=0;
        memset(ans,0,sizeof(ans));
        for(ll i=1;i<=n;i++)scanf("%d",&val[i]),a[i-1].u=i,a[i-1].val=val[i];
        for(ll i=1;i<=n;i++)mp[i].clear();
        for(ll i=1;i<=n-1;i++)
        {
            ll x,y;scanf("%I64d%I64d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        Dfs(1,-1,1);
        Dfs2(1,-1,1);
        build(1,n,1);
        for(ll i=1;i<=n;i++)update(dfn[i],dfn[i],val[i],1,n,1);
        ll tot=0;
        for(ll i=0;i<m;i++)
        {
            ll x,y,A,B;
            scanf("%I64d%I64d%I64d%I64d",&x,&y,&A,&B);
            b[tot].flag=1;
            b[tot].x=x;
            b[tot].y=y;
            b[tot].A=A;
            b[tot].pos=i;
            tot++;
            b[tot].flag=-1;
            b[tot].x=x;
            b[tot].y=y;
            b[tot].A=B+1;
            b[tot].pos=i;
            tot++;
        }
        sort(a,a+n,cmp2);
        sort(b,b+tot,cmp);
        ll j=0;
        for(ll i=0;i<tot;i++)
        {
            if(i==0||b[i].A!=b[i-1].A)
            {
                while(j<n&&a[j].val<b[i].A)
                {
                    update(dfn[a[j].u],dfn[a[j].u],0,1,n,1);
                    j++;
                }
            }
            Slove(b[i].x,b[i].y,b[i].pos,b[i].flag);
        }
        for(ll i=0;i<m;i++)
        {
            if(i>0)printf(" ");
            printf("%I64d",ans[i]);
        }
        printf("\n");
    }
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值