QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#317411#4802. Ternary Searchjames1BadCreeperAC ✓198ms18096kbC++141.6kb2024-01-28 23:38:172024-01-28 23:38:17

Judging History

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

  • [2024-01-28 23:38:17]
  • 评测
  • 测评结果:AC
  • 用时:198ms
  • 内存:18096kb
  • [2024-01-28 23:38:17]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long i64; 
const int N = 2e5 + 5; 

int n; 
int a[N], b[N]; i64 ans[N]; 

int C[N]; 
inline void add(int x, int k) { for (; x <= n; x += x & -x) C[x] += k; }
inline int sum(int x) { int r = 0; for (; x; x -= x & -x) r += C[x]; return r; }

int id[N], L[N]; 
vector<int> del[N]; 
inline void solve(void) {
    memset(C, 0, sizeof C); 
    for (int i = 1; i <= n; ++i) L[i] = sum(a[i] - 1), add(a[i], 1); 
    for (int i = 1; i <= n; ++i) id[a[i]] = i; 

    memset(C, 0, sizeof C); fill(del + 1, del + n + 1, vector<int>()); 
    for (int i = 1; i <= n; ++i) { // 从小到大考虑所有谷值
        int p = id[i], r = p; 
        for (int j = 17; j >= 0; --j) {
            int nr = r + (1 << j); 
            if (nr <= n && L[p] >= sum(nr) - L[p]) r = nr; 
        }
        del[r + 1].emplace_back(i); 
        add(p, 1); 
    }

    memset(C, 0, sizeof C); 
    i64 res = 0; 
    for (int i = 1; i <= n; ++i) {
        for (int x : del[i]) add(x, -1); // move to left
        res += sum(n) - sum(a[i] - 1); 
        ans[i] = min(ans[i], res); 
        add(a[i], 1); 
    }
}

int main(void) {
    ios::sync_with_stdio(0); 
    cin >> n; 
    for (int i = 1; i <= n; ++i) cin >> a[i], b[i] = a[i]; 
    sort(b + 1, b + n + 1); 
    for (int i = 1; i <= n; ++i) a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b; 
    memset(ans, 0x3f, sizeof ans); solve(); 
    for (int i = 1; i <= n; ++i) a[i] = n + 1 - a[i]; 
    solve(); 
    for (int i = 1; i <= n; ++i) cout << ans[i] << '\n'; 
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 10680kb

input:

9
11
4
5
14
1
9
19
8
10

output:

0
0
0
0
2
3
3
6
7

result:

ok 9 numbers

Test #2:

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

input:

1
167959139

output:

0

result:

ok 1 number(s): "0"

Test #3:

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

input:

10
802030518
983359971
96204862
640274071
796619138
71550121
799843967
598196518
402690754
446173607

output:

0
0
0
1
1
3
3
5
7
9

result:

ok 10 numbers

Test #4:

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

input:

200
280383943
994080326
443906342
107926501
414713819
45732490
370693588
928126378
581740643
189811400
142817640
218386999
447997927
640003704
637428425
151698689
209462128
155599305
390080477
958251194
515653943
751042490
1186761
443171900
968975479
829084542
462837689
157692260
607049612
606116398...

output:

0
0
0
0
1
1
3
4
5
8
12
15
17
18
19
25
30
35
38
38
41
42
54
60
60
62
69
81
87
93
109
112
124
126
139
148
152
164
169
180
187
193
211
221
226
244
244
252
264
273
286
307
316
328
339
358
378
392
408
416
439
445
447
463
490
503
521
536
547
567
594
619
638
638
659
668
702
724
729
758
772
777
816
836
845
...

result:

ok 200 numbers

Test #5:

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

input:

205
798721503
163087712
141733092
316449050
127486543
148708227
646902722
254007921
648511970
807308399
362190286
356609247
109012359
421274742
647319513
34648738
573893368
682755343
757300027
674077117
324958818
106483343
305773689
837270774
883136402
847234810
756696363
354615040
58759846
32739626...

output:

0
0
0
0
1
2
2
3
3
3
6
10
16
19
22
23
28
34
35
37
45
49
52
61
64
65
69
80
86
86
90
95
105
109
112
113
120
133
136
136
152
166
180
181
196
214
231
245
266
287
297
309
330
341
358
368
383
387
393
414
434
448
456
484
494
506
521
534
543
551
561
596
621
626
634
644
655
692
732
743
750
767
807
846
869
876...

result:

ok 205 numbers

Test #6:

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

input:

300
310486422
145995805
744831366
913444383
54589620
760840937
351438066
497043991
437781614
854981783
137693561
940050335
22692948
779922082
713507093
773173557
700078081
889701510
709956997
620732070
535136113
428608043
656183847
627784826
348865620
634573459
271461607
622653937
662713549
35111166...

output:

0
0
0
0
1
2
3
5
7
9
11
14
15
19
22
25
27
33
36
38
40
41
45
49
50
55
55
60
69
71
76
80
99
110
110
116
117
125
148
151
175
182
208
223
229
245
256
259
280
285
302
314
332
337
345
369
380
398
403
405
413
447
468
484
494
511
537
550
555
585
601
616
637
659
664
695
715
730
755
757
762
777
787
805
842
860...

result:

ok 300 numbers

Test #7:

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

input:

400
418345904
425283570
502048611
949470422
1539681
816939105
835973188
548395262
912622971
892988664
137695077
200974194
416058059
46966392
717454596
508236934
577018523
271703565
404875844
116208843
795979997
232130698
201391686
396794371
873677110
643916648
979303165
209135495
365323814
220296329...

output:

0
0
0
0
0
1
3
4
7
8
8
9
11
11
16
20
25
28
32
33
43
47
50
55
67
73
73
83
90
98
99
103
110
116
124
135
142
160
175
182
195
197
210
216
217
222
228
234
254
268
295
306
334
337
349
357
383
388
392
420
441
460
473
474
480
491
506
532
548
549
572
589
629
635
674
700
711
714
744
773
797
809
815
852
860
895...

result:

ok 400 numbers

Test #8:

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

input:

10000
654266585
918195389
61806003
512260326
35440115
595032690
353979552
32592347
712550231
380796590
139710715
828841723
115308806
675515525
910499939
780014628
924148821
719010926
731425929
381219228
963983190
359044179
76245693
524823828
890447664
856974623
751508756
830808517
963022804
54360656...

output:

0
0
0
1
1
2
3
5
5
7
10
10
14
16
16
18
18
22
26
34
34
45
54
57
64
70
76
82
87
94
97
97
104
108
118
125
136
143
143
159
162
165
169
172
181
202
210
213
224
230
240
255
257
277
279
307
337
342
364
367
377
403
408
416
428
459
496
504
533
534
554
571
577
595
630
637
666
684
723
740
782
797
818
840
864
87...

result:

ok 10000 numbers

Test #9:

score: 0
Accepted
time: 93ms
memory: 14196kb

input:

100000
909264242
651840489
623350850
547495386
693908264
894663677
90585843
745747150
871758382
121892899
769215576
332202967
626472418
629926442
365356165
588776397
879320123
676708911
253241624
334264710
58088451
743496068
4060883
166883807
81383194
778842188
617677263
814376024
454099047
56897081...

output:

0
0
0
0
0
0
2
3
4
8
9
13
16
19
24
27
27
29
37
43
43
52
52
54
56
70
76
77
83
89
90
92
101
106
116
127
141
146
160
171
186
192
198
207
207
208
229
247
247
268
281
297
310
323
326
339
359
365
379
395
409
417
422
436
455
465
474
493
506
511
541
575
590
618
631
646
677
689
725
742
751
782
786
801
837
867...

result:

ok 100000 numbers

Test #10:

score: 0
Accepted
time: 156ms
memory: 16180kb

input:

150000
593610248
98376074
458908422
957965730
959127651
759669531
99902673
579848604
606011447
768639951
2128743
605405193
956486615
768041769
469873926
191835294
294470724
379037444
647254616
952534726
633061613
309062234
79309702
807129949
388586177
560947476
481828902
199198541
871486573
16017326...

output:

0
0
0
0
0
2
2
3
5
9
9
12
17
19
23
24
26
29
35
44
50
52
52
61
66
73
80
82
94
97
107
115
118
133
139
146
158
167
176
186
191
204
206
225
234
237
249
259
276
280
295
318
327
333
347
355
374
375
380
380
388
397
397
422
450
450
480
496
531
547
583
619
622
634
660
688
709
713
730
768
785
815
836
847
858
8...

result:

ok 150000 numbers

Test #11:

score: 0
Accepted
time: 193ms
memory: 17588kb

input:

190000
140414614
406007592
298567632
605924836
708675375
600753299
494271070
566246098
495997778
483573743
427426250
494979459
689919340
338248109
254006654
673303859
66362765
710585447
86532982
318050353
417362365
723383976
355024957
42913831
928066077
617517952
502644865
816067465
377578850
560308...

output:

0
0
0
1
1
1
1
2
3
3
3
6
12
12
12
20
20
30
31
34
39
50
55
55
65
70
76
77
85
91
102
104
113
120
125
141
157
165
174
185
194
202
214
223
240
250
260
272
274
295
302
323
328
335
336
343
369
377
385
385
413
436
453
480
488
504
507
535
541
545
566
600
610
634
656
688
712
716
737
744
782
783
817
848
862
87...

result:

ok 190000 numbers

Test #12:

score: 0
Accepted
time: 198ms
memory: 18096kb

input:

200000
853818360
128916365
670632329
764180415
686020796
260618375
670803846
624457033
519234961
658913000
463658733
882826700
119473262
180810470
69568421
356237381
713120443
348959227
728080803
580919147
318327296
565941028
228967961
653195229
533937115
990818044
505922232
542675502
258087880
4750...

output:

0
0
0
0
1
2
3
4
5
8
8
12
15
16
16
19
25
28
29
32
38
42
50
52
57
57
64
70
82
91
95
96
114
128
141
151
157
161
170
185
187
201
209
218
224
238
244
247
252
271
278
292
299
319
337
337
348
351
362
370
377
381
392
407
421
439
446
462
474
482
514
519
551
566
571
579
599
633
635
667
692
694
724
735
742
778...

result:

ok 200000 numbers

Test #13:

score: 0
Accepted
time: 59ms
memory: 13556kb

input:

100000
607065123
607065538
607068747
607072791
607077748
607083409
607087209
607089384
607091039
607103510
607113399
607134718
607150727
607154096
607154174
607164161
607203894
607217028
607218498
607243574
607250282
607264707
607276495
607278511
607285355
607293720
607295032
607301598
607312533
607...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 100000 numbers

Test #14:

score: 0
Accepted
time: 32ms
memory: 12072kb

input:

50000
363946188
363946571
363970783
364019687
364035920
364072248
364076189
364102642
364118997
364197249
364232591
364260040
364285206
364304417
364318219
364337062
364351969
364376098
364377772
364384165
364406063
364428419
364438839
364447089
364449160
364478599
364489207
364516989
364523776
3645...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 50000 numbers

Test #15:

score: 0
Accepted
time: 115ms
memory: 16864kb

input:

200000
34955275
34958003
34968771
34979259
35000286
35009855
35011625
35014972
35019574
35020104
35021021
35032450
35035638
35055084
35060098
35065979
35069608
35074708
35079739
35084342
35085847
35088767
35100982
35109907
35111017
35118812
35121011
35122599
35130365
35132068
35135671
35137300
35139...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 200000 numbers

Test #16:

score: 0
Accepted
time: 105ms
memory: 17872kb

input:

200000
50155862
50155954
50156659
50157332
50160405
50161798
50175654
50177951
50191144
50192470
50199899
50218953
50230226
50236501
50237031
50242991
50244011
50246269
50246420
50256588
50258513
50263286
50274527
50276700
50284837
50292305
50295349
50297461
50305832
50316674
50330093
50333051
50334...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 200000 numbers

Test #17:

score: 0
Accepted
time: 155ms
memory: 16796kb

input:

200000
383784241
383784895
383787460
383789641
383797874
383780901
383772173
383768131
383767812
383755451
785557495
785557552
785564596
785568438
785577267
785555446
785534954
785528611
785527753
785517557
520568831
520571298
520586261
520598044
520606280
520562507
520548131
520546949
520542492
520...

output:

0
0
0
0
0
0
0
0
0
0
5
10
10
10
10
15
21
28
35
35
35
36
38
41
45
45
45
45
45
45
55
66
78
91
105
115
124
132
139
145
160
176
190
190
190
195
201
208
216
225
225
225
225
225
225
230
236
243
251
260
270
280
290
300
310
325
341
358
376
395
445
486
488
491
495
495
495
495
495
495
495
496
498
501
505
505
5...

result:

ok 200000 numbers