zoukankan      html  css  js  c++  java
  • hdu 5692 Snacks dfs序+线段树

    Snacks

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


    Problem Description
    百度科技园内有n个零食机,零食机之间通过n1条路相互连通。每个零食机都有一个值v,表示为小度熊提供零食的价值。

    由于零食被频繁的消耗和补充,零食机的价值v会时常发生变化。小度熊只能从编号为0的零食机出发,并且每个零食机至多经过一次。另外,小度熊会对某个零食机的零食有所偏爱,要求路线上必须有那个零食机。

    为小度熊规划一个路线,使得路线上的价值总和最大。
     
    Input
    输入数据第一行是一个整数T(T10),表示有T组测试数据。

    对于每组数据,包含两个整数n,m(1n,m100000),表示有n个零食机,m次操作。

    接下来n1行,每行两个整数xy(0x,y<n),表示编号为x的零食机与编号为y的零食机相连。

    接下来一行由n个数组成,表示从编号为0到编号为n1的零食机的初始价值v(|v|<100000)

    接下来m行,有两种操作:0 x y,表示编号为x的零食机的价值变为y1 x,表示询问从编号为0的零食机出发,必须经过编号为x零食机的路线中,价值总和的最大值。

    本题可能栈溢出,辛苦同学们提交语言选择c++,并在代码的第一行加上:

    `#pragma comment(linker, "/STACK:1024000000,1024000000") `
     
    Output
    对于每组数据,首先输出一行”Case #?:”,在问号处应填入当前数据的组数,组数从1开始计算。

    对于每次询问,输出从编号为0的零食机出发,必须经过编号为x零食机的路线中,价值总和的最大值。
     
    Sample Input
    1 6 5 0 1 1 2 0 3 3 4 5 3 7 -5 100 20 -5 -7 1 1 1 3 0 2 -1 1 1 1 5
     
    Sample Output
    Case #1: 102 27 2 20
     
    Source
     思路:你需要预处理出0-每个点的权值,如何转变到一个关于子树的问题,
       你发现询问的结果,相当于在那个点的子树中查找一个点,即0-该点的最大值;
       更新则是区间增加或删除一个值;
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    #define bug(x)  cout<<"bug"<<x<<endl;
    const int N=1e5+10,M=1e6+10,inf=1e9+10;
    const ll INF=1e18+10,mod=2147493647;
    int n,q;
    int head[N<<1],edg,flag[N];
    ll a[N],dis[N];
    int in[N],out[N],tot;
    struct is
    {
        int v,next;
    }edge[N<<1];
    void init()
    {
        memset(head,-1,sizeof(head));
        edg=0;
        tot=0;
    }
    void add(int u,int v)
    {
        edg++;
        edge[edg].v=v;
        edge[edg].next=head[u];
        head[u]=edg;
    }
    void dfs(int u,int fa,ll val)
    {
        in[u]=++tot;
        dis[u]=val;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(v==fa)continue;
            dfs(v,u,val+a[v]);
        }
        out[u]=tot;
    }
    ll maxx[N<<2],lazy[N<<2];
    void pushup(int pos)
    {
        maxx[pos]=max(maxx[pos<<1],maxx[pos<<1|1]);
    }
    void pushdown(int pos)
    {
        if(lazy[pos])
        {
            lazy[pos<<1|1]+=lazy[pos];
            lazy[pos<<1]+=lazy[pos];
            maxx[pos<<1]+=lazy[pos];
            maxx[pos<<1|1]+=lazy[pos];
            lazy[pos]=0;
        }
    }
    void build(int l,int r,int pos)
    {
        lazy[pos]=0;
        if(l==r)
        {
            maxx[pos]=dis[flag[l]];
            return;
        }
        int mid=(l+r)>>1;
        build(l,mid,pos<<1);
        build(mid+1,r,pos<<1|1);
        pushup(pos);
    }
    void update(int L,int R,ll c,int l,int r,int pos)
    {
        if(L<=l&&r<=R)
        {
            maxx[pos]+=c;
            lazy[pos]+=c;
            return;
        }
        pushdown(pos);
        int mid=(l+r)>>1;
        if(L<=mid)
            update(L,R,c,l,mid,pos<<1);
        if(R>mid)
            update(L,R,c,mid+1,r,pos<<1|1);
        pushup(pos);
    }
    ll query(int L,int R,int l,int r,int pos)
    {
        if(L<=l&&r<=R)
        {
            return maxx[pos];
        }
        pushdown(pos);
        int mid=(l+r)>>1;
        ll ans=-INF;
        if(L<=mid)
            ans=max(ans,query(L,R,l,mid,pos<<1));
        if(R>mid)
            ans=max(ans,query(L,R,mid+1,r,pos<<1|1));
        return ans;
    }
    int main()
    {
        int T,cas=1;
        scanf("%d",&T);
        while(T--)
        {
            init();
            scanf("%d%d",&n,&q);
            for(int i=1;i<n;i++)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                u++,v++;
                add(u,v);
                add(v,u);
            }
            for(int i=1;i<=n;i++)
                scanf("%lld",&a[i]);
            dfs(1,-1,a[1]);
            for(int i=1;i<=n;i++)
                flag[in[i]]=i;
            build(1,n,1);
            printf("Case #%d:
    ",cas++);
            while(q--)
            {
                int x,z;
                scanf("%d%d",&x,&z);
                z++;
                if(x)
                    printf("%lld
    ",query(in[z],out[z],1,n,1));
                else
                {
                    ll p;
                    scanf("%lld",&p);
                    update(in[z],out[z],p-a[z],1,n,1);
                    a[z]=p;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    AS3.0函数定义的方法
    AS3.0的int uint Number的使用原则
    AS3的数据类型和定义
    把AS代码链接到fla文件
    试题
    【転載】LOOP
    【購買管理】注文請書とは? 注文書との違いと書き方、収入印紙の必要性
    共通系
    【转载】SAP ABAP开发技术总结]数据引用(data references)、对象引用(object references)
    CALL FUNCTION
  • 原文地址:https://www.cnblogs.com/jhz033/p/6280320.html
Copyright © 2011-2022 走看看