QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#737253#9631. Median ReplacementDelovWA 2ms5220kbC++143.6kb2024-11-12 15:12:592024-11-12 15:13:00

Judging History

你现在查看的是最新测评结果

  • [2024-11-12 15:13:00]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:5220kb
  • [2024-11-12 15:12:59]
  • 提交

answer

#include <bits/stdc++.h>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
#define Rep(i,a,b) for(int i=a;i<=b;++i)
#define Dwn(i,a,b) for(int i=a;i>=b;--i)
#define int ll

const int maxn=3e2+10,Mod=1e9+7;

int pw(int x,int p){ int res=1,base=x;while(p){ if(p&1)res=1LL*res*base%Mod;p>>=1;base=1LL*base*base%Mod; }return res; }
int Inv(int x){ return pw(x,Mod-2); }

int n;
int L[maxn],R[maxn];
vector<int>vec;

int f[2][2][maxn],g[2][2][maxn];
int h[2];
int deg;
int S[maxn];

void Mul(int *f,int *g){
	Rep(i,0,deg)(f[i]) && (g[i]=(g[i]+1LL*f[i]*h[0])%Mod);
}

void Dec(int *f,int *g,const int k){
	Rep(i,0,deg)(f[i]) && (g[i]=(g[i]+1LL*f[i]*k)%Mod);
	Rep(i,0,deg)(f[i]) && (g[i+1]=(g[i+1]-f[i])%Mod);
}

void Add(int *f,int *g,const int k){
	Rep(i,0,deg)(f[i]) && (g[i]=(g[i]+1LL*f[i]*k)%Mod);
	Rep(i,0,deg)(f[i]) && (g[i+1]=(g[i+1]+f[i])%Mod);
}

int ps[maxn][maxn],sum[maxn][maxn];
int fac[maxn],inv[maxn];
int pre[maxn],suf[maxn];

void Init(const int n=302){
	fac[0]=inv[0]=1;Rep(i,1,n)fac[i]=1LL*fac[i-1]*i%Mod;
	inv[n]=Inv(fac[n]);Dwn(i,n-1,1)inv[i]=1LL*inv[i+1]*(i+1)%Mod;
	Rep(i,1,n){
		ps[i][0]=1;
		Rep(j,1,n)ps[i][j]=1LL*ps[i][j-1]*i%Mod;
	}
	Rep(i,1,n)Rep(j,1,n)sum[i][j]=(sum[i][j-1]+ps[j][i])%Mod;
}

int Lag(int n,int K){
	if(K==0)return n;
	if(n<=300)return sum[K][n];
	pre[0]=suf[K+2]=1;
	Rep(i,1,K+1)pre[i]=1LL*pre[i-1]*(n-i)%Mod;
	Dwn(i,1,K+1)suf[i]=1LL*suf[i+1]*(n-i)%Mod;
	int res=0;
	Rep(i,1,K+1)
		res=(res
			+ (((n-i)&1) ? -1LL : 1LL)
			* sum[K][i]*pre[i-1]%Mod*suf[i+1]
		)%Mod;
	return res;
}

int F[2][2],G[2][2],All;
int Calc(int m){
	memset(F,0,sizeof(F));
	memset(G,0,sizeof(G));
	F[0][0]=1;
	Rep(i,1,n){
		Rep(x,0,1)Rep(y,0,1)if(x+y<2){
			if(m>R[i])G[y][0]=(G[y][0]+1LL*F[x][y]*(R[i]-L[i]+1))%Mod;
			else if(m<L[i]){
				if(x+y+1<2)G[y][1]=(G[y][1]+1LL*F[x][y]*(R[i]-L[i]+1))%Mod;
			}else{
				G[y][0]=(G[y][0]+1LL*F[x][y]*(m-L[i]))%Mod;
				if(x+y+1<2)G[y][1]=(G[y][1]+1LL*F[x][y]*(R[i]-m+1))%Mod;
			}
		}
		Rep(x,0,1)Rep(y,0,1)F[x][y]=G[x][y],G[x][y]=0;
	}
	return (0LL + All -F[0][0]-F[0][1]-F[1][0])%Mod;
}

int Get(int l,int r){
	int res=0;
	Rep(i,0,deg)res=(res+(Lag(r,i)-Lag(l-1,i))*S[i]);
	return res;
}

void solve(){
	cin>>n;vec.resize(0);
	Rep(i,1,n)cin>>L[i],vec.emplace_back(L[i]);
	Rep(i,1,n)cin>>R[i],vec.emplace_back(R[i]);
	sort(vec.begin(),vec.end());
	vec.erase(unique(vec.begin(),vec.end()),vec.end());
	deg=n;
	int Ans=0;memset(f,0,sizeof(f)),memset(g,0,sizeof(g));
	All=1;Rep(i,1,n)All=1LL*All*(R[i]-L[i]+1)%Mod;
	for(int t=0;t+1<vec.size();++t){
		int l=vec[t]+1,r=vec[t+1]-1;
		if(l>r)continue;
		f[0][0][0]=1;
		h[0]=0,h[1]=0;
		Rep(i,1,n){
			if(L[i]>r){ h[0]=R[i]-L[i]+1;Mul(f[0][0],g[0][1]); }

			else if(R[i]<l){ h[0]=R[i]-L[i]+1;Rep(x,0,1)Rep(y,0,1)if(x+y<2)Mul(f[x][y],g[y][0]); }
			else {
				Dec(f[0][0],g[0][1],R[i]+1);
				Rep(x,0,1)Rep(y,0,1)if(x+y<2)Add(f[x][y],g[y][0],-L[i]);
			}
			Rep(x,0,1)Rep(y,0,1)if(x+y<2)
				Rep(i,0,deg)f[x][y][i]=g[x][y][i],g[x][y][i]=0;
		}Rep(i,0,deg)S[i]=0;
		Rep(x,0,1)Rep(y,0,1)if(x+y<2)Rep(i,0,deg)S[i]=(S[i]+f[x][y][i])%Mod,f[x][y][i]=0;
		--r;
		if(l<=r)
			Rep(i,0,deg)if(S[i]){
				int A=(Lag(r,i+1)-Lag(l-1,i+1))%Mod,B=(Lag(r+1,i+1)-Lag(l,i+1))%Mod;
				int C=(Lag(r+1,i)-Lag(l,i))%Mod;
				Ans=(Ans-1LL*S[i]*A%Mod-1LL*S[i]*C%Mod+1LL*B*S[i])%Mod;
			}
		++r;
		Ans=(Ans+1LL*(Calc(r)-Calc(r+1))*r)%Mod;
	}
	for(auto it : vec)Ans=(Ans+1LL*(Calc(it)-Calc(it+1))*it)%Mod;;
	cout<<(Ans+Mod)%Mod<<"\n";
}

int tex;
#undef int
int main (){ ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);Init();cin>>tex;while(tex--)solve(); }


详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 5176kb

input:

10
5
5 1 4 3 2
14 2 5 3 2
5
4 5 1 2 3
13 7 1 2 3
5
5 2 5 3 1
10 2 12 3 2
5
5 5 3 1 5
57 5 3 1 5
5
2 2 3 3 5
4 5 4 4 5
5
4 5 3 5 3
13 7 3 5 3
5
5 1 4 2 3
14 3 4 2 3
5
1 2 5 4 5
2 8 5 7 5
5
1 1 3 5 1
8 2 3 8 1
5
4 4 4 2 3
5 10 5 2 3

output:

180
170
650
265
182
173
120
296
192
131

result:

ok 10 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 5160kb

input:

10
5
1 2 2 5 3
6 4 2 6 3
5
4 4 1 4 3
6 7 2 5 3
5
5 3 4 2 4
5 7 5 2 6
5
1 5 3 5 2
7 7 3 5 2
5
1 3 3 2 2
10 5 3 2 2
5
4 4 4 5 3
4 11 9 5 3
5
5 3 2 1 3
13 5 2 1 5
5
5 4 1 2 5
10 6 1 2 5
5
3 5 3 4 2
5 9 3 5 2
5
1 1 3 2 1
7 3 3 3 1

output:

120
230
144
110
110
289
324
89
140
122

result:

ok 10 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 5220kb

input:

10
5
3 1 3 4 4
9 1 3 10 4
5
1 1 3 1 1
9 1 3 3 1
5
5 1 2 3 1
74 1 2 3 1
5
2 5 5 3 4
5 6 8 3 4
5
2 1 3 4 5
2 4 6 4 5
5
2 4 2 1 3
2 11 3 2 3
5
1 5 4 4 2
1 14 6 6 2
5
4 1 3 5 1
9 2 4 5 1
5
4 1 2 4 4
6 1 6 4 4
5
3 2 5 3 5
8 8 5 3 5

output:

196
76
140
172
72
80
486
84
65
224

result:

ok 10 lines

Test #4:

score: -100
Wrong Answer
time: 2ms
memory: 5144kb

input:

10
5
676437428 903889545 700650370 965758082 146716866
676437431 903889567 700650370 965758082 146716866
5
517457740 64600397 388618400 783268973 388618400
517457797 64600397 388618400 783268973 388618400
5
971452763 106948541 259878781 537741075 9504353
971452780 106948544 259878781 537741075 95043...

output:

128289630
466936225
542713907
762037329
497944943
69521138
739413553
999269895
315606450
548133785

result:

wrong answer 1st lines differ - expected: '157838571', found: '128289630'