QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#293782#4002. Easy FixrageOfThunder#AC ✓167ms47464kbC++143.0kb2023-12-29 19:01:112023-12-29 19:01:12

Judging History

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

  • [2023-12-29 19:01:12]
  • 评测
  • 测评结果:AC
  • 用时:167ms
  • 内存:47464kb
  • [2023-12-29 19:01:11]
  • 提交

answer

#include<bits/stdc++.h>

#define ll long long
#define mk make_pair
#define fi first
#define se second

using namespace std;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
	for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
	return x*f;
}

void cmax(int &x,int v){x=max(x,v);}
void cmin(int &x,int v){x=min(x,v);}

const int N=2e5+5;
int p[N],A[N],n,m;
struct BIT{
	int c[N];
	void clear(){memset(c,0,sizeof(c));}
	int lowbit(int x){return x&(-x);}
	void add(int x,int v){for(int i=x;i<=n;i+=lowbit(i))c[i]+=v;}
	int sum(int x){int res=0;for(int i=x;i;i-=lowbit(i))res+=c[i];return res;}
	int qsum(int l,int r){return sum(r)-sum(l-1);}
}T1,T2,T3,T4,T5,T;
// 1 -> (+1,-1)
// 2 -> (-1,+1)
// 3 -> (0,-1)
// 4 -> (-1,0)
// 5 -> (-1,-1)
struct Node{int s,t,f,ad,id;Node(int S,int T,int F,int AD,int I):s(S),t(T),f(F),ad(AD),id(I){}};
vector<Node>ques[N];
ll ans[N],tA[N],tB[N];
vector<pair<int,int> >qA[N],qB[N];
int ql[N],qr[N];

signed main(void){

#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	n=read();
	for(int i=1;i<=n;i++)p[i]=read(),A[i]=T.sum(p[i]),T.add(p[i],1);
	// cout<<"A = ";for(int i=1;i<=n;i++)cout<<A[i]<<" ";puts("");
	ll now=0;
	for(int i=1;i<=n;i++)now+=min(A[i],p[i]-1-A[i]);
	// cout<<"now = "<<now<<endl;
	m=read();
	for(int i=1;i<=m;i++){
		int l=read(),r=read();ans[i]=now;ql[i]=l,qr[i]=r;
		if(l==r)continue;
		if(l>r)swap(l,r),swap(ql[i],qr[i]);
		int s=p[l],t=p[r];
		if(s>t){
			ques[r-1].emplace_back(Node(t+1,s-1,1,1,i));
			ques[l].emplace_back(Node(t+1,s-1,-1,1,i));
		}
		else{
			ques[r-1].emplace_back(Node(s+1,t-1,1,-1,i));
			ques[l].emplace_back(Node(s+1,t-1,-1,-1,i));
		}
		qA[r-1].emplace_back(mk(p[l]-1,i));
		qB[l-1].emplace_back(mk(p[r]-1,i));
		if(p[l]>p[r])tA[i]++;
		ans[i]-=min(A[l],p[l]-1-A[l]);
		ans[i]-=min(A[r],p[r]-1-A[r]);
	}

	T.clear();
	for(int i=1;i<=n;i++){
		T.add(p[i],1);
		// cout<<"i = "<<i<<endl;
		if(p[i]%2==0){
			if(A[i]<=(p[i]-4)/2)T1.add(p[i],1);
			else if(A[i]>=(p[i]+2)/2)T2.add(p[i],1);
			else if(A[i]==(p[i]-2)/2)T3.add(p[i],1);
			else if(A[i]==p[i]/2)T4.add(p[i],1);
		}
		else{
			if(A[i]<=(p[i]-3)/2)T1.add(p[i],1);
			else if(A[i]>=(p[i]+1)/2)T2.add(p[i],1);
			else if(A[i]==(p[i]-1)/2)T5.add(p[i],1);
		}
		for(auto [s,t,f,ad,id]:ques[i]){
			// cout<<"Q : "<<s<<" "<<t<<" "<<f<<" "<<ad<<" "<<id<<endl;
			if(ad==1){
				if(f==1)ans[id]+=T1.qsum(s,t)-T2.qsum(s,t)-T4.qsum(s,t)-T5.qsum(s,t);
				else ans[id]-=T1.qsum(s,t)-T2.qsum(s,t)-T4.qsum(s,t)-T5.qsum(s,t);
			}
			else{
				if(f==1)ans[id]+=T2.qsum(s,t)-T1.qsum(s,t)-T3.qsum(s,t)-T5.qsum(s,t);
				else ans[id]-=T2.qsum(s,t)-T1.qsum(s,t)-T3.qsum(s,t)-T5.qsum(s,t);
			}
		}
		// cout<<"now ans = "<<ans[1]<<endl;
		for(auto [lim,id]:qA[i])tA[id]+=T.sum(lim);
		for(auto [lim,id]:qB[i])tB[id]+=T.sum(lim);
	}

	for(int i=1;i<=m;i++){
		if(ql[i]==qr[i]){cout<<now<<'\n';continue;}
		int l=ql[i],r=qr[i];
		cout<<ans[i]+min(tA[i],p[l]-1-tA[i])+min(tB[i],p[r]-1-tB[i])<<'\n';
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 26400kb

input:

7
1 6 2 7 5 4 3
7
1 7
2 6
3 5
4 4
1 1
2 1
3 7

output:

7
6
6
7
7
6
8

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 26176kb

input:

5
5 3 1 2 4
3
3 1
2 5
3 3

output:

3
0
0

result:

ok 3 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 26224kb

input:

10
10 1 6 7 9 8 4 3 5 2
10
6 7
5 2
4 1
10 4
6 2
1 9
2 9
4 5
6 8
9 7

output:

12
7
13
8
7
15
9
11
11
12

result:

ok 10 lines

Test #4:

score: 0
Accepted
time: 3ms
memory: 26132kb

input:

20
3 4 19 18 5 6 20 12 9 13 1 10 8 7 17 16 2 14 11 15
20
19 9
20 6
1 14
11 15
12 10
20 15
10 3
20 1
16 7
8 19
3 17
9 2
20 14
20 2
20 9
2 4
12 4
9 15
7 16
9 4

output:

46
48
43
50
44
42
48
45
42
45
39
41
50
46
48
44
50
42
42
48

result:

ok 20 lines

Test #5:

score: 0
Accepted
time: 0ms
memory: 28520kb

input:

100
70 54 10 72 81 84 56 15 27 19 43 100 49 44 52 33 63 40 95 17 58 2 51 39 22 18 82 1 16 99 32 29 24 94 9 98 5 37 47 14 42 73 41 31 79 64 12 6 53 26 68 67 89 13 90 4 21 93 46 74 75 88 66 57 23 7 25 48 92 62 30 8 50 61 38 87 71 34 97 28 80 11 60 91 3 35 86 96 36 20 59 65 83 45 76 77 78 69 85 55
100
...

output:

1080
1083
1056
1056
1087
1058
1082
1085
1116
1088
1072
1084
1096
1094
1072
1092
1090
1102
1096
1084
1086
1057
1062
1102
1080
1086
1082
1082
1082
1088
1080
1086
1122
1104
1086
1084
1113
1088
1060
1097
1100
1084
1089
1089
1082
1115
1087
1082
1087
1092
1086
1080
1084
1085
1082
1107
1072
1092
1061
1069
...

result:

ok 100 lines

Test #6:

score: 0
Accepted
time: 0ms
memory: 26124kb

input:

150
100 27 33 124 22 137 14 109 35 92 54 69 67 96 97 3 19 98 120 28 36 16 84 135 20 9 141 62 48 87 103 18 34 76 122 138 50 1 26 70 117 134 72 121 40 17 142 82 91 53 118 37 111 15 149 60 104 93 46 73 126 47 140 59 132 144 143 78 71 38 85 68 74 31 102 4 139 2 65 45 25 7 29 150 131 21 146 130 148 11 89...

output:

2844
2818
2837
2818
2813
2796
2774
2849
2862
2821
2832
2846
2816
2840
2844
2826
2824
2818
2749
2830
2818
2822
2799
2826
2801
2826
2823
2828
2760
2824
2851
2818
2790
2850
2797
2880
2777
2779
2825
2824
2825
2818
2844
2825
2818
2818
2825
2834
2858
2834
2833
2842
2795
2842
2856
2802
2825
2836
2833
2804
...

result:

ok 109 lines

Test #7:

score: 0
Accepted
time: 0ms
memory: 26388kb

input:

1001
683 346 384 352 291 367 447 360 490 102 779 900 781 642 299 293 619 742 834 434 817 171 341 561 122 471 145 90 769 457 882 256 525 509 338 41 851 269 96 630 751 297 347 687 903 57 80 771 393 921 82 300 580 579 507 660 463 551 578 902 626 33 927 260 822 670 116 210 349 301 166 631 390 395 860 75...

output:

121741
122016
121953
121923
122061
121986
121953
121867
121695
122378
122035
121961
121937
122668
122276
122063
122168
122565
122176
122037
122001
122599
121741
122053
122082
121891
121699
122031
122071
122077
122049
122065
122207
121927
122039
122050
122087
121807
121984
122029
121982
122033
122033...

result:

ok 2002 lines

Test #8:

score: 0
Accepted
time: 0ms
memory: 28176kb

input:

100
60 58 61 71 6 87 9 82 10 77 51 72 36 97 92 21 68 32 81 20 3 29 52 93 88 94 16 48 50 67 11 55 84 57 38 65 25 41 66 59 19 56 100 49 86 85 34 31 80 2 28 33 15 7 69 26 45 91 75 23 95 22 35 4 8 17 54 79 74 27 53 64 39 12 1 24 13 47 98 5 96 43 42 30 37 90 63 99 73 18 14 40 46 78 70 76 62 83 44 89
200
...

output:

1073
1086
1083
1095
1076
1079
1079
1094
1070
1095
1080
1084
1087
1080
1056
1116
1086
1074
1090
1096
1100
1076
1080
1070
1078
1090
1086
1092
1068
1084
1086
1084
1080
1079
1084
1082
1082
1060
1051
1098
1078
1097
1089
1078
1078
1087
1083
1066
1089
1075
1087
1089
1080
1095
1076
1081
1090
1080
1100
1096
...

result:

ok 200 lines

Test #9:

score: 0
Accepted
time: 3ms
memory: 26160kb

input:

200
59 41 64 113 107 121 20 92 106 160 197 181 83 166 36 54 152 180 52 176 22 190 153 10 94 21 125 75 74 24 159 198 175 102 79 188 6 95 40 30 154 115 46 45 42 194 7 48 185 39 189 157 186 66 87 85 1 12 31 78 3 119 127 104 26 29 124 53 184 2 70 196 76 110 34 179 8 112 109 18 132 35 123 65 44 163 191 1...

output:

4825
4773
4757
4781
4777
4785
4782
4768
4794
4814
4869
4789
4859
4824
4767
4777
4806
4794
4789
4869
4781
4779
4803
4822
4766
4737
4773
4733
4795
4777
4795
4907
4783
4786
4756
4746
4743
4798
4784
4789
4835
4805
4767
4787
4803
4783
4773
4808
4763
4780
4793
4759
4783
4796
4840
4798
4814
4799
4777
4797
...

result:

ok 100 lines

Test #10:

score: 0
Accepted
time: 0ms
memory: 26220kb

input:

1000
662 811 263 46 591 511 230 223 628 603 716 880 187 575 701 184 505 754 130 154 367 91 704 945 171 588 19 486 34 513 622 849 629 437 776 290 743 470 587 681 723 271 879 642 77 281 71 655 365 942 809 94 266 148 146 305 198 311 788 69 343 277 887 557 169 41 784 791 451 812 932 663 594 576 487 946 ...

output:

121683
121539
121718
121831
121737
121628
121167
121818
121731
121681
121693
121250
121364
121745
121395
121733
121183
121711
121758
121745
121781
121237
121552
121964
121711
121814
121807
121735
121385
121746
121793
121779
121740
121725
122351
121731
121722
121742
121552
121741
121785
121503
121852...

result:

ok 1000 lines

Test #11:

score: 0
Accepted
time: 6ms
memory: 26260kb

input:

1005
317 805 235 278 778 826 874 73 679 24 943 520 831 781 645 406 34 43 701 939 56 304 836 620 696 238 1003 957 949 795 295 941 692 484 4 27 561 85 399 80 174 856 199 382 502 144 40 748 218 698 613 250 806 49 55 606 258 297 162 254 982 355 835 102 325 59 689 403 270 167 202 777 246 448 120 300 678 ...

output:

122764
122590
122214
122778
122862
122501
122904
122745
122888
122630
122820
122856
122782
122592
122732
122792
122916
122819
123056
123002
122670
122456
122686
122756
122788
122698
122738
122658
122786
122954
122784
123040
122765
122972
122950
122788
123348
122684
122780
122891
122756
122864
122764...

result:

ok 1005 lines

Test #12:

score: 0
Accepted
time: 3ms
memory: 28520kb

input:

2000
1869 1841 1361 1803 1559 1740 1235 1362 805 1804 165 681 883 181 562 922 1576 1704 271 1260 811 399 475 1775 1519 83 1345 1666 713 798 578 443 666 867 1080 597 665 148 136 1840 846 1054 1566 1537 910 1712 1662 499 958 687 1124 1865 1284 1922 30 1194 1467 1839 1101 1426 1262 381 1812 975 473 182...

output:

503789
503648
504546
503861
504189
503890
503766
502595
503686
503684
504276
503657
503697
503032
503966
503624
503070
503669
503315
503798
503746
503648
502713
503862
503688
503966
502783
503628
503800
504532
504050
503406
503578
503464
503698
503566
503814
503222
503836
504364
503662
503722
503315...

result:

ok 2000 lines

Test #13:

score: 0
Accepted
time: 0ms
memory: 28424kb

input:

1500
1162 964 138 1372 386 1494 420 883 251 1371 663 1218 1110 1241 581 267 1169 901 531 389 416 487 428 94 792 1177 378 1175 1071 1439 817 1222 1174 1076 577 1236 827 97 345 1369 1238 1077 1366 59 184 855 897 1227 1411 1220 556 333 529 503 437 1297 539 1114 1082 284 672 186 979 185 618 493 1009 963...

output:

280833
280859
281123
280954
280912
281018
280572
281182
281128
280828
281016
281062
280788
281554
281002
281135
281096
281102
281043
280970
281022
281222
281015
280984
280944
281022
280985
281332
281244
281020
281119
280839
280892
280936
280942
281024
281048
280900
281086
281022
281016
281017
280661...

result:

ok 1500 lines

Test #14:

score: 0
Accepted
time: 69ms
memory: 37780kb

input:

50001
32799 28330 26898 17795 36551 11914 18140 12319 4684 45329 23187 30307 48712 42400 44352 17220 48100 17699 5773 9592 35944 31214 37208 1896 24298 20883 1764 46297 5828 23488 9300 35166 40644 1194 34625 5792 15396 24610 36373 46762 14317 40124 11778 33884 40392 17464 41852 29411 28407 42896 130...

output:

313567942
313570828
313541464
313566368
313570014
313580817
313573433
313564550
313567488
313553111
313563112
313553598
313571958
313575394
313564694
313562548
313569957
313583070
313567436
313565627
313566690
313566370
313568260
313563684
313581910
313564441
313560288
313566118
313563694
313566516
...

result:

ok 105000 lines

Test #15:

score: 0
Accepted
time: 80ms
memory: 38896kb

input:

100000
5564 50065 37479 98835 66549 9383 8565 18828 759 62721 53876 55132 47190 54307 88451 70144 51060 43463 64944 59094 20076 29216 70343 80957 67197 88573 95874 7804 87321 38365 72801 43211 16612 19661 45590 84693 61049 21916 18604 62447 55815 66420 64339 26670 15010 67957 30989 17712 36506 93017...

output:

1253406339
1253390279
1253396942
1253394696
1253349171
1253392721
1253395810
1253401984
1253396149
1253403883
1253402207
1253399982
1253391297
1253392479
1253385235
1253408091
1253407907
1253418670
1253408665
1253382867
1253401416
1253466811
1253376372
1253406418
1253407967
1253430595
1253362697
125...

result:

ok 100000 lines

Test #16:

score: 0
Accepted
time: 167ms
memory: 46696kb

input:

100000
42613 88139 15567 15320 48236 82357 13878 16859 25028 52206 59028 39609 6345 37794 76148 48879 60750 94076 16861 70704 43869 50468 48774 17994 96488 35143 2239 32146 18784 55024 56541 42460 85818 93468 50075 28020 24963 85276 77039 94137 48432 14952 55694 50392 65277 31223 63180 66421 87782 1...

output:

1249183103
1249136135
1249193331
1249195897
1249191599
1249187950
1249187725
1249202469
1249173210
1249220288
1249190665
1249184056
1249166726
1249185395
1249198431
1249190359
1249192951
1249195345
1249193703
1249185888
1249186317
1249229350
1249232367
1249204737
1249188497
1249192563
1249198376
124...

result:

ok 200000 lines

Test #17:

score: 0
Accepted
time: 167ms
memory: 45892kb

input:

100000
48688 63936 9895 91928 6132 41016 13085 67302 73859 40804 49516 70480 76032 16860 3125 95156 15802 60262 80815 56471 28394 51818 15117 56080 82444 84386 59809 19129 3322 8515 81028 25872 74705 47623 43712 1817 32110 89 74268 13218 31586 12854 79075 45933 75559 52336 20026 90451 27535 27787 56...

output:

1249952966
1249969920
1249972870
1249973408
1249982475
1249973006
1250003267
1250013180
1249968314
1249991838
1249972937
1249963796
1249962281
1249971804
1249972284
1249965855
1249988260
1249972596
1249973954
1249973825
1249966283
1249970848
1249973504
1249949396
1249977656
1249977198
1249973972
124...

result:

ok 190000 lines

Test #18:

score: 0
Accepted
time: 161ms
memory: 47464kb

input:

100000
83654 22209 62829 15248 19398 76932 40747 33345 33500 84596 42075 84822 23658 79287 90810 26374 30760 98065 43262 90775 51685 85971 11458 97334 6255 92570 57237 31938 11136 27661 81166 23181 16641 81714 58645 2547 90806 59578 24810 965 96531 87037 75308 45207 55041 54115 14958 75064 80135 361...

output:

1242896925
1242877365
1242892839
1242912143
1242896763
1242903916
1242895765
1242892219
1242892705
1242896655
1242891174
1242866715
1242894709
1242899883
1242901213
1242893337
1242845926
1242913387
1242896681
1242897107
1242896764
1242897593
1242896316
1242853622
1242878140
1242890807
1242889485
124...

result:

ok 195000 lines

Test #19:

score: 0
Accepted
time: 146ms
memory: 43108kb

input:

100000
97136 99502 63113 65962 77220 28085 76869 80812 79194 54927 37858 77070 82923 24976 14476 31101 21910 23508 6035 99514 73397 78025 99475 3433 99037 29842 33754 87225 7709 63618 37354 31203 98623 9266 89999 5671 82663 37376 84312 26044 43170 46508 9940 30186 81061 38885 43294 82191 91821 82080...

output:

1250417815
1250411252
1250434187
1250447869
1250457675
1250450753
1250451178
1250444725
1250480221
1250450907
1250441035
1250437191
1250449069
1250456699
1250442582
1250451439
1250456380
1250450571
1250460149
1250459337
1250452105
1250441343
1250461339
1250480232
1250454483
1250444677
1250451615
125...

result:

ok 180000 lines

Test #20:

score: 0
Accepted
time: 153ms
memory: 46192kb

input:

99995
51342 98064 70046 30032 67871 98618 33376 13638 37790 92580 19690 99340 48067 59795 82239 3620 84183 89907 44525 92732 86666 52849 61416 94708 28749 41149 2928 34542 62638 12894 91083 27954 72379 47368 63651 38655 77351 79578 48741 1636 13619 29463 36662 84020 33254 87471 85064 86101 89923 935...

output:

1248318818
1248326038
1248313596
1248255442
1248299162
1248337716
1248295076
1248307608
1248298785
1248310557
1248310988
1248334954
1248317610
1248309884
1248312288
1248348150
1248337922
1248311654
1248290737
1248336136
1248303115
1248306001
1248295290
1248313336
1248323903
1248313416
1248327398
124...

result:

ok 200000 lines