题解:P10305 [THUWC 2020] 序排泡冒
前言
提供一种代码量约为 3.5K,最大点约 1.8s 的
思路
看到与冒泡排序轮数有关的东西,考虑建立双射。
对于一个长度为
现在给出了
首先,对于
对于
而
考虑判断无解,唯一的条件就是
对于树上的一次询问
我们现在要求的就是
考虑树剖后把原序列拍到 dfs 序上,而一条路径是由很多个 dfs 的区间组成的。由于我们要区分
对于前缀最大值来说,若没有加数,从区间左端点出发,一直跳到右边第一个比当前位置的值大的,直到跳到
总复杂度
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+5,mod = 998244353;
inline int qpow(int x,int y)
{
int res = 1;
while(y)
{
if(y&1) res = 1ll*res*x%mod;
y>>=1;
x = 1ll*x*x%mod;
}
return res;
}
int fac[N];
int n,q,a[N],f[N],dfn[N],pre[N],idx,son[N],sz[N],top[N],dep[N];
vector<int> g[N];
void dfs1(int u,int fa)
{
dep[u] = dep[fa]+1,sz[u] = 1,f[u] = fa;
for(auto v:g[u])
{
if(v==fa) continue;
dfs1(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[son[u]]) son[u] = v;
}
}
void dfs2(int u,int tp)
{
top[u] = tp,pre[dfn[u] = ++idx] = u;
if(!son[u]) return;
dfs2(son[u],tp);
for(auto v:g[u])
{
if(v==f[u]||v==son[u]) continue;
dfs2(v,v);
}
}
inline int lca(int x,int y)
{
while(top[x]^top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x = f[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
return x;
}
inline int dis(int x,int y){return dep[x]+dep[y]-2*dep[lca(x,y)];}
inline int jump(int x,int k)//x 的 k 级祖先
{
while(1)
{
if(k<=dep[x]-dep[top[x]]) return pre[dfn[x]-k];
k-=dep[x]-dep[top[x]]+1;
x = f[top[x]];
}
}
inline int get(int x,int y,int z)//路径上第 z 个点
{
int l = lca(x,y);
if(dep[x]-dep[l]>=z) return jump(x,z);
else return jump(y,dep[x]+dep[y]-2*dep[l]-z);
}
int to1[N][20],to2[N][20],stk[N],tot,b[N];
inline int ask1(int l,int r,int &mx)//前缀最大值
{
int p = l;
for(int i = 19;~i;i--)
if(b[to1[p][i]]<mx) p = to1[p][i];
if(b[p]<mx) p = to1[p][0];
if(p>r) return 0;
int res = 1;
for(int i = 19;~i;i--)
if(to1[p][i]<=r)
res+=(1<<i),p = to1[p][i];
mx = b[p];
return res;
}
inline int ask2(int l,int r,int &mx)//后缀最大值
{
int p = r;
for(int i = 19;~i;i--)
if(b[to2[p][i]]<mx) p = to2[p][i];
if(b[p]<mx) p = to2[p][0];
if(p<l) return 0;
int res = 1;
for(int i = 19;~i;i--)
if(to2[p][i]>=l)
res+=(1<<i),p = to2[p][i];
mx = b[p];
return res;
}
inline int ask(int x,int y,int &mx)
{
vector<pair<int,int>> vec;
int res = 0;
while(top[x]^top[y])
{
if(dep[top[x]]>dep[top[y]])
{
res+=ask2(dfn[top[x]],dfn[x],mx);
x = f[top[x]];
}
else
{
vec.push_back({dfn[top[y]],dfn[y]});
y = f[top[y]];
}
}
if(dep[x]<dep[y]) vec.push_back({dfn[x],dfn[y]});
else res+=ask2(dfn[y],dfn[x],mx);
reverse(vec.begin(),vec.end());
for(auto _:vec) res+=ask1(_.first,_.second,mx);
return res;
}
signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>q;
fac[0] = 1;
for(int i = 1;i<=n;i++)
fac[i] = 1ll*fac[i-1]*i%mod;
for(int i = 1;i<=n;i++)
cin>>a[i];
for(int i = 1,u,v;i<n;i++)
cin>>u>>v,g[u].push_back(v),g[v].push_back(u);
dfs1(1,0),dfs2(1,1);
for(int i = 1;i<=n;i++)
b[dfn[i]] = a[i];
b[0] = b[n+1] = 2e9;
stk[tot = 0] = n+1;
for(int i = n;i;i--)
{
while(top&&b[stk[tot]]<b[i]) to2[stk[tot]][0] = i,tot--;
to1[i][0] = stk[tot];
stk[++tot] = i;
}
to1[n+1][0] = n+1;
for(int j = 1;j<20;j++)
for(int i = 1;i<=n+1;i++)
to1[i][j] = to1[to1[i][j-1]][j-1],to2[i][j] = to2[to2[i][j-1]][j-1];
while(q--)
{
int x,y,z;
cin>>x>>y>>z;
int d = dis(x,y);
if(z==0) cout<<"1\n";
else if(z>=d)
{
int mx = 0;
int res = ask(x,y,mx);
if(res==d+1) cout<<fac[d+1]<<'\n';
else cout<<"0\n";
}
else
{
int p1 = get(y,x,z),p2 = get(y,x,z-1);
int mx = 0;
int res1 = ask(x,p1,mx);
int res2 = ask(p2,y,mx);
if(res2!=z) cout<<"0\n";
else cout<<1ll*fac[z]*qpow(z+1,res1)%mod<<'\n';
}
}
return 0;
}