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 ...
随机推荐
- php中utf-8转unicode
public function utf8_unicode($str) { $unicode = array(); $values = array(); $lookingFor = 1; for ($i ...
- 云计算之路-阿里云上:SLB引起的https访问速度慢问题
自从我们在阿里云SLB上部署了https之后(详见在SLB上部署https遇到的问题及解决方法),陆续有园友向我们反馈登录时速度很慢(登录时是通过https访问),有些园友甚至无法访问登录页面. 而我 ...
- python的inspect模块
一.type and members 1. inspect.getmembers(object[, predicate]) 第二个参数通常可以根据需要调用如下16个方法: 返回值为object的所有成 ...
- WordCount 基础功能
软测第一次作业 该项目在码云上的地址: https://gitee.com/zhege/WordCount 一,概述 WordCount的基础功能需求分析大致如下:对程序设计语言源文件统计字符数.单词 ...
- sklearn中predict()与predict_proba()用法区别
predict是训练后返回预测结果,是标签值. predict_proba返回的是一个 n 行 k 列的数组, 第 i 行 第 j 列上的数值是模型预测 第 i 个预测样本为某个标签的概率,并且每一行 ...
- CentOS下创建和root权限完全相同用户
新建用户 [root@bagon ~]# useradd newroot 修改密码 [root@bagon ~]# passwd newroot 编辑/etc/passwd,找到新建用户那一行 new ...
- Spring 学习笔记(二)—— IOC 容器(BeanFactory)
使用Spring IoC容器后,容器会自动对被管理对象进行初始化并完成对象之间的依赖关系的维护,在被管理对象中无须调用Spring的API. 为了实现IoC功能,Spring提供了两个访问接口: or ...
- BATCH梯度下降,单变量线性回归
- InfluxDB安装后web页面无法访问的解决方案
本文属于<InfluxDB系列教程>文章系列,该系列共包括以下 16 部分: InfluxDB学习之InfluxDB的安装和简介 InfluxDB学习之InfluxDB的基本概念 Infl ...
- Offer 收割编程练习赛 87B 方圆距离
与坐标轴平行的矩形和圆的位置关系. 分两种情况. 圆与矩形交集不为空 此时答案为零.问题归结为如何判断圆与矩形交集不为空. 先排除矩形顶点在圆内或圆心在矩形内. 此时,若矩形与圆交集不为空,则必有矩形 ...