QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#315030 | #2711. Loop Town | Crysfly🌈 | 25 ✓ | 1324ms | 134952kb | C++17 | 2.5kb | 2024-01-26 19:31:23 | 2024-01-26 19:31:23 |
Judging History
answer
// what is matter? never mind.
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define ull unsigned long long
#define int long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 4000005
#define inf 0x3f3f3f3f
struct bit{
vector<int>tr;
int n;
void init(int N){n=N,tr=vector<int>(N+1,0);}
void add(int x,int y){for(;x<=n;x+=x&-x)tr[x]+=y;}
int ask(int x){
int s=0;
for(;x;x^=x&-x)s+=tr[x];
return s;
}
void down(){For(i,1,n)tr[i]+=tr[i^(i&-i)];}
}tr;
int n,m,L;
int t[maxn],k;
struct node{
int l,r,op,id;
}a[maxn];
int sum,res;
int val[maxn],s1[maxn],s2[maxn];
int st[maxn],tp;
signed main()
{
n=read(),L=read();
For(i,1,n){
int l=read(),r=read();
if(l<r) a[++m]={l,r,1,i},a[++m]={l+L,r+L,0,i};
else a[++m]={l,r+L,1,i};
}
For(i,1,m) t[++k]=a[i].l,t[++k]=a[i].r;
sort(t+1,t+k+1),k=unique(t+1,t+k+1)-t-1;
#define V(x) lower_bound(t+1,t+k+1,x)-t
For(i,1,m) a[i].l=V(a[i].l),a[i].r=V(a[i].r);
sort(a+1,a+m+1,[&](node x,node y){
return x.l<y.l;
});
// For(i,1,m)cout<<a[i].l<<" "<<a[i].r<<" "<<a[i].id<<"\n";
tr.init(k);
int all=0;
For(i,1,m){
val[a[i].id]+=all-tr.ask(a[i].r);
if(a[i].op) tr.add(a[i].r,1),++all;
}
For(i,1,n)sum+=val[i];
// cout<<"sum "<<sum<<"\n";
tr.init(k);
Rep(i,m,1){
if(a[i].op) val[a[i].id]-=tr.ask(a[i].r);
tr.add(a[i].r,1);
}
// For(i,1,n)cout<<val[i]<<" \n"[i==n];
sort(val+1,val+n+1);
For(i,1,n)s1[i]=s1[i-1]+val[i]*2,s2[i]=s2[i-1]-val[n-i+1]*2;
// For(i,0,n)
// For(j,0,n-i)
// res=min(res,2*i*j+i*(n-i)+j*(n-j)+s1[i]+s2[j]);
For(i,1,n)s1[i]+=i*(n-i),s2[i]+=i*(n-i);
auto slope=[&](int i,int j){
return 1.0l*(s2[i]-s2[j])/(j-i);
};
For(i,0,n){
while(tp>=2 && slope(st[tp-1],st[tp])<=slope(st[tp],i))--tp;
st[++tp]=i;
}
auto F=[&](int i,int j){
return 2*i*j+s1[i]+s2[j];
};
int j=1;
Rep(i,n,0){
while(j<tp && F(i,st[j])>=F(i,st[j+1]))++j;
res=min(res,F(i,st[j]));
}
cout<<res+sum;
return 0;
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 12
Accepted
Test #1:
score: 12
Accepted
time: 1ms
memory: 7720kb
input:
3 100 10 50 30 20 60 40
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 1ms
memory: 7788kb
input:
4 100 30 70 10 12 60 75 90 50
output:
1
result:
ok single line: '1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 10084kb
input:
5000 1000000000 122673490 785087980 471383263 619641996 802868008 404957433 840100498 514750784 862185816 340264735 40401408 302319993 217914697 633645728 306953710 1370207 588914847 392017693 99375184 951900586 632042853 192334004 282625056 271661393 702036110 528038472 658508052 910479247 79240500...
output:
4136135
result:
ok single line: '4136135'
Test #4:
score: 0
Accepted
time: 0ms
memory: 8040kb
input:
5000 1000000000 753237767 446463910 269085397 481746304 191449188 824850246 903048923 288123314 106413855 317500737 143182615 539442043 958826519 814240231 987065953 474098959 337265966 234431527 130587819 936394050 194216056 819645384 780162258 611205676 43320659 382571837 700818515 285725052 98861...
output:
4140190
result:
ok single line: '4140190'
Test #5:
score: 0
Accepted
time: 2ms
memory: 8108kb
input:
5000 1000000000 910987381 398123645 61332235 933233104 978901219 704119884 437031586 62228609 775214338 609198512 919470443 378651691 686365097 268118665 918672380 293092230 814731455 406037937 209684695 84907661 673177395 871645909 825259655 549247076 897920980 170141072 254161971 414031249 9148137...
output:
4093985
result:
ok single line: '4093985'
Test #6:
score: 0
Accepted
time: 5ms
memory: 12152kb
input:
5000 1000000000 816412433 399564937 153914279 734772120 503521245 78710830 517602241 94187043 664737420 243518674 869525747 447961218 524878196 102602867 50828010 640346295 385326529 969914294 602319604 183311316 722827407 306851078 438106797 19290449 227766650 813927991 976994080 557839183 31253287...
output:
4408
result:
ok single line: '4408'
Test #7:
score: 0
Accepted
time: 4ms
memory: 8176kb
input:
5000 1000000000 28388137 764830661 91525934 701426454 681357016 132959481 447440752 361105518 900095008 903073523 866820402 937356600 478357741 328150290 24660968 769320836 623574736 193495679 251987871 547003586 362036678 441512804 140313706 649019530 42021552 752535636 716691328 96316088 438142041...
output:
6247500
result:
ok single line: '6247500'
Test #8:
score: 0
Accepted
time: 4ms
memory: 8152kb
input:
5000 1000000000 523895573 66240481 516502358 75668190 388043022 199009031 612996742 969750035 852258497 734222812 498327340 92733369 633852854 952531193 153408387 429605920 732152962 844521769 203285359 383697215 885794725 698269331 328349033 258091409 739736756 836626037 519461276 71347262 64973000...
output:
6243080
result:
ok single line: '6243080'
Subtask #2:
score: 6
Accepted
Dependency #1:
100%
Accepted
Test #9:
score: 6
Accepted
time: 90ms
memory: 22756kb
input:
100000 1000000000 982654643 951206885 971711003 649198658 330051411 736074437 281582567 198586473 495377222 121313883 323425615 55396062 223417630 503522632 118652799 221114190 152804501 548139222 799792579 83106179 295111245 583378463 177183223 701873391 806328282 658908645 666145312 366507184 9783...
output:
1663629920
result:
ok single line: '1663629920'
Test #10:
score: 0
Accepted
time: 89ms
memory: 21504kb
input:
100000 1000000000 865989725 299048685 136038136 668323264 300113335 640959942 802292014 53282056 690707272 13596850 823488334 542878424 303187506 771796286 155670080 148303398 980374695 20014811 880458456 701621776 375334350 769233500 317323342 39955723 709994062 802016842 537846778 421347345 704347...
output:
1663800649
result:
ok single line: '1663800649'
Test #11:
score: 0
Accepted
time: 86ms
memory: 20396kb
input:
100000 1000000000 842191447 790120117 986740612 878599217 653178196 138228415 706155642 933477644 278612123 629825051 776584983 912432889 769024218 704896961 901330959 529262221 337757984 136891414 719335773 745838613 586972792 883554291 929580638 694532569 433431350 427248662 860323882 749781746 37...
output:
1665032934
result:
ok single line: '1665032934'
Test #12:
score: 0
Accepted
time: 79ms
memory: 22660kb
input:
100000 1000000000 438112273 508349246 519182810 905815591 578839084 738919219 543981363 460118443 692437570 18587159 301939574 765577937 813202479 350772635 98943366 290349543 623660394 549732740 193804720 995867256 526787026 950773181 870295046 283744310 955279433 629163985 247577604 512550947 9355...
output:
1664379454
result:
ok single line: '1664379454'
Test #13:
score: 0
Accepted
time: 89ms
memory: 19776kb
input:
100000 1000000000 671440944 140561850 365777804 874752756 210790111 367274117 196421919 404358605 992525171 578371201 971003541 825284090 590397475 954933704 269482465 108602534 633252282 133934263 846893129 332988341 700703486 540718132 372357560 114818944 621680778 263405432 372406790 63779153 592...
output:
1663466587
result:
ok single line: '1663466587'
Test #14:
score: 0
Accepted
time: 78ms
memory: 25188kb
input:
100000 1000000000 61149808 292401826 102627456 334036178 406370501 633558900 122115154 353927159 284434748 512979802 698658680 927396343 84046192 315114368 585608387 813934116 437198502 664555215 695404947 924548933 899435319 130979731 625724602 854916584 784904084 13196470 757561972 986534634 84751...
output:
89642
result:
ok single line: '89642'
Test #15:
score: 0
Accepted
time: 80ms
memory: 21264kb
input:
100000 1000000000 473240419 951055506 319677683 105186528 459003660 965054951 138387860 284718733 390690029 33244800 356596313 67008719 571592336 852088980 459615566 964437137 999430718 424908652 434905744 989289412 947331275 477237575 99979414 323780651 639301560 785274825 225061066 197017486 31964...
output:
2499950000
result:
ok single line: '2499950000'
Test #16:
score: 0
Accepted
time: 88ms
memory: 21208kb
input:
100000 1000000000 920419775 940100024 173114274 689423875 152602360 709090134 672549098 189567342 876287882 984299877 911833627 948718993 620892932 241335589 923228047 937366016 216563431 646257958 720517708 140911080 430353622 431799434 69101370 793956292 805475877 56133770 243367403 619639577 3320...
output:
2499860232
result:
ok single line: '2499860232'
Subtask #3:
score: 7
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #17:
score: 7
Accepted
time: 1315ms
memory: 134840kb
input:
1000000 1000000000 790688400 946217800 572768031 243939831 288088971 669801067 910752802 434950979 564490112 352184641 375075334 733111902 156676898 698705953 80612108 655423283 60559455 62389668 476388506 911502344 9302025 561395145 808040563 720576055 608057114 668522649 855214390 96976995 5589535...
output:
166629377806
result:
ok single line: '166629377806'
Test #18:
score: 0
Accepted
time: 1309ms
memory: 134576kb
input:
1000000 1000000000 297553649 948355663 134595239 398113429 838681247 131365900 133486433 31523538 120773951 206629068 999713628 264981293 448341373 982535533 409782306 711549811 686575109 762860687 585277024 969698629 31639995 488913254 992081434 265622727 56864539 449328115 206376652 287461441 3416...
output:
166637625353
result:
ok single line: '166637625353'
Test #19:
score: 0
Accepted
time: 1293ms
memory: 134048kb
input:
1000000 1000000000 563208374 863933945 220086102 841308329 115613563 958314541 220376972 981145789 488789896 228266879 713997104 256476293 382881491 430248396 370125649 208288091 996772353 785105279 413721871 135880139 331796292 59550022 309259906 981191833 913538649 383816903 973012662 574810795 92...
output:
166542648261
result:
ok single line: '166542648261'
Test #20:
score: 0
Accepted
time: 1324ms
memory: 134212kb
input:
1000000 1000000000 67622995 242661223 693286671 160634549 134998475 892702612 544363723 724364810 823144782 183084657 260115102 176210172 459638607 676721463 680123303 913320142 493517820 363768957 623694227 736746930 560129147 886370178 370486045 811587175 648713331 732478189 989333122 123399545 46...
output:
166599688018
result:
ok single line: '166599688018'
Test #21:
score: 0
Accepted
time: 1308ms
memory: 133584kb
input:
1000000 1000000000 246380766 195603088 734657934 916312206 241572284 627878247 911418946 154634668 824330033 146551503 286976306 444878291 941504029 13657218 88377337 7330047 192293258 558481192 996671741 475745630 563693222 960627938 287246371 888671814 918504530 398640076 37960602 986574907 468858...
output:
166600291317
result:
ok single line: '166600291317'
Test #22:
score: 0
Accepted
time: 1322ms
memory: 133308kb
input:
1000000 1000000000 998321106 774515791 259114233 469187573 870519487 887530354 362352434 535079643 20761010 150050756 802524212 755378091 326692271 471490014 161190335 516108053 710140099 92457801 569580114 315119834 752424432 408542974 238457165 754078934 581079276 841361946 737670679 901202108 230...
output:
166612431279
result:
ok single line: '166612431279'
Test #23:
score: 0
Accepted
time: 1302ms
memory: 134280kb
input:
1000000 1000000000 356104814 965678273 923643596 359983366 115011404 906337903 107055452 488338232 713533430 469083559 674082084 895983516 829619568 207518960 315069927 478774569 963250956 616065676 308645706 216034694 748590898 695927844 692843853 456841072 219194255 214679102 264884427 22533318 66...
output:
166621243812
result:
ok single line: '166621243812'
Test #24:
score: 0
Accepted
time: 822ms
memory: 106892kb
input:
1000000 1000000000 292773205 138614909 434323513 280768199 249582627 95360300 38668325 884950576 962106131 808042005 748588178 594593573 18143839 864419096 731391729 577586440 686766427 533418965 762905951 608593779 162590001 9078285 8024741 854170594 154897413 1434372 458227030 305025720 157345797 ...
output:
896156
result:
ok single line: '896156'
Test #25:
score: 0
Accepted
time: 1172ms
memory: 134952kb
input:
1000000 1000000000 327314242 969149189 82268774 213374084 498784072 797203475 925221179 370880488 165820374 130313831 822268759 473909753 545835651 750420494 438013566 858069817 399750244 895994232 357071009 939448372 355836688 940734294 501171433 794799544 734460623 561966966 798365869 497953124 86...
output:
249999500000
result:
ok single line: '249999500000'
Test #26:
score: 0
Accepted
time: 1201ms
memory: 131880kb
input:
1000000 1000000000 232503412 591977348 222744191 601647269 455224978 370479336 863355847 961023877 303515308 521902112 248525402 576517388 994954045 829433293 61213593 763403402 479632792 345858790 919259642 905467000 161266567 663083252 176700093 647801984 850392034 974190956 933305679 891411060 36...
output:
249998603580
result:
ok single line: '249998603580'
Extra Test:
score: 0
Extra Test Passed