NOI模拟(5.10) BJOID1T3 求和 (bzoj5293)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/scar_lyw/article/details/80288620

求和

题目背景:

5.10 模拟 BJOI2018D1T3

分析:LCA

 

当时一看到被吓了一跳,这是要斯特林数还是要拉格朗日差值啊,然后看到k <= 50的时候,脸上就只剩下了一个滑稽······直接用sum[i][j]表示第i个点到跟的路径上所有点的权值的j次方之和,对于一次询问u, v, k,答案就是sum[u][k] + sum[v][k] - sum[lca(u, v)][k] - sum[father[lca(u, v)]][k],直接做LCA就好了,倍增,链剖,ST表,想用什么用什么。而且据说出题人数据造锅了,貌似直接就是随机数据程度,啥都不想写还可以直接暴力(滑稽······)

 

Source:

 

/*
    created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>
 
inline char read() {
    static const int IN_LEN = 1024 * 1024;
    static char buf[IN_LEN], *s, *t;
    if (s == t) {
        t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
        if (s == t) return -1;
    }
    return *s++;
}
 
///*
template<class T>
inline void R(T &x) {
    static char c;
    static bool iosig;
    for (c = read(), iosig = false; !isdigit(c); c = read()) {
        if (c == -1) return ;
        if (c == '-') iosig = true; 
    }
    for (x = 0; isdigit(c); c = read()) 
        x = ((x << 2) + x << 1) + (c ^ '0');
    if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN];
char *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}


template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout), oh = obuf;
}
 
/*
template<class T>
inline void R(T &x) {
    static char c;
    static bool iosig;
    for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
        if (c == '-') iosig = true; 
    for (x = 0; isdigit(c); c = getchar()) 
        x = ((x << 2) + x << 1) + (c ^ '0');
    if (iosig) x = -x;
}
//*/

const int MAXN = 300000 + 10;
const int mod = 998244353;

int n, m, x, y, k, lca;
std::vector<int> edge[MAXN];
int top[MAXN], son[MAXN], size[MAXN], father[MAXN], dep[MAXN];

int sum[MAXN][60];
inline void add_edge(int x, int y) {
    edge[x].push_back(y), edge[y].push_back(x);
}

inline void dfs1(int cur, int fa) {
    dep[cur] = dep[fa] + 1, father[cur] = fa, size[cur] = 1;
    for (int i = 0, ret = 1; i <= 50; ++i, 
        ret = (long long)ret * (dep[cur] - 1) % mod) {
        sum[cur][i] = (ret + sum[fa][i]) % mod;       
    }
    for (int p = 0; p < edge[cur].size(); ++p) {
        int v = edge[cur][p];
        if (v != fa) {
            dfs1(v, cur), size[cur] += size[v];
            if (size[v] > size[son[cur]]) son[cur] = v;
        }
    }
}

inline void dfs2(int cur, int tp) {
    top[cur] = tp;
    if (son[cur]) dfs2(son[cur], tp);
    for (int p = 0; p < edge[cur].size(); ++p) {
        int v = edge[cur][p];
        if (top[v] == 0) dfs2(v, v);
    }
}

inline int query_lca(int u, int v) {
    while (top[u] != top[v]) 
        (dep[top[u]] > dep[top[v]]) ? u = father[top[u]] : v = father[top[v]];
    return (dep[u] > dep[v]) ? v : u;
}

inline void solve() {
    R(n);
    for (int i = 1; i < n; ++i) R(x), R(y), add_edge(x, y);
    R(m), dfs1(1, 0), dfs2(1, 1);
    while (m--) {
        R(x), R(y), R(k), lca = query_lca(x, y);
        W(((sum[x][k] + sum[y][k] - sum[lca][k] - sum[father[lca]][k]) 
            % mod + mod) % mod), write_char('\n');
    }
}

int main() {
    freopen("sum.in", "r", stdin);
    freopen("sum.out", "w", stdout);
    solve();
    flush();
    return 0;
}
阅读更多

没有更多推荐了,返回首页