出题人的做法是 \(O(n\sqrt{n\log n})\),结果这场结束后就被狂喷,一群人给出了 \(O(n\sqrt{n})\) 做法,甚至 \(O(n\log n)\) 都出来了……

首先发现,修改一个点时,如果把这个点看成根,其它点权期望的变化只和在根的哪个儿子的子树中有关,\(\frac{n-sz[u]}{n}d\)(选除了这个子树中的点都能经过 \(x\))。

\(O(n\sqrt{n\log n})\) 很显然,对修改的点的度数分类讨论,度数小的就是一堆子树加,度数大的就打个标记,查询的时候把大点的贡献也算上就行了。

\(O(n\sqrt{n})\) 的话,把线段树/树状数组换成分块。度数小的修改复杂度总共是 \(O(\text{块数}+\text{度数})\)(每棵子树的区间不交)。

\(O(n\log n)\) 就不用度数根号分治了。

考虑树剖(似乎很套路?),修改一个点时,只需要对重儿子和外子树区间加。

询问一个点时,发现需要再统计的就是跳重链时,从一条链跳到另外一条链时链头和父亲之间的贡献(只有这时才有轻儿子)。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=300030,mod=998244353;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
char ch=getchar();ll x=0,f=0;
while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
return f?-x:x;
}
int n,q,el,head[maxn],to[maxn],nxt[maxn],b[maxn],dep[maxn],sz[maxn],son[maxn],fa[maxn],top[maxn],lft[maxn],rig[maxn],cnt,tag[maxn];
inline void add(int u,int v){
to[++el]=v;nxt[el]=head[u];head[u]=el;
}
inline int qpow(int a,int b){
int ans=1;
for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) ans=1ll*ans*a%mod;
return ans;
}
inline void update(int p,int v){
for(int i=p;i<=n;i+=i&-i) b[i]=(b[i]+v)%mod;
}
inline void update(int l,int r,int v){
update(l,v);update(r+1,(mod-v)%mod);
}
inline int query(int p){
int s=0;
for(int i=p;i;i-=i&-i) s=(s+b[i])%mod;
return s;
}
void dfs1(int u,int f){
dep[u]=dep[fa[u]=f]+1;
sz[u]=1;
for(int i=head[u];i;i=nxt[i]){
int v=to[i];
if(v==f) continue;
dfs1(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int topf){
top[u]=topf;
lft[u]=++cnt;
if(son[u]) dfs2(son[u],topf);
for(int i=head[u];i;i=nxt[i]){
int v=to[i];
if(v==fa[u] || v==son[u]) continue;
dfs2(v,v);
}
rig[u]=cnt;
}
void update_tree(int u,int d){
tag[u]=(tag[u]+d)%mod;
if(son[u]) update(lft[son[u]],rig[son[u]],1ll*(n-sz[son[u]])*d%mod);
if(u!=1){
update(1,lft[u]-1,1ll*sz[u]*d%mod);
if(rig[u]!=n) update(rig[u]+1,n,1ll*sz[u]*d%mod);
}
}
int query_tree(int u){
int s=(1ll*n*tag[u]+query(lft[u]))%mod;
while(u){
s=(s+1ll*tag[fa[top[u]]]*(n-sz[top[u]]))%mod;
u=fa[top[u]];
}
return s;
}
int main(){
n=read();q=read();
int inv=qpow(n,mod-2);
FOR(i,1,n-1){
int u=read(),v=read();
add(u,v);add(v,u);
}
dfs1(1,0);dfs2(1,1);
while(q--){
int tp=read(),u=read();
if(tp==1) update_tree(u,1ll*inv*read()%mod);
else printf("%d\n",query_tree(u));
}
}

CF1254D Tree Queries(树链剖分)的更多相关文章

  1. HDU 4718 The LCIS on the Tree(树链剖分)

    Problem Description For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i < ...

  2. Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分

    D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...

  3. 南昌网络赛J. Distance on the tree 树链剖分

    Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...

  4. 【POJ3237】Tree(树链剖分)

    题意:在一棵N个节点,有边权的树上维护以下操作: 1:单边修改,将第X条边的边权修改成Y 2:区间取反,将点X与Y在树上路径中的所有边边权取反 3:区间询问最大值,询问X到Y树上路径中边权最大值 n& ...

  5. POJ 3237:Tree(树链剖分)

    http://poj.org/problem?id=3237 题意:树链剖分.操作有三种:改变一条边的边权,将 a 到 b 的每条边的边权都翻转(即 w[i] = -w[i]),询问 a 到 b 的最 ...

  6. QTREE3 spoj 2798. Query on a tree again! 树链剖分+线段树

    Query on a tree again! 给出一棵树,树节点的颜色初始时为白色,有两种操作: 0.把节点x的颜色置反(黑变白,白变黑). 1.询问节点1到节点x的路径上第一个黑色节点的编号. 分析 ...

  7. spoj 375 Query on a tree(树链剖分,线段树)

      Query on a tree Time Limit: 851MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Sub ...

  8. bzoj 3637: Query on a tree VI 树链剖分 && AC600

    3637: Query on a tree VI Time Limit: 8 Sec  Memory Limit: 1024 MBSubmit: 206  Solved: 38[Submit][Sta ...

  9. poj 3237 Tree(树链剖分,线段树)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 7268   Accepted: 1969 Description ...

  10. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

随机推荐

  1. Dynamics CRM通过定制应用程序功能区为符合条件的实体表单增加按钮

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复167或者20151029可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 前面的博文都是为一个实体添加按钮 ...

  2. FloatingActionButton动态更换背景色

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/186 FloatingActionButton 动态更换背 ...

  3. 31.Java基础_日期/日期格式/日历类

    Date类 Date对象构造方法 Date对象常用方法 import java.util.*; public class test { public static void main(String[] ...

  4. com.mysql.cj.exceptions.DataReadException: Zero date value prohibited

    com.mysql.cj.exceptions.DataReadException: Zero date value prohibited at com.mysql.cj.result.SqlTime ...

  5. 【西北师大-2108Java】第十一次作业成绩汇总

    [西北师大-2108Java]第十一次作业成绩汇总 作业题目 面向对象程序设计(JAVA) 第13周学习指导及要求 实验目的与要求 (1)掌握事件处理的基本原理,理解其用途: (2)掌握AWT事件模型 ...

  6. Java之IO初识(字节流和字符流)

    IO概述 生活中,你肯定经历过这样的场景.当你编辑一个文本文件,忘记了 ctrl+s ,可能文件就白白编辑了.当你电脑上插入一个U盘,可以把一个视频,拷贝到你的电脑硬盘里.那么数据都是在哪些设备上的呢 ...

  7. 富文本编辑器 tinymce 的安装与使用

    百度的富文本编辑器大家都熟悉,那么下面给大家介绍一款富文本编辑器tinymce ,个人感觉比百度的界面好看,调用方便,就不知道各位大神怎么看咯! tinymce中文文档 以下是vue中使用示例,献上最 ...

  8. ueditor设置

    1: 2:

  9. oracle 查询数据一直提示:“error code [17004]; 无效的列类型”111111

    oracle 查询数据一直提示:“error code [17004]; 无效的列类型”111111 问题场景:oracle,jpa,insert原因:插入的字段中有null.导致类型转换出问题,这个 ...

  10. PHP Swoole与TCP三次握手

    握手常见问题 1.连接拒绝 2.Operation now in progress 多是因为丢包.错误ip.backlog满了&阻塞&tcp_abort_on_overflow=0 3 ...