@@ -114,34 +114,30 @@ class BinaryIndexedTree:
114
114
self .n = n
115
115
self .c = [0 ] * (n + 1 )
116
116
117
- @ staticmethod
118
- def lowbit (x ):
119
- return x & - x
120
-
121
- def update (self , x , delta ):
117
+ def update (self , x : int , v : int ):
122
118
while x <= self .n:
123
- self .c[x] += delta
124
- x += BinaryIndexedTree.lowbit(x)
119
+ self .c[x] += v
120
+ x += x & - x
125
121
126
- def query (self , x ) :
122
+ def query (self , x : int ) -> int :
127
123
s = 0
128
- while x > 0 :
124
+ while x:
129
125
s += self .c[x]
130
- x -= BinaryIndexedTree.lowbit(x)
126
+ x -= x & - x
131
127
return s
132
128
133
129
134
130
class Solution :
135
131
def createSortedArray (self , instructions : List[int ]) -> int :
136
- n = max (instructions)
137
- tree = BinaryIndexedTree(n )
132
+ m = max (instructions)
133
+ tree = BinaryIndexedTree(m )
138
134
ans = 0
139
- for num in instructions:
140
- a = tree.query(num - 1 )
141
- b = tree.query(n) - tree.query(num )
142
- ans += min (a, b)
143
- tree.update(num , 1 )
144
- return ans % int (( 1e9 + 7 ))
135
+ mod = 10 ** 9 + 7
136
+ for i, x in enumerate (instructions):
137
+ cost = min ( tree.query(x - 1 ), i - tree.query(x) )
138
+ ans += cost
139
+ tree.update(x , 1 )
140
+ return ans % mod
145
141
```
146
142
147
143
线段树:
@@ -212,50 +208,48 @@ class Solution:
212
208
树状数组:
213
209
214
210
``` java
215
- class Solution {
216
- public int createSortedArray (int [] instructions ) {
217
- int n = 100010 ;
218
- int mod = (int ) 1e9 + 7 ;
219
- BinaryIndexedTree tree = new BinaryIndexedTree (n);
220
- int ans = 0 ;
221
- for (int num : instructions) {
222
- int a = tree. query(num - 1 );
223
- int b = tree. query(n) - tree. query(num);
224
- ans += Math . min(a, b);
225
- ans %= mod;
226
- tree. update(num, 1 );
227
- }
228
- return ans;
229
- }
230
- }
231
-
232
211
class BinaryIndexedTree {
233
212
private int n;
234
213
private int [] c;
235
214
236
215
public BinaryIndexedTree (int n ) {
237
216
this . n = n;
238
- c = new int [n + 1 ];
217
+ this . c = new int [n + 1 ];
239
218
}
240
219
241
- public void update (int x , int delta ) {
220
+ public void update (int x , int v ) {
242
221
while (x <= n) {
243
- c[x] += delta ;
244
- x += lowbit(x) ;
222
+ c[x] += v ;
223
+ x += x & - x ;
245
224
}
246
225
}
247
226
248
227
public int query (int x ) {
249
228
int s = 0 ;
250
229
while (x > 0 ) {
251
230
s += c[x];
252
- x -= lowbit(x) ;
231
+ x -= x & - x ;
253
232
}
254
233
return s;
255
234
}
235
+ }
256
236
257
- public static int lowbit (int x ) {
258
- return x & - x;
237
+ class Solution {
238
+ public int createSortedArray (int [] instructions ) {
239
+ int m = 0 ;
240
+ for (int x : instructions) {
241
+ m = Math . max(m, x);
242
+ }
243
+ BinaryIndexedTree tree = new BinaryIndexedTree (m);
244
+ int ans = 0 ;
245
+ final int mod = (int ) 1e9 + 7 ;
246
+ for (int i = 0 ; i < instructions. length; ++ i) {
247
+ int x = instructions[i];
248
+ int cost = Math . min(tree. query(x - 1 ), i - tree. query(x));
249
+ ans = (ans + cost) % mod;
250
+ tree. update(x, 1 );
251
+ }
252
+ return ans;
259
253
}
260
254
}
261
255
```
@@ -350,47 +344,43 @@ class SegmentTree {
350
344
``` cpp
351
345
class BinaryIndexedTree {
352
346
public:
353
- int n;
354
- vector<int > c;
355
-
356
347
BinaryIndexedTree(int _ n)
357
348
: n(_ n)
358
- , c(_n + 1) { }
349
+ , c(_ n + 1) {}
359
350
360
351
void update(int x, int delta) {
361
352
while (x <= n) {
362
353
c[x] += delta;
363
- x += lowbit(x) ;
354
+ x += x & -x ;
364
355
}
365
356
}
366
357
367
358
int query (int x) {
368
359
int s = 0;
369
- while (x > 0 ) {
360
+ while (x) {
370
361
s += c[ x] ;
371
- x -= lowbit(x) ;
362
+ x -= x & -x ;
372
363
}
373
364
return s;
374
365
}
375
366
376
- int lowbit(int x) {
377
- return x & -x ;
378
- }
367
+ private:
368
+ int n ;
369
+ vector< int > c;
379
370
};
380
371
381
372
class Solution {
382
373
public:
383
374
int createSortedArray(vector<int >& instructions) {
384
- int n = 100010 ;
385
- int mod = 1e9 + 7 ;
386
- BinaryIndexedTree * tree = new BinaryIndexedTree(n) ;
375
+ int m = * max_element(instructions.begin(), instructions.end()) ;
376
+ BinaryIndexedTree tree(m) ;
377
+ const int mod = 1e9 + 7 ;
387
378
int ans = 0;
388
- for (int num : instructions) {
389
- int a = tree->query(num - 1);
390
- int b = tree->query(n) - tree->query(num);
391
- ans += min(a, b);
392
- ans %= mod;
393
- tree->update(num, 1);
379
+ for (int i = 0; i < instructions.size(); ++i) {
380
+ int x = instructions[ i] ;
381
+ int cost = min(tree.query(x - 1), i - tree.query(x));
382
+ ans = (ans + cost) % mod;
383
+ tree.update(x, 1);
394
384
}
395
385
return ans;
396
386
}
@@ -487,38 +477,42 @@ func newBinaryIndexedTree(n int) *BinaryIndexedTree {
487
477
return &BinaryIndexedTree{n, c}
488
478
}
489
479
490
- func (this *BinaryIndexedTree ) lowbit (x int ) int {
491
- return x & -x
492
- }
493
-
494
480
func (this *BinaryIndexedTree ) update (x , delta int ) {
495
481
for x <= this.n {
496
482
this.c [x] += delta
497
- x += this. lowbit (x)
483
+ x += x & -x
498
484
}
499
485
}
500
486
501
487
func (this *BinaryIndexedTree ) query (x int ) int {
502
488
s := 0
503
489
for x > 0 {
504
490
s += this.c [x]
505
- x -= this. lowbit (x)
491
+ x -= x & -x
506
492
}
507
493
return s
508
494
}
509
495
510
- func createSortedArray (instructions []int ) int {
511
- n := 100010
512
- mod := int (1e9 + 7 )
513
- tree := newBinaryIndexedTree (n)
514
- ans := 0
515
- for _ , num := range instructions {
516
- a , b := tree.query (num-1 ), tree.query (n)-tree.query (num)
517
- ans += min (a, b)
518
- ans %= mod
519
- tree.update (num, 1 )
496
+ func createSortedArray (instructions []int ) (ans int ) {
497
+ m := 0
498
+ for _ , x := range instructions {
499
+ m = max (m, x)
500
+ }
501
+ tree := newBinaryIndexedTree (m)
502
+ const mod = 1e9 + 7
503
+ for i , x := range instructions {
504
+ cost := min (tree.query (x-1 ), i-tree.query (x))
505
+ ans = (ans + cost) % mod
506
+ tree.update (x, 1 )
507
+ }
508
+ return
509
+ }
510
+
511
+ func max (a , b int ) int {
512
+ if a > b {
513
+ return a
520
514
}
521
- return ans
515
+ return b
522
516
}
523
517
524
518
func min (a , b int ) int {
@@ -529,6 +523,50 @@ func min(a, b int) int {
529
523
}
530
524
```
531
525
526
+ ### ** TypeScript**
527
+
528
+ ``` ts
529
+ class BinaryIndexedTree {
530
+ private n: number ;
531
+ private c: number [];
532
+
533
+ constructor (n : number ) {
534
+ this .n = n ;
535
+ this .c = new Array (n + 1 ).fill (0 );
536
+ }
537
+
538
+ public update(x : number , v : number ): void {
539
+ while (x <= this .n ) {
540
+ this .c [x ] += v ;
541
+ x += x & - x ;
542
+ }
543
+ }
544
+
545
+ public query(x : number ): number {
546
+ let s = 0 ;
547
+ while (x > 0 ) {
548
+ s += this .c [x ];
549
+ x -= x & - x ;
550
+ }
551
+ return s ;
552
+ }
553
+ }
554
+
555
+ function createSortedArray(instructions : number []): number {
556
+ const m = Math .max (... instructions );
557
+ const tree = new BinaryIndexedTree (m );
558
+ let ans = 0 ;
559
+ const mod = 10 ** 9 + 7 ;
560
+ for (let i = 0 ; i < instructions .length ; ++ i ) {
561
+ const x = instructions [i ];
562
+ const cost = Math .min (tree .query (x - 1 ), i - tree .query (x ));
563
+ ans = (ans + cost ) % mod ;
564
+ tree .update (x , 1 );
565
+ }
566
+ return ans ;
567
+ }
568
+ ```
569
+
532
570
### ** ...**
533
571
534
572
```
0 commit comments