[BZOJ3683]Falsita
[BZOJ3683]Falsita
题目大意:
一个\(n(n\le3\times10^5)\)个结点的树,每个结点有一个权值\(w_i\),\(m(m\le3\times10^5)\)次操作,操作包含以下\(3\)种:
- 将结点\(u\)的权值加上\(d\);
- 将以\(u\)为根的子树中的每一个结点加上\(d\);
- 询问任取一个以\(u\)为LCA的点对\((x,y)\),\(w_x+w_y\)的期望值。
思路:
首先可以用一遍树形DP求出不考虑修改的答案\(ans_i\)。
对于操作\(1\),会对\(u\)本身的答案产生\(d\times(size[u]-1)\)的贡献。对\(par[u]\)到根上每个结点\(x\)的贡献为\(d\times(size[x]-size[y])\),其中\(y\)为\(x\)在链上的子结点。
对于操作\(2\),会对\(u\)子树内每个结点产生\(2d\times pair[x]\)的贡献,其中\(pair[u]\)为以\(u\)为LCA的点对数。对到根的链上结点贡献为\(d\times(size[x]-size[y])\times size[u]\)。
由于对于每个结点,\((size[x]-size[y])\)和\(pair[x]\)都是固定的,因此对于链上修改和子树修改,我们只需要维护\(d\)即可。这可以用树链剖分+线段树实现。
时间复杂度\(\mathcal O(n\log^2n)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) neg|=ch=='-';
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return neg?-x:x;
}
inline char getalpha() {
register char ch;
while(!isalpha(ch=getchar()));
return ch;
}
typedef long long int64;
const int N=3e5+1;
int par[N],w[N],size[N],dep[N],son[N],dfn[N],top[N];
int64 pair[N],ans[N],sum[N];
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
}
void dfs(const int &x,const int &par) {
size[x]=1;
sum[x]=w[x];
dep[x]=dep[par]+1;
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
dfs(y,x);
sum[x]+=sum[y];
pair[x]+=1ll*size[x]*size[y];
size[x]+=size[y];
if(size[y]>size[son[x]]) {
son[x]=y;
}
}
ans[x]=1ll*w[x]*(size[x]-1);
for(register unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
ans[x]+=1ll*sum[y]*(size[x]-size[y]);
}
}
void dfs(const int &x) {
dfn[x]=++dfn[0];
top[x]=x==son[par[x]]?top[par[x]]:x;
if(son[x]) dfs(son[x]);
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==son[x]) continue;
dfs(y);
}
}
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
int64 val[N<<2];
public:
void add(const int &p,const int &b,const int &e,const int &l,const int &r,const int64 &d) {
if(b==l&&e==r) {
val[p]+=d;
return;
}
if(l<=mid) add(p _left,b,mid,l,std::min(mid,r),d);
if(r>mid) add(p _right,mid+1,e,std::max(mid+1,l),r,d);
}
int64 query(const int &p,const int &b,const int &e,const int &x) const {
int64 ret=val[p];
if(b==e) return ret;
if(x<=mid) ret+=query(p _left,b,mid,x);
if(x>mid) ret+=query(p _right,mid+1,e,x);
return ret;
}
#undef _left
#undef _right
#undef mid
};
SegmentTree t1,t2;
inline void modify0(int x,const int64 &d) {
//链上修改
while(x) {
if(x!=top[x]) t1.add(1,1,dfn[0],dfn[top[x]],dfn[par[x]],d);
x=top[x];
if(par[x]!=0) ans[par[x]]+=d*(size[par[x]]-size[x]);
x=par[x];
}
}
inline void modify1(int x,const int64 &d) {
//单点修改
ans[x]+=d*(size[x]-1);
modify0(x,d);
}
inline void modify2(const int &x,const int64 &d) {
//子树修改
if(size[x]>1) t2.add(1,1,dfn[0],dfn[x],dfn[x]+size[x]-1,d);
modify0(x,d*size[x]);
}
inline double query(const int &x) {
return 1.*(ans[x]+1ll*t1.query(1,1,dfn[0],dfn[x])*(size[x]-size[son[x]])+2ll*t2.query(1,1,dfn[0],dfn[x])*pair[x])/pair[x];
}
int main() {
const int n=getint(),m=getint();
for(register int i=2;i<=n;i++) {
add_edge(par[i]=getint(),i);
}
for(register int i=1;i<=n;i++) {
w[i]=getint();
}
dfs(1,0);
dfs(1);
for(register int i=0;i<m;i++) {
const char opt=getalpha();
const int u=getint();
if(opt=='S') modify1(u,getint());
if(opt=='M') modify2(u,getint());
if(opt=='Q') printf("%.6f\n",query(u));
}
return 0;
}
[BZOJ3683]Falsita的更多相关文章
- NKOJ-4573 Falsita
问题描述: 到海边了呢...... 如果没有那次选择,现在是不是会好些呢...... 都过去了. 仰望着星空,迎面吹过一阵阵海风,倚靠着护栏,Fine 在海边静静地伫立着,在一个个无际的长夜后,Fin ...
- POJ3683 Falsita
http://poj.org/problem?id=3683 思路:2-SAT,输出任意一组方案,O(m+n) #include<cstdio> #include<iostream& ...
- 【BZOJ】3683: Falsita
题解 这道题维护方法比较简单,也有点奇妙 我们可以很容易求出经过所有点的路径条数,和初始时分子的大小 然后单点修改的时候,相当于给当前点\(v\)加上\(delta * (siz[v] - 1)\) ...
- CH Round #51 - Shinrein祭 #1
A.Phorni 题目:http://www.contesthunter.org/contest/CH%20Round%20%2351%20-%20Shinrein祭%20%231/Phorni 没做 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
随机推荐
- C++ 使用LockWorkStation()的过程遇到的问题
关于函数“LockWorkStation()”,参见:https://msdn.microsoft.com/en-us/library/windows/desktop/aa376875.aspx Ho ...
- 论文阅读笔记二十一:MULTI-SCALE CONTEXT AGGREGATION BY DILATED CONVOLUTIONS(ICRL2016)
论文源址:https://arxiv.org/abs/1511.07122 tensorflow Github:https://github.com/ndrplz/dilation-tensorflo ...
- 集腋成裘-03-css基础-02
1.1 三种写法 内嵌式:样式只作用于当前文件,没有真正实现结构表现分离 外链式:作用范围是当前站点,真正实现了内容与表现分离 行内样式:仅限于当前标签,结构混在一起 1.2 标签分类 1.2.1 块 ...
- 如何查看响应端口号被个程序占用(Windows)
我们以80端口为例,在dos输入命令“ netstat -aon|findstr "80" 后按回车显示如下,可以看到占用80端口对应的程序的PID号为1752 ...
- 005-Python字典
Python字典(dict) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号{}中: ...
- tempalte模板
tempalte模板层: 功能:为了更有逻辑的将数据库中的数据渲染到模板中: 模拟数据源: DB = [ {"hostname":"c1.com"," ...
- cmake方式使用vlfeat
目录 environment statement compile vlfeat with cmake compile example project with cmake 1. make sure c ...
- logstash配置白名单决定去哪个index
input { kafka { bootstrap_servers => "127.0.0.1:9092" client_id => "log" a ...
- RSA javascript加密 lua解密
一个在线RSA非对称加密解密,可以用这个地址生成公钥和私钥 https://blog.zhengxianjun.com/online-tool/rsa/ javascript加密 jsencrypt. ...
- 前端:Jquery 处理同一Name的Radio组时,绑定checked属性异常的问题.(已解决)
将: $("input[type=radio][name=optionsContractGroup][value=201]").attr("checked",t ...