题目链接  2016 ZJCPC Problem B

题意  CF 660F的树上版本。

其他做的方法都差不多,关键是把凸壳放到树上。

每次确定扔掉几个元素的时候直接$O(1)$修改(先不清楚这个位置之后的元素因为之后还要恢复),然后$O(1)$恢复,通过这个来实现可持久。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second typedef long long LL; const int N = 1e5 + 10; int T;
int n;
int r;
int deep[N], q[N];
LL a[N], c[N], s[N];
LL ans;
vector <int> v[N]; inline LL y(int x){
return c[x] - 1ll * deep[x] * s[x];
} inline long double g(int j, int k){
double dy = 1.00 * y(j) - 1.00 * y(k);
double dx = 1.00 * deep[j] - 1.00 * deep[k];
return dy / dx;
} inline LL calc(int x, int y){
return c[x] - c[y] - 1ll * deep[y] * (s[x] - s[y]);
} void pre(int x, int fa, int dep){
deep[x] = dep;
s[x] = s[fa] + a[x];
c[x] = c[fa] + 1ll * dep * a[x]; for (auto u : v[x]){
pre(u, x, dep + 1);
}
} inline int pos(LL x, int tail){
x = -x;
int l = 1, r = tail - 1, ret = 0; while (l <= r){
int mid = (l + r) >> 1;
if (g(q[mid], q[mid - 1]) < x) l = (ret = mid) + 1;
else r = mid - 1;
} return q[ret];
} inline int gettail(int x, int tail){
int l = 2, r = tail, ret = 0; if (g(x, q[1]) < g(q[1], q[0])) return 1; while (l <= r){
int mid = (l + r) >> 1;
if (g(x, q[mid - 1]) >= g(q[mid - 1], q[mid - 2])) l = (ret = mid) + 1;
else r = mid - 1;
} return ret;
} void dfs(int x, int tail){ int y = pos(s[x], tail);
ans = max(ans, calc(x, y)); int cnt, t, re, la;
if (tail <= 1 || g(x, q[tail - 1]) >= g(q[tail - 1], q[tail - 2])){
re = tail;
la = q[tail];
q[tail] = x;
tail++;
cnt = tail;
} else{
t = gettail(x, tail); //get the position
re = t;
la = q[t];
q[t] = x;
cnt = t + 1;
}
//replace for (auto u : v[x]) dfs(u, cnt); //continue solving q[re] = la; //undo
} int main(){ scanf("%d", &T);
while (T--){
scanf("%d", &n);
rep(i, 0, n + 1) v[i].clear(); rep(i, 1, n) scanf("%lld", a + i);
rep(i, 2, n){
int x;
scanf("%d", &x);
v[x].push_back(i);
} pre(1, 0, 1); rep(i, 0, n + 1) q[i] = 0;
r = 0;
q[r++] = 0; ans = 0;
dfs(1, r);
printf("%lld\n", ans);
} return 0;
}

  

ZOJ 3937 More Health Points (2016 浙江省赛 B题,可持久维护凸壳)的更多相关文章

  1. ZOJ 3940 Modulo Query (2016年浙江省赛E题,区间折叠 + map运用)

    题目链接  2016 ZJCPC Problem E 考虑一个开区间$[0, x)$对$a_{i}$取模的过程. $[0, x)$中小于$a_{i}$的部分不变,大于等于$a_{i}$的部分被切下来变 ...

  2. ZOJ 3879 Capture the Flag 15年浙江省赛K题

    每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...

  3. ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds      Me ...

  4. ZOJ 3872 Beauty of Array DP 15年浙江省赛D题

    也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉 What a pity... 题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和 ...

  5. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

  6. ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds      Me ...

  7. 2016湖南省赛--A题--2016

    2016 [TOC] Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input ...

  8. 2016国赛B题小区数据爬取软件

    -------------------------请以任何方式留言给作者,否则视为窃取----------------------------- 看你们找数据找的那么辛苦 我就苦逼的花了1个小时写了个 ...

  9. ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)

    Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...

随机推荐

  1. 剑指Offer - 九度1367 - 二叉搜索树的后序遍历序列

    剑指Offer - 九度1367 - 二叉搜索树的后序遍历序列2013-11-23 03:16 题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出 ...

  2. 《Cracking the Coding Interview》——第5章:位操作——题目8

    2014-03-19 06:33 题目:用一个byte数组来模拟WxH的屏幕,每个二进制位表示一个像素.请设计一个画水平线的函数. 解法:一个点一个点地画就可以了.如果要优化的话,其实可以把中间整字节 ...

  3. (笔记) RealTimeRender[实时渲染] C2

    @author: 白袍小道 @来源:RealTime Render @建议书籍:龙书.RealTimeR第四版.GPUGem和PRO (来源:暗影不解释) 引点 这一章关注的管线中的管道功能,而非实现 ...

  4. JavaScript里面的面向对象

    1.JavaScript里面没有类,但是利用函数可以起到类似的作用,例如简单的构造方法,跟Python差别不大 function f1(mame,age){ this.Name = name; thi ...

  5. Could not automatically select an Xcode project. Specify one in your Podfile like so

    需要将Podfile文件放置在根目录下,而不能放置在项目的里面. 更改路径即可

  6. python 读取consul配置

    自动化通过rcp client调用远端服务接口时,都需要将远端测试服务ip.端口记录在配置文件. 但由于,服务发布或重启会导致ip.端口变动. 以下将通过python-consul 自动去读取cons ...

  7. java实现远程开机

    import java.io.IOException; import java.net.*;public class 远程开机 { public static void main(String[] a ...

  8. MySQL常用客户端 命令

    登录MySQL mysql -h localhost -uroot -p 授权指定用户访问指定数据库 GRANT ALL ON cookbook.* TO 'cbuser'@'localhost' I ...

  9. rem布局和hotcss原理分析

    rem布局的开源方案hotcss, 其原理个人理解如下: 手机px = (手机页面宽度/设计稿宽度) * 设计稿px 手机rem = 手机px / fontSize = (手机页面宽度/设计稿宽度) ...

  10. WebStorm 2017.1.2 汉化破解

    第一步:下载 官方地址: http://www.jetbrains.com/webstorm/ 第二步:破解 安装后第一次打开会弹出一个窗口,选择“License server”,在输入框输入下面的网 ...