题目概括:
给定一棵有 个节点的带点权(点权为数列 )树,定义点 的 为该字数内点权的最大值。请求出分别把编号为 的点的点权修依次修改为 至 时树上的所有点的 的和的和,形式化的,求出 ,答案对 取模,并强制在线。
思路分析:
-
注意到这里的 为子树之内的最大点权,因此点权随深度单调不降。而修改一个点只会影响这个点到根的链上所有点的点权。
-
我们考虑到当 被修改成 时,只会影响该点和子树包含该点的点的 ,考虑 被修改对上述点的影响分为两部分 后再令 ,如果 那么 一定不是 子树中的最大值,所以这不会影响 ,否则我们就把 改成除了 的最大值,也就是次大值,我们记这个值为 。这个时候对于任意 ,这里 为 点的子树,。
-
考虑分段讨论 的取值,显然对于 只有两种取值: 和 ,由于我们需要确定是用最大值还是次大值,我们需要求出节点 使其满足 并且深度最小,这样在区间 中 为次大值,而在 中 为最大值。
-
继续考虑 的取值,由于其具有单调性,所以我们可以找到两个分界点 和 ,对于 我们要求其满足 且深度最小,对于 我们要求其满足 且其深度最小,这样 的取值被我们分成了三个部分:
- 对于 ,,所以 。
- 对于 ,,所以 或 这点我们之后分类讨论。
- 对于 ,,所以 。
-
我们考虑这些分段的贡献,对于第一个分段,,直接用等差数列求和公式求和 。对于第二个分段, 或 ,我们考虑两种情况,对于 ,,对于 ,,对于前半段,显然一共有 个数字,所以贡献就是 ,对于后半段我们还是可以套用等差数列求和,为 ,相加得到总贡献并整理式子。
-
整理好式子显然我们可以发现我们需要维护 和 这两个和,我们还是可以通过倍增来实现,继续计算第三个分段,,直接乘上数量得到 ,这个式子也需要维护 。
-
到这里我们便能够看出来我们需要维护的变量有哪些,分别要维护:最大值求和,最大值平方求和,次大值求和,次大值平方求和,路径求和,祖先倍增,而这几个都可以通过倍增来维护,子树最大值,子树次大值,深度,全局权值之和,这些都可以通过 来维护。为了维护求和我们还需要做两个函数来快速算出 的路径上的最大值求和,最大值平方求和以及次大值求和,次大值平方求和。
-
因为这里需要除 所以我们要求出 的逆元即 。
-
接下来考虑分解点如何去找,对于最大值次大值分界点 ,我们可以使用倍增一次性得到。对于分段分界点 ,我们先用次大值向上找并且不超过 这样才能保证始终是次大值,然后考虑两种情况:
- ,说明 在 以下也就是次大值区间,那我们不需要额外考虑 ,直接去跳 即可。
- ,说明 用次大值跳到了 那我们还需要继续来看看 能不能用最大值继续向上跳。
-
对于 9 的第一种情况而言我们具体如何找 ?显然可以考虑先用次大值向上跳,如果能跳到 说明还有空间,直接继续用最大值跳。而对于第二种情况,直接从 开始用最大值跳。
-
对于 和 以及 我们要判断是否合法,如果不合法我们将其改为 作为边界处理, 的父亲我们看作 。
-
这样我们就得到了 和 以及 ,直接分段计算贡献,对于第一段只需要看 是否小于等于 ,如果满足说明有第一段,直接算上即可,对于第二段我们要分成三种情况:
- 如果 在 和 之间,那么我们分开计数,对于 到 的段用次大值,对于 到 的段用最大值。
- 如果 在 下方,我们直接用最大值计算。
- 如果 在 上方,我们直接用次大值计算。
接下来继续考虑段三,显然也分成两种情况:
- 在 上方,那我们就用次大值计算 到 这一段,用最大值计算 到根。
- 在 下方,那我们直接用最大值计算从 到根。
需要注意这里我们要特判深度是否大于 来确保到根这段时存在的,另外这里的求和大多数用了父亲到上一个是因为我们的定义,请仔细回想之前的定义,画个图就能想清楚了。
-
因为时间复杂度紧张我们需要优化取模,使用快速幂和超级快读。
具体实现流程:
- 定义变量,把我的变量名字中所有
f看成first,所有s看成second,ny2为 的逆元,你就可以理解了。另外因为fmax是关键字所以我宏定义了一下防止重名。
#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;- 取模优化,第一个是直接修改,第二个是返回的。
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;
}- 初始化和读入。
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);- 输出答案,更新
lastans。
cout << ans << '\n';
lastans = ans;- 超级快读优化。
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'完整代码:
#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;
}