QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#296710 | #4055. 整数序列 | -Ofast | 100 ✓ | 3738ms | 66624kb | C++14 | 2.7kb | 2024-01-03 14:21:56 | 2024-01-03 14:21:56 |
Judging History
answer
#include <bits/stdc++.h>
#define mp make_pair
#define fir first
#define sec second
#define mt make_tuple
#define ll long long
using namespace std;
const int N=3e5+10;
const ll inf=1e18;
int n,q,a[N],b[N],x,y,lg,block;
int nxt[N],lst[N],sz[N],st[N][18];
map <pair<int,int>,ll> ans;
ll tab[N<<1];ll calc(vector <pair<int,int>> s){
int x_y=0;ll res=-inf,sum=0;vector <int> bin;
tab[x_y+n]=0;bin.push_back(x_y+n);
for(auto it:s){
sum+=it.fir;x_y+=it.sec;
res=max(res,sum-tab[x_y+n]);
tab[x_y+n]=min(tab[x_y+n],sum);
bin.push_back(x_y+n);
}for(auto x:bin)tab[x]=inf;
return res;
}
set <int> S[N];
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>q;lg=log2(n)+1;
block=sqrtl((1ll*n*n)/(1ll*q*log2(n)))+1;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
for(int i=0;i<=(n<<1);i++)tab[i]=inf;
for(int i=n;i>=1;i--){
nxt[i]=lst[a[i]];
st[i][0]=nxt[i];
for(int j=1;j<=lg;j++)
st[i][j]=st[st[i][j-1]][j-1];
lst[a[i]]=i;sz[a[i]]++;
S[a[i]].insert(i);
}
for(int i=1;i<=n;i++){
if(sz[i]<block)continue;
for(int j=1;j<=n;j++){
if(i==j||sz[j]<block)continue;
x=i,y=j;vector <pair<int,int>> in;
int p1=lst[x],p2=lst[y];
while(p1&&p2)
if(p1<p2){in.push_back(mp(b[p1],1));p1=nxt[p1];}
else{in.push_back(mp(b[p2],-1));p2=nxt[p2];}
while(p1)in.push_back(mp(b[p1],1)),p1=nxt[p1];
while(p2)in.push_back(mp(b[p2],-1)),p2=nxt[p2];
ans[mp(x,y)]=calc(in);
}
}
while(q--){
cin>>x>>y;
if(ans.count(mp(x,y))){cout<<ans[mp(x,y)]<<"\n";continue;}
if(ans.count(mp(y,x))){cout<<ans[mp(y,x)]<<"\n";continue;}
if(sz[x]>sz[y])swap(x,y);
int cnt=0,pos=lst[y];
vector <pair<int,pair<int,int>>> seq;
vector <pair<int,int>> in;
vector <int> undo;
//cout<<"BEGIN"<<endl;
for(int i=lst[x];i;i=nxt[i]){
//cout<<i<<endl;
++cnt;
seq.push_back(mp(i,mp(b[i],1)));
//cout<<"YES"<<endl;
if((pos&&pos<nxt[i])||!nxt[i]){
auto it=S[y].lower_bound(i);
for(int j=1;j<=cnt+1;j++){
if(it==S[y].end())break;
seq.push_back(mp(*it,mp(b[*it],-1)));
undo.push_back(*it);S[y].erase(it);
it=S[y].lower_bound(i);
}
it=S[y].lower_bound(i);
if(it==S[y].begin())continue;it--;
for(int j=1;j<=cnt+1;j++){
seq.push_back(mp(*it,mp(b[*it],-1)));
undo.push_back(*it);S[y].erase(it);
it=S[y].lower_bound(i);
if(it==S[y].begin())break;it--;
}
cnt=0;pos=nxt[pos];
}
}
//cout<<"END"<<endl;
for(auto it:undo)S[y].insert(it);
sort(seq.begin(),seq.end());
for(auto it:seq)in.push_back(it.sec);
//for(auto it:in)
// cout<<it.fir<<" "<<it.sec<<"\n";
cout<<calc(in)<<"\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 20ms
memory: 25748kb
input:
5000 4999 1242 1550 3974 121 3049 2220 2031 3596 4083 346 2893 1242 2610 4935 4633 4195 4937 3783 1241 1867 4633 3519 660 4861 3616 1628 4633 3519 137 2347 4634 1242 1469 781 1632 1051 3147 288 3154 4633 1993 288 780 1242 4086 4312 4924 328 137 3154 2738 756 1952 98 1524 3974 3072 3519 3346 1242 165...
output:
1654077332 586562376 1647677275 9480498939 4337498611 2321392596 684160122 2961273810 3652261363 5768409888 2141765733 3446798022 1013520529 2585196873 1880556733 1082439832 2973800608 3395319881 -719609264 3650012737 1335753656 2356743437 2467991607 2774173366 1830795793 4127732589 1953952920 19319...
result:
ok 4999 numbers
Test #2:
score: 5
Accepted
time: 19ms
memory: 24476kb
input:
4999 5000 3440 4463 103 3377 2009 1045 2434 2920 2434 1564 2870 2732 749 1324 1178 4353 2127 1161 2482 853 1835 1830 3673 294 4750 4809 676 676 1374 1324 4039 2870 3962 788 4839 3204 1324 3440 3220 703 3440 391 2870 221 3440 4655 1461 2097 2612 4253 1865 2275 4011 3395 2612 829 1559 4216 2434 2434 1...
output:
2431371641 3504197188 2801830916 2708894780 2142511353 3560512545 1488249705 3944797080 7305163029 813723164 3307776497 1517843290 1767435435 2607802220 1042973982 3222063247 1375218761 1983436411 3991174125 1011628113 1482720392 1752276707 1867793702 2912803142 1586320530 1874783255 -532744293 5437...
result:
ok 5000 numbers
Test #3:
score: 5
Accepted
time: 16ms
memory: 27076kb
input:
5000 4999 721 2854 978 3366 2366 2071 3792 3235 3235 4826 3204 2352 2296 3204 2038 3159 3204 507 2366 1665 2338 2504 2247 3342 4826 4826 3456 1200 3204 2366 447 4608 1300 447 109 1933 162 3226 3258 74 3215 2951 2410 2262 721 4826 2808 1157 1180 1316 2421 481 3230 1316 3056 2359 322 3204 2262 3204 59...
output:
4168530749 3301336051 1328365521 1631901291 2469627282 4155007438 1208484545 2171379787 2301433988 3151307956 1580471032 2848621793 -134937345 1695300151 1403551326 872676430 1711924639 52205391 3566349635 1914704044 2818195255 4948501430 1477693277 1325417496 1180193991 2432291844 -343079851 460560...
result:
ok 4999 numbers
Test #4:
score: 5
Accepted
time: 20ms
memory: 24460kb
input:
4999 5000 1169 743 965 2404 2438 551 4692 337 743 1866 2453 743 1866 1157 3204 2181 1311 4867 3139 56 955 4635 3301 2569 1067 700 1866 1165 4224 743 3620 2360 689 4746 1177 1157 2569 4357 4765 1043 2601 1169 1866 4044 4224 4727 743 743 3191 700 1675 1866 551 3301 4224 4217 2204 3735 1871 689 743 700...
output:
3238411667 422996002 3009692904 2023457310 1697128767 1732840951 -771798243 -147296037 1663332547 -145057279 3153350823 656351788 1141898611 5198597934 1219203999 3609030959 2021751936 -192679403 -743424200 2503805266 3157480950 1801414071 8251501917 3994954212 2265920218 2263046979 1152268009 17149...
result:
ok 5000 numbers
Test #5:
score: 5
Accepted
time: 2261ms
memory: 64596kb
input:
300000 1000000 210616 109093 194161 18266 74476 222692 179435 112558 155417 194161 60442 219569 121583 64920 210616 119012 79775 84215 18266 109093 18266 197546 74044 194161 162053 233329 119012 74476 8047 126329 194161 201328 189361 194161 112200 99319 74044 201328 289127 119012 179435 283375 19512...
output:
4219197017 4554888226 4604171163 32314484666 26764270945 4857537785 4127652115 1012704318 3951500069 26194054668 3828302916 4805755198 3967375531 25497531513 3805726339 3979250323 24474967319 751331816 43214940859 53455936977 27815310752 3427954635 36207955009 3967778826 939635489 827247600 46014411...
result:
ok 1000000 numbers
Test #6:
score: 5
Accepted
time: 2190ms
memory: 64528kb
input:
299999 1000000 215543 183462 57241 150146 75108 2589 28359 104656 85936 49139 272845 26114 183462 92642 110658 2589 218311 231610 144686 2589 161638 92555 130256 91926 28344 272662 51005 105130 131618 89779 28359 28420 172552 218311 92555 131618 144364 188255 2589 215543 57241 110658 25236 28344 131...
output:
2510540872 1052440958 27314264168 4304554906 1772422921 1324939526 1611336175 4846424545 -31509146 4291394952 7232630075 821140187 4916538807 5136416553 46596960029 35441936879 1360949254 4702622729 4139838277 142852489392 4195091059 21679319871 631027572 4281270365 923942862 45177923 5396629188 364...
result:
ok 1000000 numbers
Test #7:
score: 5
Accepted
time: 919ms
memory: 49736kb
input:
150000 500000 132436 8515 14753 132436 44442 132436 63224 132436 132436 132436 10776 132436 132436 132436 106000 132436 132436 138973 18627 139106 106971 132436 132436 126363 135723 319 76472 10220 132436 132436 132436 132436 63083 48327 132436 81505 132436 132436 148574 132436 132436 132436 132436 ...
output:
1068142142 1655225246 530261633 1125887539 1315380311 1337243549 610772435 855266778 1857990366 992821984 995555499 521203661 1324305675 1218395792 1595023708 897045138 868747680 966790047 1125459045 1075768980 1697552843 1072385442 1172448570 824034058 1877789598 930008455 993214893 1153351012 1254...
result:
ok 500000 numbers
Test #8:
score: 5
Accepted
time: 936ms
memory: 48088kb
input:
150000 500000 5202 5202 5202 36166 5202 5202 95724 5202 5202 92605 5202 5202 5202 5202 64052 5202 5202 51686 5202 94344 147952 134686 124500 5202 5202 5202 5202 5202 5202 5202 5202 19171 5202 107149 5202 10457 92605 10677 5202 5202 5202 90631 5202 5202 5202 5202 5202 5202 5202 97189 64052 16557 5202...
output:
1282956208 1203392879 1680347551 997677571 1377920509 1516225244 1473045154 1085598608 1115976311 1189267746 1673860056 1103464182 1794803263 1166468498 1520532888 1635882851 504249763 1561528057 1760976523 589795308 798753505 1597116188 672593941 658927711 1682713789 1177387154 1054444159 155643411...
result:
ok 500000 numbers
Test #9:
score: 5
Accepted
time: 951ms
memory: 46928kb
input:
149999 500000 130539 130539 130539 135015 130539 68088 130539 135629 130539 130539 130539 130539 130539 129686 145610 73567 52505 130539 130539 130539 14931 38544 107261 130539 130539 51142 14931 68088 130539 35668 130539 130539 106082 61754 130539 65537 130539 130539 69769 130539 130539 20244 10074...
output:
-205375765 905737478 343029078 -794101853 -372946727 1417276813 139227247 1244737498 684855474 772266018 -137157671 -523066024 -776434262 391992090 -711699607 263771501 -883411004 736170849 -812687906 8107439 -7967946 2676142 814862405 -698311969 556234064 118261600 227871113 567566156 -407456736 82...
result:
ok 500000 numbers
Test #10:
score: 5
Accepted
time: 1027ms
memory: 55728kb
input:
200000 500000 18780 73334 73334 129506 73334 28788 73334 73334 73334 73334 26535 36488 73334 73334 73334 121456 129506 119075 190638 142411 111475 73334 73334 91293 73334 73334 73334 73334 73334 73334 184692 178784 118264 129506 116620 118404 122852 36488 73334 163977 73334 50087 10210 199390 73334 ...
output:
971690580 -132332974 966677117 723225128 1037957768 81976352 -804939046 387359431 200373975 187313052 1081997501 1727973106 329734194 1193756984 1148436182 819398796 903789164 698957212 744640841 831928686 425471994 1428837853 310717747 -628763800 547556528 219345415 -108145284 296341615 374366606 6...
result:
ok 500000 numbers
Test #11:
score: 5
Accepted
time: 1047ms
memory: 55420kb
input:
200000 500000 117561 117561 108780 140071 76023 163608 147648 117561 117561 117561 117561 117561 146331 117561 117561 50485 117561 117561 117561 147648 169614 117561 117561 117561 117561 117561 134029 161477 117561 117561 74827 99188 117561 117561 117561 117561 186557 117561 117561 159944 50485 1244...
output:
745022908 860518205 1902958326 -621871139 1491002751 775652860 951785915 -552159248 783161954 -19316674 578016403 746633410 1023310836 -1013840733 343876335 551382277 1049314326 1053196641 -1440039327 339730352 920268812 1003571322 811198922 -707732826 1645132939 725222914 -1110765528 1669828024 104...
result:
ok 500000 numbers
Test #12:
score: 5
Accepted
time: 3648ms
memory: 66624kb
input:
300000 1000000 56219 143695 226286 249694 160309 135267 279081 101944 14091 99207 27252 286872 97823 160309 153937 193629 195292 241817 48132 241817 191469 52905 239169 295659 88239 133799 133799 8234 50907 228407 95709 268342 241817 296967 135267 116559 100835 104182 39121 71692 191469 133799 24181...
output:
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1492 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 674 2 2 2 2 2 2 2 2 2 2 2 2 2 1154 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...
result:
ok 1000000 numbers
Test #13:
score: 5
Accepted
time: 3489ms
memory: 66216kb
input:
300000 1000000 252427 92158 13474 286432 38970 155012 77876 141812 211941 90991 298722 187008 281303 130407 151235 239285 180713 183820 178795 151235 151161 286432 13474 126097 182453 225946 224795 126021 140814 123569 212222 183820 151235 248332 134648 27062 100877 3515 276112 138511 212687 13474 4...
output:
2 1240 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1076 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1150 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 742 2 2 2 2 2 2 2 2 2 2...
result:
ok 1000000 numbers
Test #14:
score: 5
Accepted
time: 3532ms
memory: 66092kb
input:
299999 1000000 102271 20892 168471 178536 165808 72575 225025 79470 180501 268666 197798 145975 243581 255187 142045 197006 30081 242593 83648 170294 86352 78447 177452 275062 165294 113565 142413 293611 280979 4033 272066 195915 122234 86343 79470 261560 26913 221328 168471 78447 18922 296613 10213...
output:
2 2 2 2 2 2 2 2 2 1696 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1456 2 2 2 2 2 2 2 2 2 1282 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2...
result:
ok 1000000 numbers
Test #15:
score: 5
Accepted
time: 3700ms
memory: 66308kb
input:
299999 1000000 151289 14535 244254 290111 288090 130841 27455 106184 47286 236908 173179 159601 134454 134838 14535 203110 92926 274608 87246 215015 245740 295574 123490 290111 134838 216821 114378 188238 134454 188365 234630 2226 97676 134838 290111 2226 44319 241093 115893 134838 43192 179884 1385...
output:
1797459939 985671941 941452126 1782196886 1866835971 721478314 1045518973 1014980449 536872434 1197107393 644352434 534299024 1553554511 574432101 1392545607 1675059596 1676658792 540692711 1487788721 1731935448 1196949730 1095226604 1850129899 1587856203 1343694231 1869588093 889658402 1078778339 8...
result:
ok 1000000 numbers
Test #16:
score: 5
Accepted
time: 3738ms
memory: 66220kb
input:
300000 1000000 147825 195767 150390 77132 239532 278774 160659 276921 115387 7629 183295 110547 290985 115387 91975 103610 40841 207522 145488 91975 111180 16525 178046 16950 295408 243529 247392 267715 278774 217808 16525 115387 194968 91975 77824 139035 253468 295408 32471 153777 237234 104953 147...
output:
1056898397 1494774202 764686467 1802515615 1118898045 895101530 1806203132 1569249044 1674734546 1135785594 1425678429 708727426 1664586653 551380170 497100810 569405192 367812642 1877980394 1209400814 693196409 862167660 1775903718 1512751710 945023696 1498334230 1412647019 732910321 1551363584 939...
result:
ok 1000000 numbers
Test #17:
score: 5
Accepted
time: 3669ms
memory: 65968kb
input:
300000 1000000 92527 298213 72198 196854 158708 76270 195790 236817 208173 208173 71266 17759 29102 195790 247368 262696 97153 164699 97153 106281 257441 210052 195790 111950 239957 179014 150594 241963 112387 273448 151221 150594 208173 136209 129633 46494 46494 84383 72786 162998 46494 46494 17291...
output:
862648951 1363852765 1659791933 99971440 429574042 683444366 -452120779 -597153403 1841185448 541280567 409696837 -300647507 964973534 902734492 1286402035 -250141320 529763068 1047349568 -244608998 -298684874 -201866351 -736545511 1543833090 871008434 -1401082460 38498407 358109019 1417287649 45640...
result:
ok 1000000 numbers
Test #18:
score: 5
Accepted
time: 3563ms
memory: 65964kb
input:
299999 1000000 174942 205062 214513 219196 259901 19736 239253 95131 119460 179415 8195 48699 123674 252242 179415 204410 61013 276817 145744 121754 204410 182272 255628 274322 137057 82468 231315 122482 222185 63411 123674 186416 161540 77373 206186 23868 112918 123674 95131 236420 204496 178859 11...
output:
447181519 79110342 947325822 1154302346 507597669 1060767435 -128563111 -183358887 1510519798 928922373 442348498 265223729 28954071644 958445424 1503945196 1638566794 522437485 375277949 18304249 -698496814 651704972 -87719654 497701625 258597706 21922449 546543594 16697942978 1723674021 -122965113...
result:
ok 1000000 numbers
Test #19:
score: 5
Accepted
time: 3693ms
memory: 66240kb
input:
300000 1000000 229819 100462 229819 78236 229819 249816 70315 167375 157866 183526 100462 116410 105610 50013 71906 100462 1512 168934 147968 58495 52997 92028 291180 30449 218217 299205 90916 100462 69006 290092 36728 261594 1512 142526 228482 1512 272954 1512 142691 123023 1512 47880 50013 76498 1...
output:
-357819734 -197642007 1522128972 1233700461 1871725770 451292382 -1163727133 1768275887 30723866 238953033 808073317 -669542485 538804808 791893991 14936480569 430434743 1471501624 -660365223 24826852 312851967 1205813740 370086678 1589694132 983264009 -145465623 -899113192 -435326923 735454104 1287...
result:
ok 1000000 numbers
Test #20:
score: 5
Accepted
time: 3606ms
memory: 66184kb
input:
300000 1000000 135822 88561 243121 279557 10947 146084 66559 172695 40359 288955 234013 187141 288955 187141 271817 84283 208724 139740 288955 266331 15885 59084 225069 124976 187141 103698 288955 187141 92132 238696 291003 23394 40340 109700 88561 86940 51845 266331 255230 288955 229039 239891 1871...
output:
192239551 59055443 728315057 62311328 964858830 495623584 -937147835 413304009 -948173024 968706285 -471430510 452578907 87265180 673532122 -103742169 253148703 948400119 -471130538 261208368 -752676521 -746350668 1080703044 1146411605 1641897014 1056975712 33028076773 352437386 -448126049 54465551 ...
result:
ok 1000000 numbers