XOR-pyramid (Codeforces 984D)
Tags: DP, dp

For an array $$$b$$$ of length $$$m$$$ we define the function $$$f$$$ as  $$$ f(b) = \begin{cases} b[1] & \quad \text{if } m = 1 \\ f(b[1] \oplus b[2],b[2] \oplus b[3],\dots,b[m-1] \oplus b[m]) & \quad \text{otherwise,} \end{cases} $$$ where $$$\oplus$$$ is bitwise exclusive OR.For example, $$$f(1,2,4,8)=f(1\oplus2,2\oplus4,4\oplus8)=f(3,6,12)=f(3\oplus6,6\oplus12)=f(5,10)=f(5\oplus10)=f(15)=15$$$You are given an array $$$a$$$ and a few queries. Each query is represented as two integers $$$l$$$ and $$$r$$$. The answer is the maximum value of $$$f$$$ on all continuous subsegments of the array $$$a_l, a_{l+1}, \ldots, a_r$$$.

Input: The first line contains a single integer $$$n$$$ ($$$1 \le n \le 5000$$$) — the length of $$$a$$$.The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 2^{30}-1$$$) — the elements of the array.The third line contains a single integer $$$q$$$ ($$$1 \le q \le 100\,000$$$) — the number of queries.Each of the next $$$q$$$ lines contains a query represented as two integers $$$l$$$, $$$r$$$ ($$$1 \le l \le r \le n$$$).

Output: Print $$$q$$$ lines — the answers for the queries.

Note: In first sample in both queries the maximum value of the function is reached on the subsegment that is equal to the whole segment.In second sample, optimal segment for first query are $$$[3,6]$$$, for second query — $$$[2,5]$$$, for third — $$$[3,4]$$$, for fourth — $$$[1,2]$$$.

Sample Inputs:
3
8 4 1
2
2 3
1 2

6
1 2 4 8 16 32
4
1 6
2 5
3 4
1 2

Sample Outputs:
5
12

60
30
12
3

Solution (Python):
import sys
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
A = list(map(int, input().split()))
dp = [[0] * n for i in range(n)]
for i in range(n):
    dp[0][i] = A[i]
for i in range(1, n):
    for j in range(n - i):
        dp[i][j] = dp[i - 1][j] ^ dp[i - 1][j + 1]
for i in range(1, n):
    for j in range(n - i):
        dp[i][j] = max(dp[i][j], dp[i - 1][j], dp[i - 1][j + 1])
q = int(input())
for i in range(q):
    (l, r) = map(int, input().split())
    (l, r) = (l - 1, r - 1)
    print(dp[r - l][l])

Solution (Python):
from sys import stdin, stdout
n = int(stdin.readline())
a = list(map(int, stdin.readline().split(' ')))
list_a = [a]
for _ in range(n - 1):
    temp = []
    for j in range(1, len(list_a[-1])):
        temp.append(list_a[-1][j - 1] ^ list_a[-1][j])
    list_a.append(temp)
for j in range(1, len(list_a)):
    for k in range(len(list_a[j])):
        list_a[j][k] = max(list_a[j][k], list_a[j - 1][k], list_a[j - 1][k + 1])
q = int(stdin.readline())
for _ in range(q):
    (l, r) = map(int, stdin.readline().split(' '))
    stdout.write(str(list_a[r - l][l - 1]) + '\n')

Solution (Python):
import sys
n = int(input())
matriz = [[0] * n for i in range(n)]
maiores = matriz
entrada = list(map(int, sys.stdin.readline().split()))
for i in range(n):
    matriz[i][i] = entrada[i]
for i in range(n - 1, -1, -1):
    for j in range(i + 1, n):
        a = matriz[i][j - 1]
        b = matriz[i + 1][j]
        xor = a ^ b
        matriz[i][j] = xor
for i in range(n - 1, -1, -1):
    for j in range(i + 1, n):
        a = matriz[i][j - 1]
        b = matriz[i + 1][j]
        matriz[i][j] = max(matriz[i][j], a, b)
m = int(input())
for i in range(m):
    query = list(map(int, sys.stdin.readline().split()))
    print(matriz[query[0] - 1][query[1] - 1])