id
int64 1
3.58k
| problem_description
stringlengths 516
21.8k
| instruction
int64 0
3
| solution_c
dict |
---|---|---|---|
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "const int maxn = 2002;\nint ans[maxn][maxn], best[maxn][maxn];\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& queries) {\n int n = a.size();\n for(int i = 0; i < n; i++){\n ans[i][i] = a[i];\n best[i][i] = a[i];\n }\n for(int d = 1; d < n; d++){\n for(int i = 0; i + d < n; i++){\n ans[i][i + d] = ans[i][i + d - 1] ^ ans[i + 1][i + d];\n best[i][i + d] = max({ans[i][i + d], best[i][i + d - 1], best[i + 1][i + d]});\n }\n }\n vector<int> res;\n for(auto q : queries){\n res.push_back(best[q[0]][q[1]]);\n }\n return res;\n }\n};",
"memory": "331748"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "const int inf = 1e9; \nclass SegTree {\n public: \n #define left (p<<1) \n #define right (left|1)\n #define mid ((l+r)>>1)\n #define toleft left,l,mid\n #define toright right,mid+1,r\n int n; \n vector<int> t; \n SegTree(int n):n(n),t(4*n, 0){}\n void update(int p,int l,int r,int ind,int val) {\n if(ind>r||ind<l)return void(); \n if(l==r)return t[p]=max(t[p],val),void(); \n update(toleft,ind,val); update(toright,ind,val); \n t[p] = mer(t[left], t[right]); \n }\n \n int query(int p,int l,int r,int i, int j) {\n if(i>r||j<l)return 0; \n if(i<=l&&r<=j)return t[p]; \n return mer(query(toleft,i,j), query(toright,i,j)); \n }\n \n int mer (int a,int b){\n return max(a,b); \n }\n \n #undef toright \n #undef toleft \n #undef mid\n #undef right\n #undef left \n}; \nconst int N = 2e3+1; \nint a[N][N]; \nint mx[N][N]; \nclass Solution {\npublic:\n \n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size(); \n memset(a, 0, sizeof(a));\n memset(mx, 0, sizeof(mx)); \n for(int i=0;i<nums.size();i++){\n a[i][i] = nums[i]; \n mx[i][i] = nums[i]; \n }\n for(int size = 2;size<=n;size++){\n for(int i=0;i+size<=n;i++) {\n a[i][i+size-1] = (a[i+1][i+size-1]^a[i][i+size-2]);\n mx[i][i+size-1] = max({a[i][i+size-1], mx[i+1][i+size-1], mx[i][i+size-2]}); \n }\n }\n vector<int> ans; \n for(auto q:queries){\n ans.push_back(mx[q[0]][q[1]]); \n }\n return ans; \n }\n};",
"memory": "334594"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": " int dp[2001][2001];\n int xorScore[2001][2001];\n vector<int> a;\nclass Solution {\npublic:\n int maket(int i, int j){\n if(i==j){\n xorScore[i][j] = a[i];\n return a[i];\n }\n if(xorScore[i][j] == -1){\n xorScore[i][j] = (maket(i, j-1)^maket(i+1, j));\n }\n return xorScore[i][j];\n }\n\n int make(int i, int j){\n if(i==j){\n dp[i][j] = a[i];\n return a[i];\n }\n if(i>j)\n return INT_MAX;\n if(dp[i][j] == -1){\n dp[i][j] = max(max(maket(i, j), make(i+1, j)), max(maket(i, j), make(i, j-1)));\n }\n return dp[i][j];\n }\n\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n a = nums;\n for(int i=0; i<a.size(); i++){\n for(int j=0; j<a.size(); j++){\n dp[i][j] = -1;\n xorScore[i][j] = -1;\n }\n }\n vector<int> ans;\n for(auto i : queries){\n ans.push_back(make(i[0], i[1]));\n }\n return ans;\n }\n};",
"memory": "334594"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& queries) {\n int n=a.size();\n int val[n][n];\n for(int len=0;len<n;len++){\n for(int i=0;i<n;i++){\n if(i+len>=n)continue;\n if(len==0){\n val[i][i]=a[i];\n }\n else{\n val[i][i+len]=val[i][i+len-1]^val[i+1][i+len];\n }\n }\n }\n int ans[n][n];\n for(int i=0;i<n;i++){\n ans[i][i]=val[i][i];\n }\n for(int len=1;len<n;len++){\n for(int i=0;i<n;i++){\n if(i+len>=n)continue;\n ans[i][i+len]=max({ans[i][i+len-1],ans[i+1][i+len],val[i][i+len]});\n }\n }\n vector<int>v;\n for(vector<int> v1 : queries){\n v.push_back(ans[v1[0]][v1[1]]);\n }\n return v;\n }\n};",
"memory": "337440"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n int dp[n][n];\n int mi[n][n];\n for(int i=0;i<n;i++){\n for(int j=0;i+j<n;j++){\n if(!i){\n dp[j][j]=nums[j];\n mi[j][j]=dp[j][j];\n }else{\n dp[j][i+j]=dp[j][i+j-1]^dp[j+1][i+j];\n mi[j][i+j]=dp[j][i+j];\n mi[j][i+j]=max(mi[j][i+j],mi[j][i+j-1]);\n mi[j][i+j]=max(mi[j][i+j],mi[j+1][i+j]);\n }\n }\n }\n vector<int>ans;\n for(auto x:queries){\n ans.push_back(mi[x[0]][x[1]]);\n }\n return ans;\n }\n};",
"memory": "337440"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint x[2001][2001];\nint dp[2001][2001];\nint n;\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n memset(dp, 0, sizeof(dp));\n memset(x, 0, sizeof(x));\n n=nums.size();\n for(int i=0;i<n;i++)x[i][i]=dp[i][i]=nums[i];\n for(int i=0;i<n-1;i++){\n x[i][i+1]=nums[i]^nums[i+1];\n dp[i][i+1]=max({nums[i+1], nums[i], x[i][i+1]});\n }\n int len=3;\n while(len<=n){\n for(int i=0;i<n-len+1;i++){\n x[i][i+len-1]=x[i+1][i+len-1]^x[i][i+len-2];\n dp[i][i+len-1] = max({x[i][i+len-1], dp[i+1][i+len-1], dp[i][i+len-2]});\n }\n len++;\n }\n vector<int> ans;\n for(auto q:queries)ans.push_back(dp[q[0]][q[1]]);\n return ans;\n\n }\n};",
"memory": "340286"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[2001][2001][2]={0};\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& q) {\n int n=nums.size();\n for(int i=0;i<n;i++){\n dp[0][i][0]=nums[i];\n dp[0][i][1]=nums[i];\n }\n for(int i=1;i<n;i++){\n for(int j=i;j<n;j++){\n dp[i][j][1]=dp[i-1][j][1] ^ dp[i-1][j-1][1];\n dp[i][j][0]=max({ dp[i][j][1], dp[i-1][j][0], dp[i-1][j-1][0] });\n }\n }\n vector<int> ans;\n for(auto it:q){\n int x=it[1]-it[0];\n int y=it[1];\n ans.push_back(dp[x][y][0]);\n }\n return ans;\n }\n};",
"memory": "340286"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n \n int B[2002][2002];\n \n int dp[2002][2002];\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n \n for(int i=0;i<=n;i++) {\n for(int j=0;j<=i;j++) {\n if (j==0) {\n B[i][j]=1;\n } else {\n B[i][j]=B[i-1][j]+B[i-1][j-1];\n }\n B[i][j]%=2;\n }\n }\n \n \n // a1,a2,a3,a4,a5\n // a1^a2,a2^a3,a3^a4,a4^a5\n // a1^a3,a2^a4,a3^a5\n // a1^a2^a3^a4,a2^a3^a4^a5\n \n // 0,0,1,0,0\n // 0,1,1,0\n // 1,0,1\n \n \n for(int len=1;len<=n;len++) {\n vector<int> V;\n for(int pos=0;pos<len;pos++) {\n if (B[len-1][pos]) V.push_back(pos);\n }\n for(int i=0;i+len<=n;i++) {\n int j=i+len-1;\n dp[i][j]=0;\n for(int x : V) {\n dp[i][j]^=nums[x+i];\n }\n }\n }\n \n for(int len=2;len<=n;len++) {\n for(int i=0;i+len<=n;i++) {\n int j=i+len-1;\n dp[i][j]=max(dp[i+1][j],dp[i][j]);\n dp[i][j]=max(dp[i][j-1],dp[i][j]);\n }\n }\n vector<int> ret;\n for(auto tmp : queries) {\n int l=tmp[0];\n int r=tmp[1];\n ret.push_back(dp[l][r]);\n }\n return ret;\n \n \n }\n};",
"memory": "343133"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n int dp[n][n];\n int dp2[n][n] , dpp[n][n];\n for(int i = 0 ; i < n ;i++){\n dp[i][i] = nums[i];\n dp2[i][i] = nums[i];\n dpp[i][i] = nums[i];\n }\n for(int len = 2; len <=n ; len++ ){\n for(int l = 0 ; l <= n - len ; l++){\n int r = l + len - 1;\n dp[l][r] = dp[l][r-1] ^ dp[l+1][r];\n dp2[l][r] = max(dp2[l][r-1] , dp[l][r] );\n dpp[l][r] = max(dpp[l+1][r], dp2[l][r]);\n }\n }\n vector<int> ans;\n for(auto q : queries){\n ans.push_back(dpp[q[0]][q[1]]);\n }\n return ans;\n }\n};",
"memory": "345979"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<pair<int, int>>> heap(n);\n heap[0].resize(n);\n for (int i = 0; i < n; ++i)\n heap[0][i] = {nums[i], nums[i]};\n for (int len = 2; len <= n; ++len)\n {\n int sz = n - len + 1;\n heap[len-1].resize(sz);\n for (int i = 0; i < sz; ++i)\n {\n auto val = heap[len-2][i].first ^ heap[len-2][i+1].first;\n auto maxNum = max(heap[len-2][i].second, heap[len-2][i+1].second);\n maxNum = max(maxNum, val);\n heap[len-1][i] = {val, maxNum};\n }\n }\n vector<int> result;\n for (const auto & v : queries)\n {\n auto len = v[1] - v[0] + 1;\n result.emplace_back(heap[len-1][v[0]].second);\n }\n return result;\n }\n};",
"memory": "348825"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<pair<int, int>>> heap(n);\n heap[0].resize(n);\n for (int i = 0; i < n; ++i)\n heap[0][i] = {nums[i], nums[i]};\n for (int len = 2; len <= n; ++len)\n {\n int sz = n - len + 1;\n heap[len-1].resize(sz);\n for (int i = 0; i < sz; ++i)\n {\n auto val = heap[len-2][i].first ^ heap[len-2][i+1].first;\n auto maxNum = max(heap[len-2][i].second, heap[len-2][i+1].second);\n maxNum = max(maxNum, val);\n heap[len-1][i] = {val, maxNum};\n }\n }\n vector<int> result;\n for (const auto & v : queries)\n {\n auto len = v[1] - v[0] + 1;\n result.emplace_back(heap[len-1][v[0]].second);\n }\n return result;\n }\n};",
"memory": "351671"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<pair<int, int>>> heap(n);\n heap[0].resize(n);\n for (int i = 0; i < n; ++i)\n heap[0][i] = {nums[i], nums[i]};\n for (int len = 2; len <= n; ++len)\n {\n int sz = n - len + 1;\n heap[len-1].resize(sz);\n for (int i = 0; i < sz; ++i)\n {\n auto val = heap[len-2][i].first ^ heap[len-2][i+1].first;\n auto maxNum = max(heap[len-2][i].second, heap[len-2][i+1].second);\n maxNum = max(maxNum, val);\n heap[len-1][i] = {val, maxNum};\n }\n }\n vector<int> result;\n for (const auto & v : queries)\n {\n auto len = v[1] - v[0] + 1;\n result.emplace_back(heap[len-1][v[0]].second);\n }\n return result;\n }\n};",
"memory": "351671"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<pair<int, int>>> heap(n);\n heap[0].resize(n);\n for (int i = 0; i < n; ++i)\n heap[0][i] = {nums[i], nums[i]};\n for (int len = 2; len <= n; ++len)\n {\n int sz = n - len + 1;\n heap[len-1].resize(sz);\n for (int i = 0; i < sz; ++i)\n {\n auto val = heap[len-2][i].first ^ heap[len-2][i+1].first;\n auto maxNum = max(heap[len-2][i].second, heap[len-2][i+1].second);\n maxNum = max(maxNum, val);\n heap[len-1][i] = {val, maxNum};\n }\n }\n vector<int> result;\n for (const auto & v : queries)\n {\n auto len = v[1] - v[0] + 1;\n result.emplace_back(heap[len-1][v[0]].second);\n }\n return result;\n }\n};",
"memory": "354518"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<pair<int, int>>> heap(n);\n heap[0].resize(n);\n for (int i = 0; i < n; ++i)\n heap[0][i] = {nums[i], nums[i]};\n for (int len = 2; len <= n; ++len)\n {\n int sz = n - len + 1;\n heap[len-1].resize(sz);\n for (int i = 0; i < sz; ++i)\n {\n auto val = heap[len-2][i].first ^ heap[len-2][i+1].first;\n auto maxNum = max(heap[len-2][i].second, heap[len-2][i+1].second);\n maxNum = max(maxNum, val);\n heap[len-1][i] = {val, maxNum};\n }\n }\n vector<int> result;\n for (const auto & v : queries)\n {\n auto len = v[1] - v[0] + 1;\n result.emplace_back(heap[len-1][v[0]].second);\n }\n return result;\n }\n};",
"memory": "354518"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int f[2002][2002];\n int a[2002][2002];\n int res[2002][2002];\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n vector<int> ans;\n int n = nums.size();\n //init\n for(int len = 0; len <n; len++){\n for(int i=0; i<n; i++){\n \n int j=i+len;\n\n if(j >= n)continue;\n \n if(j == i){\n a[i][j] = nums[j];\n f[i][j] = nums[j];\n }else{\n a[i][j] = (a[i][j-1]^a[i+1][j]);\n f[i][j] = max(f[i][j-1], a[i][j]);\n }\n \n }\n }\n\n\n for(int r=0; r<n; r++){\n for(int l=r; l>=0; l--){\n if(l == r){\n res[l][r] = nums[l];\n }else{\n res[l][r] = max(res[l+1][r], f[l][r]);\n }\n }\n\n }\n\n\n\n //queries\n\n for(auto q: queries){\n \n\n \n ans.push_back(res[q[0]][q[1]]);\n }\n\n\n\n return ans;\n }\n};",
"memory": "357364"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n /*\n Sol01.Get all subarryas and getther max xor for eah query TC = O(k^2 *q)\n Sol02.Use Sliding WIndow to calculate XOR of each subarray\n Sol03.Maybe use prefix sum similar and getthe xor\n\n Sol:-\n 0 1 2 3 4 5\n queris : [0,0],[1,1],[2,2],[3,3],[4,4],[5,5]\n [0,1],[1,2],[2,3],[3,4],[4,5]\n [0,2],[1,3],[2,4],[3,5]\n [0,3],[1,4],[2,5]\n [0,4],[1,5]\n [0,5]\n \n f(0,1,2) = f(0^1,1^2) = f( (0^1)^(1^2) )= f( 0^2 )\n\n\n */\n\n // void print1dVector(vector<int> v )\n // {\n // for( auto it : v ) cout<<it<<\" \";\n // cout<<endl;\n // }\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n vector<vector<int>> dp;\n int n = nums.size();\n\n vector<int> temp;\n for( int i = 0 ; i < n ; i++)\n {\n temp.push_back(nums[i]);\n }\n\n \n dp.push_back(temp);\n temp.clear();\n\n int t = 1;\n for( int i = 1 ; i < n ; i++ )\n {\n for( int j = 0 ; j < n-t ; j++ )\n {\n temp.push_back( dp[i-1][j]^dp[i-1][j+1] ) ;\n }\n\n dp.push_back(temp);\n temp.clear();\n t++;\n }\n // for(int j=0;j<nums.size();j++) {\n // for(auto it : dp[j]) {\n // cout<<it<<\" \";\n // }cout<<endl;\n // }\n\n for(int j=0;j<nums.size();j++) {\n for(int i=1;i<n && j<dp[i].size();i++) {\n dp[i][j]=max(dp[i][j],dp[i-1][j]);\n }\n }\n\n // for(int j=0;j<nums.size();j++) {\n // for(auto it : dp[j]) {\n // cout<<it<<\" \";\n // }cout<<endl;\n // }\n\n\n vector<int> ans;\n for( auto it : queries )\n {\n int l = it[0];\n int r = it[1];\n\n // printf(\"l = %d , r = %d\\n\",l,r);\n int maxi = 0;\n for( int j = l ; j <= r ; j++ )\n {\n maxi = max( maxi , dp[r-j][j] );\n }\n // cout<<endl;\n\n \n\n ans.push_back(maxi);\n }\n\n return ans;\n\n\n\n\n\n\n\n }\n}; ",
"memory": "357364"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& que) {\n vector<vector<int>> b;\n int n = a.size();\n b.push_back(a);\n for (int i = 1; i < n; i ++) {\n b.push_back(vector<int>(n - i));\n for (int j = 0; j < n - i; j ++) {\n b[i][j] = b[i - 1][j] ^ b[i - 1][j + 1]; \n // cout<<i<<' '<<j<<' '<<b[i][j]<<endl;\n }\n }\n \n vector<vector<int>> c = b;\n \n for (int i = 1; i < n; i ++) {\n for (int j = 0; j < c[i].size(); j ++)\n c[i][j] = max(c[i][j], c[i - 1][j]);\n }\n \n for (int i = 1; i < n; i ++) {\n for (int j = 0; j < c[i].size(); j ++)\n if (c[i - 1].size() > j + 1)\n c[i][j] = max(c[i][j], c[i - 1][j + 1]);\n }\n \n vector<int> ans;\n for (auto &q : que) {\n int l = q[0], r = q[1];\n ans.push_back(c[r - l][l]);\n }\n \n return ans;\n }\n};",
"memory": "360210"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& que) {\n vector<vector<int>> b;\n int n = a.size();\n b.push_back(a);\n for (int i = 1; i < n; i ++) {\n b.push_back(vector<int>(n - i));\n for (int j = 0; j < n - i; j ++) {\n b[i][j] = b[i - 1][j] ^ b[i - 1][j + 1]; \n // cout<<i<<' '<<j<<' '<<b[i][j]<<endl;\n }\n }\n \n vector<vector<int>> c = b;\n \n for (int i = 1; i < n; i ++)\n for (int j = 0; j < c[i].size(); j ++) {\n b[i][j] = max(b[i][j], b[i - 1][j]);\n if (b[i - 1].size() > j + 1)\n b[i][j] = max(b[i][j], b[i - 1][j + 1]);\n }\n \n vector<int> ans;\n for (auto &q : que) {\n int l = q[0], r = q[1];\n ans.push_back(b[r - l][l]);\n }\n \n return ans;\n }\n};",
"memory": "360210"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> res;\n \n struct Tree {\n int val=0;\n vector<Tree*> c;\n \n Tree(){\n c=vector<Tree*>(2, nullptr);\n }\n };\n \n void Insert(Tree* root, int val) {\n auto curr=root;\n for (int i=31; i>=0; --i) {\n bool v=val&(1<<i);\n if (curr->c[v] == nullptr){\n curr->c[v]= new Tree();\n }\n curr=curr->c[v];\n }\n curr->val = val;\n }\n \n int Query(Tree* root, int val) {\n auto curr=root;\n for (int i=31; i>=0; --i) {\n bool v=val&(1<<i);\n if (curr->c[!v] != nullptr) {\n curr=curr->c[!v];\n } else {\n curr=curr->c[v];\n }\n }\n return (curr->val) ^ val;\n }\n \n void Solve(vector<vector<int>>& qu, int f, vector<int>& nums) {\n //cout << \"solve\\n\";\n Tree* root = new Tree();\n Insert(root, 0);\n int curr=0;\n int qIdx=0;\n for (int i=f; i<nums.size(); ++i) {\n curr=nums[i];\n //cout << f << \"-\" << curr << \"\\n\";\n Insert(root, curr);\n while (qIdx < qu.size() && qu[qIdx][0] == i) {\n int q = Query(root, curr);\n res[qu[qIdx][1]]=q;\n ++qIdx;\n }\n }\n }\n\n \n\n \n\n \n \n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& qu) {\n\n \n int n=nums.size();\n res = vector<int>(qu.size(), 0);\n vector<vector<int>> dp(n+1, vector<int>(n+1, 0));\n for (int i=0; i<n; ++i){\n dp[1][i] = nums[i];\n }\n for (int len=2; len<=n; ++len) {\n for (int i=0; i+len<=n; ++i) {\n dp[len][i] = dp[len-1][i] ^ dp[len-1][i+1];\n\n }\n\n }\n \n for (int len=2; len<=n; ++len) {\n for (int i=0; i+len<=n; ++i) {\n dp[len][i] = max(dp[len][i], max(dp[len-1][i], dp[len-1][i+1]));\n }\n }\n\n for (int i=0; i<qu.size(); ++i) {\n int from = qu[i][0];\n int to = qu[i][1];\n int len=to-from+1;\n res[i]=dp[len][from];\n }\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n return res;\n\n\n\n }\n};",
"memory": "363056"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "#include <iostream>\n#define ll long long\n\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n ll n = nums.size(), q = queries.size();\n ll X[n][n], dp[n][n];\n for (int i=0; i<n; ++i) X[i][i] = dp[i][i] = nums[i];\n for (int s=1; s<n; ++s) {\n for (int i=0; i+s<n; ++i) {\n X[i][i+s] = X[i][i+s-1] ^ X[i+1][i+s];\n dp[i][i+s] = X[i][i+s];\n dp[i][i+s] = max(dp[i][i+s], dp[i][i+s-1]);\n dp[i][i+s] = max(dp[i][i+s], dp[i+1][i+s]);\n }\n }\n vector <int> F;\n for (auto u : queries) {\n F.push_back(dp[u[0]][u[1]]);\n }\n return F;\n }\n};",
"memory": "365903"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n vector<vector<int>> dp(n,vector<int>(n));\n for(int i=0;i<n;i++){\n dp[i][i]=nums[i];\n }\n for(int len=2;len<=n;len++){\n for(int st=0;st+len-1<n;st++){\n int end=st+len-1;\n dp[st][end]=dp[st][end-1]^dp[st+1][end];\n }\n }\n for(int len=2;len<=n;len++){\n for(int st=0;st+len-1<n;st++){\n int end=st+len-1;\n dp[st][end]=max({dp[st][end],dp[st][end-1],dp[st+1][end]});\n }\n }\n vector<int> res;\n for(vector<int>& quer:queries){\n int u=quer[0],v=quer[1];\n res.push_back(dp[u][v]);\n }\n return res;\n }\n};",
"memory": "365903"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "template <class T>\nbool umax(T &a, const T b) { return a < b ? a = b, 1 : 0; }\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& queries) {\n // Key Observation: dp[i][j] = dp[i + 1][j] ^ dp[i][j - 1]\n int n = a.size(), m = queries.size();\n vector<vector<int>> dp(n, vector<int>(n, 0));\n for (int i = n - 1; i >= 0; i--) {\n for (int j = i; j < n;j++) {\n if (i == j) dp[i][j] = a[i];\n else if (i + 1 == j) dp[i][j] = (a[i] ^ a[j]);\n else dp[i][j] = dp[i + 1][j] ^ dp[i][j - 1];\n }\n }\n\n vector<array<int, 3>> v(m);\n for (int i =0;i<m;i++) v[i] = {queries[i][0],queries[i][1],i};\n sort(v.begin(), v.end(), [](auto& lh, auto&rh) {\n if (lh[1] != rh[1]) return lh[1] < rh[1];\n return lh[0] > rh[0];\n });\n\n vector<int> ans(m, 0);\n vector<int> best = a;\n for (int r = 0, j =0; r < n;r++) {\n for (int l = r; l >=0;l--) {\n umax(best[l], dp[l][r]);\n if (l < r) umax(best[l], best[l + 1]);\n }\n while (j < m and v[j][1] == r) {\n ans[v[j][2]] = best[v[j][0]];\n j++;\n }\n }\n return ans;\n }\n};",
"memory": "368749"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> dp(n, vector<int>{});\n for (int i = 0; i < n; i++) {\n dp[i].reserve(n-i);\n }\n dp[0] = nums;\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < n - i; j++) {\n dp[i].push_back(dp[i-1][j] ^ dp[i-1][j+1]);\n }\n // cout << \"the \" << i << \"th row of dp size is: \" << dp[i].size() << endl;\n }\n vector<vector<int>> dp2(dp);\n /*for (int i = 0; i < n; i++) {\n cout << \"the \" << i << \"th row of dp2 size is: \" << dp2[i].size() << endl;\n }*/\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < n - i; j++) {\n dp2[i][j] = max(dp[i][j], max(dp2[i-1][j], dp2[i-1][j+1]));\n }\n }\n int m = queries.size();\n vector<int> ans(m, 0);\n for (int i = 0; i < m; i++) {\n ans[i] = dp2[queries[i][1] - queries[i][0]][queries[i][0]];\n }\n return ans;\n }\n};",
"memory": "371595"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& A, vector<vector<int>>& Q) {\n unordered_map<int, unordered_map<int, vector<int>>> mp;\n for(int i = 0; i < Q.size(); i++) mp[Q[i][1]-Q[i][0]+1][Q[i][0]].push_back(i);\n vector<int> res(Q.size()), B(A.size());\n for(int l = 1; l <= A.size(); l++ ) {\n for(int i = 0; i + l - 1 < A.size(); i++) {\n if(l > 1) A[i] ^= A[i+1];\n B[i] = (l == 1 ? A[i] : max({B[i], B[i+1], A[i]}));\n if(mp[l].find(i) != mp[l].end()) for(auto idx : mp[l][i]) res[idx] = B[i];\n }\n }\n return res;\n }\n};",
"memory": "374441"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& A, vector<vector<int>>& Q) {\n unordered_map<int, unordered_map<int, vector<int>>> mp;\n for(int i = 0; i < Q.size(); i++) mp[Q[i][1]-Q[i][0]+1][Q[i][0]].push_back(i);\n vector<int> res(Q.size()), B(A.size());\n for(int l = 1; l <= A.size(); l++ ) {\n for(int i = 0; i + l - 1 < A.size(); i++) {\n if(l > 1) A[i] ^= A[i+1];\n B[i] = (l == 1 ? A[i] : max({B[i], B[i+1], A[i]}));\n if(mp[l].find(i) != mp[l].end()) for(auto idx : mp[l][i]) res[idx] = B[i];\n }\n }\n return res;\n }\n};",
"memory": "374441"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n vector<int> l,r; \n for(int i = 0; i < queries.size(); i++){ \n \tl.push_back(queries[i][0]); \n r.push_back(queries[i][1]); \n }\n\t\tint n = nums.size(); \n \n vector<vector<int>> compute(n, vector<int>(n, -1)); \n for(int len = 1; len <= n; len++){\n for(int i = 0; i + len - 1 < n; i++){ \n int x = 0;\n for(int u = 0; u < len; u++)\n if((u & len-1) == u)\n x ^= nums[i+u];\n if(len > 1) compute[i][i + len - 1] = max(compute[i][i + len - 2], compute[i + 1][i + len - 1]); \n \tcompute[i][i + len - 1] = max(compute[i][i + len - 1], x);\n }\n }\n vector<int> answer; \n for(int i = 0; i < l.size(); i++)\n \tanswer.push_back(compute[l[i]][r[i]]); \n return answer; \n }\n};",
"memory": "377288"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& q) {\n int n = a.size(), m = q.size();\n int val[n][n];\n vector<vector<int>> dp(n, vector<int>(n));\n for(int i = 0; i < n; i ++) {\n val[i][i] = a[i];\n dp[i][i] = a[i];\n }\n for(int len = 2; len <= n; len ++) {\n for(int i = 0; i < n; i ++) {\n int j = i + len - 1;\n if(j >= n) break;\n val[i][j] = val[i + 1][j] ^ val[i][j - 1];\n dp[i][j] = val[i][j];\n dp[i][j] = max(dp[i][j], dp[i + 1][j]);\n dp[i][j] = max(dp[i][j], dp[i][j - 1]);\n }\n }\n vector<int> ans;\n for(auto &x : q) {\n ans.push_back(dp[x[0]][x[1]]);\n }\n return ans;\n }\n};",
"memory": "380134"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> xors(n, vector<int>(n, 0));\n for(int i = 0; i < n; i++) xors[i][i] = nums[i];\n for(int l = 1; l < n; l++) {\n for(int i = 0; i + l < n; i++) {\n xors[i][i + l] = xors[i][i + l - 1] ^ xors[i + 1][i + l];\n }\n }\n for(int l = 1; l < n; l++) {\n for(int i = 0; i + l < n; i++) {\n xors[i][i + l] = max(xors[i][i + l], max(xors[i][i + l - 1], xors[i + 1][i + l]));\n }\n }\n vector<int> ans{};\n ans.reserve(queries.size());\n for(vector<int>& q: queries) {\n ans.push_back(xors[q[0]][q[1]]);\n }\n return ans;\n }\n};",
"memory": "382980"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> t(n, vector<int>(n, 0));\n\n // Initialize the base cases\n for (int j = 0; j < n; ++j) {\n t[j][j] = nums[j];\n }\n\n // Compute XOR values for subarrays\n for (int j = 1; j < n; ++j) {\n for (int i = 0; i + j < n; ++i) {\n t[i][i + j] = t[i][i + j - 1] ^ t[i + 1][i + j];\n }\n }\n \n // Compute the maximum XOR value for subarrays\n for (int j = 1; j < n; ++j) {\n for (int i = 0; i + j < n; ++i) {\n t[i][i + j] =\n max(t[i][i + j], max(t[i][i + j - 1], t[i + 1][i + j]));\n }\n }\n // Process queries\n vector<int> res(queries.size());\n for (int idx = 0; idx < queries.size(); ++idx) {\n int l = queries[idx][0];\n int r = queries[idx][1];\n res[idx] = t[l][r];\n }\n\n return res;\n }\n};",
"memory": "382980"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\n\n vector<int> t;\n\n int getMax(int v, int tl, int tr, int l, int r) {\n if (l > r) \n return 0;\n if (l == tl && r == tr) {\n return t[v];\n }\n int tm = (tl + tr) / 2;\n return max(getMax(v*2, tl, tm, l, min(r, tm)),\n getMax(v*2+1, tm+1, tr, max(l, tm+1), r));\n }\n\n void update(int v, int tl, int tr, int pos, int new_val) {\n if (tl == tr) {\n t[v] = max(t[v], new_val);\n } else {\n int tm = (tl + tr) / 2;\n if (pos <= tm)\n update(v*2, tl, tm, pos, new_val);\n else\n update(v*2+1, tm+1, tr, pos, new_val);\n t[v] = max(t[v*2], t[v*2+1]);\n }\n }\n\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int N = nums.size();\n t.resize(4 * N);\n\n vector<vector<int>> vals(N, vector<int>(N));\n for(int i = 0; i < N; i++) {\n vals[i][i] = nums[i];\n }\n for(int d = 1; d < N; d++) {\n for(int i = 0; i + d < N; i++) {\n int j = i + d;\n vals[i][j] = vals[i][j - 1] ^ vals[i + 1][j];\n }\n }\n\n vector<vector<pair<int, int>>> q(N);\n for(int i = 0; i < queries.size(); i++) {\n q[queries[i][0]].push_back({queries[i][1], i});\n }\n\n vector<int> ans(queries.size());\n for(int i = N - 1; i >= 0; i--) {\n // vector<int> vals;\n // int v = 0;\n // for(int j = i; j < N; j++) {\n // v ^= nums[j];\n // vals.push_back(v);\n // // cout << i << \" \" << j << \" \" << v << \"\\n\";\n // // update(1, 0, N, j, v);\n // }\n for(int j = i; j < N; j++) {\n //0 -> 0\n //1 -> 1\n //2 -> 1\n //3 -> 2\n // int newi = (j - i + 1) / 2;\n // cout << i << \" \" << j << \" \" << vals[newi] << \"\\n\";\n // cout << i << \" \" << j << \" \" << newi << \"\\n\";\n update(1, 0, N, j, vals[i][j]);\n }\n for(auto&[j, ind]: q[i]) {\n ans[ind] = getMax(1, 0, N, i, j);\n }\n }\n return ans;\n }\n};\n\n\n//2 8 4 32 16 1\n//2",
"memory": "385826"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> dp(n, vector<int>(n));\n for(int i = 0; i < n; i++)\n dp[i][i] = nums[i];\n \n for(int len = 2; len <= n; len++) {\n for(int st = 0; st + len - 1 < n; st++) {\n int end = st + len - 1;\n dp[st][end] = dp[st][end-1] ^ dp[st+1][end]; \n } \n }\n\n for(int len = 2; len <= n; len++) {\n for(int st = 0; st + len - 1 < n; st++) {\n int end = st + len - 1;\n dp[st][end] = max({dp[st][end], dp[st][end-1], dp[st+1][end]});\n }\n }\n\n vector<int> res;\n for(vector<int>& quer: queries) {\n int u = quer[0], v = quer[1];\n res.push_back(dp[u][v]);\n }\n\n return res;\n }\n};",
"memory": "385826"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n\n vector<vector<int>> xors(n, vector<int>(n));\n\n for (int i = 0; i < n; i++) xors[0][i] = nums[i];\n for (int i = 1; i < n; i++)\n for (int j = 0; j < n - i; j++)\n xors[i][j] = (xors[i - 1][j] ^ xors[i - 1][j + 1]);\n \n for (int i = 1; i < n; i++)\n for (int j = 0; j < n - i; j++)\n xors[i][j] = max(xors[i][j], max(xors[i - 1][j], xors[i - 1][j + 1]));\n\n // for (int i = 0; i < n; i++)\n // for (int j = 0; j < n - i; j++)\n // cout << xors[i][j] << \" \\n\"[j == n - 2];\n\n vector<int> ans;\n for (auto& qu : queries) {\n int l = qu[0], r = qu[1];\n ans.push_back(xors[r - l][l]);\n }\n\n return ans;\n\n }\n};",
"memory": "388673"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> dp(n, vector<int>(n, -1));\n\n for (int i = n - 1; i >= 0; i--) {\n for (int j = i; j < n; j++) {\n if (i == j) {\n dp[i][j] = nums[i];\n continue;\n }\n dp[i][j] = dp[i][j - 1] ^ dp[i + 1][j];\n }\n }\n\n for (int i = n - 1; i >= 0; i--) {\n for (int j = i; j < n; j++) {\n if (i == j) {\n continue;\n }\n dp[i][j] = max({dp[i][j], dp[i][j - 1], dp[i + 1][j]});\n }\n }\n\n vector<int> ans;\n for (auto &query: queries) {\n ans.push_back(dp[query[0]][query[1]]);\n }\n\n return ans;\n }\n};",
"memory": "388673"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size(), i, j, x, y;\n vector < int > res(queries.size());\n vector < vector < int > > dp(n, vector < int > (n, 0));\n for(i=0;i<n;i++) dp[0][i]=nums[i];\n for(i=1;i<n;i++){\n for(j=0;j<n-i;j++){\n dp[i][j]=dp[i-1][j] ^ dp[i-1][j+1];\n }\n }\n i=0;\n \n for(i=1;i<n;i++){\n \n for(j=n-1-i;j>=0;j--){\n \n \n dp[i][j] = max(dp[i][j], max(dp[i-1][j], dp[i-1][j+1]));\n // cout<<dp[i][j]<<\" \";\n }\n // cout<<endl;\n }\n // cout<<\"here\\n\";\n i=0;\n for(auto q: queries){\n // cout<<q[1]-q[0]<<\" \"<<q[0]<<endl;\n res[i]=dp[q[1]-q[0]][q[0]];\n i++;\n }\n return res;\n }\n};",
"memory": "391519"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n void debug(vector<vector<int>> grid) {\n for (auto i : grid) {\n for (int j : i) {\n cout<<j<<\" \";\n }\n cout<<endl;\n }\n }\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& Q) {\n int N = nums.size(), M = Q.size();\n vector<int> res(M,0);\n vector<vector<int>> xors(N, vector<int>(N,0)); //each [i][j] == subarr i -> j\n xors[0][0] = nums[0];\n for (int i=1; i<N; i++) {\n xors[i][i] = nums[i];\n for (int j=i-1; j>=0; j--) {\n xors[i][j] = xors[i-1][j]^xors[i][j+1];\n }\n }\n //debug(xors);\n //convert to funnel downleft\n for (int i=1; i<N; i++) {\n for (int j=i-1; j>=0; j--) {\n int neighbors = max(xors[i-1][j], xors[i][j+1]);\n xors[i][j] = max(neighbors, xors[i][j]);\n }\n }\n //debug(xors);\n for (int i=0; i<M; i++) {\n auto cur = Q[i];\n res[i] = xors[cur[1]][cur[0]];\n }\n return res;\n }\n /*\n logic {\n if you look at the bits between a given range, if a bit is set an odd number of times, it will be set at the end?\n or only the ends are set? pretty sure its odd cus it sort of propagates only if odd but evens eventually cancel\n so we can prefix\n\n wait but actually if the range is odd length...\n bits(N, vector<int>(32,0));\n for (int i=0; i<N; i++) {\n int cur = nums[i];\n for (int j=0; j<32; j++) {\n if (1<<j > cur) break;\n bits[i][j] = cur&(1<<j);\n }\n }\n ok weird stuff happens maybe we cant use the bits to get in O1 time so what if we just precompute everything\n seeing that N<2000, N^2 is valid STOP JUMPING TO CONCLUSION IMPORTANT TO TRY BRUTE FORCE ALSO AND OPTIMIZE OFF OF IT\n each i j XOR the positions above and to the right\n 0 7 3 2 8 5 1\n 0 0\n 7 7 7\n 3 3 4 3\n 2 6 5 1 2\n 8 8 14 11 10 8\n so the max of a given range is just the max within the selected triangle\n so within 7 3 2 8, the max is 14, but within 3 2 8 the max is 11. so how to find in constant time?\n N^2 per query is obviously just going through each\n N can be done if we prefix max downward, so we get\n 0 7 3 2 8 5 1\n 0 0\n 7 7 7\n 3 7 7 3\n 2 7 7 3 2\n 8 8 14 11 10 8\n but O(1) time can be done if we prefix max horizontally as well going leftward*\n 0 7 3 2 8 5 1\n 0 0\n 7 7 7\n 3 7 7 3\n 2 7 7 3 2\n 8 8 14 11 10 8\n }\n */\n};",
"memory": "391519"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> arr(n, vector<int>(n));\n vector<int> times, pos;\n for(int size = 1; size <= n; size++) {\n if(size == 1) {\n times.push_back(1);\n pos.push_back(0);\n } else {\n times.push_back(1);\n int last = 1;\n for(int i = 1; i + 1 < size; i++) {\n tie(last, times[i]) = make_pair(times[i], (last + times[i]) % 2);\n }\n pos.clear();\n for(int i = 0; i < size; i++) {\n if(times[i] % 2) {\n pos.push_back(i);\n }\n }\n }\n for(int left = 0; left + size <= n; left++) {\n int score = 0;\n for(int delta: pos) {\n score = score ^ nums[left + delta];\n }\n arr[left][left + size - 1] = score;\n }\n }\n\n int m = queries.size();\n vector<tuple<int, int, int>> q(m);\n for(int i = 0; i < m; i++) {\n q[i] = {queries[i][0], queries[i][1], i};\n }\n sort(q.begin(), q.end());\n\n for(int right = 0; right < n; right++) {\n for(int left = n - 2; left >= 0; left--) {\n arr[left][right] = max(arr[left][right], arr[left + 1][right]);\n }\n }\n\n vector<int> res(m);\n int pre = 0, left = -1, right = -1;\n for(int k = 0; k < m; k++) {\n auto [x, y, i] = q[k];\n if(left != x) {\n left = x;\n right = -1;\n pre = 0;\n }\n while(right < y) {\n right++;\n pre = max(pre, arr[left][right]);\n }\n res[i] = pre;\n }\n return res;\n }\n};",
"memory": "394365"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\n\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<int> v[n];\n v[0]=nums;\n // BRUTE FORCE\n for(int depth=1;depth<n;depth++) {\n for(int i=0;i+1<v[depth-1].size();i++)\n v[depth].push_back( v[depth-1][i]^v[depth-1][i+1]);\n }\n\n // PREFIX SUMS (max's) column wise\n for(int j=0;j<nums.size();j++) {\n for(int i=1;i<n && j<v[i].size();i++) {\n v[i][j]=max(v[i][j],v[i-1][j]);\n }\n }\n\n vector<int> result;\n for(int i=0;i<queries.size();i++) {\n int left = queries[i][0];\n int right = queries[i][1];\n int ans = 0;\n // Ans in O(N)\n for(int j=left ;j<=right;j++) {\n ans = max(ans, v[(right-j)][j]);\n }\n result.push_back(ans);\n }\n return result;\n }\n};\n\n/*\n\n0: 1 2 3 4 5 6 7 8 \n1: 12 23 34 45 56 67 78 \n2: 13 24 35 46 57 68 \n3: 1234 2345 3456 4567 5678 \n4: 15 26 37 48 \n5: 1256 2367 3478 \n6: 1357 2468 \n7: 12345678\n\n\n*/",
"memory": "394365"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n \n int n=nums.size();\n vector<vector<int>> dp(n,vector<int>(n));\n\n for(int i=0;i<n;i++) dp[i][i]=nums[i];\n\n for(int len=2;len<=n;len++){\n for(int st=0;st+len-1<n;st++){\n int end=st+len-1;\n dp[st][end]=dp[st][end-1]^dp[st+1][end];\n }\n\n }\n \n for(int len=2;len<=n;len++){\n for(int st=0;st+len-1<n;st++){\n int end=st+len-1;\n dp[st][end]=max({dp[st][end],dp[st+1][end],dp[st][end-1]});\n }\n }\n\n vector<int> ans;\n\n for(auto i:queries){\n int u=i[0],v=i[1];\n ans.push_back(dp[u][v]);\n }\n\n return ans;\n\n }\n};",
"memory": "397211"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums,\n vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> dp(n, vector<int>(n));\n for (int i = 0; i < n; i++)\n dp[i][i] = nums[i];\n for (int len = 2; len <= n; len++) {\n for (int i = 0; i + len - 1 < n; i++) {\n int j = i + len - 1;\n dp[i][j] = dp[i][j - 1] ^ dp[i + 1][j];\n }\n }\n for (int len = 2; len <= n; len++) {\n for (int i = 0; i + len - 1 < n; i++) {\n int j = i + len - 1;\n dp[i][j] = max({dp[i][j], dp[i][j - 1], dp[i + 1][j]});\n }\n }\n vector<int> ans;\n for (auto x : queries) {\n ans.push_back(dp[x[0]][x[1]]);\n }\n return ans;\n }\n};",
"memory": "397211"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n void singleSubarray(int l, int r, vector<vector<int>>& maxSubscore, vector<vector<int>>& subscore) {\n if (subscore[r][l] == -1) {\n singleSubarray(l + 1, r, maxSubscore, subscore);\n singleSubarray(l, r - 1, maxSubscore, subscore);\n subscore[r][l] = subscore[r][l+1] ^ subscore[r-1][l];\n maxSubscore[r][l] = max(subscore[r][l], max(maxSubscore[r][l+1], maxSubscore[r-1][l]));\n }\n }\n\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n vector<vector<int>> maxSubscore {};\n vector<vector<int>> subscore {};\n maxSubscore.reserve(nums.size());\n subscore.reserve(nums.size());\n for (int i {0}; i < nums.size(); i++) {\n maxSubscore.push_back(vector<int> (i + 1));\n subscore.push_back(vector<int> (i + 1, -1));\n subscore[i][i] = nums[i];\n maxSubscore[i][i] = nums[i];\n }\n vector<int> soln {};\n soln.reserve(queries.size());\n for (auto query : queries) {\n singleSubarray(query[0], query[1], maxSubscore, subscore);\n soln.push_back(maxSubscore[query[1]][query[0]]);\n }\n return soln;\n }\n};",
"memory": "400058"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int> &nums, vector<vector<int>> &queries) {\n int n = nums.size();\n vector<int> xors(n * n, -1);\n for(int i = 0; i < n; i++) {\n for(int j = i; j < n; j++){\n xors[i * n + j] = i == 0 ? nums[j] : xors[(i - 1) * n + j] ^ xors[(i - 1) * n + j - 1];\n }\n }\n vector<int> max_arr(n * n, 0);\n vector<int> aux(n, 0);\n for(int i = n - 1; i > -1; i--) {\n for(int j = i; j < n; j++) {\n if(i == j) {\n aux[j] = xors[j];\n max_arr[i * n + j] = xors[j];\n }else {\n aux[j] = max(aux[j], xors[(j - i) * n + j]);\n max_arr[i * n + j] = max(aux[j], max_arr[i * n + j - 1]);\n }\n }\n }\n vector<int> result;\n for(int i = 0; i < queries.size(); i++) result.push_back(max_arr[queries[i][0] * n + queries[i][1]]);\n return result;\n }\n};",
"memory": "402904"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n const int n = nums.size();\n vector<vector<int>> levels(n);\n levels[n - 1] = nums;\n for (int l = n - 1; l --> 0; ) {\n levels[l].resize(l + 1);\n for (int i = 0; i < levels[l].size(); ++i) {\n levels[l][i] = levels[l + 1][i] ^ levels[l + 1][i + 1];\n }\n }\n vector<vector<int>> scores(n, vector<int>(n));\n for (int i = 0; i < n; ++i) {\n scores[i][i] = nums[i];\n }\n for (int len = 2; len <= n; ++len) {\n for (int b = 0; b + len - 1 < n; ++b) {\n const int e = b + len - 1;\n scores[b][e] = max(scores[b + 1][e], scores[b][e - 1]);\n // + score of the whole segment here\n scores[b][e] = max(scores[b][e], levels[n - len][b]);\n }\n }\n vector<int> ans;\n for (const auto& q : queries) {\n ans.push_back(scores[q[0]][q[1]]);\n }\n return ans;\n }\n};\n",
"memory": "402904"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "#ifndef ATCODER_SEGTREE_HPP\n#define ATCODER_SEGTREE_HPP 1\n\n#include <algorithm>\n#include <cassert>\n#include <functional>\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\n#if __cplusplus >= 202002L\n\nusing std::bit_ceil;\n\n#else\n\n// @return same with std::bit::bit_ceil\nunsigned int bit_ceil(unsigned int n) {\n unsigned int x = 1;\n while (x < (unsigned int)(n)) x *= 2;\n return x;\n}\n\n#endif\n\n// @param n `1 <= n`\n// @return same with std::bit::countr_zero\nint countr_zero(unsigned int n) {\n#ifdef _MSC_VER\n unsigned long index;\n _BitScanForward(&index, n);\n return index;\n#else\n return __builtin_ctz(n);\n#endif\n}\n\n// @param n `1 <= n`\n// @return same with std::bit::countr_zero\nconstexpr int countr_zero_constexpr(unsigned int n) {\n int x = 0;\n while (!(n & (1 << x))) x++;\n return x;\n}\n\n} // namespace internal\n\n} // namespace atcoder\n\nnamespace atcoder {\n\n#if __cplusplus >= 201703L\n\ntemplate <class S, auto op, auto e> struct segtree {\n static_assert(std::is_convertible_v<decltype(op), std::function<S(S, S)>>,\n \"op must work as S(S, S)\");\n static_assert(std::is_convertible_v<decltype(e), std::function<S()>>,\n \"e must work as S()\");\n\n#else\n\ntemplate <class S, S (*op)(S, S), S (*e)()> struct segtree {\n\n#endif\n\n public:\n segtree() : segtree(0) {}\n explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}\n explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {\n size = (int)internal::bit_ceil((unsigned int)(_n));\n log = internal::countr_zero((unsigned int)size);\n d = std::vector<S>(2 * size, e());\n for (int i = 0; i < _n; i++) d[size + i] = v[i];\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n\n void set(int p, S x) {\n assert(0 <= p && p < _n);\n p += size;\n d[p] = x;\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n\n S get(int p) const {\n assert(0 <= p && p < _n);\n return d[p + size];\n }\n\n S prod(int l, int r) const {\n assert(0 <= l && l <= r && r <= _n);\n S sml = e(), smr = e();\n l += size;\n r += size;\n\n while (l < r) {\n if (l & 1) sml = op(sml, d[l++]);\n if (r & 1) smr = op(d[--r], smr);\n l >>= 1;\n r >>= 1;\n }\n return op(sml, smr);\n }\n\n S all_prod() const { return d[1]; }\n\n template <bool (*f)(S)> int max_right(int l) const {\n return max_right(l, [](S x) { return f(x); });\n }\n template <class F> int max_right(int l, F f) const {\n assert(0 <= l && l <= _n);\n assert(f(e()));\n if (l == _n) return _n;\n l += size;\n S sm = e();\n do {\n while (l % 2 == 0) l >>= 1;\n if (!f(op(sm, d[l]))) {\n while (l < size) {\n l = (2 * l);\n if (f(op(sm, d[l]))) {\n sm = op(sm, d[l]);\n l++;\n }\n }\n return l - size;\n }\n sm = op(sm, d[l]);\n l++;\n } while ((l & -l) != l);\n return _n;\n }\n\n template <bool (*f)(S)> int min_left(int r) const {\n return min_left(r, [](S x) { return f(x); });\n }\n template <class F> int min_left(int r, F f) const {\n assert(0 <= r && r <= _n);\n assert(f(e()));\n if (r == 0) return 0;\n r += size;\n S sm = e();\n do {\n r--;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!f(op(d[r], sm))) {\n while (r < size) {\n r = (2 * r + 1);\n if (f(op(d[r], sm))) {\n sm = op(d[r], sm);\n r--;\n }\n }\n return r + 1 - size;\n }\n sm = op(d[r], sm);\n } while ((r & -r) != r);\n return 0;\n }\n\n private:\n int _n, size, log;\n std::vector<S> d;\n\n void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }\n};\n\n} // namespace atcoder\n\n#endif // ATCODER_SEGTREE_HPP\nint op(int a, int b){\n return max(a, b);\n}\nint e(){\n return 0;\n}\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector xors(n, vector<int>(n));\n for(int i=0;i<n;i++) xors[i][i] = nums[i];\n for(int d=1;d<n;d++) {\n for(int j=0;j<n;j++) {\n if(j+d < n && j + 1 < n) xors[j][j+d] = xors[j+1][j+d];\n if(j+d < n && j + d - 1 < n) xors[j][j+d] ^= xors[j][j+d-1];\n }\n }\n vector<array<int, 3>> qs;\n vector<int> ans(queries.size());\n for(int i=0;i<queries.size();i++) qs.push_back({queries[i][1], queries[i][0], i});\n sort(qs.begin(), qs.end());\n int pre = -1;\n atcoder::segtree<int, op, e> seg(n);\n for(int i=0;i<queries.size();i++){\n while(qs[i][0] > pre){\n pre++;\n for(int j=0;j<=pre;j++) if(seg.get(j) < xors[j][pre]) seg.set(j, xors[j][pre]);\n }\n ans[qs[i][2]] = seg.prod(qs[i][1], qs[i][0]+1);\n }\n return ans;\n }\n};",
"memory": "405750"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n vector<vector<int>> p;\n \n int curr = 0;\n int dp[2001][2001];\n\n void create(int x){\n p.push_back({});\n curr = max(curr,x);\n p[0].push_back(x);\n for(int i = 1;i<p.size();i++){\n int sz = p[i].size();\n int val = p[i-1][sz + 1] ^ p[i-1][sz];\n p[i].push_back(val);\n curr = max(val,curr);\n }\n }\n \n int dfs(int r,int c){\n if(r < 0)\n return 0;\n if(dp[r][c] != -1)\n return dp[r][c];\n return dp[r][c] = max({dfs(r-1,c),dfs(r-1,c + 1),p[r][c]});\n }\n\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& q) {\n unordered_map<int,vector<pair<int,int>>> f;\n vector<int> res(q.size());\n memset(dp,-1,sizeof(dp));\n for(int i : nums)\n create(i);\n\n for(int i = 0;i<q.size();i++){\n int l = q[i][0];\n int r = q[i][1];\n int len = r - l;\n res[i] = dfs(len,l);\n }\n return res;\n }\n};",
"memory": "405750"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> dp(n, vector<int> (n));\n vector<vector<int>> vv = {{0}};\n for(int i=0; i<n-1; i++){\n vector<int> u = vv.back();\n vector<int> w = vv.back();\n for(auto &p: u) p++;\n vv.push_back(vector<int>{});\n int i1=0, i2=0;\n while(i1< u.size() && i2 < w.size()) {\n if(u[i1]==w[i2]) { \n i1++; i2++;\n } else if(u[i1]<w[i2]) {\n vv.back().push_back(u[i1]); i1++;\n } else {\n vv.back().push_back(w[i2]); i2++;\n }\n }\n while(i1<u.size()) {\n vv.back().push_back(u[i1]); i1++;\n }\n while(i2<w.size()) {\n vv.back().push_back(w[i2]); i2++;\n }\n }\n for(int i=0; i<n; i++){\n int maxx = nums[i];\n dp[i][i] = nums[i];\n for(int j=i+1; j<n; j++) {\n int xxor = 0;\n for(auto p : vv[j-i]) xxor^=nums[p+i];\n maxx=max(xxor,maxx);\n dp[i][j]=maxx;\n }\n }\n vector<int> ans;\n for(int i=0; i<n; i++){\n int x = dp[i][i];\n for(int j=i; j>=0; j--){\n x=max(dp[j][i],x);\n dp[j][i]=x;\n }\n }\n for(auto p : queries){\n int mxx = 0;\n ans.push_back(dp[p[0]][p[1]]);\n }\n return ans;\n }\n};",
"memory": "408596"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "int dp[2001][2001];\nclass Solution {\npublic:\n vector<int>v;\n vector<vector<int>>pre;\n int calc(int l,int r){\n return pre[l][r];\n }\n int rec(int l,int r){\n if(l>r)return 0;\n int &ret=dp[l][r];\n if(~ret)return ret;\n ret=calc(l,r);\n ret=max(ret,rec(l+1,r));\n ret=max(ret,rec(l,r-1));\n return ret;\n }\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n v=nums;\n int n=nums.size();\n pre=vector<vector<int>>(n,vector<int>(n));\n for(int i=0;i<n;i++){\n pre[i][i]=nums[i];\n }\n for(int i=n-2;i>=0;i--){\n for(int j=i+1;j<n;j++){\n pre[i][j]=pre[i+1][j]^pre[i][j-1];\n }\n }\n memset(dp,-1,sizeof dp);\n vector<int>ans;\n for(auto it:queries){\n int l=it[0],r=it[1];\n ans.push_back(rec(l,r));\n }\n return ans;\n }\n};",
"memory": "411443"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n // dp[i][j]: the largest XOR score of the subarray a[i:j]\n int n = nums.size();\n vector<vector<int>> dp(n, vector(n, 0));\n vector<int> result(queries.size(), 0);\n for(int i = 0; i < n; i++){\n dp[0][i] = nums[i];\n }\n\n for(int i = 1; i < n; i++){\n for(int j = i; j < n; j++){\n dp[i][j] = dp[i - 1][j - 1] ^ dp[i - 1][j];\n }\n }\n\n // calculate the maximum xor score for subarrays num[i:j]\n // the maximum xor score for num[i:j] is dp[j - i][j]\n for(int i = 1; i < n; i++){\n for(int j = i; j < n; j++){\n dp[i][j] = max(dp[i][j], max(dp[i - 1][j - 1], dp[i - 1][j]));\n }\n }\n\n for(int i = 0; i < queries.size(); i++){\n auto q = queries[i];\n int start = q[0], end = q[1];\n result[i] = dp[end - start][end];\n }\n\n return result;\n }\n};",
"memory": "411443"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "#define ll long long\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n vector<int>v;\n int n = nums.size();\n vector<vector<ll>>dp(n,vector<ll>(n+1,0));\n for(int i=0;i<n;i++){\n dp[i][1]=nums[i];\n }\n for(int i=2;i<=n;i++){\n int flg = i%2;\n for(int j=n-i;j>=0;j--){\n int k = j+i-1;\n if(!flg){\n dp[j][i]=dp[j][i-1]^dp[j+1][i-1];\n }\n else{\n dp[j][i]=dp[j][i-2]^dp[j+2][i-2];\n }\n }\n }\n for(int i=0;i<n;i++){\n for(int j=2;j<=n;j++){\n dp[i][j]=max(dp[i][j],dp[i][j-1]);\n }\n }\n for(auto&it:queries){\n ll mx = LLONG_MIN;\n int l = it[0];\n int r = it[1];\n int len = r-l+1;\n for(int i=l;i<=r;i++){\n mx=max(mx,dp[i][len]);\n len--;\n }\n v.push_back(mx);\n }\n return v;\n }\n};",
"memory": "414289"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n // dp[i][j]: the largest XOR score of the subarray a[i:j]\n int n = nums.size();\n vector<vector<int>> dp(n, vector(n, 0));\n vector<int> result(queries.size(), 0);\n for(int i = 0; i < n; i++){\n dp[0][i] = nums[i];\n }\n\n for(int i = 1; i < n; i++){\n for(int j = i; j < n; j++){\n dp[i][j] = dp[i - 1][j - 1] ^ dp[i - 1][j];\n }\n }\n\n for(int i = 1; i < n; i++){\n for(int j = i; j < n; j++){\n dp[i][j] = max(dp[i][j], max(dp[i - 1][j - 1], dp[i - 1][j]));\n }\n }\n\n for(int i = 0; i < queries.size(); i++){\n auto q = queries[i];\n int start = q[0], end = q[1];\n int temp_len = end - start + 1;\n result[i] = dp[temp_len - 1][end];\n }\n\n return result;\n }\n};",
"memory": "414289"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int len = nums.size();\n vector<vector<int>> xorDP(len, vector<int>(len, 0));\n // vector<vector<int>> xorScore(len, vector<int>(len, 0));\n for (int i = 0; i < len; i++) \n xorDP[i][i] = nums[i];\n \n for (int j = 1; j < len; j++) \n for (int i = j-1; i >= 0; i--) \n xorDP[i][j] = xorDP[i + 1][j] ^ xorDP[i][j - 1];\n for (int j = 1; j < len; j++) \n for (int i = j-1; i >= 0; i--) \n xorDP[i][j] = max(xorDP[i][j], max(xorDP[i+1][j], xorDP[i][j-1]));\n\n vector<int> result;\n for (auto query: queries)\n result.push_back(xorDP[query[0]][query[1]]);\n return result;\n }\n};",
"memory": "417135"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n #define pb push_back\n vector<int> maximumSubarrayXor(vector<int>& v, vector<vector<int>>& query) {\n int n=v.size();\n int p[n][n];memset(p,0,sizeof(p));\n vector<vector<int>>d(n);\n for(int i=0;i<n;i++){\n if(i==0){\n d[i]=v;\n }\n else{\n for(int j=0;j<d[i-1].size()-1;j++){\n d[i].pb(d[i-1][j]^d[i-1][j+1]);\n }\n }\n }\n for(int i=0;i<n;i++){\n for(int j=0;j<d[i].size();j++){\n p[j][j+i]=d[i][j];\n }\n }\n int dp[n][n];memset(dp,0,sizeof(dp));\n for(int i=n-1;i>=0;i--){\n for(int j=i;j<n;j++){\n if(i==j){\n dp[i][j]=p[i][j];\n }\n else{\n dp[i][j]=max({dp[i][j-1],dp[i+1][j],p[i][j]});\n }\n }\n }\n vector<int>ans;\n for(auto &e:query){\n int l=e[0],r=e[1];\n ans.push_back(dp[l][r]);\n }\n return ans;\n }\n};",
"memory": "425674"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n vector <vector <int>> grid;\n grid.push_back(nums);\n while(grid.back().size()>1){\n vector <int> arr = grid.back();\n for(int i = 0;i<arr.size()-1;i++) arr[i]^=arr[i+1];\n arr.pop_back();\n grid.push_back(arr);\n }\n\n int n = nums.size();\n vector <vector <int>> dp(n,vector <int>(n,0));\n for(int i= 0;i<n;i++) dp[i][i] = nums[i];\n for(int len = 2;len<=n;len++){\n for(int i= 0;i<n;i++){\n int j = i+len-1;\n if(j>=n) break;\n dp[i][j]= max({dp[i+1][j],dp[i][j-1],grid[len-1][i]});\n }\n }\n\n vector <int> ans;\n for(auto &q:queries){\n int l= q[0],r = q[1];\n ans.push_back(dp[l][r]);\n }\n return ans;\n }\n};",
"memory": "428520"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n vector <vector <int>> grid;\n grid.push_back(nums);\n while(grid.back().size()>1){\n vector <int> arr = grid.back();\n for(int i = 0;i<arr.size()-1;i++) arr[i]^=arr[i+1];\n arr.pop_back();\n grid.push_back(arr);\n }\n\n int n = nums.size();\n vector <vector <int>> dp(n,vector <int>(n,0));\n for(int i= 0;i<n;i++) dp[i][i] = nums[i];\n for(int len = 2;len<=n;len++){\n for(int i= 0;i<n;i++){\n if(i+len-1>=n) break;\n int j = i+len-1;\n dp[i][j]= max({dp[i+1][j],dp[i][j-1],grid[len-1][i]});\n }\n }\n\n vector <int> ans;\n for(auto &q:queries){\n int l= q[0],r = q[1];\n ans.push_back(dp[l][r]);\n }\n return ans;\n }\n};",
"memory": "431366"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n vector<vector<int>> dp;\n dp.push_back(nums);\n for(int i=1;i<n;i++){\n vector <int> temp;\n for(int j=1;j<dp[i-1].size();j++){\n int x=dp[i-1][j-1];\n int y=dp[i-1][j];\n //cout<<x<<\" \"<<y<<\" \"<<(x^y)<<endl;\n temp.push_back(x^y);\n }\n dp.push_back(temp);\n }\n for(int i=1;i<dp.size();i++){\n for(int j=0;j<dp[i].size();j++){\n dp[i][j]=max(dp[i][j],dp[i-1][j]);\n dp[i][j]=max(dp[i][j],dp[i-1][j+1]);\n //cout<<dp[i][j]<<\" \";\n }\n //cout<<endl;\n }\n vector <int> answer;\n for(auto &it:queries){\n int l=it[0];\n int r=it[1];\n //cout<<l<<\" \"<<r<<endl;\n answer.push_back(dp[r-l][l]);\n }\n return answer;\n }\n};",
"memory": "431366"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& A, vector<vector<int>>& queries) {\n vector<vector<int>> B = { A };\n \n while (B.back().size() != 1) {\n vector<int> nextLevel;\n for (size_t i = 0; i < B.back().size() - 1; ++i) {\n nextLevel.push_back(B.back()[i] ^ B.back()[i + 1]);\n }\n B.push_back(nextLevel);\n }\n \n for (size_t i = 1; i < B.size(); ++i) {\n for (size_t j = 0; j < B[i].size(); ++j) {\n B[i][j] = max({B[i][j], B[i - 1][j], B[i - 1][j + 1]});\n }\n }\n \n vector<int> result;\n for (const auto& query : queries) {\n int l = query[0];\n int r = query[1];\n result.push_back(B[r - l][l]);\n }\n \n return result;\n }\n};",
"memory": "434213"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "\nclass Solution {\npublic:\n std::vector<int> maximumSubarrayXor(std::vector<int>& A, std::vector<std::vector<int>>& queries) {\n std::vector<std::vector<int>> B = {A};\n \n while (B.back().size() != 1) {\n std::vector<int> newRow;\n for (size_t i = 0; i < B.back().size() - 1; ++i) {\n newRow.push_back(B.back()[i] ^ B.back()[i + 1]);\n }\n B.push_back(newRow);\n }\n \n for (size_t i = 1; i < B.size(); ++i) {\n for (size_t j = 0; j < B[i].size(); ++j) {\n B[i][j] = std::max({B[i][j], B[i-1][j], B[i-1][j+1]});\n }\n }\n \n std::vector<int> result;\n for (const auto& query : queries) {\n int l = query[0], r = query[1];\n result.push_back(B[r-l][l]);\n }\n \n return result;\n }\n};\n",
"memory": "434213"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n std::vector<int> maximumSubarrayXor(std::vector<int>& nums, std::vector<std::vector<int>>& queries) {\n int n = nums.size();\n std::vector<std::vector<int>> dp(n);\n\n dp[0] = nums;\n \n for (int i = 1; i < n; ++i) {\n std::vector<int> xorLevel;\n for (int j = 0; j < nums.size() - 1; ++j) {\n xorLevel.push_back(nums[j] ^ nums[j + 1]); // Compute XOR for current level\n }\n nums = xorLevel;\n dp[i] = nums;\n \n // Update maximum values in dp\n for (int j = 0; j < nums.size(); ++j) {\n dp[i][j] = std::max({nums[j], dp[i-1][j], (j+1 < dp[i-1].size()) ? dp[i-1][j+1] : INT_MIN});\n }\n }\n\n std::vector<int> result;\n for (auto& q : queries) {\n int l = q[0], r = q[1];\n result.push_back(dp[r - l][l]);\n }\n\n return result;\n }\n};\n",
"memory": "437059"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> dp(n);\n\n dp[0] = nums;\n \n for (int i = 1; i < n; ++i) {\n vector<int> xorLevel;\n for (int j = 0; j < nums.size() - 1; ++j) {\n xorLevel.push_back(nums[j] ^ nums[j + 1]); \n }\n nums = xorLevel;\n dp[i] = nums;\n \n // Update maximum values in dp\n for (int j = 0; j < nums.size(); ++j) {\n dp[i][j] = max({nums[j], dp[i-1][j], (j+1 < dp[i-1].size()) ? dp[i-1][j+1] : INT_MIN});\n }\n }\n\n vector<int> result;\n for (auto& q : queries) {\n int l = q[0], r = q[1];\n result.push_back(dp[r - l][l]);\n }\n return result;\n }\n};",
"memory": "437059"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n pair<int,int> find(int i, int j, vector<int>&nums, vector<vector<pair<int, int>>>&dp){\n if(i == j)return {nums[i], nums[i]};\n\n if(dp[i][j].first!=-1)return dp[i][j];\n\n pair<int, int> left = find(i, j-1, nums, dp);\n pair<int, int> right = find(i+1, j, nums, dp); \n\n int xori = left.first ^ right.first;\n int maxi = max({xori, left.second, right.second});\n\n return dp[i][j] = {xori, maxi};\n }\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n vector<int>ans;\n int n = nums.size();\n vector<vector<pair<int, int>>>dp(n, vector<pair<int, int>>(n, {0, 0}));\n // for(auto it : queries){\n // pair<int,int> res = find(it[0], it[1], nums, dp);\n // ans.push_back(res.second);\n // }\n\n for(int i = 0; i<n; i++){\n dp[i][i] = {nums[i], nums[i]};\n } \n\n for(int d = 1; d<n; d++){\n int i = 0, j = i+d;\n while(j < n){\n\n pair<int, int> left = dp[i][j-1] ;\n pair<int, int> right = dp[i+1][j]; \n\n int xori = left.first ^ right.first;\n int maxi = max({xori, left.second, right.second});\n\n dp[i][j] = {xori, maxi};\n i++, j++;\n }\n }\n\n for(auto it : queries){\n int l = it[0], r = it[1];\n ans.push_back(dp[l][r].second);\n }\n return ans;\n }\n};",
"memory": "439905"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n vector<vector<int>> adj(n);\n adj[0] = nums;\n for(int i=1;i<n;i++){\n for(int j=0;j+1<adj[i-1].size();j++){\n adj[i].push_back(adj[i-1][j]^adj[i-1][j+1]);\n }\n }\n for(int i=1;i<n;i++){\n for(int j=0;j+1<adj[i-1].size();j++){\n adj[i][j]=max(adj[i][j],adj[i-1][j]);\n adj[i][j]=max(adj[i][j],adj[i-1][j+1]);\n }\n }\n vector<int> res;\n for(auto q:queries){\n int l = q[0], r = q[1];\n int sz = r - l;\n res.push_back(adj[sz][l]);\n }\n return res;\n\n }\n};",
"memory": "439905"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> xorVal( n + 1, vector<int> ( n + 1, 0 ) );\n for( int len = 1; len <= n; len++ )\n {\n for( int i = 0; i < n; i++ )\n {\n if( len == 1 )\n {\n xorVal[ i ][ len ] = nums[ i ];\n }\n else if( len + i > n )\n {\n xorVal[ i ][ len ] = 0;\n }\n else\n {\n xorVal[ i ][ len ] = xorVal[ i ][ len - 1 ] ^ xorVal[ i + 1 ][ len - 1 ];\n }\n }\n }\n\n vector<vector<int>> dp( n + 1, vector<int> ( n + 1, 0 ) );\n\n for( int len = 1; len <= n; len++ )\n {\n for( int i = 0; i < n; i++ )\n {\n if( len == 1 )\n {\n dp[ i ][ len ] = xorVal[ i ][ len ];\n }\n else\n {\n dp[ i ][ len ] = max( dp[ i + 1 ][ len - 1 ], max( dp[ i ][ len - 1 ], xorVal[ i ][ len ] ) );\n }\n }\n }\n int m = queries.size();\n vector<int> ans( m );\n for( int i = 0; i < m; i++ )\n {\n int l = queries[ i ][ 0 ];\n int r = queries[ i ][ 1 ];\n ans[ i ] = dp[ l ][ r - l + 1 ];\n }\n return ans;\n }\n};",
"memory": "442751"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> dp(n, vector<int>(n));\n vector<vector<int>> max_dp(n, vector<int>(n));\n for (int i = 0; i < n; i++) {\n dp[i][i] = nums[i];\n }\n for (int i = n-2; i >=0; i--) {\n for (int j = i+1; j < n; j++) {\n dp[i][j] = dp[i][j-1] ^ dp[i+1][j];\n }\n }\n for (int i = n-2; i >=0; i--) {\n for (int j = i+1; j < n; j++) {\n dp[i][j] = max(dp[i][j], max(dp[i][j-1], dp[i+1][j]));\n }\n }\n\n vector<int> ans;\n for (auto & q : queries) {\n ans.push_back(dp[q[0]][q[1]]);\n }\n return ans;\n }\n};",
"memory": "442751"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n vector<vector<pair<int,int>>>xor1(n);\n for(int i=0;i<n;i++){\n for(int j=0;j+i<n;j++){\n if(i==0){\n xor1[j].push_back({nums[j], nums[j]});\n }else{\n int val=(xor1[j][i-1].first^xor1[j+1][i-1].first);\n xor1[j].push_back({val, max({xor1[j][i-1].second, xor1[j+1][i-1].second, val})});\n }\n }\n }\n \n vector<int>ans;\n for(auto &q: queries){\n ans.push_back(xor1[q[0]][q[1]-q[0]].second);\n }\n\n return ans;\n }\n};",
"memory": "445598"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n struct Return_Value {\n int score;\n int max_score;\n Return_Value() {\n score = -1;\n max_score = -1;\n }\n Return_Value(int score, int max_score) : score(score), max_score(max_score) {}\n bool empty() {\n return score == -1;\n }\n };\n Return_Value XORScore(const vector<int>& nums, vector<vector<Return_Value>>& DP, int begin, int end) {\n if (end - begin == 1) {\n DP[begin][end] = Return_Value(nums[begin], nums[begin]);\n } else if (DP[begin][end].empty()) {\n Return_Value left = XORScore(nums, DP, begin, end - 1);\n Return_Value right = XORScore(nums, DP, begin + 1, end);\n int score = left.score ^ right.score;\n int max_score = max(max(left.max_score, right.max_score), score);\n DP[begin][end] = Return_Value(score, max_score);\n }\n //printf(\"%d:%d\\tscore: %d, max: %d\\n\", begin, end, DP[begin][end].score, DP[begin][end].max_score);\n return DP[begin][end];\n }\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n vector<vector<Return_Value>> DP(nums.size() + 1, vector<Return_Value>(nums.size() + 1, Return_Value()));\n vector<int> result;\n for (vector<int>& query : queries) { \n int start = query[0];\n int end = query[1] + 1;\n int max = XORScore(nums, DP, start, end).max_score;\n result.push_back(max);\n //printf(\"\\n\");\n }\n return result;\n }\n};",
"memory": "445598"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n #define ll long long\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<ll>>dp(n,vector<ll>(n,0));\n for(int i=0;i<n;i++){\n dp[i][i]=nums[i];\n }\n int sz=1;\n while(!nums.empty()){\n for(int i=0;i<nums.size()-1;i++){\n int j = i+sz;\n nums[i] ^= nums[i+1];\n dp[i][j] = max({1LL*nums[i] , dp[i][j-1] , dp[i+1][j]});\n }\n sz++;\n nums.pop_back();\n }\n // cout<<dp<<endl;\n // for(int i=0;i<n;i++){\n // for(int j=0;j<n;j++){\n // cout<<dp[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n\n vector<int>ans;\n for(auto &it:queries){\n int u = it[0];\n int v =it[1];\n ans.push_back(dp[u][v]);\n }\n return ans;\n }\n};",
"memory": "448444"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& q)\n {\n int n=nums.size();\n vector<vector<pair<int,int>>> w(n,vector<pair<int,int>>(n));\n for(int i=0;i<n;i++)\n {\n w[i][i]={nums[i],nums[i]};\n }\n for(int d=1;d<n;d++)\n {\n for(int i=0;i<n;i++)\n {\n if(i+d==n)\n {\n break;\n }\n w[i][i+d].first=max(w[i][i+d-1].first,w[i+1][i+d].first);\n w[i][i+d].second=w[i][i+d-1].second^w[i+1][i+d].second;\n w[i][i+d].first=max(w[i][i+d].first,w[i][i+d].second);\n }\n }\n vector<int> ans;\n for(auto &it:q)\n {\n int l=it[0],r=it[1];\n ans.push_back(w[l][r].first);\n }\n return ans;\n }\n};",
"memory": "451290"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "\nclass Solution {\npublic:\n pair<int,int> solve(int i,int j, vector<vector<pair<int,int>>>&dp,vector<int>&nums)\n {\n if(i==j)return {nums[i],nums[i]};\n if(dp[i][j]!=make_pair(-1,-1))return dp[i][j];\n pair<int,int> left=solve(i,j-1,dp,nums);\n pair<int,int> down=solve(i+1,j,dp,nums);\n int window=left.second^down.second;\n int maxi=max({window,left.first,down.first});\n return dp[i][j]={maxi,window};\n }\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) { \n int n=nums.size();\n vector<vector<pair<int,int>>>dp(n,vector<pair<int,int>>(n,{-1,-1}));\n\n vector<int>res;\n for(auto&i:queries)\n {\n res.push_back(solve(i[0],i[1],dp,nums).first);\n }\n \n return res;\n }\n};",
"memory": "451290"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& q) {\n int n = nums.size();\n vector<vector<int>> v(n);\n for(int i = 0; i < nums.size(); i++) {\n v[0].push_back(nums[i]);\n }\n for(int i = 1; i < n; i++) {\n for(int j = 0; j < v[i-1].size()-1; j++) {\n int x = v[i-1][j]^v[i-1][j+1];\n v[i].push_back(x);\n }\n }\n vector<vector<int>> dp(n+1,vector<int>(n+1));\n for(int i = n-1; i >= 0; i--) {\n int sz = n-v[i].size();\n for(int j = 0; j < v[i].size(); j++) {\n dp[j][j+sz] = v[i][j];\n }\n }\n for(int i = 1; i < n; i++) {\n for(int j = 0; j < n; j++) {\n if((j+i)==n) break;\n dp[j][j+i] = max(dp[j][j+i],max(dp[j+1][j+i],dp[j][j+i-1]));\n }\n }\n vector<int> ans;\n for(int i = 0; i < q.size(); i++) {\n int l = q[i][0], r = q[i][1];\n ans.push_back(dp[l][r]);\n }\n return ans;\n }\n};",
"memory": "454136"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n vector<vector<int>> pyramid(nums.size(), vector<int>());\n vector<vector<int>> maxor(nums.size(), vector<int>());\n pyramid[0] = nums;\n maxor[0] = nums;\n for (int i = 1; i < nums.size(); i++) {\n for (int j = 0; j < pyramid[i - 1].size() - 1; j++) {\n pyramid[i].push_back(pyramid[i - 1][j] ^ pyramid[i - 1][j + 1]);\n maxor[i].push_back(max(pyramid[i][j], max(maxor[i - 1][j], maxor[i - 1][j + 1])));\n }\n }\n vector<int> output;\n for (auto &q: queries) {\n output.push_back(maxor[q[1] - q[0]][q[0]]);\n }\n return output;\n }\n};",
"memory": "454136"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp[2000][2000];\n int find(int i,int n,vector<vector<int>>&v){\n if(i>n)return 0;\n if(dp[i][n]!=-1)return dp[i][n];\n return dp[i][n]=max({v[n-i][i],find(i+1,n,v),find(i,n-1,v)});\n }\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n memset(dp,-1,sizeof(dp));\n vector<vector<int>>v(n);\n v[0]=nums;\n for(int i=1;i<n;i++){\n for(int j=0;j<v[i-1].size()-1;j++){\n v[i].push_back(v[i-1][j]^v[i-1][j+1]);\n }\n }\n find(0,n-1,v);\n int m=queries.size();\n vector<int>ans(m,0);\n int i=0;\n for(auto a:queries){\n ans[i++]=dp[a[0]][a[1]];\n }\n return ans;\n }\n};",
"memory": "456983"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<long>> dp(n,vector<long>(n));\n\n for(int i = 0 ; i < n ; i++) dp[i][i] = nums[i];\n\n for(int i = 2 ; i <= n ; i++){\n for(int st = 0 ; st + i - 1 < n ; st++){\n int end = st + i - 1;\n dp[st][end] = dp[st][end-1]^dp[st+1][end];\n }\n }\n\n for(int i = 2 ; i <= n ; i++){\n for(int st = 0 ; st + i - 1 < n ; st++){\n int end = st + i - 1;\n dp[st][end] = max(dp[st][end] , max(dp[st][end-1],dp[st+1][end]));\n }\n }\n\n vector<int> ans;\n\n for(auto q : queries){\n int l = q[0] , r = q[1];\n ans.push_back(dp[l][r]);\n }\n\n return ans;\n }\n};",
"memory": "459829"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n const static int N = 2e3 + 1;\n\n pair<int, int> solve(int l, int r, vector<int>& a, vector<vector<pair<int, int>>>& dp){\n if (l == r){\n return dp[l][r] = {a[l], a[l]};\n }\n if (l > r) return {0, 0};\n\n if (dp[l][r].first != -1) return dp[l][r];\n\n int maxXorScore = 0, xorScore = 0;\n pair<int, int> leftPortion = solve(l, r - 1, a, dp);\n pair<int, int> rightPortion = solve(l + 1, r, a, dp);\n xorScore = leftPortion.second ^ rightPortion.second;\n maxXorScore = max({leftPortion.first, rightPortion.first, xorScore});\n\n return dp[l][r] = {maxXorScore, xorScore};\n }\n\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& q) {\n int n = a.size();\n vector<vector<pair<int, int>>> dp(n, vector<pair<int, int>> (n, {-1, -1}));\n solve(0, n - 1, a, dp);\n vector<int> ans;\n for (auto i: q){\n int x = i[0], y = i[1];\n ans.push_back(dp[x][y].first);\n }\n return ans;\n }\n};",
"memory": "459829"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<pair<int,int>> ans[2001],ans2[2001];\n int dp[2001][2001];\n \n #define dpp dp[l][r]\n int solve(int l, int r) {\n if(l>r) return 0;\n if(dpp!=-1) return dpp;\n int where=lower_bound(ans[l].begin(), ans[l].end(), make_pair(r+1,0))-ans[l].begin()-1;\n dpp=max(ans[l][where].second, solve(l+1, r));\n return dpp;\n }\n \n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& que) {\n memset(dp, -1, sizeof dp);\n int n=a.size();\n for(int i=0;i<n;i++) ans[i].push_back({a[i], i});\n for(int j=1;j<n;j++) {\n for(int i=0;i+j<n;i++) {\n ans[i].push_back({ans[i+1].back().first ^ ans[i].back().first, i+j});\n }\n }\n for(int i=0;i<n;i++) {\n for(const auto j: ans[i]) {\n if(ans2[i].empty()) ans2[i].push_back({j.second, j.first});\n else if(ans2[i].back().second < j.first) {\n ans2[i].push_back({j.second, j.first});\n }\n }\n }\n swap(ans, ans2);\n // for(int i=0;i<n;i++) {\n // int cur=a[i];\n // ans[i].push_back({i, cur});\n // for(int j=i+1;j<n;j++) {\n // cur=a[i]^a[j];\n // if(cur > ans[i].back().second) {\n // ans[i].push_back({j, cur});\n // }\n // }\n // for(auto who: ans[i]) cout<<who.first<<\" \"<<who.second<<endl;\n // cout<<\"===============\\n\";\n // }\n vector<int> fans;\n for(const auto& q: que) {\n fans.push_back(solve(q[0], q[1]));\n }\n return fans;\n }\n};",
"memory": "462675"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "#define faster ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)\nclass Solution {\npublic:\n // unordered_map<int, unordered_map<int, pair<int, int>>> dp;\n pair<int, int> helper(vector<int> &nums, int i, int j, vector<vector<pair<int, int>>> &dp) {\n if(i > j) return {0, 0};\n if(i == j) {\n return {nums[i], nums[i]};\n }\n if(dp[i][j].first != -1) return dp[i][j];\n auto [xor1, score1] = helper(nums, i + 1, j, dp);\n auto [xor2, score2] = helper(nums, i, j - 1, dp);\n return dp[i][j] = {xor1 ^ xor2, max({score1, score2, xor1 ^ xor2})};\n }\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n vector<int> ans;\n faster;\n int n = nums.size();\n vector<vector<pair<int, int>>> dp(n + 1, vector<pair<int, int>> (n + 1, {-1, -1}));\n for(auto i: queries) {\n ans.push_back(helper(nums, i[0], i[1], dp).second);\n }\n // pair<int, int> def = {0, 0};\n // for(int i = 0; i < n; i++) dp[i][i] = {nums[i], nums[i]};\n // for(auto q: queries) {\n // int start = q[0];\n // int end = q[1];\n // for(int i = n - 1; i >= start; i--) {\n // for(int j = 0; j <= end; j++) {\n // auto [xor1, score1] = i + 1 < n ? dp[i + 1][j] : def;\n // auto [xor2, score2] = j - 1 >= 0 ? dp[i][j - 1] : def;\n // dp[i][j] = {xor1 ^ xor2, max({score1, score2, xor1 ^ xor2})};\n // }\n // }\n // ans.push_back(dp[start][end].second);\n // }\n return ans;\n }\n};",
"memory": "462675"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n vector<vector<int>>xr(n, vector<int>(n)), mx(n, vector<int>(n));\n for(int i=0; i<n; i++){\n xr[i][i] = nums[i];\n mx[i][i] = nums[i];\n }\n \n for(int len=2; len<=n; len++){\n for(int st=0; st+len-1<n; st++){\n int end = st + len - 1;\n xr[st][end] = xr[st][end-1]^xr[st+1][end];\n mx[st][end] = max({xr[st][end], mx[st][end-1], mx[st+1][end]});\n }\n }\n \n vector<int>ans;\n for(auto it : queries){\n ans.push_back(mx[it[0]][it[1]]);\n }\n \n return ans;\n }\n};",
"memory": "465521"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n vector<vector<int>> xs(n,vector<int>(n)),mxs(n,vector<int>(n));\n for(int i=0;i<n;i++){ \n xs[i][i]=nums[i];\n mxs[i][i]=nums[i];\n }\n for(int len=2;len<=n;len++){\n for(int i=0;i+len-1<n;i++){\n int j=i+len-1;\n xs[i][j]=xs[i][j-1]^xs[i+1][j];\n mxs[i][j]=max({mxs[i][j-1],mxs[i+1][j],xs[i][j]});\n }\n }\n vector<int> ans;\n for(auto q:queries) ans.push_back(mxs[q[0]][q[1]]);\n return ans;\n }\n};",
"memory": "465521"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\n vector<int> v;\n vector<vector<int>> dp,dp2;\n int calc(int s,int e){\n if(s==e) return v[s];\n if(dp2[s][e]>=0) return dp2[s][e];\n dp2[s][e]=calc(s,e-1)^calc(s+1,e);\n return dp2[s][e];\n }\n int rec(int s,int e){\n if(s==e) return v[s];\n if(dp[s][e]>=0) return dp[s][e];\n else{\n dp[s][e]=max(dp2[s][e],max(rec(s,e-1),rec(s+1,e)));;\n return dp[s][e];\n }\n }\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n v=nums;\n int w=1,n=nums.size();\n vector<int> res;\n dp=vector<vector<int>>(n,vector<int>(n,-1));\n dp2=vector<vector<int>>(n,vector<int>(n,-1));\n for(int i=0;i<n;i++) dp2[i][i]=nums[i];\n while(w<=n){\n int s=0,e=w++;\n for(;e<nums.size();e++,s++){\n dp2[s][e]=dp2[s][e-1]^dp2[s+1][e];\n }\n }\n \n for(auto q:queries){\n res.push_back(rec(q[0],q[1]));\n }\n return res;\n }\n};",
"memory": "468368"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\n vector<int> v;\n vector<vector<int>> dp,dp2;\n int rec(int s,int e){\n if(s==e) return v[s];\n if(dp[s][e]>=0) return dp[s][e];\n else{\n dp[s][e]=max(dp2[s][e],max(rec(s,e-1),rec(s+1,e)));;\n return dp[s][e];\n }\n }\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n v=nums;\n int w=1,n=nums.size();\n vector<int> res;\n dp=vector<vector<int>>(n,vector<int>(n,-1));\n dp2=vector<vector<int>>(n,vector<int>(n,-1));\n for(int i=0;i<n;i++) dp2[i][i]=nums[i];\n while(w<=n){\n int s=0,e=w++;\n for(;e<nums.size();e++,s++){\n dp2[s][e]=dp2[s][e-1]^dp2[s+1][e];\n }\n }\n for(auto q:queries){\n res.push_back(rec(q[0],q[1]));\n }\n return res;\n }\n};",
"memory": "468368"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "// segtree from atcoder\n// under CC0 lincense\n// https://atcoder.github.io/ac-library/document_en/segtree.html\n\nunsigned int bit_ceil(unsigned int n) {\n unsigned int x = 1;\n while (x < (unsigned int)(n)) x *= 2;\n return x;\n}\n\nint countr_zero(unsigned int n) {\n return __builtin_ctz(n);\n}\n\ntemplate <class S, S (*op)(S, S), S (*e)()> struct segtree {\n public:\n segtree() : segtree(0) {}\n explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}\n explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {\n size = (int)bit_ceil((unsigned int)(_n));\n log = countr_zero((unsigned int)size);\n d = std::vector<S>(2 * size, e());\n for (int i = 0; i < _n; i++) d[size + i] = v[i];\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n\n void set(int p, S x) {\n assert(0 <= p && p < _n);\n p += size;\n d[p] = x;\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n\n S get(int p) const {\n assert(0 <= p && p < _n);\n return d[p + size];\n }\n\n S prod(int l, int r) const {\n assert(0 <= l && l <= r && r <= _n);\n S sml = e(), smr = e();\n l += size;\n r += size;\n\n while (l < r) {\n if (l & 1) sml = op(sml, d[l++]);\n if (r & 1) smr = op(d[--r], smr);\n l >>= 1;\n r >>= 1;\n }\n return op(sml, smr);\n }\n\n S all_prod() const { return d[1]; }\n\n template <bool (*f)(S)> int max_right(int l) const {\n return max_right(l, [](S x) { return f(x); });\n }\n template <class F> int max_right(int l, F f) const {\n assert(0 <= l && l <= _n);\n assert(f(e()));\n if (l == _n) return _n;\n l += size;\n S sm = e();\n do {\n while (l % 2 == 0) l >>= 1;\n if (!f(op(sm, d[l]))) {\n while (l < size) {\n l = (2 * l);\n if (f(op(sm, d[l]))) {\n sm = op(sm, d[l]);\n l++;\n }\n }\n return l - size;\n }\n sm = op(sm, d[l]);\n l++;\n } while ((l & -l) != l);\n return _n;\n }\n\n template <bool (*f)(S)> int min_left(int r) const {\n return min_left(r, [](S x) { return f(x); });\n }\n template <class F> int min_left(int r, F f) const {\n assert(0 <= r && r <= _n);\n assert(f(e()));\n if (r == 0) return 0;\n r += size;\n S sm = e();\n do {\n r--;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!f(op(d[r], sm))) {\n while (r < size) {\n r = (2 * r + 1);\n if (f(op(d[r], sm))) {\n sm = op(d[r], sm);\n r--;\n }\n }\n return r + 1 - size;\n }\n sm = op(d[r], sm);\n } while ((r & -r) != r);\n return 0;\n }\n\n private:\n int _n, size, log;\n std::vector<S> d;\n\n void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }\n};\n\nclass Solution {\n vector<vector<int>> getPascal(int n)\n {\n vector<vector<int>> arr(n);\n for (int line = 0; line < n; line++)\n {\n arr[line] = vector<int>(line+1, 1);\n for (int i = 0; i <= line; i++)\n {\n if (line == i || i == 0)\n arr[line][i] = 1;\n else\n arr[line][i] = (arr[line - 1][i - 1] + \n arr[line - 1][i]) % 2;\n }\n }\n return arr;\n }\n\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n //n = 30;\n vector<vector<int>> pascalTri = getPascal(n);\n //for (int i = 0; i < n; i++) {\n // for (int j = 0; j < pascalTri[i].size(); j++) {\n // cout << pascalTri[i][j] << \" \";\n // }\n // cout << endl;\n //}\n vector<vector<int>> sums(n, vector<int>(n+1, 0));\n vector<vector<int>> maxsums(n, vector<int>(n+1, 0));\n for (int i = 0; i < n; i++) {\n sums[i][1] = nums[i];\n }\n for (int len = 2; len <= n; len++) {\n int base = 1;\n while (base < len) {\n base *= 2;\n }\n base /= 2;\n for (int i = 0; i < n + 1 - len; i++) {\n int j = i + len - 1;\n sums[i][len] = (sums[i][len-base]) ^ (sums[i+base][len-base]);\n }\n }\n for (int i = 0; i < n; i++) {\n maxsums[i][1] = sums[i][1];\n }\n for (int len = 2; len <= n; len++) {\n for (int i = 0; i < n + 1 - len; i++){\n maxsums[i][len] = max(max(sums[i][len], maxsums[i][len-1]), maxsums[i+1][len-1]);\n }\n }\n vector<int> ret;\n for (vector<int>& q: queries) {\n int l = q[0];\n int r = q[1];\n int len = r - l + 1;\n //cout << l << \" \" << r << endl;\n ret.push_back(maxsums[l][len]);\n \n }\n //for (int i = 0; i < n; i++) {\n // for (int len = 1; len < n+1-i; len++) {\n // cout << maxsums[i][len] << \" \";\n // }\n // cout << endl;\n //}\n return ret;\n \n }\n};",
"memory": "471214"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\ntypedef long long int ll;\n ll doit(vector<int>&nums, vector<vector<ll> >&dp, int l, int r){\n if(l == r){\n return nums[l];\n }\n if(dp[l][r]!=-1)return dp[l][r];\n ll val = doit(nums,dp,l,r-1) ^ doit(nums,dp,l+1,r);\n dp[l][r] = val;\n return val;\n }\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n ll n = nums.size();\n vector<vector<ll> >dp(n+1, vector<ll>(n+1,-1));\n ll maxi[n+1][n+1];\n ll upto[n][n];\n for(ll i = 0;i<n;i++){\n maxi[i][i] = nums[i];\n upto[i][i] = nums[i];\n }\n for(ll i = n-2;i>=0;i--){\n for(ll j = n-1;j>i;j--){\n upto[i][j] = max(doit(nums,dp,i,j), upto[i+1][j]);\n }\n }\n for(ll i = 0;i<n;i++){\n for(ll j = i+1;j<n;j++){\n maxi[i][j] = max(maxi[i][j-1], upto[i][j]);\n }\n }\n vector<int>res;\n for(ll i = 0;i<queries.size();i++){\n ll x = queries[i][0];\n ll y = queries[i][1];\n res.push_back(maxi[x][y]);\n }\n return res;\n }\n};",
"memory": "474060"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> solve(vector<int>& v){\n vector<int> v1;\n for(int i=0;i<v.size()-1;i++){\n v1.push_back(v[i]^v[i+1]);\n }\n return v1;\n }\n\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& q) {\n int n = nums.size();\n vector<vector<int>>dp(n, vector<int>(n, -1));\n\n vector<int> v = nums;\n int cnt = 1;\n\n for(int i=0;i<v.size();i++){\n dp[i][i] = v[i];\n }\n\n while(v.size()>1){\n v = solve(v);\n for(int i=0;i<v.size();i++){\n dp[i][i+cnt] = v[i];\n }\n cnt++;\n }\n\n for(int i= 0;i<n;i++){\n int mx = -1;\n for(int j = i; j<n; j++){\n mx= max(dp[i][j], mx);\n dp[i][j] = mx;\n }\n }\n vector<int> ans;\n for(int i= 0; i<q.size(); i++){\n int l = q[i][0];\n int r = q[i][1];\n // cout<<l<<\" \"<<r<<endl;\n int mx = -1;\n for(int j = l; j<=r; j++){\n mx = max(dp[j][r], mx);\n }\n ans.push_back(mx);\n }\n\n\n\n return ans;\n }\n};\n\n// 32 48 17",
"memory": "474060"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& A, vector<vector<int>>& Q) {\n unordered_map<int, unordered_map<int, unordered_set<int>>> mp;\n for(int i = 0; i < Q.size(); i++) mp[Q[i][1]-Q[i][0]+1][Q[i][0]].insert(i);\n vector<int> res(Q.size()), B(A.size());\n for(int l = 1; l <= A.size(); l++ ) {\n for(int i = 0; i + l - 1 < A.size(); i++) {\n \n if(l > 1) A[i] ^= A[i+1];\n B[i] = (l == 1 ? A[i] : max({B[i], B[i+1], A[i]}));\n if(mp[l].find(i) != mp[l].end()) {\n for(auto idx : mp[l][i])\n res[idx] = B[i];\n //cout << i << \" \" << i + l-1 << \" ===>\" << idx << \" \" << B[i] << endl;\n }\n }\n }\n return res;\n }\n};",
"memory": "476906"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& A, vector<vector<int>>& Q) {\n unordered_map<int, unordered_map<int, unordered_set<int>>> mp;\n for(int i = 0; i < Q.size(); i++) mp[Q[i][1]-Q[i][0]+1][Q[i][0]].insert(i);\n vector<int> res(Q.size()), B(A.size());\n for(int l = 1; l <= A.size(); l++ ) {\n for(int i = 0; i + l - 1 < A.size(); i++) {\n if(l > 1) A[i] ^= A[i+1];\n B[i] = (l == 1 ? A[i] : max({B[i], B[i+1], A[i]}));\n if(mp[l].find(i) != mp[l].end()) for(auto idx : mp[l][i]) res[idx] = B[i];\n }\n }\n return res;\n }\n};",
"memory": "476906"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n vector<vector<int>> build(vector<int> nums) {\n vector<vector<int>> f(nums.size(), vector<int>(nums.size()));\n for(int i=0;i<nums.size();i++) {\n for(int j=0;j+i<nums.size();j++) {\n f[j][j+i] = nums[j];\n }\n for(int j=0;j+1<nums.size();j++) {\n nums[j] = nums[j]^nums[j+1];\n }\n }\n return f;\n }\n \n int calc(int l, int r, auto& f, auto& nums, auto& mem, auto& set) {\n int ret;\n if(l == r) ret = nums[l];\n else if(set[l][r]) return mem[l][r];\n else ret = max(f[l][r], max(calc(l,r-1,f,nums,mem,set), calc(l+1,r,f,nums,mem,set)));\n mem[l][r] = ret;\n set[l][r] = true;\n return ret;\n }\n \n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n auto f = build(nums);\n vector<vector<int>> mem(nums.size(),vector<int>(nums.size()));\n vector<vector<bool>> set(nums.size(),vector<bool>(nums.size()));\n vector<int> ret; \n for(auto& q: queries) {\n ret.push_back(calc(q[0],q[1],f,nums,mem,set));\n }\n return ret; \n }\n};",
"memory": "479753"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "\nint t[10000][10000];\nint m;\nint n;\n\nvector<vector<int>> a;\n\nvoid build_y(int vx, int lx, int rx, int vy, int ly, int ry) {\n if (ly == ry) {\n if (lx == rx) {\n t[vx][vy] = a[lx][ly];\n } else {\n t[vx][vy] = max(t[vx * 2][vy], t[vx * 2 + 1][vy]);\n }\n } else {\n int my = (ly + ry) / 2;\n build_y(vx, lx, rx, vy * 2, ly, my);\n build_y(vx, lx, rx, vy * 2 + 1, my + 1, ry);\n t[vx][vy] = max(t[vx][vy * 2], t[vx][vy * 2 + 1]);\n }\n}\n\nvoid build_x(int vx, int lx, int rx) {\n if (lx != rx) {\n int mx = (lx + rx) / 2;\n build_x(vx * 2, lx, mx);\n build_x(vx * 2 + 1, mx + 1, rx);\n }\n build_y(vx, lx, rx, 1, 0, m - 1);\n}\n\nint sum_y(int vx, int vy, int tly, int try_, int ly, int ry) {\n if (ly > ry) {\n return numeric_limits<int>::min();\n }\n if (ly == tly && try_ == ry) {\n return t[vx][vy];\n }\n int tmy = (tly + try_) / 2;\n return max(sum_y(vx, vy * 2, tly, tmy, ly, min(ry, tmy)),\n sum_y(vx, vy * 2 + 1, tmy + 1, try_, max(ly, tmy + 1), ry));\n}\n\nint sum_x(int vx, int tlx, int trx, int lx, int rx, int ly, int ry) {\n if (lx > rx) {\n return numeric_limits<int>::min();\n }\n if (lx == tlx && trx == rx) {\n return sum_y(vx, 1, 0, m - 1, ly, ry);\n }\n int tmx = (tlx + trx) / 2;\n return max(sum_x(vx * 2, tlx, tmx, lx, min(rx, tmx), ly, ry),\n sum_x(vx * 2 + 1, tmx + 1, trx, max(lx, tmx + 1), rx, ly, ry));\n}\n\nclass Solution {\n public:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n_ = nums.size();\n vector<vector<int>> tb(n_, vector<int>(n_));\n for (int i = 0; i < n_; ++i) {\n tb[i][i] = nums[i];\n }\n for (int d = 1; d < n_; ++d) {\n for (int i = 0; i < n_; ++i) {\n int j = i + d;\n if (j >= n_) {\n break;\n }\n tb[i][j] = (tb[i][j - 1] ^ tb[i + 1][j]);\n }\n }\n a = tb;\n // for (auto& v : a) {\n // for (auto c : v) {\n // cout << c << \" \";\n // }\n // cout << endl;\n // }\n cout << endl;\n m = n_;\n n = n_;\n build_x(1, 0, n - 1);\n // for (int i = 0; i < 100; ++i) {\n // for (int j = 0; j < 100; ++j) {\n // cout << t[i][j] << ' ';\n // }\n // cout << endl;\n // }\n vector<int> ret;\n for (auto& q : queries) {\n int l = q[0];\n int r = q[1];\n ret.push_back(sum_x(1, 0, n - 1, l, r, l, r));\n }\n return ret;\n }\n};\n",
"memory": "479753"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> xor_score(n,vector<int>(n+1,0));\n for(int len=1;len<=n;len++){\n for(int i=0;i<n-len+1;i++){\n if(len==1) xor_score[i][len] = nums[i];\n else xor_score[i][len] = xor_score[i][len-1] ^ xor_score[i+1][len-1];\n }\n }\n // for(int i=0;i<n;i++){\n // for(int j=1;j<=n;j++){\n // cout<<xor_score[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n vector<vector<int>> dp(n,vector<int>(n+1,0));\n for(int len=1;len<=n;len++){\n for(int i=0;i<n-len+1;i++){\n if(len==1) dp[i][len] = xor_score[i][len];\n else dp[i][len] = max({dp[i][len-1],dp[i+1][len-1],xor_score[i][len]});\n }\n }\n // for(int i=0;i<n;i++){\n // for(int j=1;j<=n;j++){\n // cout<<dp[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n int m = queries.size();\n vector<int> ans(m,0);\n for(int i=0;i<m;i++){\n int start = queries[i][0];\n int len = queries[i][1] - queries[i][0] + 1;\n ans[i] = dp[start][len];\n }\n return ans;\n }\n};",
"memory": "482599"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "auto speedup = []() {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n return 'a';\n}();\n\nclass Solution\n{\npublic:\n vector<int> maximumSubarrayXor(vector<int> &nums, vector<vector<int>> &queries)\n {\n vector<int> ans(queries.size(), 0);\n vector<vector<int>> dp(nums.size(), vector<int>(nums.size(), 0)),\n pre(nums.size(), vector<int>(nums.size(), 0));\n\n for (int s = nums.size() - 1; s >= 0; s--)\n {\n for (int e = s; e < nums.size(); e++)\n {\n if (s == e)\n dp[s][e] = pre[s][e] = nums[s];\n else\n pre[s][e] = pre[s][e - 1] ^ pre[s + 1][e];\n }\n }\n\n for (int s = nums.size() - 1; s >= 0; s--)\n {\n for (int e = s; e < nums.size(); e++)\n {\n if (s != e)\n dp[s][e] = max(pre[s][e], max(dp[s + 1][e], dp[s][e - 1]));\n }\n }\n\n for (int i = 0; i < queries.size(); i++)\n ans[i] = dp[queries[i][0]][queries[i][1]];\n\n return ans;\n }\n};",
"memory": "485445"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "static const int init = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\n// #define dbg(x) cout << #x << \":\" << (x) << endl\n#define dbg(x)\n\nclass Solution {\n vector<vector<int>> DP;\n vector<vector<int>> MP;\n\n int F(int i, int j, vector<int>& arr) {\n if (i == j)\n return DP[i][j] = arr[i];\n if (DP[i][j] != -1)\n return DP[i][j];\n DP[i][j] = F(i, j - 1, arr) ^ F(i + 1, j, arr);\n \n dbg(i);\n dbg(j);\n dbg(DP[i][j]);\n\n return DP[i][j];\n }\n\n int M(int i, int j, vector<int>& arr) {\n if (i == j)\n return MP[i][j] = arr[i];\n if (MP[i][j] != -1)\n return MP[i][j];\n\n int ans = max(M(i, j - 1, arr), M(i + 1, j, arr));\n\n MP[i][j] = max(ans, F(i, j, arr));\n \n dbg(i);\n dbg(j);\n dbg(MP[i][j]);\n\n return MP[i][j];\n }\n\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums,\n vector<vector<int>>& queries) {\n int n = nums.size(), q = queries.size();\n DP.resize(n, vector<int>(n, -1));\n MP.resize(n, vector<int>(n, -1));\n\n vector<int> ans(q);\n\n for (int i = 0; i < q; ++i) {\n auto& query = queries[i];\n int x = query[0];\n int y = query[1];\n\n ans[i] = M(x, y, nums);\n }\n\n return ans;\n }\n};",
"memory": "485445"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums,vector<vector<int>>& queries) {\n vector<vector<int>>tbl1(nums.size()+1,vector<int>(nums.size()+1,-1)),tbl2(nums.size()+1,vector<int>(nums.size()+1,-1));\n vector<int> ans;\n for(int i = 0;i < queries.size();i++)\n ans.push_back(f(queries[i][0],queries[i][1],nums,tbl1,tbl2));\n return ans;\n }\n int f(int left,int right,vector<int>& nums,vector<vector<int>>& tbl1,vector<vector<int>>& tbl2)\n {\n if(tbl1[left][right]!=-1)\n return tbl1[left][right];\n int ans = f2(left, right, nums,tbl2);\n if(left+1 <= right)\n ans = max(ans,f(left+1,right,nums,tbl1,tbl2));\n if(left <= right-1)\n ans = max(ans,f(left,right-1,nums,tbl1,tbl2));\n return tbl1[left][right] = ans;\n }\n int f2(int left,int right,vector<int>& nums,vector<vector<int>>& tbl2)\n {\n if(tbl2[left][right]!=-1)\n return tbl2[left][right];\n int ans = 0;\n if(left == right)\n return tbl2[left][right] = nums[left];\n ans = f2(left+1,right,nums,tbl2)^f2(left,right-1,nums,tbl2);\n return tbl2[left][right] =ans;\n }\n};",
"memory": "488291"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int f(int i,int j,vector<vector<int>> &prescore,vector<vector<int>> &dp){\n if(i>j) return INT_MIN;\n if(dp[i][j]!=-1) return dp[i][j];\n return dp[i][j]=max({f(i+1,j,prescore,dp),f(i,j-1,prescore,dp),prescore[i][j]});\n }\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n vector<vector<int>> prescore(n+1,vector<int>(n+1,0));\n vector<vector<int>> dp(n+1,vector<int>(n+1,-1));\n for(int i=n-1;i>=0;i--){\n for(int j=i;j<n;j++){\n if(i==j) prescore[i][j]=nums[i];\n else\n prescore[i][j]=prescore[i][j-1]^prescore[i+1][j];\n }\n }\n f(0,n-1,prescore,dp);\n vector<int>ans;\n for(int i=0;i<queries.size();i++){\n int u=queries[i][0];\n int v=queries[i][1];\n ans.push_back(dp[u][v]);\n }\n return ans;\n }\n};",
"memory": "488291"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n short i, j, n = nums.size();\n if(n == 1) return {nums[0]};\n\n int initialOp, nestedOp;\n vector<vector<int>> dp(n, vector<int>(n));\n\n for(i = 0; i < n; i++)\n dp[i][i] = nums[i];\n\n for(i = 0, j = 1; j != n || i != 1; i++, j++){\n if(j == n) j -= i - 1, i = 0;\n \n dp[i][j] = dp[i + 1][j] ^ dp[i][j - 1];\n }\n\n priority_queue<int> pq;\n\n for(i = 0; i < n; i++){\n for(j = i; j < n; j++){\n pq.push(dp[i][j]);\n dp[i][j] = pq.top();\n }\n pq = {};\n\n for(j = i; j >= 0; j--){\n pq.push(dp[j][i]);\n dp[j][i] = pq.top();\n }\n pq = {};\n }\n\n vector<int> res(queries.size());\n\n for(int i = 0, n = queries.size(); i < n; i++)\n res[i] = dp[queries[i][0]][queries[i][1]];\n\n return res;\n }\n};",
"memory": "491138"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "#define DEBUG(x...) { cout << \"(\" << #x << \")\" << \" = (\"; Print(x); }\ntemplate <typename T1> void Print(T1 t1) { cout << t1 << \" )\" << endl; }\ntemplate <typename T1, typename... T2>\nvoid Print(T1 t1, T2... t2) { cout << t1 << \",\"; Print(t2...); }\n#define ll long long\n#define sz(x) (int)x.size()\n#define vs vector<string>\n#define vc vector<char>\n#define vll vector<ll>\n#define vi vector<int>\n#define vb vector<bool>\n#define vvi vector<vi>\n#define vvb vector<vb>\n#define vvc vector<vc>\n#define pii pair<int, int>\n#define vpii vector<pii>\n#define si set<int>\n\n#define F first\n#define S second\n#define all(x) x.begin(), x.end()\n#define ub upper_bound\n#define lb lower_bound\n#define mp make_pair\n#define pb push_back\n#define po pop_back\n// #define MOD 1000000007\n#define INF52 (1LL<<52) // only use at 'll' datatypes !!\n#define INF INT_MAX\n#define PRIMES_TILL 100000\n#define FOR(i, a, b) for (int i = a; i < (int)(b); i++)\n#define FORd(i, a, b) for (int i = a; i >= b; i--)\n#define trav(a, x) for (auto &a : x)\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = sz(nums);\n vvi dpXor(n, vi(n));\n FORd(i, n-1, 0){\n FOR(j, i, n){\n if(i == j) dpXor[i][j] = nums[i];\n else{\n dpXor[i][j] = dpXor[i][j-1]^dpXor[i+1][j];\n }\n }\n }\n vvi maxXor(n, vi(n));\n FOR(i, 0, n){\n int maxVal = 0;\n FOR(j, i, n){\n maxVal = max(maxVal, dpXor[i][j]);\n maxXor[i][j] = maxVal;\n }\n }\n vi ans(sz(queries));\n int j=0;\n for(auto q : queries){\n int l = q[0], r = q[1];\n int maxVal = 0;\n FOR(i, l, r+1){\n maxVal = max(maxVal, maxXor[i][r]);\n }\n ans[j++] = maxVal;\n }\n return ans;\n }\n};",
"memory": "493984"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n vector<vector<int>>xor_vals(n+1,vector<int>(n+1));\n \n for(int len=1;len<=n;len++){\n for(int i=0;i<n;i++){\n if(len==1){\n xor_vals[i][len]=nums[i];\n }\n else{\n if(i+len>n){\n xor_vals[i][len]=0;\n }\n else\n xor_vals[i][len]=xor_vals[i][len-1]^xor_vals[i+1][len-1];\n }\n }\n }\n\n vector<vector<int>>dp(n+1,vector<int>(n+1));\n \n for(int len=1;len<=n;len++){\n for(int i=0;i<n;i++){\n if(len==1)dp[i][len]=xor_vals[i][len];\n else\n dp[i][len]=max({dp[i+1][len-1],dp[i][len-1],xor_vals[i][len]});\n }\n }\n\n vector<int>ans(queries.size());\n int x=0;\n for(auto i:queries){\n int st=i[0];\n int len=i[1]-i[0]+1;\n ans[x++]=dp[st][len];\n }\n\n return ans;\n }\n};",
"memory": "493984"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& arr, vector<vector<int>>& queries) {\n int n=arr.size();\n // [i][j] denotes subarray of length i+1, starting at index j\n vector<vector<int>> valueOfFunction(n, vector<int>(n, 0));\n for (int j = 0; j < n; j++) {\n valueOfFunction[0][j] = arr[j];\n }\n\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < n - i; j++) {\n valueOfFunction[i][j] = valueOfFunction[i - 1][j] ^ valueOfFunction[i - 1][j + 1];\n }\n }\n\n vector<vector<int>> dp = valueOfFunction;\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < n - i; j++) {\n dp[i][j] = max({dp[i][j], dp[i - 1][j], dp[i - 1][j + 1]});\n }\n }\n\n vector<int> ans;\n for (auto q:queries) {\n int l=q[0], r=q[1];\n ans.push_back(dp[r-l][l]);\n }\n return ans;\n }\n};",
"memory": "496830"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> temp(n, vector<int>(n));\n vector<vector<int>> final(n, vector<int>(n));\n for (int i = n - 1; i >= 0; --i) {\n temp[i][i] = nums[i];\n final[i][i] = nums[i];\n for (int j = i + 1; j < n; ++j) {\n temp[i][j] = temp[i][j - 1] ^ temp[i + 1][j];\n final[i][j] = max({temp[i][j], final[i][j - 1], final[i + 1][j]});\n }\n }\n vector<int> ans;\n for (auto q : queries) {\n int left = q[0], right = q[1];\n ans.push_back(final[left][right]);\n }\n return ans;\n }\n};",
"memory": "499676"
} |
3,551 | <p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p>
<ul>
<li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li>
<li>Remove the last element of <code>a</code>.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p>
<p><strong>Explanation:</strong></p>
<p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p>
<p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
<p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p>
<p><strong>Explanation:</strong></p>
<table height="70" width="472">
<thead>
<tr>
<th>Index</th>
<th>nums[l<sub>i</sub>..r<sub>i</sub>]</th>
<th>Maximum XOR Score Subarray</th>
<th>Maximum Subarray XOR Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>[0, 7, 3, 2]</td>
<td>[7]</td>
<td>7</td>
</tr>
<tr>
<td>1</td>
<td>[7, 3, 2, 8, 5]</td>
<td>[7, 3, 2, 8]</td>
<td>14</td>
</tr>
<tr>
<td>2</td>
<td>[3, 2, 8]</td>
<td>[3, 2, 8]</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>[3, 2, 8, 5, 1]</td>
<td>[2, 8, 5, 1]</td>
<td>14</td>
</tr>
<tr>
<td>4</td>
<td>[5, 1]</td>
<td>[5]</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 2000</code></li>
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2 </code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= n - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> dp(n, vector<int>(n));\n for(int i = 0; i < n; i++) dp[i][i] = nums[i];\n for(int L = 2; L <= n; L++){\n for(int i = 0; i <= n - L; i++){\n dp[i][i+L-1] = dp[i][i+L-2] ^ dp[i+1][i+L-1];\n }\n }\n\n vector<vector<int> > pre(n, vector<int> (n));\n for(int i = 0; i < n; i++) pre[i][i] = dp[i][i];\n for(int L = 2; L <= n; L++){\n for(int i = 0; i <= n-L; i++){\n pre[i][i+L-1] = max({dp[i][i+L-1], pre[i+1][i+L-1], pre[i][i+L-2]});\n }\n }\n\n vector<int> ans;\n for(auto it: queries){\n ans.push_back(pre[it[0]][it[1]]);\n }\n \n return ans;\n }\n};",
"memory": "499676"
} |
3,567 | <p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p>
<p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p>
<p>Return the <strong>binary</strong> representation of <code>date</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">date = "2080-02-29"</span></p>
<p><strong>Output:</strong> <span class="example-io">"100000100000-10-11101"</span></p>
<p><strong>Explanation:</strong></p>
<p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">date = "1900-01-01"</span></p>
<p><strong>Output:</strong> <span class="example-io">"11101101100-1-1"</span></p>
<p><strong>Explanation:</strong></p>
<p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>date.length == 10</code></li>
<li><code>date[4] == date[7] == '-'</code>, and all other <code>date[i]</code>'s are digits.</li>
<li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li>
</ul>
| 0 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef signed long long ll;\n\n#undef _P\n#define _P(...) (void)printf(__VA_ARGS__)\n#define FOR(x,to) for(x=0;x<(to);x++)\n#define FORR(x,arr) for(auto& x:arr)\n#define FORR2(x,y,arr) for(auto& [x,y]:arr)\n#define ALL(a) (a.begin()),(a.end())\n#define ZERO(a) memset(a,0,sizeof(a))\n#define MINUS(a) memset(a,0xff,sizeof(a))\ntemplate<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}\ntemplate<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}\n//-------------------------------------------------------\n\n\nclass Solution {\npublic:\n string convertDateToBinary(string date) {\n\t\tint h=(date[0]-'0')*1000+(date[1]-'0')*100+(date[2]-'0')*10+(date[3]-'0')*1;\n\t\tint m=(date[5]-'0')*10+(date[6]-'0')*1;\n\t\tint d=(date[8]-'0')*10+(date[9]-'0')*1;\n\t\tstring S;\n\t\twhile(d) {\n\t\t\tS+='0'+(d%2);\n\t\t\td/=2;\n\t\t}\n\t\tS+=\"-\";\n\t\twhile(m) {\n\t\t\tS+='0'+(m%2);\n\t\t\tm/=2;\n\t\t}\n\t\tS+=\"-\";\n\t\twhile(h) {\n\t\t\tS+='0'+(h%2);\n\t\t\th/=2;\n\t\t}\n\t\treverse(ALL(S));\n\t\treturn S;\n \n }\n};\n",
"memory": "7500"
} |
3,567 | <p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p>
<p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p>
<p>Return the <strong>binary</strong> representation of <code>date</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">date = "2080-02-29"</span></p>
<p><strong>Output:</strong> <span class="example-io">"100000100000-10-11101"</span></p>
<p><strong>Explanation:</strong></p>
<p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">date = "1900-01-01"</span></p>
<p><strong>Output:</strong> <span class="example-io">"11101101100-1-1"</span></p>
<p><strong>Explanation:</strong></p>
<p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>date.length == 10</code></li>
<li><code>date[4] == date[7] == '-'</code>, and all other <code>date[i]</code>'s are digits.</li>
<li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n int y = (date[0] - '0') * 1000 + (date[1] - '0') * 100 + (date[2] - '0') * 10 + (date[3] - '0');\n int m = (date[5] - '0') * 10 + (date[6] - '0');\n int d = (date[8] - '0') * 10 + (date[9] - '0');\n string res;\n while (d > 0) {\n res.push_back('0' + d % 2);\n d /= 2;\n }\n res.push_back('-');\n while (m > 0) {\n res.push_back('0' + m % 2);\n m /= 2;\n }\n res.push_back('-');\n while (y > 0) {\n res.push_back('0' + y % 2);\n y /= 2;\n }\n reverse(res.begin(), res.end());\n return res;\n }\n};",
"memory": "7600"
} |
3,567 | <p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p>
<p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p>
<p>Return the <strong>binary</strong> representation of <code>date</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">date = "2080-02-29"</span></p>
<p><strong>Output:</strong> <span class="example-io">"100000100000-10-11101"</span></p>
<p><strong>Explanation:</strong></p>
<p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">date = "1900-01-01"</span></p>
<p><strong>Output:</strong> <span class="example-io">"11101101100-1-1"</span></p>
<p><strong>Explanation:</strong></p>
<p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>date.length == 10</code></li>
<li><code>date[4] == date[7] == '-'</code>, and all other <code>date[i]</code>'s are digits.</li>
<li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string convertDateToBinary(string date) {\n string ans;\n int year = 1000 * (date[0] - '0') + 100 * (date[1] - '0') + 10 * (date[2] - '0') + (date[3] - '0');\n int mon = 10 * (date[5] - '0') + (date[6] - '0');\n int day = 10 * (date[8] - '0') + (date[9] - '0');\n ans += numToStr(year);\n ans += '-';\n ans += numToStr(mon);\n ans += '-';\n ans += numToStr(day);\n return ans;\n }\n string numToStr(int num) {\n string ans;\n int i = 1;\n while(i <= num) i = i << 1;\n i >>= 1;\n while(i > 0) {\n if(num - i >= 0)\n num -= i, ans += '1';\n else\n ans += '0';\n i = i >> 1;\n }\n return ans;\n }\n};",
"memory": "7900"
} |
3,567 | <p>You are given a string <code>date</code> representing a Gregorian calendar date in the <code>yyyy-mm-dd</code> format.</p>
<p><code>date</code> can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in <code>year-month-day</code> format.</p>
<p>Return the <strong>binary</strong> representation of <code>date</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">date = "2080-02-29"</span></p>
<p><strong>Output:</strong> <span class="example-io">"100000100000-10-11101"</span></p>
<p><strong>Explanation:</strong></p>
<p><span class="example-io">100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.</span></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">date = "1900-01-01"</span></p>
<p><strong>Output:</strong> <span class="example-io">"11101101100-1-1"</span></p>
<p><strong>Explanation:</strong></p>
<p><span class="example-io">11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>date.length == 10</code></li>
<li><code>date[4] == date[7] == '-'</code>, and all other <code>date[i]</code>'s are digits.</li>
<li>The input is generated such that <code>date</code> represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string inttoBinary(int val){\n string ans = \"\";\n while(val > 0){\n if(val % 2 == 1){\n ans += '1';\n }\n else{\n ans += '0';\n }\n val = val / 2;\n }\n reverse(ans.begin() , ans.end());\n return ans;\n }\n string convertDateToBinary(string date) {\n string ans = \"\";\n int val = 0;\n for(int i = 0 ; i < date.size() ; i++){\n if(date[i] == '-'){\n ans += inttoBinary(val) + '-';\n val = 0;\n }\n else{\n val = val * 10 + (date[i] - '0');\n }\n }\n\n ans += inttoBinary(val);\n return ans;\n }\n};",
"memory": "7900"
} |