• [bzoj] 2809 dispatching


    原题

    左偏树板子题。

    左偏树详解:

    #include<cstdio>
    #include<algorithm>
    #define N 100010
    typedef long long ll;
    using namespace std;
    int n,m,f[N],cnt=1,l,r,q[N],money[N],lead[N],head[N];
    ll ans;
    struct node
    {
        node *ls,*rs;
        int dis,sze,val;
        ll sum;
        node(): ls(NULL), rs(NULL), dis(0), sze(0), val(0), sum(0){}
        node(int x): ls(NULL), rs(NULL), dis(0), sze(1), val(x), sum(x){}
        node *updt()
    	{
    	    if(!ls || ls->dis<rs->dis) swap(ls,rs);
    	    dis=rs?rs->dis+1:0;
    	    sze=(ls?ls->sze:0)+(rs?rs->sze:0)+1;
    	    sum=(ls?ls->sum:0)+(rs?rs->sum:0)+val;
    	    return this;
    	}
    }*tre[N];
    struct hhh
    {
        int to,next;
    }edge[N];
    
    int read()
    {
        int ans=0,fu=1;
        char j=getchar();
        for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
        if (j=='-') fu=-1,j=getchar();
        for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
        return ans*fu;
    }
    
    void add(int u,int v)
    {
        edge[cnt].to=v;
        edge[cnt].next=head[u];
        head[u]=cnt++;
    }
    
    node *merge(node* x,node* y)
    {
        if (!x) return y;
        if (!y) return x;
        if (x->val<y->val) swap(x,y);
        x->rs=merge(x->rs,y);
        return x->updt();
    }
    
    void pop(node* &x)
    {
        while (x->sum>m)
    	x=merge(x->ls,x->rs);
    }
    
    int main()
    {
        n=read();
        m=read();
        for (int i=1;i<=n;i++)
        {
    	f[i]=read();
    	money[i]=read();
    	lead[i]=read();
    	add(f[i],i);
    	tre[i]=new node(money[i]);
        }
        q[++r]=1;
        for (int l=1;l<=r;l++)
    	for (int e=head[q[l]];e;e=edge[e].next)
    	    q[++r]=edge[e].to;
        for (int l=r,v,u;l>=1;l--)
        {
    	v=q[l];
    	u=f[v];
    	ans=max(ans,(ll)lead[v]*tre[v]->sze);
    	if (u) tre[u]=merge(tre[u],tre[v]),pop(tre[u]);
        }
        printf("%lld
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    通过网页源代码看“饭否”的网站架构
    SD2.0课程等待时候的摘抄
    SD2.0阿里妈妈的研发过程
    转:编程8字专静谦筹悟慎透恒
    SD2.0第一天总体印象
    SD2.0《Web2.0网站性能调优实践》貌似是抄袭的文章
    SD2.02个课合成一个课
    SD2.0第2天总体印象
    variant
    [zz]C++程序员的阅读清单
  • 原文地址:https://www.cnblogs.com/mrha/p/7898489.html
Copyright © 2020-2023  润新知