链接:https://ac.nowcoder.com/acm/contest/11171/B
来源:牛客网
小 Q 与异或
输入
10 10
1 2 3 4 5 6 7 8 9 10
1 2
1 3
1 5
2 6
2 8
2 10
3 7
8 10
6 9
1 7
输出
3
3
7
7
15
15
7
3
15
7
思路:
区间结果由区间内两个数决定的DP模板;
从小区间枚举到大区间,
代码
#include<bits/stdc++.h>
using namespace std;
const int N=5e3+10;
int n,m;
int f[N][N];
int a[N];
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
int l,r;
for(int i=1;i<=n;i++)
{
for(int j=i-1;j>=1;j--)
{
f[j][i]= max ( max( a[i]^a[j],f[j+1][i]),f[j][i-1]);
}
}
while(m--)
{
cin>>l>>r;
if(l==r)
{
cout<<0<<'\n';
}
else cout<<f[l][r]<<'\n';
}
}