QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#894689#7036. Can You Solve the Harder Problem?yeweiliangWA 0ms22360kbC++142.0kb2025-02-11 19:33:102025-02-11 19:33:18

Judging History

This is the latest submission verdict.

  • [2025-02-11 19:33:18]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 22360kb
  • [2025-02-11 19:33:10]
  • Submitted

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=4e5+5;
int t,n,k,top,cnt,mid,ans,tot,s[N],a[N],res[N],tong[N],sa[N],rk[N],hlp[N],height[N],f[N][25],Log[N],liser[N];
void clear(){
	for(int i=1;i<=n;i++) a[i]=sa[i]=rk[i]=res[i]=0;
}
void my_sort(int w){
	for(int i=1;i<=n;i++) tong[rk[sa[i]+w]]++;
	for(int i=1;i<=n;i++) tong[i]+=tong[i-1];
	for(int i=n;i>=1;i--) hlp[tong[rk[sa[i]+w]]--]=sa[i];
	for(int i=1;i<=n;i++) sa[i]=hlp[i];
	for(int i=1;i<=n;i++) tong[i]=0;
}
void init(){
	tot=0;
	liser[++tot]=0;
	for(int i=1;i<=n;i++) liser[++tot]=a[i];
	sort(liser+1,liser+tot+1);
	tot=unique(liser+1,liser+tot+1)-liser-1;
	s[0]=n+1;
	res[n+1]=top=0;
	for(int i=n;i>=1;i--){
		while(top&&a[s[top]]<=a[i]) top--;
		res[i]=res[s[top]]+(s[top]-i)*a[i];
		s[++top]=i;
	}
	for(int i=1;i<=n;i++){
		sa[i]=i;
		rk[i]=lower_bound(liser+1,liser+tot+1,a[i])-liser;
	}
	for(int w=1;w<n;w<<=1){
		my_sort(w);
		my_sort(0);
		for(int i=1;i<=n;i++) hlp[i]=rk[i];
		cnt=0;
		for(int i=1;i<=n;i++){
			if(hlp[sa[i]]==hlp[sa[i-1]]&&hlp[sa[i]+w]==hlp[sa[i-1]+w]) rk[sa[i]]=cnt;
			else rk[sa[i]]=++cnt;
		}
	}
	for(int i=1,len=0;i<=n;i++){
		if(rk[i]==1) continue;
		if(len) len--;
		while(a[i+len]==a[sa[rk[i]-1]+len]) len++;
		height[rk[i]]=len;
	}
	for(int i=2;i<=n;i++) Log[i]=Log[i>>1]+1;
	for(int i=1;i<=n;i++) f[i][0]=a[i];
	for(int j=1;j<=Log[n];j++) for(int i=1;i<=n-(1<<j)+1;i++) f[i][j]=max(f[i][j-1],f[i+(1<<j-1)][j-1]);
}
int query(int l,int r){
	int k=Log[r-l+1];
	return max(f[l][k],f[r-(1<<k)+1][k]);
}
int find(int l,int r,int x){
	int pl=l;
	while(l<=r){
		mid=l+r>>1;
		if(query(pl,mid)<=x) l=mid+1;
		else r=mid-1;
	}
	return r;
}
signed main(){
	scanf("%lld",&t);
	while(t--){
		clear();
		scanf("%lld",&n);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
		init();
		ans=0;
		for(int i=1;i<=n;i++){
			k=find(sa[i]+height[i],n,query(sa[i],sa[i]+height[i]-1));
			ans+=(k-sa[i]-height[i]+1)*query(sa[i],sa[i]+height[i]-1)+res[k+1];
		}
		printf("%lld\n",ans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 22360kb

input:

2
3
1 2 3
3
2 3 3

output:

11
14

result:

wrong answer 1st lines differ - expected: '14', found: '11'