传送门

首先通过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的更多相关文章

  1. [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 ...

  2. codeforces 459E

    codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...

  3. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. CodeForces 166E -Tetrahedron解题报告

    这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...

  8. 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 ...

  9. CodeForces 505B Mr. Kitayuta's Colorful Graph

    Mr. Kitayuta's Colorful Graph Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

随机推荐

  1. ELK 学习笔记之 elasticsearch Bulk操作

    Bulk操作: Bulk操作用于批量插入数据: 请求体格式: 编辑一个文件:(插入2个新的文档) curl -XPOST 'http://192.168.1.151:9200/library/book ...

  2. 使用真机导致Androidstudio打印不出log

    针对真机打印不出log这个问题,我具体的解决方案是这样: 1.你要确保你的Android studio中的菜单栏 ,Tools → Android → Enable ADB Integration这个 ...

  3. c++ exercises

    1.定义个数组并初始化,然后将数组倒置,并显示倒置前和倒置后的结果. #include <iostream> #include <stdio.h> using namespac ...

  4. 关于5G目前形式的浅谈

    目前市面上已经有十几款5G手机已经上市了,但是到2019年底的覆盖率还是非常的低,所以很多换机的用户可以再等一等,2020应该是出来的都是5G手机,也都是支持双模的NA/NSA的两种组网模式,因为工信 ...

  5. PHP 实现get 和 Post 请求

    1 get get请求比较简单,file_get_contents():即可实现 $tmpUrl = "http://测试url"; # get方法获取信息 $rawGetData ...

  6. .Net Core3.0使用gRPC

    gRPC是什么 gRPC是可以在任何环境中运行的现代开源高性能RPC框架.它可以通过可插拔的支持来有效地连接数据中心内和跨数据中心的服务,以实现负载平衡,跟踪,运行状况检查和身份验证.它也适用于分布式 ...

  7. Vue3都使用Proxy了,你更应该了解Proxy

    vue3.0的pre-alpha版代码已经开源了,就像作者之前放出的消息一样,其数据响应这一部分已经由ES6的Proxy来代替Object.defineProperty实现,感兴趣的同学可以看其实现源 ...

  8. Python之random模块和time模块

    1.random()模块的使用   import random x = random.random() y = random.random() print(x,y*10) #random.random ...

  9. PHP 数组转json格式,key的保存问题

    <?php $arr = [ 2, 3, ]; echo print_r($arr,true); echo json_encode($arr); echo "\n\n"; $ ...

  10. PMBOK(第六版) PMP笔记-质量审计、风险审计、采购审计的区分

    质量审计.风险审计.采购审计的区分   三个概念的相同之处: 都是审计的概念 都是特定知识领域的审计 三个概念的区别: (1)三个概念虽然都是审计,但分布在不同的管理过程组. ·质量审计:执行过程组, ...