题目链接  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. 《Cracking the Coding Interview》——第11章:排序和搜索——题目3

    2014-03-21 20:55 题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位.找出其中是否存在某一个值. 解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移 ...

  2. 一个关于pynoi游戏的C语言编程

    "去吧,秦,好好享受这个夜晚,我给你准备了一份礼物,希望你能喜欢."小布莱克眨着眼睛笑道,狡猾的像一头小狐狸.

  3. 使用selenium监听每一步操作

    1.创建类LogEventListener.java, 如下: package com.demo; import org.openqa.selenium.By; import org.openqa.s ...

  4. operator、explicit与implicit

    说这个之前先说下什么叫隐式转换和显示转换 1.所谓隐式转换,就是系统默认的转换,其本质是小存储容量数据类型自动转换为大存储容量数据类型. 例如:float f = 1.0: double d=f:这样 ...

  5. 虚函数&&虚继承

    如果说没有虚函数的虚继承只是一个噩梦的话,那么这里就是真正的炼狱.这个C++中最复杂的继承层次在VS上的实现其实我没有完全理解,摸爬滚打了一番也算得出了微软的实现方法吧,至于一些刁钻的实现方式我也想不 ...

  6. vb如何将数据库中某个字段显示在一个文本框

    Dim mrc As ADODB.Recordset Private Sub cmdQuery_Click() Dim txtSQL As String Dim MsgText As String t ...

  7. NetScaler Active-Active模式

    NetScaler Active-Active模式 NetScaler Active-Active模式 (此文档基于版本:NS9.3: Build 55.6 nc) By ShingTan Activ ...

  8. POJ 3180 The cow Prom Tarjan基础题

    题目用google翻译实在看不懂 其实题目意思如下 给一个有向图,求点个数大于1的强联通分量个数 #include<cstdio> #include<algorithm> #i ...

  9. 从K近邻算法、距离度量谈到KD树、SIFT+BBF算法

    转载自:http://blog.csdn.net/v_july_v/article/details/8203674/ 从K近邻算法.距离度量谈到KD树.SIFT+BBF算法 前言 前两日,在微博上说: ...

  10. Codeforces 934.D A Determined Cleanup

    D. A Determined Cleanup time limit per test 1 second memory limit per test 256 megabytes input stand ...