QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#424757 | #2070. Heavy Stones | Nt_Yester | TL | 576ms | 240880kb | C++14 | 4.1kb | 2024-05-29 16:50:17 | 2024-05-29 16:50:17 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#define N 200010
#define int long long
#define INF 0x3f3f3f3f
using namespace std;
int n,a[N];
struct P {
int val,sum,l,r,siz;
P (int _val=0,int _sum=0,int _l=INF,int _r=0) { val=_val,sum=_sum,l=_l,r=_r,siz=max(_r-_l+1,0ll); }
};
vector<P> stk;
vector<pair<P,int> > pre,suf;
P operator+(P t1,P t2) { return P(t1.val+t2.val+t1.sum*t2.siz,t1.sum+t2.sum,min(t1.l,t2.l),max(t1.r,t2.r)); }
bool operator==(P t1,P t2) { return t1.val==t2.val and t1.l==t2.l and t1.r==t2.r; }
bool operator<(P t1,P t2) {
if (t1.sum*t2.siz!=t2.sum*t1.siz) return t1.sum*t2.siz<t2.sum*t1.siz;
return t1.l<t2.l;
}
bool operator<=(P t1,P t2) { return t1<t2 or t1==t2; }
bool operator>(P t1,P t2) { return t2<t1; }
bool operator>=(P t1,P t2) { return t2<t1 or t2==t1; }
const double A=0.75;
int tcnt,rt,flatcnt,flata[N<<3];
struct Tree { P val; int ls,rs,num,siz,sizv,siznd; int Val,Sum,Siz; }t[N<<3];
void upd(int x) {
t[x].Siz=t[t[x].ls].Siz+t[t[x].rs].Siz+(t[x].num>0?t[x].val.siz:0);
t[x].Sum=t[t[x].ls].Sum+t[t[x].rs].Sum+(t[x].num>0?t[x].val.sum:0);
if (t[x].num) t[x].Val=t[t[x].ls].Val+t[x].val.val+t[t[x].rs].Val+t[x].val.sum*t[t[x].rs].Siz+t[t[x].ls].Sum*(t[x].val.siz+t[t[x].rs].Siz);
else t[x].Val=t[t[x].ls].Val+t[t[x].rs].Val+t[t[x].ls].Sum*t[t[x].rs].Siz;
t[x].siz=t[t[x].ls].siz+t[t[x].rs].siz+t[x].num;
t[x].sizv=t[t[x].ls].sizv+t[t[x].rs].sizv+1;
t[x].siznd=t[t[x].ls].siznd+t[t[x].rs].siznd+(t[x].num>0);
}
bool ifflat(int x) { return (t[x].num>0 and (t[x].siz*A<=(double)std::max(t[t[x].ls].siz,t[t[x].rs].siz) or (t[x].sizv*A>=(double)t[x].siznd))); }
void flatdfs(int x) {
if (!x) return ;
flatdfs(t[x].ls);
if (t[x].num>0) flata[++flatcnt]=x;
flatdfs(t[x].rs);
}
void flatrebuild(int &x,int l,int r) {
if (l>r) return void(x=0);
int mid=(l+r>>1);
x=flata[mid];
flatrebuild(t[x].ls,l,mid-1),flatrebuild(t[x].rs,mid+1,r);
upd(x);
}
void flat(int &x) { flatcnt=0; flatdfs(x); flatrebuild(x,1,flatcnt); }
void ins(int &x,P p) {
if (!x) {
t[x=++tcnt]={p,0,0,1,1,1,1,p.val,p.sum,p.siz};
upd(x); return;
} else {
if (t[x].val==p) t[x].num++;
else if (p<t[x].val) ins(t[x].ls,p);
else ins(t[x].rs,p);
upd(x);
if (ifflat(x)) flat(x);
}
}
void del(int &x,P p) {
if (!x) return;
if (t[x].val==p) t[x].num--;
else if (p<t[x].val) del(t[x].ls,p);
else del(t[x].rs,p);
upd(x);
if (ifflat(x)) flat(x);
}
signed main() {
scanf("%lld",&n);
for (int i=1;i<=n;i++) scanf("%lld",a+i);
for (int i=1;i<=n;i++) {
P x=P(a[i],a[i],i,i);
while (!stk.empty() and stk.back()<=x) {
P y=stk.back(); stk.pop_back();
pre.push_back({y,-1}); x=x+y;
}
stk.push_back(x); pre.push_back({x,1});
}
stk.clear();
for (int i=n;i;i--) {
P x=P(a[i],a[i],i,i);
while (!stk.empty() and stk.back()<=x) {
P y=stk.back(); stk.pop_back();
suf.push_back({y,1}); x=x+y;
}
stk.push_back(x); suf.push_back({x,-1});
}
for (auto i:stk) ins(rt,i);
int precnt=-1,sufcnt=suf.size()-1;
for (int i=1;i<=n;i++) {
while (sufcnt>=0 and (suf[sufcnt].second!=-1 or suf[sufcnt].first.l!=i+1)) {
if (suf[sufcnt].second==-1) del(rt,suf[sufcnt].first);
else ins(rt,suf[sufcnt].first);
--sufcnt;
}
printf("%lld",t[rt].Val+a[i]*(n-1));
if (i!=n) printf(" ");
while (precnt==-1 or pre[precnt].first.r!=i) {
++precnt;
if (pre[precnt].second==-1) del(rt,pre[precnt].first);
else ins(rt,pre[precnt].first);
}
}
printf("\n");
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 15ms
memory: 181852kb
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: 181288kb
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: 0
Accepted
time: 172ms
memory: 240880kb
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 9992591568189384 9992591567396390 9992591567396390 9992591567476009 9992591567864059 9992591571455644 9992591572452874 9992591574009688 99925915745...
result:
ok single line: '9992833979971052 9992833979971...4203005830427 10004203006781811'
Test #4:
score: 0
Accepted
time: 576ms
memory: 239356kb
input:
200000 754184 13065 865956 768626 744822 919059 583237 600724 524170 165425 246285 226816 966566 610515 550388 715978 549675 83917 323652 423422 266395 285394 470160 472551 743112 879255 493014 992502 158286 301973 857254 395231 282559 806597 243952 205846 976027 598783 69632 165916 622634 620757 34...
output:
10014997841701475 10014947015238274 10014947015238274 10014947015993835 10014917512559448 10014868558588544 10014784759644306 10014768120716895 10014747984710856 10014743158715252 10014743158715252 10014743158776643 10014743160298065 10014743161445632 10014743159820460 10014743158450587 100147431338...
result:
ok single line: '10014997841701475 100149470152...87021303093862 9987021303093862'
Test #5:
score: 0
Accepted
time: 520ms
memory: 239868kb
input:
200000 783905 465734 702646 342070 382378 612468 565748 741917 648664 523500 182013 89445 129572 26912 175340 537820 717500 718402 563606 124584 111640 830294 957543 449869 563613 901195 496363 918492 226878 429682 770972 654243 222706 539682 471463 654386 31958 381760 352938 783523 851106 354268 75...
output:
9985525267421517 9985468293020605 9985468291830972 9985434229308153 9985434229308153 9985434229224741 9985434226842360 9985434224335794 9985404337013738 9985374417560553 9985369520866620 9985369520652405 9985369520589872 9985369520589872 9985369520721535 9985369522444873 9985369525429091 99853695285...
result:
ok single line: '9985525267421517 9985468293020...75790145914810 9975830720773589'
Test #6:
score: 0
Accepted
time: 549ms
memory: 239936kb
input:
200000 853686 20716 174697 869738 857025 507882 375300 849975 893238 468077 184722 535631 383301 657603 49942 235054 316530 521594 464021 979922 188332 335907 114903 261684 434608 966750 619976 793830 275183 498775 690216 769588 589049 958997 995520 845297 350658 198715 707600 18882 203867 132974 16...
output:
10007482900924066 10007412205735064 10007412205735064 10007412206584086 10007412208102723 10007412205041250 10007412203774418 10007412203774418 10007381409966150 10007302822767145 10007302821693099 10007302821693099 10007302821528524 10007302820980147 10007302819330353 10007302819330353 100073028195...
result:
ok single line: '10007482900924066 100074122057...0782048899392 10000853953946590'
Test #7:
score: 0
Accepted
time: 563ms
memory: 238192kb
input:
200000 282721 513944 210659 728001 402993 628298 260323 376301 404417 439890 298253 575700 991264 735782 387209 874869 995018 778731 247456 286146 598879 216939 91609 177310 82415 36330 265803 953105 564707 201163 357501 390048 142474 266916 46616 539260 570794 792662 270237 67158 511870 166602 9035...
output:
10008509175990736 10008509175918674 10008509175918674 10008509176578011 10008509171139661 10008509170903880 10008509157826476 10008509157826476 10008509157970570 10008509158067444 10008509158067444 10008509158889313 10008509162066449 10008509131249736 10008509108213966 10008509107556161 100084882028...
result:
ok single line: '10008509175990736 100085091759...6908130262195 10006908130262195'
Test #8:
score: -100
Time Limit Exceeded
input:
200000 22 25 28 34 59 61 68 80 108 116 119 121 121 123 131 131 141 143 152 166 167 174 184 215 228 231 264 279 307 309 313 314 327 339 341 362 381 399 414 421 430 449 477 486 490 500 502 508 529 529 562 566 569 571 585 597 641 650 658 675 680 684 698 721 723 724 731 732 737 755 784 785 810 813 817 8...
output:
10003490282328154 10003490282328154 10003490282328160 10003490282328181 10003490282328283 10003490282328418 10003490282328590 10003490282328841 10003490282329300 10003490282329851 10003490282330437 10003490282331046 10003490282331657 10003490282332292 10003490282333033 10003490282333782 100034902823...