Codeforces 1178G. The Awesomest Vertex
首先通过dfs序把子树操作转化为区间操作,求最大值可以用斜率优化。
然后分个块,对每个块维护个凸包。修改时中间的打个标记,边角暴力重构;询问时中间的用斜率优化的方法求,边角的暴力求。
由于此题有绝对值,所以还要对原值取负后再维护一个凸包。。。。
时间复杂度\(O(nlogn+q \sqrt n)\)
代码很丑,感觉不是人看的。。
官方题解讲得比我好多了。。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cassert>
#include<cmath>
#define LL long long
#define ldb long double
#define fr(i,x,y) for(int i=(x);i<=(y);i++)
#define rf(i,x,y) for(int i=(x);i>=(y);i--)
#define frz(i,x,y) for(int i=(x),z=y;i<=z;i++)
using namespace std;
const int N=200006;
const int M=N<<1;
const int BM=505;
int n,q,m;
int cnt,head[N],Next[M],v[M];
int a[N],b[N],c[N],d[N];
int clo,pre[N],dfn[N],ver[N];
struct data{
int x,y;
};
int bm;
void chkmax(LL &x,LL y){ if (y>x) x=y; }
int cmp1(const int &q,const int &w){ return d[q]<d[w]; }
int cmp2(const int &q,const int &w){ return -d[q]<-d[w]; }
struct blo{
int num[BM],l,r;
int inc;
LL x[BM],y[BM];
int h[BM],t,w;
LL chk(LL x1,LL y1,LL x2,LL y2){
return x1*y2-x2*y1;
}
void AddPoint(int i){
for(;w-1>=t&&chk(x[h[w]]-x[h[w-1]],y[h[w]]-y[h[w-1]],x[i]-x[h[w]],y[i]-y[h[w]])>=0;w--);
h[++w]=i;
}
LL query(){
for(;w-1>=t&&x[h[t]]*inc+y[h[t]]<=x[h[t+1]]*inc+y[h[t+1]];t++);
return x[h[t]]*inc+y[h[t]];
}
void build(){
//int bl,r=br;
t=1;w=0;
assert(r<=n);assert(r-l<=m);
fr(i,0,r-l) x[i]=d[num[i]],y[i]=1LL*c[num[i]]*d[num[i]],AddPoint(i);
}
void init(){
assert(r<=n);assert(r-l<=m);assert(m<BM);
fr(i,l,r) num[i-l]=i;
sort(num,num+r-l+1,cmp1);
build();
}
}bo[BM];
struct bloo{
int num[BM],l,r;
int inc;
LL x[BM],y[BM];
int h[BM],t,w;
LL chk(LL x1,LL y1,LL x2,LL y2){
return x1*y2-x2*y1;
}
void AddPoint(int i){
for(;w-1>=t&&chk(x[h[w]]-x[h[w-1]],y[h[w]]-y[h[w-1]],x[i]-x[h[w]],y[i]-y[h[w]])>=0;w--);
h[++w]=i;
}
LL query(){
for(;w-1>=t&&x[h[t]]*inc+y[h[t]]<=x[h[t+1]]*inc+y[h[t+1]];t++);
return x[h[t]]*inc+y[h[t]];
}
void build(){
//int bl,r=br;
t=1;w=0;
assert(r<=n);assert(r-l<=m);
fr(i,0,r-l) x[i]=-d[num[i]],y[i]=-1LL*c[num[i]]*d[num[i]],AddPoint(i);
}
void init(){
assert(r<=n);assert(r-l<=m);assert(m<BM);
fr(i,l,r) num[i-l]=i;
sort(num,num+r-l+1,cmp2);
build();
}
}boo[BM];
void read(int &x){
char ch=getchar();x=0;int w=0;
for(;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') w=1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
if (w) x=-x;
}
void add(int x,int y){
Next[++cnt]=head[x];
head[x]=cnt;
v[cnt]=y;
}
void dfs(int x,int fa){
pre[x]=++clo;ver[clo]=x;
a[x]+=a[fa];b[x]+=b[fa];
for(int i=head[x];i;i=Next[i])
if (v[i]!=fa) dfs(v[i],x);
dfn[x]=clo;
}
void init(){
m=sqrt(n);
fr(i,1,n) c[i]=a[ver[i]],d[i]=b[ver[i]];
bo[0].l=1;bo[0].r=m-1;bm=n/m; //not real
for(int i=m;i<=n;i+=m) bo[i/m].l=i,bo[i/m].r=i+m-1;
bo[bm].r=n;
fr(o,0,bm){
//int &l=bo[o].r,&r=bo[o].r;
boo[o].l=bo[o].l;boo[o].r=bo[o].r;
bo[o].init();boo[o].init();
}
}
void incc(int l,int r,int v){
if (l/m==r/m){
fr(i,l,r) c[i]+=v;
bo[l/m].build();boo[l/m].build();
return;
}
frz(i,l,bo[l/m].r) c[i]+=v;bo[l/m].build();boo[l/m].build();
fr(i,bo[r/m].l,r) c[i]+=v;bo[r/m].build();boo[r/m].build();
frz(i,l/m+1,r/m-1) bo[i].inc+=v,boo[i].inc+=v;
}
LL query(int l,int r){
LL ans=-1000000000000000000;
if (l/m==r/m){
fr(i,l,r) chkmax(ans,1LL*abs(c[i]+bo[l/m].inc)*d[i]);
return ans;
}
frz(i,l,bo[l/m].r) chkmax(ans,1LL*abs(c[i]+bo[l/m].inc)*d[i]);
fr(i,bo[r/m].l,r) chkmax(ans,1LL*abs(c[i]+bo[r/m].inc)*d[i]);
frz(i,l/m+1,r/m-1) chkmax(ans,bo[i].query()),chkmax(ans,boo[i].query());
return ans;
}
int main(){
read(n);read(q);
int tp,x,y;
fr(i,2,n){
read(x);
add(x,i);add(i,x);
}
fr(i,1,n) read(a[i]);
fr(i,1,n) read(b[i]);
dfs(1,0);
fr(i,1,n) b[i]=abs(b[i]);
init();
while(q--){
read(tp);read(x);
if (tp==1){
read(y);
incc(pre[x],dfn[x],y);
} else{
printf("%lld\n",query(pre[x],dfn[x]));
}
}
return 0;
}
Codeforces 1178G. The Awesomest Vertex的更多相关文章
- [cf1178G]The Awesomest Vertex
2020年论文题,这里给出了一个$o(n\log^{2}n+m\log^{3}n)$的做法,例题3即为原题 1.例题1 题面 给定$n$个一次函数$f_{i}(x)$,$m$次查询$F(x)=\max ...
- codeforces 459E
codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- Educational Codeforces Round 6 E. New Year Tree dfs+线段树
题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...
- Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和
B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径
E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...
- CodeForces 166E -Tetrahedron解题报告
这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
- CodeForces 505B Mr. Kitayuta's Colorful Graph
Mr. Kitayuta's Colorful Graph Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
随机推荐
- MongoDB 学习笔记之 分析器和explain
MongoDB分析器: 检测MongoDB分析器是否打开: db.getProfilingLevel() 0表示没有打开 1表示打开了,并且如果查询的执行时间超过了第二个参数毫秒(ms)为单位的最大查 ...
- SpringBoot返回JSON
目录 1.SpringBoot返回JSON简介 2.整合jackson-databind 3.整合Gson 4.整合fastjson 1.SpringBoot返回JSON简介 随着web开发前后端分离 ...
- 利用JVM在线调试工具排查线上问题
在生产上我们经常会碰到一些不好排查的问题,例如线程安全问题,用最简单的threaddump或者heapdump不好查到问题原因.为了排查这些问题,有时我们会临时加一些日志,比如在一些关键的函数里打印出 ...
- 【WPF on .NET Core 3.0】 Stylet演示项目 - 简易图书管理系统(1)
.NET Core 3.0已经发布了,除了一大堆令人激动的功能以外,也增加了对WPF的正式支持, 那么WPF在.NET Core 3.0下的开发体验如何呢? 本文利用了Stylet框架开发.NET C ...
- CONVERT用法指南,时间字段截取方法
语法: CONVERT(VARCHAR(7),<字段名称>,121) 时间转字符:select CONVERT(VARCHAR(7),dyxq,121),* from TBYPKC03-- ...
- Spring Boot2 系列教程(十三)Spring Boot 中的全局异常处理
在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...
- 蓝牙TWS耳机IBRT的原理初分析
最近在倒腾TWS对耳的一些东西,看到一些源码,发现一个新概念,IBRT没有搞清楚,抱着吾将上下而求索的态度,详细看了一些代码,查了一些资料,还是发现了不少有价值的信息的.至少,我突然感觉自己懂了一些什 ...
- 初识域渗透利器Empire
Empire 是一款类似Metasploit 的渗透测试框架,基于python 编写,Empire是一个纯粹的PowerShell 后开发代理,建立在密码安全通信和灵活的架构上.Empire 实现了无 ...
- 我家很管事的猫——mycat初步部署实践与问题排查
mycat,阿里出品的mysql中间件,提供读写分离和分库分表方案.项目中主要使用的是其读写分离功能. [如何部署?] 本文只采用并测试了双主从模式,配置看这一篇足矣: https://www.cnb ...
- 一个关于内联优化和调用约定的Bug
很久没有更新博客了(博客园怎么还不更新后台),前几天在写一个Linux 0.11的实验 [1] 时遇到了一个奇葩的Bug,就在这简单记录一下调试过程吧. 现象 这个实验要求在Linux 0.11中实现 ...