QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#457104 | #2070. Heavy Stones | Wuyanru | WA | 6ms | 26984kb | C++14 | 3.6kb | 2024-06-29 08:01:25 | 2024-06-29 08:01:27 |
Judging History
answer
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
using ll=long long;
using ld=long double;
using pli=pair<ll,int>;
using pi=pair<int,int>;
template<typename A>
using vc=vector<A>;
inline int read()
{
int s=0,w=1;char ch;
while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
mt19937 _rand(time(0)^clock());
int len[400001],slen[400001],siz[400001],pri[400001];
ll s1[400001],s2[400001],v1[400001],v2[400001];
ll pre[200001],prel[200001],prer[200001];
int sta[200001],top;int iid[200001];
int ls[400001],rs[400001];
vc<pi>op[200005];
int a[200001];
int n,c;
inline void init(int id)
{
ls[id]=rs[id]=0,siz[id]=1,pri[id]=_rand();
slen[id]=len[id],s1[id]=v1[id],s2[id]=v2[id];
}
inline ll gl(int l,int r){ return prel[r]-prel[l-1]-(r-l+1)*pre[l-1];}
inline ll gr(int l,int r){ return prer[r]-prer[l-1]-(l-1)*(pre[r]-pre[l-1]);}
inline void push(int c,int l,int r,bool left=1){ len[c]=r-l+1,v1[c]=pre[r]-pre[l-1],v2[c]=left?gr(l,r):gl(l,r);init(c);}
inline bool r_big(int l,int mid,int r){ return (pre[mid]-pre[l-1])*(r-mid)<(pre[r]-pre[mid])*(mid-l+1);}
inline void wcnm()
{
for(int i=1;i<=n;i++)
{
pre[i]=pre[i-1]+a[i];
prel[i]=prel[i-1]+pre[i-1]+a[i];
prer[i]=prer[i-1]+(ll)i*a[i];
}
top=1,sta[1]=0;
for(int i=1;i<n;i++)
{
while(top>1&&r_big(sta[top-1]+1,sta[top],i)) op[i+1].push_back(pi(-1,iid[top--]));
iid[++top]=++c,sta[top]=i,push(c,sta[top-1]+1,i),op[i+1].push_back(pi(1,c));
}
top=1,sta[1]=n+1;
for(int i=n;i>1;i--)
{
while(top>1&&!r_big(i,sta[top]-1,sta[top-1]-1)) op[i].push_back(pi(1,iid[top--]));
iid[++top]=++c,sta[top]=i,push(c,i,sta[top-1]-1,false),op[i].push_back(pi(-1,c));
}
while(top>1) op[1].push_back(pi(1,iid[top--]));
}
inline bool cmp(int id1,int id2)//id1<id2?
{
ll num1=v1[id1]*len[id2],num2=v1[id2]*len[id1];
return num1==num2?id1<=id2:num1<num2;
}
inline void push_up(int p)
{
slen[p]=len[p],s1[p]=v1[p],s2[p]=v2[p];siz[p]=1;
if(rs[p]) s2[p]+=s2[rs[p]]+slen[rs[p]]*s1[p],slen[p]+=slen[rs[p]],s1[p]+=s1[rs[p]],siz[p]+=siz[rs[p]];
if(ls[p]) s2[p]+=s2[ls[p]]+s1[ls[p]]*slen[p],slen[p]+=slen[ls[p]],s1[p]+=s1[ls[p]],siz[p]+=siz[ls[p]];
}
void split_s(int p,int s,int &x,int &y)
{
if(!p){ x=y=0;return ;}
if(siz[ls[p]]+1<=s)
{
x=p;split_s(rs[p],s-siz[ls[p]]-1,rs[x],y);
push_up(x);
}
else
{
y=p;split_s(ls[p],s,x,ls[y]);
push_up(y);
}
}
void split_id(int p,int id,int &x,int &y)
{
if(!p){ x=y=0;return ;}
if(cmp(id,p))
{
y=p;split_id(ls[p],id,x,ls[y]);
push_up(y);
}
else
{
x=p;split_id(rs[p],id,rs[x],y);
push_up(x);
}
}
int merge(int u,int v)
{
if(!u||!v) return u|v;
if(pri[u]>pri[v])
{
rs[u]=merge(rs[u],v);
push_up(u);
return u;
}
else
{
ls[v]=merge(u,ls[v]);
push_up(v);
return v;
}
}
void output(int p)
{
if(ls[p]) output(ls[p]);
printf("%d : %d %d %d : %d %lld %lld %d %lld %lld\n",p,ls[p],rs[p],siz[p],len[p],v1[p],v2[p],slen[p],s1[p],s2[p]);
if(rs[p]) output(rs[p]);
}
int main()
{
n=read();
for(int i=1;i<=n;i++) a[i]=read();
wcnm();
int rt=0;
for(int i=1;i<=n;i++)
{
// printf("i=%d\n",i);
for(auto j:op[i])
{
// printf("%d %d (%d,%lld,%lld)\n",j.first,j.second,len[j.second],v1[j.second],v2[j.second]);
if(j.first==1)
{
int x,y;
split_id(rt,j.second,x,y);
rt=merge(x,merge(j.second,y));
}
else
{
int x,y,z;
split_id(rt,j.second,x,y);
split_s(y,1,y,z);
rt=merge(x,z);
}
}
printf("%lld ",s2[rt]-s1[rt]+(ll)(n-1)*a[i]);
// output(rt);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 6ms
memory: 26984kb
input:
5 2 1 3 5 4
output:
22 21 24 33 38
result:
wrong answer 1st lines differ - expected: '35 35 36 43 49', found: '22 21 24 33 38 '