QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#523#313662#7899. Say Hello to the FuturezhouhuanyizhouhuanyiSuccess!2024-01-24 22:41:532024-01-24 22:41:53

詳細信息

Extra Test:

Time Limit Exceeded

input:

199999
100000 99999 99998 99997 99996 99995 99994 99993 99992 99991 99990 99989 99988 99987 99986 99985 99984 99983 99982 99981 99980 99979 99978 99977 99976 99975 99974 99973 99972 99971 99970 99969 99968 99967 99966 99965 99964 99963 99962 99961 99960 99959 99958 99957 99956 99955 99954 99953 9995...

output:


result:


ID题目提交者结果用时内存语言文件大小提交时间测评时间
#313662#7899. Say Hello to the FuturezhouhuanyiTL 78ms41244kbC++203.3kb2024-01-24 22:00:102024-01-24 22:43:45

answer

#include<iostream>
#include<cstdio>
#define N 300000
#define mod 998244353
using namespace std;
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
void Adder(int &x,int d)
{
	x+=d;
	if (x>=mod) x-=mod;
	return;
}
void Adder2(int &x,int d)
{
	x+=d;
	if (x<0) x+=mod;
	return;
}
int MD(int x)
{
	return x>=mod?x-mod:x;
}
int MD2(int x)
{
	return x<0?x+mod:x;
}
int n,lg[N+1],a[N+1],ls[N+1],rs[N+1],sz[N+1],A[N+1],B[N+1],sw[N+1],sw2[N+1],s[N+1],s2[N+1],dque[N+1],top,ST[N+1][21],ans[N+1];
void dfs(int x)
{
	if (ls[x]) dfs(ls[x]);
	if (rs[x]) dfs(rs[x]);
	sz[x]=sz[ls[x]]+sz[rs[x]]+1;
	return;
}
int query(int l,int r)
{
	if (l>r) return 0;
	int lw=lg[r-l+1];
	return max(ST[l][lw],ST[r-(1<<lw)+1][lw]);
}
void dfs2(int x)
{
	int L,R,sl,sr,d;
	if (ls[x]) dfs2(ls[x]);
	if (rs[x]) dfs2(rs[x]);
	if (sz[ls[x]]<=sz[rs[x]])
	{
		for (int i=x-sz[ls[x]];i<=x;++i)
		{
			L=max(x,i+query(i,x-1)-1),R=min(x+sz[rs[x]],i+a[x]-2);
			if (L<=R)
			{
				if (L==x) Adder(ans[x],1ll*A[i-1]*B[x+1]%mod);
				if (rs[x])
				{
					d=rs[x];
					while (d)
					{
						sl=max(max(L,i+a[d]-1),d),sr=min(R,d+sz[rs[d]]);
						if (sl<=sr) Adder(ans[x],1ll*A[i-1]*MD2(sw2[sr+1]-sw2[sl])%mod);
						d=ls[d];
					}
				}
			}
		}
	}
	else
	{
		for (int i=x;i<=x+sz[rs[x]];++i)
		{
			L=max(x-sz[ls[x]],i-a[x]+2),R=min(x,i-query(x+1,i)+1);
			if (L<=R)
			{
				if (R==x) Adder(ans[x],1ll*A[x-1]*B[i+1]%mod);
				if (ls[x])
				{
					d=ls[x];
					while (d)
					{
						sl=max(L,d-sz[ls[d]]),sr=min(min(R,i-a[d]+1),d);
						if (sl<=sr) Adder(ans[x],1ll*MD2(sw[sr-1]-((sl-1>0)?sw[sl-2]:0))*B[i+1]%mod);
						d=rs[d];
					}
				}
			}
		}
	}
	return;
}
int main()
{
	for (int i=2;i<=N;++i) lg[i]=lg[i>>1]+1;
	n=read();
	for (int i=1;i<=n;++i)
	{
		ST[i][0]=a[i]=read();
		while (top&&a[dque[top]]<a[i]) ls[i]=dque[top],top--;
		if (top) rs[dque[top]]=i;
		dque[++top]=i;
	}
	for (int i=1;i<=lg[n];++i)
		for (int j=1;j+(1<<i)-1<=n;++j)
			ST[j][i]=max(ST[j][i-1],ST[j+(1<<(i-1))][i-1]);
	dfs(dque[1]),A[0]=s2[0]=1;
	for (int i=1;i<=n;++i)
	{
		if (sz[ls[i]]<=sz[rs[i]])
		{
			for (int j=i-sz[ls[i]];j<=i;++j)
				if (max(j+a[i]-1,i)<=i+sz[rs[i]])
					Adder(s[max(j+a[i]-1,i)],A[j-1]),Adder2(s[i+sz[rs[i]]+1],-A[j-1]);
		}
		else
		{
			for (int j=i;j<=i+sz[rs[i]];++j)
				if (i-sz[ls[i]]<=min(j-a[i]+1,i))
					Adder(A[j],MD2(s2[min(j-a[i]+1,i)-1]-(i-sz[ls[i]]-1>0?s2[i-sz[ls[i]]-2]:0)));
		}
		Adder(s[i],s[i-1]),Adder(A[i],s[i]),s2[i]=MD(s2[i-1]+A[i]);
	}
	for (int i=0;i<=n+1;++i) s[i]=s2[i]=0;
	B[n+1]=s2[n+1]=1;
	for (int i=n;i>=1;--i)
	{
		if (sz[ls[i]]<=sz[rs[i]])
		{
			for (int j=i-sz[ls[i]];j<=i;++j)
				if (max(j+a[i]-1,i)<=i+sz[rs[i]])
					Adder(B[j],MD2(s2[max(j+a[i]-1,i)+1]-s2[i+sz[rs[i]]+2]));
		}
		else
		{
			for (int j=i;j<=i+sz[rs[i]];++j)
				if (i-sz[ls[i]]<=min(j-a[i]+1,i))
					Adder(s[min(j-a[i]+1,i)],B[j+1]),Adder2(s[i-sz[ls[i]]-1],-B[j+1]);
		}
		Adder(s[i],s[i+1]),Adder(B[i],s[i]),s2[i]=MD(s2[i+1]+B[i]);
	}
	sw[0]=A[0],sw2[0]=B[0];
	for (int i=1;i<=n+1;++i) sw[i]=MD(sw[i-1]+A[i]),sw2[i]=MD(sw2[i-1]+B[i]);
	for (int i=1;i<=n;++i) ans[i]=A[n];
	dfs2(dque[1]);
	for (int i=1;i<=n;++i) printf("%d ",ans[i]);
	puts("");
	return 0;
}