<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>O(q) Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/oq/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/oq/</link>
	<description></description>
	<lastBuildDate>Sun, 05 Jan 2020 18:28:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>O(q) Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/oq/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1310. XOR Queries of a Subarray</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1310-xor-queries-of-a-subarray/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1310-xor-queries-of-a-subarray/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Jan 2020 18:23:01 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(n)]]></category>
		<category><![CDATA[O(q)]]></category>
		<category><![CDATA[prefix sum]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6053</guid>

					<description><![CDATA[<p>Given the array&#160;arr&#160;of positive integers and the array&#160;queries&#160;where&#160;queries[i] = [Li,&#160;Ri],&#160;for each query&#160;i&#160;compute the&#160;XOR&#160;of elements from&#160;Li&#160;to&#160;Ri&#160;(that is,&#160;arr[Li]&#160;xor&#160;arr[Li+1]&#160;xor&#160;...&#160;xor&#160;arr[Ri]&#160;). Return an array containing the result for the given&#160;queries.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1310-xor-queries-of-a-subarray/">花花酱 LeetCode 1310. XOR Queries of a Subarray</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1310. XOR Queries of a Subarray - 刷题找工作 EP294" width="500" height="375" src="https://www.youtube.com/embed/mCppc1CAshk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given the array&nbsp;<code>arr</code>&nbsp;of positive integers and the array&nbsp;<code>queries</code>&nbsp;where&nbsp;<code>queries[i] = [L<sub>i,&nbsp;</sub>R<sub>i</sub>]</code>,&nbsp;for each query&nbsp;<code>i</code>&nbsp;compute the&nbsp;<strong>XOR</strong>&nbsp;of elements from&nbsp;<code>L<sub>i</sub></code>&nbsp;to&nbsp;<code>Ri</code>&nbsp;(that is,&nbsp;<code>arr[L<sub>i</sub>]&nbsp;<strong>xor</strong>&nbsp;arr[L<sub>i+1</sub>]&nbsp;<strong>xor</strong>&nbsp;...&nbsp;<strong>xor</strong>&nbsp;arr[R<sub>i</sub>]</code>&nbsp;). Return an array containing the result for the given&nbsp;<code>queries</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8] 
<strong>Explanation:</strong> 
The binary representation of the elements in the array are:
1 = 0001 
3 = 0011 
4 = 0100 
8 = 1000 
The XOR values for queries are:
[0,1] = 1 xor 3 = 2 
[1,2] = 3 xor 4 = 7 
[0,3] = 1 xor 3 xor 4 xor 8 = 14 
[3,3] = 8
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= arr.length &lt;= 3 *&nbsp;10^4</code></li><li><code>1 &lt;= arr[i] &lt;= 10^9</code></li><li><code>1 &lt;= queries.length &lt;= 3 * 10^4</code></li><li><code>queries[i].length == 2</code></li><li><code>0 &lt;= queries[i][0] &lt;= queries[i][1] &lt; arr.length</code></li></ul>



<h2><strong>Solution: Prefix Sum</strong></h2>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/01/1310-ep294.png" alt="" class="wp-image-6057" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/01/1310-ep294.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/01/1310-ep294-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/01/1310-ep294-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Time complexity: O(n) + O(q)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; xorQueries(vector&lt;int&gt;&amp; arr, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    // xors[i + 1] = arr[0] ^ arr[1] ^ ... ^ arr[i]
    vector&lt;int&gt; xors(arr.size() + 1);
    for (int i = 0; i &lt; arr.size(); ++i) {
      xors[i + 1] = xors[i] ^ arr[i];
    }
    vector&lt;int&gt; ans;
    for (const auto&amp; q : queries) {
      const int l = q[0];
      const int r = q[1];
      ans.push_back(xors[r + 1] ^ xors[l]);
    }
    return ans;
  }
};</pre>
</div></div>tby
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1310-xor-queries-of-a-subarray/">花花酱 LeetCode 1310. XOR Queries of a Subarray</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1310-xor-queries-of-a-subarray/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
