Link
如果(l=r)那么我们给它加个特判。(虽然数据里没有)
先写出答案的表达式:
[frac{int_{l_1}^{r_1}cdotsint_{l_n}^{r_n}|sumlimits_{i=1}^nx_i|mathrm dx_ncdotsmathrm dx_1}{prodlimits_{i=1}^n(r_i-l_i)}
]
注意到(|x|=max(x,0)-min(x,0)),(min,max)的计算方法非常相似,因此我们只考虑计算:
[int_{l_1}^{r_1}cdotsint_{l_n}^{r_n}max(sumlimits_{i=1}^nx_i,0)mathrm dx_ncdotsmathrm dx_1
]
考虑容斥,我们需要计算的变成了:
[f(t_1,cdots,t_n)=int_{-infty}^{t_1}cdotsint_{-infty}^{t_n}max(sumlimits_{i=1}^nx_i,0)mathrm dx_ncdotsmathrm dx_1
]
设(y_i=t_i-x_i,S=sumlimits_{i=1}^nt_i):
[egin{aligned}
&f(t_1,cdots,t_n)=int_0^{+infty}cdotsint_0^{+infty}max(S-sumlimits_{i=1}^ny_i,0)mathrm dy_ncdotsmathrm dy_1\
&ecauseqquadmax(S-sumlimits_{i=1}^ny_i,0)=int_0^{+infty}[sumlimits_{i=1}^{n+1}y_ile S]mathrm dy_{n+1}\
& hereforeqquad f(t_1,cdots,t_n)=int_0^{+infty}cdotsint_0^{+infty}[sumlimits_{i=1}^{n+1}y_ile S]mathrm dy_{n+1}cdotsmathrm dy_1
end{aligned}
]
设(s_m=sumlimits_{i=1}^m y_i):
[f(t_1,cdots,t_n)=int_0^Scdotsint_{s_n}^S1mathrm ds_{n+1}cdotsmathrm ds_1=frac{S^{n+1}}{(n+1)!}
]
那么答案就是:
[sumlimits_{t_iin{l_i,r_i}}(-1)^{sumlimits_{i=1}^n[t_i=l_i]}f(t_1,cdots,t_n)
]
#include<cstdio>
const int N=100007,P=998244353;
int read(){int x;scanf("%d",&x);return x;}
void inc(int&a,int b){a+=b-P,a+=a>>31&P;}
void dec(int&a,int b){a-=b,a+=a>>31&P;}
int mul(int a,int b){return 1ll*a*b%P;}
int pow(int a,int k){int r=1;for(;k;k>>=1,a=mul(a,a))if(k&1)r=mul(a,r);return r;}
int n,l[N],r[N],ans;
void dfs(int k,int s,int f1,int f2)
{
if(k>n)
{
if(s>0) inc(ans,mul(f1,pow(s,n+1)));
if(s<0) dec(ans,mul(f2,pow(-s,n+1)));
return ;
}
dfs(k+1,s+l[k],P-f1,f2),dfs(k+1,s+r[k],f1,P-f2);
}
int main()
{
n=read();int m=n+1;
for(int i=1;i<=n;++i) l[i]=read(),r[i]=read(),m=mul(m,mul(r[i]-l[i],i));
dfs(1,0,1,P-1),printf("%d
",mul(ans,pow(m,P-2)));
}