QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#416871 | #2070. Heavy Stones | schrodingerstom | WA | 360ms | 61520kb | C++14 | 6.1kb | 2024-05-22 10:17:52 | 2024-05-22 10:17:52 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
#define debug(fmt,...) \
fprintf(stderr, fmt,##__VA_ARGS__)
#define debugln(fmt,...) \
fprintf(stderr,"[%d] " fmt,__LINE__,##__VA_ARGS__)
#define TIME (1e3*clock()/CLOCKS_PER_SEC)
bool memBeg;
template<typename T,typename TT> void chkmin(T &x,TT y) {if(x>y) x=y;}
template<typename T,typename TT> void chkmax(T &x,TT y) {if(x<y) x=y;}
constexpr int mod=998244353;
void inc(int &x,int y) {x+=y; x-=(x>=mod)*mod;}
void dec(int &x,int y) {x-=y; x+=(x<0)*mod;}
constexpr int add(int x,int y) {return (x+y>=mod)?x+y-mod:x+y;}
constexpr int sub(int x,int y) {return (x<y)?x-y+mod:x-y;}
constexpr int quick_pow(int x,ll times,int ret=1) {
for(;times;times>>=1,x=1ll*x*x%mod) if(times&1) ret=1ll*ret*x%mod;
return ret;
}
constexpr int maxn=2e5+5;
int n,a[maxn],stk[maxn],pstk[maxn],top;
ll pre[maxn],suf[maxn];
bool operator<(const pli &lhs,const pli &rhs) {
return lhs.first*rhs.second<rhs.first*lhs.second;
}
bool operator>(const pli &lhs,const pli &rhs) {
return lhs.first*rhs.second>rhs.first*lhs.second;
}
vector<tuple<int,int,int>> cand;
struct node {
ll ret,sum;
int len;
node(): ret(0),sum(0),len(0) {}
node(ll _ret,ll _sum,int _len): ret(_ret),sum(_sum),len(_len) {}
node operator+(const node &o) const {
return node(ret+sum*o.len+o.ret,sum+o.sum,len+o.len);
};
};
struct segment_tree {
node tree[maxn<<3];
void push_up(int cur) {
tree[cur]=tree[cur<<1]+tree[cur<<1|1];
}
void modify(int tl,int tr,int cur,int pos,const node &vl) {
if(tl==tr) return tree[cur]=vl,void();
int mid=(tl+tr)>>1;
if(pos<=mid) modify(tl,mid,cur<<1,pos,vl);
else modify(mid+1,tr,cur<<1|1,pos,vl);
push_up(cur);
}
node query(int tl,int tr,int cur,int l,int r) {
if(l<=tl&&tr<=r) return tree[cur];
int mid=(tl+tr)>>1;
if(l>mid) return query(mid+1,tr,cur<<1|1,l,r);
else if(r<=mid) return query(tl,mid,cur<<1,l,r);
else return query(tl,mid,cur<<1,l,r)+
query(mid+1,tr,cur<<1|1,l,r);
}
}T;
auto eval(const tuple<int,int,int> &o) {
return make_pair(pre[get<2>(o)]-pre[get<1>(o)-1],get<2>(o)-get<1>(o)+1);
}
bool Less1(const tuple<int,int,int> &lhs,const tuple<int,int,int> &rhs) {
auto u=eval(lhs),v=eval(rhs);
if(u<v) return true;
if(u>v) return false;
return get<0>(lhs)<get<0>(rhs);
}
ll spre[maxn];
vector<int> pop[maxn];
ll calc1(int l,int r) {
ll sum=spre[r]-spre[l-1]-(pre[r]-pre[l-1])*(l-1);
sum=(pre[r]-pre[l-1])*(r-l+2)-sum;
// debug("calc1(l = %d, r = %d) = %lld\n",l,r,sum);
return sum;
}
ll calc0(int l,int r) {
return spre[r]-spre[l-1]-(pre[r]-pre[l-1])*(l-1);
}
void insert(const tuple<int,int,int> &o) {
// debug("insert(%d, %d, %d)\n",get<0>(o),get<1>(o),get<2>(o));
int pos=lower_bound(cand.begin(),cand.end(),o,Less1)-cand.begin();
ll sum=get<0>(o)?calc1(get<1>(o),get<2>(o)):calc0(get<1>(o),get<2>(o));
T.modify(0,(int)cand.size()-1,1,pos,node(sum,
pre[get<2>(o)]-pre[get<1>(o)-1],
get<2>(o)-get<1>(o)+1));
}
void erase(const tuple<int,int,int> &o) {
// debug("erase(%d, %d, %d)\n",get<0>(o),get<1>(o),get<2>(o));
int pos=lower_bound(cand.begin(),cand.end(),o,Less1)-cand.begin();
T.modify(0,(int)cand.size()-1,1,pos,node());
}
ll calc() {
return T.query(0,(int)cand.size()-1,1,0,(int)cand.size()-1).ret;
}
bool memEn;
void fl() {
freopen(".in","r",stdin);
freopen(".out","w",stdout);
}
int main() {
debug("%.24lf\n",fabs(&memEn-&memBeg)/1024.0/1024.0);
// fl();
#ifdef LOCAL
freopen("debug","w",stderr);
#endif
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) pre[i]=pre[i-1]+a[i];
for(int i=n;i>=1;i--) suf[i]=suf[i+1]+a[i];
for(int i=1;i<n;i++) {
while(top&&make_pair(a[i],1)>
make_pair(pre[stk[top]]-pre[stk[top-1]],stk[top]-stk[top-1])) {
top--;
}
stk[++top]=i;
cand.emplace_back(0,stk[top-1]+1,stk[top]);
}
top=0; stk[0]=n+1;
for(int i=n;i>1;i--) {
while(top&&make_pair(a[i],1)>
make_pair(suf[stk[top]]-suf[stk[top-1]],stk[top-1]-stk[top])) {
pop[i].emplace_back(stk[top]);
top--;
}
stk[++top]=i;
cand.emplace_back(1,stk[top],stk[top-1]-1);
}
sort(cand.begin(),cand.end(),Less1);
for(int i=1;i<=n;i++) spre[i]=spre[i-1]+1ll*i*a[i];
for(int i=1;i<=top;i++) insert(make_tuple(1,stk[i],stk[i-1]-1));
// for(int i=1;i<=top;i++) debug("%d ",stk[i]); debug("\n");
// for(auto t:cand) {
// debug("(%d, %d, %d) ",get<0>(t),get<1>(t),get<2>(t));
// }
// debug("\n");
printf("%lld ",calc()+1ll*a[1]*(n-1));
int ptop=0;
for(int i=1;i<n;i++) {
// debug("--------i = %d--------\n",i);
while(ptop&&make_pair(a[i],1)>
make_pair(pre[pstk[ptop]]-pre[pstk[ptop-1]],pstk[ptop]-pstk[ptop-1])) {
erase(make_tuple(0,pstk[ptop-1]+1,pstk[ptop]));
ptop--;
}
pstk[++ptop]=i;
insert(make_tuple(0,pstk[ptop-1]+1,pstk[ptop]));
erase(make_tuple(1,stk[top],stk[top-1]-1));
top--;
while(!pop[i+1].empty()) {
stk[++top]=pop[i+1].back();
pop[i+1].pop_back();
insert(make_tuple(1,stk[top],stk[top-1]-1));
}
// debug("stk: "); for(int j=1;j<=top;j++) debug("%d ",stk[j]); debug("\n");
// debug("pstk: "); for(int j=1;j<=ptop;j++) debug("%d ",pstk[j]); debug("\n");
// for(int j=0;j<(int)cand.size();j++) {
// debug("%lld ",T.query(0,(int)cand.size()-1,1,j,j).ret);
// }
// debug("\n");
printf("%lld ",calc()+1ll*a[i+1]*(n-1));
}
puts("");
return 0;
}
/*
g++ template.cpp -o template -std=c++14 -Wall -Wextra -Wshadow -fsanitize=address,undefined
*/
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 49152kb
input:
5 2 1 3 5 4
output:
35 35 36 43 49
result:
ok single line: '35 35 36 43 49 '
Test #2:
score: 0
Accepted
time: 3ms
memory: 50544kb
input:
10 18 37 81 6 58 99 87 34 75 9
output:
2637 2637 2657 2657 2695 2949 2995 2905 2880 2880
result:
ok single line: '2637 2637 2657 2657 2695 2949 2995 2905 2880 2880 '
Test #3:
score: -100
Wrong Answer
time: 360ms
memory: 61520kb
input:
200000 479735 430060 863649 878892 889364 241972 927862 32858 714406 674598 88114 438434 167733 457299 951288 510096 635193 504974 649221 617914 223711 812277 364169 746142 836563 825312 994240 198961 567906 905208 509572 744710 891294 928176 833980 985385 858440 50835 86130 324862 796245 935992 383...
output:
9992833979971052 9992833979971052 9992833980354966 9992779245594768 9992703452732543 9992625566170070 9992625565960956 9992591568400874 9992591568400874 9992642080818492 9992607147102575 9992607147102575 9992607147182194 9992607147570244 9992607149729827 9992686490357794 9992684455132888 99926574027...
result:
wrong answer 1st lines differ - expected: '9992833979971052 9992833979971...4203005830427 10004203006781811', found: '9992833979971052 9992833979971...203005830427 10004203006781811 '