区间dp——异或推公式

For an array b of length m we define the function f

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

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)=15

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

    .
Input

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

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

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

Each of the next q lines contains a query represented as two integers l, r (1≤l≤r≤n).
Output

    Print q lines — the answers for the queries.
Examples
    Input

    3
    8 4 1
    2
    2 3
    1 2

    Output

    5
    12

    Input

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

    Output

    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 [3,6], for second query — [2,5], for third — [3,4], for fourth — [1,2].

这道题就是推出来一段区间具体是怎么算出来的。

dp[l][r]=dp[l+1][r]^dp[l][r-1];

就是这个公式。当时比赛的时候公式推错了。

然后就很显然用区间dp来做了。

第二个代码是错的。因为我立即更新了。而事实上立即更新会让计算出来的i-j的值发生差异。

AC代码:


#include <bits/stdc++.h>
using namespace std;
const int maxn=5500;
int a[maxn];
int dp[maxn][maxn];
int sum[maxn];
int n;
int main()
{
    scanf("%d",&n);
    sum[0]=0;
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        dp[i][i]=a[i];
    }
    int i,j;
    for(int i=1;i<n;i++)
    {
        for(int l=1;l<=n-i;l++)
        {
            int r=l+i;
            dp[l][r]=dp[l+1][r]^dp[l][r-1];
        }
    }
    for(int i=1;i<n;i++)///区间长度
    {
        for(int l=1;l<=n-i;l++)///起点
        {
            int r=l+i;
            dp[l][r]=max(dp[l][r],max(dp[l+1][r],dp[l][r-1]));
           /// cout<<"l: "<<l<<"  r:"<<r<<"   "<<dp[l][r]<<endl;
        }
    }
    int m;
    int l,r;
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&l,&r);
        cout<<dp[l][r]<<endl;
    }
    return 0;
}

错误代码:

#include<bits/stdc++.h>
using namespace std;

long long dp[5010][5010];
int main()
{
    long long a[5005];
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        dp[i][i]=a[i];
    }
    for(int i=1;i<n;i++){///区间长度
        for(int shou=1;shou+i<=n;shou++){
            int end=shou+i;
            ///dp[shou][end]=dp[shou+1][end]^dp[shou][end-1];
            dp[shou][end]=max(max(dp[shou+1][end],dp[shou][end-1]),dp[shou][end]);
            cout<<"shou:"<<shou<<"   wei:"<<end<<"   "<<dp[shou][end]<<endl;
        }
    }
    int m,l,r;
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&l,&r);
        printf("%lld\n",dp[l][r]);
    }
    return 0;
}
ps:自己太菜了,昨天比赛居然爆0了。更要拼命努力了


求1~n区间的最大异或可以使用类似于求一段区间最大异或的方法。具体步骤如下: 1. 将1~n区间内的所有数以二进制形式插入到字典树中。 2. 对于每个数,从高位到低位依次匹配字典树上的节点,如果当前位为1,就往字典树的右子树走,否则就往左子树走。匹配完整个二进制数后,我们可以得到一个最大的异或值。 3. 对于1~n区间,我们可以将其中的数看作一个二进制数,然后从高位到低位依次匹配字典树上的节点,得到最大的异或。 时间复杂度为O(nlogC),其中n为区间长度,C为数的范围。以下是求1~n区间的最大异或的C++代码: ```c++ #include <iostream> using namespace std; const int MAXN = 100010; const int MAXBITS = 30; struct TrieNode { int cnt; int children[2]; } trie[MAXN * MAXBITS]; int root, node_cnt; void insert(int x) { int p = root; for (int i = MAXBITS - 1; i >= 0; i--) { int idx = (x >> i) & 1; if (!trie[p].children[idx]) { trie[p].children[idx] = ++node_cnt; } p = trie[p].children[idx]; trie[p].cnt++; } } int query(int x) { int p = root, res = 0; for (int i = MAXBITS - 1; i >= 0; i--) { int idx = (x >> i) & 1; if (trie[trie[p].children[idx ^ 1]].cnt > 0) { res += (1 << i); p = trie[p].children[idx ^ 1]; } else { p = trie[p].children[idx]; } } return res; } int main() { int n; cin >> n; root = 1; node_cnt = 1; for (int i = 1; i <= n; i++) { insert(i); } int ans = query(n); cout << ans << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值