QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#278158 | #6420. Certain Scientific Railgun | gao_yc | AC ✓ | 51ms | 17088kb | C++14 | 4.7kb | 2023-12-07 13:16:50 | 2023-12-07 13:16:51 |
Judging History
answer
#include<bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define ll long long
using namespace std;
const int N=1e5+10;
int n,bl[N],br[N];
struct node{
int x,y;
}a[N];
pair<int,int> yl[N],yr[N];
ll ans;
int val[N];
ll t1[N<<2],t2[N<<2],tag[N<<2];
void upd(int x,int k){t1[x]+=k;t2[x]+=k;tag[x]+=k;}
void pushdown(int x){if(tag[x]){upd(ls,tag[x]);upd(rs,tag[x]);tag[x]=0;}}
void pushup(int x){t1[x]=min(t1[ls],t1[rs]);t2[x]=min(t2[ls],t2[rs]);}
void build(int x,int l,int r){
tag[x]=0;
if(l==r){t1[x]=val[l]+br[l-1];t2[x]=val[l]+br[l-1]*2;return;}
int mid=(l+r)>>1;
build(ls,l,mid);build(rs,mid+1,r);
pushup(x);
}
void modify(int x,int l,int r,int L,int R,int k){
if(L>R) return;
if(L<=l&&r<=R) return upd(x,k);
int mid=(l+r)>>1;pushdown(x);
if(L<=mid) modify(ls,l,mid,L,R,k);
if(mid<R) modify(rs,mid+1,r,L,R,k);
pushup(x);
}
int st1[N],tp1,st2[N],tp2;
int main(){
int T;scanf("%d",&T);
while(T--){
scanf("%d",&n);
int nl=0,nr=0;ans=1e16;
for(int i=1;i<=n;++i){
scanf("%d%d",&a[i].x,&a[i].y);
if(a[i].x<0) bl[++nl]=-a[i].x;
if(a[i].x>0) br[++nr]=a[i].x;
}
sort(bl+1,bl+nl+1);sort(br+1,br+nr+1);
nl=unique(bl+1,bl+nl+1)-bl-1;nr=unique(br+1,br+nr+1)-br-1;
for(int i=1;i<=n;++i){
if(a[i].x<0)
a[i].x=lower_bound(bl+1,bl+nl+1,-a[i].x)-bl,yl[a[i].x].first=max(yl[a[i].x].first,a[i].y),yl[a[i].x].second=min(yl[a[i].x].second,a[i].y);
else if(a[i].x>0)
a[i].x=lower_bound(br+1,br+nr+1,a[i].x)-br,yr[a[i].x].first=max(yr[a[i].x].first,a[i].y),yr[a[i].x].second=min(yr[a[i].x].second,a[i].y);
}
for(int i=nr;i;--i) yr[i].first=max(yr[i].first,yr[i+1].first),yr[i].second=min(yr[i].second,yr[i+1].second);
if(n<=100){
for(int i=nl;i;--i) yl[i].first=max(yl[i].first,yl[i+1].first),yl[i].second=min(yl[i].second,yl[i+1].second);
for(int i=1;i<=nl+1;++i)
for(int j=1;j<=nr+1;++j){
int xl=bl[i-1],xr=br[j-1],ya=max(yl[i].first,yr[j].first),yi=min(yl[i].second,yr[j].second);
if(xl>xr) swap(xl,xr);
if(ya+yi>0) swap(ya,yi),ya=-ya,yi=-yi;
ans=min(ans,1ll*2*xl+xr+2*ya-yi);
}
for(int i=1;i<=nr;++i) yr[i]={0,0};
for(int i=1;i<=nl;++i) yl[i]={0,0};
printf("%lld\n",ans);
continue;
}
val[nr+1]=0;
tp1=tp2=0;
for(int i=1;i<=nr;++i){
val[i]=1ll*2*yr[i].first-yr[i].second;
if(yr[i].first>yr[i+1].first) st1[++tp1]=i;
if(yr[i].second<yr[i+1].second) st2[++tp2]=i;
}
build(1,1,nr+1);
ans=min(ans,min(t1[1]+2*bl[nl],t2[1]+bl[nl]));
for(int i=nl,ny1=0,ny2=0;i;--i){
if(ny1<yl[i].first){
for(;tp1&&yr[st1[tp1]].first<=yl[i].first;--tp1) modify(1,1,nr+1,st1[tp1]+1,nr+1,2*(yr[st1[tp1]].first-ny1)),ny1=yr[st1[tp1]].first;
modify(1,1,nr+1,st1[tp1]+1,nr+1,2*(yl[i].first-ny1));
ny1=yl[i].first;
}
if(ny2>yl[i].second){
for(;tp2&&yr[st2[tp2]].second>=yl[i].second;--tp2) modify(1,1,nr+1,st2[tp2]+1,nr+1,ny2-yr[st2[tp2]].second),ny2=yr[st2[tp2]].second;
modify(1,1,nr+1,st2[tp2]+1,nr+1,ny2-yl[i].second);
ny2=yl[i].second;
}
ans=min(ans,min(t1[1]+2*bl[i-1],t2[1]+bl[i-1]));
}
tp1=tp2=0;
for(int i=1;i<=nr;++i){
val[i]=1ll*yr[i].first-2*yr[i].second;
if(yr[i].first>yr[i+1].first) st1[++tp1]=i;
if(yr[i].second<yr[i+1].second) st2[++tp2]=i;
}
build(1,1,nr+1);
ans=min(ans,min(t1[1]+2*bl[nl],t2[1]+bl[nl]));
for(int i=nl,ny1=0,ny2=0;i;--i){
if(ny1<yl[i].first){
for(;tp1&&yr[st1[tp1]].first<=yl[i].first;--tp1)
modify(1,1,nr+1,st1[tp1]+1,nr+1,yr[st1[tp1]].first-ny1),ny1=yr[st1[tp1]].first;
modify(1,1,nr+1,st1[tp1]+1,nr+1,yl[i].first-ny1);
ny1=yl[i].first;
}
if(ny2>yl[i].second){
for(;tp2&&yr[st2[tp2]].second>=yl[i].second;--tp2) modify(1,1+1,nr+1,st2[tp2]+1,nr+1,2*(ny2-yr[st2[tp2]].second)),ny2=yr[st2[tp2]].second;
modify(1,1,nr+1,st2[tp2]+1,nr+1,2*(ny2-yl[i].second));
ny2=yl[i].second;
}
ans=min(ans,min(t1[1]+2*bl[i-1],t2[1]+bl[i-1]));
}
for(int i=1;i<=nl;++i) yl[i]={0,0},bl[i]=0;
for(int i=1;i<=nr;++i) yr[i]={0,0},br[i]=0,val[i]=0;
printf("%lld\n",ans);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 8052kb
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: 2ms
memory: 8044kb
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: 29ms
memory: 17088kb
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: 33ms
memory: 16088kb
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: 23ms
memory: 16440kb
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: 26ms
memory: 16468kb
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: 31ms
memory: 16756kb
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: 22ms
memory: 10544kb
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: 19ms
memory: 14156kb
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: 18ms
memory: 12332kb
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: 27ms
memory: 14296kb
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: 29ms
memory: 14232kb
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: 6ms
memory: 8056kb
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: 51ms
memory: 16900kb
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: 42ms
memory: 16812kb
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: 48ms
memory: 16784kb
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: 46ms
memory: 16996kb
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: 32ms
memory: 16804kb
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: 27ms
memory: 17044kb
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"