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\)问你对 ...
随机推荐
- Spring使用AspectJ注解和XML配置实现AOP
本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...
- Shell初学(一)hello world
精简: 1.创建:可以使用 vi/vim 命令来创建文件如: test.sh ,扩展名并不影响脚本执行,写什么都可以. 2.hello_world: #!/bin/bash ...
- beginAppearanceTransition
- (void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated __OSX_AVAILABLE_STARTING ...
- 什么是 Delta 文件
什么是 Delta 文件 Delta 文件应用很广泛,特别是在数据库领域 What Is a Delta File? During most computer operations, copying, ...
- HashMap(不是线程安全)与ConcurrentHashMap(线程安全)
HashMap不是线程安全的 ConcurrentHashMap是线程安全的 从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是线程安全的,因此多线程操作时需要格外小心. ...
- Razor中的@:和语法
用Razor实现流畅编程 Razor尽量减少编写一个视图模板需要敲入的字符数,实现快速流畅的编程工作流.与大部分模板的语法不同,你不必在HTML中为了明确地标记出服务模块 的开始和结束而中断编程.Ra ...
- soapUI-JDBC Request
1.1.1 JDBC Requet 1.1.1.1 概述 – JDBC Request Option Description JDBC Request TestStep Toolbar 对JDB ...
- [LeetCode] 443. String Compression_Easy tag:String
Given an array of characters, compress it in-place. The length after compression must always be smal ...
- #C++初学记录(贪心算法#结构体#贪心算法)
贪心算法#结构体 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋 ...
- xhtml 的三种 doctype
{1}文档宣告 <!ODCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 [there]//EN" "http://www.w4org/TR/xhtml1/DTD/ ...