1 of 20

Contest takeup & Segtree

By Alan, Peter, ChrisT, and Junyi

2 of 20

P1: A Unique List

https://mcpt.ca/problem/duplicate

Remove duplicates from an array of length N (1 ≤ N ≤ 8), with elements ai (1 ≤ ai ≤ N).

3 of 20

Solution

  • Using a set is the most simple solution
    • Keep inserting the values and the set will continue to add them, but will ignore the duplicates
    • Then sort the set and print it out
  • Other things can be used such as ArrayLists for Java or just arrays in general
    • Use an if statement to check for duplicates and then sort
    • Or sort array, and check if the current one is the same as the one before it

4 of 20

P2: A Squirrel Problem

https://mcpt.ca/problem/asquirrelproblem

Perform range updates and point queries in sublinear time.

5 of 20

Solution

Binary Indexed Tree on a difference array.

Use the binary indexed tree to speed up updates and sum queries.

Update the difference array to perform a range update.

Perform a sum query on the difference array to do a point query.

Second solution: Lazy segment tree (more advanced)

6 of 20

BIT Code

7 of 20

P3: Component Sum

https://mcpt.ca/problem/components

You are given an empty graph, with two types of queries.

Add an edge between x and y.

Find the sum of sizes of components that have size <= K.

8 of 20

P3: Component Sum

The idea here is to maintain sizes of components using disjoint set union.

When merging two nodes, update the size of the new component to be the sum of the old components.

To query the sum of component sizes <= K, you also must maintain component sizes in a binary indexed tree.

9 of 20

Disjoint-Set Union

10 of 20

Segment Tree Motivation Problem

Given an array A, support two operations:

Increase a value in the array by v.

Find the minimum value in the subarray [L,R]

11 of 20

Explanation

  • Segment tree is a binary tree
  • Every node represents the value of a segment (for example, the sum or range minimum)
  • The children of the segment is the segment split in half
  • Every leaf node is a segment of length 1
  • The root is the whole array

12 of 20

Update

  • Update the current node’s value.
  • If the current node is not a leaf, determine which child the index being updated is contained in.
  • Recursively call update function on that child.
  • Update the current node based on its children’s values.

13 of 20

Query

  • Determine whether the range of the current node is completely disjoint from that of the query. If so, return negative infinity (some small value).
  • Determine whether the range of the current node is completely contained inside that of the query. If so return the value at this node.
  • Recursively call query on both of the children of this node, and return the maximum.

14 of 20

Segment Tree Code

15 of 20

Example

Array = {2 , 5 , 1 , 4 , 9 , 3}

Query minimum in 0-indexed range [1,4]

16 of 20

Example

17 of 20

Example

18 of 20

Example

19 of 20

Example

20 of 20

Example

Minimum = 1