• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > Hdu 6162 Ch’s gift【思维+树链剖分+线段树】

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

作者: mengxiang000000 字体:[增加 减小] 来源:互联网 时间:2017-08-27

mengxiang000000通过本文主要向大家介绍了Hdu 6162等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

 

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


 

 

题目大意:

 

给出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");
    }
}

 

 

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2017-05-28C++ HLSL实现简单的图像处理功能
  • 2017-05-28判断本机office安装版本的方法分享
  • 2017-05-28算法详解之分支限界法的具体实现
  • 2017-05-28c语言实现冒泡排序、希尔排序等多种算法示例
  • 2017-05-28深入sizeof的使用详解
  • 2017-05-28C++获取类的成员函数的函数指针详解及实例代码
  • 2017-05-28ACE反应器(Reactor)模式的深入分析
  • 2017-05-28用VC++6.0实现石头剪刀布游戏的程序
  • 2017-05-28c++统计文件中字符个数代码汇总
  • 2017-05-28详解C语言中rand函数的使用

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C++类静态成员与类静态成员函数详解
    • C++ 算法之希尔排序详解及实例
    • 通过c++11改进我们的模式之改进命令模式
    • 简单了解C语言中直接插入排序与直接选择排序实现
    • 学好C++必须做到的50条 绝对经典!
    • VC++角色游戏中的人物初始化模块代码实例
    • 纯C语言:分治假币问题源码分享
    • 浅谈时间戳与日期时间互转C语言
    • C语言实现程序开机自启动
    • C++操作SQLite简明教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有