ZOJ 3937 More Health Points (2016 浙江省赛 B题,可持久维护凸壳)
题目链接 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题,可持久维护凸壳)的更多相关文章
- ZOJ 3940 Modulo Query (2016年浙江省赛E题,区间折叠 + map运用)
题目链接 2016 ZJCPC Problem E 考虑一个开区间$[0, x)$对$a_{i}$取模的过程. $[0, x)$中小于$a_{i}$的部分不变,大于等于$a_{i}$的部分被切下来变 ...
- ZOJ 3879 Capture the Flag 15年浙江省赛K题
每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...
- ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds Me ...
- ZOJ 3872 Beauty of Array DP 15年浙江省赛D题
也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉 What a pity... 题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和 ...
- 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 ...
- ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds Me ...
- 2016湖南省赛--A题--2016
2016 [TOC] Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input ...
- 2016国赛B题小区数据爬取软件
-------------------------请以任何方式留言给作者,否则视为窃取----------------------------- 看你们找数据找的那么辛苦 我就苦逼的花了1个小时写了个 ...
- ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)
Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...
随机推荐
- Python 3基础教程13-写入文件
前面介绍了函数,这篇我们就利用Python 内构函数open来写入字符串到txt文件里. 直接看demo.py 这里有一个小问题,如果我要输入时中文到txt文件会报unicode错误,暂时没法解决.
- Java8 时间处理
Table of Contents 前言 时间单位和时区 时间点 时间段 时间的解析和格式化 时区时间 兼容旧接口 结语 前言 时间处理是一个经常会用到但又不那么好用的功能,其中的主要问题在于对人友好 ...
- Python网络编程(OSI模型、网络协议、TCP)
前言: 什么是网络? 网络是由节点和连线构成,表示诸多对象及其相互联系. 在数学上,网络是一种图,一般认为专指加权图. 网络除了数学定义外,还有具体的物理含义,即网络是从某种相同类 型的实际问题中抽象 ...
- Android 启动项 Activity
使用SDK创建一个App的时候,基本都会看到这段代码: <activity android:name=".ExampleActivity" android:icon=&quo ...
- form表单文件上传 servlet文件接收
需要导入jar包 commons-fileupload-1.3.2.jar commons-io-2.5.jar Upload.Jsp代码 <%@ page language="jav ...
- 团队冲刺Alpha(十)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- BZOJ3990 排序(sort)
排序(sort) 题目描述 小A有一个1~2N的排列A[1..2N],他希望将数组A从小到大排序.小A可以执行的操作有N种,每种操作最多可以执行一次.对于所有的i(1<=i<=N),第i种 ...
- FOJ ——Problem 1759 Super A^B mod C
Problem 1759 Super A^B mod C Accept: 1368 Submit: 4639Time Limit: 1000 mSec Memory Limit : 32 ...
- 快速激活最新JetBrains公司系列产品包括最新的phpstorm10
快速激活最新JetBrains公司系列产品包括最新的phpstorm10 IntelliJ IDEA开源社区 提供了如下通用激活方法: 注册时选择License server 然后输入框填写:http ...
- 51nod 1583 犯罪计划——矩阵乘法优化dp
文泽想在埃及做案n次,并且想在最后不用得到惩罚.案件的被分成几种类型.比如说,案件A,当案件A被重复犯两次时,案件A将被认为不是犯罪案件,因此犯案人不用得到惩罚.也就是说,案件A被犯偶数次时,犯案人将 ...