zoukankan      html  css  js  c++  java
  • bzoj2806 [Apio2012]dispatching【可并堆】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2809

    保存可并堆模版代码。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    const int maxn = 100005, maxm = 1000000005;
    
    int n, m, fa[maxn], salary[maxn], lead[maxn];
    int key[maxn], left[maxn], right[maxn], npl[maxn], cnt, siz[maxn];
    int head[maxn], to[maxn], next[maxn], lb;
    long long s[maxn], ans;
    
    inline void ist(int aa, int ss) {
    	to[lb] = ss;
    	next[lb] = head[aa];
    	head[aa] = lb;
    	++lb;
    }
    int merge(int A, int B) {
    	if (!A) {
    		return B;
    	}
    	if (!B) {
    		return A;
    	}
    	if (key[B] > key[A]) {
    		std::swap(A, B);
    	}
    	right[A] = merge(right[A], B);
    	if (npl[right[A]] > npl[left[A]]) {
    		std::swap(left[A], right[A]);
    	}
    	npl[A] = npl[right[A]] + 1;
    	s[A] = s[left[A]] + s[right[A]] + key[A];
    	siz[A] = siz[left[A]] + siz[right[A]] + 1;
    	return A;
    }
    int dfs(int r) {
    	int rt = ++cnt, tr = -666;
    	key[rt] = salary[r];
    	s[rt] = salary[r];
    	siz[rt] = 1;
    	for (int j = head[r]; j != -1; j = next[j]) {
    		tr = dfs(to[j]);
    		rt = merge(rt, tr);
    	}
    	while (s[rt] > m) {
    		rt = merge(left[rt], right[rt]);
    	}
    	ans = std::max(ans, (long long)lead[r] * (long long)siz[rt]);
    	return rt;
    }
    
    int main(void) {
    	//freopen("in.txt", "r", stdin);
    	memset(head, -1, sizeof head);
    	memset(next, -1, sizeof next);
    	npl[0] = -1;
    	scanf("%d%d", &n, &m);
    	for (int i = 1; i <= n; ++i) {
    		scanf("%d%d%d", fa + i, salary + i, lead + i);
    		ist(fa[i], i);
    	}
    	
    	dfs(1);
    	printf("%lld
    ", ans);
    	return 0;
    }
    

      

  • 相关阅读:
    C# 对XML操作-实例
    XML
    得到一个随机数组的方法
    Node Redis 小试
    Hexo快速搭建静态博客并实现远程VPS自动部署
    substr.js 字符串切割
    GraphicsMagick 学习笔记
    store.js 跨浏览器的localStorage
    bodyParser中间件的研究
    Sublime Text 使用指南
  • 原文地址:https://www.cnblogs.com/ciao-sora/p/6705911.html
Copyright © 2011-2022 走看看