跳到正文

题解

题解:P6798 「StOI-2」简单的树

xpg007约 42 分钟
原文
文章信息与版本

文章信息

收录
2026年7月26日
更新
2026年7月27日
篇幅
约 42 分钟
题目
P6798

版本

  1. v1当前

题目概括:

P6798 「StOI-2」简单的树

给定一棵有 nn 个节点的带点权(点权为数列 cc)树,定义点 uu 的 VV 为该字数内点权的最大值。请求出分别把编号为 aa 的点的点权修依次修改为 ll 至 rr 时树上的所有点的 VV 的和的和,形式化的,求出 ∑i=lr(ca=i)∑j=1nVj\sum\limits_{i=l}^{r}(c_a=i)\sum\limits_{j=1}^{n}V_j,答案对 998244353998244353 取模,并强制在线。

思路分析:

  1. 注意到这里的 VV 为子树之内的最大点权,因此点权随深度单调不降。而修改一个点只会影响这个点到根的链上所有点的点权。

  2. 我们考虑到当 cac_a 被修改成 yy 时,只会影响该点和子树包含该点的点的 VV,考虑 cac_a 被修改对上述点的影响分为两部分 ca=0c_a=0 后再令 ca=yc_a=y,如果 ca≠Vac_a \ne V_a 那么 cac_a 一定不是 aa 子树中的最大值,所以这不会影响 VaV_a,否则我们就把 VaV_a 改成除了 cac_a 的最大值,也就是次大值,我们记这个值为 v′v'。这个时候对于任意 x∈treepx\in tree_p,这里 treeptree_p 为 pp 点的子树,Vx=max⁡(v′,y)V_x=\max(v',y)。

  3. 考虑分段讨论 VxV_x 的取值,显然对于 VxV_x 只有两种取值:v′v' 和 yy,由于我们需要确定是用最大值还是次大值,我们需要求出节点 x1x_1 使其满足 Vx1=cx1V_{x_1}=c_{x_1} 并且深度最小,这样在区间 [a,x1][a,x_1] 中 v′v' 为次大值,而在 (x1,1](x_1,1] 中 v′v' 为最大值。

  4. 继续考虑 VxV_x 的取值,由于其具有单调性,所以我们可以找到两个分界点 x2x_2 和 x3x_3,对于 x2x_2 我们要求其满足 Vx2≤lV_{x_2}\le l 且深度最小,对于 x3x_3 我们要求其满足 Vx3≤rV_{x_3}\le r 且其深度最小,这样 VxV_x 的取值被我们分成了三个部分:

    • 对于 x∈[a,x2]x\in[a,x_2],v′≤lv'\le l,所以 Vx=y(l≤y≤r)V_x=y(l\le y\le r)。
    • 对于 x∈(x2,x3]x\in(x_2,x_3],l<v′≤rl<v'\le r,所以 Vx=v′V_x=v' 或 yy 这点我们之后分类讨论。
    • 对于 x∈(x3,1]x\in(x_3,1],r<v′r<v',所以 Vx=v′V_x=v'。
  5. 我们考虑这些分段的贡献,对于第一个分段,Vx=yV_x=y,直接用等差数列求和公式求和 ∑y=lry=(l+r)(r−l+1)2\sum\limits_{y=l}^ry=\frac{(l+r)(r-l+1)}{2}。对于第二个分段,Vx=v′V_x=v' 或 yy,我们考虑两种情况,对于 y∈[l,v′)y\in[l,v'),Vx=v′V_x=v',对于 y∈[v′,r]y\in[v',r],Vx=yV_x=y,对于前半段,显然一共有 v′−lv'-l 个数字,所以贡献就是 v′(v′−l)v'(v'-l),对于后半段我们还是可以套用等差数列求和,为 ∑y=v′ry=(v′+r)(r−v′+1)2\sum\limits_{y=v'}^ry=\frac{(v'+r)(r-v'+1)}{2},相加得到总贡献并整理式子。

v′(v′−l)+(v′+r)(r−v′+1)2=v′2−lv′+rv′−v′2+v′+r2−rv′+r2=2v′2−2lv′+rv′−v′2+v′+r2−rv′+r2=(2v2−v′2)+(−2lv′+rv′−rv′+v′)+r2+r2=12(v′2+(1−2l)v′+r2+r) v'(v'-l)+\frac{(v'+r)(r-v'+1)}{2} \\ =v'^2-lv'+\frac{rv'-v'^2+v'+r^2-rv'+r}{2} \\ =\frac{2v'^2-2lv'+rv'-v'^2+v'+r^2-rv'+r}{2} \\ =\frac{(2v^2-v'^2)+(-2lv'+rv'-rv'+v')+r^2+r}{2} \\ =\frac{1}{2}(v'^2+(1-2l)v'+r^2+r)
  1. 整理好式子显然我们可以发现我们需要维护 v′2v'^2 和 v′v' 这两个和,我们还是可以通过倍增来实现,继续计算第三个分段,Vx=v′V_x=v',直接乘上数量得到 v′(r−l+1)v'(r-l+1),这个式子也需要维护 v′v'。

  2. 到这里我们便能够看出来我们需要维护的变量有哪些,分别要维护:最大值求和,最大值平方求和,次大值求和,次大值平方求和,路径求和,祖先倍增,而这几个都可以通过倍增来维护,子树最大值,子树次大值,深度,全局权值之和,这些都可以通过 dfsdfs 来维护。为了维护求和我们还需要做两个函数来快速算出 x→yx\to y 的路径上的最大值求和,最大值平方求和以及次大值求和,次大值平方求和。

  3. 因为这里需要除 22 所以我们要求出 22 的逆元即 2998244353−22^{998244353-2}。

  4. 接下来考虑分解点如何去找,对于最大值次大值分界点 x1x_1,我们可以使用倍增一次性得到。对于分段分界点 x2x_2,我们先用次大值向上找并且不超过 x1x_1 这样才能保证始终是次大值,然后考虑两种情况:

    • deepx2>deepx1deep_{x_2}>deep_{x_1},说明 x2x_2 在 x1x_1 以下也就是次大值区间,那我们不需要额外考虑 x2x_2,直接去跳 x3x_3 即可。
    • deepx2=deepx1deep_{x_2}=deep_{x_1},说明 x2x_2 用次大值跳到了 x1x_1 那我们还需要继续来看看 x2x_2 能不能用最大值继续向上跳。
  5. 对于 9 的第一种情况而言我们具体如何找 x3x_3?显然可以考虑先用次大值向上跳,如果能跳到 x1x_1 说明还有空间,直接继续用最大值跳。而对于第二种情况,直接从 x1x_1 开始用最大值跳。

  6. 对于 x1x_1 和 x2x_2 以及 x3x_3 我们要判断是否合法,如果不合法我们将其改为 n+1n+1 作为边界处理,n+1n+1 的父亲我们看作 aa。

  7. 这样我们就得到了 x1x_1 和 x2x_2 以及 x3x_3,直接分段计算贡献,对于第一段只需要看 deepx2deep_{x_2} 是否小于等于 deepadeep_{a},如果满足说明有第一段,直接算上即可,对于第二段我们要分成三种情况:

    • 如果 x1x_1 在 x2x_2 和 x3x_3 之间,那么我们分开计数,对于 x2x_2 到 x1x_1 的段用次大值,对于 x1x_1 到 x3x_3 的段用最大值。
    • 如果 x1x_1 在 x2(x3)x_2(x_3) 下方,我们直接用最大值计算。
    • 如果 x1x_1 在 x2(x3)x_2(x_3) 上方,我们直接用次大值计算。

    接下来继续考虑段三,显然也分成两种情况:

    • x1x_1 在 x3x_3 上方,那我们就用次大值计算 x3x_3 到 x1x_1 这一段,用最大值计算 x1x_1 到根。
    • x1x_1 在 x3x_3 下方,那我们直接用最大值计算从 x3x_3 到根。

    需要注意这里我们要特判深度是否大于 11 来确保到根这段时存在的,另外这里的求和大多数用了父亲到上一个是因为我们的定义,请仔细回想之前的定义,画个图就能想清楚了。

  8. 因为时间复杂度紧张我们需要优化取模,使用快速幂和超级快读。

具体实现流程:

  1. 定义变量,把我的变量名字中所有 f 看成 first,所有 s 看成 second,ny2 为 12\frac{1}{2} 的逆元,你就可以理解了。另外因为 fmax 是关键字所以我宏定义了一下防止重名。
C++
#include <bits/stdc++.h>
#define fmax Fmax
using namespace std;
vector<int> e[500001];
int a[500001], fmax[500001], smax[500001], fafmax[500001][21], powfmax[500001][21], fasmax[500001][21], powsmax[500001][21], fa[500001][21], deep[500001], ps[500001], sum, mod = 998244353, ny2;
  1. 取模优化,第一个是直接修改,第二个是返回的。
C++
void add(int &a, int b){
	a += b;
	if (a >= mod)
		a -= mod;
	if (a < 0)
		a += mod;
}
int remod(int a, int b){
	a += b;
	if (a >= mod)
		a -= mod;
	if (a < 0)
		a += mod;
	return a;
}
  1. dfsdfs,第一个用来计算深度,最大值,次大值,第二个用来维护倍增表。
C++
void dfs1(int u, int Fa){
	fmax[u] = a[u];
	smax[u] = 0;
	deep[u] = deep[Fa] + 1;
	for (int i = 0; i < e[u].size(); i++){
		int v = e[u][i];
		if (v == Fa)
			continue;
		dfs1(v, u);
		if (fmax[v] > fmax[u]){
			smax[u] = max(fmax[u], smax[v]);
			fmax[u] = fmax[v];
		} else if (fmax[v] > smax[u])
			smax[u] = fmax[v];
		if (smax[v] > smax[u])
			smax[u] = smax[v];
	}
}
void dfs2(int u, int Fa){
	int t1 = fmax[Fa], t2 = smax[Fa];
	fa[u][0] = Fa;
	fafmax[u][0] = t1;
	powfmax[u][0] = 1ll * t1 * t1 % mod;
	fasmax[u][0] = t2;
	powsmax[u][0] = 1ll * t2 * t2 % mod;
	ps[u] = ps[Fa];
	add(ps[u], fmax[u]);
	for (int i = 0; i < e[u].size(); i++){
		int v = e[u][i];
		if (v == Fa)
			continue;
		dfs2(v, u);
	}
}
  1. 快速幂。
C++
int qpow(int a, int b){
	if (b == 0)
		return 1;
	if (b == 1)
		return a;
	int t = qpow(a, b / 2);
	if (b % 2 == 1)
		return 1LL * t * t % mod * a % mod;
	else
		return 1LL * t * t % mod;
}
  1. 路径最大值求和,最大值平方求和。次大值求和,次大值平方求和。
C++
pair<int, int> gfmax(int x, int y){
	int t = deep[x] - deep[y], one = fmax[x], two = 1ll * fmax[x] * fmax[x] % mod;
	for (int i = 20; i >= 0; i--)
		if (t & (1 << i)){
			add(one, fafmax[x][i]);
			add(two, powfmax[x][i]);
			x = fa[x][i];
		}
	return {one, two};
}
pair<int, int> gsmax(int x, int y){
	int t = deep[x] - deep[y], one = smax[x], two = 1ll * smax[x] * smax[x] % mod;
	for (int i = 20; i >= 0; i--)
		if (t & (1 << i)){
			add(one, fasmax[x][i]);
			add(two, powsmax[x][i]);
			x = fa[x][i];
		}
	return {one, two};
}
  1. 第二段的贡献计算有点长,我给封装了。
C++
int cala(int l, int r, int one, int two, int k){
	return (two + 1ll * remod(remod(1, -2 * l), mod) * one % mod + 1ll * remod(1ll * r * r % mod, r) * k % mod) * ny2 % mod;
}
  1. 初始化和读入。
C++
	int n, q, opt, lastans = 0;
	ny2 = qpow(2, mod - 2);
	cin >> n >> q >> opt;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
  1. 更多的初始化和预处理倍增表。
C++
	dfs1(1, 0);
	for (int i = 1; i <= n; i++)
		add(sum, fmax[i]);
	fmax[0] = 1e9;
	deep[n + 1] = 1e9;
	dfs2(1, 0);
	for (int j = 1; j <= 20; j++)
		for (int i = 1; i <= n; i++){
			fa[i][j] = fa[fa[i][j - 1]][j - 1];
			add(fafmax[i][j], fafmax[i][j - 1]);
			add(fafmax[i][j], fafmax[fa[i][j - 1]][j - 1]);
			add(fasmax[i][j], fasmax[i][j - 1]);
			add(fasmax[i][j], fasmax[fa[i][j - 1]][j - 1]);
			add(powfmax[i][j], powfmax[i][j - 1]);
			add(powfmax[i][j], powfmax[fa[i][j - 1]][j - 1]);
			add(powsmax[i][j], powsmax[i][j - 1]);
			add(powsmax[i][j], powsmax[fa[i][j - 1]][j - 1]);
		}
  1. 读入查询并解密。
C++
	for (int i = 1; i <= q; i++){
		int l, r, x, ans = 0;
		cin >> l >> r >> x;
		l = (l + 1ll * opt * lastans) % n + 1;
		r = (r + 1ll * opt * lastans) % n + 1;
		x = (x + 1ll * opt * lastans) % n + 1;
		if (l > r)
			swap(l, r);
  1. 初始化 n+1n+1 号节点作为边界。
C++
fa[n + 1][0] = x;
		deep[n + 1] = deep[x] + 1;
		for (int j = 1; j <= 20; j++)
			fa[n + 1][j] = fa[fa[n + 1][j - 1]][j - 1];
  1. 计算不在被影响链上的点的贡献。
C++
add(ans, 1ll * remod(remod(sum, -ps[x]), mod) * len % mod);
  1. 找分界点 x1x_1,x2x_2 和 x3x_3。
C++
		int len = r - l + 1, x1 = x, x2 = x, x3 = x;
		for (int j = 20; j >= 0; j--)
			if (fmax[fa[x1][j]] == a[x])
				x1 = fa[x1][j];
		if (fmax[x1] != a[x])
			x1 = n + 1;
		for (int j = 20; j >= 0; j--)
			if (smax[fa[x2][j]] <= l && deep[fa[x2][j]] >= deep[x1])
				x2 = fa[x2][j];
		if (smax[x2] > l)
			x2 = n + 1;
		if (deep[x2] > deep[x1]){
			for (int j = 20; j >= 0; j--)
				if (smax[fa[x3][j]] <= r && deep[fa[x3][j]] >= deep[x1])
					x3 = fa[x3][j];
			if (smax[x3] > r)
				x3 = n + 1;
			if (deep[x3] == deep[x1])
				for (int j = 20; j >= 0; j--)
					if (fmax[fa[x3][j]] <= r)
						x3 = fa[x3][j];
		} else {
			for (int j = 20; j >= 0; j--)
				if (fmax[fa[x2][j]] <= l)
					x2 = fa[x2][j];
			if (fmax[x2] != a[x] && fmax[x2] > l)
				x2 = n + 1;
			if (fmax[x2] == a[x] && smax[x2] > l)
				x2 = n + 1;
			x3 = x1;
			for (int j = 20; j >= 0; j--)
				if (fmax[fa[x3][j]] <= r)
					x3 = fa[x3][j];
		}
		if (fmax[x3] != a[x] && fmax[x3] > r)
			x3 = n + 1;
		if (fmax[x3] == a[x] && smax[x3] > r)
			x3 = n + 1;
  1. 段一贡献。
C++
		if (deep[x] >= deep[x2])
			add(ans, 1ll * (l + r) % mod * len % mod * ny2 % mod * ((deep[x] - deep[x2] + 1) % mod) % mod);
  1. 段二贡献。
C++
		if (deep[x2] > deep[x1] && deep[x1] > deep[x3]){
			pair<int, int> t = gsmax(fa[x2][0], x1);
			add(ans, cala(l, r, t.first, t.second, deep[x2] - deep[x1]));
			t = gfmax(fa[x1][0], x3);
			add(ans, cala(l, r, t.first, t.second, deep[x1] - deep[x3]));
		} else if (deep[x1] <= deep[x3] && deep[x3] < deep[x2]){
			pair<int, int> t = gsmax(fa[x2][0], x3);
			add(ans, cala(l, r, t.first, t.second, deep[x2] - deep[x3]));
		} else if (deep[x2] > deep[x3]){
			pair<int, int> t = gfmax(fa[x2][0], x3);
			add(ans, cala(l, r, t.first, t.second, deep[x2] - deep[x3]));
		}
  1. 段三贡献。
C++
		if (deep[x1] < deep[x3]){
			add(ans, 1ll * gsmax(fa[x3][0], x1).first * len % mod);
			if (deep[x1] > 1)
				add(ans, 1ll * gfmax(fa[x1][0], 1).first * len % mod);
		} else if (deep[x3] > 1)
			add(ans, 1ll * gfmax(fa[x3][0], 1).first * len % mod);
  1. 输出答案,更新 lastans。
C++
		cout << ans << '\n';
		lastans = ans;
  1. 超级快读优化。
C++
namespace FastIO{
	constexpr int IOSIZE=1<<20;
	char ibuf[IOSIZE],obuf[IOSIZE];
	char*p1=ibuf,*p2=ibuf,*p3=obuf;
	inline char _gc(){if(p1==p2){p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin);if(p1==p2)return EOF;}return*p1++;}
	inline void _pc(char c){if(p3==obuf+IOSIZE){fwrite(obuf,1,p3-obuf,stdout);p3=obuf;}*p3++=c;}
	inline bool _isdigit(char c){return c>='0'&&c<='9';}
	inline bool _isspace(char c){return c<=' ';}
	template<typename T,bool IsSigned=std::is_signed<T>::value>struct IntReader{static void read(T&x){char c;while(!_isdigit(c=_gc()));x=c-'0';while(_isdigit(c=_gc()))x=x*10+(c-'0');}};
	template<typename T>struct IntReader<T,true>{static void read(T&x){char c;while(!_isdigit(c=_gc())&&c!='-');bool neg=(c=='-');if(neg)c=_gc();x=c-'0';while(_isdigit(c=_gc()))x=x*10+(c-'0');if(neg)x=-x;}};
	template<typename T,bool IsSigned=std::is_signed<T>::value>struct IntWriter{static void write(T x){if(x==0){_pc('0');return;}char buf[20];int n=0;do{buf[n++]=x%10+'0';x/=10;}while(x);while(n--)_pc(buf[n]);}};
	template<typename T>struct IntWriter<T,true>{static void write(T x){if(x==0){_pc('0');return;}if(x<0){_pc('-');x=-x;}char buf[20];int n=0;do{buf[n++]=x%10+'0';x/=10;}while(x);while(n--)_pc(buf[n]);}};
	inline void read(int&x){IntReader<int>::read(x);}
	inline void read(long long&x){IntReader<long long>::read(x);}
	inline void read(unsigned int&x){IntReader<unsigned int>::read(x);}
	inline void read(unsigned long long&x){IntReader<unsigned long long>::read(x);}
	inline void read(short&x){IntReader<short>::read(x);}
	inline void read(long&x){IntReader<long>::read(x);}
	inline void read(char&x){while(_isspace(x=_gc()));}
	inline void read(bool&b){char c;while(_isspace(c=_gc()));b=(c!='0'&&c!='f');}
	inline void read(std::string&s){char c;while(_isspace(c=_gc()));s.clear();do{s+=c;}while(!_isspace(c=_gc()));}
	template<typename T>typename std::enable_if<std::is_floating_point<T>::value,void>::type read(T&x){char c;while(!_isdigit(c=_gc())&&c!='-'&&c!='.');bool neg=(c=='-');if(c=='-'||c=='.')c=_gc();x=0;T base=1;while(_isdigit(c)){x=x*10+(c-'0');c=_gc();}if(c=='.'){c=_gc();while(_isdigit(c)){base*=0.1;x+=(c-'0')*base;c=_gc();}}if(neg)x=-x;}
	inline void write(int x){IntWriter<int>::write(x);}
	inline void write(long long x){IntWriter<long long>::write(x);}
	inline void write(unsigned int x){IntWriter<unsigned int>::write(x);}
	inline void write(unsigned long long x){IntWriter<unsigned long long>::write(x);}
	inline void write(short x){IntWriter<short>::write(x);}
	inline void write(long x){IntWriter<long>::write(x);}
	inline void write(char x){_pc(x);}
	inline void write(const char*x){while(*x)_pc(*x++);}
	inline void write(const std::string&s){for(char c:s)_pc(c);}
	inline void write(bool b){_pc(b?'1':'0');}
	template<typename T,typename...Args>inline void read(T&x,Args&...args){read(x);read(args...);}
	template<typename T,typename...Args>inline void write(const T&x,const Args&...args){write(x);write(args...);}
	struct FastIO_{~FastIO_(){if(p3>obuf)fwrite(obuf,1,p3-obuf,stdout);}template<typename T>FastIO_& operator>>(T& x){read(x);return *this;}template<typename T>FastIO_& operator<<(const T& x){write(x);return *this;}}fio;
}using namespace FastIO;
#define cin FastIO::fio
#define cout FastIO::fio
#define endl '\n'

完整代码:

C++
#include <bits/stdc++.h>
#define fmax Fmax
using namespace std;
namespace FastIO{
	constexpr int IOSIZE=1<<20;
	char ibuf[IOSIZE],obuf[IOSIZE];
	char*p1=ibuf,*p2=ibuf,*p3=obuf;
	inline char _gc(){if(p1==p2){p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin);if(p1==p2)return EOF;}return*p1++;}
	inline void _pc(char c){if(p3==obuf+IOSIZE){fwrite(obuf,1,p3-obuf,stdout);p3=obuf;}*p3++=c;}
	inline bool _isdigit(char c){return c>='0'&&c<='9';}
	inline bool _isspace(char c){return c<=' ';}
	template<typename T,bool IsSigned=std::is_signed<T>::value>struct IntReader{static void read(T&x){char c;while(!_isdigit(c=_gc()));x=c-'0';while(_isdigit(c=_gc()))x=x*10+(c-'0');}};
	template<typename T>struct IntReader<T,true>{static void read(T&x){char c;while(!_isdigit(c=_gc())&&c!='-');bool neg=(c=='-');if(neg)c=_gc();x=c-'0';while(_isdigit(c=_gc()))x=x*10+(c-'0');if(neg)x=-x;}};
	template<typename T,bool IsSigned=std::is_signed<T>::value>struct IntWriter{static void write(T x){if(x==0){_pc('0');return;}char buf[20];int n=0;do{buf[n++]=x%10+'0';x/=10;}while(x);while(n--)_pc(buf[n]);}};
	template<typename T>struct IntWriter<T,true>{static void write(T x){if(x==0){_pc('0');return;}if(x<0){_pc('-');x=-x;}char buf[20];int n=0;do{buf[n++]=x%10+'0';x/=10;}while(x);while(n--)_pc(buf[n]);}};
	inline void read(int&x){IntReader<int>::read(x);}
	inline void read(long long&x){IntReader<long long>::read(x);}
	inline void read(unsigned int&x){IntReader<unsigned int>::read(x);}
	inline void read(unsigned long long&x){IntReader<unsigned long long>::read(x);}
	inline void read(short&x){IntReader<short>::read(x);}
	inline void read(long&x){IntReader<long>::read(x);}
	inline void read(char&x){while(_isspace(x=_gc()));}
	inline void read(bool&b){char c;while(_isspace(c=_gc()));b=(c!='0'&&c!='f');}
	inline void read(std::string&s){char c;while(_isspace(c=_gc()));s.clear();do{s+=c;}while(!_isspace(c=_gc()));}
	template<typename T>typename std::enable_if<std::is_floating_point<T>::value,void>::type read(T&x){char c;while(!_isdigit(c=_gc())&&c!='-'&&c!='.');bool neg=(c=='-');if(c=='-'||c=='.')c=_gc();x=0;T base=1;while(_isdigit(c)){x=x*10+(c-'0');c=_gc();}if(c=='.'){c=_gc();while(_isdigit(c)){base*=0.1;x+=(c-'0')*base;c=_gc();}}if(neg)x=-x;}
	inline void write(int x){IntWriter<int>::write(x);}
	inline void write(long long x){IntWriter<long long>::write(x);}
	inline void write(unsigned int x){IntWriter<unsigned int>::write(x);}
	inline void write(unsigned long long x){IntWriter<unsigned long long>::write(x);}
	inline void write(short x){IntWriter<short>::write(x);}
	inline void write(long x){IntWriter<long>::write(x);}
	inline void write(char x){_pc(x);}
	inline void write(const char*x){while(*x)_pc(*x++);}
	inline void write(const std::string&s){for(char c:s)_pc(c);}
	inline void write(bool b){_pc(b?'1':'0');}
	template<typename T,typename...Args>inline void read(T&x,Args&...args){read(x);read(args...);}
	template<typename T,typename...Args>inline void write(const T&x,const Args&...args){write(x);write(args...);}
	struct FastIO_{~FastIO_(){if(p3>obuf)fwrite(obuf,1,p3-obuf,stdout);}template<typename T>FastIO_& operator>>(T& x){read(x);return *this;}template<typename T>FastIO_& operator<<(const T& x){write(x);return *this;}}fio;
}using namespace FastIO;
#define cin FastIO::fio
#define cout FastIO::fio
#define endl '\n'
vector<int> e[500001];
int a[500001], fmax[500001], smax[500001], fafmax[500001][21], powfmax[500001][21], fasmax[500001][21], powsmax[500001][21], fa[500001][21], deep[500001], ps[500001], sum, mod = 998244353, ny2;
void add(int &a, int b){
	a += b;
	if (a >= mod)
		a -= mod;
	if (a < 0)
		a += mod;
}
int remod(int a, int b){
	a += b;
	if (a >= mod)
		a -= mod;
	if (a < 0)
		a += mod;
	return a;
}
void dfs1(int u, int Fa){
	fmax[u] = a[u];
	smax[u] = 0;
	deep[u] = deep[Fa] + 1;
	for (int i = 0; i < e[u].size(); i++){
		int v = e[u][i];
		if (v == Fa)
			continue;
		dfs1(v, u);
		if (fmax[v] > fmax[u]){
			smax[u] = max(fmax[u], smax[v]);
			fmax[u] = fmax[v];
		} else if (fmax[v] > smax[u])
			smax[u] = fmax[v];
		if (smax[v] > smax[u])
			smax[u] = smax[v];
	}
}
void dfs2(int u, int Fa){
	int t1 = fmax[Fa], t2 = smax[Fa];
	fa[u][0] = Fa;
	fafmax[u][0] = t1;
	powfmax[u][0] = 1ll * t1 * t1 % mod;
	fasmax[u][0] = t2;
	powsmax[u][0] = 1ll * t2 * t2 % mod;
	ps[u] = ps[Fa];
	add(ps[u], fmax[u]);
	for (int i = 0; i < e[u].size(); i++){
		int v = e[u][i];
		if (v == Fa)
			continue;
		dfs2(v, u);
	}
}
int qpow(int a, int b){
	if (b == 0)
		return 1;
	if (b == 1)
		return a;
	int t = qpow(a, b / 2);
	if (b % 2 == 1)
		return 1LL * t * t % mod * a % mod;
	else
		return 1LL * t * t % mod;
}
pair<int, int> gfmax(int x, int y){
	int t = deep[x] - deep[y], one = fmax[x], two = 1ll * fmax[x] * fmax[x] % mod;
	for (int i = 20; i >= 0; i--)
		if (t & (1 << i)){
			add(one, fafmax[x][i]);
			add(two, powfmax[x][i]);
			x = fa[x][i];
		}
	return {one, two};
}
pair<int, int> gsmax(int x, int y){
	int t = deep[x] - deep[y], one = smax[x], two = 1ll * smax[x] * smax[x] % mod;
	for (int i = 20; i >= 0; i--)
		if (t & (1 << i)){
			add(one, fasmax[x][i]);
			add(two, powsmax[x][i]);
			x = fa[x][i];
		}
	return {one, two};
}
int cala(int l, int r, int one, int two, int k){
	return (two + 1ll * remod(remod(1, -2 * l), mod) * one % mod + 1ll * remod(1ll * r * r % mod, r) * k % mod) * ny2 % mod;
}
signed main(){
	int n, q, opt, lastans = 0;
	ny2 = qpow(2, mod - 2);
	cin >> n >> q >> opt;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	dfs1(1, 0);
	for (int i = 1; i <= n; i++)
		add(sum, fmax[i]);
	fmax[0] = 1e9;
	deep[n + 1] = 1e9;
	dfs2(1, 0);
	for (int j = 1; j <= 20; j++)
		for (int i = 1; i <= n; i++){
			fa[i][j] = fa[fa[i][j - 1]][j - 1];
			add(fafmax[i][j], fafmax[i][j - 1]);
			add(fafmax[i][j], fafmax[fa[i][j - 1]][j - 1]);
			add(fasmax[i][j], fasmax[i][j - 1]);
			add(fasmax[i][j], fasmax[fa[i][j - 1]][j - 1]);
			add(powfmax[i][j], powfmax[i][j - 1]);
			add(powfmax[i][j], powfmax[fa[i][j - 1]][j - 1]);
			add(powsmax[i][j], powsmax[i][j - 1]);
			add(powsmax[i][j], powsmax[fa[i][j - 1]][j - 1]);
		}
	for (int i = 1; i <= q; i++){
		int l, r, x, ans = 0;
		cin >> l >> r >> x;
		l = (l + 1ll * opt * lastans) % n + 1;
		r = (r + 1ll * opt * lastans) % n + 1;
		x = (x + 1ll * opt * lastans) % n + 1;
		if (l > r)
			swap(l, r);
		fa[n + 1][0] = x;
		deep[n + 1] = deep[x] + 1;
		for (int j = 1; j <= 20; j++)
			fa[n + 1][j] = fa[fa[n + 1][j - 1]][j - 1];
		add(ans, 1ll * remod(remod(sum, -ps[x]), mod) * len % mod);
		int len = r - l + 1, x1 = x, x2 = x, x3 = x;
		for (int j = 20; j >= 0; j--)
			if (fmax[fa[x1][j]] == a[x])
				x1 = fa[x1][j];
		if (fmax[x1] != a[x])
			x1 = n + 1;
		for (int j = 20; j >= 0; j--)
			if (smax[fa[x2][j]] <= l && deep[fa[x2][j]] >= deep[x1])
				x2 = fa[x2][j];
		if (smax[x2] > l)
			x2 = n + 1;
		if (deep[x2] > deep[x1]){
			for (int j = 20; j >= 0; j--)
				if (smax[fa[x3][j]] <= r && deep[fa[x3][j]] >= deep[x1])
					x3 = fa[x3][j];
			if (smax[x3] > r)
				x3 = n + 1;
			if (deep[x3] == deep[x1])
				for (int j = 20; j >= 0; j--)
					if (fmax[fa[x3][j]] <= r)
						x3 = fa[x3][j];
		} else {
			for (int j = 20; j >= 0; j--)
				if (fmax[fa[x2][j]] <= l)
					x2 = fa[x2][j];
			if (fmax[x2] != a[x] && fmax[x2] > l)
				x2 = n + 1;
			if (fmax[x2] == a[x] && smax[x2] > l)
				x2 = n + 1;
			x3 = x1;
			for (int j = 20; j >= 0; j--)
				if (fmax[fa[x3][j]] <= r)
					x3 = fa[x3][j];
		}
		if (fmax[x3] != a[x] && fmax[x3] > r)
			x3 = n + 1;
		if (fmax[x3] == a[x] && smax[x3] > r)
			x3 = n + 1;
		if (deep[x] >= deep[x2])
			add(ans, 1ll * (l + r) % mod * len % mod * ny2 % mod * ((deep[x] - deep[x2] + 1) % mod) % mod);
		if (deep[x2] > deep[x1] && deep[x1] > deep[x3]){
			pair<int, int> t = gsmax(fa[x2][0], x1);
			add(ans, cala(l, r, t.first, t.second, deep[x2] - deep[x1]));
			t = gfmax(fa[x1][0], x3);
			add(ans, cala(l, r, t.first, t.second, deep[x1] - deep[x3]));
		} else if (deep[x1] <= deep[x3] && deep[x3] < deep[x2]){
			pair<int, int> t = gsmax(fa[x2][0], x3);
			add(ans, cala(l, r, t.first, t.second, deep[x2] - deep[x3]));
		} else if (deep[x2] > deep[x3]){
			pair<int, int> t = gfmax(fa[x2][0], x3);
			add(ans, cala(l, r, t.first, t.second, deep[x2] - deep[x3]));
		}
		if (deep[x1] < deep[x3]){
			add(ans, 1ll * gsmax(fa[x3][0], x1).first * len % mod);
			if (deep[x1] > 1)
				add(ans, 1ll * gfmax(fa[x1][0], 1).first * len % mod);
		} else if (deep[x3] > 1)
			add(ans, 1ll * gfmax(fa[x3][0], 1).first * len % mod);
		cout << ans << '\n';
		lastans = ans;
	}
	return 0;
}
原文