题目链接

https://codeforces.com/contest/1208/problem/F

代码

#include<bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define x first
#define y second
#define iter iterator
#define riter reverse_iterator
#define y1 Lorem_ipsum_
#define tm dolor_sit_amet_
#define pii pair<int,int>
using namespace std;

{
int x = 0,f = 1; char ch = getchar();
for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
return x*f;
}

const int mxN = 1e6;
const int mxA = 1<<21;
const int lgA = 21;
int n;
int a[mxN+3];
pii f[mxA+3];

void updmax(pii &x,int y) {if(y==x.x||y==x.y) return; if(y>x.x) {x.y = x.x,x.x = y;} else if(y>x.y) {x.y = y;}}

int main()
{
for(int i=1; i<=n; i++) {updmax(f[a[i]],i);}
for(int j=0; j<lgA; j++)
{
for(int i=0; i<(1<<lgA); i++) if(i&(1<<j))
{
updmax(f[i^(1<<j)],f[i].x);
updmax(f[i^(1<<j)],f[i].y);
}
}
int ans = 0;
for(int i=n-2; i>=1; i--)
{
int cur = 0;
for(int j=lgA-1; j>=0; j--)
{
if(a[i]&(1<<j)) {continue;}
if(f[cur|(1<<j)].y>i) {cur |= (1<<j);}
}
ans = max(ans,cur|a[i]);
}
printf("%d\n",ans);
return 0;
}