QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#303041#7899. Say Hello to the Futureucup-team266TL 3ms73312kbC++144.8kb2024-01-11 17:07:112024-01-11 17:07:11

Judging History

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

  • [2024-01-24 22:42:06]
  • hack成功,自动添加数据
  • (/hack/523)
  • [2024-01-11 17:07:11]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:73312kb
  • [2024-01-11 17:07:11]
  • 提交

answer

 /*
Things to notice:
1. do not calculate useless values
2. do not use similar names
 
Things to check:
1. submit the correct file
2. time (it is log^2 or log)
3. memory
4. prove your naive thoughts 
5. long long
6. corner case like n=0,1,inf or n=m
7. check if there is a mistake in the ds or other tools you use
8. fileio in some oi-contest

9. module on time 
10. the number of a same divisor in a math problem
11. multi-information and queries for dp and ds problems
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pii pair<long long,long long>
#define mp make_pair
#define pb push_back
const int mod=998244353;
const int inf=0x3f3f3f3f;
const int INF=1e18;
int n,a[200005],f[200005],g[200005],dp[200005];
struct ST
{
	int Lg[200005];
	pii st[20][200005];
	pii merge(pii x,pii y)
	{
		if(y.fi!=x.fi&&y.fi!=x.se)
		{
			if(a[y.fi]>a[x.fi]) x.se=x.fi,x.fi=y.fi;
			else if(a[y.fi]>a[x.se]) x.se=y.fi;
		}
		y.fi=y.se;
		if(y.fi!=x.fi&&y.fi!=x.se)
		{
			if(a[y.fi]>a[x.fi]) x.se=x.fi,x.fi=y.fi;
			else if(a[y.fi]>a[x.se]) x.se=y.fi;
		}
		return x;
	}
	void build()
	{
		Lg[1]=0,Lg[2]=1;
		for(int i=3;i<=n;i++) Lg[i]=Lg[i/2]+1;
		for(int i=1;i<=n;i++) st[0][i]=mp(i,0);
		for(int k=1;k<20;k++) for(int i=1;i+(1<<k)-1<=n;i++) st[k][i]=merge(st[k-1][i],st[k-1][i+(1<<(k-1))]);
	}
	pii query(int l,int r)
	{
		if(l>r) return mp(0LL,0LL);
		int s=Lg[r-l+1];
		return merge(st[s][l],st[s][r-(1<<s)+1]);
	}
}st;
int ans[200005];
struct BIT
{
	int t[1000005];
int lowbit(int x)
{
	return x&(-x);	
} 
void update(int x,int d)
{
	for(int i=x;i<=1000000;i+=lowbit(i)) t[i]+=d,t[i]%=mod;
}
int query(int x)
{
	int res=0;
	for(int i=x;i>=1;i-=lowbit(i)) res+=t[i],res%=mod;
	return res;
}
}bt;
void divide1(int l,int r)
{
	if(l==1&&r==n)
	{
		for(int i=1;i<=n;i++) if(a[st.query(1,i).fi]<=i) dp[i]=(dp[i]+1)%mod;
	}
	if(l==r) return;
	int mid=(l+r)>>1;
	divide1(l,mid);
	vector <array<int,3> > op;
	//{d1,d2,idx}
	for(int i=l;i<=mid;i++) op.pb({2*(i),a[st.query(i+1,mid).fi]+i,i});
	for(int i=mid+1;i<=r;i++) op.pb({2*(i-a[st.query(mid+1,i).fi])+1,i,i});
	sort(op.begin(),op.end());
	for(int i=0;i<op.size();i++)
	{
		int u=op[i][2],d2=op[i][1];
//		cout<<l<<" "<<r<<": "<<u<<" "<<d2<<" "<<dp[u]<<"\n";
		if(u<=mid) bt.update(d2,dp[u]);
		else dp[u]=(dp[u]+bt.query(d2))%mod;
	}
//	for(int i=l;i<=r;i++) cout<<dp[i]<<" ";
//	cout<<"\n"; 
//	system("pause");
	for(int i=0;i<op.size();i++)
	{
		int u=op[i][2],d2=op[i][1];
		if(u<=mid) bt.update(d2,(mod-dp[u])%mod);
	}
	divide1(mid+1,r);
}
void divide2(int l,int r)
{
	if(l==r) return;
	int mid=(l+r)>>1;
	
	vector <array<int,3> > op;
	//{d1,d2,idx}
	for(int i=l;i<=mid;i++) 
	{
		pii tmp=st.query(i,mid);
		op.pb({2*(i-1),a[tmp.fi]+i,-i});
		op.pb({2*(i-1),a[tmp.se]+i,i});
	}
	for(int i=mid+1;i<=r;i++) op.pb({2*(i-a[st.query(mid+1,i).fi])+1,i+1,i});
	sort(op.begin(),op.end()),reverse(op.begin(),op.end());
	for(int i=0;i<op.size();i++)
	{
		int u=op[i][2],d2=op[i][1];
		int flg=1;
		if(u<0) u=-u,flg=mod-1;
		if(u>mid) bt.update(d2,g[u+1]);
		else 
		{
			int idx=st.query(u,mid).fi;
			ans[idx]=(ans[idx]+flg*(bt.query(1000000)-bt.query(d2-1)+mod)%mod*f[u-1])%mod;
		}
	}
	for(int i=0;i<op.size();i++)
	{
		int u=op[i][2],d2=op[i][1];
		if(u>mid) bt.update(d2,(mod-g[u+1])%mod);
	}
	op.clear();
	
	for(int i=l;i<=mid;i++) op.pb({2*(i-1),a[st.query(i,mid).fi]+i,i});
	for(int i=mid+1;i<=r;i++) 
	{
		pii tmp=st.query(mid+1,i);
		op.pb({2*(i-a[tmp.fi])+1,i+1,-i});
		op.pb({2*(i-a[tmp.se])+1,i+1,i});
	}
	sort(op.begin(),op.end());
	for(int i=0;i<op.size();i++)
	{
		int u=op[i][2],d2=op[i][1];
		int flg=1;
//		cout<<u<<" "<<d2<<" "<<f[abs(u)-1]<<"\n";
		if(u<0) u=-u,flg=mod-1;
		if(u<=mid) bt.update(d2,f[u-1]);
		else 
		{
			int idx=st.query(mid+1,u).fi;
			ans[idx]=(ans[idx]+flg*bt.query(d2)%mod*g[u+1])%mod;
		}
	}
	for(int i=0;i<op.size();i++)
	{
		int u=op[i][2],d2=op[i][1];
		if(u<=mid) bt.update(d2,(mod-f[u-1])%mod);
	}
//	cout<<l<<" "<<r<<"\n";
//	for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
//	cout<<"\n";
//	system("pause");
	divide2(l,mid),divide2(mid+1,r);
}
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	st.build(),divide1(1,n);
	f[0]=1;
	for(int i=1;i<=n;i++) f[i]=dp[i];
//	cout<<dp[n]<<"\n";
	memset(dp,0,sizeof(dp));
	reverse(a+1,a+1+n),st.build(),divide1(1,n);
	g[n+1]=1;
	for(int i=1;i<=n;i++) g[i]=dp[n-i+1];
//	for(int i=1;i<=n;i++) cout<<f[i]<<" "<<g[i]<<"\n";
	reverse(a+1,a+1+n),st.build(),divide2(1,n);
	for(int i=1;i<=n;i++) if(a[i]!=1) ans[i]=(ans[i]+f[i-1]*g[i+1])%mod;
	for(int i=1;i<=n;i++) cout<<(f[n]+ans[i])%mod<<" ";
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int _=1;
//	cin>>_;
	while(_--) solve();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 73312kb

input:

5
1 3 2 1 2

output:

3 6 3 3 6 

result:

ok 5 tokens

Test #2:

score: -100
Time Limit Exceeded

input:

200000
15922 15391 11782 4758 1973 19909 16800 6438 3821 986 18599 2011 338 19886 12401 4169 11143 12665 3230 12565 15065 15056 5090 16908 13677 12634 11435 1425 8647 3876 10694 12256 3853 19901 5723 11065 6147 13613 13768 15794 14993 5819 8117 13871 14708 15099 7152 9810 14438 10557 3209 1265 13915...

output:


result: