跳到正文

个人记录

Nikitosh 和异或

_OMA_约 4 分钟
原文
文章信息与版本

文章信息

发布
2021年6月5日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 4 分钟

版本

  1. v1当前

题面

设 lil_{i} 为以 ii 为结尾的区间中最大的一段异或,rir_{i} 为以 ii 为开头的区间中最大的一段异或值。

则有

li=max⁡(l[i−1],suml−1⊕sumr)l_{i}=\max\left(l[i-1],sum_{l-1}\oplus sum_{r}\right) ri=max⁡(r[i+1],suml−1⊕sumr)r_{i}=\max\left(r[i+1],sum_{l-1}\oplus sum_{r}\right)

sumisum_{i} 为异或前缀和,跟前缀和是差不多的,就是运算的方式改成了异或。

最后的答案则为

ans=max⁡(ans,li+ri+1)ans=\max\left(ans,l_{i}+r_{i+1}\right)

Code:

C++
#include<cstdio>
#define MAX 400001
#define re register
namespace OMA
{
   int n;
   int l[MAX],r[MAX];
   int sum[MAX],num[MAX];
   struct Trie
   {
     int tot;
     int ch[MAX*31][2];
     void begin()
     {
       tot = 0;
       for(re int i=0; i<=n*31; i++)
       {
         for(re int j=0; j<=1; j++)
         { ch[i][j] = 0; }
       }
     }
     inline void insert(int x)
     {
       int u = 0;
       for(re int i=30; i>=0; i--)
       {
         int pos = (x>>i)&1;
         if(!ch[u][pos])
         { ch[u][pos] = ++tot; }
         u = ch[u][pos];
       }
     }
     inline int query(int x)
     {
       int u = 0,ans = 0;
       for(re int i=30; i>=0; i--)
       {
         int pos = (x>>i)&1;
         if(ch[u][pos^1])
         { u = ch[u][pos^1],ans += 1<<i; }
         else
         { u = ch[u][pos]; }
       }
       return ans;
     }
   }tree;
   inline int max(int a,int b)
   { return a>b?a:b; }
   inline int read()
   {
     int s=0,w=1; char ch=getchar();
     while(ch<'0'||ch>'9'){ if(ch=='-')w=-1; ch=getchar(); }
     while(ch>='0'&&ch<='9'){ s=s*10+ch-'0'; ch=getchar(); }
     return s*w;
   }
   signed main()
   {
     n=read();
     int ans = 0;
     for(re int i=1; i<=n; i++)
     { num[i] = read(); }
     for(re int i=1; i<=n; i++)
     { tree.insert(sum[i] ^= sum[i-1]^num[i]); }
     for(re int i=1; i<=n; i++)
     { l[i] = max(l[i-1],tree.query(sum[i])); }
     tree.begin();
     for(re int i=1; i<=n; i++)
     { sum[i] = 0; }
     for(re int i=n; i>=1; i--)
     { tree.insert(sum[i] ^= sum[i+1]^num[i]); }
     for(re int i=n; i>=1; i--)
     { r[i] = max(r[i+1],tree.query(sum[i])); }
     for(re int i=1; i<n; i++)
     { ans = max(ans,l[i]+r[i+1]); }
     printf("%d\n",ans);
     return 0;
   }
}
signed main()
{ return OMA::main(); }

Informatik    verbindet    dich    und    mich.Informatik\;\;verbindet\;\;dich\;\;und\;\;mich.

原文