QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#516268 | #7036. Can You Solve the Harder Problem? | qsc114 | WA | 2331ms | 30664kb | C++11 | 2.0kb | 2024-08-12 15:20:17 | 2024-08-12 15:20:17 |
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("%d\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14056kb
input:
2 3 1 2 3 3 2 3 3
output:
14 14
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 2331ms
memory: 30664kb
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:
198402193 -344239332 1892577427 -314107858 1366387382 -43985213 707044936 803018103 -1512512426 1682348611 -300248091 -1774724233 884169457 -457855494 -886306579 -1476711837 -1722606489 -109691787 -680068391 697211408 -1300907364 -1928259537 -621347405 -1619298281 1718304294 -1092980557 -280972812 -...
result:
wrong answer 1st lines differ - expected: '17377263880', found: '198402193'