<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://pegah-kh.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://pegah-kh.github.io/" rel="alternate" type="text/html" /><updated>2026-04-14T06:45:21-07:00</updated><id>https://pegah-kh.github.io/feed.xml</id><title type="html">Pegah Khayatan</title><subtitle>personal description</subtitle><author><name>Pegah Khayatan</name></author><entry><title type="html">Future Blog Post</title><link href="https://pegah-kh.github.io/posts/2012/08/blog-post-4/" rel="alternate" type="text/html" title="Future Blog Post" /><published>2199-01-01T00:00:00-08:00</published><updated>2199-01-01T00:00:00-08:00</updated><id>https://pegah-kh.github.io/posts/2012/08/future-post</id><content type="html" xml:base="https://pegah-kh.github.io/posts/2012/08/blog-post-4/"><![CDATA[<p>This post will show up by default. To disable scheduling of future posts, edit <code class="language-plaintext highlighter-rouge">config.yml</code> and set <code class="language-plaintext highlighter-rouge">future: false</code>.</p>]]></content><author><name>Pegah Khayatan</name></author><category term="cool posts" /><category term="category1" /><category term="category2" /><summary type="html"><![CDATA[This post will show up by default. To disable scheduling of future posts, edit config.yml and set future: false.]]></summary></entry><entry><title type="html">CF-969-D1</title><link href="https://pegah-kh.github.io/competitive-programming/blog-post-4/" rel="alternate" type="text/html" title="CF-969-D1" /><published>2024-09-15T00:00:00-07:00</published><updated>2024-09-15T00:00:00-07:00</updated><id>https://pegah-kh.github.io/competitive-programming/blog-post-4</id><content type="html" xml:base="https://pegah-kh.github.io/competitive-programming/blog-post-4/"><![CDATA[<p><strong>C</strong></p>

<p><a href="https://codeforces.com/contest/2006/problem/C">The question </a></p>

<p>Summary of the question: The goal is to count the number of brilliant consecutive subarrays of a given array. What is a brilliant array? an array (that now is no more important if set or array) from which we can arrive at a set of consecutive numbers by taking the mean of two elements in the set (the procedure may obviously be repeated several times). The length of the given array does not exceed 4x1e5.</p>

<p>Initial thoughts:</p>
<ol>
  <li>just counting the number of these subsets is important, not determining them.</li>
  <li>It is interesting thing to notice that a produced number is always the average of 2, 4, 8, … numbers in the set (if we use the same number n times, we count it n times! for example, if we have a,b,c in the set and then (a+b)/2=d and then (d+c)/2=e, then e is actually (a+b+c+c)/4)</li>
  <li>we know what are the lowest and highest numbers in the set, and producing numbers between min and max and like producing numbers, only shifted, between 0 and max-min. Shifting does not change the brilliance of a set!</li>
</ol>

<p>Post-initial thoughts:</p>

<ul>
  <li>still thinking :D</li>
</ul>

<p><strong>B</strong></p>

<p><a href="https://codeforces.com/contest/2006/problem/B">The question </a>
Solved; revision on the algorithm to add …</p>]]></content><author><name>Pegah Khayatan</name></author><category term="competitive-programming" /><summary type="html"><![CDATA[C]]></summary></entry><entry><title type="html">Articulation Points</title><link href="https://pegah-kh.github.io/competitive-programming/articulation-point/" rel="alternate" type="text/html" title="Articulation Points" /><published>2024-09-15T00:00:00-07:00</published><updated>2024-09-15T00:00:00-07:00</updated><id>https://pegah-kh.github.io/competitive-programming/articulation-point</id><content type="html" xml:base="https://pegah-kh.github.io/competitive-programming/articulation-point/"><![CDATA[<h2 id="articulation-points--bridges-in-an-undirected-graph">Articulation Points &amp; Bridges in an Undirected Graph</h2>

<p>[Disclaimer: All (or almost all) that is explained here is from the Steven and Felix Halim and Suhendry Effendy book.]</p>

<p>Articulation point in a graph: if you take out this node, your graph will split up in two parts …..
Bridge: the same thing would happen if you take such an edge out …..</p>

<p>The goal is to find such nodes or edges in an undirected graph.
I have never really used this algorithm in any problem I have solved, as far as I remember :U … but it’s a fun one, like it has those moments where you go : Aaaaaaaaaaaa, that’s why :U (this thing: :U is such a mood)(I should just quit writing otherwise I will just continue jabbering and babbering about nonesense, so, let’s stop it here)</p>

<p>What would be the stupid way to find such vertices or edges? Just take out each node or edge one by one and check if the graph remains connected. The complexity would be O(Vx(V+E)), one is for taking out each node/edge, and the other for an algorithm to run through the graph, BFS or DFS, and check if we can access all nodes starting from one.</p>

<p>But let’s be wiser, and make some changes to DFS to make it serve our goal better; define two numbers to store for each node when applying DFS:</p>
<ol>
  <li><code class="language-plaintext highlighter-rouge">dfs_num</code> : The iteration at which we visit a node</li>
  <li><code class="language-plaintext highlighter-rouge">dfs_low</code> : If <code class="language-plaintext highlighter-rouge">R</code> is the set of nodes in the DFS spanning subtree rooted at <code class="language-plaintext highlighter-rouge">u</code>, then <code class="language-plaintext highlighter-rouge">dfs_low</code> is the lowest <code class="language-plaintext highlighter-rouge">dfs_num</code> that can be found in <code class="language-plaintext highlighter-rouge">R</code> <strong>OR</strong> among the ones not in <code class="language-plaintext highlighter-rouge">R</code> but reachable by a back edge from <code class="language-plaintext highlighter-rouge">R</code>.</li>
</ol>

<p><img src="/images/articulations.png" alt="alt text" /></p>

<p>Let’s say we have done DFS over the graph and now we have all the numbers we wanted to store.
consider a vertex <code class="language-plaintext highlighter-rouge">u</code> connnected to <code class="language-plaintext highlighter-rouge">v</code>, and add the condition <code class="language-plaintext highlighter-rouge">dfs_low(v)</code> $\geq$ <code class="language-plaintext highlighter-rouge">dfs_num(u)</code>. What does this mean?</p>

<p><img src="/images/articulation_dfs_num.png" alt="alt text" /></p>

<p>This means that we have not been able to find a lower <code class="language-plaintext highlighter-rouge">dfs_num</code> in the DFS subtree rooted at <code class="language-plaintext highlighter-rouge">v</code> or among those connected to it by a back edge. In other words, the nodes visited during DFS after visiting the node <code class="language-plaintext highlighter-rouge">v</code> all have higher or equal <code class="language-plaintext highlighter-rouge">dfs_num</code>s than that of <code class="language-plaintext highlighter-rouge">v</code>, which also means that there is no back edge from nodes visited in DFS after <code class="language-plaintext highlighter-rouge">v</code> back to the nodes visited before that point, and hence <code class="language-plaintext highlighter-rouge">u</code> is an articulation point!
<strong>Exceptional Case</strong>: when the starting node of DFS has only one neighbor, it is obviously an articulation point; but, we cannot detectc it using the above process since it has the lowest <code class="language-plaintext highlighter-rouge">dfs_num</code> of all.</p>

<p><strong>Detecting Bridges</strong> is similar, but the condition to check on vertices changes just a bit: <code class="language-plaintext highlighter-rouge">dfs_low(v)</code> $&gt;$ <code class="language-plaintext highlighter-rouge">dfs_num(u)</code> . There is no equal as you can observe :U</p>

<p>Why?</p>]]></content><author><name>Pegah Khayatan</name></author><category term="competitive-programming" /><summary type="html"><![CDATA[Articulation Points &amp; Bridges in an Undirected Graph]]></summary></entry><entry><title type="html">Fenwick Tree</title><link href="https://pegah-kh.github.io/competitive-programming/binary-nums/" rel="alternate" type="text/html" title="Fenwick Tree" /><published>2024-09-15T00:00:00-07:00</published><updated>2024-09-15T00:00:00-07:00</updated><id>https://pegah-kh.github.io/competitive-programming/binary-nums</id><content type="html" xml:base="https://pegah-kh.github.io/competitive-programming/binary-nums/"><![CDATA[<h2 id="fenwick-tree-binary-indexed-tree">Fenwick Tree (Binary Indexed Tree)</h2>

<p>A <strong>Fenwick Tree</strong> (also <strong>Binary Indexed Tree (BIT)</strong>) is a useful data structure to  <strong>calculate sum of a consecutive subset</strong> (also known as Range Sum Query (RSQ)) in an ordered set.</p>

<p>The advantage is that we can use it to update elements efficiently, in <strong>O(log n)</strong>.</p>

<p>Another cool thing: we can use bit manipulation techniques for more efficiency.</p>

<p>Each node is responsible for the sum of elements in a coonsecutive subset; which one? the node $i$ keeps the sum for the subset $[(i - LSOne(i)+1), …, i]$. ($LSOne(i)$ for a binary number gives the least significant bit that is one. For example, for $6$ it would be $2$.)</p>

<p>Why is it structured like this? becuase then we can easily and efficiently:</p>
<ol>
  <li>compute $rsq(i,j)$</li>
  <li>update the value of an item at index $i$ (and the tree accordingly)</li>
</ol>

<p>First, if we want to compute $rsq(i,j)$, we can just calculate $rsq(1,j)$ and $rsq(1, i-1)$ and the substract them from eachother; so it boils down to computing $rsq(1,i)$.</p>

<p>if $i^{\prime} = i - LSOne(i)$ (what is does is simply removing the least significant one-bit and keeping the rest) did you what happened there? :D we had the node $i$ responsible for the subset $[(i - LSOne(i)+1), …, i]$, and then the node $i^{\prime}$ is responsible for $[(i^{\prime} - LSOne(i^{\prime})+1), …, (i - LSOne(i))]$, and … ; so, if we continue like that, till reaching zero, we get the sum of all elements from $1$ to $i$.
\(rsq(1,i) = n_{i} + n_{i^{\prime}} + n_{i^{\prime \prime}} ...\)</p>

<p>The complexity? $O(log n)$.</p>

<p>Secondly, we want to update the element at the index $i$, Let’s say we want to add $v$ to it : what nodes would this update affect? all the nodes such as $j$ where $ i \in [(j - LSOne(j)+1), …, j]$; what are those? those that when you drop their least significant one-bit are less than $i$ but are larger than $i$: this part is slightly more tricky to digest, but the logic behind it is that we shouold start from $i$ and add something to it to push the least significant one-bit further. It is beacuse as long as this one-bit goes further, the remaining number would be larger than $i$ after removing it. To push this one-bit further, we can add the value of the least significant one-bit to it: $[(i + LSOne(i)), (i + LSOne(i) + LSOne(i + LSOne(i))), …]$</p>

<p>The complexity? again $O(log n)$.</p>

<h3 id="simple-implementation">Simple Implementation</h3>
<p>The implementation is taken from the book Competitive Programmingn book 1, by Steven and Felix Halim and Suhendry Effendy.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="cp">#define LSOne(S)   ((S) &amp; (-S))
</span><span class="k">typedef</span> <span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">vi</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">FenwickTree</span> <span class="p">{</span>
<span class="nl">private:</span>
    <span class="n">vi</span> <span class="n">ft</span><span class="p">;</span>

<span class="nl">public:</span>
    <span class="n">FenwickTree</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">ft</span><span class="p">.</span><span class="n">assign</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>  <span class="c1">// Fenwick Tree is 1-based index, so size n+1</span>
    <span class="p">}</span>

    <span class="c1">// Function to update the Fenwick Tree with a new value at index idx</span>
    <span class="kt">void</span> <span class="n">update</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="kt">int</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">while</span> <span class="p">(</span><span class="n">idx</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">ft</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">+=</span> <span class="n">value</span><span class="p">;</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="n">LSOne</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>  <span class="c1">// Move to the next responsible node</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="c1">// Function to compute the prefix sum from index 1 to idx</span>
    <span class="kt">int</span> <span class="n">query</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
        <span class="k">while</span> <span class="p">(</span><span class="n">i</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">sum</span> <span class="o">+=</span> <span class="n">ft</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
            <span class="n">i</span> <span class="o">-=</span> <span class="n">LSOne</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>  <span class="c1">// Move to the parent node</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">sum</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="c1">// Function to get the sum in the range [l, r]</span>
    <span class="kt">int</span> <span class="n">rsqe</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="kt">int</span> <span class="n">j</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="n">query</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="o">-</span> <span class="n">query</span><span class="p">(</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">};</span>
</code></pre></div></div>

<p>Things to still add: Other operations that can be done on Fenwick Tree, their complexity, and the complete implementation.</p>]]></content><author><name>Pegah Khayatan</name></author><category term="competitive-programming" /><summary type="html"><![CDATA[Fenwick Tree (Binary Indexed Tree)]]></summary></entry><entry><title type="html">Blog Post number 3</title><link href="https://pegah-kh.github.io/posts/2014/08/blog-post-3/" rel="alternate" type="text/html" title="Blog Post number 3" /><published>2014-08-14T00:00:00-07:00</published><updated>2014-08-14T00:00:00-07:00</updated><id>https://pegah-kh.github.io/posts/2014/08/blog-post-3</id><content type="html" xml:base="https://pegah-kh.github.io/posts/2014/08/blog-post-3/"><![CDATA[<p>This is a sample blog post. Lorem ipsum I can’t remember the rest of lorem ipsum and don’t have an internet connection right now. Testing testing testing this blog post. Blog posts are cool.</p>

<h1 id="headings-are-cool">Headings are cool</h1>

<h1 id="you-can-have-many-headings">You can have many headings</h1>

<h2 id="arent-headings-cool">Aren’t headings cool?</h2>]]></content><author><name>Pegah Khayatan</name></author><category term="cool posts" /><category term="category1" /><category term="category2" /><summary type="html"><![CDATA[This is a sample blog post. Lorem ipsum I can’t remember the rest of lorem ipsum and don’t have an internet connection right now. Testing testing testing this blog post. Blog posts are cool.]]></summary></entry><entry><title type="html">Blog Post number 2</title><link href="https://pegah-kh.github.io/posts/2013/08/blog-post-2/" rel="alternate" type="text/html" title="Blog Post number 2" /><published>2013-08-14T00:00:00-07:00</published><updated>2013-08-14T00:00:00-07:00</updated><id>https://pegah-kh.github.io/posts/2013/08/blog-post-2</id><content type="html" xml:base="https://pegah-kh.github.io/posts/2013/08/blog-post-2/"><![CDATA[<p>This is a sample blog post. Lorem ipsum I can’t remember the rest of lorem ipsum and don’t have an internet connection right now. Testing testing testing this blog post. Blog posts are cool.</p>

<h1 id="headings-are-cool">Headings are cool</h1>

<h1 id="you-can-have-many-headings">You can have many headings</h1>

<h2 id="arent-headings-cool">Aren’t headings cool?</h2>]]></content><author><name>Pegah Khayatan</name></author><category term="cool posts" /><category term="category1" /><category term="category2" /><summary type="html"><![CDATA[This is a sample blog post. Lorem ipsum I can’t remember the rest of lorem ipsum and don’t have an internet connection right now. Testing testing testing this blog post. Blog posts are cool.]]></summary></entry></feed>