QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#218118 | #7079. Array | KLPP# | TL | 1853ms | 43084kb | C++20 | 3.4kb | 2023-10-17 18:45:47 | 2023-10-17 18:45:47 |
Judging History
answer
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long int lld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
#pragma GCC optimize("-O3","unroll-loops")
#pragma GCC optimize("-Ofast")
int n;
lld arr[2000000];
int nxt[2000000];
int prv[2000000];
map<lld,int> m;
set<lld> s;
lld sm(lld x, lld y){
lld ans=(y-x+1);
ans*=(x+y);
ans/=2;
return ans;
}
vector<lld >V;
const int MX=2'000'000;
class ST{
lld mn[MX*4];
public:
void build(int a, int b, int node){
mn[node]=0;
if(a==b)return;
int mid=(a+b)/2;
build(a,mid,2*node);
build(mid+1,b,2*node+1);
}
void update(int a, int b, int node, int pos, lld val){
if(pos<a || b<pos)return;
if(a==b){
mn[node]=val;
return;
}
int mid=(a+b)/2;
update(a,mid,2*node,pos,val);
update(mid+1,b,2*node+1,pos,val);
mn[node]=min(mn[2*node],mn[2*node+1]);
}
lld query(int a, int b, int node, int l, int r){
if(r<a || b<l)return 1e18;
if(l<=a && b<=r)return mn[node];
int mid=(a+b)/2;
return min(query(a,mid,2*node,l,r),query(mid+1,b,2*node+1,l,r));
}
};
ST S1,S2;
void solve(){
V.clear();
m.clear();
s.clear();
cin>>n;
rep(i,0,n)cin>>arr[i];
rep(i,0,n)V.push_back(arr[i]);
sort(V.begin(),V.end());
V.resize(unique(V.begin(),V.end())-V.begin());
int M=V.size();
S1.build(0,M-1,1);
S2.build(0,M-1,1);
for(int i=n-1;i>-1;i--){
if(m.find(arr[i])==m.end())nxt[i]=n;
else nxt[i]=m[arr[i]];
m[arr[i]]=i;
}
trav(a,m){
lld X=a.second;
S1.update(0,M-1,1,lower_bound(V.begin(),V.end(),a.first)-V.begin(),(X*(X+1))/2-a.first);
S2.update(0,M-1,1,lower_bound(V.begin(),V.end(),a.first)-V.begin(),(X*(X+1))/2+a.first);
}
m.clear();
rep(i,0,n){
if(m.find(arr[i])==m.end())prv[i]=-1;
else prv[i]=m[arr[i]];
m[arr[i]]=i;
}
lld best=0;
rep(i,0,n){
S1.update(0,M-1,1,lower_bound(V.begin(),V.end(),arr[i])-V.begin(),1e18);
S2.update(0,M-1,1,lower_bound(V.begin(),V.end(),arr[i])-V.begin(),1e18);
if(prv[i]==-1){
if((int)s.size()>0){
set<lld>::iterator it=s.lower_bound(arr[i]);
if(it!=s.end()){
best=min(best,*it-arr[i]-sm(i+1,nxt[i]));
}
if(it!=s.begin()){
best=min(best,arr[i]-*prev(it)-sm(i+1,nxt[i]));
}
}
lld I=nxt[i];
best=min(best,S1.query(0,M-1,1,0,lower_bound(V.begin(),V.end(),arr[i])-V.begin())+arr[i]-(I*(I+1))/2);
best=min(best,S2.query(0,M-1,1,lower_bound(V.begin(),V.end(),arr[i])-V.begin(),M-1)-arr[i]-(I*(I+1))/2);
}
s.insert(arr[i]);
}
s.clear();
lld ans=0;
rep(i,0,n){
s.insert(arr[i]);
lld can=i+1;
can*=s.size();
ans+=can;
}
//cout<<ans<<endl;
cout<<ans+best<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
cin>>tt;
while(tt--){
solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3380kb
input:
1 4 1 2 3 4
output:
22
result:
ok "22"
Test #2:
score: 0
Accepted
time: 197ms
memory: 3488kb
input:
100000 10 873324878 873324878 873324878 873324878 891656676 891656676 615245360 873324878 873324878 873324878 10 560723194 560723194 797429144 797429144 560723194 797429144 819647695 560723194 797429144 560723194 10 750627649 746781323 756277046 756277046 750627649 750627649 756277046 750627649 9142...
output:
134 141 180 168 109 95 181 185 144 149 202 158 161 107 210 210 255 104 210 109 82 158 149 201 154 109 206 158 161 161 158 134 206 198 161 109 143 152 183 156 171 201 149 104 210 161 181 152 152 250 185 243 206 158 152 128 147 203 225 143 203 198 206 201 158 109 114 100 183 161 162 183 154 222 181 14...
result:
ok 100000 tokens
Test #3:
score: 0
Accepted
time: 338ms
memory: 3424kb
input:
39978 23 512481863 596944631 624383245 441725511 441725511 594496544 441725511 624383245 698754670 596944631 182448912 636350614 596944631 391310300 624383245 391310300 698754670 596944631 441725511 182448912 826649520 351713941 596944631 24 789886131 679943285 874352131 191233114 214841280 21484128...
output:
2322 2416 4226 1974 3362 1631 2373 2521 3618 4540 2589 2104 2692 1407 2757 2394 3938 2106 2769 3209 3418 3828 2339 3954 5404 3191 1734 4260 2178 1354 2829 2552 2820 2385 3102 3442 3459 3027 3631 2399 1682 4564 4494 4217 3756 4176 3290 4109 1702 3740 4220 4117 2118 1840 4223 3499 3834 2472 5465 1636 ...
result:
ok 39978 tokens
Test #4:
score: 0
Accepted
time: 471ms
memory: 3448kb
input:
10000 100 70652501 126219335 870011044 503878453 331807482 42366188 570696778 892481058 11179909 898060545 596710776 892481058 892481058 126219335 938507063 540380652 869222706 898060545 380041360 643567581 977928808 655190742 75768776 126219335 386687451 513015608 898060545 540380652 798597796 7794...
output:
156439 177539 142728 105896 98255 145550 158303 139241 142500 123767 164463 173494 162837 133032 161874 108825 159157 176582 143686 146818 161864 166700 93293 121994 166017 96652 176532 106752 141748 145107 147984 139577 153293 146263 112899 130505 178781 186526 162804 138862 106517 170352 136899 13...
result:
ok 10000 tokens
Test #5:
score: 0
Accepted
time: 370ms
memory: 3448kb
input:
2854 312 996363788 551569708 173701735 548916953 952258024 368183068 334969896 577986978 880966548 888335092 56554707 551569708 115473602 634253580 72462813 954601359 56554707 551569708 91899583 889292360 819443410 501067697 944057852 123522638 888335092 957353706 173701735 979062687 618781540 72462...
output:
2061224 1860838 1010356 1174361 4143500 1474589 1510609 2004600 2605061 2155980 2176727 2397067 1531557 4124825 2541238 2474761 5227207 1465667 1794014 2862489 1879552 3233292 1783499 4062518 2333798 1421177 1282363 2275831 749765 836505 1370414 3239916 1942884 1581051 3075330 992985 2425172 1956879...
result:
ok 2854 tokens
Test #6:
score: 0
Accepted
time: 770ms
memory: 3596kb
input:
672 1783 13633474 314389801 314389801 478689538 360878436 473612024 360878436 303057784 473612024 495382779 360878436 216051209 478689538 495382779 234042033 975268148 555927466 555927466 303057784 495382779 478689538 314389801 303057784 495382779 710763719 473612024 478689538 314389801 710763719 36...
output:
25438555 169572802 576895114 782992072 520053889 306556547 978922119 730923783 773363213 622338884 186399639 1176724955 626653188 777367734 675391210 63817780 241450985 332034356 492689051 285034414 1215181602 403072096 659235443 181531295 1288006738 196234032 124249254 750551173 474661528 243082576...
result:
ok 672 tokens
Test #7:
score: 0
Accepted
time: 768ms
memory: 3612kb
input:
665 1884 988528257 299902608 10367339 690685855 297786544 151593747 178772726 128994663 356336120 679465966 391875298 926876636 570229960 916252616 981836665 180968279 383300245 495353761 613581589 820295995 569877672 29530262 648858041 696880729 936076029 546413376 955368107 569952181 233710412 921...
output:
1031986085 774354144 453499837 908363568 17851109 339542359 284218464 419696363 139411796 23621377 371250815 412164350 205376037 528615993 137490262 859288246 345876515 545360602 103616731 106611787 220924277 545575580 242323074 417131282 291975252 453966968 91112734 37085713 11881659 75083565 33927...
result:
ok 665 tokens
Test #8:
score: 0
Accepted
time: 1047ms
memory: 5212kb
input:
66 14356 253106248 592710041 414072883 103006455 322512168 697793017 901854511 153070792 719309056 122895833 885927063 180171049 637838473 327047040 524579686 524613030 298082160 632660508 558592102 901854511 905263602 47554049 372196525 789204610 976530384 367395091 187162155 67938161 370104072 601...
output:
23479834161 799502034134 675205839464 500643795049 574278312221 291248855617 397226866760 53495851508 609398896187 61697357337 434398239821 609248235282 263116591609 107700514782 748959952111 738507091193 591138130194 130173412046 290538027417 264876584681 451738843253 139503470854 368222643935 4896...
result:
ok 66 tokens
Test #9:
score: 0
Accepted
time: 1112ms
memory: 5120kb
input:
70 11577 39271197 465266350 880381180 919862794 283336256 699576432 592085978 990211654 28463348 374163546 787974727 921480225 147274330 465132652 943772918 182098783 960046651 475285160 271010889 521970889 909368831 989371018 420557956 382878850 409934528 512499811 975508706 185955347 254218975 588...
output:
238654170399 180637525482 206136021261 813929855071 294864661408 299629365609 310169518941 383763137763 343595506605 553914063017 536500010060 298432699348 177496050451 452245271911 1260424768862 268665288590 328382007261 106563243988 392005248051 338771160034 402789149006 470752638478 568901993661 ...
result:
ok 70 tokens
Test #10:
score: 0
Accepted
time: 1414ms
memory: 20292kb
input:
6 156266 744545865 498279220 516785883 422011009 210138593 449193083 51835703 736667794 182317904 114470255 65501621 620674663 549821600 521784690 616832697 567941606 489395176 199975946 743493420 810387200 816885010 229472163 363378103 902667962 726557335 900234267 629699869 936973062 622020041 810...
output:
479635798297828 362635644131716 1011525985949089 488793761980048 33668530747002 81072825072745
result:
ok 6 tokens
Test #11:
score: 0
Accepted
time: 1670ms
memory: 20304kb
input:
6 114879 350483188 229392942 835985 377187751 672550303 765919987 297302125 947197934 916324905 198629824 176071283 865203792 278920041 542453811 33409978 702150522 495350318 145066775 916324905 448457483 439321289 251885204 666171087 436957312 225978993 927693562 749127935 548765042 537503413 28356...
output:
221955883278187 534908035749935 1060938659643029 322763505720467 435675990222256 1155597836121245
result:
ok 6 tokens
Test #12:
score: 0
Accepted
time: 203ms
memory: 26724kb
input:
1 1000000 576179218 7469306 7469306 7469306 851555289 816897873 258273564 7469306 565795738 258273564 816897873 851555289 636599179 186944894 562917743 976122503 406416234 422519347 267881138 186944894 329635130 636599179 186944894 406416234 499852038 329635130 258273564 562917743 406416234 49985203...
output:
9500009492566
result:
ok "9500009492566"
Test #13:
score: 0
Accepted
time: 314ms
memory: 26608kb
input:
1 1000000 967479764 35626031 9646954 212793146 639417768 840904449 95397653 415471507 341137983 950230238 422979558 213867389 370073239 213867389 84084242 336038515 744731667 199239042 639417768 853571934 445548459 840904449 297537941 609165556 724313009 336038515 422979558 400864897 633256739 86217...
output:
50000049009241
result:
ok "50000049009241"
Test #14:
score: 0
Accepted
time: 720ms
memory: 26812kb
input:
1 1000000 659288241 86257235 637516135 497550967 429895809 429838420 442140485 658598786 276955898 752937890 504220809 619798426 976500837 14401011 111197312 122981907 693488147 309861633 125331086 403041883 734343262 592076786 531446548 481528035 429895809 46734687 512983363 67899791 830548176 2637...
output:
746997526675936
result:
ok "746997526675936"
Test #15:
score: 0
Accepted
time: 1080ms
memory: 28736kb
input:
1 1000000 665441640 909522641 992257606 937153488 23977253 797235755 142870958 248988661 142117526 862447662 66516460 177406279 238379556 676588452 300939454 131123508 840421190 983956016 238279146 444692917 430966983 838258239 577351402 941200483 692363463 982394013 304603687 905781293 191215261 93...
output:
7567046635935358
result:
ok "7567046635935358"
Test #16:
score: 0
Accepted
time: 1853ms
memory: 43084kb
input:
1 1000000 91732121 950768864 575565712 334438147 175564123 955458275 685724442 447868741 461422452 854684912 995814615 217200988 340233863 840163317 948535685 384267373 116411916 477848888 977488727 976184840 848529069 928044967 215704309 838630094 110797338 74056471 234779199 650225349 186891275 15...
output:
55151722672817014
result:
ok "55151722672817014"
Test #17:
score: 0
Accepted
time: 264ms
memory: 3436kb
input:
1000000 1 817826210 1 322299762 1 112069637 1 250533319 1 634300701 1 30937707 1 211768584 1 875552123 1 424899830 1 52950022 1 916349445 1 344132969 1 541703011 1 506166283 1 759373993 1 73271994 1 852843665 1 938555948 1 161376625 1 479658614 1 202803751 1 750203514 1 495583569 1 269316500 1 28004...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok 1000000 tokens
Test #18:
score: 0
Accepted
time: 214ms
memory: 3488kb
input:
285805 3 954255440 691069598 578739491 5 497323416 497323416 497323416 497323416 497323416 3 591656097 384754213 591656097 3 503875834 488653501 772331438 3 403554557 403554557 252769032 4 7690979 7690979 7690979 433507453 2 21333405 21333405 2 759105800 68681011 4 639903323 934982795 639903323 9349...
output:
14 15 11 14 9 14 3 5 19 6 11 27 14 6 15 9 11 17 3 11 36 3 41 19 10 11 5 3 15 3 38 6 10 9 11 11 5 27 14 36 38 3 14 9 5 3 23 3 5 15 3 10 50 36 36 19 3 36 38 15 5 19 11 14 15 3 3 23 9 5 6 30 19 6 20 3 21 3 15 3 23 3 14 41 6 17 17 14 3 6 3 23 26 3 11 3 6 3 3 27 5 6 27 10 15 11 15 5 14 14 29 15 6 26 9 5 ...
result:
ok 285805 tokens
Test #19:
score: 0
Accepted
time: 246ms
memory: 3444kb
input:
166720 4 548493387 945973495 66607405 945973495 9 876263329 616837164 481302812 876263329 616837164 876263329 616837164 306805180 705645072 10 88492568 727138540 88492568 88492568 617635673 35976650 786632275 33599123 88492568 35976650 6 886308691 306695927 306695927 986882397 608069229 306695927 5 ...
output:
26 157 255 67 20 170 251 56 214 166 27 3 229 6 56 36 95 19 3 201 56 211 21 9 3 118 154 80 94 158 134 43 17 97 142 77 59 19 280 77 178 3 21 55 11 277 14 10 6 229 84 241 247 55 3 245 11 45 36 229 6 149 174 50 241 205 6 87 62 50 36 5 127 269 36 50 45 5 3 38 134 29 41 23 6 23 109 179 120 131 59 11 118 1...
result:
ok 166720 tokens
Test #20:
score: -100
Time Limit Exceeded
input:
1 1000000 332391519 48672576 917201020 192129027 161319028 789807817 572701356 787326501 466507224 106313501 349625408 39857612 31471565 133495056 592362741 457118882 991177685 347461407 893302388 151120088 441933423 574334573 343620531 593083142 754523776 945318712 940101785 554834234 920927719 359...