Professional Documents
Culture Documents
Bytes
1
1
2
4
8
n
Type
unsigned char
unsigned short
unsigned int
unsigned long long
Type
float
double
Bytes
4
8
Min value
Max value
-128
-32768
-2148364748
-9223372036854775808
28n1
127
32767
2147483647
9223372036854775807
28n1 1
Bytes
1
2
4
8
n
Min value
0
0
0
0
0
Min value
3.4 1038
1.7 10308
Max value
255
65535
4294967295
18446744073709551615
28n 1
Max value
3.4 1038
1.7 10308
Precision
7 digits
14 digits
Big integers
http://uva.onlinejudge.org/external/4/424.html
Efficient querying
Efficient inserting
Efficient deleting
Efficient updating
Static arrays
Dynamic arrays
Linked lists
Stacks
Queues
Priority Queues
Sets
Maps
Representing sets
10
Representing sets
Empty set:
0
Single element set:
1<<i
The universe set (i.e. all elements):
(1<<n)-1
Union of sets:
x|y
Intersection of sets:
x&y
Complement of a set:
~x & ((1<<n)-1)
11
Representing sets
12
Representing sets
13
14
http://uva.onlinejudge.org/external/119/11988.html
15
Applications of Stacks
16
Applications of Queues
17
18
Applications of Sets
19
Applications of Maps
20
21
Count number of
elements < x
Find the kth smallest
element
33
We have a binary
search tree and want
to efficiently:
Naive method is to go
through all vertices,
but that is slow: O(n)
15
47
10
20
18
51
38
36
34
39
49
37
22
33, 14
15, 5
47, 8
10, 2
20, 2
5, 1
18, 1
38, 5
36, 3
34, 1
51, 2
39, 1
49, 1
37, 1
23
Count number of
elements < 38
Search for 38 in the
tree
Count the vertices
that we pass by that
are less than x
When we are at a
vertex where we
should go right, get
the size of the left
subtree and add it to
our count
33, 14
15, 5
47, 8
10, 2
20, 2
5, 1
18, 1
38, 5
36, 3
34, 1
51, 2
39, 1
49, 1
37, 1
24
Count number of
elements < 38
Search for 38 in the
tree
Count the vertices
that we pass by that
are less than x
When we are at a
vertex where we
should go right, get
the size of the left
subtree and add it to
our count
33, 14
15, 5
47, 8
10, 2
20, 2
5, 1
18, 1
38, 5
36, 3
34, 1
51, 2
39, 1
49, 1
37, 1
Time complexity
O(log n)
25
33, 14
15, 5
47, 8
10, 2
20, 2
5, 1
18, 1
38, 5
36, 3
34, 1
51, 2
39, 1
49, 1
37, 1
26
33, 14
15, 5
47, 8
10, 2
20, 2
5, 1
18, 1
38, 5
36, 3
34, 1
51, 2
39, 1
49, 1
37, 1
Example: k = 11
27
Representing graphs
28
Adjacency list
0:
1:
2:
3:
1, 2
0, 2
0, 1, 3
2
vector<int> adj[4];
adj[0].push_back(1);
adj[0].push_back(2);
adj[1].push_back(0);
adj[1].push_back(2);
adj[2].push_back(0);
adj[2].push_back(1);
adj[2].push_back(2);
adj[3].push_back(2);
29
Adjacency matrix
0
1
1
0
1
0
1
0
1
1
0
1
0
0
1
0
bool adj[4][4];
adj[0][1] = true;
adj[0][2] = true;
adj[1][0] = true;
adj[1][2] = true;
adj[2][0] = true;
adj[2][1] = true;
adj[2][3] = true;
adj[3][2] = true;
30
Edge list
0,
0,
1,
2,
1
2
2
3
31
Efficiency
Storage
Add vertex
Add edge
Remove vertex
Remove edge
Query: are u, v adjacent?
Adjacency list
O(|V| + |E|)
O(1)
O(1)
O(|E|)
O(|E|)
O(|V|)
Adjacency matrix
O(|V|2 )
O(|V|2 )
O(1)
O(|V|2 )
O(1)
O(1)
Edge list
O(|E|)
O(1)
O(1)
O(|E|)
O(|E|)
O(|E|)
32
http://uva.onlinejudge.org/external/119/11991.html
33