XOR-pyramid (Codeforces 983B)
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):
from sys import stdin

def main():
    n = int(input())
    aa = list(map(int, input().split()))
    dp = [aa]
    for i in range(n - 1, 0, -1):
        aa = aa[:]
        for j in range(i):
            aa[j] ^= aa[j + 1]
        del aa[-1]
        dp.append(aa)
    aa = dp[0]
    for (i, bb) in enumerate(dp[1:], 1):
        a = aa[0]
        for (j, b) in enumerate(bb):
            c = aa[j + 1]
            bb[j] = max(a, b, c)
            a = c
        aa = bb
    input()
    res = stdin.read().splitlines()
    for (i, s) in enumerate(res):
        (lo, hi) = map(int, s.split())
        res[i] = str(dp[hi - lo][lo - 1])
    print('\n'.join(res))
if __name__ == '__main__':
    main()

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

Solution (Python):
import os
import sys
import threading
from io import BytesIO, IOBase
from heapq import heappush, heappop, heapify
from collections import defaultdict, deque, Counter
from bisect import bisect_left as bl
from bisect import bisect_right as br

def ri():
    return int(input())

def rs():
    return input()

def rl():
    return list(map(int, input().split()))

def rls():
    return list(input().split())

def main():
    n = ri()
    a = rl()
    dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
    for i in range(n):
        dp[0][i] = a[i]
    for lvl in range(1, n):
        for idx in range(n - lvl):
            dp[lvl][idx] = dp[lvl - 1][idx] ^ dp[lvl - 1][idx + 1]
    for lvl in range(1, n):
        for idx in range(n - lvl):
            dp[lvl][idx] = max(dp[lvl][idx], dp[lvl - 1][idx], dp[lvl - 1][idx + 1])
    for _ in range(ri()):
        (l, r) = rl()
        print(dp[r - l][l - 1])
    pass
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
    return sys.stdin.readline().rstrip('\r\n')
if __name__ == '__main__':
    main()