QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#412970#7899. Say Hello to the FutureJohnAlfnovTL 0ms14264kbC++204.2kb2024-05-16 22:41:302024-05-16 22:41:30

Judging History

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

  • [2024-05-16 22:41:30]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:14264kb
  • [2024-05-16 22:41:30]
  • 提交

answer

#include<bits/stdc++.h>
#define mod 998244353
using namespace std;
int n,a[200005];
int f1[200005],f2[200005];
struct apple{
	int x,y,z;
	apple(int x=0,int y=0,int z=0):x(x),y(y),z(z){}
	bool operator<(const apple &other)const{
		if(x==other.x)return z>other.z;
		return x<other.x;
	}
}e[200005];
long long c[200005];
void add(int x,int s){
	while(x<=n){
		c[x]+=s;
		x+=x&-x;
	}
}
int query(int x){
	long long ans=0;
	while(x){
		ans+=c[x];
		x-=x&-x;
	}
	return ans%mod;
}
void solve1(int l,int r){
	if(l==r)return;
	int mid=(l+r)>>1;
	solve1(l,mid);
	int tt=0;
	int m1=0;
	for(int i=mid;i>=l;--i){
		int l1=mid-i;
		e[++tt]=apple(l1,max(m1-l1,1),i);
		m1=max(m1,a[i]);
	}
	int mm=0;
	for(int i=mid+1;i<=r;++i){
		mm=max(mm,a[i]);
		int l2=i-mid;
		e[++tt]=apple(max(mm-l2,0),l2,i);
	}
	sort(e+1,e+tt+1);
	for(int i=tt;i>=1;--i){
		if(e[i].z<=mid){
			add(e[i].y,f1[e[i].z]);
		}else{
			f1[e[i].z]=(f1[e[i].z]+query(e[i].y))%mod;
		}
	}
	for(int i=tt;i>=1;--i){
		if(e[i].z<=mid){
			add(e[i].y,mod-f1[e[i].z]);
		}
	}
	solve1(mid+1,r);
}
int ans[200005];
void ADD(int l,int r,int z){
	ans[l]=(ans[l]+z)%mod;
	ans[r+1]=(ans[r+1]+mod-z)%mod;
}
void solve2(int l,int r){
	if(l==r)return;
	int mid=(l+r)>>1;
	solve2(mid+1,r);
	int tt=0;
	int m1=0;
	for(int i=mid+1;i<=r;++i){
		int l1=i-mid-1;
		e[++tt]=apple(l1,max(m1-l1,1),-i);
		m1=max(m1,a[i]);
	}
	int mm=0;
	for(int i=mid;i>=l;--i){
		mm=max(mm,a[i]);
		int l2=mid-i+1;
		e[++tt]=apple(max(mm-l2,0),l2,-i);
	}
	sort(e+1,e+tt+1);
	for(int i=tt;i>=1;--i){
		if(-e[i].z>mid){
			add(e[i].y,f2[-e[i].z]);
		}else{
			f2[-e[i].z]=(f2[-e[i].z]+query(e[i].y))%mod;
		}
	}
	for(int i=tt;i>=1;--i){
		if(-e[i].z>mid){
			add(e[i].y,mod-f2[-e[i].z]);
		}
	}
	solve2(l,mid);
}
struct app2{
	apple a;
	int xs,wz;
	app2(apple a=0,int xs=0,int wz=0):a(a),xs(xs),wz(wz){}
	bool operator<(app2 &other)const{
		return a<other.a;
	}
}ee[200005];
void jiaaa(int &n,apple a,int xs,int wz){
	ee[++n]=app2(a,xs,wz);
}
void sol(int n){
	sort(ee+1,ee+n+1);
	for(int i=n;i>=1;--i){
		if(!ee[i].wz){
			add(ee[i].a.y,ee[i].xs);
		}else{
			ADD(ee[i].wz,ee[i].wz,1ll*ee[i].xs*query(ee[i].a.y)%mod);
		}
	}
	for(int i=n;i>=1;--i){
		if(!ee[i].wz){
			add(ee[i].a.y,mod-ee[i].xs);
		}
	}
}
void solve(int l,int r){
	if(l==r)return;
	int mid=(l+r)>>1;
	solve(l,mid);solve(mid+1,r);
	int tt=0;
	int m1=0;
	int t1=0,t2=0;
	for(int i=mid;i>=l;--i){
		int l1=mid-i;
		e[++tt]=apple(l1,max(m1-l1,1),i);
		ee[++t1]=app2(e[tt],f1[e[tt].z],0);
		m1=max(m1,a[i]);
	}
	int mm=0;
	int zd=1,cd=1,wz=mid+1;
	for(int i=mid+1;i<=r;++i){
		if(zd<a[i])cd=zd,zd=a[i],wz=i;
		else if(cd<a[i])cd=a[i];
		int l2=i-mid;
		apple a1(zd-l2,l2,i),a2(cd-l2,l2,i);
		jiaaa(t1,a1,mod-f2[i+1],wz);
		jiaaa(t1,a2,f2[i+1],wz);
	}
	sol(t1);
	for(int i=mid+1;i<=r;++i){
		mm=max(mm,a[i]);
		int l2=i-mid;
		e[++tt]=apple(max(mm-l2,0),l2,i);
	}
	sort(e+1,e+tt+1);
	for(int i=tt;i>=1;--i){
		if(e[i].z<=mid){
			add(e[i].y,f1[e[i].z]);
		}else{
			ADD(mid+1,e[i].z,1ll*query(e[i].y)*f2[e[i].z+1]%mod);
		}
	}
	for(int i=tt;i>=1;--i){
		if(e[i].z<=mid){
			add(e[i].y,mod-f1[e[i].z]);
		}
	}
	tt=0;m1=0;mm=0;
	for(int i=mid+1;i<=r;++i){
		mm=max(mm,a[i]);
		int l2=i-mid;
		e[++tt]=apple(l2,max(mm-l2,0)+1,-i);
		ee[++t2]=app2(e[tt],f2[-e[tt].z+1],0);
	}
	zd=1,cd=1,wz=mid;
	for(int i=mid;i>=l;--i){
		if(i<mid){
			int l1=mid-i;
			apple a1(zd-l1,l1+1,i),a2(cd-l1,l1+1,i);
			jiaaa(t2,a1,mod-f1[i],wz);
			jiaaa(t2,a2,f1[i],wz);
		}
		if(zd<a[i])cd=zd,zd=a[i],wz=i;
		else if(cd<a[i])cd=a[i];
	}
	sol(t2);
	for(int i=mid;i>=l;--i){
		int l1=mid-i;
		e[++tt]=apple(max(m1-l1,1),l1+1,-i);
		m1=max(m1,a[i]);
	}
	sort(e+1,e+tt+1);
	for(int i=tt;i>=1;--i){
		if(-e[i].z>mid){
			add(e[i].y,f2[-e[i].z+1]);
		}else{
			ADD(-e[i].z+1,mid,1ll*query(e[i].y)*f1[-e[i].z]%mod);
		}
	}
	for(int i=tt;i>=1;--i){
		if(-e[i].z>mid){
			add(e[i].y,mod-f2[-e[i].z+1]);
		}
	}
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i)scanf("%d",&a[i]);
	f1[0]=1;solve1(0,n);
	f2[n+1]=1;solve2(1,n+1);
	solve(0,n);
	for(int i=1;i<=n;++i)ans[i]=(ans[i]+ans[i-1])%mod;
	for(int i=1;i<=n;++i)printf("%d ",ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 14264kb

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: