hdu 3804 树链剖分+线段树

Query on a tree

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 618    Accepted Submission(s): 144


Problem Description
  There are some queries on a tree which has n nodes. Every query is described as two integers (X, Y).For each query, you should find the maximum weight of the edges in set E, which satisfies the following two conditions.
1) The edge must on the path from node X to node 1.
2) The edge’s weight should be lower or equal to Y.
  Now give you the tree and queries. Can you find out the answer for each query?
 

Input
  The first line of the input is an integer T, indicating the number of test cases. For each case, the first line contains an integer n indicates the number of nodes in the tree. Then n-1 lines follows, each line contains three integers X, Y, W indicate an edge between node X and node Y whose value is W. Then one line has one integer Q indicates the number of queries. In the next Q lines, each line contains two integers X and Y as said above.
 

Output
  For each test case, you should output Q lines. If no edge satisfy the conditions described above,just output “-1” for this query. Otherwise output the answer for this query.
 

Sample Input
  
1 3 1 2 7 2 3 5 4 3 10 3 7 3 6 3 4
 

Sample Output
  
7 7 5 -1
Hint
2<=n<=10^5 2<=Q<=10^5 1<=W,Y<=10^9 The data is guaranteed that your program will overflow if you use recursion.
 

Author
Edward_mj
 

Source

//题意:给出一棵树,Q个询问x,w,x到1的路径上不超过w的最长边是多少。
//思路:先按询问的权值从小到大排序,然后n-1条边也按权值从小到大排序,对于每个询问,
//把小于等于当前权值的边加到树上,然后求当前树的最大值。。。
#pragma comment(linker, "/STACK:10240000000000,10240000000000")
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define N 200005
#define inf 0x3fffffff
using namespace std;
struct line
{
    int x,y,w,id,sum;
}ed[N],b[N];
struct node
{
    int u,v,w,next;
}bian[N*2];
struct pp
{
    int x,y,ma;
}a[N*3];
int sz[N],head[N],top[N],son[N],id,e,ti[N],father[N],dep[N];
void add(int u,int v,int w)
{
    bian[e].u=u;
    bian[e].v=v;
    bian[e].next=head[u];
    head[u]=e++;
}
int max(int a,int b)
{
    return a>b?a:b;
}
void dfs1(int u,int fa)
{
    int i,v;
    sz[u]=1; dep[u]=dep[fa]+1; father[u]=fa; son[u]=0;
    for(i=head[u];i!=-1;i=bian[i].next)
    {
        v=bian[i].v;
        if(v==fa)  continue;
        dfs1(v,u);
        sz[u]+=sz[v];
        if(sz[son[u]]<sz[v])
            son[u]=v;
    }
}
void dfs2(int u,int fa)
{
    int i,v;
    top[u]=fa;
    ti[u]=id++;
    if(son[u]!=0)
        dfs2(son[u],fa);
    for(i=head[u];i!=-1;i=bian[i].next)
    {
        v=bian[i].v;
        if(v==son[u]||v==father[u])
            continue;
        dfs2(v,v);
    }
}
void build(int t,int x,int y)
{
    a[t].x=x; a[t].y=y; a[t].ma=-inf;
    if(x==y)  return;
    int mid=(x+y)>>1,temp=t<<1;
    build(temp,x,mid);
    build(temp+1,mid+1,y);
}
void update(int t,int x,int w)
{
    if(a[t].x==a[t].y)
    {
        a[t].ma=w;
        return;
    }
    int mid=(a[t].x+a[t].y)>>1,temp=t<<1;
    if(x<=mid)
        update(temp,x,w);
    else
        update(temp+1,x,w);
    a[t].ma=max(a[temp].ma,a[temp+1].ma);
}
int query(int t,int x,int y)
{
    if(a[t].x==x&&a[t].y==y)
        return a[t].ma;
    int mid=(a[t].x+a[t].y)>>1,temp=t<<1;
    if(y<=mid)
        return query(temp,x,y);
    else if(x>mid)
        return query(temp+1,x,y);
    else
        return max(query(temp,x,mid),query(temp+1,mid+1,y));
}
int lca(int x)
{
    int y=1,ans=-1;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]])
            swap(x,y);
        ans=max(ans,query(1,ti[top[x]],ti[x]));
        x=father[top[x]];
    }
    if(dep[x]>dep[y])
        swap(x,y);
    if(x!=y)
        ans=max(ans,query(1,ti[x]+1,ti[y]));
    return ans;
}
bool cmp1(line a,line b)
{
    return a.w<b.w;
}
bool cmp2(line a,line b)
{
    return a.y<b.y;
}
bool cmp3(line a,line b)
{
    return a.id<b.id;
}
int main()
{
    int t,i,q,j,n,ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        e=0;
        memset(head,-1,sizeof(head));
        for(i=0;i<n-1;i++)
        {
            scanf("%d%d%d",&ed[i].x,&ed[i].y,&ed[i].w);
            add(ed[i].x,ed[i].y,ed[i].w);
            add(ed[i].y,ed[i].x,ed[i].w);
        }
        sort(ed,ed+n-1,cmp1);
        dep[1]=0; id=1; sz[0]=0;
        dfs1(1,1);   dfs2(1,1);
        build(1,1,n);
        //for(i=1;i<=n;i++)
          // printf("i=%d size=%d top=%d father=%d son=%d ti=%d dep=%d\n",i,sz[i],top[i],father[i],son[i],ti[i],dep[i]);
        scanf("%d",&q);
        for(i=0;i<q;i++)
        {
            scanf("%d%d",&b[i].x,&b[i].y);
            b[i].id=i;
        }
        sort(b,b+q,cmp2);
        j=0;
        for(i=0;i<q;i++)
        {
            while(b[i].y>=ed[j].w&&j<n-1)
            {
             if(dep[ed[j].x]<dep[ed[j].y])
                swap(ed[j].x,ed[j].y);
             update(1,ti[ed[j].x],ed[j].w);
             j++;
            }
            ans=lca(b[i].x);
            b[i].sum=ans;
        }
        sort(b,b+q,cmp3);
        for(i=0;i<q;i++)
           printf("%d\n",b[i].sum);
    }
    return 0;
}


内容概要:本文介绍了奕斯伟科技集团基于RISC-V架构开发的EAM2011芯片及其应用研究。EAM2011是一款高性能实时控制芯片,支持160MHz主频和AI算法,符合汽车电子AEC-Q100 Grade 2和ASIL-B安全标准。文章详细描述了芯片的关键特性、配套软件开发套件(SDK)和集成开发环境(IDE),以及基于该芯片的ESWINEBP3901开发板的硬件资源和接口配置。文中提供了详细的代码示例,涵盖时钟配置、GPIO控制、ADC采样、CAN通信、PWM输出及RTOS任务创建等功能实现。此外,还介绍了硬件申领流程、技术资料获取渠道及开发建议,帮助开发者高效启动基于EAM2011芯片的开发工作。 适合人群:具备嵌入式系统开发经验的研发人员,特别是对RISC-V架构感兴趣的工程师和技术爱好者。 使用场景及目标:①了解EAM2011芯片的特性和应用场景,如智能汽车、智能家居和工业控制;②掌握基于EAM2011芯片的开发板和芯片的硬件资源和接口配置;③学习如何实现基本的外设驱动,如GPIO、ADC、CAN、PWM等;④通过RTOS任务创建示例,理解多任务处理和实时系统的实现。 其他说明:开发者可以根据实际需求扩展这些基础功能。建议优先掌握《EAM2011参考手册》中的关键外设寄存器配置方法,这对底层驱动开发至关重要。同时,注意硬件申领的时效性和替代方案,确保开发工作的顺利进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值