BZOJ5293: [Bjoi2018]求和 树上差分
Description
Input
Output
Sample Input
1 2
1 3
2 4
2 5
2
1 4 5
5 4 45
Sample Output
503245989
说明
样例解释
以下用d(i) 表示第i 个节点的深度。
对于样例中的树,有d(1)=0,d(2)=1,d(3)=1,d(4)=2,d(5)=2。
因此第一个询问答案为(2^5 + 1^5 + 0^5) mod 998244353 = 33
第二个询问答案为(2^45 + 1^45 + 2^45) mod 998244353 = 503245989。
Solution
因为这个k很小,所以就是个树上差分板子
预处理出深度之后弄个树上前缀和(对于不同的k暴力存就好,因为k最大50)
预处理复杂度是$O(nk)$的
然后每次询问的答案其实就是两个点的前缀和减掉他们的$LCA$的前缀和再加上他们的$LCA$的k次方
#include <bits/stdc++.h> using namespace std ; #define N 300010
#define mod 998244353
#define inf 0x3f3f3f3f
#define ll long long int n , m ;
int dep[ N ] , siz[ N ] , top[ N ] , fa[ N ] ;
int head[ N ] , cnt ;
ll c[ N ][ ] ;
struct node {
int to , nxt ;
} e[ N << ] ; void ins( int u , int v ) {
e[ ++ cnt ].to = v ;
e[ cnt ].nxt = head[ u ] ;
head[ u ] = cnt ;
} void dfs1( int u ) {
siz[ u ] = ;
for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
int v = e[ i ].to ;
if( v == fa[ u ] ) continue ;
fa[ v ] = u ;
dep[ v ] = dep[ u ] + ;
dfs1( v ) ;
siz[ u ] += siz[ v ] ;
}
} void dfs2( int u , int topf ) {
top[ u ] = topf ;
int k = ;
for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
if( e[ i ].to != fa[ u ] && siz[ e[ i ].to ] > siz[ k ] )
k = e[ i ].to ;
}
if( !k ) return ;
dfs2( k , topf ) ;
for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
if( e[ i ].to != k && e[ i ].to != fa[ u ] )
dfs2( e[ i ].to , e[ i ].to ) ;
}
} void dfs3( int u ) {
ll x = ;
for( int k = ; k <= ; k ++ ) {
x = 1ll * x * dep[ u ] % mod ;
c[ u ][ k ] = ( 1ll * c[ fa[ u ] ][ k ] + x ) % mod ;
}
for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
if( e[ i ].to == fa[ u ] ) continue ;
dfs3( e[ i ].to ) ;
}
} int lca( int x , int y ) {
while( top[ x ] != top[ y ] ) {
if( dep[ top[ x ] ] < dep[ top[ y ] ] ) swap( x , y ) ;
x = fa[ top[ x ] ] ;
}
if( dep[ x ] > dep[ y ] ) swap( x , y ) ;
return x ;
} ll power( ll a , ll b ) {
ll ans = , base = a ;
while( b ) {
if( b& ) ans = ans * base % mod ;
base = base * base % mod ;
b >>= ;
}
return ans % mod ;
} int main() {
scanf( "%d" , &n ) ;
for(int i = , u , v ; i < n ; i ++ ) {
scanf( "%d%d" , &u , &v ) ;
ins( u , v ) ; ins( v , u ) ;
}
dfs1( ) ; dfs2( , ) ; dfs3( ) ;
scanf( "%d" , &m ) ;
while( m -- ) {
int u , v , k ;
scanf( "%d%d%d" , &u , &v , &k ) ;
int l = lca( u , v ) ;
printf( "%lld\n" , ( c[ u ][ k ] % mod + c[ v ][ k ] % mod - * c[ l ][ k ] % mod + power( dep[ l ] , k ) + mod ) % mod ) ;
}
return ;
}
BZOJ5293: [Bjoi2018]求和 树上差分的更多相关文章
- BZOJ5293:[BJOI2018]求和(LCA,差分)
Description master 对树上的求和非常感兴趣.他生成了一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k 次方和,而且每次的k 可能是不同的.此处节点深度的定义是这个节点 ...
- bzoj5293: [Bjoi2018]求和
题目链接 bzoj5293: [Bjoi2018]求和 题解 暴力 对于lca为1的好坑啊.... 代码 #include<cmath> #include<cstdio> #i ...
- [BZOJ5293][BJOI2018]求和(倍增)
裸的树上倍增. #include<cstdio> #include<cstring> #include<algorithm> #define rep(i,l,r) ...
- Luogu4427 [BJOI2018]求和 (树上差分)
预处理,树上差分.注意深度减一 #include <cstdio> #include <iostream> #include <cstring> #include ...
- 【BZOJ5293】[BJOI2018]求和(前缀和,LCA)
[BZOJ5293][BJOI2018]求和(前缀和,LCA) 题面 BZOJ 洛谷 题解 送分题??? 预处理一下\(k\)次方的前缀和. 然后求个\(LCA\)就做完了?... #include& ...
- poj3417lca+树上差分
/* 给定n个点的树,在其中加入m条新边(称为非树边) 现在可以割断一条树边,一条非树边,使图分裂成两个联通块,请问有几种切割方式 对树边进行分情况讨论 如果树边不处在环中,则割断这条树边后可以割断任 ...
- BZOJ4999 This Problem Is Too Simple!(树上差分+dfs序+树状数组)
对每个权值分别考虑.则只有单点加路径求和的操作.树上差分转化为求到根的路径和,子树加即可.再差分后bit即可.注意树上差分中根的父亲是0,已经忘了是第几次因为这个挂了. #include<ios ...
- [luogu]P2680 运输计划[二分答案][树上差分]
[luogu]P2680 [NOIP2015]运输计划 题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n ...
- 【题解】CF986E Prince's Problem(树上差分+数论性质)
[题解]CF986E Prince's Problem(树上差分+数论性质) 题目大意: 给定你一棵树,有点权\(val_i\le 10^7\).现在有\(m\)组询问给定参数\(x,y,w\)问你对 ...
随机推荐
- if嵌套和elif的区别
if嵌套的使用场景: 2个(多个)条件有前后关系,必须先满足条件1,再判断是否满足条件2. elif的使用场景: 2个(多个)条件是各自独立的平级关系,满足条件几就执行响应的代码. --------- ...
- (3.3)mysql基础深入——mysql启动深入分析
基础:(2.1)学习笔记之mysql基本操作(启动与关闭) 0.mysql启动的 3种方式 (1)mysql.server (2)mysqld_safe (3)mysqld 1.启动分析 [1.1]概 ...
- mysql python pymysql模块 增删改查 查询 fetchone
import pymysql mysql_host = '192.168.0.106' port = 3306 mysql_user = 'root' mysql_pwd = ' encoding = ...
- 前端写一个月的原生 Android 是如何一种体验?
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/j01G58UC80251/article/details/79017706 一个前端程序猿的一个月原 ...
- ListView and gridview常用属性
刷新:notifyDataSetChanged 1.gridview常用属性 GridView的一些特殊属性: 1.Android:numColumns=”auto_fit” //GridVi ...
- 图练习-BFS-从起点到目标点的最短步数(sdut 2830)邻接边表
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2830 题目描述 在古老的魔兽传说中,有两个军团 ...
- PAT 1076 Forwards on Weibo[BFS][一般]
1076 Forwards on Weibo (30)(30 分) Weibo is known as the Chinese version of Twitter. One user on Weib ...
- 配置apache实现对网站某一目录的访问自动跳转到指定目录
访问www.baidu.com/Hello目录,实际访问/new_balance/hello2 Alias /Hello/ /new_balance/hello2 <Directory /new ...
- ide vscode安装
在linux系统中安装VSCode(Visual Studio Code) 在linux系统中安装VSCode(Visual Studio Code) 1.从官网下载压缩包(话说下载下来解压就直接 ...
- Codeforces Round #247 (Div. 2) C D
这题是一个背包问题 这样的 在一个k子树上 每个节点都有自己的k个孩子 然后 从原点走 走到 某个点的 和为 N 且每条的 长度不小于D 就暂停问这样的 路有多少条, 呵呵 想到了 这样做没有把他敲 ...