QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#416367#8410. Splatanie ciągów [A]Naganohara_YoimiyaCompile Error//C++148.9kb2024-05-21 19:42:402024-05-21 19:42:44

Judging History

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

  • [2024-05-21 19:42:44]
  • 评测
  • [2024-05-21 19:42:40]
  • 提交

answer

#include<bits/stdc++.h>

#define ll long long
#define mk make_pair
#define fi first
#define se second
// #define int long long

using namespace std;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
	for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
	return x*f;
}

const int mod=1e9+7;
int ksm(int x,ll y,int p=mod){
	int ans=1;y%=(p-1);
	for(int i=y;i;i>>=1,x=1ll*x*x%p)if(i&1)ans=1ll*ans*x%p;
	return ans%p;
}
int inv(int x,int p=mod){return ksm(x,p-2,p)%p;}
mt19937 rnd(time(0));
int randint(int l,int r){return rnd()%(r-l+1)+l;}
void add(int &x,int v){x+=v;if(x>=mod)x-=mod;}
void Mod(int &x){if(x>=mod)x-=mod;}
int cmod(int x){if(x>=mod)x-=mod;return x;}

template<typename T>void cmax(T &x,T v){x=max(x,v);}
template<typename T>void cmin(T &x,T v){x=min(x,v);}

const int N=3e5+5;
int a[N],b[N],n,m;

int Calc(int l,int r,int x){
	if(x==1)return n+1;
	vector<int>A;for(int i=l;i<=r;i++)A.emplace_back(a[i]);
	int m=A.size(),p=0,ans=0;
	// cout<<"calc A = ";for(int i:A)cout<<i<<" ";
	while(p<m-1){
		int q=p+1;
		while(q<m-1&&(A[q+1]>A[q])==(A[p+1]>A[p]))q++;
		ans+=(q-p-1)/(x-1);
		p=q;
	}
	// cout<<"ans = "<<ans<<endl;
	return ans;
}

const int Iv6=inv(6);
const int Iv2=inv(2);

signed main(void){

#ifndef ONLINE_JUDGE
	freopen("123.in","r",stdin);
	freopen("123.out","w",stdout);
	// freopen("in.txt","r",stdin);
#endif

	n=read(),m=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=m;i++)b[i]=read();

	vector<int>ca(n+1),cb(m+1);
	for(int i=1;i<=n;i++)ca[i]=n-i+1;
	for(int i=1;i<=m;i++)cb[i]=m-i+1;
	for(int i=1;i<=n;i++)add(ca[i],ca[i-1]);
	for(int i=1;i<=m;i++)add(cb[i],cb[i-1]);

	vector<int>ans(n+m+1);
	for(int k=2;k<=n+m;k++)ans[k]=1ll*((1ll*n*(n+1)/2)%mod)*((1ll*m*(m+1)/2)%mod)%mod;

	auto calc=[&](){
		vector<pair<int,int> >As;
		vector<vector<pair<int,int> > >vec(n+1);

		int p=1;	
		while(p<=n-1){
			int q=p+1;
			while(q<=n-1&&(a[q+1]>a[q])==(a[p+1]>a[p]))q++;
			As.emplace_back(mk(p,q)),p=q;
		}
		// cout<<"As = ";for(auto [l,r]:As)cout<<"["<<l<<","<<r<<"] ";puts(""); 
		for(int i=1;i<=n;i++)vec[i].emplace_back(mk(0,1));
		for(auto [l,r]:As){
			for(int j=1;j<=r-l;j++)vec[j].emplace_back(mk(l,r));
		}
		// puts("ok");

		auto getSum=[&](int L,int R){
			return 1ll*(L+R)*(R-L+1)%mod*Iv2%mod;
		};
		auto Sum=[&](int L,int R){
			return 1ll*(L+R)*(R-L+1)%mod*Iv2%mod;
		};
		auto getv_1=[&](int L,int x){ // sum i = 0...L floor(i/x)
			int ans=0,p=(int)(L/x);
			add(ans,1ll*Iv2*(p-1)%mod*p%mod*x%mod);
			add(ans,1ll*p*(L%x+1)%mod);
			// for(int i=0;i<=L;i++)add(ans,(int)(i/x));
			return ans;
		};
		auto getv_2=[&](int L,int x){ // sum i = 0...L floor(i/x)*(L-i+1)
			int ans=0;
			for(int i=1;i*x<=L;i++){
				int l=i*x,r=min(L,(i+1)*x-1);
				add(ans,1ll*min(i,m+1)*Sum(L-r+1,L-l+1)%mod);
			}
			return ans;
		};
		auto getv_3=[&](int L,int x){ // sum i = 0...L floor(i/x)^2
			int ans=0,p=(int)(L/x);
			add(ans,1ll*Iv6*p%mod*(p-1)%mod*(p+p-1)%mod*x%mod);
			add(ans,1ll*p*p%mod*(L%x+1)%mod);
			// for(int i=1;i*x<=L;i++){
			// 	int l=i*x,r=min(L,(i+1)*x-1);
			// 	add(ans,1ll*i*Sum(l,r)%mod);
			// }
			// for(int i=0;i<=L;i++)add(ans,((ll)(i/x))*(i/x)%mod);
			return ans;
		};
		auto getv_4=[&](int L,int x){ // sum i = 0...L floor(i/x)^2 * (L-i+1)
			int ans=0;
			for(int i=1;i*x<=L;i++){
				int l=i*x,r=min(L,(i+1)*x-1);
				add(ans,1ll*min(i,m+1)*min(i,m+1)%mod*Sum(L-r+1,L-l+1)%mod);
			}
			// for(int i=0;i<=L;i++)add(ans,((ll)(i/x))*((ll)(i/x))%mod*(L-i+1)%mod);
			return ans;
		};

		vector<int>mis(n+m+1);
		for(int k=2;k<=n;k++){
			// cerr<<"k = "<<k<<endl;
			// cout<<"now ans "<<k<<" = "<<ans[k]<<endl;
			int pre=n,sum0=0,sum1=0,sum2=0,rr1=0,rr2=0,rr0=1;
			int ms=0;

			// int tot=0;
			// for(int i=1;i<=n;i++)for(int j=i;j<=n;j++){
			// 	int s=Calc(i,j,k);
			// 	// cout<<"calc i,j,k = "<<Calc(i,j,k)<<endl;
			// 	add(sum1,min(s,m+1)),add(sum2,1ll*min(s,m+1)*min(s,m+1)%mod);
			// 	if(s==0)sum0++;

			// 	for(int j=1;j<s;j++)add(tot,max(0ll,m-j+1));
			// }
			// cout<<"tot = "<<tot<<endl;
			// cout<<"sum0,sum1,sum2 = "<<" "<<sum0<<" "<<sum1<<" "<<sum2<<endl;
			ms=1ll*((1ll*(m+m+3)*sum1-sum2-1ll*(m+m+2)*n%mod*(n+1)%mod*Iv2%mod)%mod+mod)*Iv2%mod;
			// cout<<"ms = "<<ms<<endl;
			add(ms,1ll*sum0*(m+1)%mod);
			// cout<<"ms = "<<ms<<endl;
			sum0=0;
			sum1=0,sum2=0;

			auto A=vec[k];

			vector<pair<int,int> >B;
			for(int i=0;i<A.size();i++){
				#define l fi
				#define r se
				B.emplace_back(A[i]);
				int p=A[i].r;
				int nx=(i==((int)(A.size())-1)?n:A[i+1].l);
				while(p<nx){
					int q=min(nx,p+k-1);
					B.emplace_back(mk(p,q)),p=q;
				}
				#undef l
				#undef r
			}
			A=B;

			// cout<<" -> A = ";for(auto [l,r]:A)cout<<"["<<l<<","<<r<<"] ";puts("");

			int nowsum=0,p=(int)(A.size())-1;
			for(int i=A.size()-1;i>=0;i--){
				// In[l] == In[r]
				#define l fi
				#define r se
				// cerr<<"now i = "<<i<<" [l,r] = ["<<A[i].l<<","<<A[i].r<<"] pre = "<<pre<<endl;
				int len=max(0ll,A[i].r-A[i].l-1);
				// cout<<" len = "<<len<<endl;
				if(len>=2){
					add(sum1,getv_2(max(len-2,0ll),k-1));
					add(sum2,getv_4(max(len-2,0ll),k-1));
				}
				add(sum0,getSum(max(len-k+1,0ll),len));

				// cout<<"now sum0,1,2 = "<<sum0<<" "<<sum1<<" "<<sum2<<" rr0,1,2 = "<<rr0<<" "<<rr1<<" "<<rr2<<endl;

				for(int j=0;j*(k-1)<=A[i].r-A[i].l;j++){
					int cl=j*(k-1)+2,cr=min((j+1)*(k-1)+1,A[i].r-A[i].l);
					int cnt_l=cr-cl+1;if(j==0)cnt_l++;

					if(cnt_l<=0)break;

					// cout<<"j = "<<j<<" cnt_l = "<<cnt_l<<endl;

					while(p>=i+1&&nowsum+j>m+1){
						// cout<<" p = "<<p<<" nowsum = "<<nowsum<<endl;
						nowsum-=(A[p].r-A[p].l-1)/(k-1);
						int lle=A[p].r-A[p].l;
						add(rr1,mod-1ll*lle*nowsum%mod);
						int v1=0,v2=0;
						if(lle>=2)v1=getv_1(lle-2,k-1),v2=getv_3(lle-2,k-1);
						add(rr1,mod-v1);

						add(rr2,mod-1ll*nowsum*nowsum%mod*lle%mod);
						add(rr2,mod-2ll*nowsum*v1%mod);
						add(rr2,mod-v2);
						p--;
					}

					// cout<<" -> p = "<<p<<" rr1,rr2 = "<<rr1<<" "<<rr2<<" nowsum = "<<nowsum<<endl;

					add(sum1,1ll*j*cnt_l%mod*(A[p].r-A[i].r)%mod);
					add(sum1,1ll*rr1*cnt_l%mod);

					add(sum2,1ll*j*j%mod*cnt_l%mod*(A[p].r-A[i].r)%mod);
					add(sum2,2ll*j*rr1%mod*cnt_l%mod);
					add(sum2,1ll*rr2*cnt_l%mod);

					// cout<<"now sum1,2 = "<<sum1<<" "<<sum2<<endl;

					int nowv=nowsum+j;
					if(nowv>=m+1){
						add(sum1,1ll*(m+1)*(n-A[p].r+1)%mod*cnt_l%mod);
						add(sum2,1ll*(m+1)*(m+1)%mod*(n-A[p].r+1)%mod*cnt_l%mod);
						// cout<<" IF -> sum1,2 = "<<sum1<<" "<<sum2<<endl;
						continue;
					}
					int lft=m+1-nowsum-j;assert(lft>=0);

					int rle=1;
					if(p+1<A.size())rle=A[p+1].r-A[p+1].l+1;
					int rt=min((lft+1)*(k-1)-1,max(0ll,rle-2));

					int rv1=getv_1(rt,k-1),rv2=getv_3(rt,k-1);

					int cnt_r=min((lft+1)*(k-1)+1,rle);
					// cout<<"cnt_r = "<<cnt_r<<" rv1,rv2 = "<<rv1<<" "<<rv2<<endl;
					add(sum1,1ll*(j+nowsum)*cnt_r%mod*cnt_l%mod);
					add(sum1,1ll*cnt_l*rv1%mod);

					add(sum2,1ll*(j+nowsum)*(j+nowsum)%mod*cnt_r%mod*cnt_l%mod);
					add(sum2,2ll*(j+nowsum)*rv1%mod*cnt_l%mod);
					add(sum2,1ll*rv2*cnt_l%mod);

					// cout<<"now sum1,2 = "<<sum1<<" "<<sum2<<endl;

					add(sum1,1ll*(m+1)*(n-A[p].r+1-cnt_r)%mod*cnt_l%mod);
					add(sum2,1ll*(m+1)*(m+1)%mod*(n-A[p].r+1-cnt_r)%mod*cnt_l%mod);
					
					// cout<<"now sum1,2 = "<<sum1<<" "<<sum2<<endl;
				}

				// puts("ok");

				// In[l] < In[r]
				int llen=max(0ll,A[i].r-A[i].l-2);
				int vl=getv_1(llen,k-1);
				int vvl=getv_3(llen,k-1);

				// cout<<"vl = "<<vl<<endl;
				// add(sum1,1ll*vl*(n-A[i].r+1)%mod);
				// add(sum2,1ll*vvl%mod*(n-A[i].r+1)%mod);

				// add(sum1,1ll*rr1*(pre-A[i].l)%mod);
				// add(sum2,1ll*rr2*(pre-A[i].l)%mod);

				// add(sum2,2ll*vl*rr1%mod);

				add(sum0,1ll*min(k,A[i].r-A[i].l)*rr0%mod);

				add(rr2,vvl);
				add(rr2,2ll*(len/(k-1))*rr1%mod);
				add(rr2,1ll*(len/(k-1))*(len/(k-1))%mod*(A[p].r-A[i].r)%mod);
				
				add(rr1,getv_1(llen,k-1));
				add(rr1,1ll*(len/(k-1))*(A[p].r-A[i].r)%mod);

				if(A[i].r-A[i].l+1>=k+1)rr0=0;
				rr0+=min(k,A[i].r-A[i].l);

				pre=A[i].l;

				nowsum+=(int)(A[i].r-A[i].l-1)/(k-1);

				// cout<<" -> sum0,1,2 = "<<sum0<<" "<<sum1<<" "<<sum2<<" rr0,1,2 = "<<rr0<<" "<<rr1<<" "<<rr2<<endl;
				#undef l
				#undef r
			}
			// mis[k]=1ll*(1ll*(m+m+1)*sum1-sum2+mod)*Iv2%mod;
			ms=1ll*((1ll*(m+m+3)*sum1-1ll*sum2-1ll*(m+m+2)*n%mod*(n+1)%mod*Iv2)%mod+mod)*Iv2%mod;
			add(ms,1ll*sum0*(m+1)%mod);
			mis[k]=ms;
			// cout<<"ms = "<<ms<<endl;
			if(k>=3&&mis[k]==0&&mis[k-1]==0)break;
			// cout<<"sum0,"

			add(ans[k],mod-ms);
			// cout<<" -> ans "<<k<<" = "<<ans[k]<<endl;
		}

		// for(int k=2;k<=n+m;k++)add(ans[k],mod-mis[k]);
	};

	calc();
	swap(n,m),swap(a,b);
	calc();

	for(int i=n+m;i>=1;i--)add(ans[i],mod-ans[i-1]);
	for(int i=1;i<=n+m;i++)cout<<ans[i]<<" \n"[i==n+m];

	return 0;
}

Details

answer.code: In lambda function:
answer.code:88:26: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   88 |                 for(auto [l,r]:As){
      |                          ^
answer.code:186:44: error: no matching function for call to ‘max(long long int, int)’
  186 |                                 int len=max(0ll,A[i].r-A[i].l-1);
      |                                         ~~~^~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:51,
                 from answer.code:1:
/usr/include/c++/13/bits/stl_algobase.h:257:5: note: candidate: ‘template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)’
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/13/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
answer.code:186:44: note:   deduced conflicting types for parameter ‘const _Tp’ (‘long long int’ and ‘int’)
  186 |                                 int len=max(0ll,A[i].r-A[i].l-1);
      |                                         ~~~^~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/13/bits/stl_algobase.h:303:5: note: candidate: ‘template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)’
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/13/bits/stl_algobase.h:303:5: note:   template argument deduction/substitution failed:
answer.code:186:44: note:   deduced conflicting types for parameter ‘const _Tp’ (‘long long int’ and ‘int’)
  186 |                                 int len=max(0ll,A[i].r-A[i].l-1);
      |                                         ~~~^~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61:
/usr/include/c++/13/bits/stl_algo.h:5795:5: note: candidate: ‘template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)’
 5795 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/13/bits/stl_algo.h:5795:5: note:   template argument deduction/substitution failed:
answer.code:186:44: note:   mismatched types ‘std::initializer_list<_Tp>’ and ‘long long int’
  186 |                                 int len=max(0ll,A[i].r-A[i].l-1);
      |                                         ~~~^~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/13/bits/stl_algo.h:5805:5: note: candidate: ‘template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)’
 5805 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/13/bits/stl_algo.h:5805:5: note:   template argument deduction/substitution failed:
answer.code:186:44: note:   mismatched types ‘std::initializer_list<_Tp>’ and ‘long long int’
  186 |                                 int len=max(0ll,A[i].r-A[i].l-1);
      |                                         ~~~^~~~~~~~~~~~~~~~~~~~~
answer.code:189:60: error: no matching function for call to ‘max(int, long long int)’
  189 |                                         add(sum1,getv_2(max(len-2,0ll),k-1));
      |                                                         ~~~^~~~~~~~~~~
/usr/include/c++/13/bits/stl_algobase.h:257:5: note: candidate: ‘template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)’
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/13/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
answer.code:189:60: note:   deduced conflicting types for parameter ‘const _Tp’ (‘int’ and ‘long long int’)
  189 |                                         add(sum1,getv_2(max(len-2,0ll),k-1));
      |                                                         ~~~^~~~~~~~~~~
/usr/include/c++/13/bits/stl_algobase.h:303:5: note: candidate: ‘template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)’
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/13/bits/stl_algobase.h:303:5: note:   template argument deduction/substitution failed:
answer.code:189:60: note:   deduced conflicting types for parameter ‘const _Tp’ (‘int’ and ‘long long int’)
  189 |                                         add(sum1,getv_2(max(len-2,0ll),k-1));
      |                                                         ~~~^~~~~~~~~~~
/usr/include/c++/13/bits/stl_algo.h:5795:5: note: candidate: ‘template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)’
 5795 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/13/bits/stl_algo.h:5795:5: note:   template argument deduction/substitution failed:
answer.code:189:60: note:   mismatched types ‘std::initializer_list<_Tp>’ and ‘int’
  189 |                                         add(sum1,getv_2(max(len-2,0ll),k-1));
      |                                                         ~~~^~~~~~~~~~~
/usr/include/c++/13/bits/stl_algo.h:5805:5:...