QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#268678 | #6420. Certain Scientific Railgun | C1942huangjiaxu | AC ✓ | 94ms | 30448kb | C++14 | 2.3kb | 2023-11-28 19:41:04 | 2023-11-28 19:41:04 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef long long ll;
int T,n,m,mx[N],mn[N];
pair<int,int>a[N];
ll ans;
struct seg{
#define L k<<1
#define R k<<1|1
ll tr[N<<2],tg[N<<2];
void pushtg(int k,ll v){
tr[k]+=v,tg[k]+=v;
}
void pushdown(int k){
if(tg[k]){
pushtg(L,tg[k]);
pushtg(R,tg[k]);
tg[k]=0;
}
}
void pushup(int k){
tr[k]=min(tr[L],tr[R]);
}
void build(int k,int l,int r,int S){
tg[k]=0;
if(l==r){
tr[k]=-a[l].first-(S&1)*a[l].first,void();
if(S&2)tr[k]+=mx[l-1]-mn[l-1]-mn[l-1];
else tr[k]+=mx[l-1]+mx[l-1]-mn[l-1];
return;
}
int mid=l+r>>1;
build(L,l,mid,S),build(R,mid+1,r,S);
pushup(k);
}
void modify(int k,int l,int r,int x,int y,ll v){
if(x<=l&&r<=y)return pushtg(k,v);
pushdown(k);
int mid=l+r>>1;
if(x<=mid)modify(L,l,mid,x,y,v);
if(y>mid)modify(R,mid+1,r,x,y,v);
pushup(k);
}
}sg[4];
void solve(){
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d%d",&a[i].first,&a[i].second);
a[++n]=make_pair(0,0);
sort(a+1,a+n+1);
ans=1e18,m=1,mn[n+1]=mx[n+1]=0;
while(a[m]!=make_pair(0,0))++m;
for(int i=1;i<=m;++i)mx[i]=max(mx[i-1],a[i].second),mn[i]=min(mn[i-1],a[i].second);
for(int i=n;i>=m;--i)mx[i]=max(mx[i+1],a[i].second),mn[i]=min(mn[i+1],a[i].second);
for(int i=0;i<4;++i)sg[i].build(1,1,m,i);
for(int i=n,j1=1,j2=1;i>=m;--i){
for(int o=0;o<4;++o){
ans=min(ans,sg[o].tr[1]+a[i].first+(o&1^1)*a[i].first);
if(o&2){
sg[o].modify(1,1,m,1,j1,-mx[i+1]);
sg[o].modify(1,1,m,1,j2,mn[i+1]+mn[i+1]);
}else{
sg[o].modify(1,1,m,1,j1,-mx[i+1]-mx[i+1]);
sg[o].modify(1,1,m,1,j2,mn[i+1]);
}
}
while(j1<m&&mx[j1]<=mx[i]){
++j1;
for(int o=0;o<4;++o){
if(o&2)sg[o].modify(1,1,m,j1,j1,-mx[j1-1]);
else sg[o].modify(1,1,m,j1,j1,-mx[j1-1]-mx[j1-1]);
}
}
while(j2<m&&mn[j2]>=mn[i]){
++j2;
for(int o=0;o<4;++o){
if(o&2)sg[o].modify(1,1,m,j2,j2,mn[j2-1]+mn[j2-1]);
else sg[o].modify(1,1,m,j2,j2,mn[j2-1]);
}
}
for(int o=0;o<4;++o){
if(o&2){
sg[o].modify(1,1,m,1,j1,mx[i]);
sg[o].modify(1,1,m,1,j2,-mn[i]-mn[i]);
}else{
sg[o].modify(1,1,m,1,j1,mx[i]+mx[i]);
sg[o].modify(1,1,m,1,j2,-mn[i]);
}
}
}
printf("%lld\n",ans);
}
int main(){
scanf("%d",&T);
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 20292kb
input:
3 2 0 1 1 0 4 1 1 -3 -3 4 -4 -2 2 4 1 100 3 100 -100 1 3 -100
output:
0 8 4
result:
ok 3 number(s): "0 8 4"
Test #2:
score: 0
Accepted
time: 3ms
memory: 20356kb
input:
120 4 11 11 -22 -22 33 -33 -44 44 4 -11 11 22 -22 -33 -33 44 44 4 -11 -11 22 22 -33 33 44 -44 4 11 -11 -22 22 33 33 -44 -44 4 -11 11 22 -22 33 33 -44 -44 4 11 11 -22 -22 -33 33 44 -44 4 11 -11 -22 22 -33 -33 44 44 4 -11 -11 22 22 33 -33 -44 44 4 1 1 -2 -2 3 -3 -4 4 4 -1 1 2 -2 -3 -3 4 4 4 -1 -1 2 2 ...
output:
99 99 99 99 99 99 99 99 9 9 9 9 9 9 9 9 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 0 0 0 0 0 0 0 0 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 12 12 12 12 12 12 12 12 0 0 0 0 0 0 0 0 11 11 11 11 11 11 11 11 111 111 111 111 111 111 111 111 300 300 300 300 300 300 300 300 5 5 5 5 5 5 5 5 2 2 2 2 2 2 2 2 48 48 48 48 48 48 48...
result:
ok 120 numbers
Test #3:
score: 0
Accepted
time: 23ms
memory: 19820kb
input:
1 100000 259716243 240441467 199457754 301066970 412772262 87809313 139833960 359731944 453667163 46926477 204936243 294432582 361538994 138967777 315714786 184515556 274799986 224851893 315004381 184713646 104778725 394583171 100521423 399036944 392072619 107037839 19992953 480118328 166443446 3325...
output:
500573654
result:
ok 1 number(s): "500573654"
Test #4:
score: 0
Accepted
time: 60ms
memory: 30328kb
input:
1 100000 231900334 268958119 8094073 491651466 181235767 318579810 152422229 348402615 453103138 47722205 169802544 329762126 66615606 433110853 63974760 435613334 412296501 86899836 380626267 120285438 234254409 265425647 343489254 155604658 456112096 43577922 389402607 110812358 361862073 13733807...
output:
500924008
result:
ok 1 number(s): "500924008"
Test #5:
score: 0
Accepted
time: 70ms
memory: 30356kb
input:
1 100000 243687982 257271514 406262214 92746823 115474686 384860427 179141355 321775276 111057575 388052482 85754748 413966937 89015940 411624061 97395736 402451385 394966118 105231558 118633450 381544518 121485690 377952946 93095255 406155875 285623743 214726445 230422095 268632132 137612706 361757...
output:
1001565829
result:
ok 1 number(s): "1001565829"
Test #6:
score: 0
Accepted
time: 85ms
memory: 30444kb
input:
1 100000 450661745 49199859 46152367 453651505 490854754 8343231 273253564 227017249 177014519 322480838 295759745 203932936 261849726 238301630 64867210 434153377 105275689 393750931 321581727 178232552 494756932 6104054 31280896 468013695 46903507 453060294 162085346 337538866 239622896 259681652 ...
output:
1001452257
result:
ok 1 number(s): "1001452257"
Test #7:
score: 0
Accepted
time: 89ms
memory: 30308kb
input:
1 100000 150929109 349489222 6420724 493823770 464662613 35982721 402411960 97813587 40230561 460488339 470554995 30114056 344276320 154962447 219702967 280132033 104632512 395604723 479497355 19601658 117766907 383020273 36133778 463836986 175546685 324588426 43749701 456053065 130176612 369176811 ...
output:
1501257683
result:
ok 1 number(s): "1501257683"
Test #8:
score: 0
Accepted
time: 21ms
memory: 24548kb
input:
8 10000 108715119 390304225 45853335 454319274 399590321 101245208 273660814 225432152 97670975 403201669 476219330 23861282 58911490 440808774 128367407 370649954 306462809 192987122 396996806 103458329 129292472 370369176 135445802 364903545 9556524 491441067 272140862 228185209 394219336 10628059...
output:
499504970 499504970 499504970 499504970 499504970 499504970 499504970 499504970
result:
ok 8 numbers
Test #9:
score: 0
Accepted
time: 40ms
memory: 20416kb
input:
8 10000 88475214 411685526 295971508 203464188 475025201 25141393 312127126 188194692 19542945 480366130 488061278 12628967 186934840 313365863 481957571 18601403 358812248 140919718 22594424 477659644 217700756 281415099 165264320 334364787 117366326 382623925 104117966 396054687 249226386 25018520...
output:
1000322231 1000322231 1000322231 1000322231 1000322231 1000322231 1000322231 1000322231
result:
ok 8 numbers
Test #10:
score: 0
Accepted
time: 49ms
memory: 22488kb
input:
8 10000 269878538 230854020 456626682 43333438 434144544 66494813 232368812 267733469 68096662 431017351 17852539 482545915 87803855 412171111 265856716 234461260 115315829 384621424 31746172 467793722 373607866 126486181 23907446 476867982 333063328 166736814 343578833 156172707 244551435 255629961...
output:
1000479661 1000479661 1000479661 1000479661 1000479661 1000479661 1000479661 1000479661
result:
ok 8 numbers
Test #11:
score: 0
Accepted
time: 44ms
memory: 20412kb
input:
8 10000 159480657 340897530 349562243 150040650 173755554 326392525 282856769 217394893 392793822 107352227 491621792 7387423 440310768 58951882 187152471 313516759 227502759 272030922 48462295 450696921 183559088 317228169 365794915 133411085 345365867 154485210 493284111 6477090 397676402 10174717...
output:
1000582139 1000582139 1000582139 1000582139 1000582139 1000582139 1000582139 1000582139
result:
ok 8 numbers
Test #12:
score: 0
Accepted
time: 56ms
memory: 22492kb
input:
8 10000 445062805 54324634 73636583 427209121 94242291 406610072 426842404 73694287 124540787 376414432 56028719 443696776 390646024 108939638 322649687 177349434 398037737 102667192 130048845 369861315 51589239 448100026 11585337 488662930 291289277 208605416 314642705 186222797 405227467 95703222 ...
output:
1497536843 1497536843 1497536843 1497536843 1497536843 1497536843 1497536843 1497536843
result:
ok 8 numbers
Test #13:
score: 0
Accepted
time: 12ms
memory: 20192kb
input:
5000 16 29 -33 -5 -42 3 -2 -21 -32 11 43 -12 28 50 10 -31 47 49 -11 43 -45 -30 33 -34 -40 18 49 -50 8 31 8 -6 -31 14 34 6 31 -7 48 16 -18 23 44 -35 7 -49 -12 -48 15 3 7 -15 9 18 -21 -8 24 32 -3 44 5 -12 17 16 20 4 46 18 -38 -19 -43 -36 9 -28 -38 38 9 35 40 27 -31 37 -24 -28 -3 3 -11 16 41 4 -43 -48 ...
output:
126 90 126 54 86 118 125 8 116 123 94 63 94 46 100 103 82 88 91 108 116 111 38 88 109 121 11 16 133 40 52 139 126 44 133 24 70 101 122 105 86 52 32 122 44 30 87 35 95 99 78 109 133 64 82 80 111 22 123 108 92 144 104 62 60 102 67 49 22 98 58 119 109 138 75 71 133 90 88 91 59 110 119 136 9 94 85 105 6...
result:
ok 5000 numbers
Test #14:
score: 0
Accepted
time: 94ms
memory: 29868kb
input:
1 100000 141603120 358395957 91949299 408051533 280127622 219872540 269839621 230159940 398246595 101753077 348822087 151177633 348419099 151581884 296902611 203098137 338097907 161901334 372622396 127377407 394835486 105164842 447472828 52526264 400180018 99819426 413592564 86408119 360494425 13950...
output:
1349927669
result:
ok 1 number(s): "1349927669"
Test #15:
score: 0
Accepted
time: 69ms
memory: 29504kb
input:
1 90000 443555563 56444294 83262571 416738313 281645457 218355326 445801717 54198908 83627758 416372853 211139494 288860857 361318442 138681703 212318967 287681202 149020856 350978578 123082307 376916773 191763673 308236472 263766243 236233924 339734272 160266716 376958675 123040885 164258118 335742...
output:
899981686
result:
ok 1 number(s): "899981686"
Test #16:
score: 0
Accepted
time: 71ms
memory: 30372kb
input:
1 100000 391869133 108131089 153296049 346704832 435824119 64176139 402512179 97488625 108905299 391095683 287197881 212802334 303152943 196846093 337713912 162286279 249180885 250819612 178060316 321940429 393979501 106020779 66320276 433680592 116087834 383911295 65460918 434538801 304863521 19513...
output:
449995391
result:
ok 1 number(s): "449995391"
Test #17:
score: 0
Accepted
time: 68ms
memory: 30448kb
input:
1 100000 443245093 56754965 154391186 345608928 423601982 76397561 391610115 108390868 118474673 381525118 240893979 259106413 221239315 278759789 419622607 80377653 254528678 245471355 296991874 203008329 423257726 76742206 52387171 447612751 394711133 105288839 311036506 188963231 299331944 200667...
output:
449997854
result:
ok 1 number(s): "449997854"
Test #18:
score: 0
Accepted
time: 23ms
memory: 20668kb
input:
1 100000 384625660 115374409 225960109 274039535 178741365 321258128 170591713 329407412 308060861 191938282 220134970 279865276 213943533 286056786 103518048 396481052 207461712 292537600 252876866 247123499 351804867 148195958 64016058 435984211 52872452 447126785 86817782 413183092 220795830 2792...
output:
449994089
result:
ok 1 number(s): "449994089"
Test #19:
score: 0
Accepted
time: 13ms
memory: 21264kb
input:
1 100000 77035514 422964491 178817513 321182493 158098643 341901347 288748161 211251852 376028292 123971728 195670056 304329924 148630742 351369259 101439376 398560643 50630609 449369410 78580086 421419917 446984671 53015338 426980710 73019271 272161740 227838245 322523473 177476546 445486159 545138...
output:
449991329
result:
ok 1 number(s): "449991329"