codeforce#483div2D-XOR-pyramid+DP

题意:求给定区间中最大的连续异或和;

思路:DP的思想,先dp求出每个区间的异或和,再dp更新成当前这个dp[i][j]和dp[i-1][j]、dp[i-1][j+1]中的最大值;

   这样可以保证是同一个区间亦或。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iterator>
#include <cmath>
using namespace std;

typedef long long ll;

const int maxn = 5009;
int n;
ll p,q,b;
ll dp[maxn][maxn];

int main(){
       scanf("%d", &n);
       for(int i=1; i<=n; i++)
       {
           scanf("%lld", &dp[1][i]);
       }
       for(int i=2; i<=n; ++i)
       {
           for(int j=1; j<=n-i+1; ++j)
           {
               dp[i][j] = dp[i-1][j]^dp[i-1][j+1];
           }
       }
  
       for(int i=2; i<=n; i++)
       {
           for(int j=1; j<=n-i+1;j++)
           {
               dp[i][j] = max(dp[i][j],max(dp[i-1][j],dp[i-1][j+1]));
           }
       }

       int q;
       scanf("%d", &q);
       for(int i=1; i<=q; i++)
       {
           int x,y;
           scanf("%d%d", &x,&y);
           printf("%lld\n",dp[y-x+1][x]);
       }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ckxkexing/p/9046513.html