QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#71722#4055. 整数序列feecle6418100 ✓1169ms64956kbC++144.0kb2023-01-11 21:43:052023-01-11 21:43:06

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-11 21:43:06]
  • 评测
  • 测评结果:100
  • 用时:1169ms
  • 内存:64956kb
  • [2023-01-11 21:43:05]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
const int mod=998244353,D=300000;
const ll inf=1e18;
struct Query{
	int x,id;
};
int n,q,a[600005],b[600005],s[600005],pos[600005],vst[600005],used[600005];
int isused[600005];
ll sum[600005],ans[1000005],mns[600005],_ans[600005],_occ[600005],prea[600005];
vector<int> occ[300005];
vector<Query> largeq[300005];
int p1=2000000,p2=0;
char buf[2000005],wbuf[2000005];
int gc(){
	if(p1>=2000000)fread(buf,1,2000000,stdin),p1=0;
	return buf[p1++];
}
int rd(){
	int x=0,ff=1;
	char ch=gc();
	while((ch<'0'||ch>'9')&&ch!='-')ch=gc();
	if(ch=='-')ff=-1,ch=gc();
	while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=gc();
	return x*ff;
}
void pc(char x){
	if(p2==2000000)fwrite(wbuf,1,p2,stdout),p2=0;
	wbuf[p2++]=x;
}
void wrt(ll x){
	if(x<0)return pc('-'),wrt(-x);
	if(!x)return pc('0');
	int t[20]={0};
	while(x)t[++t[0]]=x%10,x/=10;
	while(t[0])pc(t[t[0]--]+'0');
}
int main(){
	n=rd(),q=rd();
	const int B=max((int)sqrt(n),10);
	for(int i=1;i<=n;i++)a[i]=rd(),occ[a[i]].push_back(i);
	for(int i=1;i<=n;i++)b[i]=rd();
	for(int i=1,x,y;i<=q;i++){
		x=rd(),y=rd();
		if(occ[x].size()>occ[y].size())swap(x,y);
		largeq[y].push_back({x,i}),ans[i]=-inf;
	}
	fill(mns,mns+D+D+1,inf);
	for(int i=1;i<=n;i++){
		if(!largeq[i].size())continue;
		if(occ[i].size()>B){
			for(int j=1;j<=n;j++){
				s[j]=s[j-1]+(a[j]==i),sum[j]=sum[j-1]+(a[j]==i?b[j]:0),vst[j]=0;
				if(a[j]==i)_occ[s[j]]=j;
			}
			for(Query j:largeq[i]){
				int x=j.x;
				if(vst[x]){
					ans[j.id]=_ans[x];
					continue;
				}
				int lst=0;
				for(int k:occ[x])prea[k]=prea[lst]+b[k],lst=k;
				vst[x]=1,_ans[x]=-inf,used[0]=0;
				int L=-s[n]+(int)occ[x].size(),R=L,curs=L;
				lst=n+1;
				//反着来 
				for(int p=occ[x].size()-1;p>=0;p--){
					int k=occ[x][p];
					//printf("backward:%d\n",k);
					int o=s[lst-1]-s[k],t=s[lst-1];
					while(o&&curs+1<=R){
						curs++,t--,o--;
						mns[curs+D]=sum[_occ[t]]+prea[k];
						if(!isused[curs+D])used[++used[0]]=curs+D,isused[curs+D]=1;
					}
					curs+=o,R=max(R,curs),curs--,lst=k;
					if(curs<L){
						L=curs;
						continue;
					}
					mns[curs+D]=sum[k]+prea[k]-b[k];
					//cout<<"B:"<<curs<<' '<<mns[curs+D]<<'\n';
					if(!isused[curs+D])used[++used[0]]=curs+D,isused[curs+D]=1;
				}
				int o=s[lst-1],t=s[lst-1];
				while(o&&curs+1<=R){
					curs++,t--,o--;
					mns[curs+D]=sum[_occ[t]];
					if(!isused[curs+D])used[++used[0]]=curs+D,isused[curs+D]=1;
				}
				L=0,R=0,lst=0,curs=0; 
				//正着来 
				for(int k:occ[x]){
					int o=s[k]-s[lst],t=s[lst];
					while(o&&curs-1>=L){
						curs--,t++,o--;
						_ans[x]=max(_ans[x],sum[_occ[t]]+prea[lst]-mns[curs+D]);
						mns[curs+D]=min(mns[curs+D],sum[_occ[t]]+prea[lst]);
						assert(isused[curs+D]);
					}
					curs-=o,L=min(L,curs),curs++,lst=k;
					if(curs>R){
						R=curs;
						continue;
					}
					_ans[x]=max(_ans[x],sum[k]+prea[k]-mns[curs+D]);
					//printf("B %d %lld %lld\n",curs,sum[k]+prea[k],mns[curs+D]);
					mns[curs+D]=min(mns[curs+D],sum[k]+prea[k]);
					assert(isused[curs+D]);
				}
				o=s[n]-s[lst],t=s[lst];
				while(o&&curs-1>=L){
					curs--,t++,o--;
					_ans[x]=max(_ans[x],sum[_occ[t]]+prea[lst]-mns[curs+D]);
					mns[curs+D]=min(mns[curs+D],sum[_occ[t]]+prea[lst]);
					assert(isused[curs+D]);
				}
				for(int i=1;i<=used[0];i++){
					isused[used[i]]=0;
					mns[used[i]]=inf;
				}
				used[0]=0;
				ans[j.id]=_ans[x];
			}
		}
		else {
			for(Query j:largeq[i]){
				int x=j.x,s1=occ[x].size(),s2=occ[i].size();
				int p=0,q=0,s=0,L=0,R=0;
				ll sum=0;
				mns[D]=0;
				while(p<s1||q<s2){
					int o;
					if(q==s2||(p<s1&&q<s2&&occ[x][p]<occ[i][q]))o=occ[x][p++],s++;
					else o=occ[i][q++],s--;
					sum+=b[o],L=min(L,s),R=max(R,s);
					ans[j.id]=max(ans[j.id],sum-mns[s+D]);
					mns[s+D]=min(mns[s+D],sum);
				}
				fill(mns+L+D,mns+R+D+1,inf);
			}
		}
	}
	for(int i=1;i<=q;i++)wrt(ans[i]),pc('\n');
	fwrite(wbuf,1,p2,stdout);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 7ms
memory: 38632kb

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: 0ms
memory: 38500kb

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: 1ms
memory: 38364kb

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: 4ms
memory: 36624kb

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: 663ms
memory: 56748kb

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: 652ms
memory: 58748kb

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: 107ms
memory: 51848kb

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: 124ms
memory: 52364kb

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: 115ms
memory: 54688kb

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: 176ms
memory: 54596kb

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: 132ms
memory: 52288kb

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: 1057ms
memory: 61880kb

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: 1120ms
memory: 60720kb

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: 1096ms
memory: 64956kb

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: 1169ms
memory: 62604kb

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: 1128ms
memory: 62820kb

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: 1074ms
memory: 61872kb

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: 1147ms
memory: 63156kb

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: 1098ms
memory: 62392kb

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: 1096ms
memory: 61796kb

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