BSP 2011

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

BSP Tree(2011

Revised)
Willy Liu

November 8, 2011

Binary Space Partitioning Tree(BSP Tree), ,


. , ,
. 1 BSP Tree , ; 2
; 3 BSP Tree ; 4 BSP Tree
; 5 , .

(Introduction)

BSP Tree , (update) (query) . BSP Tree


, . Dynamic Range Minimum ery(RMQ) S[1..n],
: (1) S[i] a, (2) S[i..j] .

1.1

, O(1), O(n). , ,
. (cache) ,

. ,
.
n ,

n , Figure 1. O( n)
; , ,,

O(
n)
,

O(
n)
,

O( n).
length

0.

length

4
Figure 1: Grouping

length

1.2

BSP Tree
[0, 7]
[0, 3]

[4, 7]

[0, 1]
[0,.0]

[2, 3]
[1, 1]

[2, 2]

[4, 5]
[3, 3]

[4, 4]

[6, 7]
[5, 5]

[6, 6]

[7, 7]

Figure 2: BSP Tree and its nodes' intervals


, : , ?
array 2 ( 2 , 1 ),
array 2 ( 3, 4, ). O(lg n) (
array ), Figure 2. , O(lg n),
O(lg n) . O(lg n) , O(lg n)
. , 2 , O(lg n) ,
O(lg n) . BSP Tree , O(lg n) O(lg n) BSP Tree
, 2 ,
, .

1.3

(BST)

, , (oine) , BSP Tree ,


. , BSP Tree . Insert
, , Delete Insert . Lower bound
, ,
.

(Implementations)
BSP Tree :
1;
i 2i, 2i + 1

Figure 3
BSP Tree , , . BSP Tree
. , , , lower bound,
. BSP Tree /, ,
:
2

1
12+1

12

2
22+1

22

4
42

8.

42+1

52

10

32+1

32

6
52+1

11

62

12

7
62+1

13

72

14

72+1

15

Figure 3: BSP Tree and its nodes' indices


1. , .
2. , (1) , .
3. , , . -,
i, : coveri ,mini . , ,
, , ; .
, , updatei ,
timei , , , . BSP Tree , Algorithm
1, , , , etc. BSP Tree , k BSP Tree,
O(nk ), O(lgk n). .
Algorithm 1 General operations on a BSP Tree
1: procedure O(ts , te , qs , qe , i, T )
2:
if qs = ts qe = te then
3:
m (ts + te )/2
4:
if qe m then
5:
O(ts , m, qs , qe , 2i, T )
6:
else if qs m then
7:
O(m, te , qs , qe , 2i + 1, T )
8:
else
9:
O(ts , m, qs , m, 2i, T )
10:
O(m, te , m, qe , 2i + 1, T )
11:
end if
12:
end if
13: end procedure

end
8
4
2

1.

Figure 4: BIT Range Operations

(Variations and Generalizations)

3.1

Binary Indexed Tree

BSP Tree , , : Binary Indexed


Tree(BIT). BIT , .
1. , (0, k] ,
2. (0, k] , .
3. ( [1, j] [1, i] (i, j]. : (i, j] )
, , ,
(0, i] . : (2j2i , (2j + 1)2i ]. , i, j ,
( n 2 ). [1..n],
, . , ? index
k = 2i j (j odd) , (k 2i , k] , i k lowbit,
0 . k lowbit(k) : 2l = k&(k).
(0, k] BIT , :
(0, k] = (0, k lowbit(k)] (k lowbit(k), k]
, (k lowbit(k), k] BIT k , (0, k lowbit(k)] , Figure
4. , k0 = k, kj = kj1 + lowbit(kj1 ), j , k
, Figure 5. (0, k] , O(lg n). BIT
Algorithm 2, Algorithm 3.

3.2

K-dimensional BSP Tree

BSP Tree 3 , , 2
BSP Tree, , x , x y .
4

end
8
4
2
1.

6
3

Figure 5: BIT Single Operations

Algorithm 2 Single updateson a binary indexed tree


1: procedure U(i, T, n, v)
2:
while i n do
3:
Ti max(Ti , v)
4:
i increases lowbit(i)
5:
end while
6: end procedure

Algorithm 3 Range querieson a binary indexed tree


1: procedure (i, T )
2:
v0
3:
while i > 0 do
4:
v max(Ti , v)
5:
i decreases lowbit(i)
6:
end while
7:
return v
8: end procedure

k . 2 ( 2 )
. 2 BSP Tree , + , ,
,
, . , BSP Tree generalization adtree Octree,
.

(Applications)
BSP Tree , , .

4.1

(Dynamic Programming)

DP ?BSP Tree
, /.

4.2

(Discretization)

, ( 64MB), , f : N 7
R , .

4.3

(Sweep Line)

BSP Tree , , ,
. , sort, .
, R . :
, , r , (i, j) i j (|xi xj | + |yi yj |)
r. BSP Tree, , , BSP Tree
. Br [i] 45 .
|xi xj | + |yi yj | k |(xi + yi ) (xj + yj )| k |(xi yi ) (xj yj )| k
, a xa + ya , y = x , BSP Tree
xa + ya a xa ya . a xa ya .
, BSP Tree , , BSP
Tree , .

4.4

Merge Sort

, (i, j) xi < xj yi < yj zi < zj merge sort ,


x , merge sort y z . [a, b),
[b, c) , x , i [a, b), j [a, b) xi < xj ,
6

sort y , (i, j) [a, b) [b, c) yi < yj , BSP Tree z


. O(n) . DP,
merge sort DP , DP ,
. , merge sort BSP Tree, ( BSP Tree
--), .

4.5

(Time Stamp)

, S[0..n] , ,
. , time[0..n],
S[i] . S[i] time[i] , , S[i]
. . BSP Tree ,
, , ,
, .

4.6

/ (Tree eries)

traverse , i start(a) end(a),


, [start(a), end(a)] a root tree.

Exercise

Easy
Longest Increasing Subsequence
BSP Tree/BIT .
e Bus[POI XII]
n , wi . x, y
.
K-th Continuous Sum
S k. S k .

Normal
Dynamic Maximal Continuous Sum
n , q . .
Rectangle Covering
n . .
e Islands[POI XI]
n . .
Pairs[IOI 2007]
1, 2, 3 n r. r.
Sails[IOI 2007]
n hi ki ,
1 . . ,
.
PATULJCI[COCI 2009/2010 contest 3]
S, q (a, b). S[a..b] .
continued on the next page

Normal (continued)
Mountain[IOI 2005]
n , q , . (a, b, D) [a, b]
D, h , h.
.
Gold Hotel[USACO 2008 Feb.]
n , . i Di ,
Di . , [si , ei ] .
.

Hard
ALADIN[COCI 2009/2010 contest1]
S, q . . (L, R, A, B) S[L]
A mod B, S[L + 1] 2A mod B, , S[R] (R L + 1)A mod B. (L, R)
S[L..R] .
Horizontally Visible Segments[ACM Central European 2001]
n .
. . .
Ice Skates[POI XVI]
[1, n] k . r [r, r + d]
. i ri xi (/) .
.
Tetris 3D[POI XIII]
n D S . (d, s, w, x, y) , , , .
, .
.
continued on the next page

Hard (continued)
Salesman[IOI 2009]
n , (T, L, M ) , , . ,
D, U . 0 , ,
, .
Hermes[IOI 2004]
n . Hermes (0, 0) . Hermes
x y . Hermes .
Pyramid Base[IOI 2008]
M N P , (X1 , Y1 , X2 , Y2 , C) .
, . B,
.
Making the Grade[USACO 2008 Feb.]
Farmer John N , . ,
. FJ ().

|Ak Bk | .
k

Lunatic
Trees[POI XIV]
S[1..n]. i ,

|S[k] S[k + 1]| .

1k<n

(Remarks)

(Segment Tree),
, . BIT .
, (,
).

10

You might also like