For an array bb of length mm we define the function ff as
where ⊕⊕ is bitwise exclusive OR.
For example, 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)=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.
The first line contains a single integer nn (1≤n≤50001≤n≤5000) — the length of aa.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤230−10≤ai≤230−1) — the elements of the array.
The third line contains a single integer qq (1≤q≤1000001≤q≤100000) — the number of queries.
Each of the next qq lines contains a query represented as two integers ll, rr (1≤l≤r≤n1≤l≤r≤n).
Print qq lines — the answers for the queries.
3 8 4 1 2 2 3 1 2
5 12
6 1 2 4 8 16 32 4 1 6 2 5 3 4 1 2
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;
}