Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
romankurnovskii committed Feb 10, 2023
1 parent d4de967 commit f03e148
Show file tree
Hide file tree
Showing 14 changed files with 981 additions and 88 deletions.
49 changes: 26 additions & 23 deletions content/docs/algorithms-101/algorithms.md
Expand Up @@ -21,14 +21,27 @@ published: true
## Sort

```python
def insertion_sort(a_list):
for i in range(1, len(a_list)):
value = a_list[i]
while i > 0 and a_list[i - 1] > value:
a_list[i] = a_list[i - 1]
i = i - 1
a_list[i] = value
return a_list
def insertion_sort(array):
for i in range(1, len(array)):
value = array[i]
while i > 0 and array[i - 1] > value:
array[i] = array[i - 1]
i -= 1
array[i] = value
return array
```

```python
def selection_sort(array):
for i in range(len(array) - 1):
min_value = i
for j in range(i + 1, len(array)):
if array[j] < array[min_value]:
min_value = j
temp = array[min_value]
array[min_value] = array[i]
array[i] = temp
return array
```


Expand Down Expand Up @@ -354,26 +367,16 @@ def backtrack(some_len_data):
i += 1
```

### Resources
- https://algo.monster/problems/backtracking
**Links:**

## Trie
- https://algo.monster/problems/backtracking

```python
class Node:
def __init__(self, value):
self.value = value
self.children = {}

def insert(self, s, idx):
# idx: index of the current character in s
if idx != len(s):
self.children.setdefault(s[idx], Node(s[idx]))
self.children.get(s[idx]).insert(s, idx + 1)
```

## Resources

- https://www.geeksforgeeks.org/learn-data-structures-and-algorithms-dsa-tutorial/
- https://algo.monster/templates
- https://interviewnoodle.com/grokking-leetcode-a-smarter-way-to-prepare-for-coding-interviews-e86d5c9fe4e1
- [data structures](https://github.com/OpenGenus/cosmos)
- [Competitive Programming Library](https://github.com/cheran-senthil/PyRival)
- [Algorithms for Competitive Programming](https://cp-algorithms.com/)
Expand Up @@ -6,7 +6,7 @@ tags: []
categories: [Algorithms]
series:
date: 2023-02-03
lastmod: 2023-02-07
lastmod: 2023-02-09
featuredImage:
draft: false
weight: 40
Expand Down Expand Up @@ -315,8 +315,64 @@ def solve():
```

### TODO F. Range Update Point Query

https://codeforces.com/contest/1791/problem/F

There are two types of inputs (cases) (in addition to array `a` and `n` of test cases):
1. line with **two** elements: `2 x`. Starts with `2`
2. line with **three** elements: `1 l r`. Starts with `1`

In 1st case: print array `a`

In 2nd case: update the value of `𝑎𝑖` to the sum of its digits.

**Slow Solution:**

```python
def sum_of_digits(n):
sum = 0
while n:
sum += n % 10
n //= 10
return sum

def solve():
n, q = map(int, input().split())
a = list(map(int, input().split()))
while q:
q -= 1
t, *params = map(int, input().split())
if t == 1:
l, r = params
for i in range(l-1, r):
a[i] = sum_of_digits(a[i])
else:
x, = params
print(a[x-1])

t = int(input().strip())
for _ in range(t): # attempts
solve()
```

This solution is slow because of loop:

for i in range(l-1, r):
a[i] = sum_of_digits(a[i])

The key here is the following: after the operation is applied on **a<sub>i</sub>**


**Good to know**

- [Segment Tree template tutorial](../data-structures/segment-tree)
- [A Visual Introduction to Fenwick Tree | medium](https://medium.com/carpanese/a-visual-introduction-to-fenwick-tree-89b82cac5b3c)
- [Fenwick Tree](https://cp-algorithms.com/data_structures/fenwick.html)
- [Segment Tree](https://cp-algorithms.com/data_structures/segment_tree.html)
- [Дерево отрезков | algorithmica](https://ru.algorithmica.org/cs/segment-tree/)
- [Дерево Фенвика | algorithmica](https://ru.algorithmica.org/cs/range-queries/fenwick/)
- [Дерево Фенвика | habr](https://habr.com/ru/post/112828/)

### TODO G1. Teleporters (Easy Version)
https://codeforces.com/contest/1791/problem/G1

Expand All @@ -326,9 +382,21 @@ https://codeforces.com/contest/1791/problem/G2

## Links

- [Python Visualize/Debug code online](https://pythontutor.com/visualize.html#mode=edit)
- [Python collections.Counter](https://docs.python.org/3/library/collections.html#collections.Counter)
- https://github.com/archishmanghos/DSA-Contests/
- https://github.com/debochdilamo/Competative-Programming/tree/CodeForces-Solutions
- https://github.com/DilamoWondimu/Competative-programming/tree/main/CodeForces-Solutions
- https://github.com/hkirat/Algorithmic-Resources
- https://github.com/valentk777/Competitive-Programming/
- https://github.com/valentk777/Competitive-Programming/
- [Competitive Programming Library](https://github.com/cheran-senthil/PyRival)






## Codeforces Round #850 (Div. 2)

https://codeforces.com/contest/1785/
https://codeforces.com/blog/entry/112493
60 changes: 60 additions & 0 deletions content/docs/algorithms-101/codeforces/cp-template.en.md
@@ -0,0 +1,60 @@
---
title: Python template for contests
seoTitle: Python template for contests
description: Python template for contests
toc: true
tags: []
categories: [Algorithms]
series:
date: 2023-02-07
lastmod: 2023-02-09
# featuredImage:
draft: false
weight: 10
---


```python
import math
import os
import sys
import time
from collections import defaultdict, Counter


INF = sys.maxsize

# ------ INPUTS

inp = lambda: sys.stdin.readline().strip().rstrip("\r\n") #read line as string. Ex: input 1 2 3 => '1 2 3'
inpi = lambda: int(inp()) #read input as integer. input 1 => 1
inpl = lambda: list(map(int, inp().split())) #read line as list of integers. Ex: [1, 2, 3]
inp_strl = lambda: list(inp().split()) #read line as list of strings. Ex: ['1', '2', '3']

list_to_string = lambda _v: "".join(map(str, _v)) # [1,2,3] => '123'
list_to_string_list = lambda _v: " ".join(map(str, _v)) # [1,2,'3'] => '1 2 3'


# ------ SOLUTION

def solve():
# input

return


def run():
solve()
print(solve())


if __name__ == "__main__":
LOCAL_TESTS = 0
if os.environ.get("LOCAL_TESTS") or LOCAL_TESTS:
sys.stdin = open("./input.txt", "r")
start_time = time.time()
run()
print("\n--- %s seconds ---\n" % (time.time() - start_time))
else:
run()
```
66 changes: 66 additions & 0 deletions content/docs/algorithms-101/data-structures/_index.en.md
@@ -0,0 +1,66 @@
---
title: Data Structures
description: null
authors:
categories: ['programming', 'Data Structures', 'Codeforces']
tags: ['Data Structures']
# series: null
# featuredImage: null
toc: false
weight: 40
date: 2023-02-09
lastmod: 2023-02-09
published: true
---


### Tree

```python
class Node:
def __init__(self, value):
self.value = value
self.children = {}

def insert(self, s, idx):
# idx: index of the current character in s
if idx != len(s):
self.children.setdefault(s[idx], Node(s[idx]))
self.children.get(s[idx]).insert(s, idx + 1)
```

### Fenwick Tree

```python
class Fenwick: #also known as Binary Indexed Tree (BIT)
def __init__(self, n):
self.n = n
self.bit = [0] * (n+1)
def add(self, idx, val):
while idx <= self.n:
self.bit[idx] += val
idx += idx & -idx
def add_range(self, l, r, val):
self.add(l, val)
self.add(r+1, -val)
def point_query(self, idx):
#Calculates the sum of the elements from the beginning to idx
ret = 0
while idx > 0:
ret += self.bit[idx]
idx -= idx & -idx
return ret
```


- [A Visual Introduction to Fenwick Tree | medium](https://medium.com/carpanese/a-visual-introduction-to-fenwick-tree-89b82cac5b3c)
- [Fenwick Tree](https://cp-algorithms.com/data_structures/fenwick.html)
- [Дерево Фенвика | algorithmica](https://ru.algorithmica.org/cs/range-queries/fenwick/)
- [Дерево Фенвика | habr](https://habr.com/ru/post/112828/)


## Resources

- [data structures](https://github.com/OpenGenus/cosmos)
- [Competitive Programming Library](https://github.com/cheran-senthil/PyRival)
- [Algorithms for Competitive Programming](https://cp-algorithms.com/)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f03e148

Please sign in to comment.