An open API service indexing awesome lists of open source software.

https://github.com/jhasuraj01/cp

Competitive Programming Library
https://github.com/jhasuraj01/cp

array cpp dsa graph tree

Last synced: 9 months ago
JSON representation

Competitive Programming Library

Awesome Lists containing this project

README

          

# Competitive Programming Library

## Test
```cpp
g++ ./tests/main.cpp -std=c++17 -o temp && ./temp && rm ./temp
```

## Data Structures

Disjoint Set: Union by Rank

### Initialization:
- Time Complexity: O(N)
- Space Complexity: O(N)

```cpp
DisjointSet dsu(size);
```

### Operations

#### Find Set Representative
- Avg. Time Complexity: O(1)
- Time Complexity: O(log N)
- Space Complexity: O(1)

```cpp
dsu.find_set(int i);
```

#### Merge Set
- Avg. Time Complexity: O(1)
- Time Complexity: O(log N)
- Space Complexity: O(1)

```cpp
dsu.merge_set(int i, int j)
```

#### Check If Same Set
- Avg. Time Complexity: O(1)
- Time Complexity: O(log N)
- Space Complexity: O(1)

```cpp
dsu.is_same_set(int i, int j)
```

Segment Tree

### Initialization:
- Time Complexity: O(N)
- Space Complexity: O(N)

```cpp
SegmentTree segment_tree(arr, 0, [&](int a, int b) {
return a+b;
});
```

### Operations

#### Point Update
- Time Complexity: O(log N)
- Space Complexity: O(1)

```cpp
segment_tree.update(i, value);
```

#### Range Query
- Time Complexity: O(log N)
- Space Complexity: O(log N)

```cpp
segment_tree.get(i, j)
```

Static Hash

### Examples:
1. `StaticHash hash;`
2. `StaticHash hash(31);`
3. `StaticHash hash(31, 'a');`
4. `StaticHash hash(31, 'a', 1e9+9);`

Rolling Hash

### Examples:
1. `RollingHash hash;`
2. `RollingHash hash(31);`
3. `RollingHash hash(31, 'a');`
4. `RollingHash hash(31, 'a', 1e9+9);`

Custom Stack

### Usage
Can be used to build `min`/`max`/`sum`/`xor` Stack using any appropriate associative function

### Initialization:
```cpp
// Max Stack
CustomStack st(0, [&](int a, int b) {
return std::max(a, b);
});
```

### Operations:
1. `st.custom_value()`
2. `st.empty()`
3. `st.push(value)`
4. `st.pop()`
5. `st.size()`
6. `st.top()`

Custom Queue

### Usage
Can be used to build `min`/`max`/`sum`/`xor` Queue using any appropriate associative function

### Initialization:
```cpp
// Max Queue
CustomQueue qu(0, [&](int a, int b) {
return std::max(a, b);
});
```

### Operations:
1. `qu.custom_value()`
2. `qu.empty()`
3. `qu.front()`
4. `qu.push(value)`
5. `qu.pop()`
6. `qu.size()`