QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#449950#7899. Say Hello to the FutureHarry27182RE 0ms0kbC++143.0kb2024-06-21 20:30:372024-06-21 20:30:37

Judging History

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

  • [2024-06-21 20:30:37]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-06-21 20:30:37]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int n,f[200005],g[200005],dp[200005],a[200005],ans[200005];
void Add(int &x,int y){x=(x+y>=mod?x+y-mod:x+y);}
struct node{int w1,w2,pos;};
bool cmp(node x,node y){return x.w1<y.w1;}
struct BIT
{
	int tr[200005];
	void change(int x,int v){for(int i=x;i<=n;i+=i&(-i))Add(tr[i],v);return;}
	int query(int x){int res=0;for(int i=x;i;i-=i&(-i))Add(res,tr[i]);return res;}
}bit;
void cdq1(int l,int r)
{
	if(l==1&&r==n)
	{
		int mx=0;
		for(int i=1;i<=n;i++)
		{
			mx=max(mx,a[i]);
			Add(dp[i],(mx<=i));
		}
	}
	if(l==r)return;
	int mid=(l+r)>>1;
	cdq1(l,mid);
	int mx=0;vector<node>v;
	for(int i=mid;i>=l;i--)
	{
		v.emplace_back(node{i+mx,i,i});
		mx=max(mx,a[i]);
	}
	sort(v.begin(),v.end(),cmp);
	mx=0;int p=0;
	for(int i=mid+1;i<=r;i++)
	{
		mx=max(mx,a[i]);
		while(p<v.size()&&v[p].w1<=i)bit.change(v[p].w2,dp[v[p].pos]),p++;
		Add(dp[i],bit.query(max(0,i-mx)));
	}
	for(int i=0;i<p;i++)bit.change(v[i].w2,mod-dp[v[i].pos]);
	cdq1(mid+1,r);
}
void cdq2(int l,int r)
{
	if(l==r)return;
	int mid=(l+r)>>1;
	cdq2(l,mid);cdq2(mid+1,r);
	int mx=0;vector<node>v;
	for(int i=mid+1;i<=r;i++)
	{
		mx=max(mx,a[i]);
		v.emplace_back(node{i-mx,i,i});
	}
	sort(v.begin(),v.end(),cmp);reverse(v.begin(),v.end());
	int mx2=0,pos=0,p=0;mx=0;
	for(int i=mid;i>=l;i--)
	{
		while(p<v.size()&&v[p].w1>=i)bit.change(v[p].w2,g[v[p].pos]),p++;
		if(pos)Add(ans[pos],1ll*f[i]*bit.query(min(n,i+mx-1))%mod),Add(ans[pos],mod-1ll*f[i]*bit.query(min(n,i+mx2-1))%mod);
		if(a[i]>mx)mx2=mx,mx=a[i],pos=i;
		else if(a[i]>mx2)mx2=a[i];
	}
	for(int i=0;i<p;i++)bit.change(v[i].w2,mod-g[v[i].pos]);
	v.clear();mx=0;
	for(int i=mid;i>=l;i--)
	{
		v.emplace_back(node{i+mx,i,i});
		mx=max(mx,a[i]);
	}
	sort(v.begin(),v.end(),cmp);
	mx2=0;pos=0;mx=0;p=0;
	for(int i=mid+1;i<=r;i++)
	{
		if(a[i]>mx)mx2=mx,mx=a[i],pos=i;
		else if(a[i]>mx2)mx2=a[i];
		while(p<v.size()&&v[p].w1<=i)bit.change(v[p].w2,f[v[p].pos]),p++;
		if(pos)Add(ans[pos],1ll*g[i]*bit.query(max(0,i-mx2))%mod),Add(ans[pos],mod-1ll*g[i]*bit.query(max(0,i-mx))%mod);
	}
	for(int i=0;i<p;i++)bit.change(v[i].w2,mod-f[v[i].pos]);
}
int main()
{
	freopen("seq.in","r",stdin);
	freopen("seq.out","w",stdout);
	cin.tie(0)->sync_with_stdio(0);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	f[0]=1;cdq1(1,n);
	for(int i=1;i<=n;i++)f[i]=dp[i];
	reverse(a+1,a+n+1);
	for(int i=1;i<=n;i++)dp[i]=0;
	g[n+1]=1;cdq1(1,n);
	for(int i=1;i<=n;i++)g[i]=dp[n-i+1];
	reverse(a+1,a+n+1);
	for(int i=1;i<=n;i++)Add(ans[i],f[n]);
	//for(int i=1;i<=n;i++)cout<<f[i]<<' '<<g[i]<<'\n';
	cdq2(1,n);
	int mx=0,pos=0,mx2=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>mx)mx2=mx,mx=a[i],pos=i;
		else if(a[i]>mx2)mx2=a[i];
		if(pos&&mx>i&&mx2<=i)Add(ans[pos],g[i+1]);
	}
	mx=0;pos=0;mx2=0;
	for(int i=n;i>=1;i--)
	{
		if(a[i]>mx)mx2=mx,mx=a[i],pos=i;
		else if(a[i]>mx2)mx2=a[i];
		if(pos&&mx>n-i+1&&mx2<=n-i+1)Add(ans[pos],f[i-1]);
	}
	for(int i=1;i<=n;i++)cout<<ans[i]<<' ';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Dangerous Syscalls

input:

5
1 3 2 1 2

output:


result: