XOR-pyramid CodeForces - 984D(区间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 bb of length mm we define the function ff as

f(b)={b[1]if m=1f(b[1]b[2],b[2]b[3],,b[m1]b[m])otherwise,f(b)={b[1]if m=1f(b[1]⊕b[2],b[2]⊕b[3],…,b[m−1]⊕b[m])otherwise,

where  is bitwise exclusive OR.

For example, f(1,2,4,8)=f(12,24,48)=f(3,6,12)=f(36,612)=f(5,10)=f(510)=f(15)=15f(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

You are given an array aa and a few queries. Each query is represented as two integers ll and rr. The answer is the maximum value of ff on all continuous subsegments of the array al,al+1,,aral,al+1,…,ar.

Input

The first line contains a single integer nn (1n50001≤n≤5000) — the length of aa.

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai23010≤ai≤230−1) — the elements of the array.

The third line contains a single integer qq (1q1000001≤q≤100000) — the number of queries.

Each of the next qq lines contains a query represented as two integers llrr (1lrn1≤l≤r≤n).

Output

Print qq 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


题意:给出f()操作,再给出一个长为n的数列,给出t次查询,每次查询给出l,r表示区间【l,r】中的子串对于f()操作的最大值。如果有一个串{1,2,3},那么,这个串对于f()操作的答案就是(1^2 )^(2^3)。

思路:区间dp。dp[l][r]即表示区间【l,r】的子串对于f()操作的最大值。我们可以按长度来算。如果令区间对于f()操作的值为f[l][r].那么,对于长为len的区间   dp[l][r]=max(f[l][r],max(dp[l][r-1],dp[l+1][r])),其中,dp[i][r-1],和dp[i+1][r]就表示长度比len小1的区间的子串的最大值。

#include "iostream"
using  namespace std;
const int Max=5e3+10;
int dp[Max][Max],f[Max][Max];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>dp[i][i];
        f[i][i]=dp[i][i];
    }
    for(int len=2;len<=n;len++)
        for(int l=1;l+len-1<=n;l++){
            int r=l+len-1;
            f[l][r]=f[l][r-1]^f[l+1][r];
            dp[l][r]=max(f[l][r],max(dp[l][r-1],dp[l+1][r]));
        }
    int t,l,r;
    cin>>t;
    while(t--){
        cin>>l>>r;
        cout<<dp[l][r]<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80529315
今日推荐