# CodeForces 984D D. XOR-pyramid （区间DP）

D. XOR-pyramid
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

For an array  of length  we define the function  as

where  is bitwise exclusive OR.

For example,

You are given an array  and a few queries. Each query is represented as two integers  and . The answer is the maximum value of  on all continuous subsegments of the array .

Input

The first line contains a single integer  () — the length of .

The second line contains  integers  () — the elements of the array.

The third line contains a single integer  () — the number of queries.

Each of the next  lines contains a query represented as two integers  ().

Output

Print  lines — the answers for the queries.

Examples
input
Copy
3
8 4 1
2
2 3
1 2

output
Copy
5
12

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

output
Copy
60
30
12
3

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 , for second query — , for third — , for fourth — .

### 题意：（题意图解转自：点击打开链接）

给你一段长度为n序列，有q次询问；每次询问区间[L,R]内最大的f(x)是多少？

f(x)的含义：F(x)表示x个数在规定运算下的值;

F(1,2,4,8)=F(1⊕2,2⊕4,4⊕8)=F(3,6,12)=F(3⊕6,6⊕12)=F(5,10)=F(5⊕10)=F(15)=15

此区间有5个数，用F[1][5]表示，从图中最后一步可以得出规律：
F[1][5]=F[1][4]^F[2][5];

#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 5e3 + 5;
int f[maxn][maxn], dp[maxn][maxn], a[maxn];
int main()
{
int n, q;
while(~scanf("%d", &n))
{
memset(dp, 0, sizeof(dp));
memset(f, 0, sizeof(f));
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]), f[i][i] = a[i], dp[i][i] = a[i];
for(int len = 1; len <= n; len++)
{
for(int l = 1; l+len <= n; l++)
{
int r = l + len;
f[l][r] = f[l+1][r] ^ f[l][r-1];
dp[l][r] = max(f[l][r], max(dp[l+1][r], dp[l][r-1]));
}
}
int q;
cin >> q;
while(q--)
{
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", dp[l][r]);
}
}
return 0;
}