[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 ...
随机推荐
- 极光推送>>java SDK服务端集成后台项目(使用详解)
PS:如果你是第一次用推送,那就直接按照我的步骤来,再去看官方文档,这样,更容易能理解操作流程.还有——-请耐心看 极光文档(java SDK)请参考 [ 极光文档 ] 步骤一: 首先,你必须在 [极 ...
- The error may exist in com/bjpowernode/dao/StudentDao.xml ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderExcept
The error may exist in com/bjpowernode/dao/StudentDao.xml### Cause: org.apache.ibatis.builder.Builde ...
- How does exercise keep your brain young?
Exercise may protect the brain from disease and dementia as we age, but the mechanisms behind its be ...
- SpringMVC - 1.快速入门
1. HelloWorld 步骤: 加入 jar 包 mons-logging-1.1.3.jar spring-aop-4.0.0.RELEASE.jar spring-beans-4.0.0.RE ...
- SqlServerHelp
using System; using System.Collections.Generic; using System.Reflection; using System.Text; using Sy ...
- 【Maven】Select Dependency 无法检索
问题: 在 “pom.xml” 中,点击 “Dependencies” -> “Add” 添加依赖时,无法检索. 如下图所示: 解决办法: 依次点击 “Windows”->“Show ...
- 选择结构if
1.if语句 if语句是指如果满足某种条件,就进行某种处理.例如,小明妈妈跟小明说“如果你考试得了100分,星期天就带你去游乐场玩”.这句话可以通过下面的一段伪代码来描述. 如果小明考试得了100分 ...
- 5336: [TJOI2018]party
题解: 比较水啦..dp套dp f[i][j][k]表示枚举了前i位,最大公共子序列匹配状态为j,noi匹配到了第k位 因为g[j]和g[j+1]最多差1 所以可以状压成j 然后内层再dp一下搞出下一 ...
- 【转】ArcGIS10.0完全卸载全攻略
ArcGIS10.0完全卸载详细步骤: 1.开始>控制面板>添加删除程序,卸载所有ArcGIS软件和帮助文档,以及所有ArcGIS补丁.2.从添加删除程序面板中删除所有Python相关的应 ...
- flink-SQL
Table API和SQL捆绑在flink-table Maven工件中.必须将以下依赖项添加到你的项目才能使用Table API和SQL: <dependency> <groupI ...