QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#894693 | #7036. Can You Solve the Harder Problem? | yeweiliang | RE | 0ms | 22356kb | C++14 | 2.0kb | 2025-02-11 19:34:15 | 2025-02-11 19:34:20 |
Judging History
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+1;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+1;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: 100
Accepted
time: 0ms
memory: 22356kb
input:
2 3 1 2 3 3 2 3 3
output:
14 14
result:
ok 2 lines
Test #2:
score: -100
Runtime Error
input:
1000 407 205460 200518 200561 199235 198814 198136 198802 206763 198795 200705 205862 209366 204017 209481 206300 198499 197364 200897 208928 196983 205605 206396 205140 201050 199886 207314 196947 207905 204999 203288 200298 198594 198286 197714 206506 203962 197628 201127 206380 199090 200711 2063...
output:
17379714751 485034874045 1486283529 473795607423 491032978008 305600319363 488390067261 494738672242 484395289938 482595075529 34526611148 225887826961 494818640364 492897785439 487008436187 483315491295 341197520555 493048239146 491323497798 9772278463 492081166573 120835648793 488063414002 4725837...