QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#516269 | #7036. Can You Solve the Harder Problem? | qsc114 | WA | 2128ms | 31224kb | C++11 | 2.0kb | 2024-08-12 15:21:18 | 2024-08-12 15:21:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5+5;
#define ll long long
int n,a[MAXN],sa[MAXN<<1],rk[MAXN<<1],oldrk[MAXN<<1],h[MAXN];
int r[MAXN],stk[MAXN],cnt;
int f[20][MAXN],lg2[MAXN];
ll res[MAXN],sum[MAXN];
void init()
{
for(int i=1;i<=n;i++)f[0][i]=a[i];
for(int i=1;(1<<i)<=n;i++)
for(int j=1;j+(1<<i)-1<=n;j++)
f[i][j]=max(f[i-1][j],f[i-1][j+(1<<i-1)]);
for(int i=2;i<=n;i++)lg2[i]=lg2[i>>1]+1;
}
int getmx(int l,int r)
{
if(l>r)return 0;
int d=lg2[r-l+1];
return max(f[d][l],f[d][r-(1<<d)+1]);
}
int getpos(int l,int r)
{
int L=l,R=r,res=l,mx=getmx(l,r);
while(L<=R)
{
int mid=L+R>>1;
if(getmx(l,mid)<mx)L=mid+1;
else res=mid,R=mid-1;
}
return res;
}
int main()
{
int T;cin>>T;
while(T--)
{
scanf("%d",&n);
cnt=0;
for(int i=1;i<=n;i++)scanf("%d",a+i);a[n+1]=0;
init();
if(n==1)
{
printf("%d\n",a[1]);
continue;
}
for(int i=1;i<=n;i++)sa[i]=i,rk[i]=a[i],rk[i+n]=0;
for(int w=1;w<n;w<<=1)
{
sort(sa+1,sa+1+n,[&](int x,int y){
return rk[x]==rk[y]?rk[x+w]<rk[y+w]:rk[x]<rk[y];
});
memcpy(oldrk,rk,sizeof rk);
for(int i=1,p=0;i<=n;i++)
{
if(oldrk[sa[i]]==oldrk[sa[i-1]]&&
oldrk[sa[i]+w]==oldrk[sa[i-1]+w])rk[i]=p;
else rk[i]=++p;
}
}
int k=0;
for(int i=1;i<=n;i++)
{
if(rk[i]==1)continue;
if(k)--k;
while(a[i+k]==a[sa[rk[i]-1]+k])k++;
h[rk[i]]=k;
}stk[0]=n+1;
// for(int i=1;i<=n;i++)cout<<h[i]<<" ";puts("");
// for(int i=1;i<=n;i++)cout<<rk[i]<<" ";puts("");
for(int i=n;i>=1;i--)
{
while(cnt&&a[stk[cnt]]<=a[i])cnt--;
r[i]=stk[cnt];sum[i]=res[i]=0;
stk[++cnt]=i;
}
for(int i=n;i>=1;i--)
{
sum[i]+=sum[r[i]];
sum[i]+=1ll*(r[i]-i)*a[i];
}
for(int i=n;i>=1;i--)
{
if(h[i]==0)
{
res[i]=sum[i];continue;
}
int pos=getpos(i,i+h[rk[i]]-1);
int mx=getmx(i,i+h[rk[i]]-1);
res[i]+=sum[r[pos]];
res[i]+=1ll*(r[pos]-(i+h[rk[i]]))*mx;
}
ll ans=0;
for(int i=1;i<=n;i++)ans+=res[i];
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: 14076kb
input:
2 3 1 2 3 3 2 3 3
output:
14 14
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 2128ms
memory: 31224kb
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:
17378271377 484987065116 57727152275 485017196590 490992659126 322078561987 490333316680 494724257143 492408726614 491308620355 227333018597 225858542455 494805408497 493463383546 493034932461 492444527203 359054646375 493811547253 493241170649 236920412688 492620331676 131215726639 493299891635 488...
result:
wrong answer 1st lines differ - expected: '17377263880', found: '17378271377'