QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#227342#7616. Jump Graphucup-team1004WA 4ms30360kbC++142.6kb2023-10-27 12:38:322023-10-27 12:38:33

Judging History

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

  • [2023-10-27 12:38:33]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:30360kb
  • [2023-10-27 12:38:32]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T> &x){
	if(x.empty())return out<<"[]";
	out<<'['<<x[0];
	for(int i=1,len=x.size();i<len;i++)out<<','<<x[i];
	return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
	cerr<<x<<endl;
}
template<typename T,typename ... S>
void debug(T x,S...y){
	cerr<<x<<' ',debug(y...);
}
const int N=3e5+10;
int n,a[N],ls[N],rs[N];
vector<int>L[N],R[N];
int top,stk[N],dep[N],dis[N],siz[N];
ll sum[N];
void make(int u){
	if(ls[u]){
		dep[ls[u]]=dep[u]+1;
		make(ls[u]);
		for(int v=rs[ls[u]];v;v=rs[v])L[u].push_back(v);
	}
	if(rs[u]){
		dep[rs[u]]=dep[u]+1;
		make(rs[u]);
		for(int v=ls[rs[u]];v;v=ls[v])R[u].push_back(v);
	}
}
void init(int u){
	siz[u]=1,sum[u]=dis[u];
	for(int v:L[u]){
		dis[v]=dis[u]+1;
		init(v);
		siz[u]+=siz[v],sum[u]+=sum[v];
	}
	for(int v:R[u]){
		dis[v]=dis[u]+1;
		init(v);
		siz[u]+=siz[v],sum[u]+=sum[v];
	}
}
ll ans[N],one[N],pre[N],suf[N],cur[N];
void dfs(int u){
	for(int v:L[u])dfs(v);
	for(int v:R[u])dfs(v);
	auto solve=[&](const vector<int>&L,const vector<int>&R){
		ll res=0;
		for(int v:L)res+=sum[v]-1ll*dis[u]*siz[v]+siz[v];
		for(int v:R)ans[v]+=res;
		int len=R.size();
		for(int i=0,v;i<len;i++){
			v=R[i];
			if(i)pre[i]=pre[i-1];
			pre[i]+=sum[v]-1ll*dis[v]*siz[v]+(siz[v]-1);
		}
		suf[len]=0;
		for(int i=len-1,v;i>=0;i--){
			suf[i]=suf[i+1];
			if(i+1<len){
				v=R[i+1];
				suf[i]+=sum[v]-1ll*dis[u]*siz[v]+siz[v];
			}
		}
		for(int i=0,v;i<len;i++){
			v=R[i];
			cur[i]=sum[v]-1ll*dis[v]*siz[v]+siz[v]+siz[v];
		}
		for(int i=0,v;i<len;i++){
			v=R[i];
			if(i)ans[v]+=pre[i-1];
			ans[v]+=suf[i+1];
			if(i+1<len){
				ans[v]+=cur[i+1],one[v]-=siz[R[i+1]];
			}
		}
	};
	solve(L[u],R[u]);
	solve(R[u],L[u]);
}
void push(int u){
	for(int v:L[u]){
		ans[v]+=ans[u],push(v);
	}
	for(int v:R[u]){
		ans[v]+=ans[u],push(v);
	}
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++){
		for(;top&&a[stk[top]]<a[i];top--)ls[i]=stk[top];
		rs[stk[top]]=i,stk[++top]=i;
	}
	make(rs[0]);
	for(int u=rs[0];ls[u];u=ls[u])L[u].push_back(ls[u]);
	for(int u=rs[0];rs[u];u=rs[u])R[u].push_back(rs[u]);
	debug(ary(L,1,n),ary(R,1,n));
	init(rs[0]),dfs(rs[0]);
	push(rs[0]);
	for(int i=1;i<=n;i++){
		ans[i]+=one[i];
		printf("%lld%c",ans[i]+dis[i]-dep[i]+dis[i]+sum[i]-1ll*siz[i]*dis[i],"\n "[i<n]);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 30360kb

input:

6
1 6 3 2 5 4

output:

11 7 7 5 5 6

result:

wrong answer 1st lines differ - expected: '11 7 7 7 6 8', found: '11 7 7 5 5 6'