QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#103562 | #4802. Ternary Search | mod998244353 | TL | 579ms | 14772kb | C++14 | 2.1kb | 2023-05-06 17:44:27 | 2023-05-06 17:44:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=500005;
ll res[MAXN];
int n,a[MAXN],b[MAXN],pos[MAXN],t[MAXN];
inline void add(int x,int y) {
for(; x<=n; x+=x&-x) t[x]+=y;
}
inline int query(int x) {
int sum=0;
for(; x; x&=x-1) sum+=t[x];
return sum;
}
struct Tree {
int l,r,lt,cnt,mx;
ll sum;
} tr[MAXN<<2];
void build(int num,int l,int r) {
tr[num].l=l,tr[num].r=r,tr[num].sum=tr[num].cnt=tr[num].lt=0;
tr[num].mx=-1e9;
if(l==r)return ;
build(num<<1,l,l+r>>1),build(num<<1|1,l+r+2>>1,r);
}
inline void push_up(int num) {
tr[num].sum=tr[num<<1].sum+tr[num<<1|1].sum;
tr[num].cnt=tr[num<<1].cnt+tr[num<<1|1].cnt;
tr[num].mx=max(tr[num<<1].mx,tr[num<<1|1].mx);
}
inline void Tag(int num,ll x) {
tr[num].sum+=tr[num].cnt*x;
tr[num].lt+=x,tr[num].mx+=x;
}
inline void push_down(int num) {
if(tr[num].lt) {
Tag(num<<1,tr[num].lt),Tag(num<<1|1,tr[num].lt);
tr[num].lt=0;
}
}
void updatel(int num,int x,int l) {
if(tr[num].l==tr[num].r) {
tr[num].sum=tr[num].mx=-l;
tr[num].cnt=(l>0);
return;
}
push_down(num);
updatel(num<<1|(x>tr[num<<1].r),x,l);
push_up(num);
}
void chk(int num) {
if(tr[num].mx<0) return;
if(tr[num].l==tr[num].r) {
tr[num].cnt=0;
return;
}
push_down(num),chk(num<<1),chk(num<<1|1);
return push_up(num);
}
void update(int num,int l,int r,ll x) {
if(l<=tr[num].l&&tr[num].r<=r)return Tag(num,x),chk(num);
push_down(num);
if(l<=tr[num<<1].r) update(num<<1,l,r,x);
if(tr[num<<1].r<r) update(num<<1|1,l,r,x);
return push_up(num);
}
inline void solve() {
for(int i=1; i<=n; ++i) a[i]=n-a[i]+1,pos[a[i]]=i,t[i]=0;
build(1,1,n);
ll ans=0;
for(int i=1,L=0; i<=n; ++i) {
L=query(a[i]),ans+=L;
updatel(1,a[i],L);
if(a[i]<n)update(1,a[i]+1,n,1);
add(a[i],1);
res[i]=min(res[i],ans+tr[1].sum);
}
}
int main() {
scanf("%d",&n);
for(int i=1; i<=n; ++i) scanf("%d",&a[i]),b[i]=a[i],res[i]=1e18;
sort(b+1,b+n+1);
for(int i=1; i<=n; ++i) a[i]=lower_bound(b+1,b+n+1,a[i])-b;
solve(),solve();
for(int i=1; i<=n; ++i) printf("%lld\n",res[i]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 13760kb
input:
9 11 4 5 14 1 9 19 8 10
output:
0 0 0 0 2 3 3 6 7
result:
ok 9 numbers
Test #2:
score: 0
Accepted
time: 2ms
memory: 11692kb
input:
1 167959139
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 4ms
memory: 13752kb
input:
10 802030518 983359971 96204862 640274071 796619138 71550121 799843967 598196518 402690754 446173607
output:
0 0 0 1 1 3 3 5 7 9
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 0ms
memory: 13832kb
input:
200 280383943 994080326 443906342 107926501 414713819 45732490 370693588 928126378 581740643 189811400 142817640 218386999 447997927 640003704 637428425 151698689 209462128 155599305 390080477 958251194 515653943 751042490 1186761 443171900 968975479 829084542 462837689 157692260 607049612 606116398...
output:
0 0 0 0 1 1 3 4 5 8 12 15 17 18 19 25 30 35 38 38 41 42 54 60 60 62 69 81 87 93 109 112 124 126 139 148 152 164 169 180 187 193 211 221 226 244 244 252 264 273 286 307 316 328 339 358 378 392 408 416 439 445 447 463 490 503 521 536 547 567 594 619 638 638 659 668 702 724 729 758 772 777 816 836 845 ...
result:
ok 200 numbers
Test #5:
score: 0
Accepted
time: 2ms
memory: 11792kb
input:
205 798721503 163087712 141733092 316449050 127486543 148708227 646902722 254007921 648511970 807308399 362190286 356609247 109012359 421274742 647319513 34648738 573893368 682755343 757300027 674077117 324958818 106483343 305773689 837270774 883136402 847234810 756696363 354615040 58759846 32739626...
output:
0 0 0 0 1 2 2 3 3 3 6 10 16 19 22 23 28 34 35 37 45 49 52 61 64 65 69 80 86 86 90 95 105 109 112 113 120 133 136 136 152 166 180 181 196 214 231 245 266 287 297 309 330 341 358 368 383 387 393 414 434 448 456 484 494 506 521 534 543 551 561 596 621 626 634 644 655 692 732 743 750 767 807 846 869 876...
result:
ok 205 numbers
Test #6:
score: 0
Accepted
time: 0ms
memory: 11900kb
input:
300 310486422 145995805 744831366 913444383 54589620 760840937 351438066 497043991 437781614 854981783 137693561 940050335 22692948 779922082 713507093 773173557 700078081 889701510 709956997 620732070 535136113 428608043 656183847 627784826 348865620 634573459 271461607 622653937 662713549 35111166...
output:
0 0 0 0 1 2 3 5 7 9 11 14 15 19 22 25 27 33 36 38 40 41 45 49 50 55 55 60 69 71 76 80 99 110 110 116 117 125 148 151 175 182 208 223 229 245 256 259 280 285 302 314 332 337 345 369 380 398 403 405 413 447 468 484 494 511 537 550 555 585 601 616 637 659 664 695 715 730 755 757 762 777 787 805 842 860...
result:
ok 300 numbers
Test #7:
score: 0
Accepted
time: 0ms
memory: 13844kb
input:
400 418345904 425283570 502048611 949470422 1539681 816939105 835973188 548395262 912622971 892988664 137695077 200974194 416058059 46966392 717454596 508236934 577018523 271703565 404875844 116208843 795979997 232130698 201391686 396794371 873677110 643916648 979303165 209135495 365323814 220296329...
output:
0 0 0 0 0 1 3 4 7 8 8 9 11 11 16 20 25 28 32 33 43 47 50 55 67 73 73 83 90 98 99 103 110 116 124 135 142 160 175 182 195 197 210 216 217 222 228 234 254 268 295 306 334 337 349 357 383 388 392 420 441 460 473 474 480 491 506 532 548 549 572 589 629 635 674 700 711 714 744 773 797 809 815 852 860 895...
result:
ok 400 numbers
Test #8:
score: 0
Accepted
time: 579ms
memory: 14772kb
input:
10000 654266585 918195389 61806003 512260326 35440115 595032690 353979552 32592347 712550231 380796590 139710715 828841723 115308806 675515525 910499939 780014628 924148821 719010926 731425929 381219228 963983190 359044179 76245693 524823828 890447664 856974623 751508756 830808517 963022804 54360656...
output:
0 0 0 1 1 2 3 5 5 7 10 10 14 16 16 18 18 22 26 34 34 45 54 57 64 70 76 82 87 94 97 97 104 108 118 125 136 143 143 159 162 165 169 172 181 202 210 213 224 230 240 255 257 277 279 307 337 342 364 367 377 403 408 416 428 459 496 504 533 534 554 571 577 595 630 637 666 684 723 740 782 797 818 840 864 87...
result:
ok 10000 numbers
Test #9:
score: -100
Time Limit Exceeded
input:
100000 909264242 651840489 623350850 547495386 693908264 894663677 90585843 745747150 871758382 121892899 769215576 332202967 626472418 629926442 365356165 588776397 879320123 676708911 253241624 334264710 58088451 743496068 4060883 166883807 81383194 778842188 617677263 814376024 454099047 56897081...