You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/algebra/big-integer.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,13 +16,13 @@ Here we describe long arithmetic for only non-negative integers. To extend the a
16
16
17
17
We'll store numbers as a `vector<int>`, in which each element is a single "digit" of the number.
18
18
19
-
```
19
+
```cpp
20
20
typedef vector<int> lnum;
21
21
```
22
22
23
23
To improve performance we'll use $10^9$ as the base, so that each "digit" of the long number contains 9 decimal digits at once.
24
24
25
-
```
25
+
```cpp
26
26
constint base = 1000*1000*1000;
27
27
```
28
28
@@ -32,7 +32,7 @@ Digits will be stored in order from least to most significant. All operations wi
32
32
33
33
Printing the long integer is the easiest operation. First we print the last element of the vector (or 0 if the vector is empty), followed by the rest of the elements padded with leading zeros if necessary so that they are exactly 9 digits long.
34
34
35
-
```
35
+
```cpp
36
36
printf ("%d", a.empty() ? 0 : a.back());
37
37
for (int i=(int)a.size()-2; i>=0; --i)
38
38
printf ("%09d", a[i]);
@@ -44,7 +44,7 @@ Note that we cast `a.size()` to integer to avoid unsigned integer underflow if v
44
44
45
45
To read a long integer, read its notation into a `string` and then convert it to "digits":
46
46
47
-
```
47
+
```cpp
48
48
for (int i=(int)s.length(); i>0; i-=9)
49
49
if (i < 9)
50
50
a.push_back (atoi (s.substr (0, i).c_str()));
@@ -54,7 +54,7 @@ for (int i=(int)s.length(); i>0; i-=9)
54
54
55
55
If we use an array of `char` instead of a `string`, the code will be even shorter:
56
56
57
-
```
57
+
```cpp
58
58
for (int i=(int)strlen(s); i>0; i-=9) {
59
59
s[i] = 0;
60
60
a.push_back (atoi (i>=9 ? s+i-9 : s));
@@ -63,7 +63,7 @@ for (int i=(int)strlen(s); i>0; i-=9) {
63
63
64
64
If the input can contain leading zeros, they can be removed as follows:
Copy file name to clipboardExpand all lines: src/algebra/gray-code.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ This code was invented by Frank Gray in 1953.
12
12
13
13
Let's look at the bits of number $n$ and the bits of number $G(n)$. Notice that $i$-th bit of $G(n)$ equals 1 only when $i$-th bit of $n$ equals 1 and $i + 1$-th bit equals 0 or the other way around ($i$-th bit equals 0 and $i + 1$-th bit equals 1). Thus, $G(n) = n \oplus (n >> 1)$:
14
14
15
-
```
15
+
```cpp
16
16
intg (int n) {
17
17
return n ^ (n >> 1);
18
18
}
@@ -32,7 +32,7 @@ We will move from the most significant bits to the least significant ones (the l
Copy file name to clipboardExpand all lines: src/data_structures/sqrt-tree.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -186,7 +186,7 @@ Note that when we do the recursive call, we do prefix or suffix $\text{massUpdat
186
186
187
187
The following implementation of Sqrt Tree can perform the following operations: build in $O(n \cdot \log \log n)$, answer queries in $O(1)$ and update an element in $O(\sqrt{n})$.
Copy file name to clipboardExpand all lines: src/data_structures/treap.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ We implement **Build** operation with $O (N \log N)$ complexity using $N$ **Inse
53
53
54
54
## Implementation
55
55
56
-
```
56
+
```cpp
57
57
structitem {
58
58
int key, prior;
59
59
item * l, * r;
@@ -113,7 +113,7 @@ To extend the functionality of the treap, it is often necessary to store the num
113
113
114
114
When a tree changes (nodes are added or removed etc.), `cnt` of some nodes should be updated accordingly. We'll create two functions: `cnt()` will return the current value of `cnt` or 0 if the node does not exist, and `upd_cnt()` will update the value of `cnt` for this node assuming that for its children L and R the values of `cnt` have already been updated. Evidently it's sufficient to add calls of `upd_cnt()` to the end of `insert`, `erase`, `split` and `merge` to keep `cnt` values up-to-date.
115
115
116
-
```
116
+
```cpp
117
117
int cnt (pitem t) {
118
118
return t ? t->cnt : 0;
119
119
}
@@ -147,7 +147,7 @@ Now it's clear how to calculate the implicit key of current node quickly. Since
147
147
148
148
Here are the new implementations of **Split** and **Merge**:
149
149
150
-
```
150
+
```cpp
151
151
voidmerge (pitem & t, pitem l, pitem r) {
152
152
if (!l || !r)
153
153
t = l ? l : r;
@@ -187,7 +187,7 @@ Now let's consider the implementation of various operations on implicit treaps:
187
187
188
188
Here is an example implementation of the implicit treap with reverse on the interval. For each node we store field called `value` which is the actual value of the array element at current position. We also provide implementation of the function `output()`, which outputs an array that corresponds to the current state of the implicit treap.
We can choose a point $O$ arbitrarily, iterate over all edges adding the oriented area of the triangle formed by the edge and point $O$. Again, due to the sign of area, extra area will be reduced.
29
29
30
-
This method is better as it can be generalized to more complex cases (such as when some sides are arcs instead of straight lines)
30
+
This method is better as it can be generalized to more complex cases (such as when some sides are arcs instead of straight lines)
Copy file name to clipboardExpand all lines: src/graph/strongly-connected-components.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,6 +49,7 @@ Algorithm asymptotics is $O(n + m)$, because it is just two depth (breadth) firs
49
49
Finally, it is appropriate to mention [topological sort](http://e-maxx-eng.appspot.com/graph/topological-sort.html) here. First of all, step 1 of the algorithm represents topological sort of graph $G$ (actually this is exactly what vertices' sort by exit time means). Secondly, the algorithm's scheme generates strongly connected components by decreasing order of their exit times, thus it generates components - vertices of condensation graph - in topological sort order.
50
50
51
51
## Implementation
52
+
```cpp
52
53
vector < vector<int> > g, gr;
53
54
vector<char> used;
54
55
vector<int> order, component;
@@ -93,6 +94,7 @@ Finally, it is appropriate to mention [topological sort](http://e-maxx-eng.appsp
93
94
}
94
95
}
95
96
}
97
+
```
96
98
97
99
Here, $g$ is graph, $gr$ is transposed graph. Function $dfs1$ implements depth first search on graph $G$, function $dfs2$ - on transposed graph $G^T$. Function $dfs1$ fills the list $order$ with vertices in increasing order of their exit times (actually, it is making a topological sort). Function $dfs2$ stores all reached vertices in list $component$, that is going to store next strongly connected component after each run.
0 commit comments